Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
3.7 KiB

  1. //===-- DOTGraphTraitsPass.h - Print/View dotty graphs-----------*- C++ -*-===//
  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. // Templates to create dotty viewer and printer passes for GraphTraits graphs.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
  14. #define LLVM_ANALYSIS_DOTGRAPHTRAITSPASS_H
  15. #include "llvm/Analysis/CFGPrinter.h"
  16. #include "llvm/Pass.h"
  17. namespace llvm {
  18. template <class Analysis, bool Simple>
  19. class DOTGraphTraitsViewer : public FunctionPass {
  20. public:
  21. DOTGraphTraitsViewer(StringRef GraphName, char &ID)
  22. : FunctionPass(ID), Name(GraphName) {}
  23. virtual bool runOnFunction(Function &F) {
  24. Analysis *Graph = &getAnalysis<Analysis>();
  25. std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
  26. std::string Title = GraphName + " for '" + F.getName().str() + "' function";
  27. ViewGraph(Graph, Name, Simple, Title);
  28. return false;
  29. }
  30. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  31. AU.setPreservesAll();
  32. AU.addRequired<Analysis>();
  33. }
  34. private:
  35. std::string Name;
  36. };
  37. template <class Analysis, bool Simple>
  38. class DOTGraphTraitsPrinter : public FunctionPass {
  39. public:
  40. DOTGraphTraitsPrinter(StringRef GraphName, char &ID)
  41. : FunctionPass(ID), Name(GraphName) {}
  42. virtual bool runOnFunction(Function &F) {
  43. Analysis *Graph = &getAnalysis<Analysis>();
  44. std::string Filename = Name + "." + F.getName().str() + ".dot";
  45. std::string ErrorInfo;
  46. errs() << "Writing '" << Filename << "'...";
  47. raw_fd_ostream File(Filename.c_str(), ErrorInfo);
  48. std::string GraphName = DOTGraphTraits<Analysis*>::getGraphName(Graph);
  49. std::string Title = GraphName + " for '" + F.getName().str() + "' function";
  50. if (ErrorInfo.empty())
  51. WriteGraph(File, Graph, Simple, Title);
  52. else
  53. errs() << " error opening file for writing!";
  54. errs() << "\n";
  55. return false;
  56. }
  57. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  58. AU.setPreservesAll();
  59. AU.addRequired<Analysis>();
  60. }
  61. private:
  62. std::string Name;
  63. };
  64. template <class Analysis, bool Simple>
  65. class DOTGraphTraitsModuleViewer : public ModulePass {
  66. public:
  67. DOTGraphTraitsModuleViewer(StringRef GraphName, char &ID)
  68. : ModulePass(ID), Name(GraphName) {}
  69. virtual bool runOnModule(Module &M) {
  70. Analysis *Graph = &getAnalysis<Analysis>();
  71. std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
  72. ViewGraph(Graph, Name, Simple, Title);
  73. return false;
  74. }
  75. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  76. AU.setPreservesAll();
  77. AU.addRequired<Analysis>();
  78. }
  79. private:
  80. std::string Name;
  81. };
  82. template <class Analysis, bool Simple>
  83. class DOTGraphTraitsModulePrinter : public ModulePass {
  84. public:
  85. DOTGraphTraitsModulePrinter(StringRef GraphName, char &ID)
  86. : ModulePass(ID), Name(GraphName) {}
  87. virtual bool runOnModule(Module &M) {
  88. Analysis *Graph = &getAnalysis<Analysis>();
  89. std::string Filename = Name + ".dot";
  90. std::string ErrorInfo;
  91. errs() << "Writing '" << Filename << "'...";
  92. raw_fd_ostream File(Filename.c_str(), ErrorInfo);
  93. std::string Title = DOTGraphTraits<Analysis*>::getGraphName(Graph);
  94. if (ErrorInfo.empty())
  95. WriteGraph(File, Graph, Simple, Title);
  96. else
  97. errs() << " error opening file for writing!";
  98. errs() << "\n";
  99. return false;
  100. }
  101. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  102. AU.setPreservesAll();
  103. AU.addRequired<Analysis>();
  104. }
  105. private:
  106. std::string Name;
  107. };
  108. } // end namespace llvm
  109. #endif