1 //===--- VMCore/PrintModulePass.cpp - Module/Function Printer -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // PrintModulePass and PrintFunctionPass implementations.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Assembly/PrintModulePass.h"
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"
25 class PrintModulePass
: public ModulePass
{
27 raw_ostream
*Out
; // raw_ostream to print on
28 bool DeleteStream
; // Delete the ostream in our dtor?
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
) {}
37 if (DeleteStream
) delete Out
;
40 bool runOnModule(Module
&M
) {
41 (*Out
) << Banner
<< M
;
45 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
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?
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
) {}
61 ~PrintFunctionPass() {
62 if (DeleteStream
) delete Out
;
65 // runOnFunction - This pass just prints a banner followed by the
66 // function as it's processed.
68 bool runOnFunction(Function
&F
) {
69 (*Out
) << Banner
<< static_cast<Value
&>(F
);
73 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
79 char PrintModulePass::ID
= 0;
80 INITIALIZE_PASS(PrintModulePass
, "print-module",
81 "Print module to stderr", false, false)
82 char PrintFunctionPass::ID
= 0;
83 INITIALIZE_PASS(PrintFunctionPass
, "print-function",
84 "Print function to stderr", false, false)
86 /// createPrintModulePass - Create and return a pass that writes the
87 /// module to the specified raw_ostream.
88 ModulePass
*llvm::createPrintModulePass(llvm::raw_ostream
*OS
,
90 const std::string
&Banner
) {
91 return new PrintModulePass(Banner
, OS
, DeleteStream
);
94 /// createPrintFunctionPass - Create and return a pass that prints
95 /// functions to the specified raw_ostream as they are processed.
96 FunctionPass
*llvm::createPrintFunctionPass(const std::string
&Banner
,
97 llvm::raw_ostream
*OS
,
99 return new PrintFunctionPass(Banner
, OS
, DeleteStream
);