refactor: enhance header generation script with default headers and formatting support

This commit is contained in:
liang.he@intel.com 2025-10-29 02:29:26 +00:00
parent 29d5070e7f
commit ab079c1394

View File

@ -7,8 +7,11 @@ Usage:
Arguments: Arguments:
--headers: A list of header file paths to process. Each header file will be parsed, and a corresponding --headers: A list of header file paths to process. Each header file will be parsed, and a corresponding
"_checked.h" file will be generated with additional null pointer checks and error handling. "_checked.h" file will be generated with additional null pointer checks and error handling.
If not provided, a default list of headers under "core/iwasm/include/" will be used.
Example: Example:
python3 generate_checked_functions.py
# OR
python3 generate_checked_functions.py --headers core/iwasm/include/wasm_export.h python3 generate_checked_functions.py --headers core/iwasm/include/wasm_export.h
Description: Description:
@ -16,20 +19,25 @@ Description:
For each function, it generates a "checked" version that includes: For each function, it generates a "checked" version that includes:
- Null pointer checks for pointer parameters. - Null pointer checks for pointer parameters.
- Error handling using a `Result` struct. - Error handling using a `Result` struct.
- Support for variadic arguments (e.g., ...).
The generated "_checked.h" files include the original header file and define the `Result` struct, which The generated "_checked.h" files include the original header file and define the `Result` struct, which
encapsulates the return value and error codes. encapsulates the return value and error codes. The `Result` struct is dynamically generated based on the
return types of the functions in the header file.
Dependencies: Dependencies:
- pycparser: Install it using `pip install pycparser`. - pycparser: Install it using `pip install pycparser`.
- clang-format-14: Ensure it is installed for formatting the generated files.
Output: Output:
For each input header file, a corresponding "_checked.h" file is created in the same directory. For each input header file, a corresponding "_checked.h" file is created in the same directory.
The generated files are automatically formatted using clang-format-14.
""" """
from pycparser import c_ast, parse_file
import argparse import argparse
from pathlib import Path from pathlib import Path
from pycparser import c_ast, parse_file
import subprocess
# Constants for repeated strings # Constants for repeated strings
CPP_ARGS = [ CPP_ARGS = [
@ -226,14 +234,23 @@ def parse_arguments():
parser.add_argument( parser.add_argument(
"--headers", "--headers",
nargs="+", nargs="+",
required=True, required=False,
help="List of header file paths to process.", help="List of header file paths to process. Relative to the project root.",
default=[
"core/iwasm/include/aot_comp_option.h",
"core/iwasm/include/aot_export.h",
"core/iwasm/include/gc_export.h",
"core/iwasm/include/lib_export.h",
"core/iwasm/include/wasm_c_api.h",
"core/iwasm/include/wasm_export.h",
],
) )
return parser.parse_args() return parser.parse_args()
def generate_checked_headers(header_paths): def generate_checked_headers(header_paths):
"""Process each header file and generate checked versions.""" """Process each header file and generate checked versions."""
output_header = []
for input_header in header_paths: for input_header in header_paths:
input_path = Path(input_header) input_path = Path(input_header)
output_path = input_path.with_name(input_path.stem + "_checked.h") output_path = input_path.with_name(input_path.stem + "_checked.h")
@ -260,11 +277,18 @@ def generate_checked_headers(header_paths):
result_struct = generate_result_struct(return_types) result_struct = generate_result_struct(return_types)
write_checked_header(output_path, result_struct, functions, typedefs) write_checked_header(output_path, result_struct, functions, typedefs)
output_header.append(output_path)
return output_header
def main(): def main():
args = parse_arguments() args = parse_arguments()
generate_checked_headers(args.headers) generated_headers = generate_checked_headers(args.headers)
# format the generated files using clang-format-14
for header in generated_headers:
subprocess.run(["clang-format-14", "--style=file", "-i", str(header)])
if __name__ == "__main__": if __name__ == "__main__":