xref: /openbmc/linux/Documentation/process/clang-format.rst (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
1d4ef8d3fSMiguel Ojeda.. _clangformat:
2d4ef8d3fSMiguel Ojeda
3d4ef8d3fSMiguel Ojedaclang-format
4d4ef8d3fSMiguel Ojeda============
5d4ef8d3fSMiguel Ojeda
6d4ef8d3fSMiguel Ojeda``clang-format`` is a tool to format C/C++/... code according to
7d4ef8d3fSMiguel Ojedaa set of rules and heuristics. Like most tools, it is not perfect
8d4ef8d3fSMiguel Ojedanor covers every single case, but it is good enough to be helpful.
9d4ef8d3fSMiguel Ojeda
10d4ef8d3fSMiguel Ojeda``clang-format`` can be used for several purposes:
11d4ef8d3fSMiguel Ojeda
12d4ef8d3fSMiguel Ojeda  - Quickly reformat a block of code to the kernel style. Specially useful
13d4ef8d3fSMiguel Ojeda    when moving code around and aligning/sorting. See clangformatreformat_.
14d4ef8d3fSMiguel Ojeda
15d4ef8d3fSMiguel Ojeda  - Spot style mistakes, typos and possible improvements in files
16d4ef8d3fSMiguel Ojeda    you maintain, patches you review, diffs, etc. See clangformatreview_.
17d4ef8d3fSMiguel Ojeda
18d4ef8d3fSMiguel Ojeda  - Help you follow the coding style rules, specially useful for those
19d4ef8d3fSMiguel Ojeda    new to kernel development or working at the same time in several
20d4ef8d3fSMiguel Ojeda    projects with different coding styles.
21d4ef8d3fSMiguel Ojeda
22d4ef8d3fSMiguel OjedaIts configuration file is ``.clang-format`` in the root of the kernel tree.
23d4ef8d3fSMiguel OjedaThe rules contained there try to approximate the most common kernel
24d4ef8d3fSMiguel Ojedacoding style. They also try to follow :ref:`Documentation/process/coding-style.rst <codingstyle>`
25d4ef8d3fSMiguel Ojedaas much as possible. Since not all the kernel follows the same style,
26d4ef8d3fSMiguel Ojedait is possible that you may want to tweak the defaults for a particular
27d4ef8d3fSMiguel Ojedasubsystem or folder. To do so, you can override the defaults by writing
28d4ef8d3fSMiguel Ojedaanother ``.clang-format`` file in a subfolder.
29d4ef8d3fSMiguel Ojeda
30d4ef8d3fSMiguel OjedaThe tool itself has already been included in the repositories of popular
31d4ef8d3fSMiguel OjedaLinux distributions for a long time. Search for ``clang-format`` in
32d4ef8d3fSMiguel Ojedayour repositories. Otherwise, you can either download pre-built
33d4ef8d3fSMiguel OjedaLLVM/clang binaries or build the source code from:
34d4ef8d3fSMiguel Ojeda
35e7b4311eSAlexander A. Klimov    https://releases.llvm.org/download.html
36d4ef8d3fSMiguel Ojeda
37d4ef8d3fSMiguel OjedaSee more information about the tool at:
38d4ef8d3fSMiguel Ojeda
39d4ef8d3fSMiguel Ojeda    https://clang.llvm.org/docs/ClangFormat.html
40d4ef8d3fSMiguel Ojeda
41d4ef8d3fSMiguel Ojeda    https://clang.llvm.org/docs/ClangFormatStyleOptions.html
42d4ef8d3fSMiguel Ojeda
43d4ef8d3fSMiguel Ojeda
44d4ef8d3fSMiguel Ojeda.. _clangformatreview:
45d4ef8d3fSMiguel Ojeda
46d4ef8d3fSMiguel OjedaReview files and patches for coding style
47d4ef8d3fSMiguel Ojeda-----------------------------------------
48d4ef8d3fSMiguel Ojeda
49d4ef8d3fSMiguel OjedaBy running the tool in its inline mode, you can review full subsystems,
50d4ef8d3fSMiguel Ojedafolders or individual files for code style mistakes, typos or improvements.
51d4ef8d3fSMiguel Ojeda
52d4ef8d3fSMiguel OjedaTo do so, you can run something like::
53d4ef8d3fSMiguel Ojeda
54d4ef8d3fSMiguel Ojeda    # Make sure your working directory is clean!
55d4ef8d3fSMiguel Ojeda    clang-format -i kernel/*.[ch]
56d4ef8d3fSMiguel Ojeda
57d4ef8d3fSMiguel OjedaAnd then take a look at the git diff.
58d4ef8d3fSMiguel Ojeda
59d4ef8d3fSMiguel OjedaCounting the lines of such a diff is also useful for improving/tweaking
60d4ef8d3fSMiguel Ojedathe style options in the configuration file; as well as testing new
61d4ef8d3fSMiguel Ojeda``clang-format`` features/versions.
62d4ef8d3fSMiguel Ojeda
63d4ef8d3fSMiguel Ojeda``clang-format`` also supports reading unified diffs, so you can review
64d4ef8d3fSMiguel Ojedapatches and git diffs easily. See the documentation at:
65d4ef8d3fSMiguel Ojeda
66d4ef8d3fSMiguel Ojeda    https://clang.llvm.org/docs/ClangFormat.html#script-for-patch-reformatting
67d4ef8d3fSMiguel Ojeda
68d4ef8d3fSMiguel OjedaTo avoid ``clang-format`` formatting some portion of a file, you can do::
69d4ef8d3fSMiguel Ojeda
70d4ef8d3fSMiguel Ojeda    int formatted_code;
71d4ef8d3fSMiguel Ojeda    // clang-format off
72d4ef8d3fSMiguel Ojeda        void    unformatted_code  ;
73d4ef8d3fSMiguel Ojeda    // clang-format on
74d4ef8d3fSMiguel Ojeda    void formatted_code_again;
75d4ef8d3fSMiguel Ojeda
76d4ef8d3fSMiguel OjedaWhile it might be tempting to use this to keep a file always in sync with
77d4ef8d3fSMiguel Ojeda``clang-format``, specially if you are writing new files or if you are
78d4ef8d3fSMiguel Ojedaa maintainer, please note that people might be running different
79d4ef8d3fSMiguel Ojeda``clang-format`` versions or not have it available at all. Therefore,
80d4ef8d3fSMiguel Ojedayou should probably refrain yourself from using this in kernel sources;
81d4ef8d3fSMiguel Ojedaat least until we see if ``clang-format`` becomes commonplace.
82d4ef8d3fSMiguel Ojeda
83d4ef8d3fSMiguel Ojeda
84d4ef8d3fSMiguel Ojeda.. _clangformatreformat:
85d4ef8d3fSMiguel Ojeda
86d4ef8d3fSMiguel OjedaReformatting blocks of code
87d4ef8d3fSMiguel Ojeda---------------------------
88d4ef8d3fSMiguel Ojeda
89d4ef8d3fSMiguel OjedaBy using an integration with your text editor, you can reformat arbitrary
90d4ef8d3fSMiguel Ojedablocks (selections) of code with a single keystroke. This is specially
91d4ef8d3fSMiguel Ojedauseful when moving code around, for complex code that is deeply intended,
92d4ef8d3fSMiguel Ojedafor multi-line macros (and aligning their backslashes), etc.
93d4ef8d3fSMiguel Ojeda
94d4ef8d3fSMiguel OjedaRemember that you can always tweak the changes afterwards in those cases
95d4ef8d3fSMiguel Ojedawhere the tool did not do an optimal job. But as a first approximation,
96d4ef8d3fSMiguel Ojedait can be very useful.
97d4ef8d3fSMiguel Ojeda
98d4ef8d3fSMiguel OjedaThere are integrations for many popular text editors. For some of them,
99d4ef8d3fSMiguel Ojedalike vim, emacs, BBEdit and Visual Studio you can find support built-in.
100*e0a45cdaSAndrew KlychkovFor instructions, read the appropriate section at:
101d4ef8d3fSMiguel Ojeda
102d4ef8d3fSMiguel Ojeda    https://clang.llvm.org/docs/ClangFormat.html
103d4ef8d3fSMiguel Ojeda
104d4ef8d3fSMiguel OjedaFor Atom, Eclipse, Sublime Text, Visual Studio Code, XCode and other
105d4ef8d3fSMiguel Ojedaeditors and IDEs you should be able to find ready-to-use plugins.
106d4ef8d3fSMiguel Ojeda
107d4ef8d3fSMiguel OjedaFor this use case, consider using a secondary ``.clang-format``
108d4ef8d3fSMiguel Ojedaso that you can tweak a few options. See clangformatextra_.
109d4ef8d3fSMiguel Ojeda
110d4ef8d3fSMiguel Ojeda
111d4ef8d3fSMiguel Ojeda.. _clangformatmissing:
112d4ef8d3fSMiguel Ojeda
113d4ef8d3fSMiguel OjedaMissing support
114d4ef8d3fSMiguel Ojeda---------------
115d4ef8d3fSMiguel Ojeda
116d4ef8d3fSMiguel Ojeda``clang-format`` is missing support for some things that are common
117d4ef8d3fSMiguel Ojedain kernel code. They are easy to remember, so if you use the tool
118d4ef8d3fSMiguel Ojedaregularly, you will quickly learn to avoid/ignore those.
119d4ef8d3fSMiguel Ojeda
120d4ef8d3fSMiguel OjedaIn particular, some very common ones you will notice are:
121d4ef8d3fSMiguel Ojeda
122d4ef8d3fSMiguel Ojeda  - Aligned blocks of one-line ``#defines``, e.g.::
123d4ef8d3fSMiguel Ojeda
124d4ef8d3fSMiguel Ojeda        #define TRACING_MAP_BITS_DEFAULT       11
125d4ef8d3fSMiguel Ojeda        #define TRACING_MAP_BITS_MAX           17
126d4ef8d3fSMiguel Ojeda        #define TRACING_MAP_BITS_MIN           7
127d4ef8d3fSMiguel Ojeda
128d4ef8d3fSMiguel Ojeda    vs.::
129d4ef8d3fSMiguel Ojeda
130d4ef8d3fSMiguel Ojeda        #define TRACING_MAP_BITS_DEFAULT 11
131d4ef8d3fSMiguel Ojeda        #define TRACING_MAP_BITS_MAX 17
132d4ef8d3fSMiguel Ojeda        #define TRACING_MAP_BITS_MIN 7
133d4ef8d3fSMiguel Ojeda
134d4ef8d3fSMiguel Ojeda  - Aligned designated initializers, e.g.::
135d4ef8d3fSMiguel Ojeda
136d4ef8d3fSMiguel Ojeda        static const struct file_operations uprobe_events_ops = {
137d4ef8d3fSMiguel Ojeda                .owner          = THIS_MODULE,
138d4ef8d3fSMiguel Ojeda                .open           = probes_open,
139d4ef8d3fSMiguel Ojeda                .read           = seq_read,
140d4ef8d3fSMiguel Ojeda                .llseek         = seq_lseek,
141d4ef8d3fSMiguel Ojeda                .release        = seq_release,
142d4ef8d3fSMiguel Ojeda                .write          = probes_write,
143d4ef8d3fSMiguel Ojeda        };
144d4ef8d3fSMiguel Ojeda
145d4ef8d3fSMiguel Ojeda    vs.::
146d4ef8d3fSMiguel Ojeda
147d4ef8d3fSMiguel Ojeda        static const struct file_operations uprobe_events_ops = {
148d4ef8d3fSMiguel Ojeda                .owner = THIS_MODULE,
149d4ef8d3fSMiguel Ojeda                .open = probes_open,
150d4ef8d3fSMiguel Ojeda                .read = seq_read,
151d4ef8d3fSMiguel Ojeda                .llseek = seq_lseek,
152d4ef8d3fSMiguel Ojeda                .release = seq_release,
153d4ef8d3fSMiguel Ojeda                .write = probes_write,
154d4ef8d3fSMiguel Ojeda        };
155d4ef8d3fSMiguel Ojeda
156d4ef8d3fSMiguel Ojeda
157d4ef8d3fSMiguel Ojeda.. _clangformatextra:
158d4ef8d3fSMiguel Ojeda
159d4ef8d3fSMiguel OjedaExtra features/options
160d4ef8d3fSMiguel Ojeda----------------------
161d4ef8d3fSMiguel Ojeda
162d4ef8d3fSMiguel OjedaSome features/style options are not enabled by default in the configuration
163d4ef8d3fSMiguel Ojedafile in order to minimize the differences between the output and the current
164d4ef8d3fSMiguel Ojedacode. In other words, to make the difference as small as possible,
165d4ef8d3fSMiguel Ojedawhich makes reviewing full-file style, as well diffs and patches as easy
166d4ef8d3fSMiguel Ojedaas possible.
167d4ef8d3fSMiguel Ojeda
168d4ef8d3fSMiguel OjedaIn other cases (e.g. particular subsystems/folders/files), the kernel style
169d4ef8d3fSMiguel Ojedamight be different and enabling some of these options may approximate
170d4ef8d3fSMiguel Ojedabetter the style there.
171d4ef8d3fSMiguel Ojeda
172d4ef8d3fSMiguel OjedaFor instance:
173d4ef8d3fSMiguel Ojeda
174d4ef8d3fSMiguel Ojeda  - Aligning assignments (``AlignConsecutiveAssignments``).
175d4ef8d3fSMiguel Ojeda
176d4ef8d3fSMiguel Ojeda  - Aligning declarations (``AlignConsecutiveDeclarations``).
177d4ef8d3fSMiguel Ojeda
178d4ef8d3fSMiguel Ojeda  - Reflowing text in comments (``ReflowComments``).
179d4ef8d3fSMiguel Ojeda
180d4ef8d3fSMiguel Ojeda  - Sorting ``#includes`` (``SortIncludes``).
181d4ef8d3fSMiguel Ojeda
182d4ef8d3fSMiguel OjedaThey are typically useful for block re-formatting, rather than full-file.
183d4ef8d3fSMiguel OjedaYou might want to create another ``.clang-format`` file and use that one
184d4ef8d3fSMiguel Ojedafrom your editor/IDE instead.
185