diff options
Diffstat (limited to 'doc/develop/codingstyle.rst')
-rw-r--r-- | doc/develop/codingstyle.rst | 81 |
1 files changed, 79 insertions, 2 deletions
diff --git a/doc/develop/codingstyle.rst b/doc/develop/codingstyle.rst index 8ed6babe455..013bfebf7e4 100644 --- a/doc/develop/codingstyle.rst +++ b/doc/develop/codingstyle.rst @@ -12,8 +12,9 @@ or only minimal changes. The following rules apply: * All contributions to U-Boot should conform to the `Linux kernel - coding style <https://www.kernel.org/doc/html/latest/process/coding-style.html>`_ - and the `Lindent script <https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/scripts/Lindent>`_. + coding style <https://www.kernel.org/doc/html/latest/process/coding-style.html>`_. + U-Boot includes a `.clang-format` configuration file that can be used to + automatically format code according to these standards. * The exception for net files to the `multi-line comment <https://www.kernel.org/doc/html/latest/process/coding-style.html#commenting>`_ applies only to Linux, not to U-Boot. Only large hunks which are copied @@ -73,6 +74,82 @@ The following rules apply: issues are resolved *before* posting on the mailing list. For more information, read :doc:`checkpatch`. +Code Formatting with clang-format +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +U-Boot provides a `.clang-format` configuration file that was copied directly +from the Linux kernel without any modifications, ensuring complete compatibility +with kernel coding standards. Here are common ways to use clang-format: + +**Basic usage for single files:** + +.. code-block:: bash + + clang-format -style=file -i <file> + +**Format multiple files:** + +.. code-block:: bash + + find . -name '*.c' -o -name '*.h' | xargs clang-format -style=file -i + +**Integration with git (format only staged changes):** + +.. code-block:: bash + + git clang-format + +**Editor integration examples:** + +* **Vim/Neovim:** Install vim-clang-format plugin +* **Emacs:** Use clang-format.el +* **VSCode:** Install "Clang-Format" extension +* **Most IDEs:** Have built-in or plugin support for clang-format + +The `.clang-format` file in the repository root ensures consistent formatting +across the entire codebase and aligns with Linux kernel coding standards. + +**Disabling clang-format for specific code blocks:** + +In some cases, you may want to disable automatic formatting for specific code +sections, such as carefully formatted tables, assembly code, or imported code +from other projects. Use the following comments to control formatting: + +.. code-block:: c + + // clang-format off + static const struct register_config regs[] = { + { 0x1000, 0x12345678 }, // Base address register + { 0x1004, 0xabcdef00 }, // Control register + { 0x1008, 0x00000001 }, // Status register + }; + // clang-format on + +**Controversial aspects of coding style enforcement:** + +Coding style enforcement can be controversial, and it's difficult to have one +configuration that satisfies everyone's personal preferences. The goal of using +clang-format is consistency across the codebase rather than accommodating +individual preferences. While some developers may disagree with specific +formatting choices, maintaining a uniform style throughout the project makes +code more readable and maintainable for the entire development community. + +**Best practices for formatting:** + +When using clang-format to format code, consider these best practices: + +* **Format only changed blocks:** It's preferred to format only the blocks of + code that have been modified rather than entire files. This keeps diffs + focused on actual changes and makes code reviews easier. + +* **Separate formatting commits:** If you need to format entire files, create + a separate commit containing only formatting changes. This allows reviewers + to easily distinguish between functional changes and pure formatting updates. + +* **Use git clang-format:** The ``git clang-format`` command is particularly + useful as it formats only the lines that have been modified in your current + changes, avoiding unnecessary formatting of unchanged code. + * Source files originating from different projects (for example the MTD subsystem or the hush shell code from the BusyBox project) may, after careful consideration, be exempted from these rules. For such files, the |