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