For PR411:
[oota-llvm.git] / include / llvm / Assembly / PrintModulePass.h
1 //===- llvm/Assembly/PrintModulePass.h - Printing Pass ----------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines two passes to print out a module.  The PrintModulePass pass
11 // simply prints out the entire module when it is executed.  The
12 // PrintFunctionPass class is designed to be pipelined with other
13 // FunctionPass's, and prints out the functions of the class as they are
14 // processed.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_ASSEMBLY_PRINTMODULEPASS_H
19 #define LLVM_ASSEMBLY_PRINTMODULEPASS_H
20
21 #include "llvm/Pass.h"
22 #include "llvm/Module.h"
23 #include "llvm/Support/Streams.h"
24
25 namespace llvm {
26
27 class PrintModulePass : public ModulePass {
28   OStream *Out;           // ostream to print on
29   bool DeleteStream;      // Delete the ostream in our dtor?
30 public:
31   PrintModulePass() : Out(&cerr), DeleteStream(false) {}
32   PrintModulePass(OStream *o, bool DS = false)
33     : Out(o), DeleteStream(DS) {}
34
35   ~PrintModulePass() {
36     if (DeleteStream) delete Out;
37   }
38
39   bool runOnModule(Module &M) {
40     (*Out) << M << std::flush;
41     return false;
42   }
43
44   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45     AU.setPreservesAll();
46   }
47 };
48
49 class PrintFunctionPass : public FunctionPass {
50   std::string Banner;     // String to print before each function
51   OStream *Out;           // ostream to print on
52   bool DeleteStream;      // Delete the ostream in our dtor?
53 public:
54   PrintFunctionPass() : Banner(""), Out(&cerr), DeleteStream(false) {}
55   PrintFunctionPass(const std::string &B, OStream *o = &cout,
56                     bool DS = false)
57     : 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 function as
64   // 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 } // End llvm namespace
77
78 #endif