[cfe-dev] warnings in clang

Mike Stump mrs at apple.com
Fri Nov 14 15:18:20 PST 2008


On Nov 14, 2008, at 2:55 PM, Argiris Kirtzidis wrote:
> Mike Stump wrote:
>> Here are the warnings from a recent build of clang (-r59324):
>>
>> DeclSerialization.cpp: In member function 'void   
>> clang::ScopedDecl::ReadInRec(llvm::Deserializer&,  
>> clang::ASTContext&)':
>> DeclSerialization.cpp:170: warning: dereferencing type-punned  
>> pointer  will break strict-aliasing rules
>> DeclSerialization.cpp:171: warning: dereferencing type-punned  
>> pointer  will break strict-aliasing rules
>
> I'm the one to blame for these. Care to explain more about what is  
> wrong and how to fix it ?

Hum, depends upon your background.  I'd assume you're new to the  
wording in the standards that says that you can only play with a type  
as the type (plus a few other obscure cases).  If you change your code  
to always play with variables of type X, as type X, you can never get  
this error.

> D.ReadUIntPtr(reinterpret_cast<uintptr_t&>(MDC->SemanticDC),  
> SemaDCPtrID);
>
> where 'MDC->SemanticDC' is a pointer (DeclContext*).

reinterpret_cast<uintptr_t&>?  Never use reinterpret_cast.

   uintptr_t x;
   D.ReadUIntPtr(x, SemaDCPtrID);

will read a uintptr_t.  This:

   MDC->SemanticDC = (DeclContext*)x;

will convert and store it.  Another way, if you wanted, would be to  
have a templated reader that can read any pointer:

template <T&>
myread(T t) {
   read (fd, (char*)&t, sizeof (t));

this works, because read can read into any type.



More information about the cfe-dev mailing list