Skip to content

Commit cf7a59f

Browse files
committed
Merging r359809:
------------------------------------------------------------------------ r359809 | rnk | 2019-05-02 10:45:54 -0700 (Thu, 02 May 2019) | 27 lines Use primary template parameter names for variable template debug info Summary: Fixes PR41677 Consider: template <typename LHS, typename RHS> constexpr bool is_same_v = false; template <typename T> constexpr bool is_same_v<T, T> = true; template constexpr bool is_same_v<int, int>; Before this change, when emitting debug info for the `is_same_v<int, int>` global variable, clang would crash because it would try to use the template parameter list from the partial specialization to give parameter names to template arguments. This doesn't work in general, since a partial specialization can have fewer arguments than the primary template. Therefore, always use the primary template. Hypothetically we could try to use the parameter names from the partial specialization when possible, but it's not clear this really helps debugging in practice. Reviewers: JDevlieghere, aprantl, ormris, dblaikie Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D61408 ------------------------------------------------------------------------ llvm-svn: 364483
1 parent 21f32a9 commit cf7a59f

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,32 +1817,24 @@ CGDebugInfo::CollectFunctionTemplateParams(const FunctionDecl *FD,
18171817
}
18181818

18191819
llvm::DINodeArray CGDebugInfo::CollectVarTemplateParams(const VarDecl *VL,
1820-
llvm::DIFile *Unit) {
1821-
if (auto *TS = dyn_cast<VarTemplateSpecializationDecl>(VL)) {
1822-
auto T = TS->getSpecializedTemplateOrPartial();
1823-
auto TA = TS->getTemplateArgs().asArray();
1824-
// Collect parameters for a partial specialization
1825-
if (T.is<VarTemplatePartialSpecializationDecl *>()) {
1826-
const TemplateParameterList *TList =
1827-
T.get<VarTemplatePartialSpecializationDecl *>()
1828-
->getTemplateParameters();
1829-
return CollectTemplateParams(TList, TA, Unit);
1830-
}
1831-
1832-
// Collect parameters for an explicit specialization
1833-
if (T.is<VarTemplateDecl *>()) {
1834-
const TemplateParameterList *TList = T.get<VarTemplateDecl *>()
1835-
->getTemplateParameters();
1836-
return CollectTemplateParams(TList, TA, Unit);
1837-
}
1838-
}
1839-
return llvm::DINodeArray();
1820+
llvm::DIFile *Unit) {
1821+
// Always get the full list of parameters, not just the ones from the
1822+
// specialization. A partial specialization may have fewer parameters than
1823+
// there are arguments.
1824+
auto *TS = dyn_cast<VarTemplateSpecializationDecl>(VL);
1825+
if (!TS)
1826+
return llvm::DINodeArray();
1827+
VarTemplateDecl *T = TS->getSpecializedTemplate();
1828+
const TemplateParameterList *TList = T->getTemplateParameters();
1829+
auto TA = TS->getTemplateArgs().asArray();
1830+
return CollectTemplateParams(TList, TA, Unit);
18401831
}
18411832

18421833
llvm::DINodeArray CGDebugInfo::CollectCXXTemplateParams(
18431834
const ClassTemplateSpecializationDecl *TSpecial, llvm::DIFile *Unit) {
1844-
// Always get the full list of parameters, not just the ones from
1845-
// the specialization.
1835+
// Always get the full list of parameters, not just the ones from the
1836+
// specialization. A partial specialization may have fewer parameters than
1837+
// there are arguments.
18461838
TemplateParameterList *TPList =
18471839
TSpecial->getSpecializedTemplate()->getTemplateParameters();
18481840
const TemplateArgumentList &TAList = TSpecial->getTemplateArgs();

clang/test/CodeGenCXX/debug-info-template-member.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ inline int add3(int x) {
3030
// CHECK: {{![0-9]+}} = distinct !DIGlobalVariable(
3131
// CHECK-SAME: name: "var"
3232
// CHECK-SAME: templateParams: {{![0-9]+}}
33-
// CHECK: !DITemplateTypeParameter(name: "P", type: {{![0-9]+}})
33+
// CHECK: !DITemplateTypeParameter(name: "T", type: {{![0-9]+}})
3434
// CHECK: {{![0-9]+}} = distinct !DIGlobalVariable(
3535
// CHECK-SAME: name: "varray"
3636
// CHECK-SAME: templateParams: {{![0-9]+}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -emit-llvm -triple x86_64-linux-gnu %s -o - -debug-info-kind=limited | FileCheck %s
2+
3+
template <typename LHS, typename RHS> constexpr bool is_same_v = false;
4+
template <typename T> constexpr bool is_same_v<T, T> = true;
5+
6+
template constexpr bool is_same_v<int, int>;
7+
static_assert(is_same_v<int, int>, "should get partial spec");
8+
9+
// Note that the template arguments for the instantiated variable use the
10+
// parameter names from the primary template. The partial specialization might
11+
// not have enough parameters.
12+
13+
// CHECK: distinct !DIGlobalVariable(name: "is_same_v", linkageName: "_ZL9is_same_vIiiE", {{.*}} templateParams: ![[PARAMS:[0-9]+]])
14+
// CHECK: ![[PARAMS]] = !{![[LHS:[0-9]+]], ![[RHS:[0-9]+]]}
15+
// CHECK: ![[LHS]] = !DITemplateTypeParameter(name: "LHS", type: ![[INT:[0-9]+]])
16+
// CHECK: ![[INT]] = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
17+
// CHECK: ![[RHS]] = !DITemplateTypeParameter(name: "RHS", type: ![[INT]])

0 commit comments

Comments
 (0)