[clang] 8f7019a - [clang][bytecode] Fix getting typeid pointers of struct fields (#207946)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 7 04:19:15 PDT 2026


Author: Timm Baeder
Date: 2026-07-07T13:19:10+02:00
New Revision: 8f7019acdd29270a0a51006a6cbdb43d6844dd20

URL: https://github.com/llvm/llvm-project/commit/8f7019acdd29270a0a51006a6cbdb43d6844dd20
DIFF: https://github.com/llvm/llvm-project/commit/8f7019acdd29270a0a51006a6cbdb43d6844dd20.diff

LOG: [clang][bytecode] Fix getting typeid pointers of struct fields (#207946)

`getDeclPtr()` will return the declaration pointer, which might be
unrelated to the pointer we actually care about.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.cpp
    clang/lib/AST/ByteCode/Pointer.cpp
    clang/test/AST/ByteCode/typeid.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index ce044e7b47e11..0c502a745a20c 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -2744,7 +2744,7 @@ bool GetTypeidPtr(InterpState &S, CodePtr OpPC, const Type *TypeInfoType) {
   }
 
   // Pick the most-derived type.
-  CanQualType T = P.getDeclPtr().getType()->getCanonicalTypeUnqualified();
+  CanQualType T = P.stripBaseCasts().getType()->getCanonicalTypeUnqualified();
   // ... unless we're currently constructing this object.
   // FIXME: We have a similar check to this in more places.
   if (S.Current->getFunction()) {

diff  --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 4eb6df2f0d6a1..6d19379d6ecd7 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -801,7 +801,7 @@ bool Pointer::hasSameBase(const Pointer &A, const Pointer &B) {
   if (A.isFunctionPointer() && B.isFunctionPointer())
     return true;
   if (A.isTypeidPointer() && B.isTypeidPointer())
-    return true;
+    return A.asTypeidPointer().TypePtr == B.asTypeidPointer().TypePtr;
 
   if (A.StorageKind != B.StorageKind)
     return false;

diff  --git a/clang/test/AST/ByteCode/typeid.cpp b/clang/test/AST/ByteCode/typeid.cpp
index f529fb3b7a533..4e7664f505c23 100644
--- a/clang/test/AST/ByteCode/typeid.cpp
+++ b/clang/test/AST/ByteCode/typeid.cpp
@@ -93,3 +93,16 @@ namespace MissingInitalizer {
   constexpr auto &x = items[0].ti; // both-error {{must be initialized by a constant expression}} \
                                    // both-note {{initializer of 'items' is unknown}}
 }
+
+namespace TypeIdInOtherStruct {
+  struct X {
+    virtual constexpr ~X() {}
+  };
+  struct Y : X {};
+  struct Z {
+    mutable Y y;
+  };
+  constexpr Z z;
+  auto &zti = typeid(z.y);
+  static_assert(&zti == &typeid(Y));
+}


        


More information about the cfe-commits mailing list