Skip to content

[libc] implemented add_function to yaml hdrgen #97079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 92 additions & 4 deletions libc/newhdrgen/yaml_to_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


import yaml
import re
import argparse

from pathlib import Path
Expand Down Expand Up @@ -54,10 +53,10 @@ def yaml_to_classes(yaml_data):
standards = (function_data.get("standards", None),)
header.add_function(
Function(
standards,
function_data["return_type"],
function_data["name"],
arguments,
standards,
guard,
attributes,
)
Expand Down Expand Up @@ -103,16 +102,92 @@ def fill_public_api(header_str, h_def_content):
return h_def_content.replace("%%public_api()", header_str, 1)


def main(yaml_file, h_def_file, output_dir):
def parse_function_details(details):
"""
Parse function details from a list of strings and return a Function object.

Args:
details: A list containing function details

Returns:
Function: An instance of Function initialized with the details.
"""
return_type, name, arguments, standards, guard, attributes = details
standards = standards.split(",") if standards != "null" else []
arguments = [arg.strip() for arg in arguments.split(",")]
attributes = attributes.split(",") if attributes != "null" else []

return Function(
return_type=return_type,
name=name,
arguments=arguments,
standards=standards,
guard=guard if guard != "null" else None,
attributes=attributes if attributes else [],
)


def add_function_to_yaml(yaml_file, function_details):
"""
Add a function to the YAML file.

Args:
yaml_file: The path to the YAML file.
function_details: A list containing function details (return_type, name, arguments, standards, guard, attributes).
"""
new_function = parse_function_details(function_details)

with open(yaml_file, "r") as f:
yaml_data = yaml.safe_load(f)

if "functions" not in yaml_data:
yaml_data["functions"] = []

function_dict = {
"name": new_function.name,
"standards": new_function.standards,
"return_type": new_function.return_type,
"arguments": [{"type": arg} for arg in new_function.arguments],
}

if new_function.guard:
function_dict["guard"] = new_function.guard

if new_function.attributes:
function_dict["attributes"] = new_function.attributes

yaml_data["functions"].append(function_dict)

class IndentYamlListDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(IndentYamlListDumper, self).increase_indent(flow, False)

with open(yaml_file, "w") as f:
yaml.dump(
yaml_data,
f,
Dumper=IndentYamlListDumper,
default_flow_style=False,
sort_keys=False,
)

print(f"Added function {new_function.name} to {yaml_file}")


def main(yaml_file, h_def_file, output_dir, add_function=None):
"""
Main function to generate header files from YAML and .h.def templates.

Args:
yaml_file: Path to the YAML file containing header specification.
h_def_file: Path to the .h.def template file.
output_dir: Directory to output the generated header file.
add_function: Details of the function to be added to the YAML file (if any).
"""

if add_function:
add_function_to_yaml(yaml_file, add_function)

header = load_yaml_file(yaml_file)

with open(h_def_file, "r") as f:
Expand Down Expand Up @@ -143,6 +218,19 @@ def main(yaml_file, h_def_file, output_dir):
default=".",
help="Directory to output the generated header file",
)
parser.add_argument(
"--add_function",
nargs=6,
metavar=(
"RETURN_TYPE",
"NAME",
"ARGUMENTS",
"STANDARDS",
"GUARD",
"ATTRIBUTES",
),
help="Add a function to the YAML file",
)
args = parser.parse_args()

main(args.yaml_file, args.h_def_file, args.output_dir)
main(args.yaml_file, args.h_def_file, args.output_dir, args.add_function)
Loading