77 lines · cpp
1//===- LLVMPrintFunctionNames.cpp2//---------------------------------------------===//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-exception7//8//===----------------------------------------------------------------------===//9//10// Example clang plugin which simply prints the names of all the functions11// 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/Module.h"22#include "llvm/IR/PassManager.h"23#include "llvm/Passes/OptimizationLevel.h"24#include "llvm/Passes/PassBuilder.h"25#include "llvm/Support/raw_ostream.h"26using namespace clang;27 28namespace {29 30class PrintPass final : public llvm::AnalysisInfoMixin<PrintPass> {31 friend struct llvm::AnalysisInfoMixin<PrintPass>;32 33public:34 using Result = llvm::PreservedAnalyses;35 36 Result run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) {37 for (auto &F : M)38 llvm::outs() << "[PrintPass] Found function: " << F.getName() << "\n";39 return llvm::PreservedAnalyses::all();40 }41 static bool isRequired() { return true; }42};43 44void PrintCallback(llvm::PassBuilder &PB) {45 PB.registerPipelineStartEPCallback(46 [](llvm::ModulePassManager &MPM, llvm::OptimizationLevel) {47 MPM.addPass(PrintPass());48 });49}50 51class LLVMPrintFunctionsConsumer : public ASTConsumer {52public:53 LLVMPrintFunctionsConsumer(CompilerInstance &Instance) {54 Instance.getCodeGenOpts().PassBuilderCallbacks.push_back(PrintCallback);55 }56};57 58class LLVMPrintFunctionNamesAction : public PluginASTAction {59protected:60 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,61 llvm::StringRef) override {62 return std::make_unique<LLVMPrintFunctionsConsumer>(CI);63 }64 bool ParseArgs(const CompilerInstance &,65 const std::vector<std::string> &) override {66 return true;67 }68 PluginASTAction::ActionType getActionType() override {69 return AddBeforeMainAction;70 }71};72 73} // namespace74 75static FrontendPluginRegistry::Add<LLVMPrintFunctionNamesAction>76 X("llvm-print-fns", "print function names, llvm level");77