blob: 2ee49d235963c26eb9575a28ecf6c77da2aefbfa [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 Greene9b063df2010-04-02 23:17:14 +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;
Owen Andersona7aed182010-08-06 18:33:48 +000031 PrintModulePass() : ModulePass(ID), Out(&dbgs()),
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000032 DeleteStream(false) {}
David Greene9b063df2010-04-02 23:17:14 +000033 PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
Owen Andersona7aed182010-08-06 18:33:48 +000034 : 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 Greene9b063df2010-04-02 23:17:14 +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;
Owen Andersona7aed182010-08-06 18:33:48 +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)
Owen Andersona7aed182010-08-06 18:33:48 +000059 : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000060
Dan Gohman30614852010-08-16 21:57:30 +000061 ~PrintFunctionPass() {
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000062 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;
Owen Andersona57b97e2010-07-21 22:09:45 +000080INITIALIZE_PASS(PrintModulePass, "print-module",
81 "Print module to stderr", false, false);
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000082char PrintFunctionPass::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000083INITIALIZE_PASS(PrintFunctionPass, "print-function",
84 "Print function to stderr", false, false);
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000085
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 Greene9b063df2010-04-02 23:17:14 +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