blob: 3d4f19df05d8a5d903bb56a17c8b4cd20939e99c [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"
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000020using namespace llvm;
21
22namespace {
23
Nick Lewycky02d5f772009-10-25 06:33:48 +000024 class PrintModulePass : public ModulePass {
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000025 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000026 bool DeleteStream; // Delete the ostream in our dtor?
27 public:
28 static char ID;
Dan Gohman38a96312009-02-18 05:09:16 +000029 PrintModulePass() : ModulePass(&ID), Out(&errs()),
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000030 DeleteStream(false) {}
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000031 PrintModulePass(raw_ostream *o, bool DS)
Dan Gohman38a96312009-02-18 05:09:16 +000032 : ModulePass(&ID), Out(o), DeleteStream(DS) {}
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000033
34 ~PrintModulePass() {
35 if (DeleteStream) delete Out;
36 }
37
38 bool runOnModule(Module &M) {
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000039 (*Out) << M;
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000040 return false;
41 }
42
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.setPreservesAll();
45 }
46 };
47
48 class PrintFunctionPass : public FunctionPass {
49 std::string Banner; // String to print before each function
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000050 raw_ostream *Out; // raw_ostream to print on
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000051 bool DeleteStream; // Delete the ostream in our dtor?
52 public:
53 static char ID;
Dan Gohman38a96312009-02-18 05:09:16 +000054 PrintFunctionPass() : FunctionPass(&ID), Banner(""), Out(&errs()),
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000055 DeleteStream(false) {}
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000056 PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
Dan Gohman38a96312009-02-18 05:09:16 +000057 : FunctionPass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000058
59 inline ~PrintFunctionPass() {
60 if (DeleteStream) delete Out;
61 }
62
63 // runOnFunction - This pass just prints a banner followed by the
64 // function as it's processed.
65 //
66 bool runOnFunction(Function &F) {
67 (*Out) << Banner << static_cast<Value&>(F);
68 return false;
69 }
70
71 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.setPreservesAll();
73 }
74 };
75}
76
77char PrintModulePass::ID = 0;
78static RegisterPass<PrintModulePass>
79X("print-module", "Print module to stderr");
80char PrintFunctionPass::ID = 0;
81static RegisterPass<PrintFunctionPass>
82Y("print-function","Print function to stderr");
83
84/// createPrintModulePass - Create and return a pass that writes the
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000085/// module to the specified raw_ostream.
86ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
87 bool DeleteStream) {
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000088 return new PrintModulePass(OS, DeleteStream);
89}
90
91/// createPrintFunctionPass - Create and return a pass that prints
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000092/// functions to the specified raw_ostream as they are processed.
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000093FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
Daniel Dunbar81b5fa52008-10-22 03:25:22 +000094 llvm::raw_ostream *OS,
95 bool DeleteStream) {
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +000096 return new PrintFunctionPass(Banner, OS, DeleteStream);
97}
98