-
Notifications
You must be signed in to change notification settings - Fork 12
/
cfg_plotter.py
33 lines (26 loc) · 1.02 KB
/
cfg_plotter.py
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
from typing import List
from pygraphviz import AGraph
from .. import om # noqa
class CFGPlotter:
def __init__(self, cfgs: List[om.Cfg]):
self.cfgs = cfgs
self.graph = self.generate_graph()
def generate_graph(self) -> AGraph:
graph = AGraph(directed=True, splines='curved', overlap='vpsc')
for cfg in self.cfgs:
for bb in cfg.basic_blocks:
style = {}
if bb.type == 'BBExit':
style = dict(style='filled', color='#665c54')
if bb.type == 'BBEntry':
style = dict(style='filled', color='#458588')
graph.add_node(
n=bb.id, label=f'bb={bb.id} stmt={bb.stmt_ids}', **style)
for succ in bb.succs:
graph.add_edge(bb.id, succ)
for pred in bb.preds:
graph.add_edge(pred, bb.id)
return graph
def save_file(self, filepath: str):
self.graph.layout()
self.graph.draw(filepath)