[PATCH] D50608: [Alt] Silence GCC warning about broken strict aliasing rules
Kim Gräsman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 12 06:03:02 PDT 2018
kimgr created this revision.
kimgr added reviewers: sammccall, xiangzhai, HaoLiu, llvm-commits.
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.
See also https://reviews.llvm.org/D50607.
Repository:
rL LLVM
https://reviews.llvm.org/D50608
Files:
include/llvm/Support/JSON.h
Index: include/llvm/Support/JSON.h
===================================================================
--- include/llvm/Support/JSON.h
+++ include/llvm/Support/JSON.h
@@ -452,7 +452,10 @@
new (reinterpret_cast<T *>(Union.buffer)) T(std::forward<U>(V)...);
}
template <typename T> T &as() const {
- return *reinterpret_cast<T *>(Union.buffer);
+ // Using this double static_cast<> via void * instead of reinterpret_cast<>
+ // silences a -Wstrict-aliasing warning from GCC.
+ void *p = static_cast<void *>(Union.buffer);
+ return *static_cast<T *>(p);
}
template <typename Indenter>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50608.160255.patch
Type: text/x-patch
Size: 616 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180812/10164854/attachment.bin>
More information about the llvm-commits
mailing list