154faf22fSSiva Chandra Reddy.. _code_style: 254faf22fSSiva Chandra Reddy 354faf22fSSiva Chandra Reddy=================== 454faf22fSSiva Chandra ReddyThe libc code style 554faf22fSSiva Chandra Reddy=================== 654faf22fSSiva Chandra Reddy 754faf22fSSiva Chandra ReddyNaming style 854faf22fSSiva Chandra Reddy============ 954faf22fSSiva Chandra Reddy 1054faf22fSSiva Chandra ReddyFor the large part, the libc project follows the general `coding standards of 1154faf22fSSiva Chandra Reddythe LLVM project <https://llvm.org/docs/CodingStandards.html>`_. The libc 1254faf22fSSiva Chandra Reddyproject differs from that standard with respect to the naming style. The 1354faf22fSSiva Chandra Reddydifferences are as follows: 1454faf22fSSiva Chandra Reddy 1554faf22fSSiva Chandra Reddy#. **Non-const variables** - This includes function arguments, struct and 1654faf22fSSiva Chandra Reddy class data members, non-const globals and local variables. They all use the 1754faf22fSSiva Chandra Reddy ``snake_case`` style. 18e042efdaSKazu Hirata#. **const and constexpr variables** - They use the capitalized 1954faf22fSSiva Chandra Reddy ``SNAKE_CASE`` irrespective of whether they are local or global. 2054faf22fSSiva Chandra Reddy#. **Function and methods** - They use the ``snake_case`` style like the 2154faf22fSSiva Chandra Reddy non-const variables. 22e042efdaSKazu Hirata#. **Internal type names** - These are types which are internal to the libc 2354faf22fSSiva Chandra Reddy implementation. They use the ``CaptilizedCamelCase`` style. 2454faf22fSSiva Chandra Reddy#. **Public names** - These are the names as prescribed by the standards and 2554faf22fSSiva Chandra Reddy will follow the style as prescribed by the standards. 2654faf22fSSiva Chandra Reddy 2754faf22fSSiva Chandra ReddyMacro style 2854faf22fSSiva Chandra Reddy=========== 2954faf22fSSiva Chandra Reddy 3057531033SGuillaume ChateletWe define two kinds of macros: 3154faf22fSSiva Chandra Reddy 3254faf22fSSiva Chandra Reddy#. **Build defined** macros are generated by `CMake` or `Bazel` and are passed 3354faf22fSSiva Chandra Reddy down to the compiler with the ``-D`` command line flag. They start with the 3454faf22fSSiva Chandra Reddy ``LIBC_COPT_`` prefix. They are used to tune the behavior of the libc. 3554faf22fSSiva Chandra Reddy They either denote an action or define a constant. 3654faf22fSSiva Chandra Reddy 3754faf22fSSiva Chandra Reddy#. **Code defined** macros are defined within the ``src/__support/macros`` 3857531033SGuillaume Chatelet folder. They all start with the ``LIBC_`` prefix. 3954faf22fSSiva Chandra Reddy 4057531033SGuillaume Chatelet * ``src/__support/macros/properties/`` - Build related properties like 4157531033SGuillaume Chatelet target architecture or enabled CPU features defined by introspecting 425dd95687SKazu Hirata compiler defined preprocessor definitions. 4357531033SGuillaume Chatelet 4457531033SGuillaume Chatelet * ``architectures.h`` - Target architecture properties. 4557531033SGuillaume Chatelet e.g., ``LIBC_TARGET_ARCH_IS_ARM``. 4657531033SGuillaume Chatelet * ``compiler.h`` - Host compiler properties. 4757531033SGuillaume Chatelet e.g., ``LIBC_COMPILER_IS_CLANG``. 485e5a22caSGuillaume Chatelet * ``cpu_features.h`` - Target cpu feature availability. 4957531033SGuillaume Chatelet e.g., ``LIBC_TARGET_CPU_HAS_AVX2``. 5053bd411eSGuillaume Chatelet * ``types.h`` - Type properties and availability. 5175fb825bSGuillaume Chatelet e.g., ``LIBC_TYPES_HAS_FLOAT128``. 525e5a22caSGuillaume Chatelet * ``os.h`` - Target os properties. 535e5a22caSGuillaume Chatelet e.g., ``LIBC_TARGET_OS_IS_LINUX``. 5457531033SGuillaume Chatelet 5557531033SGuillaume Chatelet * ``src/__support/macros/config.h`` - Important compiler and platform 5657531033SGuillaume Chatelet features. Such macros can be used to produce portable code by 5757531033SGuillaume Chatelet parameterizing compilation based on the presence or lack of a given 5877118536SMarc Auberer feature. e.g., ``LIBC_HAS_FEATURE`` 5957531033SGuillaume Chatelet * ``src/__support/macros/attributes.h`` - Attributes for functions, types, 6057531033SGuillaume Chatelet and variables. e.g., ``LIBC_UNUSED`` 6157531033SGuillaume Chatelet * ``src/__support/macros/optimization.h`` - Portable macros for performance 6257531033SGuillaume Chatelet optimization. e.g., ``LIBC_LIKELY``, ``LIBC_LOOP_NOUNROLL`` 6354faf22fSSiva Chandra Reddy 6461c9052cSAlex BrachetInline functions and variables defined in header files 65d9428945STue Ly====================================================== 6654faf22fSSiva Chandra Reddy 6761c9052cSAlex BrachetWhen defining functions and variables inline in header files, we follow certain 6861c9052cSAlex Brachetrules: 6954faf22fSSiva Chandra Reddy 7054faf22fSSiva Chandra Reddy#. The functions should not be given file-static linkage. There can be class 7154faf22fSSiva Chandra Reddy static methods defined inline however. 7261c9052cSAlex Brachet#. Instead of using the ``inline`` keyword, functions should be tagged with the 7361c9052cSAlex Brachet ``LIBC_INLINE`` macro and variables should be tagged with the 7461c9052cSAlex Brachet ``LIBC_INLINE_VAR`` macro defined in ``src/__support/macros/attributes.h``. 7561c9052cSAlex Brachet For example: 7654faf22fSSiva Chandra Reddy 7754faf22fSSiva Chandra Reddy .. code-block:: c++ 7854faf22fSSiva Chandra Reddy 7961c9052cSAlex Brachet LIBC_INLINE_VAR constexpr bool foo = true; 8061c9052cSAlex Brachet 8154faf22fSSiva Chandra Reddy LIBC_INLINE ReturnType function_defined_inline(ArgType arg) { 8254faf22fSSiva Chandra Reddy ... 8354faf22fSSiva Chandra Reddy } 8454faf22fSSiva Chandra Reddy 8554faf22fSSiva Chandra Reddy#. The ``LIBC_INLINE`` tag should also be added to functions which have 8654faf22fSSiva Chandra Reddy definitions that are implicitly inline. Examples of such functions are 8754faf22fSSiva Chandra Reddy class methods (static and non-static) defined inline and ``constexpr`` 8854faf22fSSiva Chandra Reddy functions. 894052bc86SSiva Chandra Reddy 904052bc86SSiva Chandra ReddySetting ``errno`` from runtime code 914052bc86SSiva Chandra Reddy=================================== 924052bc86SSiva Chandra Reddy 934052bc86SSiva Chandra ReddyMany libc functions set ``errno`` to indicate an error condition. If LLVM's libc 944052bc86SSiva Chandra Reddyis being used as the only libc, then the ``errno`` from LLVM's libc is affected. 954052bc86SSiva Chandra ReddyIf LLVM's libc is being used in the :ref:`overlay_mode`, then the ``errno`` from 964052bc86SSiva Chandra Reddythe system libc is affected. When a libc function, which can potentially affect 974052bc86SSiva Chandra Reddythe ``errno``, is called from a unit test, we do not want the global ``errno`` 984052bc86SSiva Chandra Reddy(as in, the ``errno`` of the process thread running the unit test) to be 994052bc86SSiva Chandra Reddyaffected. If the global ``errno`` is affected, then the operation of the unit 1004052bc86SSiva Chandra Reddytest infrastructure itself can be affected. To avoid perturbing the unit test 1014052bc86SSiva Chandra Reddyinfrastructure around the setting of ``errno``, the following rules are to be 1024052bc86SSiva Chandra Reddyfollowed: 1034052bc86SSiva Chandra Reddy 1044052bc86SSiva Chandra Reddy#. A special macro named ``libc_errno`` defined in ``src/errno/libc_errno.h`` 1054052bc86SSiva Chandra Reddy should be used when setting ``errno`` from libc runtime code. For example, 1064052bc86SSiva Chandra Reddy code to set ``errno`` to ``EINVAL`` should be: 1074052bc86SSiva Chandra Reddy 1084052bc86SSiva Chandra Reddy .. code-block:: c++ 1094052bc86SSiva Chandra Reddy 1104052bc86SSiva Chandra Reddy libc_errno = EINVAL; 1114052bc86SSiva Chandra Reddy 1124052bc86SSiva Chandra Reddy#. ``errno`` should be set just before returning from the implementation of the 1134052bc86SSiva Chandra Reddy public function. It should not be set from within helper functions. Helper 1144052bc86SSiva Chandra Reddy functions should use idiomatic C++ constructs like 1154052bc86SSiva Chandra Reddy `cpp::optional <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/CPP/optional.h>`_ 1164052bc86SSiva Chandra Reddy and 1174052bc86SSiva Chandra Reddy `ErrorOr <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/error_or.h>`_ 1184052bc86SSiva Chandra Reddy to return error values. 1194052bc86SSiva Chandra Reddy 1204052bc86SSiva Chandra Reddy#. The header file ``src/errno/libc_errno.h`` is shipped as part of the target 1214052bc86SSiva Chandra Reddy corresponding to the ``errno`` entrypoint ``libc.src.errno.errno``. We do 122e042efdaSKazu Hirata not in general allow dependencies between entrypoints. However, the ``errno`` 1234052bc86SSiva Chandra Reddy entrypoint is the only exceptional entrypoint on which other entrypoints 1244052bc86SSiva Chandra Reddy should explicitly depend on if they set ``errno`` to indicate error 1254052bc86SSiva Chandra Reddy conditions. 1264155503bSSiva Chandra Reddy 1274155503bSSiva Chandra ReddyAssertions in libc runtime code 1284155503bSSiva Chandra Reddy=============================== 1294155503bSSiva Chandra Reddy 1304155503bSSiva Chandra ReddyThe libc developers should, and are encouraged to, use assertions freely in 1314155503bSSiva Chandra Reddythe libc runtime code. However, the assertion should be listed via the macro 1324155503bSSiva Chandra Reddy``LIBC_ASSERT`` defined in ``src/__support/libc_assert.h``. This macro can be 1334155503bSSiva Chandra Reddyused from anywhere in the libc runtime code. Internally, all it does is to 1344155503bSSiva Chandra Reddyprint the assertion expression and exit. It does not implement the semantics 1354155503bSSiva Chandra Reddyof the standard ``assert`` macro. Hence, it can be used from any where in the 1364155503bSSiva Chandra Reddylibc runtime code without causing any recursive calls or chicken-and-egg 1374155503bSSiva Chandra Reddysituations. 13808b98350SSiva Chandra Reddy 13908b98350SSiva Chandra ReddyAllocations in the libc runtime code 14008b98350SSiva Chandra Reddy==================================== 14108b98350SSiva Chandra Reddy 14208b98350SSiva Chandra ReddySome libc functions allocate memory. For example, the ``strdup`` function 14308b98350SSiva Chandra Reddyallocates new memory into which the input string is duplicated. Allocations 14408b98350SSiva Chandra Reddyare typically done by calling a function from the ``malloc`` family of 14508b98350SSiva Chandra Reddyfunctions. Such functions can fail and return an error value to indicate 14608b98350SSiva Chandra Reddyallocation failure. To conform to standards, the libc should handle 14708b98350SSiva Chandra Reddyallocation failures gracefully and surface the error conditions to the user 14808b98350SSiva Chandra Reddycode as appropriate. Since LLVM's libc is implemented in C++, we want 14908b98350SSiva Chandra Reddyallocations and deallocations to employ C++ operators ``new`` and ``delete`` 15008b98350SSiva Chandra Reddyas they implicitly invoke constructors and destructors respectively. However, 15108b98350SSiva Chandra Reddyif we use the default ``new`` and ``delete`` operators, the libc will end up 15208b98350SSiva Chandra Reddydepending on the C++ runtime. To avoid such a dependence, and to handle 15308b98350SSiva Chandra Reddyallocation failures gracefully, we use special ``new`` and ``delete`` operators 15408b98350SSiva Chandra Reddydefined in 15508b98350SSiva Chandra Reddy`src/__support/CPP/new.h <https://github.com/llvm/llvm-project/blob/main/libc/src/__support/CPP/new.h>`_. 15608b98350SSiva Chandra ReddyAllocations and deallocations using these operators employ a pattern like 15708b98350SSiva Chandra Reddythis: 15808b98350SSiva Chandra Reddy 15908b98350SSiva Chandra Reddy.. code-block:: c++ 16008b98350SSiva Chandra Reddy 16108b98350SSiva Chandra Reddy #include "src/__support/CPP/new.h" 16208b98350SSiva Chandra Reddy 16308b98350SSiva Chandra Reddy ... 16408b98350SSiva Chandra Reddy 165b6bc9d72SGuillaume Chatelet LIBC_NAMESPACE::AllocChecker ac; 16608b98350SSiva Chandra Reddy auto *obj = new (ac) Type(...); 16708b98350SSiva Chandra Reddy if (!ac) { 16808b98350SSiva Chandra Reddy // handle allocator failure. 16908b98350SSiva Chandra Reddy } 17008b98350SSiva Chandra Reddy ... 17108b98350SSiva Chandra Reddy delete obj; 17208b98350SSiva Chandra Reddy 17308b98350SSiva Chandra ReddyThe only exception to using the above pattern is if allocating using the 17408b98350SSiva Chandra Reddy``realloc`` function is of value. In such cases, prefer to use only the 17508b98350SSiva Chandra Reddy``malloc`` family of functions for allocations and deallocations. Allocation 17608b98350SSiva Chandra Reddyfailures will still need to be handled gracefully. Further, keep in mind that 17708b98350SSiva Chandra Reddythese functions do not call the constructors and destructors of the 17808b98350SSiva Chandra Reddyallocated/deallocated objects. So, use these functions carefully and only 179e042efdaSKazu Hiratawhen it is absolutely clear that constructor and destructor invocation is 18008b98350SSiva Chandra Reddynot required. 181c52b4678SNick Desaulniers 182c52b4678SNick DesaulniersWarnings in sources 183c52b4678SNick Desaulniers=================== 184c52b4678SNick Desaulniers 185c52b4678SNick DesaulniersWe expect contributions to be free of warnings from the `minimum supported 186c52b4678SNick Desaulnierscompiler versions`__ (and newer). 187c52b4678SNick Desaulniers 188c52b4678SNick Desaulniers.. __: https://libc.llvm.org/compiler_support.html#minimum-supported-versions 189f626a350SNick Desaulniers 190f626a350SNick DesaulniersHeader Inclusion Policy 191f626a350SNick Desaulniers======================= 192f626a350SNick Desaulniers 193f626a350SNick DesaulniersBecause llvm-libc supports 194f626a350SNick Desaulniers`Overlay Mode <https://libc.llvm.org/overlay_mode.html>`__ and 195f626a350SNick Desaulniers`Fullbuild Mode <https://libc.llvm.org/fullbuild_mode.html>`__ care must be 196f626a350SNick Desaulnierstaken when ``#include``'ing certain headers. 197f626a350SNick Desaulniers 198f626a350SNick DesaulniersThe ``include/`` directory contains public facing headers that users must 199f626a350SNick Desaulniersconsume for fullbuild mode. As such, types defined here will have ABI 200f626a350SNick Desaulniersimplications as these definitions may differ from the underlying system for 201f626a350SNick Desaulniersoverlay mode and are NEVER appropriate to include in ``libc/src/`` without 202f626a350SNick Desaulnierspreprocessor guards for ``LLVM_LIBC_FULL_BUILD``. 203f626a350SNick Desaulniers 204f626a350SNick DesaulniersConsider the case where an implementation in ``libc/src/`` may wish to refer to 205f626a350SNick Desaulniersa ``sigset_t``, what header should be included? ``<signal.h>``, ``<spawn.h>``, 206f626a350SNick Desaulniers``<sys/select.h>``? 207f626a350SNick Desaulniers 208f626a350SNick DesaulniersNone of the above. Instead, code under ``src/`` should ``#include 209f626a350SNick Desaulniers"hdr/types/sigset_t.h"`` which contains preprocessor guards on 210f626a350SNick Desaulniers``LLVM_LIBC_FULL_BUILD`` to either include the public type (fullbuild mode) or 211f626a350SNick Desaulniersthe underlying system header (overlay mode). 212f626a350SNick Desaulniers 213f626a350SNick DesaulniersImplementations in ``libc/src/`` should NOT be ``#include``'ing using ``<>`` or 214f626a350SNick Desaulniers``"include/*``, except for these "proxy" headers that first check for 215f626a350SNick Desaulniers``LLVM_LIBC_FULL_BUILD``. 216f626a350SNick Desaulniers 217f626a350SNick DesaulniersThese "proxy" headers are similarly used when referring to preprocessor 218f626a350SNick Desaulniersdefines. Code under ``libc/src/`` should ``#include`` a proxy header from 219f626a350SNick Desaulniers``hdr/``, which contains a guard on ``LLVM_LIBC_FULL_BUILD`` to either include 220f626a350SNick Desaulniersour header from ``libc/include/`` (fullbuild) or the corresponding underlying 221f626a350SNick Desaulnierssystem header (overlay). 2220336116eSNick Desaulniers 2230336116eSNick DesaulniersPolicy on Assembly sources 2240336116eSNick Desaulniers========================== 2250336116eSNick Desaulniers 2260336116eSNick DesaulniersCoding in high level languages such as C++ provides benefits relative to low 2270336116eSNick Desaulnierslevel languages like Assembly, such as: 2280336116eSNick Desaulniers 2290336116eSNick Desaulniers* Improved safety 2300336116eSNick Desaulniers* Compile time diagnostics 2310336116eSNick Desaulniers* Instrumentation 2320336116eSNick Desaulniers 2330336116eSNick Desaulniers * Code coverage 2340336116eSNick Desaulniers * Profile collection 2350336116eSNick Desaulniers* Sanitization 2360336116eSNick Desaulniers* Automatic generation of debug info 2370336116eSNick Desaulniers 2380336116eSNick DesaulniersWhile it's not impossible to have Assembly code that correctly provides all of 2390336116eSNick Desaulniersthe above, we do not wish to maintain such Assembly sources in llvm-libc. 2400336116eSNick Desaulniers 2410336116eSNick DesaulniersThat said, there are a few functions provided by llvm-libc that are impossible 2420336116eSNick Desaulniersto reliably implement in C++ for all compilers supported for building 2430336116eSNick Desaulniersllvm-libc. 2440336116eSNick Desaulniers 2450336116eSNick DesaulniersWe do use inline or out-of-line Assembly in an intentionally minimal set of 2460336116eSNick Desaulniersplaces; typically places where the stack or individual register state must be 2470336116eSNick Desaulniersmanipulated very carefully for correctness, or instances where a specific 2480336116eSNick Desaulniersinstruction sequence does not have a corresponding compiler builtin function 2490336116eSNick Desaulnierstoday. 2500336116eSNick Desaulniers 2510336116eSNick DesaulniersContributions adding functions implemented purely in Assembly for performance 2520336116eSNick Desaulniersare not welcome. 2530336116eSNick Desaulniers 2540336116eSNick DesaulniersContributors should strive to stick with C++ for as long as it remains 2550336116eSNick Desaulniersreasonable to do so. Ideally, bugs should be filed against compiler vendors, 2560336116eSNick Desaulniersand links to those bug reports should appear in commit messages or comments 2570336116eSNick Desaulniersthat seek to add Assembly to llvm-libc. 2580336116eSNick Desaulniers 2590336116eSNick DesaulniersPatches containing any amount of Assembly ideally should be approved by 2 2600336116eSNick Desaulniersmaintainers. llvm-libc maintainers reserve the right to reject Assembly 2610336116eSNick Desaulnierscontributions that they feel could be better maintained if rewritten in C++, 2620336116eSNick Desaulniersand to revisit this policy in the future. 263*665efe89SPiJoules 264*665efe89SPiJoulesLIBC_NAMESPACE_DECL 265*665efe89SPiJoules=================== 266*665efe89SPiJoules 267*665efe89SPiJoulesllvm-libc provides a macro `LIBC_NAMESPACE` which contains internal implementations of 268*665efe89SPiJouleslibc functions and globals. This macro should only be used as an 269*665efe89SPiJoulesidentifier for accessing such symbols within the namespace (like `LIBC_NAMESPACE::cpp::max`). 270*665efe89SPiJoulesAny usage of this namespace for declaring or defining internal symbols should 271*665efe89SPiJoulesinstead use `LIBC_NAMESPACE_DECL` which declares `LIBC_NAMESPACE` with hidden visibility. 272*665efe89SPiJoules 273*665efe89SPiJoulesExample usage: 274*665efe89SPiJoules 275*665efe89SPiJoules.. code-block:: c++ 276*665efe89SPiJoules 277*665efe89SPiJoules #include "src/__support/macros/config.h" // The macro is defined here. 278*665efe89SPiJoules 279*665efe89SPiJoules namespace LIBC_NAMESPACE_DECL { 280*665efe89SPiJoules 281*665efe89SPiJoules void new_function() { 282*665efe89SPiJoules ... 283*665efe89SPiJoules } 284*665efe89SPiJoules 285*665efe89SPiJoules } // LIBC_NAMESPACE_DECL 286*665efe89SPiJoules 287*665efe89SPiJoulesHaving hidden visibility on the namespace ensures extern declarations in a given TU 288*665efe89SPiJouleshave known visibility and never generate GOT indirextions. The attribute guarantees 289*665efe89SPiJoulesthis independently of global compile options and build systems. 290*665efe89SPiJoules 291*665efe89SPiJoules.. 292*665efe89SPiJoules TODO(97655): We should have a clang-tidy check to enforce this and a 293*665efe89SPiJoules fixit implementation. 294