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