[PATCH] D94031: [ASTMatchers] Fix child traversal over range-for loops
Stephen Kelly via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 4 13:45:25 PST 2021
steveire created this revision.
steveire added a reviewer: aaron.ballman.
steveire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D94031
Files:
clang/lib/ASTMatchers/ASTMatchFinder.cpp
clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===================================================================
--- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -2537,7 +2537,9 @@
int arr[2];
for (auto i : arr)
{
-
+ if (true)
+ {
+ }
}
}
)cpp";
@@ -2580,6 +2582,33 @@
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
}
+ {
+ auto M = cxxForRangeStmt(hasDescendant(ifStmt()));
+ EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+ EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+ }
+ {
+ EXPECT_TRUE(matches(
+ Code, traverse(TK_AsIs, cxxForRangeStmt(has(declStmt(
+ hasSingleDecl(varDecl(hasName("i")))))))));
+ EXPECT_TRUE(
+ matches(Code, traverse(TK_IgnoreUnlessSpelledInSource,
+ cxxForRangeStmt(has(varDecl(hasName("i")))))));
+ }
+ {
+ EXPECT_TRUE(matches(
+ Code, traverse(TK_AsIs, cxxForRangeStmt(has(declStmt(hasSingleDecl(
+ varDecl(hasInitializer(declRefExpr(
+ to(varDecl(hasName("arr")))))))))))));
+ EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource,
+ cxxForRangeStmt(has(declRefExpr(
+ to(varDecl(hasName("arr")))))))));
+ }
+ {
+ auto M = cxxForRangeStmt(has(compoundStmt()));
+ EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+ EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+ }
{
auto M = binaryOperator(hasOperatorName("!="));
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
Index: clang/lib/ASTMatchers/ASTMatchFinder.cpp
===================================================================
--- clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -236,6 +236,17 @@
ScopedIncrement ScopedDepth(&CurrentDepth);
return traverse(TAL);
}
+ bool TraverseCXXForRangeStmt(CXXForRangeStmt *Node) {
+ if (!Finder->isTraversalIgnoringImplicitNodes())
+ return VisitorBase::TraverseCXXForRangeStmt(Node);
+ if (!Node)
+ return true;
+ ScopedIncrement ScopedDepth(&CurrentDepth);
+ if (!match(*Node->getLoopVariable()) || !match(*Node->getRangeInit()) ||
+ !match(*Node->getBody()))
+ return false;
+ return VisitorBase::TraverseStmt(Node->getBody());
+ }
bool TraverseLambdaExpr(LambdaExpr *Node) {
if (!Finder->isTraversalIgnoringImplicitNodes())
return VisitorBase::TraverseLambdaExpr(Node);
@@ -575,8 +586,6 @@
if (isTraversalIgnoringImplicitNodes()) {
IgnoreImplicitChildren = true;
- if (Node.get<CXXForRangeStmt>())
- ScopedTraversal = true;
}
ASTNodeNotSpelledInSourceScope RAII(this, ScopedTraversal);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94031.314450.patch
Type: text/x-patch
Size: 3048 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210104/c909c87e/attachment.bin>
More information about the cfe-commits
mailing list