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/Debug.h"
20#include "llvm/Support/raw_ostream.h"
21using namespace llvm;
22
23namespace {
24
25  class PrintModulePass : public ModulePass {
26    std::string Banner;
27    raw_ostream *Out;       // raw_ostream to print on
28    bool DeleteStream;      // Delete the ostream in our dtor?
29  public:
30    static char ID;
31    PrintModulePass() : ModulePass(ID), Out(&dbgs()),
32      DeleteStream(false) {}
33    PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
34        : ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {}
35
36    ~PrintModulePass() {
37      if (DeleteStream) delete Out;
38    }
39
40    bool runOnModule(Module &M) {
41      (*Out) << Banner << M;
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
52    raw_ostream *Out;       // raw_ostream to print on
53    bool DeleteStream;      // Delete the ostream in our dtor?
54  public:
55    static char ID;
56    PrintFunctionPass() : FunctionPass(ID), Banner(""), Out(&dbgs()),
57                          DeleteStream(false) {}
58    PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
59      : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
60
61    ~PrintFunctionPass() {
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
79char PrintModulePass::ID = 0;
80INITIALIZE_PASS(PrintModulePass, "print-module",
81                "Print module to stderr", false, false)
82char PrintFunctionPass::ID = 0;
83INITIALIZE_PASS(PrintFunctionPass, "print-function",
84                "Print function to stderr", false, false)
85
86/// createPrintModulePass - Create and return a pass that writes the
87/// module to the specified raw_ostream.
88ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
89                                        bool DeleteStream,
90                                        const std::string &Banner) {
91  return new PrintModulePass(Banner, OS, DeleteStream);
92}
93
94/// createPrintFunctionPass - Create and return a pass that prints
95/// functions to the specified raw_ostream as they are processed.
96FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
97                                            llvm::raw_ostream *OS,
98                                            bool DeleteStream) {
99  return new PrintFunctionPass(Banner, OS, DeleteStream);
100}
101
102