[libc-commits] [libc] [libc] added grouping of guarded functions in header generation (PR #98532)

via libc-commits libc-commits at lists.llvm.org
Thu Jul 11 15:54:16 PDT 2024


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

>From bba46ffee512d9b6b1d85a391316c94aab92ba46 Mon Sep 17 00:00:00 2001
From: Rose Zhang <rosezhang at google.com>
Date: Thu, 11 Jul 2024 20:00:12 +0000
Subject: [PATCH 1/3] [libc] added grouping of guarded functions in header
 generation

Instead of #ifdef guards for each individual function, #ifdef and #endif
will surround all functions that have the same guard.
---
 .../class_implementation/classes/function.py  |  2 --
 libc/newhdrgen/header.py                      | 28 ++++++++++++++++++-
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/libc/newhdrgen/class_implementation/classes/function.py b/libc/newhdrgen/class_implementation/classes/function.py
index da26358e74506..ea5e8223a538e 100644
--- a/libc/newhdrgen/class_implementation/classes/function.py
+++ b/libc/newhdrgen/class_implementation/classes/function.py
@@ -29,6 +29,4 @@ def __str__(self):
             result = f"{self.return_type} {self.name}({arguments_str}) __NOEXCEPT;"
         else:
             result = f"{attributes_str} {self.return_type} {self.name}({arguments_str}) __NOEXCEPT;"
-        if self.guard:
-            result = f"#ifdef {self.guard}\n{result}\n#endif // {self.guard}"
         return result
diff --git a/libc/newhdrgen/header.py b/libc/newhdrgen/header.py
index ca960b639ab10..108bcbcfad01c 100644
--- a/libc/newhdrgen/header.py
+++ b/libc/newhdrgen/header.py
@@ -57,9 +57,35 @@ def __str__(self):
 
         content.append("\n__BEGIN_C_DECLS\n")
 
+        current_guard = None
         for function in self.functions:
-            content.append(str(function))
+            # due to sorting, all guarded functions will be at the end
+            if function.guard == None:
+                content.append(str(function))
+                content.append("")
+            else:
+                if current_guard == None:
+                    current_guard = function.guard
+                    content.append(f"#ifdef {current_guard}")
+                    content.append(str(function))
+                    content.append("")
+                elif current_guard == function.guard:
+                    content.append(str(function))
+                    content.append("")
+                else:
+                    content.pop()
+                    content.append(f"#endif // {current_guard}")
+                    content.append("")
+                    current_guard = function.guard
+                    content.append(f"#ifdef {current_guard}")
+                    content.append(str(function))
+                    content.append("")
+        if current_guard != None:
+            content.pop()
+            content.append(f"#endif // {current_guard}")
             content.append("")
+                                        
+
         for object in self.objects:
             content.append(str(object))
         if self.objects:

>From df82932939509ef401013b71917389ec2e05038f Mon Sep 17 00:00:00 2001
From: Rose Zhang <rosezhang at google.com>
Date: Thu, 11 Jul 2024 20:17:47 +0000
Subject: [PATCH 2/3] fixed darker formatting

---
 libc/newhdrgen/header.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/newhdrgen/header.py b/libc/newhdrgen/header.py
index 108bcbcfad01c..b3d7ff3ec27e3 100644
--- a/libc/newhdrgen/header.py
+++ b/libc/newhdrgen/header.py
@@ -83,8 +83,7 @@ def __str__(self):
         if current_guard != None:
             content.pop()
             content.append(f"#endif // {current_guard}")
-            content.append("")
-                                        
+            content.append("")      
 
         for object in self.objects:
             content.append(str(object))

>From aaa70af7a3f9bee24a25b926104cd92026160757 Mon Sep 17 00:00:00 2001
From: Rose Zhang <rosezhang at google.com>
Date: Thu, 11 Jul 2024 22:43:49 +0000
Subject: [PATCH 3/3] updated tests with grouped guarded functions

---
 .../tests/expected_output/test_header.h       | 20 +++++++++----
 libc/newhdrgen/tests/input/test_small.h.def   |  2 ++
 libc/newhdrgen/tests/input/test_small.yaml    | 29 +++++++++++++++----
 3 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/libc/newhdrgen/tests/expected_output/test_header.h b/libc/newhdrgen/tests/expected_output/test_header.h
index 02ca8ecf196ed..a777976134b04 100644
--- a/libc/newhdrgen/tests/expected_output/test_header.h
+++ b/libc/newhdrgen/tests/expected_output/test_header.h
@@ -10,7 +10,9 @@
 #define LLVM_LIBC_TEST_SMALL_H
 
 #include "__llvm-libc-common.h"
+#include "llvm-libc-macros/float16-macros.h"
 #include "llvm-libc-macros/test_small-macros.h"
+#include "llvm-libc-types/float128.h"
 
 #define MACRO_A 1
 
@@ -26,13 +28,21 @@ enum {
 
 __BEGIN_C_DECLS
 
-#ifdef FUNC_A_16
 CONST_FUNC_A void func_a() __NOEXCEPT;
-#endif // FUNC_A_16
 
-#ifdef FUNC_B_16
-CONST_FUNC_B int func_b(int, float) __NOEXCEPT;
-#endif // FUNC_B_16
+#ifdef LIBC_TYPES_HAS_FLOAT128
+float128 func_b() __NOEXCEPT;
+#endif // LIBC_TYPES_HAS_FLOAT128
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+_Float16 func_c(int, float) __NOEXCEPT;
+
+_Float16 func_d(int, float) __NOEXCEPT;
+#endif // LIBC_TYPES_HAS_FLOAT16
+
+#ifdef LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
+_Float16 func_e(float128) __NOEXCEPT;
+#endif // LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
 
 extern obj object_1;
 extern obj object_2;
diff --git a/libc/newhdrgen/tests/input/test_small.h.def b/libc/newhdrgen/tests/input/test_small.h.def
index de39a8b7e254d..075be957b8136 100644
--- a/libc/newhdrgen/tests/input/test_small.h.def
+++ b/libc/newhdrgen/tests/input/test_small.h.def
@@ -10,7 +10,9 @@
 #define LLVM_LIBC_TEST_SMALL_H
 
 #include "__llvm-libc-common.h"
+#include "llvm-libc-macros/float16-macros.h"
 #include "llvm-libc-macros/test_small-macros.h"
+#include "llvm-libc-types/float128.h"
 
 %%public_api()
 
diff --git a/libc/newhdrgen/tests/input/test_small.yaml b/libc/newhdrgen/tests/input/test_small.yaml
index 7d657d9ecad5f..f1f9f020ce6a4 100644
--- a/libc/newhdrgen/tests/input/test_small.yaml
+++ b/libc/newhdrgen/tests/input/test_small.yaml
@@ -23,16 +23,35 @@ functions:
     arguments: []
     standards: 
       - stdc
-    guard: FUNC_A_16
     attributes: 
       - CONST_FUNC_A
   - name: func_b
-    return_type: int
+    return_type: float128
+    arguments: []
+    standards: 
+      - stdc
+    guard: LIBC_TYPES_HAS_FLOAT128
+  - name: func_c
+    return_type: _Float16
     arguments:
       - type: int
       - type: float
     standards: 
       - stdc
-    guard: FUNC_B_16
-    attributes: 
-      - CONST_FUNC_B
+    guard: LIBC_TYPES_HAS_FLOAT16
+  - name: func_d
+    return_type: _Float16
+    arguments:
+      - type: int
+      - type: float
+    standards: 
+      - stdc
+    guard: LIBC_TYPES_HAS_FLOAT16
+  - name: func_e
+    return_type: _Float16
+    arguments:
+      - type: float128
+    standards: 
+      - stdc
+    guard: LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
+



More information about the libc-commits mailing list