PrintModulePass.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===--- 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. #include "llvm/Assembly/PrintModulePass.h"
  14. #include "llvm/Function.h"
  15. #include "llvm/Module.h"
  16. #include "llvm/Pass.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. using namespace llvm;
  19. namespace {
  20. class PrintModulePass : public ModulePass {
  21. raw_ostream *Out; // raw_ostream to print on
  22. bool DeleteStream; // Delete the ostream in our dtor?
  23. public:
  24. static char ID;
  25. PrintModulePass() : ModulePass(&ID), Out(&errs()),
  26. DeleteStream(false) {}
  27. PrintModulePass(raw_ostream *o, bool DS)
  28. : ModulePass(&ID), Out(o), DeleteStream(DS) {}
  29. ~PrintModulePass() {
  30. if (DeleteStream) delete Out;
  31. }
  32. bool runOnModule(Module &M) {
  33. (*Out) << M;
  34. return false;
  35. }
  36. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  37. AU.setPreservesAll();
  38. }
  39. };
  40. class PrintFunctionPass : public FunctionPass {
  41. std::string Banner; // String to print before each function
  42. raw_ostream *Out; // raw_ostream to print on
  43. bool DeleteStream; // Delete the ostream in our dtor?
  44. public:
  45. static char ID;
  46. PrintFunctionPass() : FunctionPass(&ID), Banner(""), Out(&errs()),
  47. DeleteStream(false) {}
  48. PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
  49. : FunctionPass(&ID), Banner(B), Out(o), DeleteStream(DS) {}
  50. inline ~PrintFunctionPass() {
  51. if (DeleteStream) delete Out;
  52. }
  53. // runOnFunction - This pass just prints a banner followed by the
  54. // function as it's processed.
  55. //
  56. bool runOnFunction(Function &F) {
  57. (*Out) << Banner << static_cast<Value&>(F);
  58. return false;
  59. }
  60. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  61. AU.setPreservesAll();
  62. }
  63. };
  64. }
  65. char PrintModulePass::ID = 0;
  66. static RegisterPass<PrintModulePass>
  67. X("print-module", "Print module to stderr");
  68. char PrintFunctionPass::ID = 0;
  69. static RegisterPass<PrintFunctionPass>
  70. Y("print-function","Print function to stderr");
  71. /// createPrintModulePass - Create and return a pass that writes the
  72. /// module to the specified raw_ostream.
  73. ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS,
  74. bool DeleteStream) {
  75. return new PrintModulePass(OS, DeleteStream);
  76. }
  77. /// createPrintFunctionPass - Create and return a pass that prints
  78. /// functions to the specified raw_ostream as they are processed.
  79. FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner,
  80. llvm::raw_ostream *OS,
  81. bool DeleteStream) {
  82. return new PrintFunctionPass(Banner, OS, DeleteStream);
  83. }