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

Chris Lattner clattner at apple.com
Tue Feb 12 20:50:54 PST 2013


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.
> - 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,

-Chris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130212/76242852/attachment.html>


More information about the llvm-commits mailing list