(patterns_to_avoid)=
LLVM-libc's conventions have changed over the years, and the tree still contains code written to older ones. Existing code is not precedent. A pattern being present in the tree, even many times, does not make it acceptable in a new change. This page lists the mistakes reviewers see most often and the old patterns that should not be copied.
This applies with particular force to changes written with AI assistance, because a tool that imitates nearby code will imitate its outdated parts too.
Larger areas of the tree that are known to need rework are listed in {ref}roadmap. Do not model new code on them. Issues for LLVM-libc are filed under the libc label in the issue tracker.
entrypoints_do_not_call_entrypoints.making_existing_software_work.original_implementations states where the line is between learning from another implementation and taking from it.libc/include/<header>.yaml and in the relevant entrypoints.txt files in addition to its implementation. A new public type needs its own registration as well. See {ref}implementing_a_function.| Seen in the tree | Write this instead |
|---|---|
A first line of the form //===-- Title ---...---===// in a file header | The current LLVM file header, shown in {ref}implementation_guidelines. Update the header of any file you touch. |
syscall_impl(...) in an entrypoint, followed by a hand-written check of the result | A wrapper in src/__support/OSUtil/<os>/syscall_wrappers/ that uses syscall_checked. See {ref}syscall_wrapper_refactor. |
| Functions and variables inside an anonymous namespace | static functions and variables. Anonymous namespaces are for types only, as in the LLVM coding standards. |
A .cpp file that gets its types from the implementation header it includes | Include what you use: each file includes the proxy header for every type and macro it names. |
#include <pthread.h> or another public header in libc/src/ | The proxy headers in libc/hdr/. |
libc_errno assigned inside an internal helper | The helper returns ErrorOr<T> and the entrypoint assigns libc_errno. |
A new file or directory named utils | A name that says what the code does. |
ASSERT_TRUE(a == b) and ASSERT_TRUE(p != nullptr) in tests | ASSERT_EQ(a, b) and ASSERT_NE(p, nullptr), which print both values on failure. |
A bare path literal such as "testdata/file.txt" or "/nonexistent" in tests | libc_make_test_file_path(...), including for paths that are meant not to exist. |
These are not style violations in the formatting sense. They make code hard to reason about, and reviewers will ask for them to be rewritten.
if inside a loop inside a loop inside an if is difficult to follow. Collapse conditions, return early, and move inner blocks into helper functions.while (true) where the exit test is the first or last statement of the body. That test is the loop condition. Write it as a while or do/while condition. The same applies to a body that ends in if (x) continue; else return;. A loop whose exit test is in the middle of the body is a different case, and while (true) with a break is a reasonable way to write it.static function and pass what it needs explicitly. A short lambda that is passed to another function as a callable is fine.cpp::function_ref with the expected signature, instead of an unconstrained typename T. Where a template is needed, constrain it, for example with cpp::enable_if_t or a static_assert. Overloading a function for the specific types it supports is fine, and so is a template that is generic on purpose. The same applies to variadic argument forwarding.7, 61, or 0xFFF in an expression leaves the reader to work out what it stands for. Give every such constant a name that says what it means. Multiplying by DAYS_PER_WEEK explains itself; multiplying by 7 does not.