blob: 2d69dce07f3f7eda96b5c971a8c81eef7660a316 [file] [log] [blame]
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +00001//===--- 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"
David Greene5f2433e2010-01-05 01:30:18 +000019#include "llvm/Support/Debug.h"
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000020#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000021using namespace llvm;
22
23namespace {
24
Nick Lewycky02d5f772009-10-25 06:33:48 +000025 class PrintModulePass : public ModulePass {
David Greene8f32cb92010-04-02 18:46:26 +000026 std::string Banner;
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000027 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000028 bool DeleteStream; // Delete the ostream in our dtor?
29 public:
30 static char ID;
David Greene5f2433e2010-01-05 01:30:18 +000031 PrintModulePass() : ModulePass(&ID), Out(&dbgs()),
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000032 DeleteStream(false) {}
David Greene8f32cb92010-04-02 18:46:26 +000033 PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
34 : ModulePass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000035
36 ~PrintModulePass() {
37 if (DeleteStream) delete Out;
38 }
39
40 bool runOnModule(Module &M) {
David Greene8f32cb92010-04-02 18:46:26 +000041 (*Out) << Banner << M;
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000042 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
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000052 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000053 bool DeleteStream; // Delete the ostream in our dtor?
54 public:
55 static char ID;
David Greene5f2433e2010-01-05 01:30:18 +000056 PrintFunctionPass() : FunctionPass(&ID), Banner(""), Out(&dbgs()),
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000057 DeleteStream(false) {}
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000058 PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
Dan Gohman38a96312009-02-18 05:09:16 +000059 : FunctionPass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000060
61 inline ~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;
80static RegisterPass<PrintModulePass>
81X("print-module", "Print module to stderr");
82char PrintFunctionPass::ID = 0;
83static RegisterPass<PrintFunctionPass>
84Y("print-function","Print function to stderr");
85
86/// createPrintModulePass - Create and return a pass that writes the
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000087/// module to the specified raw_ostream.
88ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
David Greene8f32cb92010-04-02 18:46:26 +000089 bool DeleteStream,
90 const std::string &Banner) {
91 return new PrintModulePass(Banner, OS, DeleteStream);
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000092}
93
94/// createPrintFunctionPass - Create and return a pass that prints
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000095/// functions to the specified raw_ostream as they are processed.
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000096FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000097 llvm::raw_ostream *OS,
98 bool DeleteStream) {
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000099 return new PrintFunctionPass(Banner, OS, DeleteStream);
100}
101