PrintModulePass.cpp 3.3 KB

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