malcolm.parsons updated this revision to Diff 80465. malcolm.parsons added a comment.
Use AST_TYPE_TRAVERSE_MATCHER. https://reviews.llvm.org/D27447 Files: docs/LibASTMatchersReference.html include/clang/ASTMatchers/ASTMatchers.h lib/ASTMatchers/Dynamic/Registry.cpp unittests/ASTMatchers/ASTMatchersTraversalTest.cpp Index: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -2204,5 +2204,22 @@ functionDecl(hasName("bar")))))); } +TEST(SubstTemplateTypeParmType, HasReplacementType) +{ + std::string Fragment = "template<typename T>" + "double F(T t);" + "int i;" + "double j = F(i);"; + EXPECT_TRUE(matches(Fragment, substTemplateTypeParmType(hasReplacementType( + qualType(asString("int")))))); + EXPECT_TRUE(notMatches(Fragment, substTemplateTypeParmType(hasReplacementType( + qualType(asString("double")))))); + EXPECT_TRUE( + notMatches("template<int N>" + "double F();" + "double j = F<5>();", + substTemplateTypeParmType(hasReplacementType(qualType())))); +} + } // namespace ast_matchers } // namespace clang Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -252,6 +252,7 @@ REGISTER_MATCHER(hasQualifier); REGISTER_MATCHER(hasRangeInit); REGISTER_MATCHER(hasReceiverType); + REGISTER_MATCHER(hasReplacementType); REGISTER_MATCHER(hasReturnValue); REGISTER_MATCHER(hasRHS); REGISTER_MATCHER(hasSelector); Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -5019,6 +5019,22 @@ /// \c substTemplateTypeParmType() matches the type of 't' but not '1' AST_TYPE_MATCHER(SubstTemplateTypeParmType, substTemplateTypeParmType); +/// \brief Matches template type parameter substitutions that have a replacement +/// type that matches the provided matcher. +/// +/// Given +/// \code +/// template <typename T> +/// double F(T t); +/// int i; +/// double j = F(i); +/// \endcode +/// +/// \c substTemplateTypeParmType(hasReplacementType(type())) matches int +AST_TYPE_TRAVERSE_MATCHER( + hasReplacementType, getReplacementType, + AST_POLYMORPHIC_SUPPORTED_TYPES(SubstTemplateTypeParmType)); + /// \brief Matches template type parameter types. /// /// Example matches T, but not int. Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -5421,6 +5421,20 @@ </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1SubstTemplateTypeParmType.html">SubstTemplateTypeParmType</a>></td><td class="name" onclick="toggle('hasReplacementType0')"><a name="hasReplacementType0Anchor">hasReplacementType</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td></tr> +<tr><td colspan="4" class="doc" id="hasReplacementType0"><pre>Matches template type parameter substitutions that have a replacement +type that matches the provided matcher. + +Given + template <typename T> + double F(T t); + int i; + double j = F(i); + +substTemplateTypeParmType(hasReplacementType(type())) matches int +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1SwitchStmt.html">SwitchStmt</a>></td><td class="name" onclick="toggle('forEachSwitchCase0')"><a name="forEachSwitchCase0Anchor">forEachSwitchCase</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1SwitchCase.html">SwitchCase</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="forEachSwitchCase0"><pre>Matches each case or default statement belonging to the given switch statement. This matcher may produce multiple matches.
Index: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -2204,5 +2204,22 @@ functionDecl(hasName("bar")))))); } +TEST(SubstTemplateTypeParmType, HasReplacementType) +{ + std::string Fragment = "template<typename T>" + "double F(T t);" + "int i;" + "double j = F(i);"; + EXPECT_TRUE(matches(Fragment, substTemplateTypeParmType(hasReplacementType( + qualType(asString("int")))))); + EXPECT_TRUE(notMatches(Fragment, substTemplateTypeParmType(hasReplacementType( + qualType(asString("double")))))); + EXPECT_TRUE( + notMatches("template<int N>" + "double F();" + "double j = F<5>();", + substTemplateTypeParmType(hasReplacementType(qualType())))); +} + } // namespace ast_matchers } // namespace clang Index: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -252,6 +252,7 @@ REGISTER_MATCHER(hasQualifier); REGISTER_MATCHER(hasRangeInit); REGISTER_MATCHER(hasReceiverType); + REGISTER_MATCHER(hasReplacementType); REGISTER_MATCHER(hasReturnValue); REGISTER_MATCHER(hasRHS); REGISTER_MATCHER(hasSelector); Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -5019,6 +5019,22 @@ /// \c substTemplateTypeParmType() matches the type of 't' but not '1' AST_TYPE_MATCHER(SubstTemplateTypeParmType, substTemplateTypeParmType); +/// \brief Matches template type parameter substitutions that have a replacement +/// type that matches the provided matcher. +/// +/// Given +/// \code +/// template <typename T> +/// double F(T t); +/// int i; +/// double j = F(i); +/// \endcode +/// +/// \c substTemplateTypeParmType(hasReplacementType(type())) matches int +AST_TYPE_TRAVERSE_MATCHER( + hasReplacementType, getReplacementType, + AST_POLYMORPHIC_SUPPORTED_TYPES(SubstTemplateTypeParmType)); + /// \brief Matches template type parameter types. /// /// Example matches T, but not int. Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -5421,6 +5421,20 @@ </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1SubstTemplateTypeParmType.html">SubstTemplateTypeParmType</a>></td><td class="name" onclick="toggle('hasReplacementType0')"><a name="hasReplacementType0Anchor">hasReplacementType</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Type.html">Type</a>></td></tr> +<tr><td colspan="4" class="doc" id="hasReplacementType0"><pre>Matches template type parameter substitutions that have a replacement +type that matches the provided matcher. + +Given + template <typename T> + double F(T t); + int i; + double j = F(i); + +substTemplateTypeParmType(hasReplacementType(type())) matches int +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1SwitchStmt.html">SwitchStmt</a>></td><td class="name" onclick="toggle('forEachSwitchCase0')"><a name="forEachSwitchCase0Anchor">forEachSwitchCase</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1SwitchCase.html">SwitchCase</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="forEachSwitchCase0"><pre>Matches each case or default statement belonging to the given switch statement. This matcher may produce multiple matches.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits