forked from caterinaurban/function
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty_cfg.py
executable file
·216 lines (154 loc) · 5.49 KB
/
pretty_cfg.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
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
import subprocess
import sys
import re
content = []
for line in sys.stdin:
l = line.rstrip()
if l != '':
content.append(l)
# with open("test.out") as f:
# content = f.readlines()
# # you may also want to remove whitespace characters like `\n` at the end of each line
# content = [x.strip() for x in content]
label_pattern = re.compile('(\[\s*(\d+):\]):')
cfg = []
fwd = []
cfg_dot = []
properties = {}
prop = None
target = None
result = ""
for l in content:
if l == "CFG_DOT:":
target = cfg_dot
elif l == "CFG:":
target = cfg
elif l == "Forwad Analysis:":
target = cfg
elif l.startswith("Property: "):
prop = {}
properties[l[10:]] = prop
elif label_pattern.match(l):
target = []
label = label_pattern.match(l).group(2)
prop[label] = target
elif l.startswith("Analysis Result: "):
result = l
elif l.strip() == "":
# ignore
l.strip()
elif target is None:
print("Invalid Input Format")
sys.exit(-1)
else:
target.append(l)
cfg_dot = "\n".join([s for s in cfg_dot])
dot_process = subprocess.Popen(["dot", "-Tsvg"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
cfg_dot_svg = dot_process.communicate(bytes((cfg_dot + "\n").encode()))[0].decode()
dot_process.terminate()
trees = {}
for p in properties:
trees[p] = {}
for l in properties[p]:
inv = properties[p][l]
if inv[-1].startswith("DOT:"):
dot_graph = inv[-1][5:]
del inv[-1]
try:
dot_process = subprocess.Popen(["dot", "-Tsvg"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
svg_graph = dot_process.communicate(bytes((dot_graph + "\n").encode()))[0]
dot_process.terminate()
trees[p][l] = svg_graph.decode()
except:
trees[p][l] = "Missing Graphviz installation"
print("""
<!DOCTYPE html>
<html>
<head>
<title>FuncTion Static Analyzer Result</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap core CSS -->
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" media="screen">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js"></script>
<![endif]-->
<style>
svg g.node {
pointer-events: all;
cursor: hand;
}
.property_node {
font-family: monospace;
position: absolute;
border: 2px solid black;
background-color: #eeeeee;
display:none;
}
</style>
</head>
<body>
""")
print("""
<div class="container-fluid">
<div class="row">
<h1>{0}</h1>
</div>
<div class="row">
""".format(result))
# Tab header ----------------------------------------------------------
print(""" <ul class="nav nav-tabs" role="tablist"> """)
for id,p in enumerate(properties.keys()):
print("""
<li role="presentation"><a href="#{0}" class="tab_button" id="tab_{0}" aria-controls="home" role="tab" data-toggle="tab">{1}</a></li>
""".format(id,p))
print(""" </ul> """)
# ------------------------------------------------------------------------
# Tab contents -------------------------------------------------------------
print(""" <div class="tab-content"> """)
for id,p in enumerate(properties.keys()):
print(""" <div role="tabpanel" class="tab-pane active" id="{0}"> """.format(id))
print("""<div class="cfg"> """)
print(cfg_dot_svg)
print(""" </div > """)
print("""<div class="properties"> """)
for node in properties[p]:
print("""<div class="property_node" id={0}> """.format(str(id) + "_" + str(node)))
print("<br/>".join(properties[p][node]))
print(""" </div > """)
print(""" </div > """)
print(""" </div > """)
print(""" </div > """)
print("""
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script>
$(function () {
$('.tab_button').last().click()
$('.property_node').hide()
$('.tab-pane').each(function() {
var propertyId = this.id;
console.log(this.id)
$(this).find('svg g.node title').each(function() {
var title = $(this);
var nodeId = title.text();
var clickHandler = function(e) {
console.log('Clicked: ' + propertyId + '_' + nodeId);
var nodeState = $('div.properties div.property_node#'+ propertyId + '_' + nodeId)
nodeState.css('top', e.pageY + 15);
nodeState.css('left', e.pageX + 15);
nodeState.toggle();
};
title.parent().on('click', clickHandler);
})
})
})
</script>
</body>
</html>
""")