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(intptr_t(&ID)), Out(&errs()), 
00031       DeleteStream(false) {}
00032     PrintModulePass(raw_ostream *o, bool DS)
00033       : ModulePass(intptr_t(&ID)), Out(o), DeleteStream(DS) {}
00034     
00035     ~PrintModulePass() {
00036       if (DeleteStream) delete Out;
00037     }
00038     
00039     bool runOnModule(Module &M) {
00040       (*Out) << M;
00041       Out->flush();
00042       return false;
00043     }
00044     
00045     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00046       AU.setPreservesAll();
00047     }
00048   };
00049   
00050   class PrintFunctionPass : public FunctionPass {
00051     std::string Banner;     // String to print before each function
00052     raw_ostream *Out;       // raw_ostream to print on
00053     bool DeleteStream;      // Delete the ostream in our dtor?
00054   public:
00055     static char ID;
00056     PrintFunctionPass() : FunctionPass(intptr_t(&ID)), Banner(""), Out(&errs()), 
00057                           DeleteStream(false) {}
00058     PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
00059       : FunctionPass(intptr_t(&ID)), Banner(B), Out(o), DeleteStream(DS) {}
00060     
00061     inline ~PrintFunctionPass() {
00062       if (DeleteStream) delete Out;
00063     }
00064     
00065     // runOnFunction - This pass just prints a banner followed by the
00066     // function as it's processed.
00067     //
00068     bool runOnFunction(Function &F) {
00069       (*Out) << Banner << static_cast<Value&>(F);
00070       Out->flush();
00071       return false;
00072     }
00073     
00074     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00075       AU.setPreservesAll();
00076     }
00077   };
00078 }
00079 
00080 char PrintModulePass::ID = 0;
00081 static RegisterPass<PrintModulePass>
00082 X("print-module", "Print module to stderr");
00083 char PrintFunctionPass::ID = 0;
00084 static RegisterPass<PrintFunctionPass>
00085 Y("print-function","Print function to stderr");
00086 
00087 /// createPrintModulePass - Create and return a pass that writes the
00088 /// module to the specified raw_ostream.
00089 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS, 
00090                                         bool DeleteStream) {
00091   return new PrintModulePass(OS, DeleteStream);
00092 }
00093 
00094 /// createPrintFunctionPass - Create and return a pass that prints
00095 /// functions to the specified raw_ostream as they are processed.
00096 FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
00097                                             llvm::raw_ostream *OS, 
00098                                             bool DeleteStream) {
00099   return new PrintFunctionPass(Banner, OS, DeleteStream);
00100 }
00101 



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.