[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 13:03:12 PDT 2024


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

Instead of #ifdef guards for each individual function, #ifdef and #endif
will surround all functions that have the same guard.


>From d95b2819bffb6eee3e7e935adddb9bedc2ca90a6 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] [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 d9e9c68dfc5f4..eb8199550ba33 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))
         content.append("__END_C_DECLS")



More information about the libc-commits mailing list