Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ AST Matchers

- Fixed a crash when traverse lambda expr with invalid captures. (#GH106444)

- Fixed `isInstantiated` and `isInTemplateInstantiation` to also match for variable templates. (#GH110666)

clang-format
------------

Expand Down
9 changes: 5 additions & 4 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6750,7 +6750,8 @@ AST_POLYMORPHIC_MATCHER(isTemplateInstantiation,
/// matches 'A(int) {...};' and 'A(unsigned) {...}'.
AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
auto IsInstantiation = decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
functionDecl(isTemplateInstantiation())));
functionDecl(isTemplateInstantiation()),
varDecl(isTemplateInstantiation())));
return decl(anyOf(IsInstantiation, hasAncestor(IsInstantiation)));
}

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

/// Matches explicit template specializations of function, class, or
Expand Down
39 changes: 39 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3313,6 +3313,45 @@ TEST_P(ASTMatchersTest,
declStmt(isInTemplateInstantiation())));
}

TEST_P(ASTMatchersTest, IsInstantiated_MatchesVariableInstantiation) {
if (!GetParam().isCXX14OrLater()) {
return;
}

EXPECT_TRUE(matches("template<typename T> int V = 10; void x() { V<int>; }",
varDecl(isInstantiated())));
}

TEST_P(ASTMatchersTest, IsInstantiated_NotMatchesVariableDefinition) {
if (!GetParam().isCXX14OrLater()) {
return;
}

EXPECT_TRUE(notMatches("template<typename T> int V = 10;",
varDecl(isInstantiated())));
}

TEST_P(ASTMatchersTest,
IsInTemplateInstantiation_MatchesVariableInstantiationStmt) {
if (!GetParam().isCXX14OrLater()) {
return;
}

EXPECT_TRUE(matches(
"template<typename T> auto V = []() { T i; }; void x() { V<int>(); }",
declStmt(isInTemplateInstantiation())));
}

TEST_P(ASTMatchersTest,
IsInTemplateInstantiation_NotMatchesVariableDefinitionStmt) {
if (!GetParam().isCXX14OrLater()) {
return;
}

EXPECT_TRUE(notMatches("template<typename T> auto V = []() { T i; };",
declStmt(isInTemplateInstantiation())));
}

TEST_P(ASTMatchersTest, IsInTemplateInstantiation_Sharing) {
if (!GetParam().isCXX()) {
return;
Expand Down
Loading