Skip to content

Conversation

@frederic-tingaud-sonarsource
Copy link
Contributor

Fix isInstantiated and isInTemplateInstantiation matchers, so they return true for instantiations of variable templates, and any declaration in statements contained in such instantiations.

@llvmbot llvmbot added the clang Clang issues not falling into any other category label Oct 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 1, 2024

@llvm/pr-subscribers-clang

Author: Fred Tingaud (frederic-tingaud-sonarsource)

Changes

Fix isInstantiated and isInTemplateInstantiation matchers, so they return true for instantiations of variable templates, and any declaration in statements contained in such instantiations.


Full diff: https://github.com/llvm/llvm-project/pull/110666.diff

2 Files Affected:

  • (modified) clang/include/clang/ASTMatchers/ASTMatchers.h (+4-2)
  • (modified) clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp (+41)
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index f1c72efc238784..2ba3542ec1ae51 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)));
 }
 
@@ -6771,7 +6772,8 @@ AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
 AST_MATCHER_FUNCTION(internal::Matcher<Stmt>, isInTemplateInstantiation) {
   return stmt(
       hasAncestor(decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
-                             functionDecl(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 611e1f9ba5327c..6147dbd37d3b73 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -3313,6 +3313,47 @@ 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;

@github-actions
Copy link

github-actions bot commented Oct 1, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

…tion matchers

Fix isInstantiated and isInTemplateInstantiation matchers, so they return true for instantiations of variable templates, and any declaration in statements contained in such instantiations.
@frederic-tingaud-sonarsource frederic-tingaud-sonarsource changed the title Handle variable templates in isInstantiated and isInTemplateInstantiation matchers [clang][ASTMatcher]Handle variable templates in isInstantiated and isInTemplateInstantiation matchers Oct 3, 2024
@tomasz-kaminski-sonarsource tomasz-kaminski-sonarsource changed the title [clang][ASTMatcher]Handle variable templates in isInstantiated and isInTemplateInstantiation matchers [clang][ASTMatcher] Handle variable templates in isInstantiated and isInTemplateInstantiation matchers Oct 3, 2024
@frederic-tingaud-sonarsource
Copy link
Contributor Author

@AaronBallman, I think you are the maintainer for this part of the code. Could you help me find somebody who could review the PR?

@frederic-tingaud-sonarsource
Copy link
Contributor Author

Pinging some recent contributors/reviewers to AST Matchers. @danix800, @AaronBallman, @shafik, @PiotrZSL, @steakhal, @cor3ntin

@AaronBallman AaronBallman self-requested a review October 30, 2024 18:10
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Sorry for the delayed review while I was out. No need to re-run the documentation macro because the public interfaces nor doc comments changed, and I think the existing doc comments are sufficient.

Do you need me to land the changes on your behalf?

@frederic-tingaud-sonarsource
Copy link
Contributor Author

Thanks @AaronBallman!
Yes, if you could land the changes, that would be great.

@AaronBallman AaronBallman merged commit bc79ec0 into llvm:main Oct 30, 2024
4 of 6 checks passed
smallp-o-p pushed a commit to smallp-o-p/llvm-project that referenced this pull request Nov 3, 2024
… `isInTemplateInstantiation` matchers (llvm#110666)

Fix `isInstantiated` and `isInTemplateInstantiation` matchers, so they
return true for instantiations of variable templates, and any
declaration in statements contained in such instantiations.
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
… `isInTemplateInstantiation` matchers (llvm#110666)

Fix `isInstantiated` and `isInTemplateInstantiation` matchers, so they
return true for instantiations of variable templates, and any
declaration in statements contained in such instantiations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants