[libc-commits] [libc] [libc] added newhdrgen class implementation (PR #96710)

via libc-commits libc-commits at lists.llvm.org
Tue Jun 25 16:27:59 PDT 2024


https://github.com/RoseZhang03 updated https://github.com/llvm/llvm-project/pull/96710

>From e50a92a43ff045e0eb3be14ce8b3f37b6b782d67 Mon Sep 17 00:00:00 2001
From: Rose Zhang <rosezhang at google.com>
Date: Tue, 25 Jun 2024 22:43:51 +0000
Subject: [PATCH 1/2] [libc] added newhdrgen class implementation

Added a class representation of a libc header file, allowing for easier
conversion from YAML to .h file output.

Classes include:
- Function (representing function headers)
- Include (representing various include statements found on a header
  file)
- Macro (representing macro definitions)
- Enumeration (representing enum definitions)
- Type (representing include statements for NamedTypes)
- Object (representing ObjectSpec defintitions)
---
 .../classes/enumeration.py                    | 20 ++++++++++++++
 .../class_implementation/classes/function.py  | 26 +++++++++++++++++++
 .../class_implementation/classes/include.py   | 17 ++++++++++++
 .../class_implementation/classes/macro.py     | 21 +++++++++++++++
 .../class_implementation/classes/object.py    | 18 +++++++++++++
 .../class_implementation/classes/type.py      | 17 ++++++++++++
 6 files changed, 119 insertions(+)
 create mode 100644 libc/newhdrgen/class_implementation/classes/enumeration.py
 create mode 100644 libc/newhdrgen/class_implementation/classes/function.py
 create mode 100644 libc/newhdrgen/class_implementation/classes/include.py
 create mode 100644 libc/newhdrgen/class_implementation/classes/macro.py
 create mode 100644 libc/newhdrgen/class_implementation/classes/object.py
 create mode 100644 libc/newhdrgen/class_implementation/classes/type.py

diff --git a/libc/newhdrgen/class_implementation/classes/enumeration.py b/libc/newhdrgen/class_implementation/classes/enumeration.py
new file mode 100644
index 0000000000000..c20f0a33ec174
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/enumeration.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+#
+# ====-- Enumeration class for libc function headers ----------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Enumeration:
+     def __init__(self, name, value=None):
+        self.name = name
+        self.value = value
+     def __str__(self):
+         if self.value != None:
+            return f"{self.name} = {self.value}"
+         else:
+            return f"{self.name}"      
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/function.py b/libc/newhdrgen/class_implementation/classes/function.py
new file mode 100644
index 0000000000000..b9c089ca5bfc8
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/function.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+#
+# ====-- Function class for libc function headers -------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Function:
+    def __init__(self, return_type, name, arguments, guard=None, attributes=None):
+        self.return_type = return_type
+        self.name = name
+        self.arguments = [arg if isinstance(arg, str) else arg['type'] for arg in arguments]
+        self.guard = guard
+        self.attributes = attributes or []
+
+    def __str__(self):
+        args_str = ", ".join(self.arguments)
+        attributes_str = " ".join(self.attributes)
+        result = f"{self.return_type} {self.name}({args_str}){attributes_str};"
+        if self.guard:
+            result = f"#ifdef {self.guard}\n{result}\n#endif // {self.guard}"
+        return result
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/include.py b/libc/newhdrgen/class_implementation/classes/include.py
new file mode 100644
index 0000000000000..b4583c66c88fc
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/include.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+#
+# ====-- Include class for libc function headers --------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Include:
+    def __init__(self, name):
+        self.name = name
+
+    def __str__(self):
+        return f'#include "{self.name}"'
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/macro.py b/libc/newhdrgen/class_implementation/classes/macro.py
new file mode 100644
index 0000000000000..9c562708ee284
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/macro.py
@@ -0,0 +1,21 @@
+#!/usr/bin/env python
+#
+# ====-- Macro class for libc function headers ----------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Macro:
+    def __init__(self, name, value = None):
+        self.name = name
+        self.value = value
+
+    def __str__(self):
+        if self.value != None:
+            return f'#define {self.name} {self.value}'
+        else:
+            return f"#define {self.name}"
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/object.py b/libc/newhdrgen/class_implementation/classes/object.py
new file mode 100644
index 0000000000000..ec187a5cc8a95
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/object.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+#
+# ====-- Object class for libc function headers ---------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Object:
+    def __init__(self, name, type):
+        self.name = name
+        self.type = type
+
+    def __str__(self):
+        return f'extern {self.type} {self.name}'
\ No newline at end of file
diff --git a/libc/newhdrgen/class_implementation/classes/type.py b/libc/newhdrgen/class_implementation/classes/type.py
new file mode 100644
index 0000000000000..0882e3a1e0d0b
--- /dev/null
+++ b/libc/newhdrgen/class_implementation/classes/type.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+#
+# ====-- Type class for libc function headers -----------------*- python -*--==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+
+class Type:
+    def __init__(self, type_name):
+        self.type_name = type_name
+
+    def __str__(self):
+        return f"#include <llvm-libc-types/{self.type_name}.h>"
\ No newline at end of file

>From a2120c2873b13a42ef7d4862f440c51fd9029688 Mon Sep 17 00:00:00 2001
From: Rose Zhang <rosezhang at google.com>
Date: Tue, 25 Jun 2024 23:27:41 +0000
Subject: [PATCH 2/2] resolved formatting issues, set Function arguments
 parameter as a dictionary, added standard parameter in Function

---
 .../class_implementation/classes/enumeration.py | 17 +++++++++--------
 .../class_implementation/classes/function.py    | 11 +++++++----
 .../class_implementation/classes/include.py     |  2 +-
 .../class_implementation/classes/macro.py       |  6 +++---
 .../class_implementation/classes/object.py      |  2 +-
 .../class_implementation/classes/type.py        |  2 +-
 6 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/libc/newhdrgen/class_implementation/classes/enumeration.py b/libc/newhdrgen/class_implementation/classes/enumeration.py
index c20f0a33ec174..28504f355886c 100644
--- a/libc/newhdrgen/class_implementation/classes/enumeration.py
+++ b/libc/newhdrgen/class_implementation/classes/enumeration.py
@@ -10,11 +10,12 @@
 
 
 class Enumeration:
-     def __init__(self, name, value=None):
-        self.name = name
-        self.value = value
-     def __str__(self):
-         if self.value != None:
-            return f"{self.name} = {self.value}"
-         else:
-            return f"{self.name}"      
\ No newline at end of file
+   def __init__(self, name, value=None):
+      self.name = name
+      self.value = value
+   
+   def __str__(self):
+      if self.value != None:
+         return f"{self.name} = {self.value}"
+      else:
+         return f"{self.name}"
diff --git a/libc/newhdrgen/class_implementation/classes/function.py b/libc/newhdrgen/class_implementation/classes/function.py
index b9c089ca5bfc8..b1fa4c6c32a58 100644
--- a/libc/newhdrgen/class_implementation/classes/function.py
+++ b/libc/newhdrgen/class_implementation/classes/function.py
@@ -10,12 +10,15 @@
 
 
 class Function:
-    def __init__(self, return_type, name, arguments, guard=None, attributes=None):
+    def __init__(self, standard, return_type, name, arguments, guard=None, attributes=[]):
+        self.standard = standard
         self.return_type = return_type
         self.name = name
-        self.arguments = [arg if isinstance(arg, str) else arg['type'] for arg in arguments]
+        self.arguments = [
+            arg['type'] for arg in arguments
+        ]
         self.guard = guard
-        self.attributes = attributes or []
+        self.attributes = attributes
 
     def __str__(self):
         args_str = ", ".join(self.arguments)
@@ -23,4 +26,4 @@ def __str__(self):
         result = f"{self.return_type} {self.name}({args_str}){attributes_str};"
         if self.guard:
             result = f"#ifdef {self.guard}\n{result}\n#endif // {self.guard}"
-        return result
\ No newline at end of file
+        return result
diff --git a/libc/newhdrgen/class_implementation/classes/include.py b/libc/newhdrgen/class_implementation/classes/include.py
index b4583c66c88fc..2657f2e7c931c 100644
--- a/libc/newhdrgen/class_implementation/classes/include.py
+++ b/libc/newhdrgen/class_implementation/classes/include.py
@@ -14,4 +14,4 @@ def __init__(self, name):
         self.name = name
 
     def __str__(self):
-        return f'#include "{self.name}"'
\ No newline at end of file
+        return f'#include "{self.name}"'
diff --git a/libc/newhdrgen/class_implementation/classes/macro.py b/libc/newhdrgen/class_implementation/classes/macro.py
index 9c562708ee284..bf17ae6b6c5ab 100644
--- a/libc/newhdrgen/class_implementation/classes/macro.py
+++ b/libc/newhdrgen/class_implementation/classes/macro.py
@@ -10,12 +10,12 @@
 
 
 class Macro:
-    def __init__(self, name, value = None):
+    def __init__(self, name, value=None):
         self.name = name
         self.value = value
 
     def __str__(self):
         if self.value != None:
-            return f'#define {self.name} {self.value}'
+            return f"#define {self.name} {self.value}"
         else:
-            return f"#define {self.name}"
\ No newline at end of file
+            return f"#define {self.name}"
diff --git a/libc/newhdrgen/class_implementation/classes/object.py b/libc/newhdrgen/class_implementation/classes/object.py
index ec187a5cc8a95..c65a82e1a660d 100644
--- a/libc/newhdrgen/class_implementation/classes/object.py
+++ b/libc/newhdrgen/class_implementation/classes/object.py
@@ -15,4 +15,4 @@ def __init__(self, name, type):
         self.type = type
 
     def __str__(self):
-        return f'extern {self.type} {self.name}'
\ No newline at end of file
+        return f"extern {self.type} {self.name}"
diff --git a/libc/newhdrgen/class_implementation/classes/type.py b/libc/newhdrgen/class_implementation/classes/type.py
index 0882e3a1e0d0b..8e4a8f3cc9c58 100644
--- a/libc/newhdrgen/class_implementation/classes/type.py
+++ b/libc/newhdrgen/class_implementation/classes/type.py
@@ -14,4 +14,4 @@ def __init__(self, type_name):
         self.type_name = type_name
 
     def __str__(self):
-        return f"#include <llvm-libc-types/{self.type_name}.h>"
\ No newline at end of file
+        return f"#include <llvm-libc-types/{self.type_name}.h>"



More information about the libc-commits mailing list