This is an archive of the discontinued LLVM Phabricator instance.

[Support][JSON][NFC] Silence GCC warning about broken strict aliasing rules
ClosedPublic

Authored by kimgr on Aug 12 2018, 6:02 AM.
Tokens
"Like" token, awarded by xiangzhai.

Details

Summary

The as<T>() method would trigger the following warning on GCC <7:

warning: dereferencing type-punned pointer will break
    strict-aliasing rules [-Wstrict-aliasing]

  return *reinterpret_cast<T *>(Union.buffer);
                                            ^

Union.buffer is guaranteed to be aligned to whatever types it contains,
and json::Value maintains the invariant that it only calls as<T>() for a
T it has previously placement-newed into Union.buffer. This should
follow the rules for strict aliasing.

Using two static_cast via void * instead of reinterpret_cast
silences the warning and presumably makes GCC understand that no
strict-aliasing violation is happening.

No functional change intended.

Patch by: kimgr (Kim Gräsman)

Diff Detail

Repository
rL LLVM

Event Timeline

kimgr created this revision.Aug 12 2018, 6:02 AM

I am in favour of this version.

xbolva00 accepted this revision.Aug 12 2018, 6:54 AM
This revision is now accepted and ready to land.Aug 12 2018, 6:54 AM
kimgr added a comment.Aug 12 2018, 6:59 AM

Thanks, me too!

Could you commit this for me, or should we maybe wait for @sammccall?

Yes, wait for his opinion too :)

sammccall accepted this revision.Aug 12 2018, 8:49 AM

I also prefer this version over suppressing the warning with the pragma.
Thanks!

include/llvm/Support/JSON.h
456 ↗(On Diff #160255)

Nit: seems worth mentioning that the warning is a false positive, and the upper bound on GCC version (so we know when it's obsolete)

457 ↗(On Diff #160255)

nit: I guess this should be P (or Storage)

kimgr updated this revision to Diff 160260.Aug 12 2018, 9:36 AM
kimgr retitled this revision from [Alt] Silence GCC warning about broken strict aliasing rules to Silence GCC warning about broken strict aliasing rules.
kimgr edited the summary of this revision. (Show Details)
  • Clarify comment
  • Fix naming
  • Update differential title/summary
kimgr added a comment.Aug 12 2018, 9:37 AM

If you're happy with this, could you also commit it for me? Thanks!

xbolva00 retitled this revision from Silence GCC warning about broken strict aliasing rules to [Support][JSON][NFC] Silence GCC warning about broken strict aliasing rules.Aug 12 2018, 10:31 AM
xbolva00 edited the summary of this revision. (Show Details)
This revision was automatically updated to reflect the committed changes.

If you're happy with this, could you also commit it for me? Thanks!

Yes, thanks!