[cfe-dev] warnings in clang

Mike Stump mrs at apple.com
Fri Nov 14 15:51:00 PST 2008


On Nov 14, 2008, at 3:36 PM, Sebastian Redl wrote:
> Argiris Kirtzidis wrote:
>> Here's a similar case, doesn't gcc has a problem with this ?
>>
>>  template <typename T>
>>  void ReadPtr(T*& PtrRef, const SerializedPtrID& PtrID,
>>               bool AllowBackpatch = true) {
>>    ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef), PtrID,  
>> AllowBackpatch);
>>  }
>>
> It's a trick to avoid GCC's warning. Because the function takes a  
> reference and passes a reference on, the type-punned  
> "pointer" (reference, actually) is never "dereferenced" (accessed).  
> The actual ReadUIntPtr accesses it, but by then GCC has forgotten  
> the original type.

As I read that, it sounds like someone is praying and hoping and  
violating the language standard.  Don't do that.  We don't trick gcc  
into not complaining, if you do that, you _will_ lose.  One _must_  
write code that is type safe.  That isn't a trick.  I reviewed lib/ 
Bitcode/Reader/Deserialize.cpp Deserializer::ReadUIntPtr.  :-(  I  
don't see anything that is type safe about this code.

:-(

It is really easy to fix, for example:

PtrRef = 0;

becomes:

   uintptr_t x = 0;
   memcpy (&PtrRef, x, sizeof());

suddenly, perfectly type safe, and, not an instruction larger, not a  
single clock slower to boot.

Welcome to C.



More information about the cfe-dev mailing list