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