From ca39210f6a28569116759f9e62b6120ddd746968 Mon Sep 17 00:00:00 2001 From: Congcong Cai 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, InnerMatcher) { Finder, Builder); } -constexpr std::array NameList{ +constexpr std::array MakeSmartPtrList{ "::std::make_unique", "::std::make_shared", }; -Matcher constructFrom(Matcher TypeMatcher, - Matcher 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 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 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 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 @@

Narrowing Matchers

+Matcher<FunctionDecl>templateArgumentCountIsunsigned N +
Matches if the number of template arguments equals N.
+
+Given
+  template<typename T> struct C {};
+  C<int> c;
+classTemplateSpecializationDecl(templateArgumentCountIs(1))
+  matches C<int>.
+
+ + Matcher<FunctionProtoType>hasDynamicExceptionSpec
Matches functions that have a dynamic exception specification.
 
@@ -5783,8 +5794,8 @@ 

Narrowing Matchers

-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 {};
@@ -6219,6 +6230,17 @@ 

Narrowing Matchers

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;
+classTemplateSpecializationDecl(templateArgumentCountIs(1))
+  matches C<int>.
+
+ From 3e2a7ef8e9d1b698351fb2800759eda4294008c3 Mon Sep 17 00:00:00 2001 From: Congcong Cai 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 @@

Narrowing Matchers

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>();
@@ -4839,8 +4845,14 @@

Narrowing Matchers

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>(); @@ -5800,8 +5812,14 @@

Narrowing Matchers

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>(); @@ -6237,8 +6255,14 @@

Narrowing Matchers

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 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 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, 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 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) {