[PATCH] D65735: [AST] Fix RecursiveASTVisitor visiting implicit constructor initializers.
Johan Vikström via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 5 01:57:44 PDT 2019
jvikstrom updated this revision to Diff 213291.
jvikstrom marked 4 inline comments as done.
jvikstrom added a comment.
Moved test to its own test file (also addressed comments about the test).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D65735/new/
https://reviews.llvm.org/D65735
Files:
clang/include/clang/AST/RecursiveASTVisitor.h
clang/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp
Index: clang/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp
===================================================================
--- /dev/null
+++ clang/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp
@@ -0,0 +1,57 @@
+//===- unittest/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp
+//-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "TestVisitor.h"
+
+using namespace clang;
+
+namespace {
+
+// Check to ensure that CXXCtorInitializer is not visited when implicit code
+// should not be visited and that it is visited when implicit code should be
+// visited.
+class CXXCtorInitializerVisitor
+ : public ExpectedLocationVisitor<CXXCtorInitializerVisitor> {
+public:
+ CXXCtorInitializerVisitor(bool VisitImplicitCode)
+ : VisitImplicitCode(VisitImplicitCode) {}
+
+ bool shouldVisitImplicitCode() const { return VisitImplicitCode; }
+
+ bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
+ if (!Init->isWritten())
+ VisitedImplicitInitializer = true;
+ Match("initializer", Init->getSourceLocation());
+ return true;
+ }
+
+ bool VisitedImplicitInitializer = false;
+
+private:
+ bool VisitImplicitCode;
+};
+
+TEST(RecursiveASTVisitor, CXXCtorInitializerVisitNoImplicit) {
+ for (bool VisitImplCode : {true, false}) {
+ CXXCtorInitializerVisitor Visitor(VisitImplCode);
+ Visitor.ExpectMatch("initializer", 7, 17);
+ EXPECT_TRUE(Visitor.runOver(R"cpp(
+ class A {};
+ class B : public A {
+ B() {};
+ };
+ class C : public A {
+ C() : A() {}
+ };
+ )cpp",
+ CXXCtorInitializerVisitor::Lang_CXX));
+ EXPECT_EQ(Visitor.VisitedImplicitInitializer, VisitImplCode);
+ }
+}
+} // end anonymous namespace
Index: clang/include/clang/AST/RecursiveASTVisitor.h
===================================================================
--- clang/include/clang/AST/RecursiveASTVisitor.h
+++ clang/include/clang/AST/RecursiveASTVisitor.h
@@ -2023,7 +2023,8 @@
if (CXXConstructorDecl *Ctor = dyn_cast<CXXConstructorDecl>(D)) {
// Constructor initializers.
for (auto *I : Ctor->inits()) {
- TRY_TO(TraverseConstructorInitializer(I));
+ if (I->isWritten() || getDerived().shouldVisitImplicitCode())
+ TRY_TO(TraverseConstructorInitializer(I));
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65735.213291.patch
Type: text/x-patch
Size: 2637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190805/0dd89212/attachment.bin>
More information about the cfe-commits
mailing list