Skip to content

Commit be911d8

Browse files
Handle variable templates in isInstantiated and isInTemplateInstantiation matchers
Fix isInstantiated and isInTemplateInstantiation matchers, so they return true for instantiations of variable templates, and any declaration in statements contained in such instantiations.
1 parent b8b036a commit be911d8

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 5 additions & 4 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

@@ -6769,9 +6770,9 @@ AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
67696770
/// will NOT match j += 42; as it's shared between the template definition and
67706771
/// instantiation.
67716772
AST_MATCHER_FUNCTION(internal::Matcher<Stmt>, isInTemplateInstantiation) {
6772-
return stmt(
6773-
hasAncestor(decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
6774-
functionDecl(isTemplateInstantiation())))));
6773+
return stmt(hasAncestor(decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
6774+
functionDecl(isTemplateInstantiation()),
6775+
varDecl(isTemplateInstantiation())))));
67756776
}
67766777

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

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

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

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

0 commit comments

Comments
 (0)