[clang] bc79ec0 - [clang][ASTMatcher] Handle variable templates in `isInstantiated` and `isInTemplateInstantiation` matchers (#110666)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 30 11:57:12 PDT 2024
Author: Fred Tingaud
Date: 2024-10-30T14:57:09-04:00
New Revision: bc79ec0c5bc3fce31448419846c343017ae1c5ad
URL: https://github.com/llvm/llvm-project/commit/bc79ec0c5bc3fce31448419846c343017ae1c5ad
DIFF: https://github.com/llvm/llvm-project/commit/bc79ec0c5bc3fce31448419846c343017ae1c5ad.diff
LOG: [clang][ASTMatcher] Handle variable templates in `isInstantiated` and `isInTemplateInstantiation` matchers (#110666)
Fix `isInstantiated` and `isInTemplateInstantiation` matchers, so they
return true for instantiations of variable templates, and any
declaration in statements contained in such instantiations.
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1a179e63f902f3..402203f89e23a0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -745,6 +745,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)
+
- Ensure ``hasName`` matches template specializations across inline namespaces,
making `matchesNodeFullSlow` and `matchesNodeFullFast` consistent.
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 54e484d41fb1c3..c77140842d7a6e 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -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)));
}
@@ -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
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index d696375547acce..056b7c7b571ef4 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -3342,6 +3342,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;
More information about the cfe-commits
mailing list