[Lldb-commits] [lldb] [lldb][NativePDB] Name global variables like Clang and DWARF do (PR #230563)

via lldb-commits lldb-commits at lists.llvm.org
Fri Oct 9 08:49:08 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Charles Zablit (charles-zablit)

<details>
<summary>Changes</summary>

CodeView spells anonymous namespaces `'anonymous namespace'`, and `CreateGlobalVariable` and `CreateConstantSymbol` use the CodeView name as is, while the Clang AST, type names and `SymbolFileDWARF` all say `(anonymous namespace)`. A qualified lookup like `FindFirstGlobalVariable("abc::(anonymous namespace)::g_anon_namespace_const_int")` finds nothing.

This patch replaces the CodeView spelling in the names of global variables and constants, and stops giving `S_CONSTANT` the `"::" + name` mangled name (they have no public symbol).

Fixes `TestCPPGlobalVariables` with PDB debug info.


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


2 Files Affected:

- (modified) lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp (+27-8) 
- (modified) lldb/test/API/lang/cpp/global_variables/TestCPPGlobalVariables.py (+2) 


``````````diff
diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
index 9ed1d327851fbe..dd27d643101a9d 100644
--- a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
+++ b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp
@@ -180,6 +180,25 @@ loadMatchingPDBFile(std::string exe_path, llvm::BumpPtrAllocator &allocator) {
   return pdb;
 }
 
+static std::string GetClangQualifiedName(llvm::StringRef name) {
+  std::string result;
+  while (!name.empty()) {
+    size_t pos = name.find('`');
+    result += name.take_front(pos);
+    if (pos == llvm::StringRef::npos)
+      break;
+    name = name.drop_front(pos);
+    if (name.consume_front("`anonymous namespace'") ||
+        name.consume_front("`anonymous-namespace'")) {
+      result += "(anonymous namespace)";
+    } else {
+      result += name.front();
+      name = name.drop_front();
+    }
+  }
+  return result;
+}
+
 static bool IsFunctionPrologue(const CompilandIndexItem &cci,
                                lldb::addr_t addr) {
   // FIXME: Implement this.
@@ -1139,9 +1158,10 @@ VariableSP SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id) {
   bool location_is_constant_data = false;
   bool static_member = false;
   VariableSP var_sp = std::make_shared<Variable>(
-      toOpaqueUid(var_id), name.str().c_str(), mangled_name.str().c_str(),
-      type_sp, scope, comp_unit.get(), ranges, &decl, location, is_external,
-      artificial, location_is_constant_data, static_member);
+      toOpaqueUid(var_id), GetClangQualifiedName(name).c_str(),
+      mangled_name.str().c_str(), type_sp, scope, comp_unit.get(), ranges,
+      &decl, location, is_external, artificial, location_is_constant_data,
+      static_member);
 
   return var_sp;
 }
@@ -1161,8 +1181,6 @@ SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id,
                    "Failed to deserialize ConstantSym record: {0}");
     return nullptr;
   }
-  std::string global_name("::");
-  global_name += constant.Name;
   PdbTypeSymId tid(constant.Type, false);
   SymbolFileTypeSP type_sp =
       std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
@@ -1185,9 +1203,10 @@ SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id,
   bool location_is_constant_data = true;
   bool static_member = false;
   VariableSP var_sp = std::make_shared<Variable>(
-      toOpaqueUid(var_id), constant.Name.str().c_str(), global_name.c_str(),
-      type_sp, eValueTypeVariableGlobal, module.get(), ranges, &decl, location,
-      external, artificial, location_is_constant_data, static_member);
+      toOpaqueUid(var_id), GetClangQualifiedName(constant.Name).c_str(),
+      /*mangled=*/nullptr, type_sp, eValueTypeVariableGlobal, module.get(),
+      ranges, &decl, location, external, artificial, location_is_constant_data,
+      static_member);
   return var_sp;
 }
 
diff --git a/lldb/test/API/lang/cpp/global_variables/TestCPPGlobalVariables.py b/lldb/test/API/lang/cpp/global_variables/TestCPPGlobalVariables.py
index 9d964634adbbd3..83b57d7412ee71 100644
--- a/lldb/test/API/lang/cpp/global_variables/TestCPPGlobalVariables.py
+++ b/lldb/test/API/lang/cpp/global_variables/TestCPPGlobalVariables.py
@@ -7,6 +7,8 @@
 
 
 class GlobalVariablesCppTestCase(TestBase):
+    TEST_WITH_PDB_DEBUG_INFO = True
+
     def setUp(self):
         TestBase.setUp(self)
         self.source = lldb.SBFileSpec("main.cpp")

``````````

</details>


https://github.com/llvm/llvm-project/pull/230563


More information about the lldb-commits mailing list