clang-format.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. .. _clangformat:
  2. clang-format
  3. ============
  4. ``clang-format`` is a tool to format C/C++/... code according to
  5. a set of rules and heuristics. Like most tools, it is not perfect
  6. nor covers every single case, but it is good enough to be helpful.
  7. ``clang-format`` can be used for several purposes:
  8. - Quickly reformat a block of code to the kernel style. Specially useful
  9. when moving code around and aligning/sorting. See clangformatreformat_.
  10. - Spot style mistakes, typos and possible improvements in files
  11. you maintain, patches you review, diffs, etc. See clangformatreview_.
  12. - Help you follow the coding style rules, specially useful for those
  13. new to kernel development or working at the same time in several
  14. projects with different coding styles.
  15. Its configuration file is ``.clang-format`` in the root of the kernel tree.
  16. The rules contained there try to approximate the most common kernel
  17. coding style. They also try to follow :ref:`Documentation/process/coding-style.rst <codingstyle>`
  18. as much as possible. Since not all the kernel follows the same style,
  19. it is possible that you may want to tweak the defaults for a particular
  20. subsystem or folder. To do so, you can override the defaults by writing
  21. another ``.clang-format`` file in a subfolder.
  22. The tool itself has already been included in the repositories of popular
  23. Linux distributions for a long time. Search for ``clang-format`` in
  24. your repositories. Otherwise, you can either download pre-built
  25. LLVM/clang binaries or build the source code from:
  26. https://releases.llvm.org/download.html
  27. See more information about the tool at:
  28. https://clang.llvm.org/docs/ClangFormat.html
  29. https://clang.llvm.org/docs/ClangFormatStyleOptions.html
  30. .. _clangformatreview:
  31. Review files and patches for coding style
  32. -----------------------------------------
  33. By running the tool in its inline mode, you can review full subsystems,
  34. folders or individual files for code style mistakes, typos or improvements.
  35. To do so, you can run something like::
  36. # Make sure your working directory is clean!
  37. clang-format -i kernel/*.[ch]
  38. And then take a look at the git diff.
  39. Counting the lines of such a diff is also useful for improving/tweaking
  40. the style options in the configuration file; as well as testing new
  41. ``clang-format`` features/versions.
  42. ``clang-format`` also supports reading unified diffs, so you can review
  43. patches and git diffs easily. See the documentation at:
  44. https://clang.llvm.org/docs/ClangFormat.html#script-for-patch-reformatting
  45. To avoid ``clang-format`` formatting some portion of a file, you can do::
  46. int formatted_code;
  47. // clang-format off
  48. void unformatted_code ;
  49. // clang-format on
  50. void formatted_code_again;
  51. While it might be tempting to use this to keep a file always in sync with
  52. ``clang-format``, specially if you are writing new files or if you are
  53. a maintainer, please note that people might be running different
  54. ``clang-format`` versions or not have it available at all. Therefore,
  55. you should probably refrain yourself from using this in kernel sources;
  56. at least until we see if ``clang-format`` becomes commonplace.
  57. .. _clangformatreformat:
  58. Reformatting blocks of code
  59. ---------------------------
  60. By using an integration with your text editor, you can reformat arbitrary
  61. blocks (selections) of code with a single keystroke. This is specially
  62. useful when moving code around, for complex code that is deeply intended,
  63. for multi-line macros (and aligning their backslashes), etc.
  64. Remember that you can always tweak the changes afterwards in those cases
  65. where the tool did not do an optimal job. But as a first approximation,
  66. it can be very useful.
  67. There are integrations for many popular text editors. For some of them,
  68. like vim, emacs, BBEdit and Visual Studio you can find support built-in.
  69. For instructions, read the appropiate section at:
  70. https://clang.llvm.org/docs/ClangFormat.html
  71. For Atom, Eclipse, Sublime Text, Visual Studio Code, XCode and other
  72. editors and IDEs you should be able to find ready-to-use plugins.
  73. For this use case, consider using a secondary ``.clang-format``
  74. so that you can tweak a few options. See clangformatextra_.
  75. .. _clangformatmissing:
  76. Missing support
  77. ---------------
  78. ``clang-format`` is missing support for some things that are common
  79. in kernel code. They are easy to remember, so if you use the tool
  80. regularly, you will quickly learn to avoid/ignore those.
  81. In particular, some very common ones you will notice are:
  82. - Aligned blocks of one-line ``#defines``, e.g.::
  83. #define TRACING_MAP_BITS_DEFAULT 11
  84. #define TRACING_MAP_BITS_MAX 17
  85. #define TRACING_MAP_BITS_MIN 7
  86. vs.::
  87. #define TRACING_MAP_BITS_DEFAULT 11
  88. #define TRACING_MAP_BITS_MAX 17
  89. #define TRACING_MAP_BITS_MIN 7
  90. - Aligned designated initializers, e.g.::
  91. static const struct file_operations uprobe_events_ops = {
  92. .owner = THIS_MODULE,
  93. .open = probes_open,
  94. .read = seq_read,
  95. .llseek = seq_lseek,
  96. .release = seq_release,
  97. .write = probes_write,
  98. };
  99. vs.::
  100. static const struct file_operations uprobe_events_ops = {
  101. .owner = THIS_MODULE,
  102. .open = probes_open,
  103. .read = seq_read,
  104. .llseek = seq_lseek,
  105. .release = seq_release,
  106. .write = probes_write,
  107. };
  108. .. _clangformatextra:
  109. Extra features/options
  110. ----------------------
  111. Some features/style options are not enabled by default in the configuration
  112. file in order to minimize the differences between the output and the current
  113. code. In other words, to make the difference as small as possible,
  114. which makes reviewing full-file style, as well diffs and patches as easy
  115. as possible.
  116. In other cases (e.g. particular subsystems/folders/files), the kernel style
  117. might be different and enabling some of these options may approximate
  118. better the style there.
  119. For instance:
  120. - Aligning assignments (``AlignConsecutiveAssignments``).
  121. - Aligning declarations (``AlignConsecutiveDeclarations``).
  122. - Reflowing text in comments (``ReflowComments``).
  123. - Sorting ``#includes`` (``SortIncludes``).
  124. They are typically useful for block re-formatting, rather than full-file.
  125. You might want to create another ``.clang-format`` file and use that one
  126. from your editor/IDE instead.