File indexing completed on 2026-05-10 08:44:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #ifndef LLVM_SUPPORT_GRAPHWRITER_H
0023 #define LLVM_SUPPORT_GRAPHWRITER_H
0024
0025 #include "llvm/ADT/GraphTraits.h"
0026 #include "llvm/ADT/StringRef.h"
0027 #include "llvm/ADT/Twine.h"
0028 #include "llvm/Support/DOTGraphTraits.h"
0029 #include "llvm/Support/FileSystem.h"
0030 #include "llvm/Support/raw_ostream.h"
0031 #include <iterator>
0032 #include <string>
0033 #include <type_traits>
0034 #include <vector>
0035
0036 namespace llvm {
0037
0038 namespace DOT {
0039
0040 std::string EscapeString(const std::string &Label);
0041
0042
0043
0044 StringRef getColorString(unsigned NodeNumber);
0045
0046 }
0047
0048 namespace GraphProgram {
0049
0050 enum Name {
0051 DOT,
0052 FDP,
0053 NEATO,
0054 TWOPI,
0055 CIRCO
0056 };
0057
0058 }
0059
0060 bool DisplayGraph(StringRef Filename, bool wait = true,
0061 GraphProgram::Name program = GraphProgram::DOT);
0062
0063 template<typename GraphType>
0064 class GraphWriter {
0065 raw_ostream &O;
0066 const GraphType &G;
0067 bool RenderUsingHTML = false;
0068
0069 using DOTTraits = DOTGraphTraits<GraphType>;
0070 using GTraits = GraphTraits<GraphType>;
0071 using NodeRef = typename GTraits::NodeRef;
0072 using node_iterator = typename GTraits::nodes_iterator;
0073 using child_iterator = typename GTraits::ChildIteratorType;
0074 DOTTraits DTraits;
0075
0076 static_assert(std::is_pointer_v<NodeRef>,
0077 "FIXME: Currently GraphWriter requires the NodeRef type to be "
0078 "a pointer.\nThe pointer usage should be moved to "
0079 "DOTGraphTraits, and removed from GraphWriter itself.");
0080
0081
0082
0083 bool getEdgeSourceLabels(raw_ostream &O, NodeRef Node) {
0084 child_iterator EI = GTraits::child_begin(Node);
0085 child_iterator EE = GTraits::child_end(Node);
0086 bool hasEdgeSourceLabels = false;
0087
0088 if (RenderUsingHTML)
0089 O << "</tr><tr>";
0090
0091 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
0092 std::string label = DTraits.getEdgeSourceLabel(Node, EI);
0093
0094 if (label.empty())
0095 continue;
0096
0097 hasEdgeSourceLabels = true;
0098
0099 if (RenderUsingHTML)
0100 O << "<td colspan=\"1\" port=\"s" << i << "\">" << label << "</td>";
0101 else {
0102 if (i)
0103 O << "|";
0104
0105 O << "<s" << i << ">" << DOT::EscapeString(label);
0106 }
0107 }
0108
0109 if (EI != EE && hasEdgeSourceLabels) {
0110 if (RenderUsingHTML)
0111 O << "<td colspan=\"1\" port=\"s64\">truncated...</td>";
0112 else
0113 O << "|<s64>truncated...";
0114 }
0115
0116 return hasEdgeSourceLabels;
0117 }
0118
0119 public:
0120 GraphWriter(raw_ostream &o, const GraphType &g, bool SN) : O(o), G(g) {
0121 DTraits = DOTTraits(SN);
0122 RenderUsingHTML = DTraits.renderNodesUsingHTML();
0123 }
0124
0125 void writeGraph(const std::string &Title = "") {
0126
0127 writeHeader(Title);
0128
0129
0130 writeNodes();
0131
0132
0133 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, *this);
0134
0135
0136 writeFooter();
0137 }
0138
0139 void writeHeader(const std::string &Title) {
0140 std::string GraphName(DTraits.getGraphName(G));
0141
0142 if (!Title.empty())
0143 O << "digraph \"" << DOT::EscapeString(Title) << "\" {\n";
0144 else if (!GraphName.empty())
0145 O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
0146 else
0147 O << "digraph unnamed {\n";
0148
0149 if (DTraits.renderGraphFromBottomUp())
0150 O << "\trankdir=\"BT\";\n";
0151
0152 if (!Title.empty())
0153 O << "\tlabel=\"" << DOT::EscapeString(Title) << "\";\n";
0154 else if (!GraphName.empty())
0155 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
0156 O << DTraits.getGraphProperties(G);
0157 O << "\n";
0158 }
0159
0160 void writeFooter() {
0161
0162 O << "}\n";
0163 }
0164
0165 void writeNodes() {
0166
0167 for (const auto Node : nodes<GraphType>(G))
0168 if (!isNodeHidden(Node))
0169 writeNode(Node);
0170 }
0171
0172 bool isNodeHidden(NodeRef Node) { return DTraits.isNodeHidden(Node, G); }
0173
0174 void writeNode(NodeRef Node) {
0175 std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
0176
0177 O << "\tNode" << static_cast<const void *>(Node) << " [shape=";
0178 if (RenderUsingHTML)
0179 O << "none,";
0180 else
0181 O << "record,";
0182
0183 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
0184 O << "label=";
0185
0186 if (RenderUsingHTML) {
0187
0188
0189 unsigned ColSpan = 0;
0190 child_iterator EI = GTraits::child_begin(Node);
0191 child_iterator EE = GTraits::child_end(Node);
0192 for (; EI != EE && ColSpan != 64; ++EI, ++ColSpan)
0193 ;
0194 if (ColSpan == 0)
0195 ColSpan = 1;
0196
0197 if (EI != EE)
0198 ++ColSpan;
0199 O << "<<table border=\"0\" cellborder=\"1\" cellspacing=\"0\""
0200 << " cellpadding=\"0\"><tr><td align=\"text\" colspan=\"" << ColSpan
0201 << "\">";
0202 } else
0203 O << "\"{";
0204
0205 if (!DTraits.renderGraphFromBottomUp()) {
0206 if (RenderUsingHTML)
0207 O << DTraits.getNodeLabel(Node, G) << "</td>";
0208 else
0209 O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
0210
0211
0212 std::string Id = DTraits.getNodeIdentifierLabel(Node, G);
0213 if (!Id.empty())
0214 O << "|" << DOT::EscapeString(Id);
0215
0216 std::string NodeDesc = DTraits.getNodeDescription(Node, G);
0217 if (!NodeDesc.empty())
0218 O << "|" << DOT::EscapeString(NodeDesc);
0219 }
0220
0221 std::string edgeSourceLabels;
0222 raw_string_ostream EdgeSourceLabels(edgeSourceLabels);
0223 bool hasEdgeSourceLabels = getEdgeSourceLabels(EdgeSourceLabels, Node);
0224
0225 if (hasEdgeSourceLabels) {
0226 if (!DTraits.renderGraphFromBottomUp())
0227 if (!RenderUsingHTML)
0228 O << "|";
0229
0230 if (RenderUsingHTML)
0231 O << edgeSourceLabels;
0232 else
0233 O << "{" << edgeSourceLabels << "}";
0234
0235 if (DTraits.renderGraphFromBottomUp())
0236 if (!RenderUsingHTML)
0237 O << "|";
0238 }
0239
0240 if (DTraits.renderGraphFromBottomUp()) {
0241 if (RenderUsingHTML)
0242 O << DTraits.getNodeLabel(Node, G);
0243 else
0244 O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
0245
0246
0247 std::string Id = DTraits.getNodeIdentifierLabel(Node, G);
0248 if (!Id.empty())
0249 O << "|" << DOT::EscapeString(Id);
0250
0251 std::string NodeDesc = DTraits.getNodeDescription(Node, G);
0252 if (!NodeDesc.empty())
0253 O << "|" << DOT::EscapeString(NodeDesc);
0254 }
0255
0256 if (DTraits.hasEdgeDestLabels()) {
0257 O << "|{";
0258
0259 unsigned i = 0, e = DTraits.numEdgeDestLabels(Node);
0260 for (; i != e && i != 64; ++i) {
0261 if (i) O << "|";
0262 O << "<d" << i << ">"
0263 << DOT::EscapeString(DTraits.getEdgeDestLabel(Node, i));
0264 }
0265
0266 if (i != e)
0267 O << "|<d64>truncated...";
0268 O << "}";
0269 }
0270
0271 if (RenderUsingHTML)
0272 O << "</tr></table>>";
0273 else
0274 O << "}\"";
0275 O << "];\n";
0276
0277
0278 child_iterator EI = GTraits::child_begin(Node);
0279 child_iterator EE = GTraits::child_end(Node);
0280 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
0281 if (!DTraits.isNodeHidden(*EI, G))
0282 writeEdge(Node, i, EI);
0283 for (; EI != EE; ++EI)
0284 if (!DTraits.isNodeHidden(*EI, G))
0285 writeEdge(Node, 64, EI);
0286 }
0287
0288 void writeEdge(NodeRef Node, unsigned edgeidx, child_iterator EI) {
0289 if (NodeRef TargetNode = *EI) {
0290 int DestPort = -1;
0291 if (DTraits.edgeTargetsEdgeSource(Node, EI)) {
0292 child_iterator TargetIt = DTraits.getEdgeTarget(Node, EI);
0293
0294
0295 unsigned Offset =
0296 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
0297 DestPort = static_cast<int>(Offset);
0298 }
0299
0300 if (DTraits.getEdgeSourceLabel(Node, EI).empty())
0301 edgeidx = -1;
0302
0303 emitEdge(static_cast<const void*>(Node), edgeidx,
0304 static_cast<const void*>(TargetNode), DestPort,
0305 DTraits.getEdgeAttributes(Node, EI, G));
0306 }
0307 }
0308
0309
0310 void emitSimpleNode(const void *ID, const std::string &Attr,
0311 const std::string &Label, unsigned NumEdgeSources = 0,
0312 const std::vector<std::string> *EdgeSourceLabels = nullptr) {
0313 O << "\tNode" << ID << "[ ";
0314 if (!Attr.empty())
0315 O << Attr << ",";
0316 O << " label =\"";
0317 if (NumEdgeSources) O << "{";
0318 O << DOT::EscapeString(Label);
0319 if (NumEdgeSources) {
0320 O << "|{";
0321
0322 for (unsigned i = 0; i != NumEdgeSources; ++i) {
0323 if (i) O << "|";
0324 O << "<s" << i << ">";
0325 if (EdgeSourceLabels) O << DOT::EscapeString((*EdgeSourceLabels)[i]);
0326 }
0327 O << "}}";
0328 }
0329 O << "\"];\n";
0330 }
0331
0332
0333 void emitEdge(const void *SrcNodeID, int SrcNodePort,
0334 const void *DestNodeID, int DestNodePort,
0335 const std::string &Attrs) {
0336 if (SrcNodePort > 64) return;
0337 if (DestNodePort > 64) DestNodePort = 64;
0338
0339 O << "\tNode" << SrcNodeID;
0340 if (SrcNodePort >= 0)
0341 O << ":s" << SrcNodePort;
0342 O << " -> Node" << DestNodeID;
0343 if (DestNodePort >= 0 && DTraits.hasEdgeDestLabels())
0344 O << ":d" << DestNodePort;
0345
0346 if (!Attrs.empty())
0347 O << "[" << Attrs << "]";
0348 O << ";\n";
0349 }
0350
0351
0352
0353 raw_ostream &getOStream() {
0354 return O;
0355 }
0356 };
0357
0358 template<typename GraphType>
0359 raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
0360 bool ShortNames = false,
0361 const Twine &Title = "") {
0362
0363 GraphWriter<GraphType> W(O, G, ShortNames);
0364
0365
0366 W.writeGraph(Title.str());
0367
0368 return O;
0369 }
0370
0371 std::string createGraphFilename(const Twine &Name, int &FD);
0372
0373
0374
0375
0376
0377 template <typename GraphType>
0378 std::string WriteGraph(const GraphType &G, const Twine &Name,
0379 bool ShortNames = false,
0380 const Twine &Title = "",
0381 std::string Filename = "") {
0382 int FD;
0383 if (Filename.empty()) {
0384 Filename = createGraphFilename(Name.str(), FD);
0385 } else {
0386 std::error_code EC = sys::fs::openFileForWrite(
0387 Filename, FD, sys::fs::CD_CreateAlways, sys::fs::OF_Text);
0388
0389
0390 if (EC == std::errc::file_exists) {
0391 errs() << "file exists, overwriting" << "\n";
0392 } else if (EC) {
0393 errs() << "error writing into file" << "\n";
0394 return "";
0395 } else {
0396 errs() << "writing to the newly created file " << Filename << "\n";
0397 }
0398 }
0399 raw_fd_ostream O(FD, true);
0400
0401 if (FD == -1) {
0402 errs() << "error opening file '" << Filename << "' for writing!\n";
0403 return "";
0404 }
0405
0406 llvm::WriteGraph(O, G, ShortNames, Title);
0407 errs() << " done. \n";
0408
0409 return Filename;
0410 }
0411
0412
0413 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0414 template <typename GraphType>
0415 LLVM_DUMP_METHOD void
0416 dumpDotGraphToFile(const GraphType &G, const Twine &FileName,
0417 const Twine &Title, bool ShortNames = false,
0418 const Twine &Name = "") {
0419 llvm::WriteGraph(G, Name, ShortNames, Title, FileName.str());
0420 }
0421 #endif
0422
0423
0424
0425
0426 template<typename GraphType>
0427 void ViewGraph(const GraphType &G, const Twine &Name,
0428 bool ShortNames = false, const Twine &Title = "",
0429 GraphProgram::Name Program = GraphProgram::DOT) {
0430 std::string Filename = llvm::WriteGraph(G, Name, ShortNames, Title);
0431
0432 if (Filename.empty())
0433 return;
0434
0435 DisplayGraph(Filename, false, Program);
0436 }
0437
0438 }
0439
0440 #endif