xref: /llvm-project/clang/utils/analyzer/exploded-graph-rewriter.py (revision 44fb55bf96158ac3c9a90eae1818fb6b1ddefef6)
1#!/usr/bin/env python
2#
3#===- exploded-graph-rewriter.py - ExplodedGraph dump tool -----*- python -*--#
4#
5# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6# See https://llvm.org/LICENSE.txt for license information.
7# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8#
9#===-----------------------------------------------------------------------===#
10
11
12from __future__ import print_function
13
14import argparse
15import collections
16import json
17import logging
18import re
19
20
21# A deserialized source location.
22class SourceLocation(object):
23    def __init__(self, json_loc):
24        super(SourceLocation, self).__init__()
25        self.line = json_loc['line']
26        self.col = json_loc['column']
27        self.filename = json_loc['filename'] \
28            if 'filename' in json_loc else '(main file)'
29
30
31# A deserialized program point.
32class ProgramPoint(object):
33    def __init__(self, json_pp):
34        super(ProgramPoint, self).__init__()
35        self.kind = json_pp['kind']
36        self.tag = json_pp['tag']
37        if self.kind == 'Edge':
38            self.src_id = json_pp['src_id']
39            self.dst_id = json_pp['dst_id']
40        elif self.kind == 'Statement':
41            self.stmt_kind = json_pp['stmt_kind']
42            self.pointer = json_pp['pointer']
43            self.pretty = json_pp['pretty']
44            self.loc = SourceLocation(json_pp['location']) \
45                if json_pp['location'] is not None else None
46        elif self.kind == 'BlockEntrance':
47            self.block_id = json_pp['block_id']
48
49
50# A value of a single expression in a deserialized Environment.
51class EnvironmentBinding(object):
52    def __init__(self, json_eb):
53        super(EnvironmentBinding, self).__init__()
54        self.stmt_id = json_eb['stmt_id']
55        self.pretty = json_eb['pretty']
56        self.value = json_eb['value']
57
58
59# Deserialized description of a location context.
60class LocationContext(object):
61    def __init__(self, json_frame):
62        super(LocationContext, self).__init__()
63        self.lctx_id = json_frame['lctx_id']
64        self.caption = json_frame['location_context']
65        self.decl = json_frame['calling']
66        self.line = json_frame['call_line']
67
68
69# A group of deserialized Environment bindings that correspond to a specific
70# location context.
71class EnvironmentFrame(object):
72    def __init__(self, json_frame):
73        super(EnvironmentFrame, self).__init__()
74        self.location_context = LocationContext(json_frame)
75        self.bindings = [EnvironmentBinding(b) for b in json_frame['items']] \
76            if json_frame['items'] is not None else []
77
78
79# A deserialized Environment.
80class Environment(object):
81    def __init__(self, json_e):
82        super(Environment, self).__init__()
83        self.frames = [EnvironmentFrame(f) for f in json_e]
84
85
86# A single binding in a deserialized RegionStore cluster.
87class StoreBinding(object):
88    def __init__(self, json_sb):
89        super(StoreBinding, self).__init__()
90        self.kind = json_sb['kind']
91        self.offset = json_sb['offset']
92        self.value = json_sb['value']
93
94
95# A single cluster of the deserialized RegionStore.
96class StoreCluster(object):
97    def __init__(self, json_sc):
98        super(StoreCluster, self).__init__()
99        self.base_region = json_sc['cluster']
100        self.bindings = [StoreBinding(b) for b in json_sc['items']]
101
102
103# A deserialized RegionStore.
104class Store(object):
105    def __init__(self, json_s):
106        super(Store, self).__init__()
107        self.clusters = [StoreCluster(c) for c in json_s]
108
109
110# A deserialized program state.
111class ProgramState(object):
112    def __init__(self, state_id, json_ps):
113        super(ProgramState, self).__init__()
114        logging.debug('Adding ProgramState ' + str(state_id))
115
116        self.state_id = state_id
117        self.store = Store(json_ps['store']) \
118            if json_ps['store'] is not None else None
119        self.environment = Environment(json_ps['environment']) \
120            if json_ps['environment'] is not None else None
121        # TODO: Objects under construction.
122        # TODO: Constraint ranges.
123        # TODO: Dynamic types of objects.
124        # TODO: Checker messages.
125
126
127# A deserialized exploded graph node. Has a default constructor because it
128# may be referenced as part of an edge before its contents are deserialized,
129# and in this moment we already need a room for predecessors and successors.
130class ExplodedNode(object):
131    def __init__(self):
132        super(ExplodedNode, self).__init__()
133        self.predecessors = []
134        self.successors = []
135
136    def construct(self, node_id, json_node):
137        logging.debug('Adding ' + node_id)
138        self.node_id = json_node['node_id']
139        self.ptr = json_node['pointer']
140        self.points = [ProgramPoint(p) for p in json_node['program_points']]
141        self.state = ProgramState(json_node['state_id'],
142                                  json_node['program_state']) \
143            if json_node['program_state'] is not None else None
144
145        assert self.node_name() == node_id
146
147    def node_name(self):
148        return 'Node' + self.ptr
149
150
151# A deserialized ExplodedGraph. Constructed by consuming a .dot file
152# line-by-line.
153class ExplodedGraph(object):
154    # Parse .dot files with regular expressions.
155    node_re = re.compile(
156        '^(Node0x[0-9a-f]*) \\[shape=record,.*label="{(.*)\\\\l}"\\];$')
157    edge_re = re.compile(
158        '^(Node0x[0-9a-f]*) -> (Node0x[0-9a-f]*);$')
159
160    def __init__(self):
161        super(ExplodedGraph, self).__init__()
162        self.nodes = collections.defaultdict(ExplodedNode)
163        self.root_id = None
164        self.incomplete_line = ''
165
166    def add_raw_line(self, raw_line):
167        if raw_line.startswith('//'):
168            return
169
170        # Allow line breaks by waiting for ';'. This is not valid in
171        # a .dot file, but it is useful for writing tests.
172        if len(raw_line) > 0 and raw_line[-1] != ';':
173            self.incomplete_line += raw_line
174            return
175        raw_line = self.incomplete_line + raw_line
176        self.incomplete_line = ''
177
178        # Apply regexps one by one to see if it's a node or an edge
179        # and extract contents if necessary.
180        logging.debug('Line: ' + raw_line)
181        result = self.edge_re.match(raw_line)
182        if result is not None:
183            logging.debug('Classified as edge line.')
184            pred = result.group(1)
185            succ = result.group(2)
186            self.nodes[pred].successors.append(succ)
187            self.nodes[succ].predecessors.append(pred)
188            return
189        result = self.node_re.match(raw_line)
190        if result is not None:
191            logging.debug('Classified as node line.')
192            node_id = result.group(1)
193            if len(self.nodes) == 0:
194                self.root_id = node_id
195            # Note: when writing tests you don't need to escape everything,
196            # even though in a valid dot file everything is escaped.
197            node_label = result.group(2).replace('\\l', '') \
198                                        .replace(' ', '') \
199                                        .replace('\\"', '"') \
200                                        .replace('\\{', '{') \
201                                        .replace('\\}', '}') \
202                                        .replace('\\<', '\\\\<') \
203                                        .replace('\\>', '\\\\>') \
204                                        .rstrip(',')
205            logging.debug(node_label)
206            json_node = json.loads(node_label)
207            self.nodes[node_id].construct(node_id, json_node)
208            return
209        logging.debug('Skipping.')
210
211
212# A visitor that dumps the ExplodedGraph into a DOT file with fancy HTML-based
213# syntax highlighing.
214class DotDumpVisitor(object):
215    def __init__(self):
216        super(DotDumpVisitor, self).__init__()
217
218    @staticmethod
219    def _dump_raw(s):
220        print(s, end='')
221
222    @staticmethod
223    def _dump(s):
224        print(s.replace('&', '&amp;')
225               .replace('{', '\\{')
226               .replace('}', '\\}')
227               .replace('\\<', '&lt;')
228               .replace('\\>', '&gt;')
229               .replace('\\l', '<br />')
230               .replace('|', ''), end='')
231
232    def visit_begin_graph(self, graph):
233        self._graph = graph
234        self._dump_raw('digraph "ExplodedGraph" {\n')
235        self._dump_raw('label="";\n')
236
237    def visit_program_point(self, p):
238        if p.kind in ['Edge', 'BlockEntrance', 'BlockExit']:
239            color = 'gold3'
240        elif p.kind in ['PreStmtPurgeDeadSymbols',
241                        'PostStmtPurgeDeadSymbols']:
242            color = 'red'
243        elif p.kind in ['CallEnter', 'CallExitBegin', 'CallExitEnd']:
244            color = 'blue'
245        elif p.kind in ['Statement']:
246            color = 'cyan3'
247        else:
248            color = 'forestgreen'
249
250        if p.kind == 'Statement':
251            if p.loc is not None:
252                self._dump('<tr><td align="left" width="0">'
253                           '%s:<b>%s</b>:<b>%s</b>:</td>'
254                           '<td align="left" width="0"><font color="%s">'
255                           '%s</font></td><td>%s</td></tr>'
256                           % (p.loc.filename, p.loc.line,
257                              p.loc.col, color, p.stmt_kind, p.pretty))
258            else:
259                self._dump('<tr><td align="left" width="0">'
260                           '<i>Invalid Source Location</i>:</td>'
261                           '<td align="left" width="0">'
262                           '<font color="%s">%s</font></td><td>%s</td></tr>'
263                           % (color, p.stmt_kind, p.pretty))
264        elif p.kind == 'Edge':
265            self._dump('<tr><td width="0">-</td>'
266                       '<td align="left" width="0">'
267                       '<font color="%s">%s</font></td><td align="left">'
268                       '[B%d] -\\> [B%d]</td></tr>'
269                       % (color, p.kind, p.src_id, p.dst_id))
270        else:
271            # TODO: Print more stuff for other kinds of points.
272            self._dump('<tr><td width="0">-</td>'
273                       '<td align="left" width="0" colspan="2">'
274                       '<font color="%s">%s</font></td></tr>'
275                       % (color, p.kind))
276
277    def visit_environment(self, e):
278        self._dump('<table border="0">')
279
280        for f in e.frames:
281            self._dump('<tr><td align="left"><b>%s</b></td>'
282                       '<td align="left"><font color="grey60">%s </font>'
283                       '%s</td></tr>'
284                       % (f.location_context.caption,
285                          f.location_context.decl,
286                          ('(line %s)' % f.location_context.line)
287                          if f.location_context.line is not None else ''))
288            for b in f.bindings:
289                self._dump('<tr><td align="left"><i>S%s</i></td>'
290                           '<td align="left">%s</td>'
291                           '<td align="left">%s</td></tr>'
292                           % (b.stmt_id, b.pretty, b.value))
293
294        self._dump('</table>')
295
296    def visit_store(self, s):
297        self._dump('<table border="0">')
298
299        for c in s.clusters:
300            for b in c.bindings:
301                self._dump('<tr><td align="left">%s</td>'
302                           '<td align="left">%s</td>'
303                           '<td align="left">%s</td>'
304                           '<td align="left">%s</td></tr>'
305                           % (c.base_region, b.offset,
306                              '(<i>Default</i>)' if b.kind == 'Default'
307                              else '',
308                              b.value))
309
310        self._dump('</table>')
311
312    def visit_state(self, s):
313        self._dump('<tr><td align="left">'
314                   '<b>Store: </b>')
315        if s.store is None:
316            self._dump('<i> Nothing!</i>')
317        else:
318            self._dump('</td></tr>'
319                       '<tr><td align="left">')
320            self.visit_store(s.store)
321
322        self._dump('</td></tr><hr />'
323                   '<tr><td align="left">'
324                   '<b>Environment: </b>')
325        if s.environment is None:
326            self._dump('<i> Nothing!</i>')
327        else:
328            self._dump('</td></tr>'
329                       '<tr><td align="left">')
330            self.visit_environment(s.environment)
331
332        self._dump('</td></tr>')
333
334    def visit_node(self, node):
335        self._dump('%s [shape=record,label=<<table border="0">'
336                   % (node.node_name()))
337
338        self._dump('<tr><td bgcolor="grey"><b>Node %d (%s) - '
339                   'State %s</b></td></tr>'
340                   % (node.node_id, node.ptr, node.state.state_id
341                      if node.state is not None else 'Unspecified'))
342        self._dump('<tr><td align="left" width="0">')
343        if len(node.points) > 1:
344            self._dump('<b>Program points:</b></td></tr>')
345        else:
346            self._dump('<b>Program point:</b></td></tr>')
347        self._dump('<tr><td align="left" width="0">'
348                   '<table border="0" align="left" width="0">')
349        for p in node.points:
350            self.visit_program_point(p)
351        self._dump('</table></td></tr>')
352
353        if node.state is not None:
354            self._dump('<hr />')
355            self.visit_state(node.state)
356        self._dump_raw('</table>>];\n')
357
358    def visit_edge(self, pred, succ):
359        self._dump_raw('%s -> %s;\n' % (pred.node_name(), succ.node_name()))
360
361    def visit_end_of_graph(self):
362        self._dump_raw('}\n')
363
364
365# A class that encapsulates traversal of the ExplodedGraph. Different explorer
366# kinds could potentially traverse specific sub-graphs.
367class Explorer(object):
368    def __init__(self):
369        super(Explorer, self).__init__()
370
371    def explore(self, graph, visitor):
372        visitor.visit_begin_graph(graph)
373        for node in sorted(graph.nodes):
374            logging.debug('Visiting ' + node)
375            visitor.visit_node(graph.nodes[node])
376            for succ in sorted(graph.nodes[node].successors):
377                logging.debug('Visiting edge: %s -> %s ' % (node, succ))
378                visitor.visit_edge(graph.nodes[node], graph.nodes[succ])
379        visitor.visit_end_of_graph()
380
381
382def main():
383    parser = argparse.ArgumentParser()
384    parser.add_argument('filename', type=str)
385    parser.add_argument('-d', '--debug', action='store_const', dest='loglevel',
386                        const=logging.DEBUG, default=logging.WARNING,
387                        help='enable debug prints')
388    parser.add_argument('-v', '--verbose', action='store_const',
389                        dest='loglevel', const=logging.INFO,
390                        default=logging.WARNING,
391                        help='enable info prints')
392    args = parser.parse_args()
393    logging.basicConfig(level=args.loglevel)
394
395    graph = ExplodedGraph()
396    with open(args.filename) as fd:
397        for raw_line in fd:
398            raw_line = raw_line.strip()
399            graph.add_raw_line(raw_line)
400
401    explorer = Explorer()
402    visitor = DotDumpVisitor()
403    explorer.explore(graph, visitor)
404
405
406if __name__ == '__main__':
407    main()
408