xref: /llvm-project/clang/unittests/Tooling/RecursiveASTVisitorTests/ImplicitCtor.cpp (revision 4e600751d2f7e8e7b85a71b7128b68444bdde91b)
1 //===- unittest/Tooling/RecursiveASTVisitorTests/ImplicitCtor.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 // A visitor that visits implicit declarations and matches constructors.
16 class ImplicitCtorVisitor : public ExpectedLocationVisitor {
17 public:
18   bool VisitCXXConstructorDecl(CXXConstructorDecl *Ctor) override {
19     if (Ctor->isImplicit()) {  // Was not written in source code
20       if (const CXXRecordDecl* Class = Ctor->getParent()) {
21         Match(Class->getName(), Ctor->getLocation());
22       }
23     }
24     return true;
25   }
26 };
27 
28 TEST(RecursiveASTVisitor, VisitsImplicitCopyConstructors) {
29   ImplicitCtorVisitor Visitor;
30   Visitor.ExpectMatch("Simple", 2, 8);
31   // Note: Clang lazily instantiates implicit declarations, so we need
32   // to use them in order to force them to appear in the AST.
33   EXPECT_TRUE(Visitor.runOver(
34       "struct WithCtor { WithCtor(); }; \n"
35       "struct Simple { Simple(); WithCtor w; }; \n"
36       "int main() { Simple s; Simple t(s); }\n"));
37 }
38 
39 } // end anonymous namespace
40