[PATCH] D92384: [AST][NFC] Silence GCC warning about broken strict aliasing rules

Thomas Preud'homme via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 04:40:27 PST 2020


thopre created this revision.
thopre added reviewers: jdenny, aaron.ballman, hfinkel, llvm-commits.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
thopre requested review of this revision.

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 <https://reviews.llvm.org/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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92384

Files:
  clang/include/clang/AST/Attr.h


Index: clang/include/clang/AST/Attr.h
===================================================================
--- clang/include/clang/AST/Attr.h
+++ clang/include/clang/AST/Attr.h
@@ -259,7 +259,10 @@
 
   /// Construct from a result from \c serialize.
   static ParamIdx deserialize(SerialType S) {
-    ParamIdx P(*reinterpret_cast<ParamIdx *>(&S));
+    // Using this two-step static_cast via void * instead of reinterpret_cast
+    // silences a -Wstrict-aliasing false positive from GCC7 and earlier.
+    void *ParamIdxPtr = static_cast<void *>(&S);
+    ParamIdx P(*static_cast<ParamIdx *>(ParamIdxPtr));
     assert((!P.IsValid || P.Idx >= 1) && "valid Idx must be one-origin");
     return P;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92384.308619.patch
Type: text/x-patch
Size: 703 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201201/457e853b/attachment.bin>


More information about the cfe-commits mailing list