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