Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 1 | //===- unittest/Tooling/RecursiveASTVisitorTests/TraversalScope.cpp -------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Sam McCall | 814e797 | 2018-11-14 10:33:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "TestVisitor.h" |
| 10 | |
| 11 | using namespace clang; |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | class Visitor : public ExpectedLocationVisitor<Visitor, clang::TestVisitor> { |
| 16 | public: |
| 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 | |
| 26 | TEST(RecursiveASTVisitor, RespectsTraversalScope) { |
| 27 | auto AST = tooling::buildASTFromCode( |
| 28 | R"cpp( |
| 29 | struct 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 |