This is an archive of the discontinued LLVM Phabricator instance.

Silence GCC warning about broken strict aliasing rules
AbandonedPublic

Authored by kimgr on Aug 12 2018, 5:39 AM.

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.

Older GCCs might be incorrectly identifying this as a strict-aliasing
violation and undefined behavior, which could lead to misoptimizations
down the line. We've seen no evidence of such behavior.

No functional change intended.

Diff Detail

Repository
rL LLVM

Event Timeline

kimgr created this revision.Aug 12 2018, 5:39 AM
kimgr edited the summary of this revision. (Show Details)Aug 12 2018, 6:31 AM
kimgr abandoned this revision.Aug 12 2018, 9:24 AM

Better solution in D50608.