Text of libc.llvm.org › dev › code_style.html as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

The libc code style - The LLVM C Library

Hide navigation sidebar Hide table of contents sidebar Back to top Edit this page Toggle table of contents sidebar The libc code style # Naming style # For the large part, the libc project follows the general coding standards of the LLVM project . The libc project differs from that standard with respect to the naming style. The differences are as follows: Non-const variables - This includes function arguments, struct and class data members, non-const globals and local variables.

They all use the snake_case style. const and constexpr variables - They use the capitalized SNAKE_CASE irrespective of whether they are local or global. Function and methods - They use the snake_case style like the non-const variables. Internal type names - These are types which are internal to the libc implementation. They use the CaptilizedCamelCase style. Public names - These are the names as prescribed by the standards and will follow the style as prescribed by the standards.

Macro style # We define two kinds of macros: Build defined macros are generated by CMake or Bazel and are passed down to the compiler with the -D command line flag. They start with the LIBC_COPT_ prefix. They are used to tune the behavior of the libc. They either denote an action or define a constant. Code defined macros are defined within the src/__support/macros folder. They all start with the LIBC_ prefix. src/__support/macros/properties/ - Build related properties like target architecture or enabled CPU features defined by introspecting compiler defined preprocessor definitions. architectures.h - Target architecture properties. e.g., LIBC_TARGET_ARCH_IS_ARM . compiler.h - Host compiler properties. e.g., LIBC_COMPILER_IS_CLANG . cpu_features.h - Target cpu feature availability. e.g., LIBC_TARGET_CPU_HAS_AVX2 . types.h - Type properties and availability. e.g., LIBC_TYPES_HAS_NATIVE_FLOAT128 . os.h - Target os properties. e.g., LIBC_TARGET_OS_IS_LINUX . src/__support/macros/config.h - Important compiler and platform features.

Such macros can be used to produce portable code by parameterizing compilation based on the presence or lack of a given feature. e.g., LIBC_HAS_FEATURE src/__support/macros/attributes.h - Attributes for functions, types, and variables. e.g., LIBC_UNUSED src/__support/macros/optimization.h - Portable macros for performance optimization. e.g., LIBC_LIKELY , LIBC_LOOP_NOUNROLL Inline functions and variables defined in header files # When defining functions and variables inline in header files, we follow certain rules: The functions should not be given file-static linkage.

There can be class static methods defined inline however. Instead of using the inline keyword, functions should be tagged with the LIBC_INLINE macro and variables should be tagged with the LIBC_INLINE_VAR macro defined in src/__support/macros/attributes.h . For example: LIBC_INLINE_VAR constexpr bool foo = true ; LIBC_INLINE ReturnType function_defined_inline ( ArgType arg ) { ... } The LIBC_INLINE tag should also be added to functions which have definitions that are implicitly inline.

Examples of such functions are class methods (static and non-static) defined inline and constexpr functions. Setting errno from runtime code # Many libc functions set errno to indicate an error condition. In full build mode, LLVM-libc uses its own internal errno . In Overlay Mode , the public libllvmlibc.a archive uses the system libc’s errno , whereas unit tests use LLVM-libc’s internal thread-local errno so that tested functions do not clobber the system errno used by the test harness.

To ensure errno works across all build modes, follow these rules (see errno and libc_errno in LLVM-libc for full details on targets, LIBC_ERRNO_MODE options, and build dependencies): Header inclusion: Code and unit tests that read or write errno must #include "src/__support/libc_errno.h" and use libc_errno : libc_errno = EINVAL ; Code that only needs error constants ( EINVAL , ERANGE , etc.) without accessing libc_errno should #include "hdr/errno_macros.h" instead.

Never #include <errno.h> in implementation or unit test files. Where to set errno : Set libc_errno only at the public entrypoint boundary just before returning (or via fputil::set_errno_if_required in math code). Internal helpers should not modify libc_errno directly; instead, return error values using cpp::optional or ErrorOr . Build dependencies: Targets in libc/src/__support/ must depend on libc.hdr.errno_macros or libc.src.__support.libc_errno , and never on libc.src.errno.errno .

Public entrypoints that access libc_errno should depend on libc.src.__support.libc_errno (and libc.src.errno.errno for non-math libc.a entrypoints). Test targets ( add_libc_test , add_fp_unittest , etc.) automatically link libc.src.errno.errno . Accessing values from cpp::expected , ErrorOr , and cpp::optional # Because LLVM-libc builds with -fno-exceptions , cpp::expected (along with ErrorOr<T> , which aliases cpp::expected<T, int> ) and cpp::optional cannot throw on invalid access.

Instead, their observers provide two distinct safety contracts: .value() and .error() are checked in all build modes: Calling .value() when !has_value() (or .error() on cpp::expected when has_value() ) unconditionally executes __builtin_trap() , including in release ( NDEBUG ) builds. operator* and operator-> are unchecked in release builds: They trap under #ifndef NDEBUG when !has_value() to catch bugs during testing, and compile to direct member access with no check in release builds.

Choose between them as follows: Prefer * and -> after an explicit check: Once has_value() or operator bool has been checked, prefer *res and res->member , especially in performance-sensitive paths (such as math or formatting fast paths) and whenever accessing members of the contained object ( res->field rather than res.value().field ). When the unwrapped value is needed more than once in a scope, bind it to a local variable instead of unwrapping repeatedly: auto res = linux_syscalls :: open ( path , flags , mode ); if ( ! res ) { libc_errno = res . error (); return -1 ; } int fd = * res ; Use .value() when relying on an invariant without a branch: If an algorithm or API invariant guarantees that a value is present and you intentionally omit an if (!res) check, you must use .value() rather than *res .

That ensures any unexpected violation traps deterministically in release builds instead of reading an inactive union member. .value() is also acceptable for readability on bool or pointer types: After a dominating if (!res) check, writing if (!res.value()) on ErrorOr<bool> or FILE *stream = res.value(); on ErrorOr<FILE *> avoids visually ambiguous !*res expressions or confusion between unwrapping the container and dereferencing the pointer.

Under optimization, jump-threading eliminates the redundant check in res.value() when no opaque calls intervene. Assertions in libc runtime code # The libc developers should, and are encouraged to, use assertions freely in the libc runtime code. However, the assertion should be listed via the macro LIBC_ASSERT defined in src/__support/libc_assert.h . This macro can be used from anywhere in the libc runtime code. Internally, all it does is to print the assertion expression and exit.

It does not implement the semantics of the standard assert macro. Hence, it can be used from any where in the libc runtime code without causing any recursive calls or chicken-and-egg situations. Allocations in the libc runtime code # Some libc functions allocate memory. For example, the strdup function allocates new memory into which the input string is duplicated. Allocations are typically done by calling a function from the malloc family of functions.

Such functions can fail and return an error value to indicate allocation failure. To conform to standards, the libc should handle allocation failures gracefully and surface the error conditions to the user code as appropriate. Since LLVM’s libc is implemented in C++, we want all

The rest of this page is on the live site.