Index: clang/docs/LibASTMatchersReference.html =================================================================== --- clang/docs/LibASTMatchersReference.html +++ clang/docs/LibASTMatchersReference.html @@ -3268,6 +3268,19 @@ +
Returns true when the Objective-C message is sent to an instance. + +Example +matcher = objcMessagaeExpr(isInstanceMessage()) +matches + NSString *x = @"hello"; + [x containsString:@"h"] +but not + [NSString stringWithFormat:@"format"] +
Matches ObjC selectors whose name contains a substring matched by the given RegExp. @@ -5875,6 +5888,18 @@
Matches if the Objective-C message is sent to an instance, +and the inner matcher matches on that instance. + +For example the method call in + NSString *x = @"hello"; + [x containsString:@"h"] +is matched by +objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x")))))) +
Matches on the receiver of an ObjectiveC Message expression. Index: clang/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -2736,6 +2736,41 @@ return InnerMatcher.matches(TypeDecl, Finder, Builder); } +/// Returns true when the Objective-C message is sent to an instance. +/// +/// Example +/// matcher = objcMessagaeExpr(isInstanceMessage()) +/// matches +/// \code +/// NSString *x = @"hello"; +/// [x containsString:@"h"]; +/// \endcode +/// but not +/// \code +/// [NSString stringWithFormat:@"format"]; +/// \endcode +AST_MATCHER(ObjCMessageExpr, isInstanceMessage) { + return Node.isInstanceMessage(); +} + +/// Matches if the Objective-C message is sent to an instance, +/// and the inner matcher matches on that instance. +/// +/// For example the method call in +/// \code +/// NSString *x = @"hello"; +/// [x containsString:@"h"]; +/// \endcode +/// is matched by +/// objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x")))))) +AST_MATCHER_P(ObjCMessageExpr, hasReceiver, internal::Matcher, + InnerMatcher) { + const Expr *ReceiverNode = Node.getInstanceReceiver(); + return (ReceiverNode != nullptr && + InnerMatcher.matches(*ReceiverNode->IgnoreParenImpCasts(), Finder, + Builder)); +} + /// Matches when BaseName == Selector.getAsString() /// /// matcher = objCMessageExpr(hasSelector("loadHTMLString:baseURL:")); Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp =================================================================== --- clang/lib/ASTMatchers/Dynamic/Registry.cpp +++ clang/lib/ASTMatchers/Dynamic/Registry.cpp @@ -283,6 +283,7 @@ REGISTER_MATCHER(hasParent); REGISTER_MATCHER(hasQualifier); REGISTER_MATCHER(hasRangeInit); + REGISTER_MATCHER(hasReceiver); REGISTER_MATCHER(hasReceiverType); REGISTER_MATCHER(hasReplacementType); REGISTER_MATCHER(hasReturnValue); @@ -349,6 +350,7 @@ REGISTER_MATCHER(isImplicit); REGISTER_MATCHER(isExpansionInFileMatching); REGISTER_MATCHER(isExpansionInMainFile); + REGISTER_MATCHER(isInstanceMessage); REGISTER_MATCHER(isInstantiated); REGISTER_MATCHER(isExpansionInSystemHeader); REGISTER_MATCHER(isInteger); Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp @@ -422,6 +422,35 @@ EXPECT_TRUE(matches("void x(long) { int y; x(y); }", ImplicitCastedArgument)); } +TEST(Matcher, HasReceiver) { + EXPECT_TRUE(matchesObjC( + "@interface NSString @end" + "void f(NSString *x) {" + "[x containsString]" + "}", + objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x")))))))); + + EXPECT_FALSE(matchesObjC( + "@interface NSString +(NSString *) stringWithFormat; @end" + "void f() { [NSString stringWithFormat]; }", + objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x")))))))); +} + +TEST(Matcher, isInstanceMessage) { + EXPECT_TRUE(matchesObjC( + "@interface NSString @end" + "void f(NSString *x) {" + "[x containsString]" + "}", + objcMessageExpr(isInstanceMessage()))); + + EXPECT_FALSE(matchesObjC( + "@interface NSString +(NSString *) stringWithFormat; @end" + "void f() { [NSString stringWithFormat]; }", + objcMessageExpr(isInstanceMessage()))); + +} + TEST(ForEachArgumentWithParam, ReportsNoFalsePositives) { StatementMatcher ArgumentY = declRefExpr(to(varDecl(hasName("y")))).bind("arg");