oontvoo updated this revision to Diff 266735. oontvoo added a comment. More tests for Block
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,43 @@ parmVarDecl(hasDefaultArgument()))); } +TEST(IsAtPosition, Basic) { + EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1)))); + 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(IsAtPosition, FunctionDecl) { + 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(IsAtPosition, Lambda) { + 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(IsAtPosition, BlockDecl) { + // FIXME: Needs to enable block-support (ie., -fblocks) + if (true) + return; + EXPECT_TRUE(matches("void x() { auto b = ^(int y) { }; } ", + parmVarDecl(isAtPosition(0)))); + EXPECT_TRUE(matches("void x() { auto b = ^(int y,int z) { }; } ", + parmVarDecl(isAtPosition(1)))); + EXPECT_TRUE(notMatches("void x() { auto b = ^(int y) { }; } ", + 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 @@ -4615,6 +4615,39 @@ InnerMatcher.matches(*DeclarationStatement, Finder, Builder); } +// Returns true if Node is a parameter at N in the given Context +// of type DeclT. +template <typename DeclT> +static bool __isParamAt(const clang::DeclContext *Context, + const clang::ParmVarDecl &Node, unsigned N) { + if (const auto *Decl = dyn_cast_or_null<DeclT>(Context)) + return N < Decl->param_size() && Decl->getParamDecl(N) == &Node; + + return false; +} + +/// 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) { + const clang::DeclContext *ParentContext = Node.getParentFunctionOrMethod(); + return __isParamAt<FunctionDecl>(ParentContext, Node, N) || + __isParamAt<BlockDecl>(ParentContext, Node, N) || + __isParamAt<ObjCMethodDecl>(ParentContext, Node, N); +} + /// Matches the index expression of an array subscript expression. /// /// Given Index: clang/docs/LibASTMatchersReference.html =================================================================== --- clang/docs/LibASTMatchersReference.html +++ clang/docs/LibASTMatchersReference.html @@ -4671,6 +4671,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