From 4d6ca116221a0f49232999c60a986ede987f7b46 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Wed, 12 Mar 2025 06:08:10 +0800 Subject: [PATCH] [AstMatcher]`templateArgumentCountIs` support `FunctionDecl` (#130416) `hasTemplateArgument` and `templateArgumentCountIs` are always used together. It is more convenient to make then support `FunctionDecl`. --- clang/docs/LibASTMatchersReference.html | 50 ++++++++++++++++++- clang/docs/ReleaseNotes.rst | 2 + clang/include/clang/ASTMatchers/ASTMatchers.h | 7 +++ .../ASTMatchers/ASTMatchersNarrowingTest.cpp | 7 +++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index 2a01d6cda6be..9b30057b5257 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -4108,8 +4108,14 @@ Usable as: Matcher<FunctionDecl>templateArgumentCountIsunsigned N +
Matches if the number of template arguments equals N.
+
+Given
+  template<typename T> struct C {};
+  C<int> c;
+  template<typename T> void f() {}
+  void func() { f<int>(); };
+
+classTemplateSpecializationDecl(templateArgumentCountIs(1))
+  matches C<int>.
+
+functionDecl(templateArgumentCountIs(1))
+  matches f<int>();
+
+ + Matcher<FunctionProtoType>hasDynamicExceptionSpec
Matches functions that have a dynamic exception specification.
 
@@ -5783,14 +5806,20 @@ classTemplateSpecializationDecl(
 
-Matcher<TemplateSpecializationType>templateArgumentCountIsunsigned N -
Matches if the number of template arguments equals N.
+Matcher<TemplateSpecializationType>templateArgumentCountIsunsigned N
+
Matches if the number of template arguments equals N.
 
 Given
   template<typename T> struct C {};
   C<int> c;
+  template<typename T> void f() {}
+  void func() { f<int>(); };
+
 classTemplateSpecializationDecl(templateArgumentCountIs(1))
   matches C<int>.
+
+functionDecl(templateArgumentCountIs(1))
+  matches f<int>();
 
@@ -6219,6 +6248,23 @@ cxxRecordDecl(hasName("::X"), isTemplateInstantiation()) Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
+ +Matcher<VarTemplateSpecializationDecl>templateArgumentCountIsunsigned N +
Matches if the number of template arguments equals N.
+
+Given
+  template<typename T> struct C {};
+  C<int> c;
+  template<typename T> void f() {}
+  void func() { f<int>(); };
+
+classTemplateSpecializationDecl(templateArgumentCountIs(1))
+  matches C<int>.
+
+functionDecl(templateArgumentCountIs(1))
+  matches f<int>();
+
+ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 004f78f22ac3..b4f8e182408f 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -397,6 +397,8 @@ AST Matchers ------------ - Ensure ``isDerivedFrom`` matches the correct base in case more than one alias exists. +- Extend ``templateArgumentCountIs`` to support function and variable template + specialization. clang-format ------------ diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 5c12bf0f277a..738617759eb2 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -1084,12 +1084,19 @@ AST_POLYMORPHIC_MATCHER_P2( /// \code /// template struct C {}; /// C c; +/// template void f() {} +/// void func() { f(); }; /// \endcode +/// /// classTemplateSpecializationDecl(templateArgumentCountIs(1)) /// matches C. +/// +/// functionDecl(templateArgumentCountIs(1)) +/// matches f(); AST_POLYMORPHIC_MATCHER_P( templateArgumentCountIs, AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl, + VarTemplateSpecializationDecl, FunctionDecl, TemplateSpecializationType), unsigned, N) { return internal::getTemplateSpecializationArgs(Node).size() == N; diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp index 4e6baedae2be..49abe881eeab 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -2028,6 +2028,13 @@ TEST_P(ASTMatchersTest, TemplateArgumentCountIs) { EXPECT_TRUE( notMatches("template struct C {}; C c;", templateSpecializationType(templateArgumentCountIs(2)))); + + const char *FuncTemplateCode = + "template T f(); auto v = f();"; + EXPECT_TRUE( + matches(FuncTemplateCode, functionDecl(templateArgumentCountIs(1)))); + EXPECT_TRUE( + notMatches(FuncTemplateCode, functionDecl(templateArgumentCountIs(2)))); } TEST_P(ASTMatchersTest, IsIntegral) {