diff options
Diffstat (limited to 'other/analysis')
-rwxr-xr-x | other/analysis/check_recursion | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/other/analysis/check_recursion b/other/analysis/check_recursion index 21063527..a8a6e0ef 100755 --- a/other/analysis/check_recursion +++ b/other/analysis/check_recursion | |||
@@ -1,8 +1,8 @@ | |||
1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python3 |
2 | """ | 2 | """ |
3 | Tool to check for recursive calls in toxcore C code. | 3 | Tool to check for recursive calls in toxcore C code. |
4 | 4 | ||
5 | Usage: | 5 | Usage: |
6 | 6 | ||
7 | cat toxav/*.c toxcore/*.c toxencryptsave/*.c \ | 7 | cat toxav/*.c toxcore/*.c toxencryptsave/*.c \ |
8 | | clang `pkg-config --cflags libsodium opus vpx` \ | 8 | | clang `pkg-config --cflags libsodium opus vpx` \ |
@@ -10,16 +10,17 @@ cat toxav/*.c toxcore/*.c toxencryptsave/*.c \ | |||
10 | | opt -analyze -print-callgraph 2>&1 \ | 10 | | opt -analyze -print-callgraph 2>&1 \ |
11 | | other/analysis/check_recursion | 11 | | other/analysis/check_recursion |
12 | """ | 12 | """ |
13 | |||
14 | from __future__ import print_function | ||
15 | |||
16 | import collections | 13 | import collections |
17 | import fileinput | 14 | import fileinput |
18 | import re | 15 | import re |
19 | import sys | 16 | import sys |
20 | import time | 17 | import time |
18 | from typing import Dict | ||
19 | from typing import List | ||
20 | from typing import Set | ||
21 | 21 | ||
22 | def load_callgraph(): | 22 | |
23 | def load_callgraph() -> Dict: | ||
23 | """ | 24 | """ |
24 | Parses the output from opt -print-callgraph from stdin or argv. | 25 | Parses the output from opt -print-callgraph from stdin or argv. |
25 | 26 | ||
@@ -39,7 +40,14 @@ def load_callgraph(): | |||
39 | 40 | ||
40 | return {k: sorted(v) for k, v in graph.items()} | 41 | return {k: sorted(v) for k, v in graph.items()} |
41 | 42 | ||
42 | def walk(visited, callgraph, cycles, stack, cur): | 43 | |
44 | def walk( | ||
45 | visited: Set, | ||
46 | callgraph: Dict, | ||
47 | cycles: Set, | ||
48 | stack: List, | ||
49 | cur: str, | ||
50 | ) -> None: | ||
43 | """ | 51 | """ |
44 | Detects cycles in the callgraph and adds them to the cycles parameter. | 52 | Detects cycles in the callgraph and adds them to the cycles parameter. |
45 | """ | 53 | """ |
@@ -54,13 +62,15 @@ def walk(visited, callgraph, cycles, stack, cur): | |||
54 | visited.add(callee) | 62 | visited.add(callee) |
55 | stack.pop() | 63 | stack.pop() |
56 | 64 | ||
57 | def get_time(): | 65 | |
66 | def get_time() -> int: | ||
58 | """ | 67 | """ |
59 | Return the current time in milliseconds. | 68 | Return the current time in milliseconds. |
60 | """ | 69 | """ |
61 | return int(round(time.time() * 1000)) | 70 | return int(round(time.time() * 1000)) |
62 | 71 | ||
63 | def find_recursion(expected): | 72 | |
73 | def find_recursion(expected: Set) -> None: | ||
64 | """ | 74 | """ |
65 | Main function: detects cycles and prints them. | 75 | Main function: detects cycles and prints them. |
66 | 76 | ||
@@ -98,6 +108,7 @@ def find_recursion(expected): | |||
98 | if expected or cycles: | 108 | if expected or cycles: |
99 | sys.exit(1) | 109 | sys.exit(1) |
100 | 110 | ||
111 | |||
101 | find_recursion(expected={ | 112 | find_recursion(expected={ |
102 | "add_to_closest -> add_to_closest", | 113 | "add_to_closest -> add_to_closest", |
103 | "add_to_list -> add_to_list", | 114 | "add_to_list -> add_to_list", |