Skip to content

Commit e63525d

Browse files
committed
[clang] Add missing support for traversal kind in addMatcher overloads
This was noted in llvm#170540, and seems to simply be an oversight. This patch just add the same logic already used in other addMatcher() implementations that honor the traversal kind.
1 parent 5daad5b commit e63525d

File tree

2 files changed

+85
-7
lines changed

2 files changed

+85
-7
lines changed

clang/lib/ASTMatchers/ASTMatchFinder.cpp

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,13 @@ void MatchFinder::addMatcher(const DeclarationMatcher &NodeMatch,
16811681

16821682
void MatchFinder::addMatcher(const TypeMatcher &NodeMatch,
16831683
MatchCallback *Action) {
1684-
Matchers.Type.emplace_back(NodeMatch, Action);
1684+
std::optional<TraversalKind> TK;
1685+
if (Action)
1686+
TK = Action->getCheckTraversalKind();
1687+
if (TK)
1688+
Matchers.Type.emplace_back(traverse(*TK, NodeMatch), Action);
1689+
else
1690+
Matchers.Type.emplace_back(NodeMatch, Action);
16851691
Matchers.AllCallbacks.insert(Action);
16861692
}
16871693

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

17001706
void MatchFinder::addMatcher(const NestedNameSpecifierMatcher &NodeMatch,
17011707
MatchCallback *Action) {
1702-
Matchers.NestedNameSpecifier.emplace_back(NodeMatch, Action);
1708+
std::optional<TraversalKind> TK;
1709+
if (Action)
1710+
TK = Action->getCheckTraversalKind();
1711+
if (TK)
1712+
Matchers.NestedNameSpecifier.emplace_back(traverse(*TK, NodeMatch), Action);
1713+
else
1714+
Matchers.NestedNameSpecifier.emplace_back(NodeMatch, Action);
17031715
Matchers.AllCallbacks.insert(Action);
17041716
}
17051717

17061718
void MatchFinder::addMatcher(const NestedNameSpecifierLocMatcher &NodeMatch,
17071719
MatchCallback *Action) {
1708-
Matchers.NestedNameSpecifierLoc.emplace_back(NodeMatch, Action);
1720+
std::optional<TraversalKind> TK;
1721+
if (Action)
1722+
TK = Action->getCheckTraversalKind();
1723+
if (TK)
1724+
Matchers.NestedNameSpecifierLoc.emplace_back(traverse(*TK, NodeMatch),
1725+
Action);
1726+
else
1727+
Matchers.NestedNameSpecifierLoc.emplace_back(NodeMatch, Action);
17091728
Matchers.AllCallbacks.insert(Action);
17101729
}
17111730

17121731
void MatchFinder::addMatcher(const TypeLocMatcher &NodeMatch,
17131732
MatchCallback *Action) {
1714-
Matchers.TypeLoc.emplace_back(NodeMatch, Action);
1733+
std::optional<TraversalKind> TK;
1734+
if (Action)
1735+
TK = Action->getCheckTraversalKind();
1736+
if (TK)
1737+
Matchers.TypeLoc.emplace_back(traverse(*TK, NodeMatch), Action);
1738+
else
1739+
Matchers.TypeLoc.emplace_back(NodeMatch, Action);
17151740
Matchers.AllCallbacks.insert(Action);
17161741
}
17171742

17181743
void MatchFinder::addMatcher(const CXXCtorInitializerMatcher &NodeMatch,
17191744
MatchCallback *Action) {
1720-
Matchers.CtorInit.emplace_back(NodeMatch, Action);
1745+
std::optional<TraversalKind> TK;
1746+
if (Action)
1747+
TK = Action->getCheckTraversalKind();
1748+
if (TK)
1749+
Matchers.CtorInit.emplace_back(traverse(*TK, NodeMatch), Action);
1750+
else
1751+
Matchers.CtorInit.emplace_back(NodeMatch, Action);
17211752
Matchers.AllCallbacks.insert(Action);
17221753
}
17231754

17241755
void MatchFinder::addMatcher(const TemplateArgumentLocMatcher &NodeMatch,
17251756
MatchCallback *Action) {
1726-
Matchers.TemplateArgumentLoc.emplace_back(NodeMatch, Action);
1757+
std::optional<TraversalKind> TK;
1758+
if (Action)
1759+
TK = Action->getCheckTraversalKind();
1760+
if (TK)
1761+
Matchers.TemplateArgumentLoc.emplace_back(traverse(*TK, NodeMatch), Action);
1762+
else
1763+
Matchers.TemplateArgumentLoc.emplace_back(NodeMatch, Action);
17271764
Matchers.AllCallbacks.insert(Action);
17281765
}
17291766

17301767
void MatchFinder::addMatcher(const AttrMatcher &AttrMatch,
17311768
MatchCallback *Action) {
1732-
Matchers.Attr.emplace_back(AttrMatch, Action);
1769+
std::optional<TraversalKind> TK;
1770+
if (Action)
1771+
TK = Action->getCheckTraversalKind();
1772+
if (TK)
1773+
Matchers.Attr.emplace_back(traverse(*TK, AttrMatch), Action);
1774+
else
1775+
Matchers.Attr.emplace_back(AttrMatch, Action);
17331776
Matchers.AllCallbacks.insert(Action);
17341777
}
17351778

clang/unittests/ASTMatchers/ASTMatchersInternalTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,41 @@ TEST(DynTypedMatcherTest, ConstructWithTraversalKindOverridesNestedTK) {
297297
llvm::ValueIs(TK_IgnoreUnlessSpelledInSource));
298298
}
299299

300+
TEST(MatchFinder, AddMatcherOverloadsHonorTraversalKind) {
301+
StringRef Code = R"cpp(
302+
struct B {};
303+
struct C : B {
304+
C() {}
305+
};
306+
)cpp";
307+
308+
// C() has an implicit initializer for B.
309+
auto Matcher = cxxCtorInitializer(isBaseInitializer());
310+
311+
{
312+
bool Matched = false;
313+
MatchFinder Finder;
314+
struct TestCallback : public MatchFinder::MatchCallback {
315+
std::optional<TraversalKind> TK;
316+
bool *Matched;
317+
TestCallback(std::optional<TraversalKind> TK, bool *Matched)
318+
: TK(TK), Matched(Matched) {}
319+
void run(const MatchFinder::MatchResult &Result) override {
320+
*Matched = true;
321+
}
322+
std::optional<TraversalKind> getCheckTraversalKind() const override {
323+
return TK;
324+
}
325+
} Callback(TK_IgnoreUnlessSpelledInSource, &Matched);
326+
Finder.addMatcher(Matcher, &Callback);
327+
std::unique_ptr<FrontendActionFactory> Factory(
328+
newFrontendActionFactory(&Finder));
329+
ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), Code));
330+
EXPECT_FALSE(Matched) << "Matcher not using specified TraversalKind, "
331+
"TK_IgnoreUnlessSpelledInSource";
332+
}
333+
}
334+
300335
TEST(IsInlineMatcher, IsInline) {
301336
EXPECT_TRUE(matches("void g(); inline void f();",
302337
functionDecl(isInline(), hasName("f"))));

0 commit comments

Comments
 (0)