10 For the large part, the libc project follows the general `coding standards of
11 the LLVM project <https://llvm.org/docs/CodingStandards.html>`_. The libc
12 project differs from that standard with respect to the naming style. The
13 differences are as follows:
15 #. **Non-const variables** - This includes function arguments, struct and
16 class data members, non-const globals and local variables. They all use the
18 #. **const and constexpr variables** - They use the capitalized
19 ``SNAKE_CASE`` irrespective of whether they are local or global.
20 #. **Function and methods** - They use the ``snake_case`` style like the
22 #. **Internal type names** - These are types which are internal to the libc
23 implementation. They use the ``CaptilizedCamelCase`` style.
24 #. **Public names** - These are the names as prescribed by the standards and
25 will follow the style as prescribed by the standards.
30 We define two kinds of macros:
32 #. **Build defined** macros are generated by `CMake` or `Bazel` and are passed
33 down to the compiler with the ``-D`` command line flag. They start with the
34 ``LIBC_COPT_`` prefix. They are used to tune the behavior of the libc.
35 They either denote an action or define a constant.
37 #. **Code defined** macros are defined within the ``src/__support/macros``
38 folder. They all start with the ``LIBC_`` prefix.
40 * ``src/__support/macros/properties/`` - Build related properties like
41 target architecture or enabled CPU features defined by introspecting
42 compiler defined preprocessor definitions.
44 * ``architectures.h`` - Target architecture properties.
45 e.g., ``LIBC_TARGET_ARCH_IS_ARM``.
46 * ``compiler.h`` - Host compiler properties.
47 e.g., ``LIBC_COMPILER_IS_CLANG``.
48 * ``cpu_features.h`` - Target cpu feature availability.
49 e.g., ``LIBC_TARGET_CPU_HAS_AVX2``.
50 * ``types.h`` - Type properties and availability.
51 e.g., ``LIBC_TYPES_HAS_FLOAT128``.
52 * ``os.h`` - Target os properties.
53 e.g., ``LIBC_TARGET_OS_IS_LINUX``.
55 * ``src/__support/macros/config.h`` - Important compiler and platform
56 features. Such macros can be used to produce portable code by
57 parameterizing compilation based on the presence or lack of a given
58 feature. e.g., ``LIBC_HAS_FEATURE``
59 * ``src/__support/macros/attributes.h`` - Attributes for functions, types,
60 and variables. e.g., ``LIBC_UNUSED``
61 * ``src/__support/macros/optimization.h`` - Portable macros for performance
62 optimization. e.g., ``LIBC_LIKELY``, ``LIBC_LOOP_NOUNROLL``
64 Inline functions and variables defined in header files
65 ======================================================
67 When defining functions and variables inline in header files, we follow certain
70 #. The functions should not be given file-static linkage. There can be class
71 static methods defined inline however.
72 #. Instead of using the ``inline`` keyword, functions should be tagged with the
73 ``LIBC_INLINE`` macro and variables should be tagged with the
74 ``LIBC_INLINE_VAR`` macro defined in ``src/__support/macros/attributes.h``.
79 LIBC_INLINE_VAR constexpr bool foo = true;
81 LIBC_INLINE ReturnType function_defined_inline(ArgType arg) {
85 #. The ``LIBC_INLINE`` tag should also be added to functions which have
86 definitions that are implicitly inline. Examples of such functions are
87 class methods (static and non-static) defined inline and ``constexpr``
90 Setting ``errno`` from runtime code
91 ===================================
93 Many libc functions set ``errno`` to indicate an error condition. If LLVM's libc
94 is being used as the only libc, then the ``errno`` from LLVM's libc is affected.
95 If LLVM's libc is being used in the :ref:`overlay_mode`, then the ``errno`` from
96 the system libc is affected. When a libc function, which can potentially affect
97 the ``errno``, is called from a unit test, we do not want the global ``errno``
98 (as in, the ``errno`` of the process thread running the unit test) to be
99 affected. If the global ``errno`` is affected, then the operation of the unit
100 test infrastructure itself can be affected. To avoid perturbing the unit test
101 infrastructure around the setting of ``errno``, the following rules are to be
104 #. A special macro named ``libc_errno`` defined in ``src/errno/libc_errno.h``
105 should be used when setting ``errno`` from libc runtime code. For example,
106 code to set ``errno`` to ``EINVAL`` should be:
112 #. ``errno`` should be set just before returning from the implementation of the
113 public function. It should not be set from within helper functions. Helper
114 functions should use idiomatic C++ constructs like
115 `cpp::optional <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/CPP/optional.h>`_
117 `ErrorOr <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/error_or.h>`_
118 to return error values.
120 #. The header file ``src/errno/libc_errno.h`` is shipped as part of the target
121 corresponding to the ``errno`` entrypoint ``libc.src.errno.errno``. We do
122 not in general allow dependencies between entrypoints. However, the ``errno``
123 entrypoint is the only exceptional entrypoint on which other entrypoints
124 should explicitly depend on if they set ``errno`` to indicate error
127 Assertions in libc runtime code
128 ===============================
130 The libc developers should, and are encouraged to, use assertions freely in
131 the libc runtime code. However, the assertion should be listed via the macro
132 ``LIBC_ASSERT`` defined in ``src/__support/libc_assert.h``. This macro can be
133 used from anywhere in the libc runtime code. Internally, all it does is to
134 print the assertion expression and exit. It does not implement the semantics
135 of the standard ``assert`` macro. Hence, it can be used from any where in the
136 libc runtime code without causing any recursive calls or chicken-and-egg
139 Allocations in the libc runtime code
140 ====================================
142 Some libc functions allocate memory. For example, the ``strdup`` function
143 allocates new memory into which the input string is duplicated. Allocations
144 are typically done by calling a function from the ``malloc`` family of
145 functions. Such functions can fail and return an error value to indicate
146 allocation failure. To conform to standards, the libc should handle
147 allocation failures gracefully and surface the error conditions to the user
148 code as appropriate. Since LLVM's libc is implemented in C++, we want
149 allocations and deallocations to employ C++ operators ``new`` and ``delete``
150 as they implicitly invoke constructors and destructors respectively. However,
151 if we use the default ``new`` and ``delete`` operators, the libc will end up
152 depending on the C++ runtime. To avoid such a dependence, and to handle
153 allocation failures gracefully, we use special ``new`` and ``delete`` operators
155 `src/__support/CPP/new.h <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/CPP/new.h>`_.
156 Allocations and deallocations using these operators employ a pattern like
161 #include "src/__support/CPP/new.h"
165 LIBC_NAMESPACE::AllocChecker ac;
166 auto *obj = new (ac) Type(...);
168 // handle allocator failure.
173 The only exception to using the above pattern is if allocating using the
174 ``realloc`` function is of value. In such cases, prefer to use only the
175 ``malloc`` family of functions for allocations and deallocations. Allocation
176 failures will still need to be handled gracefully. Further, keep in mind that
177 these functions do not call the constructors and destructors of the
178 allocated/deallocated objects. So, use these functions carefully and only
179 when it is absolutely clear that constructor and destructor invocation is
185 We expect contributions to be free of warnings from the `minimum supported
186 compiler versions`__ (and newer).
188 .. __: https://libc.llvm.org/compiler_support.html#minimum-supported-versions
190 Header Inclusion Policy
191 =======================
193 Because llvm-libc supports
194 `Overlay Mode <https://libc.llvm.org/overlay_mode.html>`__ and
195 `Fullbuild Mode <https://libc.llvm.org/fullbuild_mode.html>`__ care must be
196 taken when ``#include``'ing certain headers.
198 The ``include/`` directory contains public facing headers that users must
199 consume for fullbuild mode. As such, types defined here will have ABI
200 implications as these definitions may differ from the underlying system for
201 overlay mode and are NEVER appropriate to include in ``libc/src/`` without
202 preprocessor guards for ``LLVM_LIBC_FULL_BUILD``.
204 Consider the case where an implementation in ``libc/src/`` may wish to refer to
205 a ``sigset_t``, what header should be included? ``<signal.h>``, ``<spawn.h>``,
208 None of the above. Instead, code under ``src/`` should ``#include
209 "hdr/types/sigset_t.h"`` which contains preprocessor guards on
210 ``LLVM_LIBC_FULL_BUILD`` to either include the public type (fullbuild mode) or
211 the underlying system header (overlay mode).
213 Implementations in ``libc/src/`` should NOT be ``#include``'ing using ``<>`` or
214 ``"include/*``, except for these "proxy" headers that first check for
215 ``LLVM_LIBC_FULL_BUILD``.
217 These "proxy" headers are similarly used when referring to preprocessor
218 defines. Code under ``libc/src/`` should ``#include`` a proxy header from
219 ``hdr/``, which contains a guard on ``LLVM_LIBC_FULL_BUILD`` to either include
220 our header from ``libc/include/`` (fullbuild) or the corresponding underlying
221 system header (overlay).
223 Policy on Assembly sources
224 ==========================
226 Coding in high level languages such as C++ provides benefits relative to low
227 level languages like Assembly, such as:
230 * Compile time diagnostics
236 * Automatic generation of debug info
238 While it's not impossible to have Assembly code that correctly provides all of
239 the above, we do not wish to maintain such Assembly sources in llvm-libc.
241 That said, there are a few functions provided by llvm-libc that are impossible
242 to reliably implement in C++ for all compilers supported for building
245 We do use inline or out-of-line Assembly in an intentionally minimal set of
246 places; typically places where the stack or individual register state must be
247 manipulated very carefully for correctness, or instances where a specific
248 instruction sequence does not have a corresponding compiler builtin function
251 Contributions adding functions implemented purely in Assembly for performance
254 Contributors should strive to stick with C++ for as long as it remains
255 reasonable to do so. Ideally, bugs should be filed against compiler vendors,
256 and links to those bug reports should appear in commit messages or comments
257 that seek to add Assembly to llvm-libc.
259 Patches containing any amount of Assembly ideally should be approved by 2
260 maintainers. llvm-libc maintainers reserve the right to reject Assembly
261 contributions that they feel could be better maintained if rewritten in C++,
262 and to revisit this policy in the future.
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.
277 #include "src/__support/macros/config.h" // The macro is defined here.
279 namespace LIBC_NAMESPACE_DECL {
281 void new_function() {
285 } // LIBC_NAMESPACE_DECL
287 Having hidden visibility on the namespace ensures extern 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.
292 TODO(97655): We should have a clang-tidy check to enforce this and a
293 fixit implementation.