[clang] Do not print locations in anonymous tag names. (PR #159592)

Shubham Sandeep Rastogi via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 18 14:51:16 PDT 2025


https://github.com/rastogishubham updated https://github.com/llvm/llvm-project/pull/159592

>From 549f627e6402ad1d2225d8f14ccb5bd58619435b Mon Sep 17 00:00:00 2001
From: Shubham Sandeep Rastogi <srastogi22 at apple.com>
Date: Thu, 18 Sep 2025 07:49:37 -0700
Subject: [PATCH] Do not print locations in anonymous tag names.

If we use LTO as a driver, we can see that even if we have lambdas in
template parameters, two debug entries from different CUs will get
merged in LTO, but their human-readable DW_AT_name will be different if
we emit the locations in anonymous tag names.

Therefore we would never have two dies with the same linkage name but
different human readable names. Which probably should not happen.

This change prevents that from happening.
---
 clang/lib/CodeGen/CGDebugInfo.cpp                  |  5 +++++
 clang/test/DebugInfo/CXX/anonymous-locs.cpp        | 14 ++++++++++++++
 clang/test/DebugInfo/CXX/prefix-map-lambda.cpp     |  4 +---
 clang/test/DebugInfo/CXX/simple-template-names.cpp | 12 ++++++------
 clang/test/DebugInfo/Generic/debug-prefix-map.cpp  |  2 +-
 clang/test/DebugInfo/Generic/slash.test            |  4 ++--
 6 files changed, 29 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/DebugInfo/CXX/anonymous-locs.cpp

diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 578d09f7971d6..48a873f2b52ac 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -397,6 +397,11 @@ PrintingPolicy CGDebugInfo::getPrintingPolicy() const {
 
   // Apply -fdebug-prefix-map.
   PP.Callbacks = &PrintCB;
+  // If we have lambdas as template parameters, two debug entries from different
+  // CUs will get merged in LTO, but their human-readable DW_AT_name will be
+  // different. Disable printing of location of an anonymous tag name to prevent
+  // that from happening.
+  PP.AnonymousTagLocations = false;
   return PP;
 }
 
diff --git a/clang/test/DebugInfo/CXX/anonymous-locs.cpp b/clang/test/DebugInfo/CXX/anonymous-locs.cpp
new file mode 100644
index 0000000000000..48a8aff57d520
--- /dev/null
+++ b/clang/test/DebugInfo/CXX/anonymous-locs.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -std=c++20 -emit-obj -debug-info-kind=standalone -dwarf-version=5 -triple x86_64-apple-darwin -o %t %s 
+// RUN: llvm-dwarfdump %t | FileCheck %s
+
+// CHECK: DW_TAG_structure_type
+// CHECK-NEXT: DW_AT_calling_convention	(DW_CC_pass_by_value)
+// CHECK-NEXT: DW_AT_name	("Foo<(lambda){}>")
+
+template<auto T>
+struct Foo {
+};
+
+Foo<[] {}> f;
+
+auto func() { return f; }
\ No newline at end of file
diff --git a/clang/test/DebugInfo/CXX/prefix-map-lambda.cpp b/clang/test/DebugInfo/CXX/prefix-map-lambda.cpp
index f0fb1a312c8be..961aff1d66253 100644
--- a/clang/test/DebugInfo/CXX/prefix-map-lambda.cpp
+++ b/clang/test/DebugInfo/CXX/prefix-map-lambda.cpp
@@ -3,8 +3,6 @@
 
 template <typename T> void b(T) {}
 void c() {
-  // CHECK: !DISubprogram(name: "b<(lambda at
-  // CHECK-SAME:          SOURCE_ROOT
-  // CHECK-SAME:          [[@LINE+1]]:{{[0-9]+}})>"
+  // CHECK: !DISubprogram(name: "b<(lambda)>"
   b([]{});
 }
diff --git a/clang/test/DebugInfo/CXX/simple-template-names.cpp b/clang/test/DebugInfo/CXX/simple-template-names.cpp
index 5a5d706e81972..97eb58c67b31f 100644
--- a/clang/test/DebugInfo/CXX/simple-template-names.cpp
+++ b/clang/test/DebugInfo/CXX/simple-template-names.cpp
@@ -70,18 +70,18 @@ void f() {
   // anything other than another unnamed class/struct.
   auto Lambda = [] {};
   f1<decltype(Lambda)>();
-  // CHECK: !DISubprogram(name: "f1<(lambda at {{.*}}simple-template-names.cpp:[[# @LINE - 2]]:17)>",
+  // CHECK: !DISubprogram(name: "f1<(lambda)>",
   f1<t1<t1<decltype(Lambda)>>>();
-  // CHECK: !DISubprogram(name: "f1<t1<t1<(lambda at {{.*}}> > >",
+  // CHECK: !DISubprogram(name: "f1<t1<t1<(lambda)> > >",
   struct {
   } unnamed_struct;
   f1<decltype(unnamed_struct)>();
-  // CHECK: !DISubprogram(name: "f1<(unnamed struct at {{.*}}simple-template-names.cpp:[[# @LINE - 3]]:3)>",
+  // CHECK: !DISubprogram(name: "f1<(unnamed)>",
   f1<void (decltype(unnamed_struct))>();
-  // CHECK: !DISubprogram(name: "f1<void ((unnamed struct at {{.*}}simple-template-names.cpp:[[# @LINE - 5]]:3))>",
+  // CHECK: !DISubprogram(name: "f1<void ((unnamed))>",
   enum {} unnamed_enum;
   f1<decltype(unnamed_enum)>();
-  // CHECK: !DISubprogram(name: "f1<(unnamed enum at {{.*}}simple-template-names.cpp:[[# @LINE - 2]]:3)>",
+  // CHECK: !DISubprogram(name: "f1<(unnamed)>",
 
   // Declarations can't readily be reversed as the value in the DWARF only
   // contains the address of the value - we'd have to do symbol lookup to find
@@ -128,5 +128,5 @@ void f() {
   // CHECK: !DISubprogram(name: "f1<int () __attribute__((noreturn))>",
   
   f4<UnnamedEnum1>();
-  // CHECK: !DISubprogram(name: "f4<((unnamed enum at {{.*}}))0>"
+  // CHECK: !DISubprogram(name: "f4<((unnamed))0>"
 }
diff --git a/clang/test/DebugInfo/Generic/debug-prefix-map.cpp b/clang/test/DebugInfo/Generic/debug-prefix-map.cpp
index 174bef5a07699..e6eadd66d1ffd 100644
--- a/clang/test/DebugInfo/Generic/debug-prefix-map.cpp
+++ b/clang/test/DebugInfo/Generic/debug-prefix-map.cpp
@@ -8,4 +8,4 @@ struct alignas(64) an {
 
 struct an *pan = new an;
 
-// CHECK: !DISubprogram(name: "(unnamed struct at ./UNLIKELY_PATH/empty{{/|\\\\}}{{.*}}",
+// CHECK: !DISubprogram(name: "(unnamed)",
diff --git a/clang/test/DebugInfo/Generic/slash.test b/clang/test/DebugInfo/Generic/slash.test
index 0e42912c18d21..09ce08502746a 100644
--- a/clang/test/DebugInfo/Generic/slash.test
+++ b/clang/test/DebugInfo/Generic/slash.test
@@ -6,5 +6,5 @@ RUN: cd %t-dir
 RUN: %clang -target x86_64-pc-win32 -emit-llvm -S -g  %t-dir/debug-info-slash.cpp -Iheader -o - | FileCheck --check-prefix=WIN %s
 RUN: %clang -target x86_64-linux-gnu -emit-llvm -S -g  %t-dir/debug-info-slash.cpp -Iheader -o - | FileCheck --check-prefix=LINUX %s
 
-WIN:   lambda at header\\Inputs\\debug-info-slash.h
-LINUX: lambda at header/Inputs/debug-info-slash.h
+WIN:   lambda
+LINUX: lambda



More information about the cfe-commits mailing list