Skip to content

Commit

Permalink
[ASTMatchers] Allow use of mapAnyOf in more contexts
Browse files Browse the repository at this point in the history
Add an operator overload to ArgumentAdaptingMatcherFunc to allow use of
mapAnyOf within hasAncestor, hasParent etc.

Differential Revision: https://reviews.llvm.org/D94864
  • Loading branch information
steveire committed Jan 19, 2021
1 parent 3471455 commit ecf6966
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clang/include/clang/ASTMatchers/ASTMatchersInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ecf6966

Please sign in to comment.