Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AstMatcher]templateArgumentCountIs support FunctionDecl #130416

Conversation

HerrCai0907
Copy link
Contributor

hasTemplateArgument and templateArgumentCountIs are always used together. It is more convenient to make then support FunctionDecl.

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.
`hasTemplateArgument` and `templateArgumentCountIs` are always used together. It is more convenient to make then support `FunctionDecl`.
@llvmbot
Copy link
Member

llvmbot commented Mar 8, 2025

@llvm/pr-subscribers-clang

Author: Congcong Cai (HerrCai0907)

Changes

hasTemplateArgument and templateArgumentCountIs are always used together. It is more convenient to make then support FunctionDecl.


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

1 Files Affected:

  • (modified) clang/include/clang/ASTMatchers/ASTMatchers.h (+1)
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;

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

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

You may need to update documentation for this matcher in clang/docs/LibASTMatchersReference.html

Copy link
Contributor Author

dump-ast-matchers has some bug. I will fix it after #130726 merging

Base automatically changed from users/ccc03-08-_clang-tidy_nfc_refactor_matcher_for_bugprone-optional-value-conversion to main March 11, 2025 06:52
`hasTemplateArgument` and `templateArgumentCountIs` are always used together. It is more convenient to make then support `FunctionDecl`.
@HerrCai0907 HerrCai0907 force-pushed the users/ccc03-08-_astmatcher_templateargumentcountis_support_functiondecl_ branch from 97ea315 to b28bd17 Compare March 11, 2025 06:54
…tcher_templateargumentcountis_support_functiondecl_
…ateargumentcountis_support_functiondecl_' into users/ccc03-08-_astmatcher_templateargumentcountis_support_functiondecl_
@HerrCai0907 HerrCai0907 requested a review from PiotrZSL March 11, 2025 09:07
<tr><td colspan="4" class="doc" id="templateArgumentCountIs2"><pre>Matches if the number of template arguments equals N.

Given
template&lt;typename T&gt; struct C {};
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this example be for functionDecl, instead of classTemplateSpecializationDecl?

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is auto-generated from the python script based on the comments on the matcher.

Given
template&lt;typename T&gt; struct C {};
C&lt;int&gt; c;
classTemplateSpecializationDecl(templateArgumentCountIs(1))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

@carlosgalvezp
Copy link
Contributor

Adding @AaronBallman as owner for ASTMatchers

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.

Generally LG, but I did have a testing and commenting request.

Please also add a note to clang/docs/ReleaseNotes.rst so users know about the new match.

<tr><td colspan="4" class="doc" id="templateArgumentCountIs2"><pre>Matches if the number of template arguments equals N.

Given
template&lt;typename T&gt; struct C {};
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is auto-generated from the python script based on the comments on the matcher.

@@ -1090,6 +1090,7 @@ AST_POLYMORPHIC_MATCHER_P2(
AST_POLYMORPHIC_MATCHER_P(
templateArgumentCountIs,
AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
VarTemplateSpecializationDecl, FunctionDecl,
Copy link
Collaborator

Choose a reason for hiding this comment

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

It'd probably be good to update the example code above. Also, can you add test coverage to clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp?

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

Copy link
Contributor Author

HerrCai0907 commented Mar 11, 2025

Merge activity

  • Mar 11, 6:00 PM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Mar 11, 6:03 PM EDT: Graphite couldn't merge this PR because it had conflicts with the trunk branch.
  • Mar 11, 6:06 PM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Mar 11, 6:08 PM EDT: A user merged this pull request with Graphite.

@HerrCai0907 HerrCai0907 merged commit 4d6ca11 into main Mar 11, 2025
12 checks passed
@HerrCai0907 HerrCai0907 deleted the users/ccc03-08-_astmatcher_templateargumentcountis_support_functiondecl_ branch March 11, 2025 22:08
Bertik23 pushed a commit to Bertik23/llvm-project that referenced this pull request Mar 12, 2025
…0416)

`hasTemplateArgument` and `templateArgumentCountIs` are always used together. It is more convenient to make then support `FunctionDecl`.
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.

5 participants