[cfe-dev] new warnings n -r61596

Eli Friedman eli.friedman at gmail.com
Mon Jan 5 12:22:42 PST 2009


On Mon, Jan 5, 2009 at 9:14 AM, Douglas Gregor <dgregor at apple.com> wrote:
>
> On Jan 2, 2009, at 4:23 PM, Mike Stump wrote:
>
>> DeclCXX.cpp: In member function 'clang::Decl**
>> clang::LinkageSpecDecl::decls_begin() const':
>> DeclCXX.cpp:326: warning: dereferencing type-punned pointer will break
>> strict-aliasing rules
>> DeclCXX.cpp: In member function 'clang::Decl**
>> clang::LinkageSpecDecl::decls_end() const':
>> DeclCXX.cpp:331: warning: dereferencing type-punned pointer will break
>> strict-aliasing rules
>
> Thanks, Mike, but I'm a bit confused: I've turned on -Wstrict-aliasing
> and I don't see this warning, so I don't know how to suppress it.

I think you have to turn on optimizations for this warning to work.
(Sucks, but that's the way gcc works...)

> I believe that the code itself is correct (the void* decl is either
> handled as a Decl* or as a Decl**, depending on the HadBraces bit).

Not that it really matters anymore, since the code in question is
gone, but the code is wrong... the issue boils down to a testcase like
the following:

class Decl;
void* Decls;
Decl** x() {return (Decl**)&Decls;}
Decl* y() {return *x();}

Calling y() results in undefined behavior because the Decl** points to
a void*, and Decl* and void* aren't allowed to alias.  One correct
solution is using a declaration like "union {Decl **declArrayPtr,
*declPtr} Decls;" instead of "void *Decls;".

-Eli



More information about the cfe-dev mailing list