Skip to content

Commit 3c5720b

Browse files
committed
verification/rvgen: Annotate DA functions with types
Functions in automata.py, dot2c.py and dot2k.py don't have type annotations and it can get complicated to remember how to use them. Add minimal type annotations. Reviewed-by: Nam Cao <namcao@linutronix.de> Link: https://lore.kernel.org/r/20251126104241.291258-6-gmonaco@redhat.com Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
1 parent 531b50e commit 3c5720b

3 files changed

Lines changed: 41 additions & 41 deletions

File tree

tools/verification/rvgen/rvgen/automata.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, file_path, model_name=None):
2828
self.function = self.__create_matrix()
2929
self.events_start, self.events_start_run = self.__store_init_events()
3030

31-
def __get_model_name(self):
31+
def __get_model_name(self) -> str:
3232
basename = ntpath.basename(self.__dot_path)
3333
if not basename.endswith(".dot") and not basename.endswith(".gv"):
3434
print("not a dot file")
@@ -40,7 +40,7 @@ def __get_model_name(self):
4040

4141
return model_name
4242

43-
def __open_dot(self):
43+
def __open_dot(self) -> list[str]:
4444
cursor = 0
4545
dot_lines = []
4646
try:
@@ -60,13 +60,13 @@ def __open_dot(self):
6060
cursor += 1
6161
return dot_lines
6262

63-
def __get_cursor_begin_states(self):
63+
def __get_cursor_begin_states(self) -> int:
6464
cursor = 0
6565
while self.__dot_lines[cursor].split()[0] != "{node":
6666
cursor += 1
6767
return cursor
6868

69-
def __get_cursor_begin_events(self):
69+
def __get_cursor_begin_events(self) -> int:
7070
cursor = 0
7171
while self.__dot_lines[cursor].split()[0] != "{node":
7272
cursor += 1
@@ -76,7 +76,7 @@ def __get_cursor_begin_events(self):
7676
cursor += 1
7777
return cursor
7878

79-
def __get_state_variables(self):
79+
def __get_state_variables(self) -> tuple[list[str], str, list[str]]:
8080
# wait for node declaration
8181
states = []
8282
final_states = []
@@ -116,7 +116,7 @@ def __get_state_variables(self):
116116

117117
return states, initial_state, final_states
118118

119-
def __get_event_variables(self):
119+
def __get_event_variables(self) -> list[str]:
120120
# here we are at the begin of transitions, take a note, we will return later.
121121
cursor = self.__get_cursor_begin_events()
122122

@@ -140,7 +140,7 @@ def __get_event_variables(self):
140140

141141
return sorted(set(events))
142142

143-
def __create_matrix(self):
143+
def __create_matrix(self) -> list[list[str]]:
144144
# transform the array into a dictionary
145145
events = self.events
146146
states = self.states
@@ -174,7 +174,7 @@ def __create_matrix(self):
174174

175175
return matrix
176176

177-
def __store_init_events(self):
177+
def __store_init_events(self) -> tuple[list[bool], list[bool]]:
178178
events_start = [False] * len(self.events)
179179
events_start_run = [False] * len(self.events)
180180
for i, _ in enumerate(self.events):
@@ -196,10 +196,10 @@ def __store_init_events(self):
196196
events_start_run[i] = True
197197
return events_start, events_start_run
198198

199-
def is_start_event(self, event):
199+
def is_start_event(self, event: str) -> bool:
200200
return self.events_start[self.events.index(event)]
201201

202-
def is_start_run_event(self, event):
202+
def is_start_run_event(self, event: str) -> bool:
203203
# prefer handle_start_event if there
204204
if any(self.events_start):
205205
return False

tools/verification/rvgen/rvgen/dot2c.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __buff_to_string(self, buff):
3535
# cut off the last \n
3636
return string[:-1]
3737

38-
def __get_enum_states_content(self):
38+
def __get_enum_states_content(self) -> list[str]:
3939
buff = []
4040
buff.append("\t%s%s = 0," % (self.initial_state, self.enum_suffix))
4141
for state in self.states:
@@ -49,15 +49,15 @@ def get_enum_states_string(self):
4949
buff = self.__get_enum_states_content()
5050
return self.__buff_to_string(buff)
5151

52-
def format_states_enum(self):
52+
def format_states_enum(self) -> list[str]:
5353
buff = []
5454
buff.append("enum %s {" % self.enum_states_def)
5555
buff.append(self.get_enum_states_string())
5656
buff.append("};\n")
5757

5858
return buff
5959

60-
def __get_enum_events_content(self):
60+
def __get_enum_events_content(self) -> list[str]:
6161
buff = []
6262
first = True
6363
for event in self.events:
@@ -75,15 +75,15 @@ def get_enum_events_string(self):
7575
buff = self.__get_enum_events_content()
7676
return self.__buff_to_string(buff)
7777

78-
def format_events_enum(self):
78+
def format_events_enum(self) -> list[str]:
7979
buff = []
8080
buff.append("enum %s {" % self.enum_events_def)
8181
buff.append(self.get_enum_events_string())
8282
buff.append("};\n")
8383

8484
return buff
8585

86-
def get_minimun_type(self):
86+
def get_minimun_type(self) -> str:
8787
min_type = "unsigned char"
8888

8989
if self.states.__len__() > 255:
@@ -97,7 +97,7 @@ def get_minimun_type(self):
9797

9898
return min_type
9999

100-
def format_automaton_definition(self):
100+
def format_automaton_definition(self) -> list[str]:
101101
min_type = self.get_minimun_type()
102102
buff = []
103103
buff.append("struct %s {" % self.struct_automaton_def)
@@ -109,12 +109,12 @@ def format_automaton_definition(self):
109109
buff.append("};\n")
110110
return buff
111111

112-
def format_aut_init_header(self):
112+
def format_aut_init_header(self) -> list[str]:
113113
buff = []
114114
buff.append("static const struct %s %s = {" % (self.struct_automaton_def, self.var_automaton_def))
115115
return buff
116116

117-
def __get_string_vector_per_line_content(self, buff):
117+
def __get_string_vector_per_line_content(self, buff: list[str]) -> str:
118118
first = True
119119
string = ""
120120
for entry in buff:
@@ -133,26 +133,26 @@ def get_aut_init_events_string(self):
133133
def get_aut_init_states_string(self):
134134
return self.__get_string_vector_per_line_content(self.states)
135135

136-
def format_aut_init_events_string(self):
136+
def format_aut_init_events_string(self) -> list[str]:
137137
buff = []
138138
buff.append("\t.event_names = {")
139139
buff.append(self.get_aut_init_events_string())
140140
buff.append("\t},")
141141
return buff
142142

143-
def format_aut_init_states_string(self):
143+
def format_aut_init_states_string(self) -> list[str]:
144144
buff = []
145145
buff.append("\t.state_names = {")
146146
buff.append(self.get_aut_init_states_string())
147147
buff.append("\t},")
148148

149149
return buff
150150

151-
def __get_max_strlen_of_states(self):
151+
def __get_max_strlen_of_states(self) -> int:
152152
max_state_name = max(self.states, key = len).__len__()
153153
return max(max_state_name, self.invalid_state_str.__len__())
154154

155-
def get_aut_init_function(self):
155+
def get_aut_init_function(self) -> str:
156156
nr_states = self.states.__len__()
157157
nr_events = self.events.__len__()
158158
buff = []
@@ -180,25 +180,25 @@ def get_aut_init_function(self):
180180

181181
return self.__buff_to_string(buff)
182182

183-
def format_aut_init_function(self):
183+
def format_aut_init_function(self) -> list[str]:
184184
buff = []
185185
buff.append("\t.function = {")
186186
buff.append(self.get_aut_init_function())
187187
buff.append("\t},")
188188

189189
return buff
190190

191-
def get_aut_init_initial_state(self):
191+
def get_aut_init_initial_state(self) -> str:
192192
return self.initial_state
193193

194-
def format_aut_init_initial_state(self):
194+
def format_aut_init_initial_state(self) -> list[str]:
195195
buff = []
196196
initial_state = self.get_aut_init_initial_state()
197197
buff.append("\t.initial_state = " + initial_state + self.enum_suffix + ",")
198198

199199
return buff
200200

201-
def get_aut_init_final_states(self):
201+
def get_aut_init_final_states(self) -> str:
202202
line = ""
203203
first = True
204204
for state in self.states:
@@ -213,29 +213,29 @@ def get_aut_init_final_states(self):
213213
line = line + '0'
214214
return line
215215

216-
def format_aut_init_final_states(self):
216+
def format_aut_init_final_states(self) -> list[str]:
217217
buff = []
218218
buff.append("\t.final_states = { %s }," % self.get_aut_init_final_states())
219219

220220
return buff
221221

222-
def __get_automaton_initialization_footer_string(self):
222+
def __get_automaton_initialization_footer_string(self) -> str:
223223
footer = "};\n"
224224
return footer
225225

226-
def format_aut_init_footer(self):
226+
def format_aut_init_footer(self) -> list[str]:
227227
buff = []
228228
buff.append(self.__get_automaton_initialization_footer_string())
229229

230230
return buff
231231

232-
def format_invalid_state(self):
232+
def format_invalid_state(self) -> list[str]:
233233
buff = []
234234
buff.append("#define %s state_max%s\n" % (self.invalid_state_str, self.enum_suffix))
235235

236236
return buff
237237

238-
def format_model(self):
238+
def format_model(self) -> list[str]:
239239
buff = []
240240
buff += self.format_states_enum()
241241
buff += self.format_invalid_state()

tools/verification/rvgen/rvgen/dot2k.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def __init__(self, file_path, MonitorType, extra_params={}):
2121
Dot2c.__init__(self, file_path, extra_params.get("model_name"))
2222
self.enum_suffix = "_%s" % self.name
2323

24-
def fill_monitor_type(self):
24+
def fill_monitor_type(self) -> str:
2525
return self.monitor_type.upper()
2626

27-
def fill_tracepoint_handlers_skel(self):
27+
def fill_tracepoint_handlers_skel(self) -> str:
2828
buff = []
2929
for event in self.events:
3030
buff.append("static void handle_%s(void *data, /* XXX: fill header */)" % event)
@@ -45,19 +45,19 @@ def fill_tracepoint_handlers_skel(self):
4545
buff.append("")
4646
return '\n'.join(buff)
4747

48-
def fill_tracepoint_attach_probe(self):
48+
def fill_tracepoint_attach_probe(self) -> str:
4949
buff = []
5050
for event in self.events:
5151
buff.append("\trv_attach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
5252
return '\n'.join(buff)
5353

54-
def fill_tracepoint_detach_helper(self):
54+
def fill_tracepoint_detach_helper(self) -> str:
5555
buff = []
5656
for event in self.events:
5757
buff.append("\trv_detach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event))
5858
return '\n'.join(buff)
5959

60-
def fill_model_h_header(self):
60+
def fill_model_h_header(self) -> list[str]:
6161
buff = []
6262
buff.append("/* SPDX-License-Identifier: GPL-2.0 */")
6363
buff.append("/*")
@@ -71,7 +71,7 @@ def fill_model_h_header(self):
7171

7272
return buff
7373

74-
def fill_model_h(self):
74+
def fill_model_h(self) -> str:
7575
#
7676
# Adjust the definition names
7777
#
@@ -85,17 +85,17 @@ def fill_model_h(self):
8585

8686
return '\n'.join(buff)
8787

88-
def fill_monitor_class_type(self):
88+
def fill_monitor_class_type(self) -> str:
8989
if self.monitor_type == "per_task":
9090
return "DA_MON_EVENTS_ID"
9191
return "DA_MON_EVENTS_IMPLICIT"
9292

93-
def fill_monitor_class(self):
93+
def fill_monitor_class(self) -> str:
9494
if self.monitor_type == "per_task":
9595
return "da_monitor_id"
9696
return "da_monitor"
9797

98-
def fill_tracepoint_args_skel(self, tp_type):
98+
def fill_tracepoint_args_skel(self, tp_type: str) -> str:
9999
buff = []
100100
tp_args_event = [
101101
("char *", "state"),
@@ -117,7 +117,7 @@ def fill_tracepoint_args_skel(self, tp_type):
117117
buff.append(" TP_ARGS(%s)" % tp_args_c)
118118
return '\n'.join(buff)
119119

120-
def fill_main_c(self):
120+
def fill_main_c(self) -> str:
121121
main_c = super().fill_main_c()
122122

123123
min_type = self.get_minimun_type()

0 commit comments

Comments
 (0)