LLVM API Documentation

PrintModulePass.cpp

Go to the documentation of this file.
00001 //===--- VMCore/PrintModulePass.cpp - Module/Function Printer -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // PrintModulePass and PrintFunctionPass implementations.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Assembly/PrintModulePass.h"
00015 
00016 #include "llvm/Function.h"
00017 #include "llvm/Module.h"
00018 #include "llvm/Pass.h"
00019 #include "llvm/Support/Compiler.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 using namespace llvm;
00022 
00023 namespace {
00024 
00025   class VISIBILITY_HIDDEN PrintModulePass : public ModulePass {
00026     raw_ostream *Out;       // raw_ostream to print on
00027     bool DeleteStream;      // Delete the ostream in our dtor?
00028   public:
00029     static char ID;
00030     PrintModulePass() : ModulePass(&ID), Out(&errs()), 
00031       DeleteStream(false) {}
00032     PrintModulePass(raw_ostream *o, bool DS)
00033       : ModulePass(&ID), Out(o), DeleteStream(DS) {}
00034     
00035     ~PrintModulePass() {
00036       if (DeleteStream) delete Out;
00037     }
00038     
00039     bool runOnModule(Module &M) {
00040       (*Out) << M;
00041       return false;
00042     }
00043     
00044     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00045       AU.setPreservesAll();
00046     }
00047   };
00048   
00049   class PrintFunctionPass : public FunctionPass {
00050     std::string Banner;     // String to print before each function
00051     raw_ostream *Out;       // raw_ostream to print on
00052     bool DeleteStream;      // Delete the ostream in our dtor?
00053   public:
00054     static char ID;
00055     PrintFunctionPass() : FunctionPass(&ID), Banner(""), Out(&errs()), 
00056                           DeleteStream(false) {}
00057     PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
00058       : FunctionPass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
00059     
00060     inline ~PrintFunctionPass() {
00061       if (DeleteStream) delete Out;
00062     }
00063     
00064     // runOnFunction - This pass just prints a banner followed by the
00065     // function as it's processed.
00066     //
00067     bool runOnFunction(Function &F) {
00068       (*Out) << Banner << static_cast<Value&>(F);
00069       return false;
00070     }
00071     
00072     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00073       AU.setPreservesAll();
00074     }
00075   };
00076 }
00077 
00078 char PrintModulePass::ID = 0;
00079 static RegisterPass<PrintModulePass>
00080 X("print-module", "Print module to stderr");
00081 char PrintFunctionPass::ID = 0;
00082 static RegisterPass<PrintFunctionPass>
00083 Y("print-function","Print function to stderr");
00084 
00085 /// createPrintModulePass - Create and return a pass that writes the
00086 /// module to the specified raw_ostream.
00087 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS, 
00088                                         bool DeleteStream) {
00089   return new PrintModulePass(OS, DeleteStream);
00090 }
00091 
00092 /// createPrintFunctionPass - Create and return a pass that prints
00093 /// functions to the specified raw_ostream as they are processed.
00094 FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
00095                                             llvm::raw_ostream *OS, 
00096                                             bool DeleteStream) {
00097   return new PrintFunctionPass(Banner, OS, DeleteStream);
00098 }
00099