diff options
-rw-r--r-- | clang/docs/LibASTMatchersReference.html | 85 | ||||
-rw-r--r-- | clang/include/clang/ASTMatchers/ASTMatchers.h | 33 | ||||
-rw-r--r-- | clang/lib/ASTMatchers/ASTMatchersInternal.cpp | 2 | ||||
-rw-r--r-- | clang/lib/ASTMatchers/Dynamic/Registry.cpp | 1 | ||||
-rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersTest.h | 2 | ||||
-rw-r--r-- | clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp | 4 |
6 files changed, 118 insertions, 9 deletions
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index bc58e3d9a60..6c67fd5b60e 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -124,6 +124,18 @@ accessSpecDecl() </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>></td><td class="name" onclick="toggle('blockDecl0')"><a name="blockDecl0Anchor">blockDecl</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1BlockDecl.html">BlockDecl</a>>...</td></tr> +<tr><td colspan="4" class="doc" id="blockDecl0"><pre>Matches block declarations. + +Example matches the declaration of the nameless block printing an input +integer. + + myFunc(^(int p) { + printf("%d", p); + }) +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1Decl.html">Decl</a>></td><td class="name" onclick="toggle('classTemplateDecl0')"><a name="classTemplateDecl0Anchor">classTemplateDecl</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ClassTemplateDecl.html">ClassTemplateDecl</a>>...</td></tr> <tr><td colspan="4" class="doc" id="classTemplateDecl0"><pre>Matches C++ class template declarations. @@ -4352,6 +4364,55 @@ Example matches b (matcher = binaryOperator(hasRHS())) </pre></td></tr> +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1BlockDecl.html">BlockDecl</a>></td><td class="name" onclick="toggle('hasAnyParameter2')"><a name="hasAnyParameter2Anchor">hasAnyParameter</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ParmVarDecl.html">ParmVarDecl</a>> InnerMatcher</td></tr> +<tr><td colspan="4" class="doc" id="hasAnyParameter2"><pre>Matches any parameter of a function or an ObjC method declaration or a +block. + +Does not match the 'this' parameter of a method. + +Given + class X { void f(int x, int y, int z) {} }; +cxxMethodDecl(hasAnyParameter(hasName("y"))) + matches f(int x, int y, int z) {} +with hasAnyParameter(...) + matching int y + +For ObjectiveC, given + @interface I - (void) f:(int) y; @end + +the matcher objcMethodDecl(hasAnyParameter(hasName("y"))) +matches the declaration of method f with hasParameter +matching y. + +For blocks, given + b = ^(int y) { printf("%d", y) }; + +the matcher blockDecl(hasAnyParameter(hasName("y"))) +matches the declaration of the block b with hasParameter +matching y. +</pre></td></tr> + + +<tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1BlockDecl.html">BlockDecl</a>></td><td class="name" onclick="toggle('hasParameter2')"><a name="hasParameter2Anchor">hasParameter</a></td><td>unsigned N, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ParmVarDecl.html">ParmVarDecl</a>> InnerMatcher</td></tr> +<tr><td colspan="4" class="doc" id="hasParameter2"><pre>Matches the n'th parameter of a function or an ObjC method +declaration or a block. + +Given + class X { void f(int x) {} }; +cxxMethodDecl(hasParameter(0, hasType(varDecl()))) + matches f(int x) {} +with hasParameter(...) + matching int x + +For ObjectiveC, given + @interface I - (void) f:(int) y; @end + +the matcher objcMethodDecl(hasParameter(0, hasName("y"))) +matches the declaration of method f with hasParameter +matching y. +</pre></td></tr> + + <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1BlockPointerTypeLoc.html">BlockPointerTypeLoc</a>></td><td class="name" onclick="toggle('pointeeLoc0')"><a name="pointeeLoc0Anchor">pointeeLoc</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1TypeLoc.html">TypeLoc</a>></td></tr> <tr><td colspan="4" class="doc" id="pointeeLoc0"><pre>Narrows PointerType (and similar) matchers to those where the pointee matches a given matcher. @@ -5336,7 +5397,8 @@ matches 'int x = 0' in <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('hasAnyParameter0')"><a name="hasAnyParameter0Anchor">hasAnyParameter</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ParmVarDecl.html">ParmVarDecl</a>> InnerMatcher</td></tr> -<tr><td colspan="4" class="doc" id="hasAnyParameter0"><pre>Matches any parameter of a function or ObjC method declaration. +<tr><td colspan="4" class="doc" id="hasAnyParameter0"><pre>Matches any parameter of a function or an ObjC method declaration or a +block. Does not match the 'this' parameter of a method. @@ -5353,6 +5415,13 @@ For ObjectiveC, given the matcher objcMethodDecl(hasAnyParameter(hasName("y"))) matches the declaration of method f with hasParameter matching y. + +For blocks, given + b = ^(int y) { printf("%d", y) }; + +the matcher blockDecl(hasAnyParameter(hasName("y"))) +matches the declaration of the block b with hasParameter +matching y. </pre></td></tr> @@ -5393,7 +5462,7 @@ with compoundStmt() <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1FunctionDecl.html">FunctionDecl</a>></td><td class="name" onclick="toggle('hasParameter0')"><a name="hasParameter0Anchor">hasParameter</a></td><td>unsigned N, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ParmVarDecl.html">ParmVarDecl</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="hasParameter0"><pre>Matches the n'th parameter of a function or an ObjC method -declaration. +declaration or a block. Given class X { void f(int x) {} }; @@ -5767,7 +5836,8 @@ matches the [webView ...] message invocation. <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ObjCMethodDecl.html">ObjCMethodDecl</a>></td><td class="name" onclick="toggle('hasAnyParameter1')"><a name="hasAnyParameter1Anchor">hasAnyParameter</a></td><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ParmVarDecl.html">ParmVarDecl</a>> InnerMatcher</td></tr> -<tr><td colspan="4" class="doc" id="hasAnyParameter1"><pre>Matches any parameter of a function or ObjC method declaration. +<tr><td colspan="4" class="doc" id="hasAnyParameter1"><pre>Matches any parameter of a function or an ObjC method declaration or a +block. Does not match the 'this' parameter of a method. @@ -5784,12 +5854,19 @@ For ObjectiveC, given the matcher objcMethodDecl(hasAnyParameter(hasName("y"))) matches the declaration of method f with hasParameter matching y. + +For blocks, given + b = ^(int y) { printf("%d", y) }; + +the matcher blockDecl(hasAnyParameter(hasName("y"))) +matches the declaration of the block b with hasParameter +matching y. </pre></td></tr> <tr><td>Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ObjCMethodDecl.html">ObjCMethodDecl</a>></td><td class="name" onclick="toggle('hasParameter1')"><a name="hasParameter1Anchor">hasParameter</a></td><td>unsigned N, Matcher<<a href="http://clang.llvm.org/doxygen/classclang_1_1ParmVarDecl.html">ParmVarDecl</a>> InnerMatcher</td></tr> <tr><td colspan="4" class="doc" id="hasParameter1"><pre>Matches the n'th parameter of a function or an ObjC method -declaration. +declaration or a block. Given class X { void f(int x) {} }; diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index b1b3f7269e5..c9e182172cf 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -1227,6 +1227,19 @@ extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryImplDecl> extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCMethodDecl> objcMethodDecl; +/// Matches block declarations. +/// +/// Example matches the declaration of the nameless block printing an input +/// integer. +/// +/// \code +/// myFunc(^(int p) { +/// printf("%d", p); +/// }) +/// \endcode +extern const internal::VariadicDynCastAllOfMatcher<Decl, BlockDecl> + blockDecl; + /// Matches Objective-C instance variable declarations. /// /// Example matches _enabled @@ -3479,7 +3492,7 @@ AST_MATCHER(CXXConstructExpr, requiresZeroInitialization) { } /// Matches the n'th parameter of a function or an ObjC method -/// declaration. +/// declaration or a block. /// /// Given /// \code @@ -3500,7 +3513,8 @@ AST_MATCHER(CXXConstructExpr, requiresZeroInitialization) { /// matching y. AST_POLYMORPHIC_MATCHER_P2(hasParameter, AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, - ObjCMethodDecl), + ObjCMethodDecl, + BlockDecl), unsigned, N, internal::Matcher<ParmVarDecl>, InnerMatcher) { return (N < Node.parameters().size() @@ -3561,7 +3575,8 @@ AST_POLYMORPHIC_MATCHER_P2(forEachArgumentWithParam, return Matched; } -/// Matches any parameter of a function or ObjC method declaration. +/// Matches any parameter of a function or an ObjC method declaration or a +/// block. /// /// Does not match the 'this' parameter of a method. /// @@ -3582,9 +3597,19 @@ AST_POLYMORPHIC_MATCHER_P2(forEachArgumentWithParam, /// the matcher objcMethodDecl(hasAnyParameter(hasName("y"))) /// matches the declaration of method f with hasParameter /// matching y. +/// +/// For blocks, given +/// \code +/// b = ^(int y) { printf("%d", y) }; +/// \endcode +/// +/// the matcher blockDecl(hasAnyParameter(hasName("y"))) +/// matches the declaration of the block b with hasParameter +/// matching y. AST_POLYMORPHIC_MATCHER_P(hasAnyParameter, AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, - ObjCMethodDecl), + ObjCMethodDecl, + BlockDecl), internal::Matcher<ParmVarDecl>, InnerMatcher) { return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(), diff --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp index e9ff825d90b..92b680fd179 100644 --- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -626,6 +626,8 @@ const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryImplDecl> objcCategoryImplDecl; const internal::VariadicDynCastAllOfMatcher<Decl, ObjCMethodDecl> objcMethodDecl; +const internal::VariadicDynCastAllOfMatcher<Decl, BlockDecl> + blockDecl; const internal::VariadicDynCastAllOfMatcher<Decl, ObjCIvarDecl> objcIvarDecl; const internal::VariadicDynCastAllOfMatcher<Decl, ObjCPropertyDecl> objcPropertyDecl; diff --git a/clang/lib/ASTMatchers/Dynamic/Registry.cpp b/clang/lib/ASTMatchers/Dynamic/Registry.cpp index e6a6a20916e..84e6e450f2a 100644 --- a/clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ b/clang/lib/ASTMatchers/Dynamic/Registry.cpp @@ -136,6 +136,7 @@ RegistryMaps::RegistryMaps() { REGISTER_MATCHER(autoType); REGISTER_MATCHER(binaryOperator); REGISTER_MATCHER(binaryConditionalOperator); + REGISTER_MATCHER(blockDecl); REGISTER_MATCHER(blockPointerType); REGISTER_MATCHER(booleanType); REGISTER_MATCHER(breakStmt); diff --git a/clang/unittests/ASTMatchers/ASTMatchersTest.h b/clang/unittests/ASTMatchers/ASTMatchersTest.h index 7cfe5b9e370..504668872f4 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTest.h +++ b/clang/unittests/ASTMatchers/ASTMatchersTest.h @@ -132,7 +132,7 @@ testing::AssertionResult matchesObjC(const std::string &Code, const T &AMatcher, bool ExpectMatch = true) { return matchesConditionally(Code, AMatcher, ExpectMatch, {"-fobjc-nonfragile-abi", "-Wno-objc-root-class", - "-Wno-incomplete-implementation"}, + "-fblocks", "-Wno-incomplete-implementation"}, FileContentMappings(), "input.m"); } diff --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp index c9c70a5f257..b18cd631302 100644 --- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -541,6 +541,8 @@ TEST(HasParameter, CallsInnerMatcher) { cxxMethodDecl(hasParameter(0, hasName("x"))))); EXPECT_TRUE(matchesObjC("@interface I -(void)f:(int) x; @end", objcMethodDecl(hasParameter(0, hasName("x"))))); + EXPECT_TRUE(matchesObjC("int main() { void (^b)(int) = ^(int p) {}; }", + blockDecl(hasParameter(0, hasName("p"))))); } TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) { @@ -572,6 +574,8 @@ TEST(HasAnyParameter, MatchesIndependentlyOfPosition) { cxxMethodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); EXPECT_TRUE(matchesObjC("@interface I -(void)f:(int) x; @end", objcMethodDecl(hasAnyParameter(hasName("x"))))); + EXPECT_TRUE(matchesObjC("int main() { void (^b)(int) = ^(int p) {}; }", + blockDecl(hasAnyParameter(hasName("p"))))); } TEST(Returns, MatchesReturnTypes) { |