Index: docs/LibASTMatchersReference.html =================================================================== --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -3932,6 +3932,17 @@ +
Matches when at least one of the supplied string equals to the
+Selector.getAsString()
+
+ matcher = objCMessageExpr(hasSelector("methodA:", "methodB:"));
+ matches both of the expressions below:
+ [myObj methodA:argA];
+ [myObj methodB:argB];
+Matches statements inside of a template instantiation.
Index: include/clang/ASTMatchers/ASTMatchers.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchers.h
+++ include/clang/ASTMatchers/ASTMatchers.h
@@ -2313,9 +2313,7 @@
/// namespace a { namespace b { class X; } }
/// \endcode
inline internal::Matcher hasName(const std::string &Name) {
- std::vector Names;
- Names.push_back(Name);
- return internal::Matcher(new internal::HasNameMatcher(Names));
+ return internal::Matcher(new internal::HasNameMatcher({Name}));
}
/// \brief Matches NamedDecl nodes that have any of the specified names.
@@ -2711,7 +2709,7 @@
const QualType TypeDecl = Node.getReceiverType();
return InnerMatcher.matches(TypeDecl, Finder, Builder);
}
-
+
/// \brief Matches when BaseName == Selector.getAsString()
///
/// matcher = objCMessageExpr(hasSelector("loadHTMLString:baseURL:"));
@@ -2725,7 +2723,21 @@
return BaseName.compare(Sel.getAsString()) == 0;
}
-
+
+/// \brief Matches when at least one of the supplied string equals to the
+/// Selector.getAsString()
+///
+/// matcher = objCMessageExpr(hasSelector("methodA:", "methodB:"));
+/// matches both of the expressions below:
+/// \code
+/// [myObj methodA:argA];
+/// [myObj methodB:argB];
+/// \endcode
+extern const internal::VariadicFunction,
+ StringRef,
+ internal::hasAnySelectorFunc>
+ hasAnySelector;
+
/// \brief Matches ObjC selectors whose name contains
/// a substring matched by the given RegExp.
/// matcher = objCMessageExpr(matchesSelector("loadHTMLString\:baseURL?"));
Index: include/clang/ASTMatchers/ASTMatchersInternal.h
===================================================================
--- include/clang/ASTMatchers/ASTMatchersInternal.h
+++ include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -731,6 +731,11 @@
/// HasNameMatcher.
Matcher hasAnyNameFunc(ArrayRef NameRefs);
+/// \brief Trampoline function to use VariadicFunction<> to construct a
+/// hasAnySelector matcher.
+Matcher hasAnySelectorFunc(
+ ArrayRef NameRefs);
+
/// \brief Matches declarations for QualType and CallExpr.
///
/// Type argument DeclMatcherT is required by PolymorphicMatcherWithParam1 but
Index: lib/ASTMatchers/ASTMatchersInternal.cpp
===================================================================
--- lib/ASTMatchers/ASTMatchersInternal.cpp
+++ lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -315,12 +315,32 @@
return false;
}
-Matcher hasAnyNameFunc(ArrayRef NameRefs) {
+inline static
+std::vector vectorFromRefs(ArrayRef NameRefs) {
std::vector Names;
for (auto *Name : NameRefs)
Names.emplace_back(*Name);
- return internal::Matcher(
- new internal::HasNameMatcher(std::move(Names)));
+ return Names;
+}
+
+Matcher hasAnyNameFunc(ArrayRef NameRefs) {
+ std::vector Names = vectorFromRefs(NameRefs);
+ return internal::Matcher(new internal::HasNameMatcher(Names));
+}
+
+AST_MATCHER_P(ObjCMessageExpr, hasAnySelectorMatcher, std::vector,
+ Matches) {
+ std::string SelString = Node.getSelector().getAsString();
+ for (std::string S : Matches)
+ if (S.compare(SelString) == 0)
+ return true;
+ return false;
+}
+
+Matcher hasAnySelectorFunc(
+ ArrayRef NameRefs) {
+ std::vector Names = vectorFromRefs(NameRefs);
+ return hasAnySelectorMatcher(Names);
}
HasNameMatcher::HasNameMatcher(std::vector N)
@@ -393,7 +413,8 @@
/// Return true if there are still any patterns left.
bool consumeNameSuffix(StringRef NodeName, bool CanSkip) {
for (size_t I = 0; I < Patterns.size();) {
- if (internal::consumeNameSuffix(Patterns[I].P, NodeName) ||
+ if (::clang::ast_matchers::internal::consumeNameSuffix(Patterns[I].P,
+ NodeName) ||
CanSkip) {
++I;
} else {
Index: lib/ASTMatchers/Dynamic/Registry.cpp
===================================================================
--- lib/ASTMatchers/Dynamic/Registry.cpp
+++ lib/ASTMatchers/Dynamic/Registry.cpp
@@ -286,6 +286,7 @@
REGISTER_MATCHER(hasReturnValue);
REGISTER_MATCHER(hasRHS);
REGISTER_MATCHER(hasSelector);
+ REGISTER_MATCHER(hasAnySelector);
REGISTER_MATCHER(hasSingleDecl);
REGISTER_MATCHER(hasSize);
REGISTER_MATCHER(hasSizeExpr);
Index: unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -1565,9 +1565,20 @@
EXPECT_TRUE(matchesObjC(
Objc1String,
objcMessageExpr(anything())));
+ EXPECT_TRUE(matchesObjC(Objc1String,
+ objcMessageExpr(hasAnySelector({
+ "contents", "meth:"}))
+
+ ));
EXPECT_TRUE(matchesObjC(
Objc1String,
objcMessageExpr(hasSelector("contents"))));
+ EXPECT_TRUE(matchesObjC(
+ Objc1String,
+ objcMessageExpr(hasAnySelector("contents", "contentsA"))));
+ EXPECT_FALSE(matchesObjC(
+ Objc1String,
+ objcMessageExpr(hasAnySelector("contentsB", "contentsC"))));
EXPECT_TRUE(matchesObjC(
Objc1String,
objcMessageExpr(matchesSelector("cont*"))));
Index: unittests/ASTMatchers/Dynamic/ParserTest.cpp
===================================================================
--- unittests/ASTMatchers/Dynamic/ParserTest.cpp
+++ unittests/ASTMatchers/Dynamic/ParserTest.cpp
@@ -236,6 +236,17 @@
Error.toStringFull());
}
+TEST(ParserTest, VariadicMatchTest) {
+ Diagnostics Error;
+ llvm::Optional OM(Parser::parseMatcherExpression(
+ "stmt(objcMessageExpr(hasAnySelector(\"methodA\", \"methodB:\")))",
+ &Error));
+ EXPECT_EQ("", Error.toStringFull());
+ auto M = OM->unconditionalConvertTo();
+ EXPECT_TRUE(matchesObjC("@interface I @end "
+ "void foo(I* i) { [i methodA]; }", M));
+}
+
std::string ParseWithError(StringRef Code) {
Diagnostics Error;
VariantValue Value;