[llvm] r174827 - ok, ok, stop fighting type punning warnings by just using a union.

David Blaikie dblaikie at gmail.com
Tue Feb 12 21:10:22 PST 2013


On Tue, Feb 12, 2013 at 8:50 PM, Chris Lattner <clattner at apple.com> wrote:
>
> On Feb 10, 2013, at 12:45 AM, David Blaikie <dblaikie at gmail.com> wrote:
>
>> +    union {
>> +      uint8_t ArrayMember[sizeof(word_t)];
>> +      support::detail::packed_endian_specific_integral
>> +      <word_t, support::little, support::unaligned> EndianMember;
>> +    } buf = { { 0 } };
>>
>> -    typedef support::detail::packed_endian_specific_integral
>> -       <word_t, support::little, support::unaligned> Endian_T;
>> -    CurWord = *(Endian_T*)buf;
>> +    BitStream->getBitcodeBytes().readBytes(NextChar, sizeof(buf),
>> +                                           buf.ArrayMember, NULL);
>> +    // Handle big-endian byte-swapping if necessary.
>> +    CurWord = buf.EndianMember;
>
> This is still invalid C++ isn't it (UB to read from the non active member of
> a union)
>
> That's theoretically true, but all implementations (that I'm aware of at
> least) support this as implementation defined behavior.

True, true

> - why not just use memcpy, the language-sanctioned way to do this operation?
>
> No idea, this just didn't occur to me, I'll make it so!  Thanks David,

Thanks - sorry about the duplicate reply, didn't mean to pester just
couldn't find my original reply.

- David



More information about the llvm-commits mailing list