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