oontvoo updated this revision to Diff 266615. oontvoo marked 7 inline comments as done. oontvoo added a comment.
- Added docs - Make it work with {block,objcMethod}Decl too. More tests Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D80603/new/ https://reviews.llvm.org/D80603 Files: clang/docs/LibASTMatchersReference.html clang/include/clang/ASTMatchers/ASTMatchers.h clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -2643,6 +2643,29 @@ parmVarDecl(hasDefaultArgument()))); } +TEST(IsAtPosition, Basic) { + EXPECT_TRUE(matches("void x(int a) {}", parmVarDecl(isAtPosition(0)))); + EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0)))); + EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1)))); + EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1)))); + + // Tests with function-decls + EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0)))); + EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0)))); + EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1)))); + EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1)))); + + // Tests with lamdas + EXPECT_TRUE( + matches("void x() { [](int a) {}; }", parmVarDecl(isAtPosition(0)))); + EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }", + parmVarDecl(isAtPosition(0)))); + EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }", + parmVarDecl(isAtPosition(1)))); + EXPECT_TRUE( + notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1)))); +} + TEST(IsArray, Basic) { EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];", cxxNewExpr(isArray()))); Index: clang/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -3096,6 +3096,37 @@ internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc>> hasAncestor; +/// Matches the ParmVarDecl nodes that are at the N'th position in the parameter +/// list. The parameter list could be that of either a block, function, or +/// objc-method. +/// +/// +/// Given +/// +/// \code +/// void f(int a, int b, int c) { +/// } +/// \endcode +/// +/// ``parmVarDecl(isAtPosition(0))`` matches ``int a``. +/// +/// ``parmVarDecl(isAtPosition(1))`` matches ``int b``. +AST_MATCHER_P(clang::ParmVarDecl, isAtPosition, unsigned, N) { + constexpr char Name[] = "##isAtPosition_parmVar_"; + + // The direct parent of any parmVarDecl is a TypeLoc, and not a Decl, + // so we have to go up one more level. + return parmVarDecl( + hasParent(typeLoc(anyOf(hasParent(blockDecl(hasParameter( + N, parmVarDecl().bind(Name)))), + hasParent(objcMethodDecl(hasParameter( + N, parmVarDecl().bind(Name)))), + hasParent(functionDecl(hasParameter( + N, parmVarDecl().bind(Name))))))), + equalsBoundNode(Name)) + .matches(Node, Finder, Builder); +} + /// Matches if the provided matcher does not match. /// /// Example matches Y (matcher = cxxRecordDecl(unless(hasName("X")))) Index: clang/docs/LibASTMatchersReference.html =================================================================== --- clang/docs/LibASTMatchersReference.html +++ clang/docs/LibASTMatchersReference.html @@ -1226,6 +1226,11 @@ </pre></td></tr> +<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('fixedPointLiteral0')"><a name="fixedPointLiteral0Anchor">fixedPointLiteral</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FixedPointLiteral.html">FixedPointLiteral</a>>...</td></tr> +<tr><td colspan="4" class="doc" id="fixedPointLiteral0"><pre>Matches fixed point literals +</pre></td></tr> + + <tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1Stmt.html">Stmt</a>></td><td class="name" onclick="toggle('floatLiteral0')"><a name="floatLiteral0Anchor">floatLiteral</a></td><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FloatingLiteral.html">FloatingLiteral</a>>...</td></tr> <tr><td colspan="4" class="doc" id="floatLiteral0"><pre>Matches float literals of all sizes / encodings, e.g. 1.0, 1.0f, 1.0L and 1e10. @@ -4671,6 +4676,23 @@ Usable as: Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>>, Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1VarDecl.html">VarDecl</a>>, Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html">CXXRecordDecl</a>> </pre></td></tr> + +<tr><td>Matcher<clang::ParmVarDecl></td><td class="name" onclick="toggle('isAtPosition0')"><a name="isAtPosition0Anchor">isAtPosition</a></td><td>unsigned N</td></tr> +<tr><td colspan="4" class="doc" id="isAtPosition0"><pre>Matches the ParmVarDecl nodes that are at the N'th position in the parameter +list. The parameter list could be that of either a block, function, or +objc-method. + + +Given + +void f(int a, int b, int c) { +} + +``parmVarDecl(isAtPosition(0))`` matches ``int a``. + +``parmVarDecl(isAtPosition(1))`` matches ``int b``. +</pre></td></tr> + <!--END_NARROWING_MATCHERS --> </table>
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits