Static Value-Flow Analysis
Loading...
Searching...
No Matches
GraphWriter.h
Go to the documentation of this file.
1//===- Graphs/GraphWriter.h - Write graph to a .dot file --*- C++ -*-===//
2//
3// From the LLVM Project with some modifications, under the Apache License v2.0
4// with LLVM Exceptions. See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a simple interface that can be used to print out generic
10// LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T
11// graphviz package (http://www.research.att.com/sw/tools/graphviz/) which can
12// be used to turn the files output by this interface into a variety of
13// different graphics formats.
14//
15// Graphs do not need to implement any interface past what is already required
16// by the GraphTraits template, but they can choose to implement specializations
17// of the DOTGraphTraits template if they want to customize the graphs output in
18// any way.
19//
20//===----------------------------------------------------------------------===//
21
22#ifndef GRAPHS_GRAPHWRITER_H
23#define GRAPHS_GRAPHWRITER_H
24
25#include "Graphs/GraphTraits.h"
27#include "Util/Options.h"
28#include <algorithm>
29#include <cstddef>
30#include <iterator>
31#include <string>
32#include <type_traits>
33#include <vector>
34#include <fstream>
35#include <sstream>
36#include <iostream>
37
38namespace SVF
39{
40
41namespace DOT // Private functions...
42{
43
44std::string EscapeStr(const std::string &Label);
45
46} // end namespace DOT
47
48namespace GraphProgram
49{
50
59
60} // end namespace GraphProgram
61
62template<typename GraphType>
64{
65 std::ofstream &O;
66 const GraphType &G;
67
70 using NodeRef = typename GTraits::NodeRef;
71 using node_iterator = typename GTraits::nodes_iterator;
72 using child_iterator = typename GTraits::ChildIteratorType;
74
75 // NodeRef may be a pointer or a value type (e.g. a sliced-view contextual
76 // NodeRef). Node identity for the dot output is obtained from
77 // DOTGraphTraits::getNodeIdentifier (defaults to the pointer value).
78
79 // Writes the edge labels of the node to O and returns true if there are any
80 // edge labels not equal to the empty string "".
81 bool getEdgeSourceLabels(std::stringstream &O2, NodeRef Node)
82 {
83 child_iterator EI = GTraits::child_begin(Node);
84 child_iterator EE = GTraits::child_end(Node);
85 bool hasEdgeSourceLabels = false;
86
87 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
88 {
89 std::string label = DTraits.getEdgeSourceLabel(Node, EI);
90
91 if (label.empty())
92 continue;
93
95
96 if (i)
97 O2 << "|";
98
99 O2 << "<s" << i << ">" << DOT::EscapeStr(label);
100 }
101
102 if (EI != EE && hasEdgeSourceLabels)
103 O2 << "|<s64>truncated...";
104
105 return hasEdgeSourceLabels;
106 }
107
108public:
109 GraphWriter(std::ofstream &o, const GraphType &g, bool SN) : O(o), G(g)
110 {
112 }
113
114 void writeGraph(const std::string &Title = "")
115 {
116 // Output the header for the graph...
118
119 // Emit all of the nodes in the graph...
120 writeNodes();
121
122 // Output any customizations on the graph
124
125 // Output the end of the graph
126 writeFooter();
127 }
128
129 void writeHeader(const std::string &Title)
130 {
131 std::string GraphName(DTraits.getGraphName(G));
132
133 if (!Title.empty())
134 O << "digraph \"" << DOT::EscapeStr(Title) << "\" {\n";
135 else if (!GraphName.empty())
136 O << "digraph \"" << DOT::EscapeStr(GraphName) << "\" {\n";
137 else
138 O << "digraph unnamed {\n";
139
141 O << "\trankdir=\"BT\";\n";
142
143 if (!Title.empty())
144 O << "\tlabel=\"" << DOT::EscapeStr(Title) << "\";\n";
145 else if (!GraphName.empty())
146 O << "\tlabel=\"" << DOT::EscapeStr(GraphName) << "\";\n";
148 O << "\n";
149 }
150
152 {
153 // Finish off the graph
154 O << "}\n";
155 }
156
158 {
159 // Loop over the graph, printing it out...
160 for (const auto Node : nodes<GraphType>(G))
161 if (!isNodeHidden(Node))
162 writeNode(Node);
163 }
164
166 {
167 return DTraits.isNodeHidden(Node, G);
168 }
169
171 {
172 std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
173
174 O << "\tNode" << DTraits.getNodeIdentifier(Node) << " [";
175 if (!NodeAttributes.empty()) O << NodeAttributes << ",";
176 O << "label=\"{";
177
179 {
180 std::string label = DTraits.getNodeLabel(Node, G);
181 if (label.length() > Options::MaxNodeLabelLength())
182 {
183 label = label.substr(0, Options::MaxNodeLabelLength()) + "...";
184 }
185
187
188 // If we should include the address of the node in the label, do so now.
189 std::string Id = DTraits.getNodeIdentifierLabel(Node, G);
190 if (!Id.empty())
191 O << "|" << DOT::EscapeStr(Id);
192
193 std::string NodeDesc = DTraits.getNodeDescription(Node, G);
194 if (!NodeDesc.empty())
195 O << "|" << DOT::EscapeStr(NodeDesc);
196 }
197
198 std::string edgeSourceLabels;
199 std::stringstream EdgeSourceLabels(edgeSourceLabels);
201
203 {
204 if (!DTraits.renderGraphFromBottomUp()) O << "|";
205
206 O << "{" << EdgeSourceLabels.str() << "}";
207
208 if (DTraits.renderGraphFromBottomUp()) O << "|";
209 }
210
212 {
214
215 // If we should include the address of the node in the label, do so now.
216 std::string Id = DTraits.getNodeIdentifierLabel(Node, G);
217 if (!Id.empty())
218 O << "|" << DOT::EscapeStr(Id);
219
220 std::string NodeDesc = DTraits.getNodeDescription(Node, G);
221 if (!NodeDesc.empty())
222 O << "|" << DOT::EscapeStr(NodeDesc);
223 }
224
226 {
227 O << "|{";
228
229 unsigned i = 0, e = DTraits.numEdgeDestLabels(Node);
230 for (; i != e && i != 64; ++i)
231 {
232 if (i) O << "|";
233 O << "<d" << i << ">"
235 }
236
237 if (i != e)
238 O << "|<d64>truncated...";
239 O << "}";
240 }
241
242 O << "}\"];\n"; // Finish printing the "node" line
243
244 // Output all of the edges now
245 child_iterator EI = GTraits::child_begin(Node);
246 child_iterator EE = GTraits::child_end(Node);
247 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
248 if (!DTraits.isNodeHidden(*EI, G))
249 writeEdge(Node, i, EI);
250 for (; EI != EE; ++EI)
251 if (!DTraits.isNodeHidden(*EI, G))
252 writeEdge(Node, 64, EI);
253 }
254
256 {
257 if (NodeRef TargetNode = *EI)
258 {
259 int DestPort = -1;
261 {
263
264 // Figure out which edge this targets...
265 unsigned Offset =
266 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
267 DestPort = static_cast<int>(Offset);
268 }
269
270 if (DTraits.getEdgeSourceLabel(Node, EI).empty())
271 edgeidx = -1;
272
275 DTraits.getEdgeAttributes(Node, EI, G));
276 }
277 }
278
280 void emitSimpleNode(const void *ID, const std::string &Attr,
281 const std::string &Label, unsigned NumEdgeSources = 0,
282 const std::vector<std::string> *EdgeSourceLabels = nullptr)
283 {
284 O << "\tNode" << ID << "[ ";
285 if (!Attr.empty())
286 O << Attr << ",";
287 O << " label =\"";
288 if (NumEdgeSources) O << "{";
290 if (NumEdgeSources)
291 {
292 O << "|{";
293
294 for (unsigned i = 0; i != NumEdgeSources; ++i)
295 {
296 if (i) O << "|";
297 O << "<s" << i << ">";
299 }
300 O << "}}";
301 }
302 O << "\"];\n";
303 }
304
306 void emitEdge(const void *SrcNodeID, int SrcNodePort,
307 const void *DestNodeID, int DestNodePort,
308 const std::string &Attrs)
309 {
310 if (SrcNodePort > 64) return; // Emanating from truncated part?
311 if (DestNodePort > 64) DestNodePort = 64; // Targeting the truncated part?
312
313 O << "\tNode" << SrcNodeID;
314 if (SrcNodePort >= 0)
315 O << ":s" << SrcNodePort;
316 O << " -> Node" << DestNodeID;
318 O << ":d" << DestNodePort;
319
320 if (!Attrs.empty())
321 O << "[" << Attrs << "]";
322 O << ";\n";
323 }
324
327 std::ofstream &getOStream()
328 {
329 return O;
330 }
331};
332
333template<typename GraphType>
334std::ofstream &WriteGraph(std::ofstream &O, const GraphType &G,
335 bool ShortNames = false)
336{
337 // Start the graph emission process...
339
340 // Emit the graph.
341 W.writeGraph("");
342
343 return O;
344}
345
350template <typename GraphType>
351std::string WriteGraph(const GraphType &G,
352 bool ShortNames = false,
353 std::string Filename = "")
354{
355
356 std::ofstream O(Filename);
357
358 if (O.fail())
359 {
360 std::cerr << "error opening file '" << Filename << "' for writing!\n";
361 O.close();
362 return "";
363 }
364
366 O.close();
367
368 std::cerr << " done. \n";
369
370 return Filename;
371}
372
376template<typename GraphType>
377void ViewGraph(const GraphType &G,const std::string& name,
378 bool ShortNames = false,
380{
382}
383
384} // end namespace llvm
385
386#endif // LLVM_SUPPORT_GRAPHWRITER_H
const char *const name
Definition cJSON.h:264
const GraphType & G
Definition GraphWriter.h:66
void emitSimpleNode(const void *ID, const std::string &Attr, const std::string &Label, unsigned NumEdgeSources=0, const std::vector< std::string > *EdgeSourceLabels=nullptr)
emitSimpleNode - Outputs a simple (non-record) node
void writeNode(NodeRef Node)
DOTGraphTraits< GraphType > DOTTraits
Definition GraphWriter.h:68
std::ofstream & O
Definition GraphWriter.h:65
void emitEdge(const void *SrcNodeID, int SrcNodePort, const void *DestNodeID, int DestNodePort, const std::string &Attrs)
emitEdge - Output an edge from a simple node into the graph...
DOTTraits DTraits
Definition GraphWriter.h:73
void writeEdge(NodeRef Node, unsigned edgeidx, child_iterator EI)
GenericGraphTraits< GraphType > GTraits
Definition GraphWriter.h:69
bool isNodeHidden(NodeRef Node)
bool getEdgeSourceLabels(std::stringstream &O2, NodeRef Node)
Definition GraphWriter.h:81
typename GTraits::nodes_iterator node_iterator
Definition GraphWriter.h:71
void writeHeader(const std::string &Title)
typename GTraits::NodeRef NodeRef
Definition GraphWriter.h:70
typename GTraits::ChildIteratorType child_iterator
Definition GraphWriter.h:72
void writeGraph(const std::string &Title="")
std::ofstream & getOStream()
GraphWriter(std::ofstream &o, const GraphType &g, bool SN)
static const Option< u32_t > MaxNodeLabelLength
Definition Options.h:276
std::string EscapeStr(const std::string &Label)
for isBitcode
Definition BasicTypes.h:70
void ViewGraph(const GraphType &G, const std::string &name, bool ShortNames=false, GraphProgram::Name Program=GraphProgram::DOT)
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
std::ofstream & WriteGraph(std::ofstream &O, const GraphType &G, bool ShortNames=false)
static std::string getNodeIdentifierLabel(NodeT, const GraphType &)
static EdgeIter getEdgeTarget(NodeT, EdgeIter I)
static std::string getGraphName(const GraphType &)
static void addCustomGraphFeatures(const GraphType &, GraphWriter &)
static std::string getEdgeSourceLabel(NodeT, EdgeIter)
static std::string getNodeAttributes(NodeT, const GraphType &)
static const void * getNodeIdentifier(NodeT N)
static std::string getEdgeAttributes(NodeT, EdgeIter, const GraphType &)
std::string getNodeLabel(NodeT, const GraphType &)
static unsigned numEdgeDestLabels(NodeT)
static std::string getEdgeDestLabel(NodeT, unsigned)
static bool renderGraphFromBottomUp()
static bool edgeTargetsEdgeSource(NodeT, EdgeIter)
static std::string getNodeDescription(NodeT, const GraphType &)
static bool isNodeHidden(NodeT, const GraphType &)
static std::string getGraphProperties(const GraphType &)
typename GraphType::UnknownGraphTypeError NodeRef
Definition GraphTraits.h:80