[clang] [AST] Avoid casting tagged DeclGroup pointer to Decl* in DeclGroupRef (PR #238331)

Romaric Jodin via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 1 00:31:51 PST 2026


================
@@ -120,9 +120,10 @@ class DeclGroupRef {
 
   void *getAsOpaquePtr() const { return D; }
   static DeclGroupRef getFromOpaquePtr(void *Ptr) {
-    DeclGroupRef X;
-    X.D = static_cast<Decl*>(Ptr);
-    return X;
+    if ((reinterpret_cast<uintptr_t>(Ptr) & Mask) == SingleDeclKind)
+      return DeclGroupRef(static_cast<Decl *>(Ptr));
+    return DeclGroupRef(reinterpret_cast<DeclGroup *>(
+        reinterpret_cast<uintptr_t>(Ptr) & ~Mask));
----------------
rjodinchr wrote:

Yes, `static_cast<Decl *>(void *)` (`CK_BitCast`) is checked by `-fsanitize=cfi-unrelated-cast`, whereas the `uintptr_t` cast (`CK_IntegralToPointer`) isn't.

Switched `DeclGroupRef` to `llvm::PointerUnion<Decl *, DeclGroup *>` (using `getAddrOfPtr1()` for single-decl iteration) and updated `clang.natvis` and the test. Note that this adds `#include "clang/AST/DeclBase.h"` to `DeclGroup.h` so `Decl` is complete for `PointerLikeTypeTraits<Decl *>`; let me know if you'd rather avoid that include.

https://github.com/llvm/llvm-project/pull/238331


More information about the cfe-commits mailing list