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

Conversation

aaryanshukla
Copy link
Contributor

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
@llvmbot llvmbot added the libc label Jun 28, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 28, 2024

@llvm/pr-subscribers-libc

Author: None (aaryanshukla)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/97079.diff

1 Files Affected:

  • (modified) libc/newhdrgen/yaml_to_classes.py (+67-3)
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)

@@ -23,6 +22,11 @@
from class_implementation.classes.object import Object


class MyDumper(yaml.Dumper):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Consider renaming this to something more descriptive (maybe IndentListDumper?)
  2. If you don't need this class anywhere else, you can define it in add_function_to_yaml(...) to reduce its scope.

"--add_function",
nargs=6,
metavar=(
"NAME",
Copy link
Contributor

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)...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

Copy link
Contributor

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
Copy link

github-actions bot commented Jun 28, 2024

✅ With the latest revision this PR passed the Python code formatter.

attributes = attributes.split(",") if attributes != "null" else []

return Function(
name=name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ordering here is different

@aaryanshukla
Copy link
Contributor Author

aaryanshukla commented Jul 1, 2024 via email

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aaryanshukla aaryanshukla merged commit 03e4647 into llvm:main Jul 1, 2024
6 checks passed
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
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
kbluck pushed a commit to kbluck/llvm-project that referenced this pull request Jul 6, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants