From: Jakub Jelinek Date: Tue, 17 Jan 2006 09:57:56 +0000 (+0100) Subject: re PR c/25682 (ICE when using old sytle offsetof (with non zero start) as array size) X-Git-Tag: releases/gcc-4.2.0~4818 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3aa2ddb8859364abdbe391467e91c0741a7fba3e;p=thirdparty%2Fgcc.git re PR c/25682 (ICE when using old sytle offsetof (with non zero start) as array size) PR c/25682 * 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. From-SVN: r109812 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 030333b3e075..ca2c55534c9d 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2006-01-17 Jakub Jelinek + + 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 * common.opt (ftoplevel-reorder): New option. diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index bda87890de55..e4428485dfeb 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -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); diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 34538932b49f..77bb00b61ccd 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2006-01-17 Jakub Jelinek + + 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 * parser.c: Include "cgraph.h". diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index d28481b94f0e..48e985c5f42a 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -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) { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index a69bdc302dd1..89b1693d060c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2006-01-17 Jakub Jelinek + + PR c/25682 + * gcc.dg/pr25682.c: New test. + * g++.dg/parse/array-size2.C: New test. + 2006-01-16 Ian Lance Taylor * 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 index 000000000000..22a57b2dc0bc --- /dev/null +++ b/gcc/testsuite/g++.dg/parse/array-size2.C @@ -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 index 000000000000..3a1d7c2ba598 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr25682.c @@ -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); +}