Debian Codegraph @

Tools/find-includes-cycles.py

raw symbols SCIP
1 #! /usr/bin/env python
2
3 '''
4 Run this script from Source/Core/ to find all the #include cycles.
5 '''
6
7 import subprocess
8
9 def get_local_includes_for(path):
10 lines = open(path).read().split('\n')
11 includes = [l.strip() for l in lines if l.strip().startswith('#include')]
12 return [i.split()[1][1:-1] for i in includes if '"' in i.split()[1]]
13
14 def find_all_files():
15 '''Could probably use os.walk, but meh.'''
16 f = subprocess.check_output(['find', '.', '-name', '*.h'],
17 universal_newlines=True).strip().split('\n')
18 return [p[2:] for p in f]
19
20 def make_include_graph():
21 return { f: get_local_includes_for(f) for f in find_all_files() }
22
23 def strongly_connected_components(graph):
24 """
25 Tarjan's Algorithm (named for its discoverer, Robert Tarjan) is a graph theory algorithm
26 for finding the strongly connected components of a graph.
27
28 Based on: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
29 """
30
31 index_counter = [0]
32 stack = []
33 lowlinks = {}
34 index = {}
35 result = []
36
37 def strongconnect(node):
38 # set the depth index for this node to the smallest unused index
39 index[node] = index_counter[0]
40 lowlinks[node] = index_counter[0]
41 index_counter[0] += 1
42 stack.append(node)
43
44 # Consider successors of `node`
45 try:
46 successors = graph[node]
47 except:
48 successors = []
49 for successor in successors:
50 if successor not in lowlinks:
51 # Successor has not yet been visited; recurse on it
52 strongconnect(successor)
53 lowlinks[node] = min(lowlinks[node],lowlinks[successor])
54 elif successor in stack:
55 # the successor is in the stack and hence in the current strongly connected component (SCC)
56 lowlinks[node] = min(lowlinks[node],index[successor])
57
58 # If `node` is a root node, pop the stack and generate an SCC
59 if lowlinks[node] == index[node]:
60 connected_component = []
61
62 while True:
63 successor = stack.pop()
64 connected_component.append(successor)
65 if successor == node: break
66 component = tuple(connected_component)
67 # storing the result
68 result.append(component)
69
70 for node in graph:
71 if node not in lowlinks:
72 strongconnect(node)
73
74 return result
75
76 if __name__ == '__main__':
77 comp = strongly_connected_components(make_include_graph())
78 for c in comp:
79 if len(c) != 1:
80 print(c)