Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame^] | 1 | //===--- VMCore/PrintModulePass.cpp - Module/Function Printer -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // PrintModulePass and PrintFunctionPass implementations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Assembly/PrintModulePass.h" |
| 15 | |
| 16 | #include "llvm/Function.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class VISIBILITY_HIDDEN PrintModulePass : public ModulePass { |
| 25 | OStream *Out; // ostream to print on |
| 26 | bool DeleteStream; // Delete the ostream in our dtor? |
| 27 | public: |
| 28 | static char ID; |
| 29 | PrintModulePass() : ModulePass(intptr_t(&ID)), Out(&cerr), |
| 30 | DeleteStream(false) {} |
| 31 | PrintModulePass(OStream *o, bool DS) |
| 32 | : ModulePass(intptr_t(&ID)), Out(o), DeleteStream(DS) {} |
| 33 | |
| 34 | ~PrintModulePass() { |
| 35 | if (DeleteStream) delete Out; |
| 36 | } |
| 37 | |
| 38 | bool runOnModule(Module &M) { |
| 39 | (*Out) << M << std::flush; |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 44 | AU.setPreservesAll(); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | class PrintFunctionPass : public FunctionPass { |
| 49 | std::string Banner; // String to print before each function |
| 50 | OStream *Out; // ostream to print on |
| 51 | bool DeleteStream; // Delete the ostream in our dtor? |
| 52 | public: |
| 53 | static char ID; |
| 54 | PrintFunctionPass() : FunctionPass(intptr_t(&ID)), Banner(""), Out(&cerr), |
| 55 | DeleteStream(false) {} |
| 56 | PrintFunctionPass(const std::string &B, OStream *o, bool DS) |
| 57 | : FunctionPass(intptr_t(&ID)), Banner(B), Out(o), DeleteStream(DS) {} |
| 58 | |
| 59 | inline ~PrintFunctionPass() { |
| 60 | if (DeleteStream) delete Out; |
| 61 | } |
| 62 | |
| 63 | // runOnFunction - This pass just prints a banner followed by the |
| 64 | // function as it's processed. |
| 65 | // |
| 66 | bool runOnFunction(Function &F) { |
| 67 | (*Out) << Banner << static_cast<Value&>(F); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 72 | AU.setPreservesAll(); |
| 73 | } |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | char PrintModulePass::ID = 0; |
| 78 | static RegisterPass<PrintModulePass> |
| 79 | X("print-module", "Print module to stderr"); |
| 80 | char PrintFunctionPass::ID = 0; |
| 81 | static RegisterPass<PrintFunctionPass> |
| 82 | Y("print-function","Print function to stderr"); |
| 83 | |
| 84 | /// createPrintModulePass - Create and return a pass that writes the |
| 85 | /// module to the specified OStream. |
| 86 | ModulePass *llvm::createPrintModulePass(llvm::OStream *OS, bool DeleteStream) { |
| 87 | return new PrintModulePass(OS, DeleteStream); |
| 88 | } |
| 89 | |
| 90 | /// createPrintFunctionPass - Create and return a pass that prints |
| 91 | /// functions to the specified OStream as they are processed. |
| 92 | FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner, |
| 93 | OStream *OS, bool DeleteStream) { |
| 94 | return new PrintFunctionPass(Banner, OS, DeleteStream); |
| 95 | } |
| 96 | |