[libc-commits] [libc] [libc] implemented add_function to yaml hdrgen (PR #97079)

via libc-commits libc-commits at lists.llvm.org
Fri Jun 28 14:06:37 PDT 2024


https://github.com/aaryanshukla updated https://github.com/llvm/llvm-project/pull/97079

>From ef689d2eb87c4ed717bbd399ab756e36c88010c2 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Fri, 28 Jun 2024 16:14:44 +0000
Subject: [PATCH 1/5] [libc] implemented add_function to yaml hdrgen

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
---
 libc/newhdrgen/yaml_to_classes.py | 70 +++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 3 deletions(-)

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)

>From cb6116ccb4bb77cb87ad2b6a67f9938ed3d32a3c Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Fri, 28 Jun 2024 17:21:52 +0000
Subject: [PATCH 2/5] addressed comments regarding naming/scope

---
 libc/newhdrgen/yaml_to_classes.py | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/libc/newhdrgen/yaml_to_classes.py b/libc/newhdrgen/yaml_to_classes.py
index 79700b1b54615..89547d2669618 100644
--- a/libc/newhdrgen/yaml_to_classes.py
+++ b/libc/newhdrgen/yaml_to_classes.py
@@ -22,10 +22,6 @@
 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):
     """
@@ -115,7 +111,13 @@ def add_function_to_yaml(yaml_file, function_details):
         yaml_file: The path to the YAML file.
         function_details: A list containing function details:
         (name, return_type, guard, attributes, arguments, standards).
+        
     """
+    
+    class IndentYamlDumper(yaml.Dumper):
+        def increase_indent(self, flow=False, indentless=False):
+            return super(IndentYamlDumper, self).increase_indent(flow, False)
+
     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(",")]
@@ -144,7 +146,7 @@ def add_function_to_yaml(yaml_file, function_details):
 
     with open(yaml_file, "w") as f:
         yaml.dump(
-            yaml_data, f, Dumper=MyDumper, default_flow_style=False, sort_keys=False
+            yaml_data, f, Dumper=IndentYamlDumper, default_flow_style=False, sort_keys=False
         )
 
     print(f"Added function {name} to {yaml_file}")

>From 35e2d85aa4481f683ad78bc2bd315990587cd867 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Fri, 28 Jun 2024 17:55:12 +0000
Subject: [PATCH 3/5] changed class name and moved class

---
 libc/newhdrgen/yaml_to_classes.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libc/newhdrgen/yaml_to_classes.py b/libc/newhdrgen/yaml_to_classes.py
index 89547d2669618..8f7d8c3904475 100644
--- a/libc/newhdrgen/yaml_to_classes.py
+++ b/libc/newhdrgen/yaml_to_classes.py
@@ -114,9 +114,9 @@ def add_function_to_yaml(yaml_file, function_details):
         
     """
     
-    class IndentYamlDumper(yaml.Dumper):
+    class IndentYamlListDumper(yaml.Dumper):
         def increase_indent(self, flow=False, indentless=False):
-            return super(IndentYamlDumper, self).increase_indent(flow, False)
+            return super(IndentYamlListDumper, self).increase_indent(flow, False)
 
     name, return_type, guard, attributes, arguments, standards = function_details
     attributes = attributes.split(",") if attributes != "null" else []
@@ -146,7 +146,7 @@ def increase_indent(self, flow=False, indentless=False):
 
     with open(yaml_file, "w") as f:
         yaml.dump(
-            yaml_data, f, Dumper=IndentYamlDumper, default_flow_style=False, sort_keys=False
+            yaml_data, f, Dumper=IndentYamlListDumper, default_flow_style=False, sort_keys=False
         )
 
     print(f"Added function {name} to {yaml_file}")

>From 19e58746f76e4d5a9bec0a9b8826b0e1acb14827 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Fri, 28 Jun 2024 21:03:25 +0000
Subject: [PATCH 4/5] adding a function now uses an object for details

created extra parser function for object to hold values for CL
---
 libc/newhdrgen/yaml_to_classes.py | 80 ++++++++++++++++++-------------
 1 file changed, 48 insertions(+), 32 deletions(-)

diff --git a/libc/newhdrgen/yaml_to_classes.py b/libc/newhdrgen/yaml_to_classes.py
index 8f7d8c3904475..c64b601279939 100644
--- a/libc/newhdrgen/yaml_to_classes.py
+++ b/libc/newhdrgen/yaml_to_classes.py
@@ -22,7 +22,6 @@
 from class_implementation.classes.object import Object
 
 
-
 def yaml_to_classes(yaml_data):
     """
     Convert YAML data to header classes.
@@ -103,53 +102,70 @@ def fill_public_api(header_str, h_def_content):
     return h_def_content.replace("%%public_api()", header_str, 1)
 
 
-def add_function_to_yaml(yaml_file, function_details):
+def parse_function_details(details):
     """
-    Add a function to the YAML file.
+    Parse function details from a list of strings and return a Function object.
 
     Args:
-        yaml_file: The path to the YAML file.
-        function_details: A list containing function details:
-        (name, return_type, guard, attributes, arguments, standards).
-        
-    """
-    
-    class IndentYamlListDumper(yaml.Dumper):
-        def increase_indent(self, flow=False, indentless=False):
-            return super(IndentYamlListDumper, self).increase_indent(flow, False)
+        details: A list containing function details
 
-    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(",")]
+    Returns:
+        Function: An instance of Function initialized with the details.
+    """
+    name, standards, return_type, arguments, 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 []
 
-    new_function = {
-        "name": name,
-        "standard": standards,
-        "return_type": return_type,
-        "arguments": arguments,
-    }
+    return Function(
+        name=name,
+        standards=standards,
+        return_type=return_type,
+        arguments=arguments,
+        guard=guard if guard != "null" else None,
+        attributes=attributes if attributes else None
+    )
 
-    if guard != "null":
-        new_function["guard"] = guard
 
-    if attributes:
-        new_function["attributes"] = attributes
+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, standards, return_type, arguments, 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"] = []
 
-    yaml_data["functions"].append(new_function)
+    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
-        )
+        yaml.dump(yaml_data, f, Dumper=IndentYamlListDumper, default_flow_style=False, sort_keys=False)
 
-    print(f"Added function {name} to {yaml_file}")
+    print(f"Added function {new_function.name} to {yaml_file}")
 
 
 def main(yaml_file, h_def_file, output_dir, add_function=None):
@@ -201,11 +217,11 @@ def main(yaml_file, h_def_file, output_dir, add_function=None):
         nargs=6,
         metavar=(
             "NAME",
+            "STANDARDS",
             "RETURN_TYPE",
+            "ARGUMENTS",
             "GUARD",
             "ATTRIBUTES",
-            "ARGUMENTS",
-            "STANDARDS",
         ),
         help="Add a function to the YAML file",
     )

>From a32400a48b79d19b47563cc6745c2d4d4a6543da Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Fri, 28 Jun 2024 21:06:09 +0000
Subject: [PATCH 5/5] black formatting added

---
 libc/newhdrgen/yaml_to_classes.py | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/libc/newhdrgen/yaml_to_classes.py b/libc/newhdrgen/yaml_to_classes.py
index c64b601279939..b4b7a881a66ad 100644
--- a/libc/newhdrgen/yaml_to_classes.py
+++ b/libc/newhdrgen/yaml_to_classes.py
@@ -123,7 +123,7 @@ def parse_function_details(details):
         return_type=return_type,
         arguments=arguments,
         guard=guard if guard != "null" else None,
-        attributes=attributes if attributes else None
+        attributes=attributes if attributes else None,
     )
 
 
@@ -136,7 +136,7 @@ def add_function_to_yaml(yaml_file, function_details):
         function_details: A list containing function details (name, standards, return_type, arguments, guard, attributes).
     """
     new_function = parse_function_details(function_details)
-    
+
     with open(yaml_file, "r") as f:
         yaml_data = yaml.safe_load(f)
 
@@ -163,7 +163,13 @@ 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)
+        yaml.dump(
+            yaml_data,
+            f,
+            Dumper=IndentYamlListDumper,
+            default_flow_style=False,
+            sort_keys=False,
+        )
 
     print(f"Added function {new_function.name} to {yaml_file}")
 



More information about the libc-commits mailing list