Bump version to 19.1.0 (final)
[llvm-project.git] / libc / docs / dev / clang_tidy_checks.rst
blob91d415a2e0d47e16386346cba4015665cf32cec6
1 .. _clang_tidy_checks:
3 LLVM libc clang-tidy checks
4 ===========================
7 .. warning::
8   This page is severely out of date. Much of the information it contains may be
9   incorrect. Please only remove this warning once the page has been updated.
11 These are the clang-tidy checks designed to help enforce implementation
12 standards.
13 The configuration file is ``src/.clang-tidy``.
15 restrict-system-libc-header
16 ---------------------------
17 One of libc-project’s design goals is to use kernel headers and compiler
18 provided headers to prevent code duplication on a per platform basis. This
19 presents a problem when writing implementations since system libc headers are
20 easy to include accidentally and we can't just use the ``-nostdinc`` flag.
21 Improperly included system headers can introduce runtime errors because the C
22 standard outlines function prototypes and behaviors but doesn’t define
23 underlying implementation details such as the layout of a struct.
25 This check prevents accidental inclusion of system libc headers when writing a
26 libc implementation.
28 .. code-block:: c++
30    #include <stdio.h>            // Not allowed because it is part of system libc.
31    #include <stddef.h>           // Allowed because it is provided by the compiler.
32    #include "internal/stdio.h"   // Allowed because it is NOT part of system libc.
35 implementation-in-namespace
36 ---------------------------
38 It is part of our implementation standards that all implementation pieces live
39 under the ``LIBC_NAMESPACE_DECL`` namespace. This prevents pollution of the
40 global namespace. Without a formal check to ensure this, an implementation
41 might compile and pass unit tests, but not produce a usable libc function.
43 This check that ensures any function call resolves to a function within the
44 ``LIBC_NAMESPACE_DECL`` namespace.
46 .. code-block:: c++
48     // Correct: implementation inside the correct namespace.
49     namespace LIBC_NAMESPACE_DECL {
50         void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {}
51         // Namespaces within LIBC_NAMESPACE namespace are allowed.
52         namespace inner{
53             int localVar = 0;
54         }
55         // Functions with C linkage are allowed.
56         extern "C" void str_fuzz(){}
57     }
59     // Incorrect: implementation not in a namespace.
60     void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {}
62     // Incorrect: outer most namespace is not correct.
63     namespace something_else {
64         void LLVM_LIBC_ENTRYPOINT(strcpy)(char *dest, const char *src) {}
65     }
68   TODO(97655): The clang-tidy check should be updated to ensure the namespace
69   declaration uses LIBC_NAMESPACE_DECL as opposed to LIBC_NAMESPACE. The former
70   should be used for accessing globals in LIBC_NAMESPACE rather than declaration.
73 callee-namespace
74 ----------------
75 LLVM-libc is distinct because it is designed to maintain interoperability with
76 other libc libraries, including the one that lives on the system. This feature
77 creates some uncertainty about which library a call resolves to especially when
78 a public header with non-namespaced functions like ``string.h`` is included.
80 This check ensures any function call resolves to a function within the
81 LIBC_NAMESPACE namespace.
83 There are exceptions for the following functions:
84 ``__errno_location`` so that ``errno`` can be set;
85 ``malloc``, ``calloc``, ``realloc``, ``aligned_alloc``, and ``free`` since they
86 are always external and can be intercepted.
88 .. code-block:: c++
90     namespace LIBC_NAMESPACE_DECL {
92     // Allow calls with the fully qualified name.
93     LIBC_NAMESPACE::strlen("hello");
95     // Allow calls to compiler provided functions.
96     (void)__builtin_abs(-1);
98     // Bare calls are allowed as long as they resolve to the correct namespace.
99     strlen("world");
101     // Disallow calling into functions in the global namespace.
102     ::strlen("!");
104     // Allow calling into specific global functions (explained above)
105     ::malloc(10);
107     } // namespace LIBC_NAMESPACE_DECL