18c2ecf20Sopenharmony_ci.. _clangformat: 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ciclang-format 48c2ecf20Sopenharmony_ci============ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci``clang-format`` is a tool to format C/C++/... code according to 78c2ecf20Sopenharmony_cia set of rules and heuristics. Like most tools, it is not perfect 88c2ecf20Sopenharmony_cinor covers every single case, but it is good enough to be helpful. 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci``clang-format`` can be used for several purposes: 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci - Quickly reformat a block of code to the kernel style. Specially useful 138c2ecf20Sopenharmony_ci when moving code around and aligning/sorting. See clangformatreformat_. 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci - Spot style mistakes, typos and possible improvements in files 168c2ecf20Sopenharmony_ci you maintain, patches you review, diffs, etc. See clangformatreview_. 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci - Help you follow the coding style rules, specially useful for those 198c2ecf20Sopenharmony_ci new to kernel development or working at the same time in several 208c2ecf20Sopenharmony_ci projects with different coding styles. 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ciIts configuration file is ``.clang-format`` in the root of the kernel tree. 238c2ecf20Sopenharmony_ciThe rules contained there try to approximate the most common kernel 248c2ecf20Sopenharmony_cicoding style. They also try to follow :ref:`Documentation/process/coding-style.rst <codingstyle>` 258c2ecf20Sopenharmony_cias much as possible. Since not all the kernel follows the same style, 268c2ecf20Sopenharmony_ciit is possible that you may want to tweak the defaults for a particular 278c2ecf20Sopenharmony_cisubsystem or folder. To do so, you can override the defaults by writing 288c2ecf20Sopenharmony_cianother ``.clang-format`` file in a subfolder. 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciThe tool itself has already been included in the repositories of popular 318c2ecf20Sopenharmony_ciLinux distributions for a long time. Search for ``clang-format`` in 328c2ecf20Sopenharmony_ciyour repositories. Otherwise, you can either download pre-built 338c2ecf20Sopenharmony_ciLLVM/clang binaries or build the source code from: 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci https://releases.llvm.org/download.html 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ciSee more information about the tool at: 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci https://clang.llvm.org/docs/ClangFormat.html 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci https://clang.llvm.org/docs/ClangFormatStyleOptions.html 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci.. _clangformatreview: 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ciReview files and patches for coding style 478c2ecf20Sopenharmony_ci----------------------------------------- 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ciBy running the tool in its inline mode, you can review full subsystems, 508c2ecf20Sopenharmony_cifolders or individual files for code style mistakes, typos or improvements. 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ciTo do so, you can run something like:: 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci # Make sure your working directory is clean! 558c2ecf20Sopenharmony_ci clang-format -i kernel/*.[ch] 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ciAnd then take a look at the git diff. 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciCounting the lines of such a diff is also useful for improving/tweaking 608c2ecf20Sopenharmony_cithe style options in the configuration file; as well as testing new 618c2ecf20Sopenharmony_ci``clang-format`` features/versions. 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci``clang-format`` also supports reading unified diffs, so you can review 648c2ecf20Sopenharmony_cipatches and git diffs easily. See the documentation at: 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci https://clang.llvm.org/docs/ClangFormat.html#script-for-patch-reformatting 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciTo avoid ``clang-format`` formatting some portion of a file, you can do:: 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci int formatted_code; 718c2ecf20Sopenharmony_ci // clang-format off 728c2ecf20Sopenharmony_ci void unformatted_code ; 738c2ecf20Sopenharmony_ci // clang-format on 748c2ecf20Sopenharmony_ci void formatted_code_again; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ciWhile it might be tempting to use this to keep a file always in sync with 778c2ecf20Sopenharmony_ci``clang-format``, specially if you are writing new files or if you are 788c2ecf20Sopenharmony_cia maintainer, please note that people might be running different 798c2ecf20Sopenharmony_ci``clang-format`` versions or not have it available at all. Therefore, 808c2ecf20Sopenharmony_ciyou should probably refrain yourself from using this in kernel sources; 818c2ecf20Sopenharmony_ciat least until we see if ``clang-format`` becomes commonplace. 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci.. _clangformatreformat: 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ciReformatting blocks of code 878c2ecf20Sopenharmony_ci--------------------------- 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ciBy using an integration with your text editor, you can reformat arbitrary 908c2ecf20Sopenharmony_ciblocks (selections) of code with a single keystroke. This is specially 918c2ecf20Sopenharmony_ciuseful when moving code around, for complex code that is deeply intended, 928c2ecf20Sopenharmony_cifor multi-line macros (and aligning their backslashes), etc. 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ciRemember that you can always tweak the changes afterwards in those cases 958c2ecf20Sopenharmony_ciwhere the tool did not do an optimal job. But as a first approximation, 968c2ecf20Sopenharmony_ciit can be very useful. 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ciThere are integrations for many popular text editors. For some of them, 998c2ecf20Sopenharmony_cilike vim, emacs, BBEdit and Visual Studio you can find support built-in. 1008c2ecf20Sopenharmony_ciFor instructions, read the appropiate section at: 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci https://clang.llvm.org/docs/ClangFormat.html 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ciFor Atom, Eclipse, Sublime Text, Visual Studio Code, XCode and other 1058c2ecf20Sopenharmony_cieditors and IDEs you should be able to find ready-to-use plugins. 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ciFor this use case, consider using a secondary ``.clang-format`` 1088c2ecf20Sopenharmony_ciso that you can tweak a few options. See clangformatextra_. 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci.. _clangformatmissing: 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ciMissing support 1148c2ecf20Sopenharmony_ci--------------- 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci``clang-format`` is missing support for some things that are common 1178c2ecf20Sopenharmony_ciin kernel code. They are easy to remember, so if you use the tool 1188c2ecf20Sopenharmony_ciregularly, you will quickly learn to avoid/ignore those. 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ciIn particular, some very common ones you will notice are: 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci - Aligned blocks of one-line ``#defines``, e.g.:: 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci #define TRACING_MAP_BITS_DEFAULT 11 1258c2ecf20Sopenharmony_ci #define TRACING_MAP_BITS_MAX 17 1268c2ecf20Sopenharmony_ci #define TRACING_MAP_BITS_MIN 7 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci vs.:: 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci #define TRACING_MAP_BITS_DEFAULT 11 1318c2ecf20Sopenharmony_ci #define TRACING_MAP_BITS_MAX 17 1328c2ecf20Sopenharmony_ci #define TRACING_MAP_BITS_MIN 7 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci - Aligned designated initializers, e.g.:: 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci static const struct file_operations uprobe_events_ops = { 1378c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 1388c2ecf20Sopenharmony_ci .open = probes_open, 1398c2ecf20Sopenharmony_ci .read = seq_read, 1408c2ecf20Sopenharmony_ci .llseek = seq_lseek, 1418c2ecf20Sopenharmony_ci .release = seq_release, 1428c2ecf20Sopenharmony_ci .write = probes_write, 1438c2ecf20Sopenharmony_ci }; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci vs.:: 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci static const struct file_operations uprobe_events_ops = { 1488c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 1498c2ecf20Sopenharmony_ci .open = probes_open, 1508c2ecf20Sopenharmony_ci .read = seq_read, 1518c2ecf20Sopenharmony_ci .llseek = seq_lseek, 1528c2ecf20Sopenharmony_ci .release = seq_release, 1538c2ecf20Sopenharmony_ci .write = probes_write, 1548c2ecf20Sopenharmony_ci }; 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci.. _clangformatextra: 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ciExtra features/options 1608c2ecf20Sopenharmony_ci---------------------- 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ciSome features/style options are not enabled by default in the configuration 1638c2ecf20Sopenharmony_cifile in order to minimize the differences between the output and the current 1648c2ecf20Sopenharmony_cicode. In other words, to make the difference as small as possible, 1658c2ecf20Sopenharmony_ciwhich makes reviewing full-file style, as well diffs and patches as easy 1668c2ecf20Sopenharmony_cias possible. 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ciIn other cases (e.g. particular subsystems/folders/files), the kernel style 1698c2ecf20Sopenharmony_cimight be different and enabling some of these options may approximate 1708c2ecf20Sopenharmony_cibetter the style there. 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ciFor instance: 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci - Aligning assignments (``AlignConsecutiveAssignments``). 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci - Aligning declarations (``AlignConsecutiveDeclarations``). 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci - Reflowing text in comments (``ReflowComments``). 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci - Sorting ``#includes`` (``SortIncludes``). 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ciThey are typically useful for block re-formatting, rather than full-file. 1838c2ecf20Sopenharmony_ciYou might want to create another ``.clang-format`` file and use that one 1848c2ecf20Sopenharmony_cifrom your editor/IDE instead. 185