llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-debuginfo

Author: oltolm (oltolm)

<details>
<summary>Changes</summary>

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 
`&lt;unnamed-type-one&gt;` instead of `&lt;unnamed-tag&gt;`, 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.

---
Full diff: https://github.com/llvm/llvm-project/pull/246914.diff


5 Files Affected:

- (modified) clang/lib/AST/ItaniumCXXABI.cpp (+19-4) 
- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+13-8) 
- (added) clang/test/DebugInfo/CXX/codeview-lambda-names.cpp (+22) 
- (modified) clang/test/DebugInfo/CXX/codeview-unnamed.cpp (+41) 
- (modified) clang/test/DebugInfo/CXX/ms-anonymous-tag.cpp (+1) 


``````````diff
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/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 b625a80313c38f..01faf91a5c87bf 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: "<lambda2>"
+  // 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;

``````````

</details>


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

Reply via email to