Index: lib/CodeGen/CGDebugInfo.cpp =================================================================== --- lib/CodeGen/CGDebugInfo.cpp +++ lib/CodeGen/CGDebugInfo.cpp @@ -2982,8 +2982,27 @@ Name = VD->getName(); if (VD->getDeclContext() && !isa(VD->getDeclContext()) && - !isa(VD->getDeclContext())) + !isa(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(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(); 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" +// CXX14: !DIGlobalVariable(name: "arraytemp" +template T vartemp = T(); +template T arraytemp[N]; + +void func() { + vartemp = 5; + arraytemp[0] = 1; +}