Skip to content

[libc] Allow each function can have extra attributes by defining LLVM_LIBC_FUNCTION_ATTR_func macro. #116160

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
Nov 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions libc/src/__support/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,24 @@
#define LLVM_LIBC_FUNCTION_ATTR
#endif

// Allow each function `func` can have extra attributes specified by defining:
// `LLVM_LIBC_FUNCTION_ATTR_func` macro, which should always start with
// "LLVM_LIBC_EMPTY,"
//
// For example:
// #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, __attribute__((weak))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, __attribute__((weak))
// #define LLVM_LIBC_FUNCTION_ATTR_memcpy LLVM_LIBC_EMPTY, [[gnu::weak]]

Let's encourage the usage of the C++11/C23 style attribute syntax, rather than the GNU C extension syntax.

Though, don't we expect this to be used by the command line cmake invocation? Perhaps an example of that in this comment, or moving our bazel files over to use that would be welcome additions to this PR?

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps worth documenting any new command line options in docs/dev/implementation_standard.rst.

#define LLVM_LIBC_EMPTY

#define GET_SECOND(first, second, ...) second
#define EXPAND_THEN_SECOND(name) GET_SECOND(name, LLVM_LIBC_EMPTY, )

#define LLVM_LIBC_ATTR(name) EXPAND_THEN_SECOND(LLVM_LIBC_FUNCTION_ATTR_##name)
#define EXPAND_ATTR(name) LLVM_LIBC_ATTR(name)
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 you need EXPAND_ATTR and can just use LLVM_LIBC_ATTR below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you're right, EXPAND_ATTR is not needed.


// MacOS needs to be excluded because it does not support aliasing.
#if defined(LIBC_COPT_PUBLIC_PACKAGING) && (!defined(__APPLE__))
#define LLVM_LIBC_FUNCTION_IMPL(type, name, arglist) \
EXPAND_ATTR(name) \
LLVM_LIBC_FUNCTION_ATTR decltype(LIBC_NAMESPACE::name) \
__##name##_impl__ __asm__(#name); \
decltype(LIBC_NAMESPACE::name) name [[gnu::alias(#name)]]; \
Expand Down
Loading