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.

114 lines
3.5 KiB

  1. //===-- CFGPrinter.h - CFG printer external interface -----------*- 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. // This file defines external functions that can be called to explicitly
  11. // instantiate the CFG printer.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ANALYSIS_CFGPRINTER_H
  15. #define LLVM_ANALYSIS_CFGPRINTER_H
  16. #include "llvm/Assembly/Writer.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/Support/CFG.h"
  21. #include "llvm/Support/GraphWriter.h"
  22. namespace llvm {
  23. template<>
  24. struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
  25. DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
  26. static std::string getGraphName(const Function *F) {
  27. return "CFG for '" + F->getName().str() + "' function";
  28. }
  29. static std::string getSimpleNodeLabel(const BasicBlock *Node,
  30. const Function *) {
  31. if (!Node->getName().empty())
  32. return Node->getName().str();
  33. std::string Str;
  34. raw_string_ostream OS(Str);
  35. WriteAsOperand(OS, Node, false);
  36. return OS.str();
  37. }
  38. static std::string getCompleteNodeLabel(const BasicBlock *Node,
  39. const Function *) {
  40. std::string Str;
  41. raw_string_ostream OS(Str);
  42. if (Node->getName().empty()) {
  43. WriteAsOperand(OS, Node, false);
  44. OS << ":";
  45. }
  46. OS << *Node;
  47. std::string OutStr = OS.str();
  48. if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
  49. // Process string output to make it nicer...
  50. for (unsigned i = 0; i != OutStr.length(); ++i)
  51. if (OutStr[i] == '\n') { // Left justify
  52. OutStr[i] = '\\';
  53. OutStr.insert(OutStr.begin()+i+1, 'l');
  54. } else if (OutStr[i] == ';') { // Delete comments!
  55. unsigned Idx = OutStr.find('\n', i+1); // Find end of line
  56. OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
  57. --i;
  58. }
  59. return OutStr;
  60. }
  61. std::string getNodeLabel(const BasicBlock *Node,
  62. const Function *Graph) {
  63. if (isSimple())
  64. return getSimpleNodeLabel(Node, Graph);
  65. else
  66. return getCompleteNodeLabel(Node, Graph);
  67. }
  68. static std::string getEdgeSourceLabel(const BasicBlock *Node,
  69. succ_const_iterator I) {
  70. // Label source of conditional branches with "T" or "F"
  71. if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
  72. if (BI->isConditional())
  73. return (I == succ_begin(Node)) ? "T" : "F";
  74. // Label source of switch edges with the associated value.
  75. if (const SwitchInst *SI = dyn_cast<SwitchInst>(Node->getTerminator())) {
  76. unsigned SuccNo = I.getSuccessorIndex();
  77. if (SuccNo == 0) return "def";
  78. std::string Str;
  79. raw_string_ostream OS(Str);
  80. SwitchInst::ConstCaseIt Case =
  81. SwitchInst::ConstCaseIt::fromSuccessorIndex(SI, SuccNo);
  82. OS << Case.getCaseValue()->getValue();
  83. return OS.str();
  84. }
  85. return "";
  86. }
  87. };
  88. } // End llvm namespace
  89. namespace llvm {
  90. class FunctionPass;
  91. FunctionPass *createCFGPrinterPass ();
  92. FunctionPass *createCFGOnlyPrinterPass ();
  93. } // End llvm namespace
  94. #endif