[PATCH] D44842: Add Parameters to DW_AT_name Attribute of Template Variables

Matthew Voss via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 23 12:30:25 PDT 2018


ormris created this revision.
ormris added reviewers: dblaikie, aprantl, probinson, JDevlieghere.

This patch adds the associated template parameters to the DWARF name attribute
of all template variable specializations, mirroring how they are referenced in
the source code.


Repository:
  rC Clang

https://reviews.llvm.org/D44842

Files:
  lib/CodeGen/CGDebugInfo.cpp
  test/CodeGenCXX/debug-info-template.cpp


Index: test/CodeGenCXX/debug-info-template.cpp
===================================================================
--- test/CodeGenCXX/debug-info-template.cpp
+++ test/CodeGenCXX/debug-info-template.cpp
@@ -160,3 +160,14 @@
 };
 
 PaddingAtEndTemplate<&PaddedObj> PaddedTemplateObj;
+
+// RUN: %clang -S -emit-llvm -target x86_64-unknown_unknown -g %s -o - -std=c++14 | FileCheck %s --check-prefix=CXX14
+// CXX14: !DIGlobalVariable(name: "vartemp<int>"
+// CXX14: !DIGlobalVariable(name: "arraytemp<int,1>"
+template <typename T> T vartemp = T();
+template <typename T, int N> T arraytemp[N];
+
+void func() {
+  vartemp<int> = 5;
+  arraytemp<int, 1>[0] = 1;
+}
Index: lib/CodeGen/CGDebugInfo.cpp
===================================================================
--- lib/CodeGen/CGDebugInfo.cpp
+++ lib/CodeGen/CGDebugInfo.cpp
@@ -2982,8 +2982,27 @@
 
   Name = VD->getName();
   if (VD->getDeclContext() && !isa<FunctionDecl>(VD->getDeclContext()) &&
-      !isa<ObjCMethodDecl>(VD->getDeclContext()))
+      !isa<ObjCMethodDecl>(VD->getDeclContext())) {
     LinkageName = CGM.getMangledName(VD);
+    // If this node refers to an instantiation of a variable template, add the
+    // template parameters to its name. This disambiguates it from other
+    // instantiations.
+    if (auto *VSD = dyn_cast<VarTemplateSpecializationDecl>(VD)) {
+      std::string NameString = Name.str();
+      llvm::raw_string_ostream ParameterizedName(NameString);
+      ParameterizedName << "<";
+      bool first = true;
+      for (auto Parameter : VSD->getTemplateArgs().asArray()) {
+        if (!first)
+          ParameterizedName << ",";
+        Parameter.print(getPrintingPolicy(), ParameterizedName);
+        first = false;
+      }
+      ParameterizedName << ">";
+      Name = internString(ParameterizedName.str());
+    }
+  }
+
   if (LinkageName == Name)
     LinkageName = StringRef();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44842.139635.patch
Type: text/x-patch
Size: 1899 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180323/c14c6033/attachment.bin>


More information about the cfe-commits mailing list