|
| 1 | +.. _implementing_a_function: |
| 2 | + |
| 3 | +=========================== |
| 4 | +Implementing a New Function |
| 5 | +=========================== |
| 6 | + |
| 7 | +This guide provides a step-by-step walkthrough for adding a new function to LLVM-libc. |
| 8 | + |
| 9 | +.. contents:: Table of Contents |
| 10 | + :depth: 2 |
| 11 | + :local: |
| 12 | + |
| 13 | +Overview |
| 14 | +======== |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +Step-by-Step Checklist |
| 19 | +====================== |
| 20 | + |
| 21 | +1. Header Entry |
| 22 | +--------------- |
| 23 | + |
| 24 | +Update the standard YAML file that describes the public header to ensure the function is included in the generated public header. |
| 25 | + |
| 26 | +* **File**: ``libc/include/<header>.yaml`` (or ``libc/include/sys/<header>.yaml`` for system headers) |
| 27 | +* Add the new function to the ``functions`` list. |
| 28 | +* Specify its name, return type, and arguments. |
| 29 | +* List the standards it complies with (e.g., ``stdc``, ``POSIX``). |
| 30 | + |
| 31 | +2. Header Declaration |
| 32 | +--------------------- |
| 33 | + |
| 34 | +Declare the function in the internal implementation header file. This file is used by other internal code. |
| 35 | + |
| 36 | +* **File**: ``libc/src/<header>/<func>.h`` |
| 37 | +* Follow the structure defined in :ref:`implementation_standard`. |
| 38 | +* Ensure the declaration is inside the ``LIBC_NAMESPACE_DECL`` namespace. |
| 39 | + |
| 40 | +3. Implementation |
| 41 | +----------------- |
| 42 | + |
| 43 | +Write the actual code for the function. |
| 44 | + |
| 45 | +* **File**: ``libc/src/<header>/<func>.cpp`` (or ``libc/src/<header>/<os>/<func>.cpp`` for platform-specific implementations) |
| 46 | +* Use the ``LLVM_LIBC_FUNCTION`` macro. |
| 47 | +* Refer to :ref:`code_style` for naming and layout conventions. |
| 48 | + |
| 49 | +4. CMake Rule |
| 50 | +------------- |
| 51 | + |
| 52 | +Add a CMake target for the new function so it can be compiled. |
| 53 | + |
| 54 | +* **File**: ``libc/src/<header>/CMakeLists.txt`` |
| 55 | +* Add an ``add_entrypoint_object`` rule for the new file. |
| 56 | +* List all internal dependencies correctly to ensure proper build order. |
| 57 | + |
| 58 | +5. Platform Registration |
| 59 | +------------------------ |
| 60 | + |
| 61 | +Register the new entrypoint for the target platforms to include it in the build. |
| 62 | + |
| 63 | +* **File**: ``libc/config/<os>/<arch>/entrypoints.txt`` |
| 64 | +* Add the new function to the list of active entrypoints. |
| 65 | + |
| 66 | +6. Testing |
| 67 | +---------- |
| 68 | + |
| 69 | +Create tests to verify the implementation. |
| 70 | + |
| 71 | +* **File**: ``libc/test/src/<header>/<func>_test.cpp`` |
| 72 | +* Add corresponding tests using the internal testing framework. |
| 73 | +* Update the ``CMakeLists.txt`` in the test directory (``libc/test/src/<header>/CMakeLists.txt``) to include the new test target. |
0 commit comments