[cfe-commits] r81251 - in /cfe/trunk: include/clang/AST/APValue.h lib/AST/APValue.cpp

Mike Stump mrs at apple.com
Tue Sep 8 14:16:28 PDT 2009


On Sep 8, 2009, at 1:22 PM, Douglas Gregor wrote:
> On Sep 8, 2009, at 1:16 PM, Mike Stump wrote:
>
>> On Sep 8, 2009, at 12:57 PM, Douglas Gregor wrote:
>>> Make sure to access APValue's data via a char array (rather than
>>> through an array of void*), so that we don't run afoul of the
>>> strict-aliasing rules in C++ 3.10p15.
>>
>>> Unfortunately, GCC 4.4 still complains about this code.
>>
>>> -    return *(APSInt*)(void*)Data;
>>> +    return *(APSInt*)(char*)Data;
>>
>> This doesn't match your description.  If you think this access is  
>> through a char to ensure this conforms to the standard...  I'd be  
>> happy to give you the aliasing rules class...  This is an access  
>> through a type of APSInt, which is unrelated to char.
>
>
> Where do I enroll?

You're enrolled.  :-)  Tomorrow I will fly out to your location,  
should be there by 11am.  I'll just wait around until you're ready  
then we can have the class.  I'll have to leave by 7pm to make the  
trip back home.

> Alternatively, do you see how to eliminate the strict-aliasing  
> violations in APValue? I'm clearly missing something.

In short:

union {
   int i;
   float f;
} *up, u;

up->i = 1;
up->f = 1.0;
u.i =1;
u.f = 1.0;

is the canonical way to do this.  As long as you only access the last  
type stored, this conforms to the language standards.  If you have a  
proclivity of doing this with other than the last type stored, this  
will work with gcc, as long as you leave the source code that does the  
access as up-> or u (as a gcc extension).  If you memcpy through a  
character array, this also conforms to the standard.  For example:

   i.i = 1;
   memmove (&i.f, &i.i, sizeof (i.f));
   printf (i.f);

is required to work by the standard (1).  The optimizer isn't allow to  
reorder the store of 1 past the read of the floating point value.


1 - This maybe contentious.  I don't think it is and in the C++  
standard, we refined the object model somewhat from c to clarify this.



More information about the cfe-commits mailing list