[libc-commits] [libc] 03e4647 - [libc] implemented add_function to yaml hdrgen (#97079)
via libc-commits
libc-commits at lists.llvm.org
Mon Jul 1 15:44:02 PDT 2024
Author: aaryanshukla
Date: 2024-07-01T15:43:59-07:00
New Revision: 03e46475f510d9d7fa813dcfa1fd91894af39862
URL: https://github.com/llvm/llvm-project/commit/03e46475f510d9d7fa813dcfa1fd91894af39862
DIFF: https://github.com/llvm/llvm-project/commit/03e46475f510d9d7fa813dcfa1fd91894af39862.diff
LOG: [libc] implemented add_function to yaml hdrgen (#97079)
users can now input a new function in the CLI: the yaml, and .h will
be updated instantly
tested on different cases and does not change the yaml format in anyway
except decreasing extra spaces if present
Added:
Modified:
libc/newhdrgen/yaml_to_classes.py
Removed:
################################################################################
diff --git a/libc/newhdrgen/yaml_to_classes.py b/libc/newhdrgen/yaml_to_classes.py
index 7159dd9cc8881..9b52c9cf9bb7c 100644
--- a/libc/newhdrgen/yaml_to_classes.py
+++ b/libc/newhdrgen/yaml_to_classes.py
@@ -10,7 +10,6 @@
import yaml
-import re
import argparse
from pathlib import Path
@@ -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,
)
@@ -103,7 +102,79 @@ 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.
@@ -111,8 +182,12 @@ def main(yaml_file, h_def_file, output_dir):
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:
@@ -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)
More information about the libc-commits
mailing list