blob: c05be7f2e31f6f414795d284190631f74c840ace [file] [log] [blame]
Sam McCall814e7972018-11-14 10:33:30 +00001//===- unittest/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp -------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sam McCall814e7972018-11-14 10:33:30 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "TestVisitor.h"
10
11using namespace clang;
12
13namespace {
14
15class Visitor : public ExpectedLocationVisitor<Visitor, clang::TestVisitor> {
16public:
17 Visitor(ASTContext *Context) { this->Context = Context; }
18
19 bool VisitNamedDecl(NamedDecl *D) {
20 if (!D->isImplicit())
21 Match(D->getName(), D->getLocation());
22 return true;
23 }
24};
25
26TEST(RecursiveASTVisitor, RespectsTraversalScope) {
27 auto AST = tooling::buildASTFromCode(
28 R"cpp(
29struct foo {
30 struct bar {
31 struct baz {};
32 };
33};
34 )cpp",
35 "foo.cpp", std::make_shared<PCHContainerOperations>());
36 auto &Ctx = AST->getASTContext();
37 auto &TU = *Ctx.getTranslationUnitDecl();
38 auto &Foo = *TU.lookup(&Ctx.Idents.get("foo")).front();
39 auto &Bar = *cast<DeclContext>(Foo).lookup(&Ctx.Idents.get("bar")).front();
40
41 Ctx.setTraversalScope({&Bar});
42
43 Visitor V(&Ctx);
44 V.DisallowMatch("foo", 2, 8);
45 V.ExpectMatch("bar", 3, 10);
46 V.ExpectMatch("baz", 4, 12);
47 V.TraverseAST(Ctx);
48}
49
50} // end anonymous namespace