/
incdeps.py
executable file
·146 lines (111 loc) · 3.9 KB
/
incdeps.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
#!/usr/bin/env python
# encoding: utf-8
# Copyright (C) 2016 John Törnblom
"""
A simple command line tool for dumping a Graphviz description (dot) that
describes include dependencies. Heavily inspired by cindex-includes.py
disitributed with the clang source code.
"""
import ctypes.util
import logging
import sys
import os
from clang.cindex import Index, Config
for libname in ['clang', 'clang-4.0', 'clang-3.9', 'clang-3.8', 'clang-3.7']:
filename = ctypes.util.find_library(libname)
if filename:
Config.set_library_file(filename)
break
logger = logging.getLogger('incdeps')
def emit_node(out, uid, label):
out.write(' "%s" [label="%s"];\n' % (uid, label))
def emit_cluster(out, children, cluster_path='', parent_path=''):
prefix = ' ' * (cluster_path.count(os.path.sep) - 1)
nodes = list()
clusters = dict()
for path, item in children.items():
if isinstance(item, str):
nodes.append(path)
else:
clusters[path] = item
if nodes or len(clusters) > 1:
label = cluster_path.replace(parent_path, '', 1)
parent_path = cluster_path + os.path.sep
out.write(prefix + 'subgraph "cluster %s" {\n' % cluster_path)
out.write(prefix + ' label="%s";\n' % label)
out.write(prefix + ' style=filled;\n')
out.write(prefix + ' color=black;\n')
out.write(prefix + ' fillcolor="#CCCCCC30";\n')
for node in nodes:
out.write(prefix)
emit_node(out, node, os.path.basename(node))
for path, item in clusters.items():
emit_cluster(out, item, path, parent_path)
if nodes or len(clusters) > 1:
out.write(prefix)
out.write("}\n")
def emit_edge(out, source_uid, target_uid):
out.write(' "%s" -> "%s";\n' % (source_uid, target_uid))
def emit_graph(out, tu, once=False):
out.write('digraph {\n')
out.write(' rankdir=LR;\n')
out.write(' node [shape=box, style=rounded];\n')
cluster_root = dict()
nodes = set()
def add_to_cluster(filename):
dirname = os.path.dirname(filename)
cluster = cluster_root
path = ''
for name in dirname.split(os.path.sep):
if path != os.path.sep:
path += os.path.sep
path += name
if path not in cluster:
cluster[path] = dict()
cluster = cluster[path]
cluster[filename] = filename
add_to_cluster(os.path.abspath(tu.spelling))
for i in tu.get_includes():
source = os.path.abspath(i.source.name)
target = os.path.abspath(i.include.name)
if target not in nodes:
add_to_cluster(source)
add_to_cluster(target)
emit_edge(out, source, target)
if once:
nodes.add(target)
emit_cluster(out, cluster_root)
out.write("}\n")
def main():
if len(sys.argv) < 2:
sys.stderr.write('missing arguments (see -h for help)\n')
sys.exit(1)
if '-h' in sys.argv:
sys.stderr.write('%s\n\n' % __doc__.strip())
sys.stderr.write('usage example: %s /usr/include/stdio.h | dot -Txlib\n' % sys.argv[0])
sys.exit(1)
logging.basicConfig(level=logging.INFO)
index = Index.create()
tu = index.parse(None, sys.argv[1:])
rc = 0
if tu:
emit_graph(sys.stdout, tu)
log_map = {
0: logger.debug,
1: logger.info,
2: logger.warning,
3: logger.error,
4: logger.fatal
}
rc = 0
for d in tu.diagnostics:
rc = max(d.severity, rc)
log = log_map[d.severity]
log('in file %s, line %d:%s' % (d.location.file.name,
d.location.line,
d.spelling))
else:
rc = 1
sys.exit(rc)
if __name__ == '__main__':
main()