[PATCH] D16413: Let RecursiveASTVisitor visit array index VarDecls

Nico Weber via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 21 09:53:29 PST 2016


thakis created this revision.
thakis added reviewers: rsmith, klimek.
thakis added a subscriber: cfe-commits.
Herald added a subscriber: klimek.

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

Files:
  include/clang/AST/RecursiveASTVisitor.h
  unittests/ASTMatchers/ASTMatchersTest.cpp

Index: include/clang/AST/RecursiveASTVisitor.h
===================================================================
--- include/clang/AST/RecursiveASTVisitor.h
+++ include/clang/AST/RecursiveASTVisitor.h
@@ -809,6 +809,10 @@
 
   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: unittests/ASTMatchers/ASTMatchersTest.cpp
===================================================================
--- unittests/ASTMatchers/ASTMatchersTest.cpp
+++ unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -4207,6 +4207,14 @@
               hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation())))))));
 }
 
+TEST(HasAncestor, FIXMEbettername) {
+  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; } };"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16413.45549.patch
Type: text/x-patch
Size: 1269 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160121/a971f6d1/attachment.bin>


More information about the cfe-commits mailing list