LLVM API Documentation

PrintModulePass.h

Go to the documentation of this file.
00001 //===- llvm/Assembly/PrintModulePass.h - Printing Pass ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines two passes to print out a module.  The PrintModulePass pass
00011 // simply prints out the entire module when it is executed.  The
00012 // PrintFunctionPass class is designed to be pipelined with other
00013 // FunctionPass's, and prints out the functions of the class as they are
00014 // processed.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #ifndef LLVM_ASSEMBLY_PRINTMODULEPASS_H
00019 #define LLVM_ASSEMBLY_PRINTMODULEPASS_H
00020 
00021 #include "llvm/Pass.h"
00022 #include "llvm/Module.h"
00023 #include <iostream>
00024 
00025 namespace llvm {
00026 
00027 class PrintModulePass : public ModulePass {
00028   std::ostream *Out;      // ostream to print on
00029   bool DeleteStream;      // Delete the ostream in our dtor?
00030 public:
00031   PrintModulePass() : Out(&std::cerr), DeleteStream(false) {}
00032   PrintModulePass(std::ostream *o, bool DS = false)
00033     : Out(o), DeleteStream(DS) {
00034   }
00035 
00036   ~PrintModulePass() {
00037     if (DeleteStream) delete Out;
00038   }
00039 
00040   bool runOnModule(Module &M) {
00041     (*Out) << M << std::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   std::ostream *Out;      // ostream to print on
00053   bool DeleteStream;      // Delete the ostream in our dtor?
00054 public:
00055   PrintFunctionPass() : Banner(""), Out(&std::cerr), DeleteStream(false) {}
00056   PrintFunctionPass(const std::string &B, std::ostream *o = &std::cout,
00057                     bool DS = false)
00058     : Banner(B), Out(o), DeleteStream(DS) {
00059   }
00060 
00061   inline ~PrintFunctionPass() {
00062     if (DeleteStream) delete Out;
00063   }
00064 
00065   // runOnFunction - This pass just prints a banner followed by the function as
00066   // it's processed.
00067   //
00068   bool runOnFunction(Function &F) {
00069     (*Out) << Banner << static_cast<Value&>(F);
00070     return false;
00071   }
00072 
00073   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00074     AU.setPreservesAll();
00075   }
00076 };
00077 
00078 } // End llvm namespace
00079 
00080 #endif