[libc-commits] [libc] [libc] Add LIBC_NAMESPACE_HIDDEN_DECL macro (PR #97109)
via libc-commits
libc-commits at lists.llvm.org
Wed Jul 3 16:15:59 PDT 2024
https://github.com/PiJoules updated https://github.com/llvm/llvm-project/pull/97109
>From 0fe1259e3df7c893b3bfb653fb7346a9869fad86 Mon Sep 17 00:00:00 2001
From: Leonard Chan <leonardchan at google.com>
Date: Fri, 28 Jun 2024 13:27:20 -0700
Subject: [PATCH] [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.
---
libc/docs/dev/code_style.rst | 31 ++++++++++++++++++++++++++++++
libc/src/__support/macros/config.h | 8 ++++++++
libc/src/stdlib/rand_util.cpp | 1 +
libc/src/stdlib/rand_util.h | 1 +
4 files changed, 41 insertions(+)
diff --git a/libc/docs/dev/code_style.rst b/libc/docs/dev/code_style.rst
index 170ef6598a9d8..363035203c393 100644
--- a/libc/docs/dev/code_style.rst
+++ b/libc/docs/dev/code_style.rst
@@ -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 etern 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(leonardchan): We should have a clang-tidy check to enforce this and a
+ fixit implementation.
diff --git a/libc/src/__support/macros/config.h b/libc/src/__support/macros/config.h
index 6390c7992325d..e6f9e4eeca26f 100644
--- a/libc/src/__support/macros/config.h
+++ b/libc/src/__support/macros/config.h
@@ -27,4 +27,12 @@
#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 internal implementations within this namespace have hidden
+// visibility. 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
diff --git a/libc/src/stdlib/rand_util.cpp b/libc/src/stdlib/rand_util.cpp
index ff3478db70003..d8b0fdc7d159b 100644
--- a/libc/src/stdlib/rand_util.cpp
+++ b/libc/src/stdlib/rand_util.cpp
@@ -9,6 +9,7 @@
#include "src/stdlib/rand_util.h"
#include "src/__support/CPP/atomic.h"
#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE {
diff --git a/libc/src/stdlib/rand_util.h b/libc/src/stdlib/rand_util.h
index 5d7febf8248d8..eaf8dbef534db 100644
--- a/libc/src/stdlib/rand_util.h
+++ b/libc/src/stdlib/rand_util.h
@@ -11,6 +11,7 @@
#include "src/__support/CPP/atomic.h"
#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE {
More information about the libc-commits
mailing list