Skip to content

Commit 5cb2ebc

Browse files
authored
Reland "[clang] Preserve found-decl when constructing VarTemplateIds" (#82612)
Update include-cleaner tests. Now that we have proper found-decls set up for VarTemplates, in case of instationtations we point to primary templates and not specializations. To be changed in a follow-up patch.
1 parent 5f1319b commit 5cb2ebc

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,24 +200,24 @@ TEST(WalkAST, VarTemplates) {
200200
EXPECT_THAT(testWalk(R"cpp(
201201
template <typename T> T $explicit^Foo = 0;)cpp",
202202
"int z = ^Foo<int>;"),
203-
ElementsAre(Decl::VarTemplateSpecialization));
203+
ElementsAre(Decl::VarTemplate));
204204
EXPECT_THAT(testWalk(R"cpp(
205-
template<typename T> T Foo = 0;
206-
template<> int $explicit^Foo<int> = 1;)cpp",
205+
template<typename T> T $explicit^Foo = 0;
206+
template<> int Foo<int> = 1;)cpp",
207207
"int x = ^Foo<int>;"),
208-
ElementsAre(Decl::VarTemplateSpecialization));
208+
ElementsAre(Decl::VarTemplate));
209209
// FIXME: This points at implicit specialization, instead we should point to
210210
// explicit partial specializaiton pattern.
211211
EXPECT_THAT(testWalk(R"cpp(
212-
template<typename T> T Foo = 0;
213-
template<typename T> T* $explicit^Foo<T*> = nullptr;)cpp",
212+
template<typename T> T $explicit^Foo = 0;
213+
template<typename T> T* Foo<T*> = nullptr;)cpp",
214214
"int *x = ^Foo<int *>;"),
215-
ElementsAre(Decl::VarTemplateSpecialization));
215+
ElementsAre(Decl::VarTemplate));
216216
EXPECT_THAT(testWalk(R"cpp(
217217
template<typename T> T $explicit^Foo = 0;
218218
template int Foo<int>;)cpp",
219219
"int x = ^Foo<int>;"),
220-
ElementsAre(Decl::VarTemplateSpecialization));
220+
ElementsAre(Decl::VarTemplate));
221221
}
222222
TEST(WalkAST, FunctionTemplates) {
223223
// Explicit instantiation and (partial) specialization references primary

clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8540,7 +8540,7 @@ class Sema final {
85408540
/// if the arguments are dependent.
85418541
ExprResult CheckVarTemplateId(const CXXScopeSpec &SS,
85428542
const DeclarationNameInfo &NameInfo,
8543-
VarTemplateDecl *Template,
8543+
VarTemplateDecl *Template, NamedDecl *FoundD,
85448544
SourceLocation TemplateLoc,
85458545
const TemplateArgumentListInfo *TemplateArgs);
85468546

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,11 +4958,10 @@ Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc,
49584958
return Decl;
49594959
}
49604960

4961-
ExprResult
4962-
Sema::CheckVarTemplateId(const CXXScopeSpec &SS,
4963-
const DeclarationNameInfo &NameInfo,
4964-
VarTemplateDecl *Template, SourceLocation TemplateLoc,
4965-
const TemplateArgumentListInfo *TemplateArgs) {
4961+
ExprResult Sema::CheckVarTemplateId(
4962+
const CXXScopeSpec &SS, const DeclarationNameInfo &NameInfo,
4963+
VarTemplateDecl *Template, NamedDecl *FoundD, SourceLocation TemplateLoc,
4964+
const TemplateArgumentListInfo *TemplateArgs) {
49664965

49674966
DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(),
49684967
*TemplateArgs);
@@ -4978,8 +4977,7 @@ Sema::CheckVarTemplateId(const CXXScopeSpec &SS,
49784977
NameInfo.getLoc());
49794978

49804979
// Build an ordinary singleton decl ref.
4981-
return BuildDeclarationNameExpr(SS, NameInfo, Var,
4982-
/*FoundD=*/nullptr, TemplateArgs);
4980+
return BuildDeclarationNameExpr(SS, NameInfo, Var, FoundD, TemplateArgs);
49834981
}
49844982

49854983
void Sema::diagnoseMissingTemplateArguments(TemplateName Name,
@@ -5066,9 +5064,9 @@ ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
50665064
bool KnownDependent = false;
50675065
// In C++1y, check variable template ids.
50685066
if (R.getAsSingle<VarTemplateDecl>()) {
5069-
ExprResult Res = CheckVarTemplateId(SS, R.getLookupNameInfo(),
5070-
R.getAsSingle<VarTemplateDecl>(),
5071-
TemplateKWLoc, TemplateArgs);
5067+
ExprResult Res = CheckVarTemplateId(
5068+
SS, R.getLookupNameInfo(), R.getAsSingle<VarTemplateDecl>(),
5069+
R.getRepresentativeDecl(), TemplateKWLoc, TemplateArgs);
50725070
if (Res.isInvalid() || Res.isUsable())
50735071
return Res;
50745072
// Result is dependent. Carry on to build an UnresolvedLookupEpxr.

clang/test/AST/ast-dump-using.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace a {
44
struct S;
5+
template <typename T> T x = {};
56
}
67
namespace b {
78
using a::S;
@@ -21,4 +22,10 @@ typedef S e; // check the same UsingType is reused.
2122
// CHECK-NEXT: `-UsingType [[TYPE_ADDR]] 'a::S' sugar
2223
// CHECK-NEXT: |-UsingShadow [[SHADOW_ADDR]] 'S'
2324
// CHECK-NEXT: `-RecordType {{.*}} 'a::S'
25+
using a::x;
26+
27+
void foo() {
28+
x<int> = 3;
29+
// CHECK: DeclRefExpr {{.*}} 'x' {{.*}} (UsingShadow {{.*}} 'x')
30+
}
2431
}

0 commit comments

Comments
 (0)