[clang] [clang-tools-extra] [AstMatcher]`templateArgumentCountIs` support `FunctionDecl` (PR #130416)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 11 08:54:13 PDT 2025
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/130416
>From ca39210f6a28569116759f9e62b6120ddd746968 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 8 Mar 2025 21:22:47 +0800
Subject: [PATCH 1/5] [clang-tidy][NFC]refactor matcher for
bugprone-optional-value-conversion
The old `constructFrom` has hidden requirement which TypeMatcher must be used before ArgumentMatcher because there are bind inside.
Inlining this function to make it more intuitive.
---
.../bugprone/OptionalValueConversionCheck.cpp | 42 +++++++++----------
1 file changed, 19 insertions(+), 23 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
index 55ca4809f058a..33e823ac07490 100644
--- a/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/OptionalValueConversionCheck.cpp
@@ -27,28 +27,11 @@ AST_MATCHER_P(QualType, hasCleanType, Matcher<QualType>, InnerMatcher) {
Finder, Builder);
}
-constexpr std::array<StringRef, 2> NameList{
+constexpr std::array<StringRef, 2> MakeSmartPtrList{
"::std::make_unique",
"::std::make_shared",
};
-Matcher<Expr> constructFrom(Matcher<QualType> TypeMatcher,
- Matcher<Expr> ArgumentMatcher) {
- return expr(
- anyOf(
- // construct optional
- cxxConstructExpr(argumentCountIs(1U), hasType(TypeMatcher),
- hasArgument(0U, ArgumentMatcher)),
- // known template methods in std
- callExpr(argumentCountIs(1),
- callee(functionDecl(
- matchers::matchesAnyListedName(NameList),
- hasTemplateArgument(0, refersToType(TypeMatcher)))),
- hasArgument(0, ArgumentMatcher))),
- unless(anyOf(hasAncestor(typeLoc()),
- hasAncestor(expr(matchers::hasUnevaluatedContext())))));
-}
-
} // namespace
OptionalValueConversionCheck::OptionalValueConversionCheck(
@@ -74,7 +57,7 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
auto EqualsBoundOptionalType =
qualType(hasCleanType(equalsBoundNode("optional-type")));
- auto OptionalDereferenceMatcher = callExpr(
+ auto OptionalDerefMatcherImpl = callExpr(
anyOf(
cxxOperatorCallExpr(hasOverloadedOperatorName("*"),
hasUnaryOperand(hasType(EqualsBoundOptionalType)))
@@ -88,11 +71,24 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
auto StdMoveCallMatcher =
callExpr(argumentCountIs(1), callee(functionDecl(hasName("::std::move"))),
- hasArgument(0, ignoringImpCasts(OptionalDereferenceMatcher)));
+ hasArgument(0, ignoringImpCasts(OptionalDerefMatcherImpl)));
+ auto OptionalDerefMatcher =
+ ignoringImpCasts(anyOf(OptionalDerefMatcherImpl, StdMoveCallMatcher));
+
Finder->addMatcher(
- expr(constructFrom(BindOptionalType,
- ignoringImpCasts(anyOf(OptionalDereferenceMatcher,
- StdMoveCallMatcher))))
+ expr(anyOf(
+ // construct optional
+ cxxConstructExpr(argumentCountIs(1), hasType(BindOptionalType),
+ hasArgument(0, OptionalDerefMatcher)),
+ // known template methods in std
+ callExpr(
+ argumentCountIs(1),
+ callee(functionDecl(
+ matchers::matchesAnyListedName(MakeSmartPtrList),
+ hasTemplateArgument(0, refersToType(BindOptionalType)))),
+ hasArgument(0, OptionalDerefMatcher))),
+ unless(anyOf(hasAncestor(typeLoc()),
+ hasAncestor(expr(matchers::hasUnevaluatedContext())))))
.bind("expr"),
this);
}
>From 97ea31513d00893e797ff3c97ac441e54a11bbb8 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 8 Mar 2025 21:33:02 +0800
Subject: [PATCH 2/5] [AstMatcher]`templateArgumentCountIs` support
`FunctionDecl`
`hasTemplateArgument` and `templateArgumentCountIs` are always used together. It is more convenient to make then support `FunctionDecl`.
---
clang/include/clang/ASTMatchers/ASTMatchers.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 0f7e3a8a01762..03d522072f6c1 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1090,6 +1090,7 @@ AST_POLYMORPHIC_MATCHER_P2(
AST_POLYMORPHIC_MATCHER_P(
templateArgumentCountIs,
AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
+ VarTemplateSpecializationDecl, FunctionDecl,
TemplateSpecializationType),
unsigned, N) {
return internal::getTemplateSpecializationArgs(Node).size() == N;
>From b28bd17fd58c6f41b3d860cb490cdbfeef8b8c3e Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Sat, 8 Mar 2025 21:33:02 +0800
Subject: [PATCH 3/5] [AstMatcher]`templateArgumentCountIs` support
`FunctionDecl`
`hasTemplateArgument` and `templateArgumentCountIs` are always used together. It is more convenient to make then support `FunctionDecl`.
---
clang/include/clang/ASTMatchers/ASTMatchers.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 0f7e3a8a01762..03d522072f6c1 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1090,6 +1090,7 @@ AST_POLYMORPHIC_MATCHER_P2(
AST_POLYMORPHIC_MATCHER_P(
templateArgumentCountIs,
AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
+ VarTemplateSpecializationDecl, FunctionDecl,
TemplateSpecializationType),
unsigned, N) {
return internal::getTemplateSpecializationArgs(Node).size() == N;
>From c04d320a0184d5009bdeb98b2e8c1fddd0f91a4d Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 11 Mar 2025 17:05:54 +0800
Subject: [PATCH 4/5] add doc
---
clang/docs/LibASTMatchersReference.html | 26 +++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index 2a01d6cda6bed..7018750ad82e7 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -4833,6 +4833,17 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</pre></td></tr>
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('templateArgumentCountIs2')"><a name="templateArgumentCountIs2Anchor">templateArgumentCountIs</a></td><td>unsigned N</td></tr>
+<tr><td colspan="4" class="doc" id="templateArgumentCountIs2"><pre>Matches if the number of template arguments equals N.
+
+Given
+ template<typename T> struct C {};
+ C<int> c;
+classTemplateSpecializationDecl(templateArgumentCountIs(1))
+ matches C<int>.
+</pre></td></tr>
+
+
<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionProtoType.html">FunctionProtoType</a>></td><td class="name" onclick="toggle('hasDynamicExceptionSpec1')"><a name="hasDynamicExceptionSpec1Anchor">hasDynamicExceptionSpec</a></td><td></td></tr>
<tr><td colspan="4" class="doc" id="hasDynamicExceptionSpec1"><pre>Matches functions that have a dynamic exception specification.
@@ -5783,8 +5794,8 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</pre></td></tr>
-<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1TemplateSpecializationType.html">TemplateSpecializationType</a>></td><td class="name" onclick="toggle('templateArgumentCountIs1')"><a name="templateArgumentCountIs1Anchor">templateArgumentCountIs</a></td><td>unsigned N</td></tr>
-<tr><td colspan="4" class="doc" id="templateArgumentCountIs1"><pre>Matches if the number of template arguments equals N.
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1TemplateSpecializationType.html">TemplateSpecializationType</a>></td><td class="name" onclick="toggle('templateArgumentCountIs3')"><a name="templateArgumentCountIs3Anchor">templateArgumentCountIs</a></td><td>unsigned N</td></tr>
+<tr><td colspan="4" class="doc" id="templateArgumentCountIs3"><pre>Matches if the number of template arguments equals N.
Given
template<typename T> struct C {};
@@ -6219,6 +6230,17 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
Usable as: Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>>, Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>>, Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html">CXXRecordDecl</a>>
</pre></td></tr>
+
+<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1VarTemplateSpecializationDecl.html">VarTemplateSpecializationDecl</a>></td><td class="name" onclick="toggle('templateArgumentCountIs1')"><a name="templateArgumentCountIs1Anchor">templateArgumentCountIs</a></td><td>unsigned N</td></tr>
+<tr><td colspan="4" class="doc" id="templateArgumentCountIs1"><pre>Matches if the number of template arguments equals N.
+
+Given
+ template<typename T> struct C {};
+ C<int> c;
+classTemplateSpecializationDecl(templateArgumentCountIs(1))
+ matches C<int>.
+</pre></td></tr>
+
<!--END_NARROWING_MATCHERS -->
</table>
>From 3e2a7ef8e9d1b698351fb2800759eda4294008c3 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 11 Mar 2025 15:53:28 +0000
Subject: [PATCH 5/5] add test and doc
---
clang/docs/LibASTMatchersReference.html | 24 +++++++++++++++++++
clang/docs/ReleaseNotes.rst | 2 ++
clang/include/clang/ASTMatchers/ASTMatchers.h | 6 +++++
.../ASTMatchers/ASTMatchersNarrowingTest.cpp | 7 ++++++
4 files changed, 39 insertions(+)
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index 7018750ad82e7..9b30057b5257f 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -4108,8 +4108,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
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>();
</pre></td></tr>
@@ -4839,8 +4845,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
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>();
</pre></td></tr>
@@ -5800,8 +5812,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
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>();
</pre></td></tr>
@@ -6237,8 +6255,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
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>();
</pre></td></tr>
<!--END_NARROWING_MATCHERS -->
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9e68e23c15580..0afde62b6e814 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -395,6 +395,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 a45f2dbc306ad..738617759eb29 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1084,9 +1084,15 @@ AST_POLYMORPHIC_MATCHER_P2(
/// \code
/// template<typename T> struct C {};
/// C<int> c;
+/// template<typename T> void f() {}
+/// void func() { f<int>(); };
/// \endcode
+///
/// classTemplateSpecializationDecl(templateArgumentCountIs(1))
/// matches C<int>.
+///
+/// functionDecl(templateArgumentCountIs(1))
+/// matches f<int>();
AST_POLYMORPHIC_MATCHER_P(
templateArgumentCountIs,
AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
diff --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 4e6baedae2be5..49abe881eeabb 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<typename T> struct C {}; C<int> c;",
templateSpecializationType(templateArgumentCountIs(2))));
+
+ const char *FuncTemplateCode =
+ "template<typename T> T f(); auto v = f<int>();";
+ EXPECT_TRUE(
+ matches(FuncTemplateCode, functionDecl(templateArgumentCountIs(1))));
+ EXPECT_TRUE(
+ notMatches(FuncTemplateCode, functionDecl(templateArgumentCountIs(2))));
}
TEST_P(ASTMatchersTest, IsIntegral) {
More information about the cfe-commits
mailing list