[ASTMatchers] Allow use of mapAnyOf in more contexts
authorStephen Kelly <steveire@gmail.com>
Sat, 16 Jan 2021 14:25:11 +0000 (14:25 +0000)
committerStephen Kelly <steveire@gmail.com>
Tue, 19 Jan 2021 22:10:09 +0000 (22:10 +0000)
Add an operator overload to ArgumentAdaptingMatcherFunc to allow use of
mapAnyOf within hasAncestor, hasParent etc.

Differential Revision: https://reviews.llvm.org/D94864

clang/include/clang/ASTMatchers/ASTMatchersInternal.h
clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

index aa78a89..2af4e6e 100644 (file)
@@ -1443,6 +1443,13 @@ struct ArgumentAdaptingMatcherFunc {
   operator()(const Matcher<T> &InnerMatcher) const {
     return create(InnerMatcher);
   }
+
+  template <typename... T>
+  ArgumentAdaptingMatcherFuncAdaptor<ArgumentAdapterT,
+                                     typename GetClade<T...>::Type, ToTypes>
+  operator()(const MapAnyOfHelper<T...> &InnerMatcher) const {
+    return create(InnerMatcher.with());
+  }
 };
 
 template <typename T> class TraversalMatcher : public MatcherInterface<T> {
index b3582a0..d681620 100644 (file)
@@ -491,6 +491,17 @@ void F() {
       Code, traverse(TK_IgnoreUnlessSpelledInSource,
                      mapAnyOf(ifStmt, forStmt).with(hasCondition(falseExpr)))));
 
+  EXPECT_TRUE(
+      matches(Code, cxxBoolLiteral(equals(true),
+                                   hasAncestor(mapAnyOf(ifStmt, forStmt)))));
+
+  EXPECT_TRUE(
+      matches(Code, cxxBoolLiteral(equals(false),
+                                   hasAncestor(mapAnyOf(ifStmt, forStmt)))));
+
+  EXPECT_TRUE(
+      notMatches(Code, floatLiteral(hasAncestor(mapAnyOf(ifStmt, forStmt)))));
+
   Code = R"cpp(
 void func(bool b) {}
 struct S {