(implementing_a_function)=
This guide provides a step-by-step walkthrough for adding a new function to LLVM-libc.
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).
Update the standard YAML file that describes the public header to ensure the function is included in the generated public header.
libc/include/<header>.yaml (or libc/include/sys/<header>.yaml for system headers)functions list.stdc, POSIX).Declare the function in the internal implementation header file. This file is used by other internal code.
libc/src/<header>/<func>.himplementation_standard.LIBC_NAMESPACE_DECL namespace.Write the actual code for the function.
libc/src/<header>/<func>.cpp (or libc/src/<header>/<os>/<func>.cpp for platform-specific implementations)LLVM_LIBC_FUNCTION macro.code_style for naming and layout conventions and to {ref}implementation_guidelines for memory use and input checking.syscall_wrapper_refactor.Add a CMake target for the new function so it can be compiled.
libc/src/<header>/CMakeLists.txtadd_entrypoint_object rule for the new file.errno, include both libc.src.__support.libc_errno and libc.src.errno.errno in DEPENDS (see {ref}setting_errno).Register the new entrypoint for the target platforms to include it in the build.
libc/config/<os>/<arch>/entrypoints.txtcode_organization for the minimum set and for the difference between the overlay and full build lists.Create tests to verify the implementation.
libc/test/src/<header>/<func>_test.cpperrno, use ErrnoCheckingTest and ErrnoSetterMatcher (see {ref}setting_errno).CMakeLists.txt in the test directory (libc/test/src/<header>/CMakeLists.txt) to include the new test target.testing_guide for what the test should cover and how to confirm that it ran.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:
libc/include/llvm-libc-types/<type>.h and an add_header entry for it in libc/include/llvm-libc-types/CMakeLists.txt. A structure such as struct stat is named struct_stat in file and target names. See {ref}choosing_type_layout and {ref}writing_public_headers before choosing the definition.types in libc/include/<header>.yaml for every header that provides it, and add .llvm-libc-types.<type> to the DEPENDS of that header's add_header_macro rule in libc/include/CMakeLists.txt.libc/hdr/types/<type>.h and an add_proxy_header_library entry in libc/hdr/types/CMakeLists.txt. Implementation and test code include the type through this header. See the header inclusion policy in {ref}code_style.