mwyman created this revision. mwyman added reviewers: benhamilton, klimek. Herald added a project: clang. Herald added a subscriber: cfe-commits.
isClassMessage is an equivalent to isInstanceMessage for ObjCMessageExpr, but matches message expressions to classes. isClassMethod and isInstanceMethod check whether a method declaration (or definition) is for a class method or instance method (respectively). Repository: rC Clang https://reviews.llvm.org/D60920 Files: clang/docs/LibASTMatchersReference.html clang/include/clang/ASTMatchers/ASTMatchers.h clang/lib/ASTMatchers/Dynamic/Registry.cpp clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -454,6 +454,20 @@ objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x")))))))); } +TEST(Matcher, isClassMessage) { + EXPECT_TRUE(matchesObjC( + "@interface NSString +(NSString *) stringWithFormat; @end " + "void f() { [NSString stringWithFormat]; }", + objcMessageExpr(isClassMessage()))); + + EXPECT_FALSE(matchesObjC( + "@interface NSString @end " + "void f(NSString *x) {" + "[x containsString];" + "}", + objcMessageExpr(isClassMessage()))); +} + TEST(Matcher, isInstanceMessage) { EXPECT_TRUE(matchesObjC( "@interface NSString @end " @@ -469,6 +483,46 @@ } +TEST(Matcher, isClassMethod) { + EXPECT_TRUE(matchesObjC( + "@interface Bar + (void)bar; @end", + objcMethodDecl(isClassMethod()))); + + EXPECT_TRUE(matchesObjC( + "@interface Bar @end" + "@implementation Bar + (void)bar {} @end", + objcMethodDecl(isClassMethod()))); + + EXPECT_FALSE(matchesObjC( + "@interface Foo - (void)foo; @end", + objcMethodDecl(isClassMethod()))); + + EXPECT_FALSE(matchesObjC( + "@interface Foo @end " + "@implementation Foo - (void)foo {} @end", + objcMethodDecl(isClassMethod()))); +} + +TEST(Matcher, isInstanceMethod) { + EXPECT_TRUE(matchesObjC( + "@interface Foo - (void)foo; @end", + objcMethodDecl(isInstanceMethod()))); + + EXPECT_TRUE(matchesObjC( + "@interface Foo @end " + "@implementation Foo - (void)foo {} @end", + objcMethodDecl(isInstanceMethod()))); + + EXPECT_FALSE(matchesObjC( + "@interface Bar + (void)bar; @end", + objcMethodDecl(isInstanceMethod()))); + + EXPECT_FALSE(matchesObjC( + "@interface Bar @end" + "@implementation Bar + (void)bar {} @end", + objcMethodDecl(isInstanceMethod()))); +} + TEST(MatcherCXXMemberCallExpr, On) { auto Snippet1 = R"cc( struct Y { Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ clang/lib/ASTMatchers/Dynamic/Registry.cpp @@ -344,6 +344,8 @@ REGISTER_MATCHER(isBitField); REGISTER_MATCHER(isCatchAll); REGISTER_MATCHER(isClass); + REGISTER_MATCHER(isClassMessage); + REGISTER_MATCHER(isClassMethod); REGISTER_MATCHER(isConst); REGISTER_MATCHER(isConstQualified); REGISTER_MATCHER(isConstexpr); @@ -367,6 +369,7 @@ REGISTER_MATCHER(isInTemplateInstantiation); REGISTER_MATCHER(isInline); REGISTER_MATCHER(isInstanceMessage); + REGISTER_MATCHER(isInstanceMethod); REGISTER_MATCHER(isInstantiated); REGISTER_MATCHER(isInstantiationDependent); REGISTER_MATCHER(isInteger); Index: clang/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -2938,10 +2938,59 @@ return InnerMatcher.matches(TypeDecl, Finder, Builder); } +/// Returns true when the Objective-C method declaration is a class method. +/// +/// Example +/// matcher = objcMethodDecl(isClassMethod()) +/// matches +/// \code +/// @interface I + (void)foo; @end +/// \endcode +/// but not +/// \code +/// @interface I - (void)bar; @end +/// \endcode +AST_MATCHER(ObjCMethodDecl, isClassMethod) { + return Node.isClassMethod(); +} + +/// Returns true when the Objective-C method declaration is an instance method. +/// +/// Example +/// matcher = objcMethodDecl(isInstanceMethod()) +/// matches +/// \code +/// @interface I - (void)bar; @end +/// \endcode +/// but not +/// \code +/// @interface I + (void)foo; @end +/// \endcode +AST_MATCHER(ObjCMethodDecl, isInstanceMethod) { + return Node.isInstanceMethod(); +} + +/// Returns true when the Objective-C message is sent to a class. +/// +/// Example +/// matcher = objcMessageExpr(isClassMessage()) +/// matches +/// \code +/// [NSString stringWithFormat:@"format"]; +/// \endcode +/// but not +/// \code +/// NSString *x = @"hello"; +/// [x containsString:@"h"]; +/// \endcode +AST_MATCHER(ObjCMessageExpr, isClassMessage) { + return Node.isClassMessage(); +} + /// Returns true when the Objective-C message is sent to an instance. /// /// Example -/// matcher = objcMessagaeExpr(isInstanceMessage()) +/// matcher = objcMessageExpr(isInstanceMessage()) /// matches /// \code /// NSString *x = @"hello"; Index: clang/docs/LibASTMatchersReference.html =================================================================== --- clang/docs/LibASTMatchersReference.html +++ clang/docs/LibASTMatchersReference.html @@ -3567,11 +3567,24 @@ </pre></td></tr> +<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1ObjCMessageExpr.html">ObjCMessageExpr</a>></td><td class="name" onclick="toggle('isClassMessage0')"><a name="isClassMessage0Anchor">isClassMessage</a></td><td></td></tr> +<tr><td colspan="4" class="doc" id="isClassMessage0"><pre>Returns true when the Objective-C message is sent to a class. + +Example +matcher = objcMessageExpr(isClassMessage()) +matches + [NSString stringWithFormat:@"format"]; +but not + NSString *x = @"hello"; + [x containsString:@"h"]; +</pre></td></tr> + + <tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1ObjCMessageExpr.html">ObjCMessageExpr</a>></td><td class="name" onclick="toggle('isInstanceMessage0')"><a name="isInstanceMessage0Anchor">isInstanceMessage</a></td><td></td></tr> <tr><td colspan="4" class="doc" id="isInstanceMessage0"><pre>Returns true when the Objective-C message is sent to an instance. Example -matcher = objcMessagaeExpr(isInstanceMessage()) +matcher = objcMessageExpr(isInstanceMessage()) matches NSString *x = @"hello"; [x containsString:@"h"]; @@ -3580,6 +3593,30 @@ </pre></td></tr> +<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1ObjCMethodDecl.html">ObjCMethodDecl</a>></td><td class="name" onclick="toggle('isClassMethod0')"><a name="isClassMethod0Anchor">isClassMethod</a></td><td></td></tr> +<tr><td colspan="4" class="doc" id="isClassMethod0"><pre>Returns true when the Objective-C method declaration is a class method. + +Example +matcher = objcMethodDecl(isClassMethod()) +matches + @interface I + (void)foo; @end +but not + @interface I - (void)bar; @end +</pre></td></tr> + + +<tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1ObjCMethodDecl.html">ObjCMethodDecl</a>></td><td class="name" onclick="toggle('isInstanceMessage0')"><a name="isInstanceMessage0Anchor">isInstanceMethod</a></td><td></td></tr> +<tr><td colspan="4" class="doc" id="isInstanceMethod0"><pre>Returns true when the Objective-C method declaration is an instance method. + +Example +matcher = objcMethodDecl(isInstanceMethod()) +matches + @interface I - (void)bar; @end +but not + @interface I + (void)foo; @end +</pre></td></tr> + + <tr><td>Matcher<<a href="https://clang.llvm.org/doxygen/classclang_1_1ObjCMessageExpr.html">ObjCMessageExpr</a>></td><td class="name" onclick="toggle('matchesSelector0')"><a name="matchesSelector0Anchor">matchesSelector</a></td><td>std::string RegExp</td></tr> <tr><td colspan="4" class="doc" id="matchesSelector0"><pre>Matches ObjC selectors whose name contains a substring matched by the given RegExp.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits