This was made as an attempt to write down as much of what I know about LLVM-libc as possible. The process involved using an LLM agent to interview me about what wasn't clear from the codebase and then write docs based on my answers. After that were several rounds of revisions where I tweaked phrasing and corrected more issues. The final documents are an accurate reflection of my understanding of the state of LLVM-libc. Assisted-by: Automated tooling, human reviewed.
4.0 KiB
(implementing_a_function)=
Implementing a New Function
This guide provides a step-by-step walkthrough for adding a new function to LLVM-libc.
Overview
Adding a new function involves several steps, from updating the public specification to implementing and testing the code. Below is the standard checklist for contributors.
Before starting, read {ref}code_organization and {ref}patterns_to_avoid.
The most common mistake in a new function is calling another public function
from it, which is not allowed (see
{ref}entrypoints_do_not_call_entrypoints).
Step-by-Step Checklist
1. Header Entry
Update the standard YAML file that describes the public header to ensure the function is included in the generated public header.
- File:
libc/include/<header>.yaml(orlibc/include/sys/<header>.yamlfor system headers) - Add the new function to the
functionslist. - Specify its name, return type, and arguments.
- List the standards it complies with (e.g.,
stdc,POSIX).
2. Header Declaration
Declare the function in the internal implementation header file. This file is used by other internal code.
- File:
libc/src/<header>/<func>.h - Follow the structure defined in {ref}
implementation_standard. - Ensure the declaration is inside the
LIBC_NAMESPACE_DECLnamespace.
3. Implementation
Write the actual code for the function.
- File:
libc/src/<header>/<func>.cpp(orlibc/src/<header>/<os>/<func>.cppfor platform-specific implementations) - Use the
LLVM_LIBC_FUNCTIONmacro. - Refer to {ref}
code_stylefor naming and layout conventions and to {ref}implementation_guidelinesfor memory use and input checking. - A function that makes a system call does so through a wrapper. See
{ref}
syscall_wrapper_refactor.
4. CMake Rule
Add a CMake target for the new function so it can be compiled.
- File:
libc/src/<header>/CMakeLists.txt - Add an
add_entrypoint_objectrule for the new file. - List all internal dependencies correctly to ensure proper build order. If the
function reads or sets
errno, include bothlibc.src.__support.libc_errnoandlibc.src.errno.errnoinDEPENDS(see {ref}setting_errno).
5. Platform Registration
Register the new entrypoint for the target platforms to include it in the build.
- File:
libc/config/<os>/<arch>/entrypoints.txt - Add the new function to the list of active entrypoints, on every target
where it works. See {ref}
code_organizationfor the minimum set and for the difference between the overlay and full build lists.
6. Testing
Create tests to verify the implementation.
- File:
libc/test/src/<header>/<func>_test.cpp - Add corresponding tests using the internal testing framework. For functions
setting
errno, useErrnoCheckingTestandErrnoSetterMatcher(see {ref}setting_errno). - Update the
CMakeLists.txtin the test directory (libc/test/src/<header>/CMakeLists.txt) to include the new test target. - See {ref}
testing_guidefor what the test should cover and how to confirm that it ran.
Adding a New Public Type
A function sometimes needs a public type that LLVM-libc does not define yet. A type is registered separately from the functions that use it:
- Definition: add
libc/include/llvm-libc-types/<type>.hand anadd_headerentry for it inlibc/include/llvm-libc-types/CMakeLists.txt. A structure such asstruct statis namedstruct_statin file and target names. See {ref}choosing_type_layoutand {ref}writing_public_headersbefore choosing the definition. - Public header: list the type under
typesinlibc/include/<header>.yamlfor every header that provides it, and add.llvm-libc-types.<type>to theDEPENDSof that header'sadd_header_macrorule inlibc/include/CMakeLists.txt. - Proxy header: add
libc/hdr/types/<type>.hand anadd_proxy_header_libraryentry inlibc/hdr/types/CMakeLists.txt. Implementation and test code include the type through this header. See the header inclusion policy in {ref}code_style.