Skip to content

Commit 68a82a2

Browse files
authored
[libc] Elide extra space in hdrgen function declarations (#127287)
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.
1 parent 25e4333 commit 68a82a2

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

libc/utils/hdrgen/function.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,12 @@ def collapse(type_string):
8181
def __str__(self):
8282
attrs_str = "".join(f"{attr} " for attr in self.attributes)
8383
arguments_str = ", ".join(self.arguments) if self.arguments else "void"
84-
return attrs_str + f"{self.return_type} {self.name}({arguments_str})"
84+
# The rendering of the return type may look like `int` or it may look
85+
# like `int *` (and other examples). For `int`, a space is always
86+
# needed to separate the tokens. For `int *`, no whitespace matters to
87+
# the syntax one way or the other, but an extra space between `*` and
88+
# the function identifier is not the canonical style.
89+
type_str = str(self.return_type)
90+
if type_str[-1].isalnum() or type_str[-1] == "_":
91+
type_str += " "
92+
return attrs_str + type_str + self.name + "(" + arguments_str + ")"

libc/utils/hdrgen/tests/expected_output/subdir/test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type_a func(type_b) __NOEXCEPT;
1919

2020
void gnufunc(type_a) __NOEXCEPT;
2121

22+
int *ptrfunc(void) __NOEXCEPT;
23+
2224
__END_C_DECLS
2325

2426
#endif // LLVM_LIBC_SUBDIR_TEST_H

libc/utils/hdrgen/tests/input/subdir/test.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ functions:
1212
- type: type_a
1313
standards:
1414
- gnu
15+
- name: ptrfunc
16+
return_type: int *
17+
arguments: []

0 commit comments

Comments
 (0)