Skip to content

Commit d792e47

Browse files
author
MarcoFalke
committed
Merge #13228: Add script to detect circular dependencies between source modules
a7b295e Add circular dependencies script (Pieter Wuille) Pull request description: This script finds dependencies between source code modules, treating the `.cpp` and `.h` file as one unit (so it will detect `A.cpp` depending on `B.h` where `B.cpp` depends on `A.h`). This can be used to find out which modules cannot be used independently from each other. It is very simplistic at this point, and assumes that a `.cpp` file's corresponding header has the exact same name, with `.cpp` replaced by `.h`. Furthermore, it assumes all `#include`s are relative to the `src/` directory. This is not a linter, and is not enforced through Travis or otherwise. This is the current output: ``` $ ../contrib/devtools/circular-dependencies.py {*,*/*,*/*/*}.{h,cpp} Circular dependency: chain -> pow -> chain Circular dependency: chainparamsbase -> util -> chainparamsbase Circular dependency: checkpoints -> validation -> checkpoints Circular dependency: init -> index/txindex -> init Circular dependency: init -> validation -> init Circular dependency: init -> net_processing -> init Circular dependency: init -> rpc/server -> init Circular dependency: init -> txdb -> init Circular dependency: init -> validationinterface -> init Circular dependency: random -> util -> random Circular dependency: sync -> util -> sync Circular dependency: txmempool -> validation -> txmempool Circular dependency: txmempool -> policy/fees -> txmempool Circular dependency: validation -> index/txindex -> validation Circular dependency: validation -> policy/policy -> validation Circular dependency: validation -> validationinterface -> validation Circular dependency: qt/addresstablemodel -> qt/walletmodel -> qt/addresstablemodel Circular dependency: qt/bantablemodel -> qt/clientmodel -> qt/bantablemodel Circular dependency: qt/bitcoingui -> qt/walletview -> qt/bitcoingui Circular dependency: qt/bitcoingui -> qt/walletframe -> qt/bitcoingui Circular dependency: qt/bitcoingui -> qt/utilitydialog -> qt/bitcoingui Circular dependency: qt/clientmodel -> qt/peertablemodel -> qt/clientmodel Circular dependency: qt/paymentserver -> qt/walletmodel -> qt/paymentserver Circular dependency: qt/recentrequeststablemodel -> qt/walletmodel -> qt/recentrequeststablemodel Circular dependency: qt/sendcoinsdialog -> qt/walletmodel -> qt/sendcoinsdialog Circular dependency: qt/transactiontablemodel -> qt/walletmodel -> qt/transactiontablemodel Circular dependency: qt/walletmodel -> qt/walletmodeltransaction -> qt/walletmodel Circular dependency: rpc/rawtransaction -> wallet/rpcwallet -> rpc/rawtransaction Circular dependency: wallet/coincontrol -> wallet/wallet -> wallet/coincontrol Circular dependency: wallet/fees -> wallet/wallet -> wallet/fees Circular dependency: wallet/rpcwallet -> wallet/wallet -> wallet/rpcwallet Circular dependency: wallet/walletdb -> wallet/wallet -> wallet/walletdb Circular dependency: txmempool -> validation -> policy/rbf -> txmempool Circular dependency: txmempool -> validation -> validationinterface -> txmempool Circular dependency: qt/addressbookpage -> qt/bitcoingui -> qt/walletview -> qt/addressbookpage Circular dependency: qt/guiutil -> qt/walletmodel -> qt/optionsmodel -> qt/guiutil Circular dependency: qt/addressbookpage -> qt/bitcoingui -> qt/walletview -> qt/signverifymessagedialog -> qt/addressbookpage Circular dependency: qt/addressbookpage -> qt/bitcoingui -> qt/walletview -> qt/receivecoinsdialog -> qt/addressbookpage Circular dependency: qt/guiutil -> qt/walletmodel -> qt/optionsmodel -> qt/intro -> qt/guiutil Circular dependency: qt/addressbookpage -> qt/bitcoingui -> qt/walletview -> qt/sendcoinsdialog -> qt/sendcoinsentry -> qt/addressbookpage ``` Tree-SHA512: 29bc985b7a41699f4666b0aaa785ca63c2145e84c37458536f4dcf8e3de8f1312cf0323fe09cb8f348a9d363583f76eac2d5bee574bc6a9f9cc97a9b0aad406f
2 parents d9ebb63 + a7b295e commit d792e47

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

contrib/devtools/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,14 @@ It will do the following automatically:
194194
- add missing translations to the build system (TODO)
195195

196196
See doc/translation-process.md for more information.
197+
198+
circular-dependencies.py
199+
========================
200+
201+
Run this script from the root of the source tree (`src/`) to find circular dependencies in the source code.
202+
This looks only at which files include other files, treating the `.cpp` and `.h` file as one unit.
203+
204+
Example usage:
205+
206+
cd .../src
207+
../contrib/devtools/circular-dependencies.py {*,*/*,*/*/*}.{h,cpp}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import re
5+
6+
MAPPING = {
7+
'core_read.cpp': 'core_io.cpp',
8+
'core_write.cpp': 'core_io.cpp',
9+
}
10+
11+
def module_name(path):
12+
if path in MAPPING:
13+
path = MAPPING[path]
14+
if path.endswith(".h"):
15+
return path[:-2]
16+
if path.endswith(".c"):
17+
return path[:-2]
18+
if path.endswith(".cpp"):
19+
return path[:-4]
20+
return None
21+
22+
files = dict()
23+
deps = dict()
24+
25+
RE = re.compile("^#include <(.*)>")
26+
27+
# Iterate over files, and create list of modules
28+
for arg in sys.argv[1:]:
29+
module = module_name(arg)
30+
if module is None:
31+
print("Ignoring file %s (does not constitute module)\n" % arg)
32+
else:
33+
files[arg] = module
34+
deps[module] = set()
35+
36+
# Iterate again, and build list of direct dependencies for each module
37+
# TODO: implement support for multiple include directories
38+
for arg in sorted(files.keys()):
39+
module = files[arg]
40+
with open(arg, 'r') as f:
41+
for line in f:
42+
match = RE.match(line)
43+
if match:
44+
include = match.group(1)
45+
included_module = module_name(include)
46+
if included_module is not None and included_module in deps and included_module != module:
47+
deps[module].add(included_module)
48+
49+
# Loop to find the shortest (remaining) circular dependency
50+
have_cycle = False
51+
while True:
52+
shortest_cycle = None
53+
for module in sorted(deps.keys()):
54+
# Build the transitive closure of dependencies of module
55+
closure = dict()
56+
for dep in deps[module]:
57+
closure[dep] = []
58+
while True:
59+
old_size = len(closure)
60+
old_closure_keys = sorted(closure.keys())
61+
for src in old_closure_keys:
62+
for dep in deps[src]:
63+
if dep not in closure:
64+
closure[dep] = closure[src] + [src]
65+
if len(closure) == old_size:
66+
break
67+
# If module is in its own transitive closure, it's a circular dependency; check if it is the shortest
68+
if module in closure and (shortest_cycle is None or len(closure[module]) + 1 < len(shortest_cycle)):
69+
shortest_cycle = [module] + closure[module]
70+
if shortest_cycle is None:
71+
break
72+
# We have the shortest circular dependency; report it
73+
module = shortest_cycle[0]
74+
print("Circular dependency: %s" % (" -> ".join(shortest_cycle + [module])))
75+
# And then break the dependency to avoid repeating in other cycles
76+
deps[shortest_cycle[-1]] = deps[shortest_cycle[-1]] - set([module])
77+
have_cycle = True
78+
79+
sys.exit(1 if have_cycle else 0)

0 commit comments

Comments
 (0)