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.

369 lines
11 KiB

  1. //===-- llvm/Support/GraphWriter.h - Write graph to a .dot file -*- 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 a simple interface that can be used to print out generic
  11. // LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T
  12. // graphviz package (http://www.research.att.com/sw/tools/graphviz/) which can
  13. // be used to turn the files output by this interface into a variety of
  14. // different graphics formats.
  15. //
  16. // Graphs do not need to implement any interface past what is already required
  17. // by the GraphTraits template, but they can choose to implement specializations
  18. // of the DOTGraphTraits template if they want to customize the graphs output in
  19. // any way.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_SUPPORT_GRAPHWRITER_H
  23. #define LLVM_SUPPORT_GRAPHWRITER_H
  24. #include "llvm/ADT/GraphTraits.h"
  25. #include "llvm/Support/DOTGraphTraits.h"
  26. #include "llvm/Support/Path.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <cassert>
  29. #include <vector>
  30. namespace llvm {
  31. namespace DOT { // Private functions...
  32. std::string EscapeString(const std::string &Label);
  33. /// \brief Get a color string for this node number. Simply round-robin selects
  34. /// from a reasonable number of colors.
  35. StringRef getColorString(unsigned NodeNumber);
  36. }
  37. namespace GraphProgram {
  38. enum Name {
  39. DOT,
  40. FDP,
  41. NEATO,
  42. TWOPI,
  43. CIRCO
  44. };
  45. }
  46. void DisplayGraph(const sys::Path& Filename, bool wait=true, GraphProgram::Name program = GraphProgram::DOT);
  47. template<typename GraphType>
  48. class GraphWriter {
  49. raw_ostream &O;
  50. const GraphType &G;
  51. typedef DOTGraphTraits<GraphType> DOTTraits;
  52. typedef GraphTraits<GraphType> GTraits;
  53. typedef typename GTraits::NodeType NodeType;
  54. typedef typename GTraits::nodes_iterator node_iterator;
  55. typedef typename GTraits::ChildIteratorType child_iterator;
  56. DOTTraits DTraits;
  57. // Writes the edge labels of the node to O and returns true if there are any
  58. // edge labels not equal to the empty string "".
  59. bool getEdgeSourceLabels(raw_ostream &O, NodeType *Node) {
  60. child_iterator EI = GTraits::child_begin(Node);
  61. child_iterator EE = GTraits::child_end(Node);
  62. bool hasEdgeSourceLabels = false;
  63. for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
  64. std::string label = DTraits.getEdgeSourceLabel(Node, EI);
  65. if (label.empty())
  66. continue;
  67. hasEdgeSourceLabels = true;
  68. if (i)
  69. O << "|";
  70. O << "<s" << i << ">" << DOT::EscapeString(label);
  71. }
  72. if (EI != EE && hasEdgeSourceLabels)
  73. O << "|<s64>truncated...";
  74. return hasEdgeSourceLabels;
  75. }
  76. public:
  77. GraphWriter(raw_ostream &o, const GraphType &g, bool SN) : O(o), G(g) {
  78. DTraits = DOTTraits(SN);
  79. }
  80. void writeGraph(const std::string &Title = "") {
  81. // Output the header for the graph...
  82. writeHeader(Title);
  83. // Emit all of the nodes in the graph...
  84. writeNodes();
  85. // Output any customizations on the graph
  86. DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, *this);
  87. // Output the end of the graph
  88. writeFooter();
  89. }
  90. void writeHeader(const std::string &Title) {
  91. std::string GraphName = DTraits.getGraphName(G);
  92. if (!Title.empty())
  93. O << "digraph \"" << DOT::EscapeString(Title) << "\" {\n";
  94. else if (!GraphName.empty())
  95. O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
  96. else
  97. O << "digraph unnamed {\n";
  98. if (DTraits.renderGraphFromBottomUp())
  99. O << "\trankdir=\"BT\";\n";
  100. if (!Title.empty())
  101. O << "\tlabel=\"" << DOT::EscapeString(Title) << "\";\n";
  102. else if (!GraphName.empty())
  103. O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
  104. O << DTraits.getGraphProperties(G);
  105. O << "\n";
  106. }
  107. void writeFooter() {
  108. // Finish off the graph
  109. O << "}\n";
  110. }
  111. void writeNodes() {
  112. // Loop over the graph, printing it out...
  113. for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
  114. I != E; ++I)
  115. if (!isNodeHidden(*I))
  116. writeNode(*I);
  117. }
  118. bool isNodeHidden(NodeType &Node) {
  119. return isNodeHidden(&Node);
  120. }
  121. bool isNodeHidden(NodeType *const *Node) {
  122. return isNodeHidden(*Node);
  123. }
  124. bool isNodeHidden(NodeType *Node) {
  125. return DTraits.isNodeHidden(Node);
  126. }
  127. void writeNode(NodeType& Node) {
  128. writeNode(&Node);
  129. }
  130. void writeNode(NodeType *const *Node) {
  131. writeNode(*Node);
  132. }
  133. void writeNode(NodeType *Node) {
  134. std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
  135. O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
  136. if (!NodeAttributes.empty()) O << NodeAttributes << ",";
  137. O << "label=\"{";
  138. if (!DTraits.renderGraphFromBottomUp()) {
  139. O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
  140. // If we should include the address of the node in the label, do so now.
  141. if (DTraits.hasNodeAddressLabel(Node, G))
  142. O << "|" << static_cast<const void*>(Node);
  143. std::string NodeDesc = DTraits.getNodeDescription(Node, G);
  144. if (!NodeDesc.empty())
  145. O << "|" << DOT::EscapeString(NodeDesc);
  146. }
  147. std::string edgeSourceLabels;
  148. raw_string_ostream EdgeSourceLabels(edgeSourceLabels);
  149. bool hasEdgeSourceLabels = getEdgeSourceLabels(EdgeSourceLabels, Node);
  150. if (hasEdgeSourceLabels) {
  151. if (!DTraits.renderGraphFromBottomUp()) O << "|";
  152. O << "{" << EdgeSourceLabels.str() << "}";
  153. if (DTraits.renderGraphFromBottomUp()) O << "|";
  154. }
  155. if (DTraits.renderGraphFromBottomUp()) {
  156. O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
  157. // If we should include the address of the node in the label, do so now.
  158. if (DTraits.hasNodeAddressLabel(Node, G))
  159. O << "|" << static_cast<const void*>(Node);
  160. std::string NodeDesc = DTraits.getNodeDescription(Node, G);
  161. if (!NodeDesc.empty())
  162. O << "|" << DOT::EscapeString(NodeDesc);
  163. }
  164. if (DTraits.hasEdgeDestLabels()) {
  165. O << "|{";
  166. unsigned i = 0, e = DTraits.numEdgeDestLabels(Node);
  167. for (; i != e && i != 64; ++i) {
  168. if (i) O << "|";
  169. O << "<d" << i << ">"
  170. << DOT::EscapeString(DTraits.getEdgeDestLabel(Node, i));
  171. }
  172. if (i != e)
  173. O << "|<d64>truncated...";
  174. O << "}";
  175. }
  176. O << "}\"];\n"; // Finish printing the "node" line
  177. // Output all of the edges now
  178. child_iterator EI = GTraits::child_begin(Node);
  179. child_iterator EE = GTraits::child_end(Node);
  180. for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
  181. if (!DTraits.isNodeHidden(*EI))
  182. writeEdge(Node, i, EI);
  183. for (; EI != EE; ++EI)
  184. if (!DTraits.isNodeHidden(*EI))
  185. writeEdge(Node, 64, EI);
  186. }
  187. void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
  188. if (NodeType *TargetNode = *EI) {
  189. int DestPort = -1;
  190. if (DTraits.edgeTargetsEdgeSource(Node, EI)) {
  191. child_iterator TargetIt = DTraits.getEdgeTarget(Node, EI);
  192. // Figure out which edge this targets...
  193. unsigned Offset =
  194. (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
  195. DestPort = static_cast<int>(Offset);
  196. }
  197. if (DTraits.getEdgeSourceLabel(Node, EI).empty())
  198. edgeidx = -1;
  199. emitEdge(static_cast<const void*>(Node), edgeidx,
  200. static_cast<const void*>(TargetNode), DestPort,
  201. DTraits.getEdgeAttributes(Node, EI, G));
  202. }
  203. }
  204. /// emitSimpleNode - Outputs a simple (non-record) node
  205. void emitSimpleNode(const void *ID, const std::string &Attr,
  206. const std::string &Label, unsigned NumEdgeSources = 0,
  207. const std::vector<std::string> *EdgeSourceLabels = 0) {
  208. O << "\tNode" << ID << "[ ";
  209. if (!Attr.empty())
  210. O << Attr << ",";
  211. O << " label =\"";
  212. if (NumEdgeSources) O << "{";
  213. O << DOT::EscapeString(Label);
  214. if (NumEdgeSources) {
  215. O << "|{";
  216. for (unsigned i = 0; i != NumEdgeSources; ++i) {
  217. if (i) O << "|";
  218. O << "<s" << i << ">";
  219. if (EdgeSourceLabels) O << DOT::EscapeString((*EdgeSourceLabels)[i]);
  220. }
  221. O << "}}";
  222. }
  223. O << "\"];\n";
  224. }
  225. /// emitEdge - Output an edge from a simple node into the graph...
  226. void emitEdge(const void *SrcNodeID, int SrcNodePort,
  227. const void *DestNodeID, int DestNodePort,
  228. const std::string &Attrs) {
  229. if (SrcNodePort > 64) return; // Eminating from truncated part?
  230. if (DestNodePort > 64) DestNodePort = 64; // Targeting the truncated part?
  231. O << "\tNode" << SrcNodeID;
  232. if (SrcNodePort >= 0)
  233. O << ":s" << SrcNodePort;
  234. O << " -> Node" << DestNodeID;
  235. if (DestNodePort >= 0 && DTraits.hasEdgeDestLabels())
  236. O << ":d" << DestNodePort;
  237. if (!Attrs.empty())
  238. O << "[" << Attrs << "]";
  239. O << ";\n";
  240. }
  241. /// getOStream - Get the raw output stream into the graph file. Useful to
  242. /// write fancy things using addCustomGraphFeatures().
  243. raw_ostream &getOStream() {
  244. return O;
  245. }
  246. };
  247. template<typename GraphType>
  248. raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
  249. bool ShortNames = false,
  250. const Twine &Title = "") {
  251. // Start the graph emission process...
  252. GraphWriter<GraphType> W(O, G, ShortNames);
  253. // Emit the graph.
  254. W.writeGraph(Title.str());
  255. return O;
  256. }
  257. template<typename GraphType>
  258. sys::Path WriteGraph(const GraphType &G, const Twine &Name,
  259. bool ShortNames = false, const Twine &Title = "") {
  260. std::string ErrMsg;
  261. sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
  262. if (Filename.isEmpty()) {
  263. errs() << "Error: " << ErrMsg << "\n";
  264. return Filename;
  265. }
  266. Filename.appendComponent((Name + ".dot").str());
  267. if (Filename.makeUnique(true,&ErrMsg)) {
  268. errs() << "Error: " << ErrMsg << "\n";
  269. return sys::Path();
  270. }
  271. errs() << "Writing '" << Filename.str() << "'... ";
  272. std::string ErrorInfo;
  273. raw_fd_ostream O(Filename.c_str(), ErrorInfo);
  274. if (ErrorInfo.empty()) {
  275. llvm::WriteGraph(O, G, ShortNames, Title);
  276. errs() << " done. \n";
  277. } else {
  278. errs() << "error opening file '" << Filename.str() << "' for writing!\n";
  279. Filename.clear();
  280. }
  281. return Filename;
  282. }
  283. /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
  284. /// then cleanup. For use from the debugger.
  285. ///
  286. template<typename GraphType>
  287. void ViewGraph(const GraphType &G, const Twine &Name,
  288. bool ShortNames = false, const Twine &Title = "",
  289. GraphProgram::Name Program = GraphProgram::DOT) {
  290. sys::Path Filename = llvm::WriteGraph(G, Name, ShortNames, Title);
  291. if (Filename.isEmpty())
  292. return;
  293. DisplayGraph(Filename, true, Program);
  294. }
  295. } // End llvm namespace
  296. #endif