Skip to content

Commit 3e2a7ef

Browse files
committed
add test and doc
1 parent d4d9536 commit 3e2a7ef

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

clang/docs/LibASTMatchersReference.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4108,8 +4108,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
41084108
Given
41094109
template&lt;typename T&gt; struct C {};
41104110
C&lt;int&gt; c;
4111+
template&lt;typename T&gt; void f() {}
4112+
void func() { f&lt;int&gt;(); };
4113+
41114114
classTemplateSpecializationDecl(templateArgumentCountIs(1))
41124115
matches C&lt;int&gt;.
4116+
4117+
functionDecl(templateArgumentCountIs(1))
4118+
matches f&lt;int&gt;();
41134119
</pre></td></tr>
41144120

41154121

@@ -4839,8 +4845,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
48394845
Given
48404846
template&lt;typename T&gt; struct C {};
48414847
C&lt;int&gt; c;
4848+
template&lt;typename T&gt; void f() {}
4849+
void func() { f&lt;int&gt;(); };
4850+
48424851
classTemplateSpecializationDecl(templateArgumentCountIs(1))
48434852
matches C&lt;int&gt;.
4853+
4854+
functionDecl(templateArgumentCountIs(1))
4855+
matches f&lt;int&gt;();
48444856
</pre></td></tr>
48454857

48464858

@@ -5800,8 +5812,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
58005812
Given
58015813
template&lt;typename T&gt; struct C {};
58025814
C&lt;int&gt; c;
5815+
template&lt;typename T&gt; void f() {}
5816+
void func() { f&lt;int&gt;(); };
5817+
58035818
classTemplateSpecializationDecl(templateArgumentCountIs(1))
58045819
matches C&lt;int&gt;.
5820+
5821+
functionDecl(templateArgumentCountIs(1))
5822+
matches f&lt;int&gt;();
58055823
</pre></td></tr>
58065824

58075825

@@ -6237,8 +6255,14 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
62376255
Given
62386256
template&lt;typename T&gt; struct C {};
62396257
C&lt;int&gt; c;
6258+
template&lt;typename T&gt; void f() {}
6259+
void func() { f&lt;int&gt;(); };
6260+
62406261
classTemplateSpecializationDecl(templateArgumentCountIs(1))
62416262
matches C&lt;int&gt;.
6263+
6264+
functionDecl(templateArgumentCountIs(1))
6265+
matches f&lt;int&gt;();
62426266
</pre></td></tr>
62436267

62446268
<!--END_NARROWING_MATCHERS -->

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,8 @@ AST Matchers
395395
------------
396396

397397
- Ensure ``isDerivedFrom`` matches the correct base in case more than one alias exists.
398+
- Extend ``templateArgumentCountIs`` to support function and variable template
399+
specialization.
398400

399401
clang-format
400402
------------

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,9 +1084,15 @@ AST_POLYMORPHIC_MATCHER_P2(
10841084
/// \code
10851085
/// template<typename T> struct C {};
10861086
/// C<int> c;
1087+
/// template<typename T> void f() {}
1088+
/// void func() { f<int>(); };
10871089
/// \endcode
1090+
///
10881091
/// classTemplateSpecializationDecl(templateArgumentCountIs(1))
10891092
/// matches C<int>.
1093+
///
1094+
/// functionDecl(templateArgumentCountIs(1))
1095+
/// matches f<int>();
10901096
AST_POLYMORPHIC_MATCHER_P(
10911097
templateArgumentCountIs,
10921098
AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,

clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,13 @@ TEST_P(ASTMatchersTest, TemplateArgumentCountIs) {
20282028
EXPECT_TRUE(
20292029
notMatches("template<typename T> struct C {}; C<int> c;",
20302030
templateSpecializationType(templateArgumentCountIs(2))));
2031+
2032+
const char *FuncTemplateCode =
2033+
"template<typename T> T f(); auto v = f<int>();";
2034+
EXPECT_TRUE(
2035+
matches(FuncTemplateCode, functionDecl(templateArgumentCountIs(1))));
2036+
EXPECT_TRUE(
2037+
notMatches(FuncTemplateCode, functionDecl(templateArgumentCountIs(2))));
20312038
}
20322039

20332040
TEST_P(ASTMatchersTest, IsIntegral) {

0 commit comments

Comments
 (0)