[patch] Fix second part of PR c++/27601: ICE using offsetof with member functions
Volker Reichelt
reichelt@igpm.rwth-aachen.de
Mon Jun 5 11:54:00 GMT 2006
While applying
http://gcc.gnu.org/ml/gcc-patches/2006-06/msg00161.html
I noticed that the following invalid code snippet also triggers an ICE:
struct bar {
void foo();
};
int a = __builtin_offsetof(bar, foo);
bug.cc:5: internal compiler error: tree check: expected field_decl, have baselink in fold_offsetof_1, at c-common.c:5992
Please submit a full bug report, [etc.]
As the error message says, the problem is that the case COMPONENT_REF
in fold_offsetof_1 does not handle BASELINKs, but only FIELD_DECLs.
The following patch emits an appropriate error message in this case
and returns an error_mark_node to fix the ICE.
It would have been nicer if I could have used the test
"TREE_CODE (t) == BASELINK" or even "BASELINK_P (t)" instead of
"TREE_CODE (t) != FIELD_DECL", but BASELINK and BASELINK_P are
only available via cp-tree.h which is not included in c-common.c.
Well, the worst thing that *could* happen is that the error message
for something that cannot be handled by offsetof is slightly off
(because we assume it is a member function).
Bootstrapped and regtested on x86_64-unknown-linux-gnu.
Ok for mainline, 4.1 branch, and 4.0 branch?
Regards,
Volker
:ADDPATCH C++:
2006-06-05 Volker Reichelt <reichelt@igpm.rwth-aachen.de>
PR c++/27601
* c-common.c (fold_offsetof_1): Handle member functions.
===================================================================
--- gcc/gcc/c-common.c 2006-06-05 03:31:52 +0200
+++ gcc/gcc/c-common.c 2006-06-05 03:27:55 +0200
@@ -5989,6 +5989,11 @@ fold_offsetof_1 (tree expr)
return base;
t = TREE_OPERAND (expr, 1);
+ if (TREE_CODE (t) != FIELD_DECL)
+ {
+ error ("cannot apply %<offsetof%> to member function %qD", t);
+ return error_mark_node;
+ }
if (DECL_C_BIT_FIELD (t))
{
error ("attempt to take address of bit-field structure "
===================================================================
2006-06-03 Volker Reichelt <reichelt@igpm.rwth-aachen.de>
PR c++/27601
* g++.dg/ext/offsetof1.C: Test member functions.
===================================================================
--- gcc/gcc/testsuite/g++.dg/ext/offsetof1.C 2006-06-05 03:50:11 +0200
+++ gcc/gcc/testsuite/g++.dg/ext/offsetof1.C 2006-06-05 03:51:11 +0200
@@ -4,6 +4,9 @@
struct bar {
static int foo;
+ static int baz();
};
int a = __builtin_offsetof(bar, foo); // { dg-error "static data member" }
+int b = __builtin_offsetof(bar, baz); // { dg-error "member function" }
+int c = __builtin_offsetof(bar, ~bar); // { dg-error "member function" }
===================================================================
More information about the Gcc-patches
mailing list