3 LLVM libc clang-tidy checks
4 ===========================
5 These are the clang-tidy checks designed to help enforce implementation
7 The configuration file is ``src/.clang-tidy``.
9 restrict-system-libc-header
10 ---------------------------
11 One of libc-project’s design goals is to use kernel headers and compiler
12 provided headers to prevent code duplication on a per platform basis. This
13 presents a problem when writing implementations since system libc headers are
14 easy to include accidentally and we can't just use the ``-nostdinc`` flag.
15 Improperly included system headers can introduce runtime errors because the C
16 standard outlines function prototypes and behaviors but doesn’t define
17 underlying implementation details such as the layout of a struct.
19 This check prevents accidental inclusion of system libc headers when writing a
24 #include <stdio.h> // Not allowed because it is part of system libc.
25 #include <stddef.h> // Allowed because it is provided by the compiler.
26 #include "internal/stdio.h" // Allowed because it is NOT part of system libc.
29 implementation-in-namespace
30 ---------------------------
32 It is part of our implementation standards that all implementation pieces live
33 under the ``__llvm_libc`` namespace. This prevents pollution of the global
34 namespace. Without a formal check to ensure this, an implementation might
35 compile and pass unit tests, but not produce a usable libc function.
37 This check that ensures any function call resolves to a function within the
38 ``__llvm_libc`` namespace.
42 // Correct: implementation inside the correct namespace.
43 namespace __llvm_libc {
44 void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {}
45 // Namespaces within __llvm_libc namespace are allowed.
49 // Functions with C linkage are allowed.
50 extern "C" void str_fuzz(){}
53 // Incorrect: implementation not in a namespace.
54 void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {}
56 // Incorrect: outer most namespace is not correct.
57 namespace something_else {
58 void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {}
64 LLVM-libc is distinct because it is designed to maintain interoperability with
65 other libc libraries, including the one that lives on the system. This feature
66 creates some uncertainty about which library a call resolves to especially when
67 a public header with non-namespaced functions like ``string.h`` is included.
69 This check ensures any function call resolves to a function within the
70 __llvm_libc namespace.
72 There are exceptions for the following functions:
73 ``__errno_location`` so that ``errno`` can be set;
74 ``malloc``, ``calloc``, ``realloc``, ``aligned_alloc``, and ``free`` since they
75 are always external and can be intercepted.
79 namespace __llvm_libc {
81 // Allow calls with the fully qualified name.
82 __llvm_libc::strlen("hello");
84 // Allow calls to compiler provided functions.
85 (void)__builtin_abs(-1);
87 // Bare calls are allowed as long as they resolve to the correct namespace.
90 // Disallow calling into functions in the global namespace.
93 // Allow calling into specific global functions (explained above)
96 } // namespace __llvm_libc