blob: e4e9939f083dc378a0d770900139713e33c6d9d8 [file] [log] [blame]
Chandler Carruthc2c50cd2013-01-02 09:10:48 +00001//===--- IR/PrintModulePass.cpp - Module/Function Printer -----------------===//
Daniel Dunbarf4db3a52008-10-21 23:33:38 +00002//
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"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000015#include "llvm/IR/Function.h"
16#include "llvm/IR/Module.h"
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000017#include "llvm/Pass.h"
David Greene9a6c9402010-01-05 01:30:18 +000018#include "llvm/Support/Debug.h"
Daniel Dunbar3b0da262008-10-22 03:25:22 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000020using namespace llvm;
21
22namespace {
23
Nick Lewycky6726b6d2009-10-25 06:33:48 +000024 class PrintModulePass : public ModulePass {
David Greene5c8aa952010-04-02 23:17:14 +000025 std::string Banner;
Daniel Dunbar3b0da262008-10-22 03:25:22 +000026 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000027 bool DeleteStream; // Delete the ostream in our dtor?
28 public:
29 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000030 PrintModulePass() : ModulePass(ID), Out(&dbgs()),
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000031 DeleteStream(false) {}
David Greene5c8aa952010-04-02 23:17:14 +000032 PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
Owen Anderson90c579d2010-08-06 18:33:48 +000033 : ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000034
35 ~PrintModulePass() {
36 if (DeleteStream) delete Out;
37 }
38
39 bool runOnModule(Module &M) {
David Greene5c8aa952010-04-02 23:17:14 +000040 (*Out) << Banner << M;
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000041 return false;
42 }
43
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
46 }
47 };
48
49 class PrintFunctionPass : public FunctionPass {
50 std::string Banner; // String to print before each function
Daniel Dunbar3b0da262008-10-22 03:25:22 +000051 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000052 bool DeleteStream; // Delete the ostream in our dtor?
53 public:
54 static char ID;
Owen Anderson90c579d2010-08-06 18:33:48 +000055 PrintFunctionPass() : FunctionPass(ID), Banner(""), Out(&dbgs()),
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000056 DeleteStream(false) {}
Daniel Dunbar3b0da262008-10-22 03:25:22 +000057 PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
Owen Anderson90c579d2010-08-06 18:33:48 +000058 : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000059
Dan Gohmanb85b31b2010-08-16 21:57:30 +000060 ~PrintFunctionPass() {
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000061 if (DeleteStream) delete Out;
62 }
63
64 // runOnFunction - This pass just prints a banner followed by the
65 // function as it's processed.
66 //
67 bool runOnFunction(Function &F) {
68 (*Out) << Banner << static_cast<Value&>(F);
69 return false;
70 }
71
72 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.setPreservesAll();
74 }
75 };
76}
77
78char PrintModulePass::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000079INITIALIZE_PASS(PrintModulePass, "print-module",
Owen Andersonce665bd2010-10-07 22:25:06 +000080 "Print module to stderr", false, false)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000081char PrintFunctionPass::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000082INITIALIZE_PASS(PrintFunctionPass, "print-function",
Owen Andersonce665bd2010-10-07 22:25:06 +000083 "Print function to stderr", false, false)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000084
85/// createPrintModulePass - Create and return a pass that writes the
Daniel Dunbar3b0da262008-10-22 03:25:22 +000086/// module to the specified raw_ostream.
87ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
David Greene5c8aa952010-04-02 23:17:14 +000088 bool DeleteStream,
89 const std::string &Banner) {
90 return new PrintModulePass(Banner, OS, DeleteStream);
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000091}
92
93/// createPrintFunctionPass - Create and return a pass that prints
Daniel Dunbar3b0da262008-10-22 03:25:22 +000094/// functions to the specified raw_ostream as they are processed.
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000095FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
Daniel Dunbar3b0da262008-10-22 03:25:22 +000096 llvm::raw_ostream *OS,
97 bool DeleteStream) {
Daniel Dunbarf4db3a52008-10-21 23:33:38 +000098 return new PrintFunctionPass(Banner, OS, DeleteStream);
99}
100