]> granicus.if.org Git - clang/commitdiff
Let RecursiveASTVisitor visit array index VarDecls
authorNico Weber <nicolasweber@gmx.de>
Fri, 22 Jan 2016 15:11:54 +0000 (15:11 +0000)
committerNico Weber <nicolasweber@gmx.de>
Fri, 22 Jan 2016 15:11:54 +0000 (15:11 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@258503 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/RecursiveASTVisitor.h
unittests/ASTMatchers/ASTMatchersTest.cpp
unittests/Tooling/RecursiveASTVisitorTest.cpp

index 725cf3bcea62f28309c1ee92195c5c87ec79ad32..0b89a963e3e9fea695a2b88a95b666d1c508d5b3 100644 (file)
@@ -809,6 +809,10 @@ bool RecursiveASTVisitor<Derived>::TraverseConstructorInitializer(
 
   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;
 }
 
index b6983052dd8196b7d71560d47d3bad66b01d2a89..ab1997ab0f319d13053389d3d610ae9e5f364711 100644 (file)
@@ -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; } };"
index 5d0e2bccd4d756216b2f59871622a43828b2e613..991ae8bb7f3d274a2879675d6f5edad27db6c6b5 100644 (file)
@@ -133,4 +133,23 @@ TEST(RecursiveASTVisitor, AttributesAreVisited) {
     "};\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