-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcCyclomaticForAllFunctions.py
More file actions
139 lines (103 loc) · 3.48 KB
/
CalcCyclomaticForAllFunctions.py
File metadata and controls
139 lines (103 loc) · 3.48 KB
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
try:
from ghidra_builtins import *
except Exception as e:
pass
from ghidra.app.tablechooser import (
AddressableRowObject,
StringColumnDisplay,
)
from ghidra.program.util import CyclomaticComplexity
def get_functions_list():
functions = currentProgram.getFunctionManager().getFunctions(True)
func_list = []
metric = CyclomaticComplexity()
for function in functions:
complexity = metric.calculateCyclomaticComplexity(function, monitor)
frequency = len(function.getCallingFunctions(monitor))
if(complexity > 0):
func_list.append(
{
"function": str(function),
"address": function.getEntryPoint(),
"complexity": complexity,
"frequency": frequency
}
)
return func_list
def top_n_complex(func_list, n):
sorted_func_list = sorted(
func_list, key=lambda elem: elem["complexity"], reverse=True
)
return sorted_func_list[:n]
def top_n_frequent(func_list, n):
sorted_func_list = sorted(
func_list, key=lambda elem: elem["frequency"], reverse=True
)
return sorted_func_list[:n]
class ComplexFunction(AddressableRowObject):
def __init__(
self, function_name, location, complexity, frequency
):
super(ComplexFunction, self).__init__()
self.function_name = function_name
self.location = location
self.complexity = complexity
self.frequency = frequency
def getAddress(self):
return self.location
class FunctionNameColumn(StringColumnDisplay):
def getColumnName(self):
return u"Function Name"
def getColumnValue(
self, row_object
):
return row_object.function_name
class ComplexityColumn(StringColumnDisplay):
def getColumnName(self):
return u"Complexity"
def getColumnValue(
self, row_object
):
return row_object.complexity
class FrequencyColumn(StringColumnDisplay):
def getColumnName(self):
return u"Frequency"
def getColumnValue(
self, row_object
):
return row_object.frequency
def configure_table_columns(table_dialog):
table_dialog.addCustomColumn(FunctionNameColumn())
table_dialog.addCustomColumn(ComplexityColumn())
table_dialog.addCustomColumn(FrequencyColumn())
if __name__ == "__main__":
choices = [
"Show All",
"Show top 10 cyclomatically complex functions",
"Show top 10 frequently used functions",
"Show top 50 cyclomatically complex functions",
"Show top 50 frequently used functions"
]
user_choice = askChoice("Choose One", "Select one", choices,1)
user_option = choices.index(user_choice)
result = get_functions_list()
if user_option == 1:
result = top_n_complex(result, 10)
elif user_option == 2:
result = top_n_frequent(result, 10)
elif user_option == 3:
result = top_n_complex(result, 50)
elif user_option == 4:
result = top_n_frequent(result, 50)
table_dialog = createTableChooserDialog(
"Functions with Cyclomatic Complexity and Frequency",
None,
)
configure_table_columns(table_dialog)
table_dialog.show()
for i in result:
table_dialog.add(
ComplexFunction(
i["function"], i["address"], i["complexity"], i["frequency"]
)
)