summaryrefslogtreecommitdiff
path: root/other/analysis/check_recursion
diff options
context:
space:
mode:
Diffstat (limited to 'other/analysis/check_recursion')
-rwxr-xr-xother/analysis/check_recursion29
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"""
3Tool to check for recursive calls in toxcore C code. 3Tool to check for recursive calls in toxcore C code.
4 4
5Usage: 5Usage:
6 6
7cat toxav/*.c toxcore/*.c toxencryptsave/*.c \ 7cat 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
14from __future__ import print_function
15
16import collections 13import collections
17import fileinput 14import fileinput
18import re 15import re
19import sys 16import sys
20import time 17import time
18from typing import Dict
19from typing import List
20from typing import Set
21 21
22def load_callgraph(): 22
23def 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
42def walk(visited, callgraph, cycles, stack, cur): 43
44def 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
57def get_time(): 65
66def 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
63def find_recursion(expected): 72
73def 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
101find_recursion(expected={ 112find_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",