Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

+41 -41
+10 -10
tools/verification/rvgen/rvgen/automata.py
··· 28 28 self.function = self.__create_matrix() 29 29 self.events_start, self.events_start_run = self.__store_init_events() 30 30 31 - def __get_model_name(self): 31 + def __get_model_name(self) -> str: 32 32 basename = ntpath.basename(self.__dot_path) 33 33 if not basename.endswith(".dot") and not basename.endswith(".gv"): 34 34 print("not a dot file") ··· 40 40 41 41 return model_name 42 42 43 - def __open_dot(self): 43 + def __open_dot(self) -> list[str]: 44 44 cursor = 0 45 45 dot_lines = [] 46 46 try: ··· 60 60 cursor += 1 61 61 return dot_lines 62 62 63 - def __get_cursor_begin_states(self): 63 + def __get_cursor_begin_states(self) -> int: 64 64 cursor = 0 65 65 while self.__dot_lines[cursor].split()[0] != "{node": 66 66 cursor += 1 67 67 return cursor 68 68 69 - def __get_cursor_begin_events(self): 69 + def __get_cursor_begin_events(self) -> int: 70 70 cursor = 0 71 71 while self.__dot_lines[cursor].split()[0] != "{node": 72 72 cursor += 1 ··· 76 76 cursor += 1 77 77 return cursor 78 78 79 - def __get_state_variables(self): 79 + def __get_state_variables(self) -> tuple[list[str], str, list[str]]: 80 80 # wait for node declaration 81 81 states = [] 82 82 final_states = [] ··· 116 116 117 117 return states, initial_state, final_states 118 118 119 - def __get_event_variables(self): 119 + def __get_event_variables(self) -> list[str]: 120 120 # here we are at the begin of transitions, take a note, we will return later. 121 121 cursor = self.__get_cursor_begin_events() 122 122 ··· 140 140 141 141 return sorted(set(events)) 142 142 143 - def __create_matrix(self): 143 + def __create_matrix(self) -> list[list[str]]: 144 144 # transform the array into a dictionary 145 145 events = self.events 146 146 states = self.states ··· 174 174 175 175 return matrix 176 176 177 - def __store_init_events(self): 177 + def __store_init_events(self) -> tuple[list[bool], list[bool]]: 178 178 events_start = [False] * len(self.events) 179 179 events_start_run = [False] * len(self.events) 180 180 for i, _ in enumerate(self.events): ··· 196 196 events_start_run[i] = True 197 197 return events_start, events_start_run 198 198 199 - def is_start_event(self, event): 199 + def is_start_event(self, event: str) -> bool: 200 200 return self.events_start[self.events.index(event)] 201 201 202 - def is_start_run_event(self, event): 202 + def is_start_run_event(self, event: str) -> bool: 203 203 # prefer handle_start_event if there 204 204 if any(self.events_start): 205 205 return False
+21 -21
tools/verification/rvgen/rvgen/dot2c.py
··· 35 35 # cut off the last \n 36 36 return string[:-1] 37 37 38 - def __get_enum_states_content(self): 38 + def __get_enum_states_content(self) -> list[str]: 39 39 buff = [] 40 40 buff.append("\t%s%s = 0," % (self.initial_state, self.enum_suffix)) 41 41 for state in self.states: ··· 49 49 buff = self.__get_enum_states_content() 50 50 return self.__buff_to_string(buff) 51 51 52 - def format_states_enum(self): 52 + def format_states_enum(self) -> list[str]: 53 53 buff = [] 54 54 buff.append("enum %s {" % self.enum_states_def) 55 55 buff.append(self.get_enum_states_string()) ··· 57 57 58 58 return buff 59 59 60 - def __get_enum_events_content(self): 60 + def __get_enum_events_content(self) -> list[str]: 61 61 buff = [] 62 62 first = True 63 63 for event in self.events: ··· 75 75 buff = self.__get_enum_events_content() 76 76 return self.__buff_to_string(buff) 77 77 78 - def format_events_enum(self): 78 + def format_events_enum(self) -> list[str]: 79 79 buff = [] 80 80 buff.append("enum %s {" % self.enum_events_def) 81 81 buff.append(self.get_enum_events_string()) ··· 83 83 84 84 return buff 85 85 86 - def get_minimun_type(self): 86 + def get_minimun_type(self) -> str: 87 87 min_type = "unsigned char" 88 88 89 89 if self.states.__len__() > 255: ··· 97 97 98 98 return min_type 99 99 100 - def format_automaton_definition(self): 100 + def format_automaton_definition(self) -> list[str]: 101 101 min_type = self.get_minimun_type() 102 102 buff = [] 103 103 buff.append("struct %s {" % self.struct_automaton_def) ··· 109 109 buff.append("};\n") 110 110 return buff 111 111 112 - def format_aut_init_header(self): 112 + def format_aut_init_header(self) -> list[str]: 113 113 buff = [] 114 114 buff.append("static const struct %s %s = {" % (self.struct_automaton_def, self.var_automaton_def)) 115 115 return buff 116 116 117 - def __get_string_vector_per_line_content(self, buff): 117 + def __get_string_vector_per_line_content(self, buff: list[str]) -> str: 118 118 first = True 119 119 string = "" 120 120 for entry in buff: ··· 133 133 def get_aut_init_states_string(self): 134 134 return self.__get_string_vector_per_line_content(self.states) 135 135 136 - def format_aut_init_events_string(self): 136 + def format_aut_init_events_string(self) -> list[str]: 137 137 buff = [] 138 138 buff.append("\t.event_names = {") 139 139 buff.append(self.get_aut_init_events_string()) 140 140 buff.append("\t},") 141 141 return buff 142 142 143 - def format_aut_init_states_string(self): 143 + def format_aut_init_states_string(self) -> list[str]: 144 144 buff = [] 145 145 buff.append("\t.state_names = {") 146 146 buff.append(self.get_aut_init_states_string()) ··· 148 148 149 149 return buff 150 150 151 - def __get_max_strlen_of_states(self): 151 + def __get_max_strlen_of_states(self) -> int: 152 152 max_state_name = max(self.states, key = len).__len__() 153 153 return max(max_state_name, self.invalid_state_str.__len__()) 154 154 155 - def get_aut_init_function(self): 155 + def get_aut_init_function(self) -> str: 156 156 nr_states = self.states.__len__() 157 157 nr_events = self.events.__len__() 158 158 buff = [] ··· 180 180 181 181 return self.__buff_to_string(buff) 182 182 183 - def format_aut_init_function(self): 183 + def format_aut_init_function(self) -> list[str]: 184 184 buff = [] 185 185 buff.append("\t.function = {") 186 186 buff.append(self.get_aut_init_function()) ··· 188 188 189 189 return buff 190 190 191 - def get_aut_init_initial_state(self): 191 + def get_aut_init_initial_state(self) -> str: 192 192 return self.initial_state 193 193 194 - def format_aut_init_initial_state(self): 194 + def format_aut_init_initial_state(self) -> list[str]: 195 195 buff = [] 196 196 initial_state = self.get_aut_init_initial_state() 197 197 buff.append("\t.initial_state = " + initial_state + self.enum_suffix + ",") 198 198 199 199 return buff 200 200 201 - def get_aut_init_final_states(self): 201 + def get_aut_init_final_states(self) -> str: 202 202 line = "" 203 203 first = True 204 204 for state in self.states: ··· 213 213 line = line + '0' 214 214 return line 215 215 216 - def format_aut_init_final_states(self): 216 + def format_aut_init_final_states(self) -> list[str]: 217 217 buff = [] 218 218 buff.append("\t.final_states = { %s }," % self.get_aut_init_final_states()) 219 219 220 220 return buff 221 221 222 - def __get_automaton_initialization_footer_string(self): 222 + def __get_automaton_initialization_footer_string(self) -> str: 223 223 footer = "};\n" 224 224 return footer 225 225 226 - def format_aut_init_footer(self): 226 + def format_aut_init_footer(self) -> list[str]: 227 227 buff = [] 228 228 buff.append(self.__get_automaton_initialization_footer_string()) 229 229 230 230 return buff 231 231 232 - def format_invalid_state(self): 232 + def format_invalid_state(self) -> list[str]: 233 233 buff = [] 234 234 buff.append("#define %s state_max%s\n" % (self.invalid_state_str, self.enum_suffix)) 235 235 236 236 return buff 237 237 238 - def format_model(self): 238 + def format_model(self) -> list[str]: 239 239 buff = [] 240 240 buff += self.format_states_enum() 241 241 buff += self.format_invalid_state()
+10 -10
tools/verification/rvgen/rvgen/dot2k.py
··· 21 21 Dot2c.__init__(self, file_path, extra_params.get("model_name")) 22 22 self.enum_suffix = "_%s" % self.name 23 23 24 - def fill_monitor_type(self): 24 + def fill_monitor_type(self) -> str: 25 25 return self.monitor_type.upper() 26 26 27 - def fill_tracepoint_handlers_skel(self): 27 + def fill_tracepoint_handlers_skel(self) -> str: 28 28 buff = [] 29 29 for event in self.events: 30 30 buff.append("static void handle_%s(void *data, /* XXX: fill header */)" % event) ··· 45 45 buff.append("") 46 46 return '\n'.join(buff) 47 47 48 - def fill_tracepoint_attach_probe(self): 48 + def fill_tracepoint_attach_probe(self) -> str: 49 49 buff = [] 50 50 for event in self.events: 51 51 buff.append("\trv_attach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event)) 52 52 return '\n'.join(buff) 53 53 54 - def fill_tracepoint_detach_helper(self): 54 + def fill_tracepoint_detach_helper(self) -> str: 55 55 buff = [] 56 56 for event in self.events: 57 57 buff.append("\trv_detach_trace_probe(\"%s\", /* XXX: tracepoint */, handle_%s);" % (self.name, event)) 58 58 return '\n'.join(buff) 59 59 60 - def fill_model_h_header(self): 60 + def fill_model_h_header(self) -> list[str]: 61 61 buff = [] 62 62 buff.append("/* SPDX-License-Identifier: GPL-2.0 */") 63 63 buff.append("/*") ··· 71 71 72 72 return buff 73 73 74 - def fill_model_h(self): 74 + def fill_model_h(self) -> str: 75 75 # 76 76 # Adjust the definition names 77 77 # ··· 85 85 86 86 return '\n'.join(buff) 87 87 88 - def fill_monitor_class_type(self): 88 + def fill_monitor_class_type(self) -> str: 89 89 if self.monitor_type == "per_task": 90 90 return "DA_MON_EVENTS_ID" 91 91 return "DA_MON_EVENTS_IMPLICIT" 92 92 93 - def fill_monitor_class(self): 93 + def fill_monitor_class(self) -> str: 94 94 if self.monitor_type == "per_task": 95 95 return "da_monitor_id" 96 96 return "da_monitor" 97 97 98 - def fill_tracepoint_args_skel(self, tp_type): 98 + def fill_tracepoint_args_skel(self, tp_type: str) -> str: 99 99 buff = [] 100 100 tp_args_event = [ 101 101 ("char *", "state"), ··· 117 117 buff.append(" TP_ARGS(%s)" % tp_args_c) 118 118 return '\n'.join(buff) 119 119 120 - def fill_main_c(self): 120 + def fill_main_c(self) -> str: 121 121 main_c = super().fill_main_c() 122 122 123 123 min_type = self.get_minimun_type()