|
| 1 | +//===- LLVMPrintFunctionNames.cpp |
| 2 | +//---------------------------------------------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// Example clang plugin which simply prints the names of all the functions |
| 11 | +// within the generated LLVM code. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include "clang/AST/AST.h" |
| 16 | +#include "clang/AST/ASTConsumer.h" |
| 17 | +#include "clang/AST/RecursiveASTVisitor.h" |
| 18 | +#include "clang/Frontend/CompilerInstance.h" |
| 19 | +#include "clang/Frontend/FrontendPluginRegistry.h" |
| 20 | +#include "clang/Sema/Sema.h" |
| 21 | +#include "llvm/IR/PassManager.h" |
| 22 | +#include "llvm/Passes/OptimizationLevel.h" |
| 23 | +#include "llvm/Passes/PassBuilder.h" |
| 24 | +#include "llvm/Support/raw_ostream.h" |
| 25 | +using namespace clang; |
| 26 | + |
| 27 | +namespace { |
| 28 | + |
| 29 | +class PrintPass final : public llvm::AnalysisInfoMixin<PrintPass> { |
| 30 | + friend struct llvm::AnalysisInfoMixin<PrintPass>; |
| 31 | + |
| 32 | +private: |
| 33 | + static llvm::AnalysisKey key; |
| 34 | + |
| 35 | +public: |
| 36 | + using Result = llvm::PreservedAnalyses; |
| 37 | + |
| 38 | + Result run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) { |
| 39 | + for (auto &F : M) |
| 40 | + llvm::outs() << "[PrintPass] Found function: " << F.getName() << "\n"; |
| 41 | + return llvm::PreservedAnalyses::all(); |
| 42 | + } |
| 43 | + static bool isRequired() { return true; } |
| 44 | +}; |
| 45 | + |
| 46 | +void PrintCallback(llvm::PassBuilder &PB) { |
| 47 | + PB.registerPipelineStartEPCallback( |
| 48 | + [](llvm::ModulePassManager &MPM, llvm::OptimizationLevel) { |
| 49 | + MPM.addPass(PrintPass()); |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +class LLVMPrintFunctionsConsumer : public ASTConsumer { |
| 54 | +public: |
| 55 | + LLVMPrintFunctionsConsumer(CompilerInstance &Instance) { |
| 56 | + Instance.getCodeGenOpts().PassBuilderCallbacks.push_back(PrintCallback); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +class LLVMPrintFunctionNamesAction : public PluginASTAction { |
| 61 | +protected: |
| 62 | + std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 63 | + llvm::StringRef) override { |
| 64 | + return std::make_unique<LLVMPrintFunctionsConsumer>(CI); |
| 65 | + } |
| 66 | + bool ParseArgs(const CompilerInstance &, |
| 67 | + const std::vector<std::string> &) override { |
| 68 | + return true; |
| 69 | + } |
| 70 | + PluginASTAction::ActionType getActionType() override { |
| 71 | + return AddBeforeMainAction; |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +} // namespace |
| 76 | + |
| 77 | +static FrontendPluginRegistry::Add<LLVMPrintFunctionNamesAction> |
| 78 | + X("llvm-print-fns", "print function names, llvm level"); |
0 commit comments