Skip to content

Commit 5b7d3d0

Browse files
committed
refactor: Make Result struct definition locally and remove dynamic type addition
1 parent 14c3c26 commit 5b7d3d0

1 file changed

Lines changed: 13 additions & 29 deletions

File tree

ci/generate_checked_functions.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
from pycparser import c_parser, c_ast, parse_file
22
import os
33

4-
# Define the Result struct as a string
5-
RESULT_STRUCT = """
6-
typedef struct {
7-
int error_code; // Error code (0 for success, non-zero for errors)
8-
union {
9-
bool bool_value;
10-
void *ptr_value;
11-
int int_value;
12-
// Add other types as needed
13-
} value;
14-
} Result;
15-
"""
16-
174

185
# Helper function to determine if a parameter is a pointer
196
def is_pointer(param):
@@ -24,8 +11,6 @@ def is_pointer(param):
2411

2512

2613
def generate_checked_function(func):
27-
global RESULT_STRUCT # Access the global Result definition
28-
2914
func_name = func.name # Access the name directly from Decl
3015
new_func_name = f"{func_name}_checked"
3116

@@ -37,14 +22,6 @@ def generate_checked_function(func):
3722
if isinstance(func.type.type, c_ast.TypeDecl):
3823
return_type = " ".join(func.type.type.type.names)
3924

40-
# Check if the return type is already in Result, if not, add it
41-
if return_type not in ["bool", "void*", "int", "uint32_t"]:
42-
# Add a new field to the Result struct dynamically
43-
RESULT_STRUCT = RESULT_STRUCT.replace(
44-
"// Add other types as needed",
45-
f" {return_type} {return_type}_value;\n // Add other types as needed",
46-
)
47-
4825
# Start building the new function
4926
new_func = [f"Result {new_func_name}("]
5027
param_list = []
@@ -110,7 +87,15 @@ def generate_checked_function(func):
11087

11188

11289
def process_header():
113-
global RESULT_STRUCT # Access the global Result definition
90+
# Define the Result struct as a string
91+
RESULT_STRUCT = """
92+
typedef struct {
93+
int error_code; // Error code (0 for success, non-zero for errors)
94+
union {
95+
// Add other types as needed
96+
} value;
97+
} Result;
98+
"""
11499

115100
# Based on current file location, adjust the path to the header file
116101
input_header = os.path.join(
@@ -156,11 +141,10 @@ def process_header():
156141

157142
# Update the Result struct with all return types
158143
for return_type in return_types:
159-
if return_type not in ["bool", "void*", "int", "uint32_t"]:
160-
RESULT_STRUCT = RESULT_STRUCT.replace(
161-
"// Add other types as needed",
162-
f" {return_type} {return_type}_value;\n // Add other types as needed",
163-
)
144+
RESULT_STRUCT = RESULT_STRUCT.replace(
145+
"// Add other types as needed",
146+
f" {return_type} {return_type}_value;\n // Add other types as needed",
147+
)
164148

165149
# Generate the new header file
166150
with open(output_header, "w") as f:

0 commit comments

Comments
 (0)