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.
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>.hFollow the structure defined in Convention for implementing entrypoints.
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 The libc code style for naming and layout conventions.
4. CMake Rule¶
Add a CMake target for the new function so it can be compiled.
File:
libc/src/<header>/CMakeLists.txtAdd 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 Setting errno from runtime code).
5. Platform Registration¶
Register the new entrypoint for the target platforms to include it in the build.
File:
libc/config/<os>/<arch>/entrypoints.txtAdd the new function to the list of active entrypoints.
6. Testing¶
Create tests to verify the implementation.
File:
libc/test/src/<header>/<func>_test.cppAdd corresponding tests using the internal testing framework. For functions setting
errno, useErrnoCheckingTestandErrnoSetterMatcher(see Setting errno from runtime code).Update the
CMakeLists.txtin the test directory (libc/test/src/<header>/CMakeLists.txt) to include the new test target.