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/raw_ostream.h"
24 class PrintModulePass
: public ModulePass
{
25 raw_ostream
*Out
; // raw_ostream to print on
26 bool DeleteStream
; // Delete the ostream in our dtor?
29 PrintModulePass() : ModulePass(&ID
), Out(&errs()),
30 DeleteStream(false) {}
31 PrintModulePass(raw_ostream
*o
, bool DS
)
32 : ModulePass(&ID
), Out(o
), DeleteStream(DS
) {}
35 if (DeleteStream
) delete Out
;
38 bool runOnModule(Module
&M
) {
43 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
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?
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
) {}
59 inline ~PrintFunctionPass() {
60 if (DeleteStream
) delete Out
;
63 // runOnFunction - This pass just prints a banner followed by the
64 // function as it's processed.
66 bool runOnFunction(Function
&F
) {
67 (*Out
) << Banner
<< static_cast<Value
&>(F
);
71 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
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");
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
,
88 return new PrintModulePass(OS
, DeleteStream
);
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
,
96 return new PrintFunctionPass(Banner
, OS
, DeleteStream
);