-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
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
@llvm/pr-subscribers-libc Author: None (aaryanshukla) Changesusers can now input a new function in the CLI: the yaml, and .h will Full diff: https://github.com/llvm/llvm-project/pull/97079.diff 1 Files Affected:
diff --git a/libc/newhdrgen/yaml_to_classes.py b/libc/newhdrgen/yaml_to_classes.py
index 7159dd9cc8881..79700b1b54615 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
@@ -23,6 +22,11 @@
from class_implementation.classes.object import Object
+class MyDumper(yaml.Dumper):
+ def increase_indent(self, flow=False, indentless=False):
+ return super(MyDumper, self).increase_indent(flow, False)
+
+
def yaml_to_classes(yaml_data):
"""
Convert YAML data to header classes.
@@ -103,7 +107,50 @@ 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 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:
+ (name, return_type, guard, attributes, arguments, standards).
+ """
+ name, return_type, guard, attributes, arguments, standards = function_details
+ attributes = attributes.split(",") if attributes != "null" else []
+ arguments = [{"type": arg.strip()} for arg in arguments.split(",")]
+ standards = standards.split(",") if standards != "null" else []
+
+ new_function = {
+ "name": name,
+ "standard": standards,
+ "return_type": return_type,
+ "arguments": arguments,
+ }
+
+ if guard != "null":
+ new_function["guard"] = guard
+
+ if attributes:
+ new_function["attributes"] = attributes
+
+ with open(yaml_file, "r") as f:
+ yaml_data = yaml.safe_load(f)
+
+ if "functions" not in yaml_data:
+ yaml_data["functions"] = []
+
+ yaml_data["functions"].append(new_function)
+
+ with open(yaml_file, "w") as f:
+ yaml.dump(
+ yaml_data, f, Dumper=MyDumper, default_flow_style=False, sort_keys=False
+ )
+
+ print(f"Added 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 +158,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 +194,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=(
+ "NAME",
+ "RETURN_TYPE",
+ "GUARD",
+ "ATTRIBUTES",
+ "ARGUMENTS",
+ "STANDARDS",
+ ),
+ 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)
|
libc/newhdrgen/yaml_to_classes.py
Outdated
@@ -23,6 +22,11 @@ | |||
from class_implementation.classes.object import Object | |||
|
|||
|
|||
class MyDumper(yaml.Dumper): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Consider renaming this to something more descriptive (maybe
IndentListDumper
?) - If you don't need this class anywhere else, you can define it in
add_function_to_yaml(...)
to reduce its scope.
libc/newhdrgen/yaml_to_classes.py
Outdated
"--add_function", | ||
nargs=6, | ||
metavar=( | ||
"NAME", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably be in the same order that you'd find them in a function definition (i.e. RETURN_TYPE NAME (ARGUMENTS)...
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems like the same order as before, maybe the formatter reordered them?
created extra parser function for object to hold values for CL
✅ With the latest revision this PR passed the Python code formatter. |
libc/newhdrgen/yaml_to_classes.py
Outdated
attributes = attributes.split(",") if attributes != "null" else [] | ||
|
||
return Function( | ||
name=name, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ordering here is different
I tested with the function class we have right now and the results are the
same, I don’t think we need to edit the class because we are appending
values so order doesn’t make a difference.
…On Mon, Jul 1, 2024 at 10:26 AM RoseZhang03 ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In libc/newhdrgen/yaml_to_classes.py
<#97079 (comment)>:
> + """
+ 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(
+ name=name,
Ordering here is different
------------------------------
In libc/newhdrgen/yaml_to_classes.py
<#97079 (comment)>:
> + name=name,
+ standards=standards,
+ return_type=return_type,
+ arguments=arguments,
+ guard=guard if guard != "null" else None,
+ attributes=attributes if attributes else None,
+ )
+
+
+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).
Keep consistent ordering. For the patch I will go with this one.
—
Reply to this email directly, view it on GitHub
<#97079 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AMZZRVDJZ6MDUKHK7PARFMTZKGGL5AVCNFSM6AAAAABKCFYHH6VHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDCNJRHEYDINBTG4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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
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
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