Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -3567,11 +3567,24 @@ +
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"]; +
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 @@
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 +
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 +
Matches ObjC selectors whose name contains a substring matched by the given RegExp. Index: include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- include/clang/ASTMatchers/ASTMatchers.h +++ 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: lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- lib/ASTMatchers/Dynamic/Registry.cpp +++ 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: unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ 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 {