Skip to content

[AstMatcher]templateArgumentCountIs support FunctionDecl #130416

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions clang/docs/LibASTMatchersReference.html
Original file line number Diff line number Diff line change
Expand Up @@ -4108,8 +4108,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
Given
template&lt;typename T&gt; struct C {};
C&lt;int&gt; c;
template&lt;typename T&gt; void f() {}
void func() { f&lt;int&gt;(); };

classTemplateSpecializationDecl(templateArgumentCountIs(1))
matches C&lt;int&gt;.

functionDecl(templateArgumentCountIs(1))
matches f&lt;int&gt;();
</pre></td></tr>


Expand Down Expand Up @@ -4833,6 +4839,23 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>&gt;</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&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.

C&lt;int&gt; c;
template&lt;typename T&gt; void f() {}
void func() { f&lt;int&gt;(); };

classTemplateSpecializationDecl(templateArgumentCountIs(1))
matches C&lt;int&gt;.

functionDecl(templateArgumentCountIs(1))
matches f&lt;int&gt;();
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionProtoType.html">FunctionProtoType</a>&gt;</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.

Expand Down Expand Up @@ -5783,14 +5806,20 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1TemplateSpecializationType.html">TemplateSpecializationType</a>&gt;</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&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1TemplateSpecializationType.html">TemplateSpecializationType</a>&gt;</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&lt;typename T&gt; struct C {};
C&lt;int&gt; c;
template&lt;typename T&gt; void f() {}
void func() { f&lt;int&gt;(); };

classTemplateSpecializationDecl(templateArgumentCountIs(1))
matches C&lt;int&gt;.

functionDecl(templateArgumentCountIs(1))
matches f&lt;int&gt;();
</pre></td></tr>


Expand Down Expand Up @@ -6219,6 +6248,23 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
Usable as: Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>&gt;, Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>&gt;, Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html">CXXRecordDecl</a>&gt;
</pre></td></tr>


<tr><td>Matcher&lt;<a href="https://clang.llvm.org/doxygen/classclang_1_1VarTemplateSpecializationDecl.html">VarTemplateSpecializationDecl</a>&gt;</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&lt;typename T&gt; struct C {};
C&lt;int&gt; c;
template&lt;typename T&gt; void f() {}
void func() { f&lt;int&gt;(); };

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

matches C&lt;int&gt;.

functionDecl(templateArgumentCountIs(1))
matches f&lt;int&gt;();
</pre></td></tr>

<!--END_NARROWING_MATCHERS -->
</table>

Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1084,12 +1084,19 @@ 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,
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?

TemplateSpecializationType),
unsigned, N) {
return internal::getTemplateSpecializationArgs(Node).size() == N;
Expand Down
7 changes: 7 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down