Skip to content

Commit d30b8c9

Browse files
Handle variable templates in isInstantiated and isInTemplateInstantiation matchers
Fix `isInstantiated` and `isInTemplateInstantiation` matchers, so they return true for instantitions of the variable template, and any declaration in statement contained in such instantiation.
1 parent b8b036a commit d30b8c9

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6750,7 +6750,8 @@ AST_POLYMORPHIC_MATCHER(isTemplateInstantiation,
67506750
/// matches 'A(int) {...};' and 'A(unsigned) {...}'.
67516751
AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
67526752
auto IsInstantiation = decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
6753-
functionDecl(isTemplateInstantiation())));
6753+
functionDecl(isTemplateInstantiation()),
6754+
varDecl(isTemplateInstantiation())));
67546755
return decl(anyOf(IsInstantiation, hasAncestor(IsInstantiation)));
67556756
}
67566757

@@ -6771,7 +6772,8 @@ AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
67716772
AST_MATCHER_FUNCTION(internal::Matcher<Stmt>, isInTemplateInstantiation) {
67726773
return stmt(
67736774
hasAncestor(decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
6774-
functionDecl(isTemplateInstantiation())))));
6775+
functionDecl(isTemplateInstantiation()),
6776+
varDecl(isTemplateInstantiation())))));
67756777
}
67766778

67776779
/// Matches explicit template specializations of function, class, or

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,47 @@ TEST_P(ASTMatchersTest,
33133313
declStmt(isInTemplateInstantiation())));
33143314
}
33153315

3316+
3317+
TEST_P(ASTMatchersTest, IsInstantiated_MatchesVariableInstantiation) {
3318+
if (!GetParam().isCXX14OrLater()) {
3319+
return;
3320+
}
3321+
3322+
EXPECT_TRUE(
3323+
matches("template<typename T> int V = 10; void x() { V<int>; }",
3324+
varDecl(isInstantiated())));
3325+
}
3326+
3327+
TEST_P(ASTMatchersTest, IsInstantiated_NotMatchesVariableDefinition) {
3328+
if (!GetParam().isCXX14OrLater()) {
3329+
return;
3330+
}
3331+
3332+
EXPECT_TRUE(notMatches("template<typename T> int V = 10;",
3333+
varDecl(isInstantiated())));
3334+
}
3335+
3336+
TEST_P(ASTMatchersTest,
3337+
IsInTemplateInstantiation_MatchesVariableInstantiationStmt) {
3338+
if (!GetParam().isCXX14OrLater()) {
3339+
return;
3340+
}
3341+
3342+
EXPECT_TRUE(
3343+
matches("template<typename T> auto V = []() { T i; }; void x() { V<int>(); }",
3344+
declStmt(isInTemplateInstantiation())));
3345+
}
3346+
3347+
TEST_P(ASTMatchersTest,
3348+
IsInTemplateInstantiation_NotMatchesVariableDefinitionStmt) {
3349+
if (!GetParam().isCXX14OrLater()) {
3350+
return;
3351+
}
3352+
3353+
EXPECT_TRUE(notMatches("template<typename T> auto V = []() { T i; };",
3354+
declStmt(isInTemplateInstantiation())));
3355+
}
3356+
33163357
TEST_P(ASTMatchersTest, IsInTemplateInstantiation_Sharing) {
33173358
if (!GetParam().isCXX()) {
33183359
return;

0 commit comments

Comments
 (0)