TraversalScope.cpp 1.3 KB

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