[libc-commits] [libc] [libc] Make hdrgen support macro_header YAML field. (PR #123265)

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Thu Feb 13 13:53:27 PST 2025


================
@@ -32,11 +34,24 @@ def add_object(self, object):
     def add_function(self, function):
         self.functions.append(function)
 
+    def includes(self):
+        return sorted(
+            {
+                PurePath("llvm-libc-macros") / macro.header
+                for macro in self.macros
+                if macro.header is not None
+            }
+        )
+
     def public_api(self):
-        content = [""]
+        header_dir = PurePath(self.name).parent
+        content = [
+            f'#include "{file.relative_to(header_dir)}"' for file in self.includes()
+        ] + [""]
 
         for macro in self.macros:
-            content.append(f"{macro}\n")
+            if str(macro):
----------------
frobtech wrote:

That's actually how I tried to write it originally because I had the same misunderstanding.  I think it's a sign that the basic design of the string rendering API is questionable.  There is never a `None` in the `self.macros` list.  Rather, the `Macro` object (and all the others) implement custom conversion to `str`--which by definition must return a `str` and not any other type.  So for the "nothing to emit to the header" case, `Macro.__str__` now returns `""`.  Probably we should eventually clean up all the code not to use implicit conversion to `str` as the API for rendering but instead just have an explicit method about yielding a line for the output header, and then make that able to return `None`.  But I don't want to rework too much of the code right now.

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


More information about the libc-commits mailing list