0001 .. _clangformat:
0002
0003 clang-format
0004 ============
0005
0006 ``clang-format`` is a tool to format C/C++/... code according to
0007 a set of rules and heuristics. Like most tools, it is not perfect
0008 nor covers every single case, but it is good enough to be helpful.
0009
0010 ``clang-format`` can be used for several purposes:
0011
0012 - Quickly reformat a block of code to the kernel style. Specially useful
0013 when moving code around and aligning/sorting. See clangformatreformat_.
0014
0015 - Spot style mistakes, typos and possible improvements in files
0016 you maintain, patches you review, diffs, etc. See clangformatreview_.
0017
0018 - Help you follow the coding style rules, specially useful for those
0019 new to kernel development or working at the same time in several
0020 projects with different coding styles.
0021
0022 Its configuration file is ``.clang-format`` in the root of the kernel tree.
0023 The rules contained there try to approximate the most common kernel
0024 coding style. They also try to follow :ref:`Documentation/process/coding-style.rst <codingstyle>`
0025 as much as possible. Since not all the kernel follows the same style,
0026 it is possible that you may want to tweak the defaults for a particular
0027 subsystem or folder. To do so, you can override the defaults by writing
0028 another ``.clang-format`` file in a subfolder.
0029
0030 The tool itself has already been included in the repositories of popular
0031 Linux distributions for a long time. Search for ``clang-format`` in
0032 your repositories. Otherwise, you can either download pre-built
0033 LLVM/clang binaries or build the source code from:
0034
0035 https://releases.llvm.org/download.html
0036
0037 See more information about the tool at:
0038
0039 https://clang.llvm.org/docs/ClangFormat.html
0040
0041 https://clang.llvm.org/docs/ClangFormatStyleOptions.html
0042
0043
0044 .. _clangformatreview:
0045
0046 Review files and patches for coding style
0047 -----------------------------------------
0048
0049 By running the tool in its inline mode, you can review full subsystems,
0050 folders or individual files for code style mistakes, typos or improvements.
0051
0052 To do so, you can run something like::
0053
0054 # Make sure your working directory is clean!
0055 clang-format -i kernel/*.[ch]
0056
0057 And then take a look at the git diff.
0058
0059 Counting the lines of such a diff is also useful for improving/tweaking
0060 the style options in the configuration file; as well as testing new
0061 ``clang-format`` features/versions.
0062
0063 ``clang-format`` also supports reading unified diffs, so you can review
0064 patches and git diffs easily. See the documentation at:
0065
0066 https://clang.llvm.org/docs/ClangFormat.html#script-for-patch-reformatting
0067
0068 To avoid ``clang-format`` formatting some portion of a file, you can do::
0069
0070 int formatted_code;
0071 // clang-format off
0072 void unformatted_code ;
0073 // clang-format on
0074 void formatted_code_again;
0075
0076 While it might be tempting to use this to keep a file always in sync with
0077 ``clang-format``, specially if you are writing new files or if you are
0078 a maintainer, please note that people might be running different
0079 ``clang-format`` versions or not have it available at all. Therefore,
0080 you should probably refrain yourself from using this in kernel sources;
0081 at least until we see if ``clang-format`` becomes commonplace.
0082
0083
0084 .. _clangformatreformat:
0085
0086 Reformatting blocks of code
0087 ---------------------------
0088
0089 By using an integration with your text editor, you can reformat arbitrary
0090 blocks (selections) of code with a single keystroke. This is specially
0091 useful when moving code around, for complex code that is deeply intended,
0092 for multi-line macros (and aligning their backslashes), etc.
0093
0094 Remember that you can always tweak the changes afterwards in those cases
0095 where the tool did not do an optimal job. But as a first approximation,
0096 it can be very useful.
0097
0098 There are integrations for many popular text editors. For some of them,
0099 like vim, emacs, BBEdit and Visual Studio you can find support built-in.
0100 For instructions, read the appropriate section at:
0101
0102 https://clang.llvm.org/docs/ClangFormat.html
0103
0104 For Atom, Eclipse, Sublime Text, Visual Studio Code, XCode and other
0105 editors and IDEs you should be able to find ready-to-use plugins.
0106
0107 For this use case, consider using a secondary ``.clang-format``
0108 so that you can tweak a few options. See clangformatextra_.
0109
0110
0111 .. _clangformatmissing:
0112
0113 Missing support
0114 ---------------
0115
0116 ``clang-format`` is missing support for some things that are common
0117 in kernel code. They are easy to remember, so if you use the tool
0118 regularly, you will quickly learn to avoid/ignore those.
0119
0120 In particular, some very common ones you will notice are:
0121
0122 - Aligned blocks of one-line ``#defines``, e.g.::
0123
0124 #define TRACING_MAP_BITS_DEFAULT 11
0125 #define TRACING_MAP_BITS_MAX 17
0126 #define TRACING_MAP_BITS_MIN 7
0127
0128 vs.::
0129
0130 #define TRACING_MAP_BITS_DEFAULT 11
0131 #define TRACING_MAP_BITS_MAX 17
0132 #define TRACING_MAP_BITS_MIN 7
0133
0134 - Aligned designated initializers, e.g.::
0135
0136 static const struct file_operations uprobe_events_ops = {
0137 .owner = THIS_MODULE,
0138 .open = probes_open,
0139 .read = seq_read,
0140 .llseek = seq_lseek,
0141 .release = seq_release,
0142 .write = probes_write,
0143 };
0144
0145 vs.::
0146
0147 static const struct file_operations uprobe_events_ops = {
0148 .owner = THIS_MODULE,
0149 .open = probes_open,
0150 .read = seq_read,
0151 .llseek = seq_lseek,
0152 .release = seq_release,
0153 .write = probes_write,
0154 };
0155
0156
0157 .. _clangformatextra:
0158
0159 Extra features/options
0160 ----------------------
0161
0162 Some features/style options are not enabled by default in the configuration
0163 file in order to minimize the differences between the output and the current
0164 code. In other words, to make the difference as small as possible,
0165 which makes reviewing full-file style, as well diffs and patches as easy
0166 as possible.
0167
0168 In other cases (e.g. particular subsystems/folders/files), the kernel style
0169 might be different and enabling some of these options may approximate
0170 better the style there.
0171
0172 For instance:
0173
0174 - Aligning assignments (``AlignConsecutiveAssignments``).
0175
0176 - Aligning declarations (``AlignConsecutiveDeclarations``).
0177
0178 - Reflowing text in comments (``ReflowComments``).
0179
0180 - Sorting ``#includes`` (``SortIncludes``).
0181
0182 They are typically useful for block re-formatting, rather than full-file.
0183 You might want to create another ``.clang-format`` file and use that one
0184 from your editor/IDE instead.