This patch makes us deal with flexible array members and global
initializers correctly, thus for

struct s {
    int i;
    char c[];
} s = { 1, "01234" };

return 6 for the size of s.c instead of 0 (which is wrong, it
should have been reported as -1 instead - I didn't try to fix
that particular issue).

For the total size of the pointed-to object we can simply
use DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT if it is a decl.

Bootstrapped and tested on x86_64-unknown-linux-gnu, applied to trunk,
queued for 4.6 (it's a wrong-code bug, but not a regression).

Richard.

2011-05-19  Richard Guenther  <rguent...@suse.de>

        PR middle-end/48985
        * tree-object-size.c (addr_object_size): If the pointed-to
        variable is a decl use DECL_SIZE_UNIT instead of TYPE_SIZE_UNIT.

        * gcc.dg/builtin-object-size-11.c: New testcase.

Index: gcc/tree-object-size.c
===================================================================
*** gcc/tree-object-size.c      (revision 173900)
--- gcc/tree-object-size.c      (working copy)
*************** addr_object_size (struct object_size_inf
*** 205,210 ****
--- 205,216 ----
        pt_var_size = size_int (sz);
      }
    else if (pt_var
+          && DECL_P (pt_var)
+          && host_integerp (DECL_SIZE_UNIT (pt_var), 1)
+          && (unsigned HOST_WIDE_INT)
+               tree_low_cst (DECL_SIZE_UNIT (pt_var), 1) < offset_limit)
+     pt_var_size = DECL_SIZE_UNIT (pt_var);
+   else if (pt_var
           && (SSA_VAR_P (pt_var) || TREE_CODE (pt_var) == STRING_CST)
           && TYPE_SIZE_UNIT (TREE_TYPE (pt_var))
           && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (pt_var)), 1)
Index: gcc/testsuite/gcc.dg/builtin-object-size-11.c
===================================================================
*** gcc/testsuite/gcc.dg/builtin-object-size-11.c       (revision 0)
--- gcc/testsuite/gcc.dg/builtin-object-size-11.c       (revision 0)
***************
*** 0 ****
--- 1,20 ----
+ /* PR48985 */
+ /* { dg-do run } */
+ 
+ extern void abort (void);
+ 
+ struct s {
+     int i;
+     char c[];
+ } s = { 1, "01234" };
+ 
+ __SIZE_TYPE__ f (void) { return __builtin_object_size (&s.c, 0); }
+ 
+ int
+ main()
+ {
+   if (f() != sizeof ("01234"))
+     abort ();
+ 
+   return 0;
+ }

Reply via email to