This is GCC Bugzilla
This is GCC Bugzilla Version 2.20+
View Bug Activity | Format For Printing | Clone This Bug
In the gcc 3.4.0 prerelase, taking the offset of an array element works in gcc but not in g++. This does work in 3.3.x and earlier versions of g++. Version: g++ (GCC) 3.4.0 20040407 (prerelease) System: i686-pc-linux-gnu Red Hat 9 AthlonXP GCC Configuration: /usr2/stow/gcc/3.4.0_prerelease/gcc-3.4.0-20040406/configure --prefix=/usr2/stow/gcc/3.4.0_prerelease/ --enable-languages=c,c++ GCC Invocation: /usr2/stow/gcc/3.4.0_prerelease/bin/g++ tt.c GCC Output: tt.c: In function `int main(int, char**)': tt.c:16: error: an array reference cannot appear in a constant-expression Testcase (unpreprocessed): #include <stddef.h> #ifdef __cplusplus extern "C" int printf (__const char *__restrict __format, ...); #endif struct foobar { int bar; int foo[22]; }; int main(int argc, char **argv) { printf("%u\n", (unsigned)offsetof(struct foobar, foo[12])); } Testcase (preprocessed): # 1 "tt.c" # 1 "<built-in>" # 1 "<command line>" # 1 "tt.c" # 1 "/usr2/stow/gcc/3.4.0_prerelease/bin/../lib/gcc/i686-pc-linux-gnu/3.4.0/include/stddef.h" 1 3 4 # 151 "/usr2/stow/gcc/3.4.0_prerelease/bin/../lib/gcc/i686-pc-linux-gnu/3.4.0/include/stddef.h" 3 4 typedef int ptrdiff_t; # 213 "/usr2/stow/gcc/3.4.0_prerelease/bin/../lib/gcc/i686-pc-linux-gnu/3.4.0/include/stddef.h" 3 4 typedef unsigned int size_t; # 3 "tt.c" 2 extern "C" int printf (__const char *__restrict __format, ...); struct foobar { int bar; int foo[22]; }; int main(int argc, char **argv) { printf("%u\n", (unsigned)(__offsetof__ (reinterpret_cast <size_t> (&reinterpret_cast <char &> (static_cast<struct foobar *> (0)->foo[12]))))); }
I have a fix for 3.5.0 if this is a valid code as I think it is. But it is too invasive fix for 3.4.x.
Not sure how your fix looks like, but the correct fix looks to me a one liner in parser.c, where it checks if '[]' is allowed in constant expressions. The code is valid.
I added a one-liner check for !in_offsetof_p to the code in parser.c that detects the case of an array index and it does seem to fix the problem. Here's the patch that I tried. No regressions in the testsuite (tested on i686-pc-linux-gnu). Index: parser.c =================================================================== RCS file: /cvsroot/gcc/gcc/gcc/cp/parser.c,v retrieving revision 1.157.2.25 diff -u -c -r1.157.2.25 parser.c *** parser.c 19 Mar 2004 11:41:15 -0000 1.157.2.25 --- parser.c 27 Apr 2004 04:46:47 -0000 *************** *** 3705,3712 **** idk = CP_ID_KIND_NONE; /* Array references are not permitted in constant-expressions. */ ! if (cp_parser_non_integral_constant_expression ! (parser, "an array reference")) postfix_expression = error_mark_node; } break; --- 3705,3715 ---- idk = CP_ID_KIND_NONE; /* Array references are not permitted in constant-expressions. */ ! if (/* The "[]" operator is allowed in the implementation ! of "offsetof". */ ! !parser->in_offsetof_p ! && (cp_parser_non_integral_constant_expression ! (parser, "an array reference"))) postfix_expression = error_mark_node; } break;
That is the patch I was thinking of. You want to take care of asking for approval yourself? Otherwise I can take care of it.
Giovanni, please take care of this -- patches from unknown contributors are unfortunately too often ignored :-( W.
Giovanni, If you could request it, that'd be great! The patch is just based on your comment #2 and I thought it might be helpful to do the quick cut-n-paste and run the regression tests. I don't have all the paperwork in place to actually submit patches, however. I don't have a good idea what the dividing line between a trivial and a nontrivial patch is, but I do know that all I did was copy an if clause from elsewhere in that file, so I didn't really _write_ anything. And I suspect that Wolfgang's comment has truth to it as well. Thank you all for your help and comments and for your work on such a cool project!
Not that my patches are being reviewed that much lately, but I'll add this to the can ;)
*** Bug 15270 has been marked as a duplicate of this bug. ***
Patch submitted: http://gcc.gnu.org/ml/gcc-patches/2004-06/msg00004.html waiting for review (Mark?)
Subject: Bug 14932 CVSROOT: /cvs/gcc Module name: gcc Changes by: giovannibajo@gcc.gnu.org 2004-06-01 10:22:12 Modified files: gcc/cp : ChangeLog parser.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/g++.dg/parse: offsetof4.C Log message: PR c++/14932 * parser.c (cp_parser_postfix_expression): Allow subscript operator in offsetof. PR c++/14932 * g++.dg/parse/offsetof4.C: New test. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4072&r2=1.4073 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/parser.c.diff?cvsroot=gcc&r1=1.197&r2=1.198 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3801&r2=1.3802 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/parse/offsetof4.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
Subject: Bug 14932 CVSROOT: /cvs/gcc Module name: gcc Branch: gcc-3_4-branch Changes by: giovannibajo@gcc.gnu.org 2004-06-01 10:47:00 Modified files: gcc/cp : ChangeLog parser.c gcc/testsuite : ChangeLog Added files: gcc/testsuite/g++.dg/parse: offsetof4.C Log message: PR c++/14932 * parser.c (cp_parser_postfix_expression): Allow subscript operator in offsetof. PR c++/14932 * g++.dg/parse/offsetof4.C: New test. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3892.2.111&r2=1.3892.2.112 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/parser.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.157.2.29&r2=1.157.2.30 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3389.2.194&r2=1.3389.2.195 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/parse/offsetof4.C.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=NONE&r2=1.1.2.1
Fixed for 3.4.1 and 3.5.0. Thank you for your report (and for your patch)!
*** Bug 16868 has been marked as a duplicate of this bug. ***