Skip to content

[libc] Add LIBC_NAMESPACE_DECL macro #97109

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 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions libc/docs/dev/clang_tidy_checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ This check that ensures any function call resolves to a function within the
void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {}
}

..
TODO(97655): The clang-tidy check should be updated to ensure the namespace
declaration uses LIBC_NAMESPACE_DECL as opposed to LIBC_NAMESPACE. The former
should be used for accessing globals in LIBC_NAMESPACE rather than declaration.


callee-namespace
----------------
Expand Down
31 changes: 31 additions & 0 deletions libc/docs/dev/code_style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,34 @@ Patches containing any amount of Assembly ideally should be approved by 2
maintainers. llvm-libc maintainers reserve the right to reject Assembly
contributions that they feel could be better maintained if rewritten in C++,
and to revisit this policy in the future.

LIBC_NAMESPACE_DECL
===================

llvm-libc provides a macro `LIBC_NAMESPACE` which contains internal implementations of
libc functions and globals. This macro should only be used as an
identifier for accessing such symbols within the namespace (like `LIBC_NAMESPACE::cpp::max`).
Any usage of this namespace for declaring or defining internal symbols should
instead use `LIBC_NAMESPACE_DECL` which declares `LIBC_NAMESPACE` with hidden visibility.

Example usage:

.. code-block:: c++

#include "src/__support/macros/config.h" // The macro is defined here.

namespace LIBC_NAMESPACE_DECL {

void new_function() {
...
}

} // LIBC_NAMESPACE_DECL

Having hidden visibility on the namespace ensures extern declarations in a given TU
have known visibility and never generate GOT indirextions. The attribute guarantees
this independently of global compile options and build systems.

..
TODO(97655): We should have a clang-tidy check to enforce this and a
fixit implementation.
12 changes: 6 additions & 6 deletions libc/docs/dev/implementation_standard.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ example. The ``isalpha`` function will be declared in an internal header file
#ifndef LLVM_LIBC_SRC_CTYPE_ISALPHA_H
#define LLVM_LIBC_SRC_CTYPE_ISALPHA_H

namespace LIBC_NAMESPACE {
namespace LIBC_NAMESPACE_DECL {

int isalpha(int c);

} // namespace LIBC_NAMESPACE
} // namespace LIBC_NAMESPACE_DECL

#endif LLVM_LIBC_SRC_CTYPE_ISALPHA_H

Notice that the ``isalpha`` function declaration is nested inside the namespace
``LIBC_NAMESPACE``. All implementation constructs in LLVM-libc are declared
within the namespace ``LIBC_NAMESPACE``.
``LIBC_NAMESPACE_DECL``. All implementation constructs in LLVM-libc are declared
within the namespace ``LIBC_NAMESPACE_DECL``.

``.cpp`` File Structure
-----------------------
Expand All @@ -49,13 +49,13 @@ which must be defined with the ``LLVM_LIBC_FUNCTION`` macro. For example, the

// --- isalpha.cpp --- //

namespace LIBC_NAMESPACE {
namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, isalpha, (int c)) {
// ... implementation goes here.
}

} // namespace LIBC_NAMESPACE
} // namespace LIBC_NAMESPACE_DECL

Notice the use of the macro ``LLVM_LIBC_FUNCTION``. This macro helps us define
a C alias symbol for the C++ implementation. For example, for a library build,
Expand Down
10 changes: 10 additions & 0 deletions libc/src/__support/macros/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@
#define LIBC_HAS_FEATURE(f) 0
#endif

// Declare a LIBC_NAMESPACE with hidden visibility. `namespace
// LIBC_NAMESPACE_DECL {` should be used around all declarations and definitions
// for libc internals as opposed to just `namespace LIBC_NAMESPACE {`. This
// ensures that all declarations within this namespace have hidden
// visibility, which optimizes codegen for uses of symbols defined in other
// translation units in ways that can be necessary for correctness by avoiding
// dynamic relocations. This does not affect the public C symbols which are
// controlled independently via `LLVM_LIBC_FUNCTION_ATTR`.
#define LIBC_NAMESPACE_DECL [[gnu::visibility("hidden")]] LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
Loading