This revision was automatically updated to reflect the committed changes. Closed by commit rL277142: [ASTMatcher] Add hasTemplateArgument/hasAnyTemplateArgument support in… (authored by hokein).
Changed prior to commit: https://reviews.llvm.org/D22957?vs=66094&id=66119#toc Repository: rL LLVM https://reviews.llvm.org/D22957 Files: cfe/trunk/docs/LibASTMatchersReference.html cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Index: cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h +++ cfe/trunk/include/clang/ASTMatchers/ASTMatchers.h @@ -556,22 +556,32 @@ return Node.isImplicit(); } -/// \brief Matches classTemplateSpecializations that have at least one -/// TemplateArgument matching the given InnerMatcher. +/// \brief Matches classTemplateSpecializations, templateSpecializationType and +/// functionDecl that have at least one TemplateArgument matching the given +/// InnerMatcher. /// /// Given /// \code /// template<typename T> class A {}; /// template<> class A<double> {}; /// A<int> a; +/// +/// template<typenmae T> f() {}; +/// void func() { f<int>(); }; +/// \endcode +/// /// \endcode /// classTemplateSpecializationDecl(hasAnyTemplateArgument( /// refersToType(asString("int")))) /// matches the specialization \c A<int> +/// +/// functionDecl(hasAnyTemplateArgument(refersToType(asString("int")))) +/// matches the specialization \c f<int> AST_POLYMORPHIC_MATCHER_P( hasAnyTemplateArgument, AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl, - TemplateSpecializationType), + TemplateSpecializationType, + FunctionDecl), internal::Matcher<TemplateArgument>, InnerMatcher) { ArrayRef<TemplateArgument> List = internal::getTemplateSpecializationArgs(Node); @@ -698,22 +708,29 @@ return InnerMatcher.matches(Node.IgnoreParens(), Finder, Builder); } -/// \brief Matches classTemplateSpecializations where the n'th TemplateArgument -/// matches the given InnerMatcher. +/// \brief Matches classTemplateSpecializations, templateSpecializationType and +/// functionDecl where the n'th TemplateArgument matches the given InnerMatcher. /// /// Given /// \code /// template<typename T, typename U> class A {}; /// A<bool, int> b; /// A<int, bool> c; +/// +/// template<typenmae T> f() {}; +/// void func() { f<int>(); }; /// \endcode /// classTemplateSpecializationDecl(hasTemplateArgument( /// 1, refersToType(asString("int")))) /// matches the specialization \c A<bool, int> +/// +/// functionDecl(hasTemplateArgument(0, refersToType(asString("int")))) +/// matches the specialization \c f<int> AST_POLYMORPHIC_MATCHER_P2( hasTemplateArgument, AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl, - TemplateSpecializationType), + TemplateSpecializationType, + FunctionDecl), unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) { ArrayRef<TemplateArgument> List = internal::getTemplateSpecializationArgs(Node); Index: cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h =================================================================== --- cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h +++ cfe/trunk/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -1638,6 +1638,13 @@ return llvm::makeArrayRef(T.getArgs(), T.getNumArgs()); } +inline ArrayRef<TemplateArgument> +getTemplateSpecializationArgs(const FunctionDecl &FD) { + if (const auto* TemplateArgs = FD.getTemplateSpecializationArgs()) + return TemplateArgs->asArray(); + return ArrayRef<TemplateArgument>(); +} + struct NotEqualsBoundNodePredicate { bool operator()(const internal::BoundNodesMap &Nodes) const { return Nodes.getNode(ID) != Node; Index: cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -594,6 +594,14 @@ "A<int, bool> a;", templateSpecializationType(hasTemplateArgument( 1, refersToType(asString("int")))))); + + EXPECT_TRUE(matches( + "template<typename T> void f() {};" + "void func() { f<int>(); }", + functionDecl(hasTemplateArgument(0, refersToType(asString("int")))))); + EXPECT_TRUE(notMatches( + "template<typename T> void f() {};", + functionDecl(hasTemplateArgument(0, refersToType(asString("int")))))); } TEST(TemplateArgument, Matches) { @@ -603,6 +611,11 @@ EXPECT_TRUE(matches( "template<typename T> struct C {}; C<int> c;", templateSpecializationType(hasAnyTemplateArgument(templateArgument())))); + + EXPECT_TRUE(matches( + "template<typename T> void f() {};" + "void func() { f<int>(); }", + functionDecl(hasAnyTemplateArgument(templateArgument())))); } TEST(RefersToIntegralType, Matches) { Index: cfe/trunk/docs/LibASTMatchersReference.html =================================================================== --- cfe/trunk/docs/LibASTMatchersReference.html +++ cfe/trunk/docs/LibASTMatchersReference.html @@ -4240,30 +4240,44 @@ <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ClassTemplateSpecializationDecl.html">ClassTemplateSpecializationDecl</a>></td><td class="name" onclick="toggle('hasAnyTemplateArgument0')"><a name="hasAnyTemplateArgument0Anchor">hasAnyTemplateArgument</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateArgument.html">TemplateArgument</a>> InnerMatcher</td></tr> -<tr><td colspan="4" class="doc" id="hasAnyTemplateArgument0"><pre>Matches classTemplateSpecializations that have at least one -TemplateArgument matching the given InnerMatcher. +<tr><td colspan="4" class="doc" id="hasAnyTemplateArgument0"><pre>Matches classTemplateSpecializations, templateSpecializationType and +functionDecl that have at least one TemplateArgument matching the given +InnerMatcher. Given template<typename T> class A {}; template<> class A<double> {}; A<int> a; + + template<typenmae T> f() {}; + void func() { f<int>(); }; + classTemplateSpecializationDecl(hasAnyTemplateArgument( refersToType(asString("int")))) matches the specialization A<int> + +functionDecl(hasAnyTemplateArgument(refersToType(asString("int")))) + matches the specialization f<int> </pre></td></tr> <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ClassTemplateSpecializationDecl.html">ClassTemplateSpecializationDecl</a>></td><td class="name" onclick="toggle('hasTemplateArgument0')"><a name="hasTemplateArgument0Anchor">hasTemplateArgument</a></td><td>unsigned N, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateArgument.html">TemplateArgument</a>> InnerMatcher</td></tr> -<tr><td colspan="4" class="doc" id="hasTemplateArgument0"><pre>Matches classTemplateSpecializations where the n'th TemplateArgument -matches the given InnerMatcher. +<tr><td colspan="4" class="doc" id="hasTemplateArgument0"><pre>Matches classTemplateSpecializations, templateSpecializationType and +functionDecl where the n'th TemplateArgument matches the given InnerMatcher. Given template<typename T, typename U> class A {}; A<bool, int> b; A<int, bool> c; + + template<typenmae T> f() {}; + void func() { f<int>(); }; classTemplateSpecializationDecl(hasTemplateArgument( 1, refersToType(asString("int")))) matches the specialization A<bool, int> + +functionDecl(hasTemplateArgument(0, refersToType(asString("int")))) + matches the specialization f<int> </pre></td></tr> @@ -4679,6 +4693,28 @@ </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('hasAnyTemplateArgument2')"><a name="hasAnyTemplateArgument2Anchor">hasAnyTemplateArgument</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateArgument.html">TemplateArgument</a>> InnerMatcher</td></tr> +<tr><td colspan="4" class="doc" id="hasAnyTemplateArgument2"><pre>Matches classTemplateSpecializations, templateSpecializationType and +functionDecl that have at least one TemplateArgument matching the given +InnerMatcher. + +Given + template<typename T> class A {}; + template<> class A<double> {}; + A<int> a; + + template<typenmae T> f() {}; + void func() { f<int>(); }; + +classTemplateSpecializationDecl(hasAnyTemplateArgument( + refersToType(asString("int")))) + matches the specialization A<int> + +functionDecl(hasAnyTemplateArgument(refersToType(asString("int")))) + matches the specialization f<int> +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('hasBody4')"><a name="hasBody4Anchor">hasBody</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="hasBody4"><pre>Matches a 'for', 'while', 'do while' statement or a function definition that has a given body. @@ -4704,6 +4740,26 @@ </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('hasTemplateArgument2')"><a name="hasTemplateArgument2Anchor">hasTemplateArgument</a></td><td>unsigned N, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateArgument.html">TemplateArgument</a>> InnerMatcher</td></tr> +<tr><td colspan="4" class="doc" id="hasTemplateArgument2"><pre>Matches classTemplateSpecializations, templateSpecializationType and +functionDecl where the n'th TemplateArgument matches the given InnerMatcher. + +Given + template<typename T, typename U> class A {}; + A<bool, int> b; + A<int, bool> c; + + template<typenmae T> f() {}; + void func() { f<int>(); }; +classTemplateSpecializationDecl(hasTemplateArgument( + 1, refersToType(asString("int")))) + matches the specialization A<bool, int> + +functionDecl(hasTemplateArgument(0, refersToType(asString("int")))) + matches the specialization f<int> +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('returns0')"><a name="returns0Anchor">returns</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1QualType.html">QualType</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="returns0"><pre>Matches the return type of a function declaration. @@ -5311,16 +5367,24 @@ <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateSpecializationType.html">TemplateSpecializationType</a>></td><td class="name" onclick="toggle('hasAnyTemplateArgument1')"><a name="hasAnyTemplateArgument1Anchor">hasAnyTemplateArgument</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateArgument.html">TemplateArgument</a>> InnerMatcher</td></tr> -<tr><td colspan="4" class="doc" id="hasAnyTemplateArgument1"><pre>Matches classTemplateSpecializations that have at least one -TemplateArgument matching the given InnerMatcher. +<tr><td colspan="4" class="doc" id="hasAnyTemplateArgument1"><pre>Matches classTemplateSpecializations, templateSpecializationType and +functionDecl that have at least one TemplateArgument matching the given +InnerMatcher. Given template<typename T> class A {}; template<> class A<double> {}; A<int> a; + + template<typenmae T> f() {}; + void func() { f<int>(); }; + classTemplateSpecializationDecl(hasAnyTemplateArgument( refersToType(asString("int")))) matches the specialization A<int> + +functionDecl(hasAnyTemplateArgument(refersToType(asString("int")))) + matches the specialization f<int> </pre></td></tr> @@ -5347,16 +5411,22 @@ <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateSpecializationType.html">TemplateSpecializationType</a>></td><td class="name" onclick="toggle('hasTemplateArgument1')"><a name="hasTemplateArgument1Anchor">hasTemplateArgument</a></td><td>unsigned N, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TemplateArgument.html">TemplateArgument</a>> InnerMatcher</td></tr> -<tr><td colspan="4" class="doc" id="hasTemplateArgument1"><pre>Matches classTemplateSpecializations where the n'th TemplateArgument -matches the given InnerMatcher. +<tr><td colspan="4" class="doc" id="hasTemplateArgument1"><pre>Matches classTemplateSpecializations, templateSpecializationType and +functionDecl where the n'th TemplateArgument matches the given InnerMatcher. Given template<typename T, typename U> class A {}; A<bool, int> b; A<int, bool> c; + + template<typenmae T> f() {}; + void func() { f<int>(); }; classTemplateSpecializationDecl(hasTemplateArgument( 1, refersToType(asString("int")))) matches the specialization A<bool, int> + +functionDecl(hasTemplateArgument(0, refersToType(asString("int")))) + matches the specialization f<int> </pre></td></tr>
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits