[clang] 38f0b9e - Fix scope of typedefs present inside a template class (#146729)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 21 13:58:29 PDT 2025
Author: ykhatav
Date: 2025-08-21T16:58:25-04:00
New Revision: 38f0b9e6d965b46a2c747a0010809fc6ba103045
URL: https://github.com/llvm/llvm-project/commit/38f0b9e6d965b46a2c747a0010809fc6ba103045
DIFF: https://github.com/llvm/llvm-project/commit/38f0b9e6d965b46a2c747a0010809fc6ba103045.diff
LOG: Fix scope of typedefs present inside a template class (#146729)
When a typedef is declared within a templated class, clang incorrectly
assigns the typedef to the compilation unit (CU) scope rather than the
intended scope of the templated class. This issue arises because, during
the creation of the typedef, the context lookup in the RegionMap fails
to locate the templated class, despite its prior creation.
The problem stems from the way the context is stored in the RegionMap.
When handling templated types, the current implementation stores the
class specialization rather than the templated declaration itself. This
leads to a mismatch when attempting to retrieve the context for the
typedef.
To address this issue, the solution involves modifying the
CreatedLimitedType() function. Specifically, when a struct or class is a
templated type, we should store the actual templated declaration in the
RegionMap instead of the class specialization. This ensures that
subsequent lookups for context, such as those needed for typedef
declarations, correctly identify the templated class scope.
Fixes https://github.com/llvm/llvm-project/issues/91451
Added:
clang/test/DebugInfo/CXX/dependent-template-type-scope.cpp
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/CodeGen/CGDebugInfo.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fe1dd15c6f885..6fe54f65656fc 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -229,6 +229,7 @@ Bug Fixes in This Version
cast chain. (#GH149967).
- Fixed a crash with incompatible pointer to integer conversions in designated
initializers involving string literals. (#GH154046)
+- Fixed scope of typedefs present inside a template class. (#GH91451)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 994bdbdae860f..c44fea3e6383d 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -4184,7 +4184,14 @@ llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
break;
}
- RegionMap[RD].reset(RealDecl);
+ if (auto *CTSD =
+ dyn_cast<ClassTemplateSpecializationDecl>(Ty->getOriginalDecl())) {
+ CXXRecordDecl *TemplateDecl =
+ CTSD->getSpecializedTemplate()->getTemplatedDecl();
+ RegionMap[TemplateDecl].reset(RealDecl);
+ } else {
+ RegionMap[RD].reset(RealDecl);
+ }
TypeCache[QualType(Ty, 0).getAsOpaquePtr()].reset(RealDecl);
if (const auto *TSpecial = dyn_cast<ClassTemplateSpecializationDecl>(RD))
diff --git a/clang/test/DebugInfo/CXX/dependent-template-type-scope.cpp b/clang/test/DebugInfo/CXX/dependent-template-type-scope.cpp
new file mode 100644
index 0000000000000..25a4d8741b01e
--- /dev/null
+++ b/clang/test/DebugInfo/CXX/dependent-template-type-scope.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -debug-info-kind=standalone -o - %s | FileCheck %s
+
+template <typename T = int>
+struct Y {
+ typedef int outside;
+ outside o;
+};
+
+Y<> y;
+
+// CHECK: ![[Y:.*]] = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "Y<int>", {{.*}}identifier: "_ZTS1YIiE")
+// CHECK: !DIDerivedType(tag: DW_TAG_typedef, name: "outside", scope: ![[Y]],
More information about the cfe-commits
mailing list