Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ AST Matchers
- Added ``hasExplicitParameters`` for ``LambdaExpr`` as an output attribute to
AST JSON dumps.
- Add ``arrayTypeLoc`` matcher for matching ``ArrayTypeLoc``.
- Add missing support for ``TraversalKind`` in some ``addMatcher()`` overloads.

clang-format
------------
Expand Down
57 changes: 50 additions & 7 deletions clang/lib/ASTMatchers/ASTMatchFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,13 @@ void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,

void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
MatchCallback *Action) {
Matchers.Type.emplace_back(NodeMatch, Action);
std::optional<TraversalKind> TK;
if (Action)
TK = Action->getCheckTraversalKind();
if (TK)
Matchers.Type.emplace_back(traverse(*TK, NodeMatch), Action);
else
Matchers.Type.emplace_back(NodeMatch, Action);
Copy link
Contributor

@localspook localspook Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you, but we could factor this logic out into a function like:

template <typename T>
static internal::Matcher<T>
adjustTraversalKind(const internal::Matcher<T> &NodeMatch,
                    MatchFinder::MatchCallback *Action) {
  if (Action)
    if (std::optional<TraversalKind> TK = Action->getCheckTraversalKind())
      return traverse(*TK, NodeMatch);
  return NodeMatch;
}

then use it like:

Matchers.TypeLoc.emplace_back(adjustTraversalKind(NodeMatch, Action), Action);

Matchers.AllCallbacks.insert(Action);
}

Expand All @@ -1699,37 +1705,74 @@ void MatchFinder::addMatcher(const StatementMatcher &NodeMatch,

void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
MatchCallback *Action) {
Matchers.NestedNameSpecifier.emplace_back(NodeMatch, Action);
std::optional<TraversalKind> TK;
if (Action)
TK = Action->getCheckTraversalKind();
if (TK)
Matchers.NestedNameSpecifier.emplace_back(traverse(*TK, NodeMatch), Action);
else
Matchers.NestedNameSpecifier.emplace_back(NodeMatch, Action);
Matchers.AllCallbacks.insert(Action);
}

void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
MatchCallback *Action) {
Matchers.NestedNameSpecifierLoc.emplace_back(NodeMatch, Action);
std::optional<TraversalKind> TK;
if (Action)
TK = Action->getCheckTraversalKind();
if (TK)
Matchers.NestedNameSpecifierLoc.emplace_back(traverse(*TK, NodeMatch),
Action);
else
Matchers.NestedNameSpecifierLoc.emplace_back(NodeMatch, Action);
Matchers.AllCallbacks.insert(Action);
}

void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch,
MatchCallback *Action) {
Matchers.TypeLoc.emplace_back(NodeMatch, Action);
std::optional<TraversalKind> TK;
if (Action)
TK = Action->getCheckTraversalKind();
if (TK)
Matchers.TypeLoc.emplace_back(traverse(*TK, NodeMatch), Action);
else
Matchers.TypeLoc.emplace_back(NodeMatch, Action);
Matchers.AllCallbacks.insert(Action);
}

void MatchFinder::addMatcher(const CXXCtorInitializerMatcher &NodeMatch,
MatchCallback *Action) {
Matchers.CtorInit.emplace_back(NodeMatch, Action);
std::optional<TraversalKind> TK;
if (Action)
TK = Action->getCheckTraversalKind();
if (TK)
Matchers.CtorInit.emplace_back(traverse(*TK, NodeMatch), Action);
else
Matchers.CtorInit.emplace_back(NodeMatch, Action);
Matchers.AllCallbacks.insert(Action);
}

void MatchFinder::addMatcher(const TemplateArgumentLocMatcher &NodeMatch,
MatchCallback *Action) {
Matchers.TemplateArgumentLoc.emplace_back(NodeMatch, Action);
std::optional<TraversalKind> TK;
if (Action)
TK = Action->getCheckTraversalKind();
if (TK)
Matchers.TemplateArgumentLoc.emplace_back(traverse(*TK, NodeMatch), Action);
else
Matchers.TemplateArgumentLoc.emplace_back(NodeMatch, Action);
Matchers.AllCallbacks.insert(Action);
}

void MatchFinder::addMatcher(const AttrMatcher &AttrMatch,
MatchCallback *Action) {
Matchers.Attr.emplace_back(AttrMatch, Action);
std::optional<TraversalKind> TK;
if (Action)
TK = Action->getCheckTraversalKind();
if (TK)
Matchers.Attr.emplace_back(traverse(*TK, AttrMatch), Action);
else
Matchers.Attr.emplace_back(AttrMatch, Action);
Matchers.AllCallbacks.insert(Action);
}

Expand Down
39 changes: 37 additions & 2 deletions clang/unittests/ASTMatchers/ASTMatchersInternalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,47 @@ TEST(DynTypedMatcherTest, ConstructWithTraversalKindSetsTK) {
}

TEST(DynTypedMatcherTest, ConstructWithTraversalKindOverridesNestedTK) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two withTraversalKind seemed intended.

auto M = DynTypedMatcher(decl()).withTraversalKind(TK_AsIs).withTraversalKind(
TK_IgnoreUnlessSpelledInSource);
auto M =
DynTypedMatcher(decl()).withTraversalKind(TK_IgnoreUnlessSpelledInSource);
EXPECT_THAT(M.getTraversalKind(),
llvm::ValueIs(TK_IgnoreUnlessSpelledInSource));
}

TEST(MatchFinder, AddMatcherOverloadsHonorTraversalKind) {
StringRef Code = R"cpp(
struct B {};
struct C : B {
C() {}
};
)cpp";

// C() has an implicit initializer for B.
auto Matcher = cxxCtorInitializer(isBaseInitializer());

{
bool Matched = false;
MatchFinder Finder;
struct TestCallback : public MatchFinder::MatchCallback {
std::optional<TraversalKind> TK;
bool *Matched;
TestCallback(std::optional<TraversalKind> TK, bool *Matched)
: TK(TK), Matched(Matched) {}
void run(const MatchFinder::MatchResult &Result) override {
*Matched = true;
}
std::optional<TraversalKind> getCheckTraversalKind() const override {
return TK;
}
} Callback(TK_IgnoreUnlessSpelledInSource, &Matched);
Finder.addMatcher(Matcher, &Callback);
std::unique_ptr<FrontendActionFactory> Factory(
newFrontendActionFactory(&Finder));
ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), Code));
EXPECT_FALSE(Matched) << "Matcher not using specified TraversalKind, "
"TK_IgnoreUnlessSpelledInSource";
}
}

TEST(IsInlineMatcher, IsInline) {
EXPECT_TRUE(matches("void g(); inline void f();",
functionDecl(isInline(), hasName("f"))));
Expand Down
Loading