[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:47 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: None (RoseZhang03)

<details>
<summary>Changes</summary>

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


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


2 Files Affected:

- (modified) libc/newhdrgen/class_implementation/classes/function.py (-2) 
- (modified) libc/newhdrgen/header.py (+27-1) 


``````````diff
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")

``````````

</details>


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


More information about the libc-commits mailing list