1 //===- unittest/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp -------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "TestVisitor.h"
11 using namespace clang
;
15 class Visitor
: public ExpectedLocationVisitor
{
17 Visitor(ASTContext
*Context
) { this->Context
= Context
; }
19 bool VisitTranslationUnitDecl(TranslationUnitDecl
*D
) override
{
20 auto &SM
= D
->getParentASTContext().getSourceManager();
21 Match("TU", SM
.getLocForStartOfFile(SM
.getMainFileID()));
25 bool VisitNamedDecl(NamedDecl
*D
) override
{
27 Match(D
->getName(), D
->getLocation());
32 TEST(RecursiveASTVisitor
, RespectsTraversalScope
) {
33 auto AST
= tooling::buildASTFromCode(
41 "foo.cpp", std::make_shared
<PCHContainerOperations
>());
42 auto &Ctx
= AST
->getASTContext();
43 auto &TU
= *Ctx
.getTranslationUnitDecl();
44 auto &Foo
= *TU
.lookup(&Ctx
.Idents
.get("foo")).front();
45 auto &Bar
= *cast
<DeclContext
>(Foo
).lookup(&Ctx
.Idents
.get("bar")).front();
47 Ctx
.setTraversalScope({&Bar
});
50 V
.ExpectMatch("TU", 1, 1);
51 V
.DisallowMatch("foo", 2, 8);
52 V
.ExpectMatch("bar", 3, 10);
53 V
.ExpectMatch("baz", 4, 12);
57 } // end anonymous namespace