[PATCH] D95562: [ASTMatchers] Fix traversal below range-for elements
Stephen Kelly via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 27 14:14:41 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/D95562
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
@@ -2784,6 +2784,31 @@
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
}
+ Code = R"cpp(
+ struct Range {
+ int* begin() const;
+ int* end() const;
+ };
+ Range getRange(int);
+
+ void rangeFor()
+ {
+ for (auto i : getRange(42))
+ {
+ }
+ }
+ )cpp";
+ {
+ auto M = integerLiteral(equals(42));
+ EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+ EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+ }
+ {
+ auto M = callExpr(hasDescendant(integerLiteral(equals(42))));
+ EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M)));
+ EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M)));
+ }
+
Code = R"cpp(
void rangeFor()
{
@@ -2855,6 +2880,32 @@
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M),
true, {"-std=c++20"}));
}
+
+ Code = R"cpp(
+ struct Range {
+ int* begin() const;
+ int* end() const;
+ };
+ Range getRange(int);
+
+ int getNum(int);
+
+ void rangeFor()
+ {
+ for (auto j = getNum(42); auto i : getRange(j))
+ {
+ }
+ }
+ )cpp";
+ {
+ auto M = integerLiteral(equals(42));
+ EXPECT_TRUE(
+ matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"}));
+ EXPECT_TRUE(
+ matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M),
+ true, {"-std=c++20"}));
+ }
+
Code = R"cpp(
void hasDefaultArg(int i, int j = 0)
{
Index: clang/lib/ASTMatchers/ASTMatchFinder.cpp
===================================================================
--- clang/lib/ASTMatchers/ASTMatchFinder.cpp
+++ clang/lib/ASTMatchers/ASTMatchFinder.cpp
@@ -488,6 +488,20 @@
bool dataTraverseNode(Stmt *S, DataRecursionQueue *Queue) {
if (auto *RF = dyn_cast<CXXForRangeStmt>(S)) {
+ {
+ ASTNodeNotAsIsSourceScope RAII(this, true);
+ TraverseStmt(RF->getInit());
+ // Don't traverse under the loop variable
+ match(*RF->getLoopVariable());
+ TraverseStmt(RF->getRangeInit());
+ TraverseStmt(RF->getBody());
+ }
+ {
+ ASTNodeNotSpelledInSourceScope RAII(this, true);
+ for (auto *SubStmt : RF->children()) {
+ TraverseStmt(SubStmt);
+ }
+ }
for (auto *SubStmt : RF->children()) {
if (SubStmt == RF->getInit() || SubStmt == RF->getLoopVarStmt() ||
SubStmt == RF->getRangeInit() || SubStmt == RF->getBody()) {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95562.319673.patch
Type: text/x-patch
Size: 2765 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210127/67be1c1d/attachment.bin>
More information about the cfe-commits
mailing list