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: **code defined** and **build defined** 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. They are of two kinds
40 * **Properties** - Build related properties like used compiler, target
41 architecture or enabled CPU features defined by introspecting compiler
42 defined preprocessor definitions. e.g., ``LIBC_TARGET_ARCH_IS_ARM``,
43 ``LIBC_TARGET_CPU_HAS_AVX2``, ``LIBC_COMPILER_IS_CLANG``, ...
44 * **Attributes** - Compiler agnostic attributes or functions to handle
45 specific operations. e.g., ``LIBC_INLINE``, ``LIBC_NO_LOOP_UNROLL``,
46 ``LIBC_LIKELY``, ``LIBC_INLINE_ASM``.
48 Inline functions and variables defined in header files
49 ======================================================
51 When defining functions and variables inline in header files, we follow certain
54 #. The functions should not be given file-static linkage. There can be class
55 static methods defined inline however.
56 #. Instead of using the ``inline`` keyword, functions should be tagged with the
57 ``LIBC_INLINE`` macro and variables should be tagged with the
58 ``LIBC_INLINE_VAR`` macro defined in ``src/__support/macros/attributes.h``.
63 LIBC_INLINE_VAR constexpr bool foo = true;
65 LIBC_INLINE ReturnType function_defined_inline(ArgType arg) {
69 #. The ``LIBC_INLINE`` tag should also be added to functions which have
70 definitions that are implicitly inline. Examples of such functions are
71 class methods (static and non-static) defined inline and ``constexpr``
74 Setting ``errno`` from runtime code
75 ===================================
77 Many libc functions set ``errno`` to indicate an error condition. If LLVM's libc
78 is being used as the only libc, then the ``errno`` from LLVM's libc is affected.
79 If LLVM's libc is being used in the :ref:`overlay_mode`, then the ``errno`` from
80 the system libc is affected. When a libc function, which can potentially affect
81 the ``errno``, is called from a unit test, we do not want the global ``errno``
82 (as in, the ``errno`` of the process thread running the unit test) to be
83 affected. If the global ``errno`` is affected, then the operation of the unit
84 test infrastructure itself can be affected. To avoid perturbing the unit test
85 infrastructure around the setting of ``errno``, the following rules are to be
88 #. A special macro named ``libc_errno`` defined in ``src/errno/libc_errno.h``
89 should be used when setting ``errno`` from libc runtime code. For example,
90 code to set ``errno`` to ``EINVAL`` should be:
96 #. ``errno`` should be set just before returning from the implementation of the
97 public function. It should not be set from within helper functions. Helper
98 functions should use idiomatic C++ constructs like
99 `cpp::optional <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/CPP/optional.h>`_
101 `ErrorOr <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/error_or.h>`_
102 to return error values.
104 #. The header file ``src/errno/libc_errno.h`` is shipped as part of the target
105 corresponding to the ``errno`` entrypoint ``libc.src.errno.errno``. We do
106 not in general allow dependencies between entrypoints. However, the ``errno``
107 entrypoint is the only exceptional entrypoint on which other entrypoints
108 should explicitly depend on if they set ``errno`` to indicate error
111 Assertions in libc runtime code
112 ===============================
114 The libc developers should, and are encouraged to, use assertions freely in
115 the libc runtime code. However, the assertion should be listed via the macro
116 ``LIBC_ASSERT`` defined in ``src/__support/libc_assert.h``. This macro can be
117 used from anywhere in the libc runtime code. Internally, all it does is to
118 print the assertion expression and exit. It does not implement the semantics
119 of the standard ``assert`` macro. Hence, it can be used from any where in the
120 libc runtime code without causing any recursive calls or chicken-and-egg
123 Allocations in the libc runtime code
124 ====================================
126 Some libc functions allocate memory. For example, the ``strdup`` function
127 allocates new memory into which the input string is duplicated. Allocations
128 are typically done by calling a function from the ``malloc`` family of
129 functions. Such functions can fail and return an error value to indicate
130 allocation failure. To conform to standards, the libc should handle
131 allocation failures gracefully and surface the error conditions to the user
132 code as appropriate. Since LLVM's libc is implemented in C++, we want
133 allocations and deallocations to employ C++ operators ``new`` and ``delete``
134 as they implicitly invoke constructors and destructors respectively. However,
135 if we use the default ``new`` and ``delete`` operators, the libc will end up
136 depending on the C++ runtime. To avoid such a dependence, and to handle
137 allocation failures gracefully, we use special ``new`` and ``delete`` operators
139 `src/__support/CPP/new.h <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/CPP/new.h>`_.
140 Allocations and deallocations using these operators employ a pattern like
145 #include "src/__support/CPP/new.h"
149 __llvm_libc::AllocChecker ac;
150 auto *obj = new (ac) Type(...);
152 // handle allocator failure.
157 The only exception to using the above pattern is if allocating using the
158 ``realloc`` function is of value. In such cases, prefer to use only the
159 ``malloc`` family of functions for allocations and deallocations. Allocation
160 failures will still need to be handled gracefully. Further, keep in mind that
161 these functions do not call the constructors and destructors of the
162 allocated/deallocated objects. So, use these functions carefully and only
163 when it is absolutely clear that constructor and destructor invocation is