Skip to content

[libc] Elide extra space in hdrgen function declarations #127287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2025

Conversation

frobtech
Copy link
Contributor

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.

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.
@frobtech frobtech requested review from Caslyn and fabio-d February 15, 2025 00:51
@frobtech frobtech marked this pull request as ready for review February 15, 2025 00:52
@llvmbot llvmbot added the libc label Feb 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 15, 2025

@llvm/pr-subscribers-libc

Author: Roland McGrath (frobtech)

Changes

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.


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

3 Files Affected:

  • (modified) libc/utils/hdrgen/function.py (+4-1)
  • (modified) libc/utils/hdrgen/tests/expected_output/subdir/test.h (+2)
  • (modified) libc/utils/hdrgen/tests/input/subdir/test.yaml (+3)
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: []

@frobtech frobtech merged commit 68a82a2 into llvm:main Feb 15, 2025
9 of 13 checks passed
@frobtech frobtech deleted the p/libc-hdrgen-spaces branch February 15, 2025 01:10
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants