https://github.com/oltolm created 
https://github.com/llvm/llvm-project/pull/246914

Fix two cases where distinct types receive the same CodeView display name:

- On MinGW, retain the first declarator and typedef associated with each 
unnamed tag in `ItaniumCXXABI`. This allows names such as `<unnamed-type-one>` 
instead of `<unnamed-tag>`, enabling debuggers to resolve forward references 
and display members correctly.
- For MSVC and MinGW, compute record type identifiers before display names so 
lambda IDs are assigned before use. Lambdas stored in local variables now 
receive distinct names matching their identifiers.

Adds MinGW coverage to the unnamed-type and anonymous-tag tests, plus a 
regression test for distinct lambda names on both targets.

>From ba71ea3dbf762ed51c18ed433e394007f3a61ba6 Mon Sep 17 00:00:00 2001
From: Oleg Tolmatcev <[email protected]>
Date: Tue, 24 Nov 2026 20:33:00 +0100
Subject: [PATCH 1/2] [clang][CodeView] Preserve names for unnamed tags on
 MinGW

Record the first declarator and typedef associated with each unnamed tag
in ItaniumCXXABI, using canonical declarations for insertion and lookup.
Previously, these hooks were no-ops, leaving CodeView debug info without
the declarations needed to form names such as <unnamed-type-one>.

Without these names, all unnamed tags at the same scope are emitted as
<unnamed-tag>, so the debugger cannot match forward references to their
definitions. For example, with

  struct { int a; } one;
  typedef struct { int b; } *ptr_t;

cdb shows no members for *p of type ptr_t. With this change the types
are named <unnamed-type-one> and <unnamed-type-ptr_t> and resolve
correctly. Likewise, cdb shows no members for a field whose type is an
unnamed struct, and member functions of different unnamed structs all
appear as <unnamed-tag>::bar, so a breakpoint by name cannot select one.

Add MinGW coverage to the existing unnamed-type and anonymous-tag debug
info tests.

Assisted-by: OpenAI Codex
---
 clang/lib/AST/ItaniumCXXABI.cpp               | 23 +++++++++--
 clang/test/DebugInfo/CXX/codeview-unnamed.cpp | 41 +++++++++++++++++++
 clang/test/DebugInfo/CXX/ms-anonymous-tag.cpp |  1 +
 3 files changed, 61 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ItaniumCXXABI.cpp b/clang/lib/AST/ItaniumCXXABI.cpp
index 0bcc9394c84919..f3e1c01bcd42f8 100644
--- a/clang/lib/AST/ItaniumCXXABI.cpp
+++ b/clang/lib/AST/ItaniumCXXABI.cpp
@@ -178,6 +178,11 @@ class ItaniumSYCLNumberingContext : public 
ItaniumNumberingContext {
 class ItaniumCXXABI : public CXXABI {
 private:
   std::unique_ptr<MangleContext> Mangler;
+  llvm::SmallDenseMap<TagDecl *, DeclaratorDecl *>
+      UnnamedTagDeclToDeclaratorDecl;
+  llvm::SmallDenseMap<TagDecl *, TypedefNameDecl *>
+      UnnamedTagDeclToTypedefNameDecl;
+
 protected:
   ASTContext &Context;
 public:
@@ -228,17 +233,27 @@ class ItaniumCXXABI : public CXXABI {
                                             CXXConstructorDecl *CD) override {}
 
   void addTypedefNameForUnnamedTagDecl(TagDecl *TD,
-                                       TypedefNameDecl *DD) override {}
+                                       TypedefNameDecl *DD) override {
+    TD = TD->getCanonicalDecl();
+    DD = DD->getCanonicalDecl();
+    UnnamedTagDeclToTypedefNameDecl.insert({TD, DD});
+  }
 
   TypedefNameDecl *getTypedefNameForUnnamedTagDecl(const TagDecl *TD) override 
{
-    return nullptr;
+    return UnnamedTagDeclToTypedefNameDecl.lookup(
+        const_cast<TagDecl *>(TD->getCanonicalDecl()));
   }
 
   void addDeclaratorForUnnamedTagDecl(TagDecl *TD,
-                                      DeclaratorDecl *DD) override {}
+                                      DeclaratorDecl *DD) override {
+    TD = TD->getCanonicalDecl();
+    DD = cast<DeclaratorDecl>(DD->getCanonicalDecl());
+    UnnamedTagDeclToDeclaratorDecl.insert({TD, DD});
+  }
 
   DeclaratorDecl *getDeclaratorForUnnamedTagDecl(const TagDecl *TD) override {
-    return nullptr;
+    return UnnamedTagDeclToDeclaratorDecl.lookup(
+        const_cast<TagDecl *>(TD->getCanonicalDecl()));
   }
 
   std::unique_ptr<MangleNumberingContext>
diff --git a/clang/test/DebugInfo/CXX/codeview-unnamed.cpp 
b/clang/test/DebugInfo/CXX/codeview-unnamed.cpp
index b625a80313c38f..a2b43e3c1da936 100644
--- a/clang/test/DebugInfo/CXX/codeview-unnamed.cpp
+++ b/clang/test/DebugInfo/CXX/codeview-unnamed.cpp
@@ -1,6 +1,7 @@
 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -debug-info-kind=limited 
-emit-llvm -std=c++11 -o - %s | FileCheck --check-prefix LINUX %s
 // RUN: %clang_cc1 -triple x86_64-windows-msvc -debug-info-kind=limited 
-gcodeview -emit-llvm -std=c++11 -o - %s | FileCheck --check-prefix MSVC %s
 // RUN: %clang_cc1 -triple x86_64-uefi -debug-info-kind=limited -gcodeview 
-emit-llvm -std=c++11 -o - %s | FileCheck --check-prefix MSVC %s
+// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -debug-info-kind=limited 
-gcodeview -emit-llvm -std=c++11 -o - %s | FileCheck --check-prefix MINGW %s
 
 int main(int argc, char* argv[], char* arge[]) {
   //
@@ -15,6 +16,12 @@ int main(int argc, char* argv[], char* arge[]) {
   // MSVC-SAME:      name: "<unnamed-type-one>"
   // MSVC-SAME:      identifier: ".?AU<unnamed-type-one>@?1??main@@9@"
   // MSVC-SAME:      )
+  //
+  // MINGW:      [[TYPE_OF_ONE:![0-9]+]] = distinct !DICompositeType
+  // MINGW-SAME:     tag: DW_TAG_structure_type
+  // MINGW-SAME:     name: "<unnamed-type-one>"
+  // MINGW-SAME:     identifier: "_ZTSZ4mainE3$_0"
+  // MINGW-SAME:     )
 
 
   //
@@ -29,6 +36,12 @@ int main(int argc, char* argv[], char* arge[]) {
   // MSVC-SAME:      name: "<unnamed-type-two>"
   // MSVC-SAME:      identifier: ".?AU<unnamed-type-two>@?2??main@@9@"
   // MSVC-SAME:      )
+  //
+  // MINGW:      [[TYPE_OF_TWO:![0-9]+]] = distinct !DICompositeType
+  // MINGW-SAME:     tag: DW_TAG_structure_type
+  // MINGW-SAME:     name: "<unnamed-type-two>"
+  // MINGW-SAME:     identifier: "_ZTSZ4mainE3$_1"
+  // MINGW-SAME:     )
 
 
   //
@@ -43,6 +56,12 @@ int main(int argc, char* argv[], char* arge[]) {
   // MSVC-SAME:      name: "named"
   // MSVC-SAME:      identifier: ".?AUnamed@?1??main@@9@"
   // MSVC-SAME:      )
+  //
+  // MINGW:      [[TYPE_OF_THREE:![0-9]+]] = distinct !DICompositeType
+  // MINGW-SAME:     tag: DW_TAG_structure_type
+  // MINGW-SAME:     name: "named"
+  // MINGW-SAME:     identifier: "_ZTSZ4mainE5named"
+  // MINGW-SAME:     )
 
   //
   // LINUX:      [[TYPE_OF_FOUR:![0-9]+]] = distinct !DICompositeType(
@@ -56,6 +75,12 @@ int main(int argc, char* argv[], char* arge[]) {
   // MSVC-SAME:      name: "<lambda_0>"
   // MSVC-SAME:      identifier: ".?AV<lambda_0>@?0??main@@9@"
   // MSVC-SAME:      )
+  //
+  // MINGW:      [[TYPE_OF_FOUR:![0-9]+]] = distinct !DICompositeType
+  // MINGW-SAME:     tag: DW_TAG_class_type
+  // MINGW-SAME:     name: "<lambda0>"
+  // MINGW-SAME:     identifier: "_ZTSZ4mainE3$_2"
+  // MINGW-SAME:     )
 
 
   // In CodeView, the LF_MFUNCTION entry for "bar()" refers to the forward
@@ -71,6 +96,10 @@ int main(int argc, char* argv[], char* arge[]) {
   // MSVC:       !{{[0-9]+}} = !DILocalVariable(name: "one"
   // MSVC-SAME:      type: [[TYPE_OF_ONE]]
   // MSVC-SAME:      )
+  //
+  // MINGW:      !{{[0-9]+}} = !DILocalVariable(name: "one"
+  // MINGW-SAME:     type: [[TYPE_OF_ONE]]
+  // MINGW-SAME:     )
 
 
   // In CodeView, the LF_POINTER entry for "ptr2unnamed" refers to the forward
@@ -87,6 +116,10 @@ int main(int argc, char* argv[], char* arge[]) {
   // MSVC:       !{{[0-9]+}} = !DILocalVariable(name: "two"
   // MSVC-SAME:      type: [[TYPE_OF_TWO]]
   // MSVC-SAME:      )
+  //
+  // MINGW:      !{{[0-9]+}} = !DILocalVariable(name: "two"
+  // MINGW-SAME:     type: [[TYPE_OF_TWO]]
+  // MINGW-SAME:     )
 
 
   // In DWARF, named structures which are not externally visibile do not
@@ -102,6 +135,10 @@ int main(int argc, char* argv[], char* arge[]) {
   // MSVC:       !{{[0-9]+}} = !DILocalVariable(name: "three"
   // MSVC-SAME:      type: [[TYPE_OF_THREE]]
   // MSVC-SAME:      )
+  //
+  // MINGW:      !{{[0-9]+}} = !DILocalVariable(name: "three"
+  // MINGW-SAME:     type: [[TYPE_OF_THREE]]
+  // MINGW-SAME:     )
 
 
   // In CodeView, the LF_MFUNCTION entry for the lambda "operator()" routine
@@ -118,6 +155,10 @@ int main(int argc, char* argv[], char* arge[]) {
   // MSVC:       !{{[0-9]+}} = !DILocalVariable(name: "four"
   // MSVC-SAME:      type: [[TYPE_OF_FOUR]]
   // MSVC-SAME:      )
+  //
+  // MINGW:      !{{[0-9]+}} = !DILocalVariable(name: "four"
+  // MINGW-SAME:     type: [[TYPE_OF_FOUR]]
+  // MINGW-SAME:     )
 
   return 0;
 }
diff --git a/clang/test/DebugInfo/CXX/ms-anonymous-tag.cpp 
b/clang/test/DebugInfo/CXX/ms-anonymous-tag.cpp
index 5e2cb2190f0d65..74ad0242b1ad6c 100644
--- a/clang/test/DebugInfo/CXX/ms-anonymous-tag.cpp
+++ b/clang/test/DebugInfo/CXX/ms-anonymous-tag.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -triple x86_64-pc-win32 -debug-info-kind=limited -gcodeview 
%s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -debug-info-kind=limited 
-gcodeview %s -emit-llvm -o - | FileCheck %s
 
 typedef struct {
 } test1;

>From a0c088423fed826e9bb4d95a95c16130245bc06b Mon Sep 17 00:00:00 2001
From: Oleg Tolmatcev <[email protected]>
Date: Wed, 25 Nov 2026 13:53:03 +0100
Subject: [PATCH 2/2] [clang][CodeView] Give unnamed lambdas distinct names

Lambdas in a function body have no mangling number. Their CodeView names
(<lambda_N> for MSVC, <lambdaN> for Itanium) use an ID that is assigned
when the type is first mangled, and getLambdaString only looks that ID
up, returning 0 if it is not assigned yet.

CGDebugInfo computed the record name before the type identifier, which
is what mangles the type. As a result, every such lambda was named after
ID 0, so all lambdas in a function shared one name, e.g. <lambda_0>,
even though their identifiers differed. The debugger then shows several
types and operator() functions with the same name.

Compute the identifier before the name in CreateLimitedType and
getOrCreateRecordFwdDecl. This adds no new mangling, so it cannot change
symbol names.

This completes the fix for #47776 (d5f5deee9ee), which introduced these
names but only gave distinct names to lambdas that were mangled before
their type was described, e.g. lambdas passed to a function template,
not lambdas stored in local variables.

On Itanium the ID is shared with other unnamed types in the function,
so the lambda in codeview-unnamed.cpp is now <lambda2>, matching its
identifier _ZTSZ4mainE3$_2.

Assisted-by: Claude Code
---
 clang/lib/CodeGen/CGDebugInfo.cpp             | 21 +++++++++++-------
 .../DebugInfo/CXX/codeview-lambda-names.cpp   | 22 +++++++++++++++++++
 clang/test/DebugInfo/CXX/codeview-unnamed.cpp |  2 +-
 3 files changed, 36 insertions(+), 9 deletions(-)
 create mode 100644 clang/test/DebugInfo/CXX/codeview-lambda-names.cpp

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp 
b/clang/lib/CodeGen/CGDebugInfo.cpp
index 279a7beceeba1f..85bc700191586e 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -1552,6 +1552,14 @@ CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType 
*Ty,
   llvm::DIFile *DefUnit = getOrCreateFile(RD->getLocation());
   const unsigned Line =
       getLineNumber(RD->getLocation().isValid() ? RD->getLocation() : CurLoc);
+
+  // Don't include a linkage name in line tables only.
+  SmallString<256> Identifier;
+  if (CGM.getCodeGenOpts().hasReducedDebugInfo())
+    Identifier = getTypeIdentifier(Ty, CGM, TheCU);
+
+  // Get the name after the identifier: mangling the identifier assigns the
+  // IDs that the names of unnamed lambdas are built from.
   StringRef RDName = getClassName(RD);
 
   uint64_t Size = 0;
@@ -1572,10 +1580,6 @@ CGDebugInfo::getOrCreateRecordFwdDecl(const RecordType 
*Ty,
       Flags |= llvm::DINode::FlagNonTrivial;
 
   // Create the type.
-  SmallString<256> Identifier;
-  // Don't include a linkage name in line tables only.
-  if (CGM.getCodeGenOpts().hasReducedDebugInfo())
-    Identifier = getTypeIdentifier(Ty, CGM, TheCU);
   llvm::DICompositeType *RetTy = DBuilder.createReplaceableCompositeType(
       getTagForRecord(RD), RDName, Ctx, DefUnit, Line, 0, Size, Align, Flags,
       Identifier);
@@ -4590,10 +4594,6 @@ CGDebugInfo::getOrCreateLimitedType(const RecordType 
*Ty) {
 // TODO: Currently used for context chains when limiting debug info.
 llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
   RecordDecl *RD = Ty->getDecl()->getDefinitionOrSelf();
-  bool NameIsSimplified = false;
-
-  // Get overall information about the record type for the debug info.
-  StringRef RDName = getClassName(RD, &NameIsSimplified);
   const SourceLocation Loc = RD->getLocation();
   llvm::DIFile *DefUnit = nullptr;
   unsigned Line = 0;
@@ -4626,6 +4626,11 @@ llvm::DICompositeType 
*CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
 
   SmallString<256> Identifier = getTypeIdentifier(Ty, CGM, TheCU);
 
+  // Get the name after the identifier: mangling the identifier assigns the
+  // IDs that the names of unnamed lambdas are built from.
+  bool NameIsSimplified = false;
+  StringRef RDName = getClassName(RD, &NameIsSimplified);
+
   // Explicitly record the calling convention and export symbols for C++
   // records.
   auto Flags = llvm::DINode::FlagZero;
diff --git a/clang/test/DebugInfo/CXX/codeview-lambda-names.cpp 
b/clang/test/DebugInfo/CXX/codeview-lambda-names.cpp
new file mode 100644
index 00000000000000..b3f0a6737f12cf
--- /dev/null
+++ b/clang/test/DebugInfo/CXX/codeview-lambda-names.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -debug-info-kind=limited 
-gcodeview -emit-llvm -std=c++11 -o - %s | FileCheck --check-prefix MSVC %s
+// RUN: %clang_cc1 -triple x86_64-w64-windows-gnu -debug-info-kind=limited 
-gcodeview -emit-llvm -std=c++11 -o - %s | FileCheck --check-prefix MINGW %s
+
+// Lambdas in a function body have no mangling number, so their CodeView
+// names use IDs that are assigned when the type is mangled. Make sure each
+// lambda gets its own name rather than all of them sharing the first one.
+
+int main() {
+  auto l1 = [](int v) { return v + 1; };
+  auto l2 = [](int v) { return v + 2; };
+  return l1(1) + l2(2);
+}
+
+// MSVC:      !DICompositeType(tag: DW_TAG_class_type, name: "<lambda_0>"
+// MSVC-SAME:     identifier: ".?AV<lambda_0>@?0??main@@9@"
+// MSVC:      !DICompositeType(tag: DW_TAG_class_type, name: "<lambda_1>"
+// MSVC-SAME:     identifier: ".?AV<lambda_1>@?0??main@@9@"
+
+// MINGW:      !DICompositeType(tag: DW_TAG_class_type, name: "<lambda0>"
+// MINGW-SAME:     identifier: "_ZTSZ4mainE3$_0"
+// MINGW:      !DICompositeType(tag: DW_TAG_class_type, name: "<lambda1>"
+// MINGW-SAME:     identifier: "_ZTSZ4mainE3$_1"
diff --git a/clang/test/DebugInfo/CXX/codeview-unnamed.cpp 
b/clang/test/DebugInfo/CXX/codeview-unnamed.cpp
index a2b43e3c1da936..01faf91a5c87bf 100644
--- a/clang/test/DebugInfo/CXX/codeview-unnamed.cpp
+++ b/clang/test/DebugInfo/CXX/codeview-unnamed.cpp
@@ -78,7 +78,7 @@ int main(int argc, char* argv[], char* arge[]) {
   //
   // MINGW:      [[TYPE_OF_FOUR:![0-9]+]] = distinct !DICompositeType
   // MINGW-SAME:     tag: DW_TAG_class_type
-  // MINGW-SAME:     name: "<lambda0>"
+  // MINGW-SAME:     name: "<lambda2>"
   // MINGW-SAME:     identifier: "_ZTSZ4mainE3$_2"
   // MINGW-SAME:     )
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to