switch to TrackingVH instead of WeakVH, since these can never
[oota-llvm.git] / lib / VMCore / PrintModulePass.cpp
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/raw_ostream.h"
20 using namespace llvm;
21
22 namespace {
23
24   class PrintModulePass : public ModulePass {
25     raw_ostream *Out;       // raw_ostream to print on
26     bool DeleteStream;      // Delete the ostream in our dtor?
27   public:
28     static char ID;
29     PrintModulePass() : ModulePass(&ID), Out(&errs()), 
30       DeleteStream(false) {}
31     PrintModulePass(raw_ostream *o, bool DS)
32       : ModulePass(&ID), Out(o), DeleteStream(DS) {}
33     
34     ~PrintModulePass() {
35       if (DeleteStream) delete Out;
36     }
37     
38     bool runOnModule(Module &M) {
39       (*Out) << M;
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     raw_ostream *Out;       // raw_ostream to print on
51     bool DeleteStream;      // Delete the ostream in our dtor?
52   public:
53     static char ID;
54     PrintFunctionPass() : FunctionPass(&ID), Banner(""), Out(&errs()), 
55                           DeleteStream(false) {}
56     PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
57       : FunctionPass(&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 raw_ostream.
86 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS, 
87                                         bool DeleteStream) {
88   return new PrintModulePass(OS, DeleteStream);
89 }
90
91 /// createPrintFunctionPass - Create and return a pass that prints
92 /// functions to the specified raw_ostream as they are processed.
93 FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
94                                             llvm::raw_ostream *OS, 
95                                             bool DeleteStream) {
96   return new PrintFunctionPass(Banner, OS, DeleteStream);
97 }
98