OSDN Git Service

PR c/25682
authorjakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 17 Jan 2006 09:57:56 +0000 (09:57 +0000)
committerjakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 17 Jan 2006 09:57:56 +0000 (09:57 +0000)
* c-typeck.c (build_unary_op): Fold offsetof-like expressions
even when the pointer is not NULL.
cp/
* decl.c (compute_array_index_type): After issuing not an integral
constant-expression error, set size to 1 to avoid ICEs later on.
testsuite/
* gcc.dg/pr25682.c: New test.
* g++.dg/parse/array-size2.C: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@109812 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/c-typeck.c
gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/parse/array-size2.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr25682.c [new file with mode: 0644]

index 030333b..ca2c555 100644 (file)
@@ -1,3 +1,9 @@
+2006-01-17  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c/25682
+       * c-typeck.c (build_unary_op): Fold offsetof-like expressions
+       even when the pointer is not NULL.
+
 2006-01-16  Ian Lance Taylor  <ian@airs.com>
 
        * common.opt (ftoplevel-reorder): New option.
index bda8789..e442848 100644 (file)
@@ -3003,8 +3003,13 @@ build_unary_op (enum tree_code code, tree xarg, int flag)
         when we have proper support for integer constant expressions.  */
       val = get_base_address (arg);
       if (val && TREE_CODE (val) == INDIRECT_REF
-         && integer_zerop (TREE_OPERAND (val, 0)))
-       return fold_convert (argtype, fold_offsetof (arg));
+          && TREE_CONSTANT (TREE_OPERAND (val, 0)))
+       {
+         tree op0 = fold_convert (argtype, fold_offsetof (arg)), op1;
+
+         op1 = fold_convert (argtype, TREE_OPERAND (val, 0));
+         return fold_build2 (PLUS_EXPR, argtype, op0, op1);
+       }
 
       val = build1 (ADDR_EXPR, argtype, arg);
 
index 3453893..77bb00b 100644 (file)
@@ -1,3 +1,9 @@
+2006-01-17  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c/25682
+       * decl.c (compute_array_index_type): After issuing not an integral
+       constant-expression error, set size to 1 to avoid ICEs later on.
+
 2006-01-16  Ian Lance Taylor  <ian@airs.com>
 
        * parser.c: Include "cgraph.h".
index d28481b..48e985c 100644 (file)
@@ -6343,6 +6343,7 @@ compute_array_index_type (tree name, tree size)
               name);
       else
        error ("size of array is not an integral constant-expression");
+      size = integer_one_node;
     }
   else if (pedantic)
     {
index a69bdc3..89b1693 100644 (file)
@@ -1,3 +1,9 @@
+2006-01-17  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c/25682
+       * gcc.dg/pr25682.c: New test.
+       * g++.dg/parse/array-size2.C: New test.
+
 2006-01-16  Ian Lance Taylor  <ian@airs.com>
 
        * consistency.vlad: Remove entire directory, 1652 files.
diff --git a/gcc/testsuite/g++.dg/parse/array-size2.C b/gcc/testsuite/g++.dg/parse/array-size2.C
new file mode 100644 (file)
index 0000000..22a57b2
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c/25682
+// { dg-do compile }
+// Test whether we don't ICE on invalid array sizes.
+
+struct S
+{
+  char a[4];
+  int b;
+};
+
+extern void bar (char *, char *);
+
+void
+foo (void)
+{
+  char g[(char *) &((struct S *) 0)->b - (char *) 0];  // { dg-error "not an integral constant-expression" }
+  char h[(__SIZE_TYPE__) &((struct S *) 8)->b];                // { dg-error "not an integral constant-expression" }
+  bar (g, h);
+}
diff --git a/gcc/testsuite/gcc.dg/pr25682.c b/gcc/testsuite/gcc.dg/pr25682.c
new file mode 100644 (file)
index 0000000..3a1d7c2
--- /dev/null
@@ -0,0 +1,27 @@
+/* PR c/25682 */
+/* { dg-do compile } */
+/* Test whether we don't ICE on questionable constructs where offsetof
+   should have been used instead.  */
+
+struct S
+{
+  char a[4];
+  int b;
+};
+
+char c[(char *) &((struct S *) 0)->b - (char *) 0];
+char d[(__SIZE_TYPE__) &((struct S *) 8)->b];
+char e[sizeof (c) == __builtin_offsetof (struct S, b) ? 1 : -1];
+char f[sizeof (d) == __builtin_offsetof (struct S, b) + 8 ? 1 : -1];
+
+extern void bar (char *, char *);
+
+void
+foo (void)
+{
+  char g[(char *) &((struct S *) 0)->b - (char *) 0];
+  char h[(__SIZE_TYPE__) &((struct S *) 8)->b];
+  char i[sizeof (g) == __builtin_offsetof (struct S, b) ? 1 : -1];
+  char j[sizeof (h) == __builtin_offsetof (struct S, b) + 8 ? 1 : -1];
+  bar (g, h);
+}