[libc-commits] [libc] [libc] Elide extra space in hdrgen function declarations (PR #127287)
Roland McGrath via libc-commits
libc-commits at lists.llvm.org
Fri Feb 14 17:09:36 PST 2025
https://github.com/frobtech updated https://github.com/llvm/llvm-project/pull/127287
>From aa0af95d4bdfdf97ec1372a2a1eb4f582f76745d Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Thu, 13 Feb 2025 23:33:37 -0800
Subject: [PATCH 1/2] [libc] Elide extra space in hdrgen function declarations
When the return type's rendering already doesn't end with an
identifier character, such as when it's `T *`, then idiomatic
syntax does not include a space before the `(` and arguments.
---
libc/utils/hdrgen/function.py | 5 ++++-
libc/utils/hdrgen/tests/expected_output/subdir/test.h | 2 ++
libc/utils/hdrgen/tests/input/subdir/test.yaml | 3 +++
3 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/libc/utils/hdrgen/function.py b/libc/utils/hdrgen/function.py
index bccd2c2caa2f5..24b28f7e3c7ff 100644
--- a/libc/utils/hdrgen/function.py
+++ b/libc/utils/hdrgen/function.py
@@ -81,4 +81,7 @@ def collapse(type_string):
def __str__(self):
attrs_str = "".join(f"{attr} " for attr in self.attributes)
arguments_str = ", ".join(self.arguments) if self.arguments else "void"
- return attrs_str + f"{self.return_type} {self.name}({arguments_str})"
+ type_str = str(self.return_type)
+ if type_str[-1].isalnum() or type_str[-1] == "_":
+ type_str += " "
+ return attrs_str + type_str + self.name + "(" + arguments_str + ")"
diff --git a/libc/utils/hdrgen/tests/expected_output/subdir/test.h b/libc/utils/hdrgen/tests/expected_output/subdir/test.h
index e968f92bf4e6d..20bab502e6821 100644
--- a/libc/utils/hdrgen/tests/expected_output/subdir/test.h
+++ b/libc/utils/hdrgen/tests/expected_output/subdir/test.h
@@ -19,6 +19,8 @@ type_a func(type_b) __NOEXCEPT;
void gnufunc(type_a) __NOEXCEPT;
+int *ptrfunc(void) __NOEXCEPT;
+
__END_C_DECLS
#endif // LLVM_LIBC_SUBDIR_TEST_H
diff --git a/libc/utils/hdrgen/tests/input/subdir/test.yaml b/libc/utils/hdrgen/tests/input/subdir/test.yaml
index f325363e09cde..5bc8b29e334f8 100644
--- a/libc/utils/hdrgen/tests/input/subdir/test.yaml
+++ b/libc/utils/hdrgen/tests/input/subdir/test.yaml
@@ -12,3 +12,6 @@ functions:
- type: type_a
standards:
- gnu
+ - name: ptrfunc
+ return_type: int *
+ arguments: []
>From eed21f5fa1a9407dd89322b49503a58caf42f79a Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Fri, 14 Feb 2025 17:09:07 -0800
Subject: [PATCH 2/2] Add comment
---
libc/utils/hdrgen/function.py | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/libc/utils/hdrgen/function.py b/libc/utils/hdrgen/function.py
index 24b28f7e3c7ff..28af05f78d897 100644
--- a/libc/utils/hdrgen/function.py
+++ b/libc/utils/hdrgen/function.py
@@ -81,6 +81,11 @@ def collapse(type_string):
def __str__(self):
attrs_str = "".join(f"{attr} " for attr in self.attributes)
arguments_str = ", ".join(self.arguments) if self.arguments else "void"
+ # The rendering of the return type may look like `int` or it may look
+ # like `int *` (and other examples). For `int`, a space is always
+ # needed to separate the tokens. For `int *`, no whitespace matters to
+ # the syntax one way or the other, but an extra space between `*` and
+ # the function identifier is not the canonical style.
type_str = str(self.return_type)
if type_str[-1].isalnum() or type_str[-1] == "_":
type_str += " "
More information about the libc-commits
mailing list