r258503 - Let RecursiveASTVisitor visit array index VarDecls
Nico Weber via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 22 07:11:54 PST 2016
Author: nico
Date: Fri Jan 22 09:11:54 2016
New Revision: 258503
URL: http://llvm.org/viewvc/llvm-project?rev=258503&view=rev
Log:
Let RecursiveASTVisitor visit array index VarDecls
An implicit copy ctor creates loop VarDecls that hang off CXXCtorInitializer.
RecursiveASTVisitor used to not visit them, so that they didn't show up in the
parent map used by ASTMatchers, causing asserts() when the implicit
DeclRefExpr() in a CXXCtorInitializer referred to one of these VarDecls.
Fixes PR26227.
http://reviews.llvm.org/D16413
Modified:
cfe/trunk/include/clang/AST/RecursiveASTVisitor.h
cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp
cfe/trunk/unittests/Tooling/RecursiveASTVisitorTest.cpp
Modified: cfe/trunk/include/clang/AST/RecursiveASTVisitor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/RecursiveASTVisitor.h?rev=258503&r1=258502&r2=258503&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/RecursiveASTVisitor.h (original)
+++ cfe/trunk/include/clang/AST/RecursiveASTVisitor.h Fri Jan 22 09:11:54 2016
@@ -809,6 +809,10 @@ bool RecursiveASTVisitor<Derived>::Trave
if (Init->isWritten() || getDerived().shouldVisitImplicitCode())
TRY_TO(TraverseStmt(Init->getInit()));
+
+ if (Init->getNumArrayIndices() && getDerived().shouldVisitImplicitCode())
+ for (VarDecl *VD : Init->getArrayIndexes())
+ TRY_TO(TraverseDecl(VD));
return true;
}
Modified: cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp?rev=258503&r1=258502&r2=258503&view=diff
==============================================================================
--- cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp (original)
+++ cfe/trunk/unittests/ASTMatchers/ASTMatchersTest.cpp Fri Jan 22 09:11:54 2016
@@ -4217,6 +4217,14 @@ TEST(HasAncestor, MatchesAllAncestors) {
hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation())))))));
}
+TEST(HasAncestor, ImplicitArrayCopyCtorDeclRefExpr) {
+ EXPECT_TRUE(matches("struct MyClass {\n"
+ " int c[1];\n"
+ " static MyClass Create() { return MyClass(); }\n"
+ "};",
+ declRefExpr(to(decl(hasAncestor(decl()))))));
+}
+
TEST(HasParent, MatchesAllParents) {
EXPECT_TRUE(matches(
"template <typename T> struct C { static void f() { 42; } };"
Modified: cfe/trunk/unittests/Tooling/RecursiveASTVisitorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/RecursiveASTVisitorTest.cpp?rev=258503&r1=258502&r2=258503&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/RecursiveASTVisitorTest.cpp (original)
+++ cfe/trunk/unittests/Tooling/RecursiveASTVisitorTest.cpp Fri Jan 22 09:11:54 2016
@@ -133,4 +133,23 @@ TEST(RecursiveASTVisitor, AttributesAreV
"};\n"));
}
+// Check to ensure that VarDecls are visited.
+class VarDeclVisitor : public ExpectedLocationVisitor<VarDeclVisitor> {
+public:
+ bool VisitVarDecl(VarDecl *VD) {
+ Match(VD->getNameAsString(), VD->getLocStart());
+ return true;
+ }
+};
+
+TEST(RecursiveASTVisitor, ArrayInitializersAreVisited) {
+ VarDeclVisitor Visitor;
+ Visitor.ExpectMatch("__i0", 1, 8);
+ EXPECT_TRUE(
+ Visitor.runOver("struct MyClass {\n"
+ " int c[1];\n"
+ " static MyClass Create() { return MyClass(); }\n"
+ "};\n"));
+}
+
} // end anonymous namespace
More information about the cfe-commits
mailing list