xref: /llvm-project/clang/utils/analyzer/exploded-graph-rewriter.py (revision fc6059e8b987489579b40f32ba50cfdb1cb0cb2d)
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 difflib
17import json
18import logging
19import os
20import re
21
22
23#===-----------------------------------------------------------------------===#
24# These data structures represent a deserialized ExplodedGraph.
25#===-----------------------------------------------------------------------===#
26
27
28# A helper function for finding the difference between two dictionaries.
29def diff_dicts(curr, prev):
30    removed = [k for k in prev if k not in curr or curr[k] != prev[k]]
31    added = [k for k in curr if k not in prev or curr[k] != prev[k]]
32    return (removed, added)
33
34
35# Represents any program state trait that is a dictionary of key-value pairs.
36class GenericMap(object):
37    def __init__(self, items):
38        self.generic_map = collections.OrderedDict(items)
39
40    def diff(self, prev):
41        return diff_dicts(self.generic_map, prev.generic_map)
42
43    def is_different(self, prev):
44        removed, added = self.diff(prev)
45        return len(removed) != 0 or len(added) != 0
46
47
48# A deserialized source location.
49class SourceLocation(object):
50    def __init__(self, json_loc):
51        super(SourceLocation, self).__init__()
52        self.line = json_loc['line']
53        self.col = json_loc['column']
54        self.filename = os.path.basename(json_loc['file']) \
55            if 'file' in json_loc else '(main file)'
56
57
58# A deserialized program point.
59class ProgramPoint(object):
60    def __init__(self, json_pp):
61        super(ProgramPoint, self).__init__()
62        self.kind = json_pp['kind']
63        self.tag = json_pp['tag']
64        if self.kind == 'Edge':
65            self.src_id = json_pp['src_id']
66            self.dst_id = json_pp['dst_id']
67        elif self.kind == 'Statement':
68            self.stmt_kind = json_pp['stmt_kind']
69            self.stmt_point_kind = json_pp['stmt_point_kind']
70            self.pointer = json_pp['pointer']
71            self.pretty = json_pp['pretty']
72            self.loc = SourceLocation(json_pp['location']) \
73                if json_pp['location'] is not None else None
74        elif self.kind == 'BlockEntrance':
75            self.block_id = json_pp['block_id']
76
77
78# A single expression acting as a key in a deserialized Environment.
79class EnvironmentBindingKey(object):
80    def __init__(self, json_ek):
81        super(EnvironmentBindingKey, self).__init__()
82        # CXXCtorInitializer is not a Stmt!
83        self.stmt_id = json_ek['stmt_id'] if 'stmt_id' in json_ek \
84            else json_ek['init_id']
85        self.pretty = json_ek['pretty']
86        self.kind = json_ek['kind'] if 'kind' in json_ek else None
87
88    def _key(self):
89        return self.stmt_id
90
91    def __eq__(self, other):
92        return self._key() == other._key()
93
94    def __hash__(self):
95        return hash(self._key())
96
97
98# Deserialized description of a location context.
99class LocationContext(object):
100    def __init__(self, json_frame):
101        super(LocationContext, self).__init__()
102        self.lctx_id = json_frame['lctx_id']
103        self.caption = json_frame['location_context']
104        self.decl = json_frame['calling']
105        self.line = json_frame['call_line']
106
107    def _key(self):
108        return self.lctx_id
109
110    def __eq__(self, other):
111        return self._key() == other._key()
112
113    def __hash__(self):
114        return hash(self._key())
115
116
117# A group of deserialized Environment bindings that correspond to a specific
118# location context.
119class EnvironmentFrame(object):
120    def __init__(self, json_frame):
121        super(EnvironmentFrame, self).__init__()
122        self.location_context = LocationContext(json_frame)
123        self.bindings = collections.OrderedDict(
124            [(EnvironmentBindingKey(b),
125              b['value']) for b in json_frame['items']]
126            if json_frame['items'] is not None else [])
127
128    def diff_bindings(self, prev):
129        return diff_dicts(self.bindings, prev.bindings)
130
131    def is_different(self, prev):
132        removed, added = self.diff_bindings(prev)
133        return len(removed) != 0 or len(added) != 0
134
135
136# A deserialized Environment. This class can also hold other entities that
137# are similar to Environment, such as Objects Under Construction.
138class GenericEnvironment(object):
139    def __init__(self, json_e):
140        super(GenericEnvironment, self).__init__()
141        self.frames = [EnvironmentFrame(f) for f in json_e]
142
143    def diff_frames(self, prev):
144        # TODO: It's difficult to display a good diff when frame numbers shift.
145        if len(self.frames) != len(prev.frames):
146            return None
147
148        updated = []
149        for i in range(len(self.frames)):
150            f = self.frames[i]
151            prev_f = prev.frames[i]
152            if f.location_context == prev_f.location_context:
153                if f.is_different(prev_f):
154                    updated.append(i)
155            else:
156                # We have the whole frame replaced with another frame.
157                # TODO: Produce a nice diff.
158                return None
159
160        # TODO: Add support for added/removed.
161        return updated
162
163    def is_different(self, prev):
164        updated = self.diff_frames(prev)
165        return updated is None or len(updated) > 0
166
167
168# A single binding key in a deserialized RegionStore cluster.
169class StoreBindingKey(object):
170    def __init__(self, json_sk):
171        super(StoreBindingKey, self).__init__()
172        self.kind = json_sk['kind']
173        self.offset = json_sk['offset']
174
175    def _key(self):
176        return (self.kind, self.offset)
177
178    def __eq__(self, other):
179        return self._key() == other._key()
180
181    def __hash__(self):
182        return hash(self._key())
183
184
185# A single cluster of the deserialized RegionStore.
186class StoreCluster(object):
187    def __init__(self, json_sc):
188        super(StoreCluster, self).__init__()
189        self.base_region = json_sc['cluster']
190        self.bindings = collections.OrderedDict(
191            [(StoreBindingKey(b), b['value']) for b in json_sc['items']])
192
193    def diff_bindings(self, prev):
194        return diff_dicts(self.bindings, prev.bindings)
195
196    def is_different(self, prev):
197        removed, added = self.diff_bindings(prev)
198        return len(removed) != 0 or len(added) != 0
199
200
201# A deserialized RegionStore.
202class Store(object):
203    def __init__(self, json_s):
204        super(Store, self).__init__()
205        self.ptr = json_s['pointer']
206        self.clusters = collections.OrderedDict(
207            [(c['pointer'], StoreCluster(c)) for c in json_s['items']])
208
209    def diff_clusters(self, prev):
210        removed = [k for k in prev.clusters if k not in self.clusters]
211        added = [k for k in self.clusters if k not in prev.clusters]
212        updated = [k for k in prev.clusters if k in self.clusters
213                   and prev.clusters[k].is_different(self.clusters[k])]
214        return (removed, added, updated)
215
216    def is_different(self, prev):
217        removed, added, updated = self.diff_clusters(prev)
218        return len(removed) != 0 or len(added) != 0 or len(updated) != 0
219
220
221# Deserialized messages from a single checker in a single program state.
222# Basically a list of raw strings.
223class CheckerLines(object):
224    def __init__(self, json_lines):
225        super(CheckerLines, self).__init__()
226        self.lines = json_lines
227
228    def diff_lines(self, prev):
229        lines = difflib.ndiff(prev.lines, self.lines)
230        return [l.strip() for l in lines
231                if l.startswith('+') or l.startswith('-')]
232
233    def is_different(self, prev):
234        return len(self.diff_lines(prev)) > 0
235
236
237# Deserialized messages of all checkers, separated by checker.
238class CheckerMessages(object):
239    def __init__(self, json_m):
240        super(CheckerMessages, self).__init__()
241        self.items = collections.OrderedDict(
242            [(m['checker'], CheckerLines(m['messages'])) for m in json_m])
243
244    def diff_messages(self, prev):
245        removed = [k for k in prev.items if k not in self.items]
246        added = [k for k in self.items if k not in prev.items]
247        updated = [k for k in prev.items if k in self.items
248                   and prev.items[k].is_different(self.items[k])]
249        return (removed, added, updated)
250
251    def is_different(self, prev):
252        removed, added, updated = self.diff_messages(prev)
253        return len(removed) != 0 or len(added) != 0 or len(updated) != 0
254
255
256# A deserialized program state.
257class ProgramState(object):
258    def __init__(self, state_id, json_ps):
259        super(ProgramState, self).__init__()
260        logging.debug('Adding ProgramState ' + str(state_id))
261
262        self.state_id = state_id
263
264        self.store = Store(json_ps['store']) \
265            if json_ps['store'] is not None else None
266
267        self.environment = \
268            GenericEnvironment(json_ps['environment']['items']) \
269            if json_ps['environment'] is not None else None
270
271        self.constraints = GenericMap([
272            (c['symbol'], c['range']) for c in json_ps['constraints']
273        ]) if json_ps['constraints'] is not None else None
274
275        self.dynamic_types = GenericMap([
276                (t['region'], '%s%s' % (t['dyn_type'],
277                                        ' (or a sub-class)'
278                                        if t['sub_classable'] else ''))
279                for t in json_ps['dynamic_types']]) \
280            if json_ps['dynamic_types'] is not None else None
281
282        self.constructing_objects = \
283            GenericEnvironment(json_ps['constructing_objects']) \
284            if json_ps['constructing_objects'] is not None else None
285
286        self.checker_messages = CheckerMessages(json_ps['checker_messages']) \
287            if json_ps['checker_messages'] is not None else None
288
289
290# A deserialized exploded graph node. Has a default constructor because it
291# may be referenced as part of an edge before its contents are deserialized,
292# and in this moment we already need a room for predecessors and successors.
293class ExplodedNode(object):
294    def __init__(self):
295        super(ExplodedNode, self).__init__()
296        self.predecessors = []
297        self.successors = []
298
299    def construct(self, node_id, json_node):
300        logging.debug('Adding ' + node_id)
301        self.node_id = json_node['node_id']
302        self.ptr = json_node['pointer']
303        self.has_report = json_node['has_report']
304        self.is_sink = json_node['is_sink']
305        self.points = [ProgramPoint(p) for p in json_node['program_points']]
306        self.state = ProgramState(json_node['state_id'],
307                                  json_node['program_state']) \
308            if json_node['program_state'] is not None else None
309
310        assert self.node_name() == node_id
311
312    def node_name(self):
313        return 'Node' + self.ptr
314
315
316# A deserialized ExplodedGraph. Constructed by consuming a .dot file
317# line-by-line.
318class ExplodedGraph(object):
319    # Parse .dot files with regular expressions.
320    node_re = re.compile(
321        '^(Node0x[0-9a-f]*) \\[shape=record,.*label="{(.*)\\\\l}"\\];$')
322    edge_re = re.compile(
323        '^(Node0x[0-9a-f]*) -> (Node0x[0-9a-f]*);$')
324
325    def __init__(self):
326        super(ExplodedGraph, self).__init__()
327        self.nodes = collections.defaultdict(ExplodedNode)
328        self.root_id = None
329        self.incomplete_line = ''
330
331    def add_raw_line(self, raw_line):
332        if raw_line.startswith('//'):
333            return
334
335        # Allow line breaks by waiting for ';'. This is not valid in
336        # a .dot file, but it is useful for writing tests.
337        if len(raw_line) > 0 and raw_line[-1] != ';':
338            self.incomplete_line += raw_line
339            return
340        raw_line = self.incomplete_line + raw_line
341        self.incomplete_line = ''
342
343        # Apply regexps one by one to see if it's a node or an edge
344        # and extract contents if necessary.
345        logging.debug('Line: ' + raw_line)
346        result = self.edge_re.match(raw_line)
347        if result is not None:
348            logging.debug('Classified as edge line.')
349            pred = result.group(1)
350            succ = result.group(2)
351            self.nodes[pred].successors.append(succ)
352            self.nodes[succ].predecessors.append(pred)
353            return
354        result = self.node_re.match(raw_line)
355        if result is not None:
356            logging.debug('Classified as node line.')
357            node_id = result.group(1)
358            if len(self.nodes) == 0:
359                self.root_id = node_id
360            # Note: when writing tests you don't need to escape everything,
361            # even though in a valid dot file everything is escaped.
362            node_label = result.group(2).replace('\\l', '') \
363                                        .replace(' ', '') \
364                                        .replace('\\"', '"') \
365                                        .replace('\\{', '{') \
366                                        .replace('\\}', '}') \
367                                        .replace('\\\\', '\\') \
368                                        .replace('\\|', '|') \
369                                        .replace('\\<', '\\\\<') \
370                                        .replace('\\>', '\\\\>') \
371                                        .rstrip(',')
372            logging.debug(node_label)
373            json_node = json.loads(node_label)
374            self.nodes[node_id].construct(node_id, json_node)
375            return
376        logging.debug('Skipping.')
377
378
379#===-----------------------------------------------------------------------===#
380# Visitors traverse a deserialized ExplodedGraph and do different things
381# with every node and edge.
382#===-----------------------------------------------------------------------===#
383
384
385# A visitor that dumps the ExplodedGraph into a DOT file with fancy HTML-based
386# syntax highlighing.
387class DotDumpVisitor(object):
388    def __init__(self, do_diffs, dark_mode, gray_mode, topo_mode):
389        super(DotDumpVisitor, self).__init__()
390        self._do_diffs = do_diffs
391        self._dark_mode = dark_mode
392        self._gray_mode = gray_mode
393        self._topo_mode = topo_mode
394
395    @staticmethod
396    def _dump_raw(s):
397        print(s, end='')
398
399    def _dump(self, s):
400        s = s.replace('&', '&amp;') \
401             .replace('{', '\\{') \
402             .replace('}', '\\}') \
403             .replace('\\<', '&lt;') \
404             .replace('\\>', '&gt;') \
405             .replace('\\l', '<br />') \
406             .replace('|', '\\|')
407        if self._gray_mode:
408            s = re.sub(r'<font color="[a-z0-9]*">', '', s)
409            s = re.sub(r'</font>', '', s)
410        self._dump_raw(s)
411
412    @staticmethod
413    def _diff_plus_minus(is_added):
414        if is_added is None:
415            return ''
416        if is_added:
417            return '<font color="forestgreen">+</font>'
418        return '<font color="red">-</font>'
419
420    @staticmethod
421    def _short_pretty(s):
422        if s is None:
423            return None
424        if len(s) < 20:
425            return s
426        left = s.find('{')
427        right = s.rfind('}')
428        if left == -1 or right == -1 or left >= right:
429            return s
430        candidate = s[0:left + 1] + ' ... ' + s[right:]
431        if len(candidate) >= len(s):
432            return s
433        return candidate
434
435    def visit_begin_graph(self, graph):
436        self._graph = graph
437        self._dump_raw('digraph "ExplodedGraph" {\n')
438        if self._dark_mode:
439            self._dump_raw('bgcolor="gray10";\n')
440        self._dump_raw('label="";\n')
441
442    def visit_program_point(self, p):
443        if p.kind in ['Edge', 'BlockEntrance', 'BlockExit']:
444            color = 'gold3'
445        elif p.kind in ['PreStmtPurgeDeadSymbols',
446                        'PostStmtPurgeDeadSymbols']:
447            color = 'red'
448        elif p.kind in ['CallEnter', 'CallExitBegin', 'CallExitEnd']:
449            color = 'dodgerblue' if self._dark_mode else 'blue'
450        elif p.kind in ['Statement']:
451            color = 'cyan4'
452        else:
453            color = 'forestgreen'
454
455        if p.kind == 'Statement':
456            # This avoids pretty-printing huge statements such as CompoundStmt.
457            # Such statements show up only at [Pre|Post]StmtPurgeDeadSymbols
458            skip_pretty = 'PurgeDeadSymbols' in p.stmt_point_kind
459            stmt_color = 'cyan3'
460            if p.loc is not None:
461                self._dump('<tr><td align="left" width="0">'
462                           '%s:<b>%s</b>:<b>%s</b>:</td>'
463                           '<td align="left" width="0"><font color="%s">'
464                           '%s</font></td>'
465                           '<td align="left"><font color="%s">%s</font></td>'
466                           '<td>%s</td></tr>'
467                           % (p.loc.filename, p.loc.line,
468                              p.loc.col, color, p.stmt_kind,
469                              stmt_color, p.stmt_point_kind,
470                              self._short_pretty(p.pretty)
471                              if not skip_pretty else ''))
472            else:
473                self._dump('<tr><td align="left" width="0">'
474                           '<i>Invalid Source Location</i>:</td>'
475                           '<td align="left" width="0">'
476                           '<font color="%s">%s</font></td>'
477                           '<td align="left"><font color="%s">%s</font></td>'
478                           '<td>%s</td></tr>'
479                           % (color, p.stmt_kind,
480                              stmt_color, p.stmt_point_kind,
481                              self._short_pretty(p.pretty)
482                              if not skip_pretty else ''))
483        elif p.kind == 'Edge':
484            self._dump('<tr><td width="0"></td>'
485                       '<td align="left" width="0">'
486                       '<font color="%s">%s</font></td><td align="left">'
487                       '[B%d] -\\> [B%d]</td></tr>'
488                       % (color, 'BlockEdge', p.src_id, p.dst_id))
489        elif p.kind == 'BlockEntrance':
490            self._dump('<tr><td width="0"></td>'
491                       '<td align="left" width="0">'
492                       '<font color="%s">%s</font></td>'
493                       '<td align="left">[B%d]</td></tr>'
494                       % (color, p.kind, p.block_id))
495        else:
496            # TODO: Print more stuff for other kinds of points.
497            self._dump('<tr><td width="0"></td>'
498                       '<td align="left" width="0" colspan="2">'
499                       '<font color="%s">%s</font></td></tr>'
500                       % (color, p.kind))
501
502        if p.tag is not None:
503            self._dump('<tr><td width="0"></td>'
504                       '<td colspan="3" align="left">'
505                       '<b>Tag: </b> <font color="crimson">'
506                       '%s</font></td></tr>' % p.tag)
507
508    def visit_environment(self, e, prev_e=None):
509        self._dump('<table border="0">')
510
511        def dump_location_context(lc, is_added=None):
512            self._dump('<tr><td>%s</td>'
513                       '<td align="left"><b>%s</b></td>'
514                       '<td align="left" colspan="2">'
515                       '<font color="gray60">%s </font>'
516                       '%s</td></tr>'
517                       % (self._diff_plus_minus(is_added),
518                          lc.caption, lc.decl,
519                          ('(line %s)' % lc.line) if lc.line is not None
520                          else ''))
521
522        def dump_binding(f, b, is_added=None):
523            self._dump('<tr><td>%s</td>'
524                       '<td align="left"><i>S%s</i></td>'
525                       '%s'
526                       '<td align="left">%s</td>'
527                       '<td align="left">%s</td></tr>'
528                       % (self._diff_plus_minus(is_added),
529                          b.stmt_id,
530                          '<td align="left"><font color="%s"><i>'
531                          '%s</i></font></td>' % (
532                              'lavender' if self._dark_mode else 'darkgreen',
533                              ('(%s)' % b.kind) if b.kind is not None else ' '
534                          ),
535                          self._short_pretty(b.pretty), f.bindings[b]))
536
537        frames_updated = e.diff_frames(prev_e) if prev_e is not None else None
538        if frames_updated:
539            for i in frames_updated:
540                f = e.frames[i]
541                prev_f = prev_e.frames[i]
542                dump_location_context(f.location_context)
543                bindings_removed, bindings_added = f.diff_bindings(prev_f)
544                for b in bindings_removed:
545                    dump_binding(prev_f, b, False)
546                for b in bindings_added:
547                    dump_binding(f, b, True)
548        else:
549            for f in e.frames:
550                dump_location_context(f.location_context)
551                for b in f.bindings:
552                    dump_binding(f, b)
553
554        self._dump('</table>')
555
556    def visit_environment_in_state(self, selector, title, s, prev_s=None):
557        e = getattr(s, selector)
558        prev_e = getattr(prev_s, selector) if prev_s is not None else None
559        if e is None and prev_e is None:
560            return
561
562        self._dump('<hr /><tr><td align="left"><b>%s: </b>' % title)
563        if e is None:
564            self._dump('<i> Nothing!</i>')
565        else:
566            if prev_e is not None:
567                if e.is_different(prev_e):
568                    self._dump('</td></tr><tr><td align="left">')
569                    self.visit_environment(e, prev_e)
570                else:
571                    self._dump('<i> No changes!</i>')
572            else:
573                self._dump('</td></tr><tr><td align="left">')
574                self.visit_environment(e)
575
576        self._dump('</td></tr>')
577
578    def visit_store(self, s, prev_s=None):
579        self._dump('<table border="0">')
580
581        def dump_binding(s, c, b, is_added=None):
582            self._dump('<tr><td>%s</td>'
583                       '<td align="left">%s</td>'
584                       '<td align="left">%s</td>'
585                       '<td align="left">%s</td>'
586                       '<td align="left">%s</td></tr>'
587                       % (self._diff_plus_minus(is_added),
588                          s.clusters[c].base_region, b.offset,
589                          '(<i>Default</i>)' if b.kind == 'Default'
590                          else '',
591                          s.clusters[c].bindings[b]))
592
593        if prev_s is not None:
594            clusters_removed, clusters_added, clusters_updated = \
595                s.diff_clusters(prev_s)
596            for c in clusters_removed:
597                for b in prev_s.clusters[c].bindings:
598                    dump_binding(prev_s, c, b, False)
599            for c in clusters_updated:
600                bindings_removed, bindings_added = \
601                    s.clusters[c].diff_bindings(prev_s.clusters[c])
602                for b in bindings_removed:
603                    dump_binding(prev_s, c, b, False)
604                for b in bindings_added:
605                    dump_binding(s, c, b, True)
606            for c in clusters_added:
607                for b in s.clusters[c].bindings:
608                    dump_binding(s, c, b, True)
609        else:
610            for c in s.clusters:
611                for b in s.clusters[c].bindings:
612                    dump_binding(s, c, b)
613
614        self._dump('</table>')
615
616    def visit_store_in_state(self, s, prev_s=None):
617        st = s.store
618        prev_st = prev_s.store if prev_s is not None else None
619        if st is None and prev_st is None:
620            return
621
622        self._dump('<hr /><tr><td align="left"><b>Store: </b>')
623        if st is None:
624            self._dump('<i> Nothing!</i>')
625        else:
626            if prev_st is not None:
627                if s.store.is_different(prev_st):
628                    self._dump('</td></tr><tr><td align="left">')
629                    self.visit_store(st, prev_st)
630                else:
631                    self._dump('<i> No changes!</i>')
632            else:
633                self._dump('</td></tr><tr><td align="left">')
634                self.visit_store(st)
635        self._dump('</td></tr>')
636
637    def visit_generic_map(self, m, prev_m=None):
638        self._dump('<table border="0">')
639
640        def dump_pair(m, k, is_added=None):
641            self._dump('<tr><td>%s</td>'
642                       '<td align="left">%s</td>'
643                       '<td align="left">%s</td></tr>'
644                       % (self._diff_plus_minus(is_added),
645                          k, m.generic_map[k]))
646
647        if prev_m is not None:
648            removed, added = m.diff(prev_m)
649            for k in removed:
650                dump_pair(prev_m, k, False)
651            for k in added:
652                dump_pair(m, k, True)
653        else:
654            for k in m.generic_map:
655                dump_pair(m, k, None)
656
657        self._dump('</table>')
658
659    def visit_generic_map_in_state(self, selector, title, s, prev_s=None):
660        m = getattr(s, selector)
661        prev_m = getattr(prev_s, selector) if prev_s is not None else None
662        if m is None and prev_m is None:
663            return
664
665        self._dump('<hr />')
666        self._dump('<tr><td align="left">'
667                   '<b>%s: </b>' % title)
668        if m is None:
669            self._dump('<i> Nothing!</i>')
670        else:
671            if prev_m is not None:
672                if m.is_different(prev_m):
673                    self._dump('</td></tr><tr><td align="left">')
674                    self.visit_generic_map(m, prev_m)
675                else:
676                    self._dump('<i> No changes!</i>')
677            else:
678                self._dump('</td></tr><tr><td align="left">')
679                self.visit_generic_map(m)
680
681        self._dump('</td></tr>')
682
683    def visit_checker_messages(self, m, prev_m=None):
684        self._dump('<table border="0">')
685
686        def dump_line(l, is_added=None):
687            self._dump('<tr><td>%s</td>'
688                       '<td align="left">%s</td></tr>'
689                       % (self._diff_plus_minus(is_added), l))
690
691        def dump_chk(chk, is_added=None):
692            dump_line('<i>%s</i>:' % chk, is_added)
693
694        if prev_m is not None:
695            removed, added, updated = m.diff_messages(prev_m)
696            for chk in removed:
697                dump_chk(chk, False)
698                for l in prev_m.items[chk].lines:
699                    dump_line(l, False)
700            for chk in updated:
701                dump_chk(chk)
702                for l in m.items[chk].diff_lines(prev_m.items[chk]):
703                    dump_line(l[1:], l.startswith('+'))
704            for chk in added:
705                dump_chk(chk, True)
706                for l in m.items[chk].lines:
707                    dump_line(l, True)
708        else:
709            for chk in m.items:
710                dump_chk(chk)
711                for l in m.items[chk].lines:
712                    dump_line(l)
713
714        self._dump('</table>')
715
716    def visit_checker_messages_in_state(self, s, prev_s=None):
717        m = s.checker_messages
718        prev_m = prev_s.checker_messages if prev_s is not None else None
719        if m is None and prev_m is None:
720            return
721
722        self._dump('<hr />')
723        self._dump('<tr><td align="left">'
724                   '<b>Checker State: </b>')
725        if m is None:
726            self._dump('<i> Nothing!</i>')
727        else:
728            if prev_m is not None:
729                if m.is_different(prev_m):
730                    self._dump('</td></tr><tr><td align="left">')
731                    self.visit_checker_messages(m, prev_m)
732                else:
733                    self._dump('<i> No changes!</i>')
734            else:
735                self._dump('</td></tr><tr><td align="left">')
736                self.visit_checker_messages(m)
737
738        self._dump('</td></tr>')
739
740    def visit_state(self, s, prev_s):
741        self.visit_store_in_state(s, prev_s)
742        self.visit_environment_in_state('environment', 'Environment',
743                                        s, prev_s)
744        self.visit_generic_map_in_state('constraints', 'Ranges',
745                                        s, prev_s)
746        self.visit_generic_map_in_state('dynamic_types', 'Dynamic Types',
747                                        s, prev_s)
748        self.visit_environment_in_state('constructing_objects',
749                                        'Objects Under Construction',
750                                        s, prev_s)
751        self.visit_checker_messages_in_state(s, prev_s)
752
753    def visit_node(self, node):
754        self._dump('%s [shape=record,'
755                   % (node.node_name()))
756        if self._dark_mode:
757            self._dump('color="white",fontcolor="gray80",')
758        self._dump('label=<<table border="0">')
759
760        self._dump('<tr><td bgcolor="%s"><b>Node %d (%s) - '
761                   'State %s</b></td></tr>'
762                   % ("gray20" if self._dark_mode else "gray",
763                      node.node_id, node.ptr, node.state.state_id
764                      if node.state is not None else 'Unspecified'))
765        if node.has_report:
766            self._dump('<tr><td><font color="red"><b>Bug Report Attached'
767                       '</b></font></td></tr>')
768        if node.is_sink:
769            self._dump('<tr><td><font color="cornflowerblue"><b>Sink Node'
770                       '</b></font></td></tr>')
771        if not self._topo_mode:
772            self._dump('<tr><td align="left" width="0">')
773            if len(node.points) > 1:
774                self._dump('<b>Program points:</b></td></tr>')
775            else:
776                self._dump('<b>Program point:</b></td></tr>')
777        self._dump('<tr><td align="left" width="0">'
778                   '<table border="0" align="left" width="0">')
779        for p in node.points:
780            self.visit_program_point(p)
781        self._dump('</table></td></tr>')
782
783        if node.state is not None and not self._topo_mode:
784            prev_s = None
785            # Do diffs only when we have a unique predecessor.
786            # Don't do diffs on the leaf nodes because they're
787            # the important ones.
788            if self._do_diffs and len(node.predecessors) == 1 \
789               and len(node.successors) > 0:
790                prev_s = self._graph.nodes[node.predecessors[0]].state
791            self.visit_state(node.state, prev_s)
792        self._dump_raw('</table>>];\n')
793
794    def visit_edge(self, pred, succ):
795        self._dump_raw('%s -> %s%s;\n' % (
796            pred.node_name(), succ.node_name(),
797            ' [color="white"]' if self._dark_mode else ''
798        ))
799
800    def visit_end_of_graph(self):
801        self._dump_raw('}\n')
802
803
804#===-----------------------------------------------------------------------===#
805# Explorers know how to traverse the ExplodedGraph in a certain order.
806# They would invoke a Visitor on every node or edge they encounter.
807#===-----------------------------------------------------------------------===#
808
809
810# BasicExplorer explores the whole graph in no particular order.
811class BasicExplorer(object):
812    def __init__(self):
813        super(BasicExplorer, self).__init__()
814
815    def explore(self, graph, visitor):
816        visitor.visit_begin_graph(graph)
817        for node in sorted(graph.nodes):
818            logging.debug('Visiting ' + node)
819            visitor.visit_node(graph.nodes[node])
820            for succ in sorted(graph.nodes[node].successors):
821                logging.debug('Visiting edge: %s -> %s ' % (node, succ))
822                visitor.visit_edge(graph.nodes[node], graph.nodes[succ])
823        visitor.visit_end_of_graph()
824
825
826# SinglePathExplorer traverses only a single path - the leftmost path
827# from the root. Useful when the trimmed graph is still too large
828# due to a large amount of equivalent reports.
829class SinglePathExplorer(object):
830    def __init__(self):
831        super(SinglePathExplorer, self).__init__()
832
833    def explore(self, graph, visitor):
834        visitor.visit_begin_graph(graph)
835
836        # Keep track of visited nodes in order to avoid loops.
837        visited = set()
838        node_id = graph.root_id
839        while True:
840            visited.add(node_id)
841            node = graph.nodes[node_id]
842            logging.debug('Visiting ' + node_id)
843            visitor.visit_node(node)
844            if len(node.successors) == 0:
845                break
846
847            succ_id = node.successors[0]
848            succ = graph.nodes[succ_id]
849            logging.debug('Visiting edge: %s -> %s ' % (node_id, succ_id))
850            visitor.visit_edge(node, succ)
851            if succ_id in visited:
852                break
853
854            node_id = succ_id
855
856        visitor.visit_end_of_graph()
857
858
859#===-----------------------------------------------------------------------===#
860# The entry point to the script.
861#===-----------------------------------------------------------------------===#
862
863
864def main():
865    parser = argparse.ArgumentParser()
866    parser.add_argument('filename', type=str)
867    parser.add_argument('-v', '--verbose', action='store_const',
868                        dest='loglevel', const=logging.DEBUG,
869                        default=logging.WARNING,
870                        help='enable info prints')
871    parser.add_argument('-d', '--diff', action='store_const', dest='diff',
872                        const=True, default=False,
873                        help='display differences between states')
874    parser.add_argument('-t', '--topology', action='store_const',
875                        dest='topology', const=True, default=False,
876                        help='only display program points, omit states')
877    parser.add_argument('-s', '--single-path', action='store_const',
878                        dest='single_path', const=True, default=False,
879                        help='only display the leftmost path in the graph '
880                             '(useful for trimmed graphs that still '
881                             'branch too much)')
882    parser.add_argument('--dark', action='store_const', dest='dark',
883                        const=True, default=False,
884                        help='dark mode')
885    parser.add_argument('--gray', action='store_const', dest='gray',
886                        const=True, default=False,
887                        help='black-and-white mode')
888    args = parser.parse_args()
889    logging.basicConfig(level=args.loglevel)
890
891    graph = ExplodedGraph()
892    with open(args.filename) as fd:
893        for raw_line in fd:
894            raw_line = raw_line.strip()
895            graph.add_raw_line(raw_line)
896
897    explorer = SinglePathExplorer() if args.single_path else BasicExplorer()
898    visitor = DotDumpVisitor(args.diff, args.dark, args.gray, args.topology)
899
900    explorer.explore(graph, visitor)
901
902
903if __name__ == '__main__':
904    main()
905