1 //===- LLVMPrintFunctionNames.cpp
2 //---------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 // Example clang plugin which simply prints the names of all the functions
11 // within the generated LLVM code.
13 //===----------------------------------------------------------------------===//
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"
26 using namespace clang
;
30 class PrintPass final
: public llvm::AnalysisInfoMixin
<PrintPass
> {
31 friend struct llvm::AnalysisInfoMixin
<PrintPass
>;
34 using Result
= llvm::PreservedAnalyses
;
36 Result
run(llvm::Module
&M
, llvm::ModuleAnalysisManager
&MAM
) {
38 llvm::outs() << "[PrintPass] Found function: " << F
.getName() << "\n";
39 return llvm::PreservedAnalyses::all();
41 static bool isRequired() { return true; }
44 void PrintCallback(llvm::PassBuilder
&PB
) {
45 PB
.registerPipelineStartEPCallback(
46 [](llvm::ModulePassManager
&MPM
, llvm::OptimizationLevel
) {
47 MPM
.addPass(PrintPass());
51 class LLVMPrintFunctionsConsumer
: public ASTConsumer
{
53 LLVMPrintFunctionsConsumer(CompilerInstance
&Instance
) {
54 Instance
.getCodeGenOpts().PassBuilderCallbacks
.push_back(PrintCallback
);
58 class LLVMPrintFunctionNamesAction
: public PluginASTAction
{
60 std::unique_ptr
<ASTConsumer
> CreateASTConsumer(CompilerInstance
&CI
,
61 llvm::StringRef
) override
{
62 return std::make_unique
<LLVMPrintFunctionsConsumer
>(CI
);
64 bool ParseArgs(const CompilerInstance
&,
65 const std::vector
<std::string
> &) override
{
68 PluginASTAction::ActionType
getActionType() override
{
69 return AddBeforeMainAction
;
75 static FrontendPluginRegistry::Add
<LLVMPrintFunctionNamesAction
>
76 X("llvm-print-fns", "print function names, llvm level");