-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathgraphviz_converter.cc
More file actions
276 lines (242 loc) · 9.76 KB
/
graphviz_converter.cc
File metadata and controls
276 lines (242 loc) · 9.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2019-2020 the ProGraML authors.
//
// Contact Chris Cummins <chrisc.101@gmail.com>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "programl/graph/format/graphviz_converter.h"
#include <iomanip>
#include <sstream>
#include "programl/proto/program_graph.pb.h"
#include "absl/container/flat_hash_map.h"
#include "boost/graph/graphviz.hpp"
#include "labm8/cpp/status.h"
#include "labm8/cpp/status_macros.h"
#include "labm8/cpp/string.h"
namespace error = labm8::error;
namespace programl {
namespace graph {
namespace format {
// The maximum length of a label. Labels longer than this are truncated with
// ellipses.
static const int kMaximumLabelLen = 32;
using AttributeMap = absl::flat_hash_map<string, string>;
using VertexProperties =
boost::property<boost::vertex_attribute_t, AttributeMap>;
using EdgeProperties =
boost::property<boost::edge_index_t, int,
boost::property<boost::edge_attribute_t, AttributeMap>>;
using GraphProperties = boost::property<
boost::graph_name_t, string,
boost::property<
boost::graph_graph_attribute_t, AttributeMap,
boost::property<
boost::graph_vertex_attribute_t, AttributeMap,
boost::property<boost::graph_edge_attribute_t, AttributeMap>>>>;
// An adjacency list for program graphs. We store the source Edge message as
// edge properties.
using GraphvizGraph = boost::adjacency_list<
/*OutEdgeList=*/boost::vecS,
/*VertexList=*/boost::vecS,
/*Directed=*/boost::directedS,
/*VertexProperties=*/VertexProperties,
/*EdgeProperties=*/EdgeProperties,
/*GraphProperties=*/GraphProperties>;
labm8::Status SerializeGraphVizToString(const ProgramGraph& graph,
std::ostream* ostream,
const NodeLabel& nodeLabelFormat,
const string& nodeFeatureName) {
// To construct a graphviz graph, we create a main graph and then produce
// a subgraph for each function in the graph. Vertices (nodes) are then added
// to the subgraphs, and edges added to the main graph.
// Create a main graph and pre-allocate the number of nodes in the graph.
// The main graph is used to store subgraphs which contain the actual
// vertices. Vertices can't be added directly to the main graph.
boost::subgraph<GraphvizGraph> main(graph.node_size());
boost::get_property(main, boost::graph_name) = "main";
// Set global graph, node, and edge style attributes.
boost::get_property(main, boost::graph_graph_attribute)["margin"] = "0";
boost::get_property(main, boost::graph_graph_attribute)["nodesep"] = "0.4";
boost::get_property(main, boost::graph_graph_attribute)["ranksep"] = "0.4";
boost::get_property(main, boost::graph_graph_attribute)["fontname"] =
"Inconsolata";
boost::get_property(main, boost::graph_graph_attribute)["fontsize"] = "20";
boost::get_property(main, boost::graph_vertex_attribute)["fontname"] =
"Inconsolata";
boost::get_property(main, boost::graph_vertex_attribute)["fontsize"] = "20";
boost::get_property(main, boost::graph_vertex_attribute)["penwidth"] = "2";
boost::get_property(main, boost::graph_vertex_attribute)["width"] = "1";
boost::get_property(main, boost::graph_vertex_attribute)["margin"] = "0";
boost::get_property(main, boost::graph_edge_attribute)["fontname"] =
"Inconsolata";
boost::get_property(main, boost::graph_edge_attribute)["fontsize"] = "20";
boost::get_property(main, boost::graph_edge_attribute)["penwidth"] = "3";
boost::get_property(main, boost::graph_edge_attribute)["arrowsize"] = ".8";
// Since we can't add any vertices directly to the main graph, create a
// subgraph for all nodes which do not have a function.
boost::subgraph<GraphvizGraph>& external = main.create_subgraph();
boost::get_property(external, boost::graph_name) = "external";
// Generate a list of per-function subgraphs.
std::vector<std::reference_wrapper<boost::subgraph<GraphvizGraph>>>
functionGraphs;
for (int i = 0; i < graph.function_size(); ++i) {
const auto& function = graph.function(i);
functionGraphs.push_back(main.create_subgraph());
auto& functionGraph = functionGraphs[functionGraphs.size() - 1].get();
// Set the name of the function.
string functionName = function.name();
labm8::TruncateWithEllipsis(functionName, kMaximumLabelLen);
boost::get_property(functionGraph, boost::graph_graph_attribute)["label"] =
functionName;
boost::get_property(functionGraph, boost::graph_graph_attribute)["margin"] =
"10";
boost::get_property(functionGraph, boost::graph_graph_attribute)["style"] =
"dotted";
// Set the name of the graph. Names must begin with "cluster".
std::stringstream subgraphName;
subgraphName << "cluster" << functionName;
boost::get_property(functionGraphs[functionGraphs.size() - 1].get(),
boost::graph_name) = subgraphName.str();
}
// Create the vertices.
for (int i = 0; i < graph.node_size(); ++i) {
const Node& node = graph.node(i);
// Determine the subgraph to add this node to.
boost::subgraph<GraphvizGraph>* dst = &external;
if (i && (node.type() == Node::INSTRUCTION || node.type() == Node::VARIABLE)) {
dst = &functionGraphs[node.function()].get();
}
// Create the vertex.
auto vertex = add_vertex(i, *dst);
// Get the attributes dictionary for this vertex.
auto& attributes = get(boost::vertex_attribute, *dst)[vertex];
// Set the node text.
std::stringstream textStream;
string text;
switch (nodeLabelFormat) {
case kNone:
break;
case kText:
text = node.text();
break;
case kFeature: {
std::stringstream os;
const auto& it = node.features().feature().find(nodeFeatureName);
if (it == node.features().feature().end()) {
// Do nothing if the node is not found.
break;
}
const Feature& feature = it->second;
// Int array
for (int i = 0; i < feature.int64_list().value_size(); ++i) {
if (i) {
os << ", ";
}
os << feature.int64_list().value(i);
}
// Float array
os << std::setprecision(4);
for (int i = 0; i < feature.float_list().value_size(); ++i) {
if (i) {
os << ", ";
}
os << feature.float_list().value(i);
}
// Bytes array
for (int i = 0; i < feature.bytes_list().value_size(); ++i) {
if (i) {
os << ", ";
}
string value(feature.bytes_list().value(i));
labm8::TruncateWithEllipsis(value, kMaximumLabelLen);
os << value;
}
text = os.str();
break;
}
}
labm8::TruncateWithEllipsis(text, kMaximumLabelLen);
attributes["label"] = text;
attributes["style"] = "filled";
// Set the node shape.
switch (node.type()) {
case Node::INSTRUCTION:
attributes["shape"] = "box";
attributes["fillcolor"] = "#3c78d8";
attributes["fontcolor"] = "#ffffff";
break;
case Node::VARIABLE:
attributes["shape"] = "ellipse";
attributes["fillcolor"] = "#f4cccc";
attributes["color"] = "#990000";
attributes["fontcolor"] = "#990000";
break;
case Node::CONSTANT:
attributes["shape"] = "octagon";
attributes["fillcolor"] = "#e99c9c";
attributes["color"] = "#990000";
attributes["fontcolor"] = "#990000";
break;
case Node::TYPE:
attributes["shape"] = "diamond";
attributes["fillcolor"] = "#cccccc";
attributes["color"] = "#cccccc";
attributes["fontcolor"] = "#222222";
break;
}
}
// Add the edges to the graph.
for (int i = 0; i < graph.edge_size(); ++i) {
const Edge& edge = graph.edge(i);
// Create the edge.
auto newEdge = boost::add_edge(edge.source(), edge.target(), main);
// Get the attributes dictionary for this edge.
auto& attributes = get(boost::edge_attribute, main)[newEdge.first];
// Set the edge color.
string color;
switch (edge.flow()) {
case Edge::CONTROL:
attributes["color"] = "#345393";
attributes["weight"] = "10";
break;
case Edge::DATA:
attributes["color"] = "#990000";
attributes["weight"] = "0";
break;
case Edge::CALL:
attributes["color"] = "#65ae4d";
attributes["weight"] = "1";
break;
case Edge::TYPE:
attributes["color"] = "#aaaaaa";
attributes["weight"] = "1";
attributes["penwidth"] = "1.5";
break;
}
// Set the edge label.
if (edge.position()) {
// Position labels for control edge are drawn close to the originating
// instruction. For control edges, they are drawn close to the branching
// instruction. For data and type edges, they are drawn close to the
// consuming node.
const string label =
edge.flow() == Edge::CONTROL ? "taillabel" : "headlabel";
attributes[label] = std::to_string(edge.position());
attributes["labelfontcolor"] = attributes["color"];
}
}
boost::write_graphviz(*ostream, main);
return labm8::Status::OK;
}
} // namespace format
} // namespace graph
} // namespace programl