Skip to content

Commit 507bbc4

Browse files
author
Thomas Preud'homme
committed
[AST][NFC] Silence GCC warning about broken strict aliasing rules
The deserialize() method would trigger the following warning on GCC <7: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] ParamIdx P(*reinterpret_cast<ParamIdx *>(&S)); ^ &S was previously reinterpret_casted from a ParamIdx into a SerialType, it is therefore safe to cast back into a ParamIdx. Similar to what was done in D50608, we replace it with two static_cast via void * which silences the warning and presumably makes GCC understand that no strict-aliasing violation is happening. No functional change intended. Reviewed By: aaron.ballman Differential Revision: https://reviews.llvm.org/D92384
1 parent c4a2222 commit 507bbc4

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,10 @@ class ParamIdx {
259259

260260
/// Construct from a result from \c serialize.
261261
static ParamIdx deserialize(SerialType S) {
262-
ParamIdx P(*reinterpret_cast<ParamIdx *>(&S));
262+
// Using this two-step static_cast via void * instead of reinterpret_cast
263+
// silences a -Wstrict-aliasing false positive from GCC7 and earlier.
264+
void *ParamIdxPtr = static_cast<void *>(&S);
265+
ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
263266
assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
264267
return P;
265268
}

0 commit comments

Comments
 (0)