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