RecursiveASTVisitorTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===- unittest/AST/RecursiveASTVisitorTest.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 "clang/AST/RecursiveASTVisitor.h"
  9. #include "clang/AST/ASTConsumer.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/AST/Attr.h"
  12. #include "clang/Frontend/FrontendAction.h"
  13. #include "clang/Tooling/Tooling.h"
  14. #include "llvm/ADT/FunctionExtras.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "gmock/gmock.h"
  17. #include "gtest/gtest.h"
  18. #include <cassert>
  19. using namespace clang;
  20. using ::testing::ElementsAre;
  21. namespace {
  22. class ProcessASTAction : public clang::ASTFrontendAction {
  23. public:
  24. ProcessASTAction(llvm::unique_function<void(clang::ASTContext &)> Process)
  25. : Process(std::move(Process)) {
  26. assert(this->Process);
  27. }
  28. std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
  29. StringRef InFile) {
  30. class Consumer : public ASTConsumer {
  31. public:
  32. Consumer(llvm::function_ref<void(ASTContext &CTx)> Process)
  33. : Process(Process) {}
  34. void HandleTranslationUnit(ASTContext &Ctx) override { Process(Ctx); }
  35. private:
  36. llvm::function_ref<void(ASTContext &CTx)> Process;
  37. };
  38. return std::make_unique<Consumer>(Process);
  39. }
  40. private:
  41. llvm::unique_function<void(clang::ASTContext &)> Process;
  42. };
  43. enum class VisitEvent {
  44. StartTraverseFunction,
  45. EndTraverseFunction,
  46. StartTraverseAttr,
  47. EndTraverseAttr
  48. };
  49. class CollectInterestingEvents
  50. : public RecursiveASTVisitor<CollectInterestingEvents> {
  51. public:
  52. bool TraverseFunctionDecl(FunctionDecl *D) {
  53. Events.push_back(VisitEvent::StartTraverseFunction);
  54. bool Ret = RecursiveASTVisitor::TraverseFunctionDecl(D);
  55. Events.push_back(VisitEvent::EndTraverseFunction);
  56. return Ret;
  57. }
  58. bool TraverseAttr(Attr *A) {
  59. Events.push_back(VisitEvent::StartTraverseAttr);
  60. bool Ret = RecursiveASTVisitor::TraverseAttr(A);
  61. Events.push_back(VisitEvent::EndTraverseAttr);
  62. return Ret;
  63. }
  64. std::vector<VisitEvent> takeEvents() && { return std::move(Events); }
  65. private:
  66. std::vector<VisitEvent> Events;
  67. };
  68. std::vector<VisitEvent> collectEvents(llvm::StringRef Code) {
  69. CollectInterestingEvents Visitor;
  70. clang::tooling::runToolOnCode(
  71. std::make_unique<ProcessASTAction>(
  72. [&](clang::ASTContext &Ctx) { Visitor.TraverseAST(Ctx); }),
  73. Code);
  74. return std::move(Visitor).takeEvents();
  75. }
  76. } // namespace
  77. TEST(RecursiveASTVisitorTest, AttributesInsideDecls) {
  78. /// Check attributes are traversed inside TraverseFunctionDecl.
  79. llvm::StringRef Code = R"cpp(
  80. __attribute__((annotate("something"))) int foo() { return 10; }
  81. )cpp";
  82. EXPECT_THAT(collectEvents(Code),
  83. ElementsAre(VisitEvent::StartTraverseFunction,
  84. VisitEvent::StartTraverseAttr,
  85. VisitEvent::EndTraverseAttr,
  86. VisitEvent::EndTraverseFunction));
  87. }