xref: /llvm-project/clang/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp (revision 4e600751d2f7e8e7b85a71b7128b68444bdde91b)
1be60f97dSJohan Vikstrom //=- unittest/Tooling/RecursiveASTVisitorTests/ImplicitCtorInitializer.cpp -=//
2be60f97dSJohan Vikstrom //
3be60f97dSJohan Vikstrom // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4be60f97dSJohan Vikstrom // See https://llvm.org/LICENSE.txt for license information.
5be60f97dSJohan Vikstrom // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6be60f97dSJohan Vikstrom //
7be60f97dSJohan Vikstrom //===----------------------------------------------------------------------===//
8be60f97dSJohan Vikstrom 
9be60f97dSJohan Vikstrom #include "TestVisitor.h"
10be60f97dSJohan Vikstrom 
11be60f97dSJohan Vikstrom using namespace clang;
12be60f97dSJohan Vikstrom 
13be60f97dSJohan Vikstrom namespace {
14be60f97dSJohan Vikstrom 
15*4e600751SSirraide class CXXCtorInitializerVisitor : public ExpectedLocationVisitor {
16be60f97dSJohan Vikstrom public:
17*4e600751SSirraide   CXXCtorInitializerVisitor(bool VisitImplicitCode) {
18*4e600751SSirraide     ShouldVisitImplicitCode = VisitImplicitCode;
19*4e600751SSirraide   }
20be60f97dSJohan Vikstrom 
21*4e600751SSirraide   bool TraverseConstructorInitializer(CXXCtorInitializer *Init) override {
22be60f97dSJohan Vikstrom     if (!Init->isWritten())
23be60f97dSJohan Vikstrom       VisitedImplicitInitializer = true;
24be60f97dSJohan Vikstrom     Match("initializer", Init->getSourceLocation());
25*4e600751SSirraide     return ExpectedLocationVisitor::TraverseConstructorInitializer(Init);
26be60f97dSJohan Vikstrom   }
27be60f97dSJohan Vikstrom 
28be60f97dSJohan Vikstrom   bool VisitedImplicitInitializer = false;
29be60f97dSJohan Vikstrom };
30be60f97dSJohan Vikstrom 
31be60f97dSJohan Vikstrom // Check to ensure that CXXCtorInitializer is not visited when implicit code
32be60f97dSJohan Vikstrom // should not be visited and that it is visited when implicit code should be
33be60f97dSJohan Vikstrom // visited.
34be60f97dSJohan Vikstrom TEST(RecursiveASTVisitor, CXXCtorInitializerVisitNoImplicit) {
35be60f97dSJohan Vikstrom   for (bool VisitImplCode : {true, false}) {
36be60f97dSJohan Vikstrom     CXXCtorInitializerVisitor Visitor(VisitImplCode);
37be60f97dSJohan Vikstrom     Visitor.ExpectMatch("initializer", 7, 17);
3886a98baaSJohan Vikstrom     llvm::StringRef Code = R"cpp(
39be60f97dSJohan Vikstrom         class A {};
40be60f97dSJohan Vikstrom         class B : public A {
41be60f97dSJohan Vikstrom           B() {};
42be60f97dSJohan Vikstrom         };
43be60f97dSJohan Vikstrom         class C : public A {
44be60f97dSJohan Vikstrom           C() : A() {}
45be60f97dSJohan Vikstrom         };
46a96cfee9SDavid Green       )cpp";
47a96cfee9SDavid Green     EXPECT_TRUE(Visitor.runOver(Code, CXXCtorInitializerVisitor::Lang_CXX));
48be60f97dSJohan Vikstrom     EXPECT_EQ(Visitor.VisitedImplicitInitializer, VisitImplCode);
49be60f97dSJohan Vikstrom   }
50be60f97dSJohan Vikstrom }
51be60f97dSJohan Vikstrom } // end anonymous namespace
52