1 //===--- VMCore/PrintModulePass.cpp - Module/Function Printer -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // PrintModulePass and PrintFunctionPass implementations.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Assembly/PrintModulePass.h"
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/raw_ostream.h"
25 class VISIBILITY_HIDDEN PrintModulePass : public ModulePass {
26 raw_ostream *Out; // raw_ostream to print on
27 bool DeleteStream; // Delete the ostream in our dtor?
30 PrintModulePass() : ModulePass(&ID), Out(&errs()),
31 DeleteStream(false) {}
32 PrintModulePass(raw_ostream *o, bool DS)
33 : ModulePass(&ID), Out(o), DeleteStream(DS) {}
36 if (DeleteStream) delete Out;
39 bool runOnModule(Module &M) {
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 class PrintFunctionPass : public FunctionPass {
51 std::string Banner; // String to print before each function
52 raw_ostream *Out; // raw_ostream to print on
53 bool DeleteStream; // Delete the ostream in our dtor?
56 PrintFunctionPass() : FunctionPass(&ID), Banner(""), Out(&errs()),
57 DeleteStream(false) {}
58 PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
59 : FunctionPass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
61 inline ~PrintFunctionPass() {
62 if (DeleteStream) delete Out;
65 // runOnFunction - This pass just prints a banner followed by the
66 // function as it's processed.
68 bool runOnFunction(Function &F) {
69 (*Out) << Banner << static_cast<Value&>(F);
74 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
80 char PrintModulePass::ID = 0;
81 static RegisterPass<PrintModulePass>
82 X("print-module", "Print module to stderr");
83 char PrintFunctionPass::ID = 0;
84 static RegisterPass<PrintFunctionPass>
85 Y("print-function","Print function to stderr");
87 /// createPrintModulePass - Create and return a pass that writes the
88 /// module to the specified raw_ostream.
89 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
91 return new PrintModulePass(OS, DeleteStream);
94 /// createPrintFunctionPass - Create and return a pass that prints
95 /// functions to the specified raw_ostream as they are processed.
96 FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
97 llvm::raw_ostream *OS,
99 return new PrintFunctionPass(Banner, OS, DeleteStream);