Clang Project

clang_source_code/unittests/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp
1//===- unittest/Tooling/RecursiveASTVisitorTests/TraversalScope.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
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"28);
45  V.ExpectMatch("bar"310);
46  V.ExpectMatch("baz"412);
47  V.TraverseAST(Ctx);
48}
49
50// end anonymous namespace
51