Skip to content

Commit ab079c1

Browse files
committed
refactor: enhance header generation script with default headers and formatting support
1 parent 29d5070 commit ab079c1

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

ci/generate_checked_functions.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,37 @@
77
Arguments:
88
--headers: A list of header file paths to process. Each header file will be parsed, and a corresponding
99
"_checked.h" file will be generated with additional null pointer checks and error handling.
10+
If not provided, a default list of headers under "core/iwasm/include/" will be used.
1011
1112
Example:
13+
python3 generate_checked_functions.py
14+
# OR
1215
python3 generate_checked_functions.py --headers core/iwasm/include/wasm_export.h
1316
1417
Description:
1518
The script parses the provided header files using `pycparser` to extract function declarations and typedefs.
1619
For each function, it generates a "checked" version that includes:
1720
- Null pointer checks for pointer parameters.
1821
- Error handling using a `Result` struct.
22+
- Support for variadic arguments (e.g., ...).
1923
2024
The generated "_checked.h" files include the original header file and define the `Result` struct, which
21-
encapsulates the return value and error codes.
25+
encapsulates the return value and error codes. The `Result` struct is dynamically generated based on the
26+
return types of the functions in the header file.
2227
2328
Dependencies:
2429
- pycparser: Install it using `pip install pycparser`.
30+
- clang-format-14: Ensure it is installed for formatting the generated files.
2531
2632
Output:
2733
For each input header file, a corresponding "_checked.h" file is created in the same directory.
34+
The generated files are automatically formatted using clang-format-14.
2835
"""
2936

30-
from pycparser import c_ast, parse_file
3137
import argparse
3238
from pathlib import Path
39+
from pycparser import c_ast, parse_file
40+
import subprocess
3341

3442
# Constants for repeated strings
3543
CPP_ARGS = [
@@ -226,14 +234,23 @@ def parse_arguments():
226234
parser.add_argument(
227235
"--headers",
228236
nargs="+",
229-
required=True,
230-
help="List of header file paths to process.",
237+
required=False,
238+
help="List of header file paths to process. Relative to the project root.",
239+
default=[
240+
"core/iwasm/include/aot_comp_option.h",
241+
"core/iwasm/include/aot_export.h",
242+
"core/iwasm/include/gc_export.h",
243+
"core/iwasm/include/lib_export.h",
244+
"core/iwasm/include/wasm_c_api.h",
245+
"core/iwasm/include/wasm_export.h",
246+
],
231247
)
232248
return parser.parse_args()
233249

234250

235251
def generate_checked_headers(header_paths):
236252
"""Process each header file and generate checked versions."""
253+
output_header = []
237254
for input_header in header_paths:
238255
input_path = Path(input_header)
239256
output_path = input_path.with_name(input_path.stem + "_checked.h")
@@ -260,11 +277,18 @@ def generate_checked_headers(header_paths):
260277

261278
result_struct = generate_result_struct(return_types)
262279
write_checked_header(output_path, result_struct, functions, typedefs)
280+
output_header.append(output_path)
281+
282+
return output_header
263283

264284

265285
def main():
266286
args = parse_arguments()
267-
generate_checked_headers(args.headers)
287+
generated_headers = generate_checked_headers(args.headers)
288+
289+
# format the generated files using clang-format-14
290+
for header in generated_headers:
291+
subprocess.run(["clang-format-14", "--style=file", "-i", str(header)])
268292

269293

270294
if __name__ == "__main__":

0 commit comments

Comments
 (0)