xref: /llvm-project/libc/docs/dev/code_style.rst (revision 665efe896746b1dd138773e6e4d300ec97de27c2)
1.. _code_style:
2
3===================
4The libc code style
5===================
6
7Naming style
8============
9
10For the large part, the libc project follows the general `coding standards of
11the LLVM project <https://llvm.org/docs/CodingStandards.html>`_. The libc
12project differs from that standard with respect to the naming style. The
13differences are as follows:
14
15#. **Non-const variables** - This includes function arguments, struct and
16   class data members, non-const globals and local variables. They all use the
17   ``snake_case`` style.
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
21   non-const variables.
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.
26
27Macro style
28===========
29
30We define two kinds of macros:
31
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.
36
37#. **Code defined** macros are defined within the ``src/__support/macros``
38   folder. They all start with the ``LIBC_`` prefix.
39
40   * ``src/__support/macros/properties/`` - Build related properties like
41     target architecture or enabled CPU features defined by introspecting
42     compiler defined preprocessor definitions.
43
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``.
54
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``
63
64Inline functions and variables defined in header files
65======================================================
66
67When defining functions and variables inline in header files, we follow certain
68rules:
69
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``.
75   For example:
76
77   .. code-block:: c++
78
79     LIBC_INLINE_VAR constexpr bool foo = true;
80
81     LIBC_INLINE ReturnType function_defined_inline(ArgType arg) {
82       ...
83     }
84
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``
88   functions.
89
90Setting ``errno`` from runtime code
91===================================
92
93Many libc functions set ``errno`` to indicate an error condition. If LLVM's libc
94is being used as the only libc, then the ``errno`` from LLVM's libc is affected.
95If LLVM's libc is being used in the :ref:`overlay_mode`, then the ``errno`` from
96the system libc is affected. When a libc function, which can potentially affect
97the ``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
99affected. If the global ``errno`` is affected, then the operation of the unit
100test infrastructure itself can be affected. To avoid perturbing the unit test
101infrastructure around the setting of ``errno``, the following rules are to be
102followed:
103
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:
107
108   .. code-block:: c++
109
110    libc_errno = EINVAL;
111
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>`_
116   and
117   `ErrorOr <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/error_or.h>`_
118   to return error values.
119
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
125   conditions.
126
127Assertions in libc runtime code
128===============================
129
130The libc developers should, and are encouraged to, use assertions freely in
131the 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
133used from anywhere in the libc runtime code. Internally, all it does is to
134print the assertion expression and exit. It does not implement the semantics
135of the standard ``assert`` macro. Hence, it can be used from any where in the
136libc runtime code without causing any recursive calls or chicken-and-egg
137situations.
138
139Allocations in the libc runtime code
140====================================
141
142Some libc functions allocate memory. For example, the ``strdup`` function
143allocates new memory into which the input string is duplicated. Allocations
144are typically done by calling a function from the ``malloc`` family of
145functions. Such functions can fail and return an error value to indicate
146allocation failure. To conform to standards, the libc should handle
147allocation failures gracefully and surface the error conditions to the user
148code as appropriate. Since LLVM's libc is implemented in C++, we want
149allocations and deallocations to employ C++ operators ``new`` and ``delete``
150as they implicitly invoke constructors and destructors respectively. However,
151if we use the default ``new`` and ``delete`` operators, the libc will end up
152depending on the C++ runtime. To avoid such a dependence, and to handle
153allocation failures gracefully, we use special ``new`` and ``delete`` operators
154defined in
155`src/__support/CPP/new.h <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/CPP/new.h>`_.
156Allocations and deallocations using these operators employ a pattern like
157this:
158
159.. code-block:: c++
160
161   #include "src/__support/CPP/new.h"
162
163   ...
164
165     LIBC_NAMESPACE::AllocChecker ac;
166     auto *obj = new (ac) Type(...);
167     if (!ac) {
168       // handle allocator failure.
169     }
170     ...
171     delete obj;
172
173The 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
176failures will still need to be handled gracefully. Further, keep in mind that
177these functions do not call the constructors and destructors of the
178allocated/deallocated objects. So, use these functions carefully and only
179when it is absolutely clear that constructor and destructor invocation is
180not required.
181
182Warnings in sources
183===================
184
185We expect contributions to be free of warnings from the `minimum supported
186compiler versions`__ (and newer).
187
188.. __: https://libc.llvm.org/compiler_support.html#minimum-supported-versions
189
190Header Inclusion Policy
191=======================
192
193Because 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
196taken when ``#include``'ing certain headers.
197
198The ``include/`` directory contains public facing headers that users must
199consume for fullbuild mode. As such, types defined here will have ABI
200implications as these definitions may differ from the underlying system for
201overlay mode and are NEVER appropriate to include in ``libc/src/`` without
202preprocessor guards for ``LLVM_LIBC_FULL_BUILD``.
203
204Consider the case where an implementation in ``libc/src/`` may wish to refer to
205a ``sigset_t``, what header should be included? ``<signal.h>``, ``<spawn.h>``,
206``<sys/select.h>``?
207
208None 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
211the underlying system header (overlay mode).
212
213Implementations 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``.
216
217These "proxy" headers are similarly used when referring to preprocessor
218defines. Code under ``libc/src/`` should ``#include`` a proxy header from
219``hdr/``, which contains a guard on ``LLVM_LIBC_FULL_BUILD`` to either include
220our header from ``libc/include/`` (fullbuild) or the corresponding underlying
221system header (overlay).
222
223Policy on Assembly sources
224==========================
225
226Coding in high level languages such as C++ provides benefits relative to low
227level languages like Assembly, such as:
228
229* Improved safety
230* Compile time diagnostics
231* Instrumentation
232
233  * Code coverage
234  * Profile collection
235* Sanitization
236* Automatic generation of debug info
237
238While it's not impossible to have Assembly code that correctly provides all of
239the above, we do not wish to maintain such Assembly sources in llvm-libc.
240
241That said, there are a few functions provided by llvm-libc that are impossible
242to reliably implement in C++ for all compilers supported for building
243llvm-libc.
244
245We do use inline or out-of-line Assembly in an intentionally minimal set of
246places; typically places where the stack or individual register state must be
247manipulated very carefully for correctness, or instances where a specific
248instruction sequence does not have a corresponding compiler builtin function
249today.
250
251Contributions adding functions implemented purely in Assembly for performance
252are not welcome.
253
254Contributors should strive to stick with C++ for as long as it remains
255reasonable to do so. Ideally, bugs should be filed against compiler vendors,
256and links to those bug reports should appear in commit messages or comments
257that seek to add Assembly to llvm-libc.
258
259Patches containing any amount of Assembly ideally should be approved by 2
260maintainers. llvm-libc maintainers reserve the right to reject Assembly
261contributions that they feel could be better maintained if rewritten in C++,
262and to revisit this policy in the future.
263
264LIBC_NAMESPACE_DECL
265===================
266
267llvm-libc provides a macro `LIBC_NAMESPACE` which contains internal implementations of
268libc functions and globals. This macro should only be used as an
269identifier for accessing such symbols within the namespace (like `LIBC_NAMESPACE::cpp::max`).
270Any usage of this namespace for declaring or defining internal symbols should
271instead use `LIBC_NAMESPACE_DECL` which declares `LIBC_NAMESPACE` with hidden visibility.
272
273Example 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
287Having hidden visibility on the namespace ensures extern declarations in a given TU
288have known visibility and never generate GOT indirextions. The attribute guarantees
289this independently of global compile options and build systems.
290
291..
292  TODO(97655): We should have a clang-tidy check to enforce this and a
293  fixit implementation.
294