1 //===- unittest/Tooling/RecursiveASTVisitorTests/ImplicitCtor.cpp ---------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "TestVisitor.h" 11 12 using namespace clang; 13 14 namespace { 15 16 // A visitor that visits implicit declarations and matches constructors. 17 class ImplicitCtorVisitor 18 : public ExpectedLocationVisitor<ImplicitCtorVisitor> { 19 public: 20 bool shouldVisitImplicitCode() const { return true; } 21 22 bool VisitCXXConstructorDecl(CXXConstructorDecl* Ctor) { 23 if (Ctor->isImplicit()) { // Was not written in source code 24 if (const CXXRecordDecl* Class = Ctor->getParent()) { 25 Match(Class->getName(), Ctor->getLocation()); 26 } 27 } 28 return true; 29 } 30 }; 31 32 TEST(RecursiveASTVisitor, VisitsImplicitCopyConstructors) { 33 ImplicitCtorVisitor Visitor; 34 Visitor.ExpectMatch("Simple", 2, 8); 35 // Note: Clang lazily instantiates implicit declarations, so we need 36 // to use them in order to force them to appear in the AST. 37 EXPECT_TRUE(Visitor.runOver( 38 "struct WithCtor { WithCtor(); }; \n" 39 "struct Simple { Simple(); WithCtor w; }; \n" 40 "int main() { Simple s; Simple t(s); }\n")); 41 } 42 43 } // end anonymous namespace 44