Skip to content

[libc] add LLVM_LIBC_CAST macro. #127319

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 4 commits into from
Feb 19, 2025
Merged

[libc] add LLVM_LIBC_CAST macro. #127319

merged 4 commits into from
Feb 19, 2025

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Feb 15, 2025

related: #127238

This patch adds a macro called LLVM_LIBC_CAST, similar to __BIONIC_CAST, for type conversion in endian.h.

@llvmbot
Copy link
Member

llvmbot commented Feb 15, 2025

@llvm/pr-subscribers-libc

Author: None (c8ef)

Changes

related: #127238

This patch adds a macro called LLVM_LIBC_CAST, similar to __BIONIC_CAST, for type conversion in endian.h.


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

1 Files Affected:

  • (modified) libc/include/__llvm-libc-common.h (+6)
diff --git a/libc/include/__llvm-libc-common.h b/libc/include/__llvm-libc-common.h
index a0fa506c01ab8..83682e0a440d8 100644
--- a/libc/include/__llvm-libc-common.h
+++ b/libc/include/__llvm-libc-common.h
@@ -47,6 +47,9 @@
 #define __NOEXCEPT throw()
 #endif
 
+#undef LLVM_LIBC_CAST
+#define LLVM_LIBC_CAST(cast, type, value) (cast<type>(value))
+
 #else // not __cplusplus
 
 #undef __BEGIN_C_DECLS
@@ -85,6 +88,9 @@
 #undef _Returns_twice
 #define _Returns_twice __attribute__((returns_twice))
 
+#undef LLVM_LIBC_CAST
+#define LLVM_LIBC_CAST(cast, type, value) ((type)(value))
+
 #endif // __cplusplus
 
 #endif // LLVM_LIBC_COMMON_H

@c8ef c8ef requested a review from lntue February 15, 2025 12:59
Copy link
Member

@nickdesaulniers nickdesaulniers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but please do use it now from endian.h

@@ -47,6 +47,9 @@
#define __NOEXCEPT throw()
#endif

#undef LLVM_LIBC_CAST
#define LLVM_LIBC_CAST(cast, type, value) (cast<type>(value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This merits a comment about its intended uses.
No macro should be defined in this file unless it's in the implementation namespace.
Sticking to the __ prefix seems like the best convention, with the exception of the C11 keywords that are in the _[A-Z] part of the implementation namespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

#define htole16(x) ((uint16_t)(x))
#define htole32(x) ((uint32_t)(x))
#define htole64(x) ((uint64_t)(x))
#define htole16(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just make the reinterpret cast implicit? Unsure if we'd ever need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bionic implementation omits it, while the glibc, musl, and freebsd implementations enforce it. Unsure which is more suitable.

@c8ef
Copy link
Contributor Author

c8ef commented Feb 18, 2025

The CI error seems unrelated.

@c8ef c8ef requested a review from jhuber6 February 18, 2025 00:57
#define htole16(x) ((uint16_t)(x))
#define htole32(x) ((uint32_t)(x))
#define htole64(x) ((uint64_t)(x))
#define htole16(x) __LLVM_LIBC_CAST(reinterpret_cast, uint16_t, x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be a reinterpret_cast. Since these types should be compatible I think this should be a static_cast.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@c8ef
Copy link
Contributor Author

c8ef commented Feb 19, 2025

Thank you for your review! I will merge this PR now and address any further comments in future patches.

@c8ef c8ef merged commit 826af17 into llvm:main Feb 19, 2025
11 of 13 checks passed
@c8ef c8ef deleted the bionic-cast branch February 19, 2025 15:28
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.

7 participants