[llvm-dev] GCC 5 and -Wstrict-aliasing in JSON.h

Liu Hao via llvm-dev llvm-dev at lists.llvm.org
Tue Aug 14 04:06:41 PDT 2018


在 2018-08-14 17:51, Andrew Haley 写道:
> On 08/12/2018 02:19 PM, Kim Gräsman wrote:
>> I still feel a little uncomfortable, because I think Jonathan makes an
>> excellent point -- if GCC thinks there's a strict-aliasing violation
>> (whether the standard agrees or not) and classifies this as undefined
>> behavior, we might invoke the optimizers wrath. The warning is a nice
>> hint that this could happen.
> 
> Indeed.  And I'm not convinced that the pointer cast is necessary anyway:
> if the type passed in is a union, why not simply take the union member of
> the appropriate type?
> 

As this piece of code occurs in 'JSON.h' I presume it is sort of variant 
implementation, then handwritten specializations of this template, 
taking the member expected, might be a bit superfluous.

> I don't think that GCC would produce this warning unnecessarily.  You
> are in dangerous waters.
> 

It it still in doubt that whether a union can /provide storage for/ 
other objects. Other than that, I think it should be safe to assume the 
behavior is quite defined here, as long as the object constructed in the 
union is always referenced through the _correct_ type of reference (that 
is, a reference type to the dynamic type, or a type similar to the 
dynamic type, possibly cv-qualified or different only in sign-ness, of 
the object that has been constructed in the union), and the union itself 
is never accessed directly, because no rule in [basic.lval]-11 is ever 
violated.

-- 
Best regards,
LH_Mouse


More information about the llvm-dev mailing list