Skip to content

Commit f49c5c4

Browse files
committed
[libc] Add LIBC_NAMESPACE_DECL macro
This defines to LIBC_NAMESPACE with `__attribute__((visibility("hidden")))` so all the symbols under it have hidden visibility. This new macro should be used when declaring a new namespace that will have internal functions/globals and LIBC_NAMESPACE should be used as a means of accessing functions/globals declared within LIBC_NAMESPACE_DECL.
1 parent e34dbb1 commit f49c5c4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

libc/docs/dev/code_style.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,34 @@ Patches containing any amount of Assembly ideally should be approved by 2
260260
maintainers. llvm-libc maintainers reserve the right to reject Assembly
261261
contributions that they feel could be better maintained if rewritten in C++,
262262
and to revisit this policy in the future.
263+
264+
LIBC_NAMESPACE_DECL
265+
===================
266+
267+
llvm-libc provides a macro `LIBC_NAMESPACE` which contains internal implementations of
268+
libc functions and globals. This macro should only be used as an
269+
identifier for accessing such symbols within the namespace (like `LIBC_NAMESPACE::cpp::max`).
270+
Any usage of this namespace for declaring or defining internal symbols should
271+
instead use `LIBC_NAMESPACE_DECL` which declares `LIBC_NAMESPACE` with hidden visibility.
272+
273+
Example usage:
274+
275+
.. code-block:: c++
276+
277+
#include "src/__support/macros/config.h" // The macro is defined here.
278+
279+
namespace LIBC_NAMESPACE_DECL {
280+
281+
void new_function() {
282+
...
283+
}
284+
285+
} // LIBC_NAMESPACE_DECL
286+
287+
Having hidden visibility on the namespace ensures etern declarations in a given TU
288+
have known visibility and never generate GOT indirextions. The attribute guarantees
289+
this independently of global compile options and build systems.
290+
291+
..
292+
TODO(leonardchan): We should have a clang-tidy check to enforce this and a
293+
fixit implementation.

libc/src/__support/macros/config.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,12 @@
2727
#define LIBC_HAS_FEATURE(f) 0
2828
#endif
2929

30+
// Declare a LIBC_NAMESPACE with hidden visibility. `namespace
31+
// LIBC_NAMESPACE_DECL {` should be used around all declarations and definitions
32+
// for libc internals as opposed to just `namespace LIBC_NAMESPACE {`. This
33+
// ensures that all internal implementations within this namespace have hidden
34+
// visibility. This does not affect the public C symbols which are controlled
35+
// independently via `LLVM_LIBC_FUNCTION_ATTR`.
36+
#define LIBC_NAMESPACE_DECL [[gnu::visibility("hidden")]] LIBC_NAMESPACE
37+
3038
#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H

0 commit comments

Comments
 (0)