1 //===- unittest/Tooling/RecursiveASTVisitorTests/TraversalScope.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 class Visitor : public ExpectedLocationVisitor<Visitor, clang::TestVisitor> { 17 public: 18 Visitor(ASTContext *Context) { this->Context = Context; } 19 20 bool VisitNamedDecl(NamedDecl *D) { 21 if (!D->isImplicit()) 22 Match(D->getName(), D->getLocation()); 23 return true; 24 } 25 }; 26 27 TEST(RecursiveASTVisitor, RespectsTraversalScope) { 28 auto AST = tooling::buildASTFromCode( 29 R"cpp( 30 struct foo { 31 struct bar { 32 struct baz {}; 33 }; 34 }; 35 )cpp", 36 "foo.cpp", std::make_shared<PCHContainerOperations>()); 37 auto &Ctx = AST->getASTContext(); 38 auto &TU = *Ctx.getTranslationUnitDecl(); 39 auto &Foo = *TU.lookup(&Ctx.Idents.get("foo")).front(); 40 auto &Bar = *cast<DeclContext>(Foo).lookup(&Ctx.Idents.get("bar")).front(); 41 42 Ctx.setTraversalScope({&Bar}); 43 44 Visitor V(&Ctx); 45 V.DisallowMatch("foo", 2, 8); 46 V.ExpectMatch("bar", 3, 10); 47 V.ExpectMatch("baz", 4, 12); 48 V.TraverseAST(Ctx); 49 } 50 51 } // end anonymous namespace 52