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" |
David Greene | 5f2433e | 2010-01-05 01:30:18 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 25 | class PrintModulePass : public ModulePass { |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 26 | std::string Banner; |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 27 | raw_ostream *Out; // raw_ostream to print on |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 28 | bool DeleteStream; // Delete the ostream in our dtor? |
| 29 | public: |
| 30 | static char ID; |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 31 | PrintModulePass() : ModulePass(ID), Out(&dbgs()), |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 32 | DeleteStream(false) {} |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 33 | PrintModulePass(const std::string &B, raw_ostream *o, bool DS) |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 34 | : ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {} |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 35 | |
| 36 | ~PrintModulePass() { |
| 37 | if (DeleteStream) delete Out; |
| 38 | } |
| 39 | |
| 40 | bool runOnModule(Module &M) { |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 41 | (*Out) << Banner << M; |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
| 45 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 46 | AU.setPreservesAll(); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | class PrintFunctionPass : public FunctionPass { |
| 51 | std::string Banner; // String to print before each function |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 52 | raw_ostream *Out; // raw_ostream to print on |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 53 | bool DeleteStream; // Delete the ostream in our dtor? |
| 54 | public: |
| 55 | static char ID; |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 56 | PrintFunctionPass() : FunctionPass(ID), Banner(""), Out(&dbgs()), |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 57 | DeleteStream(false) {} |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 58 | PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS) |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 59 | : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {} |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 60 | |
Dan Gohman | 3061485 | 2010-08-16 21:57:30 +0000 | [diff] [blame^] | 61 | ~PrintFunctionPass() { |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 62 | if (DeleteStream) delete Out; |
| 63 | } |
| 64 | |
| 65 | // runOnFunction - This pass just prints a banner followed by the |
| 66 | // function as it's processed. |
| 67 | // |
| 68 | bool runOnFunction(Function &F) { |
| 69 | (*Out) << Banner << static_cast<Value&>(F); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 74 | AU.setPreservesAll(); |
| 75 | } |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | char PrintModulePass::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 80 | INITIALIZE_PASS(PrintModulePass, "print-module", |
| 81 | "Print module to stderr", false, false); |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 82 | char PrintFunctionPass::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 83 | INITIALIZE_PASS(PrintFunctionPass, "print-function", |
| 84 | "Print function to stderr", false, false); |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 85 | |
| 86 | /// createPrintModulePass - Create and return a pass that writes the |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 87 | /// module to the specified raw_ostream. |
| 88 | ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS, |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 89 | bool DeleteStream, |
| 90 | const std::string &Banner) { |
| 91 | return new PrintModulePass(Banner, OS, DeleteStream); |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /// createPrintFunctionPass - Create and return a pass that prints |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 95 | /// functions to the specified raw_ostream as they are processed. |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 96 | FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner, |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 97 | llvm::raw_ostream *OS, |
| 98 | bool DeleteStream) { |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 99 | return new PrintFunctionPass(Banner, OS, DeleteStream); |
| 100 | } |
| 101 | |