blob: 0ce4eb7f15bb584061a3fa2f0367f36ad6f1e872 [file] [log] [blame]
Alexander Kornienko62d06b72013-09-04 15:09:13 +00001==========================
2Clang-Format Style Options
3==========================
4
5:doc:`ClangFormatStyleOptions` describes configurable formatting style options
6supported by :doc:`LibFormat` and :doc:`ClangFormat`.
7
8When using :program:`clang-format` command line utility or
9``clang::format::reformat(...)`` functions from code, one can either use one of
10the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit) or create a
11custom style by configuring specific style options.
12
13
14Configuring Style with clang-format
15===================================
16
17:program:`clang-format` supports two ways to provide custom style options:
18directly specify style configuration in the ``-style=`` command line option or
Hans Wennborg9a7a50e2013-09-10 15:41:12 +000019use ``-style=file`` and put style configuration in the ``.clang-format`` or
20``_clang-format`` file in the project directory.
Alexander Kornienko62d06b72013-09-04 15:09:13 +000021
22When using ``-style=file``, :program:`clang-format` for each input file will
23try to find the ``.clang-format`` file located in the closest parent directory
24of the input file. When the standard input is used, the search is started from
25the current directory.
26
27The ``.clang-format`` file uses YAML format:
28
29.. code-block:: yaml
30
31 key1: value1
32 key2: value2
33 # A comment.
34 ...
35
36An easy way to get a valid ``.clang-format`` file containing all configuration
37options of a certain predefined style is:
38
39.. code-block:: console
40
41 clang-format -style=llvm -dump-config > .clang-format
42
43When specifying configuration in the ``-style=`` option, the same configuration
44is applied for all input files. The format of the configuration is:
45
46.. code-block:: console
47
48 -style='{key1: value1, key2: value2, ...}'
49
50
51Configuring Style in Code
52=========================
53
54When using ``clang::format::reformat(...)`` functions, the format is specified
55by supplying the `clang::format::FormatStyle
56<http://clang.llvm.org/doxygen/structclang_1_1format_1_1FormatStyle.html>`_
57structure.
58
59
60Configurable Format Style Options
61=================================
62
63This section lists the supported style options. Value type is specified for
64each option. For enumeration types possible values are specified both as a C++
Alexander Kornienkoc4f73e02013-09-04 15:14:18 +000065enumeration member (with a prefix, e.g. ``LS_Auto``), and as a value usable in
66the configuration (without a prefix: ``Auto``).
Alexander Kornienko62d06b72013-09-04 15:09:13 +000067
68
69**BasedOnStyle** (``string``)
70 The style used for all options not specifically set in the configuration.
71
72 This option is supported only in the :program:`clang-format` configuration
73 (both within ``-style='{...}'`` and the ``.clang-format`` file).
74
75 Possible values:
76
77 * ``LLVM``
78 A style complying with the `LLVM coding standards
79 <http://llvm.org/docs/CodingStandards.html>`_
80 * ``Google``
81 A style complying with `Google's C++ style guide
82 <http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml>`_
83 * ``Chromium``
84 A style complying with `Chromium's style guide
85 <http://www.chromium.org/developers/coding-style>`_
86 * ``Mozilla``
87 A style complying with `Mozilla's style guide
88 <https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style>`_
89 * ``WebKit``
90 A style complying with `WebKit's style guide
91 <http://www.webkit.org/coding/coding-style.html>`_
92
93.. START_FORMAT_STYLE_OPTIONS
94
95**AccessModifierOffset** (``int``)
96 The extra indent or outdent of access modifiers, e.g. ``public:``.
97
98**AlignEscapedNewlinesLeft** (``bool``)
99 If ``true``, aligns escaped newlines as far left as possible.
100 Otherwise puts them into the right-most column.
101
102**AlignTrailingComments** (``bool``)
103 If ``true``, aligns trailing comments.
104
105**AllowAllParametersOfDeclarationOnNextLine** (``bool``)
106 Allow putting all parameters of a function declaration onto
107 the next line even if ``BinPackParameters`` is ``false``.
108
109**AllowShortIfStatementsOnASingleLine** (``bool``)
110 If ``true``, ``if (a) return;`` can be put on a single
111 line.
112
113**AllowShortLoopsOnASingleLine** (``bool``)
114 If ``true``, ``while (true) continue;`` can be put on a
115 single line.
116
117**AlwaysBreakBeforeMultilineStrings** (``bool``)
118 If ``true``, always break before multiline string literals.
119
120**AlwaysBreakTemplateDeclarations** (``bool``)
121 If ``true``, always break after the ``template<...>`` of a
122 template declaration.
123
124**BinPackParameters** (``bool``)
125 If ``false``, a function call's or function definition's parameters
126 will either all be on the same line or will have one line each.
127
128**BreakBeforeBinaryOperators** (``bool``)
129 If ``true``, binary operators will be placed after line breaks.
130
131**BreakBeforeBraces** (``BraceBreakingStyle``)
132 The brace breaking style to use.
133
134 Possible values:
135
136 * ``BS_Attach`` (in configuration: ``Attach``)
137 Always attach braces to surrounding context.
138 * ``BS_Linux`` (in configuration: ``Linux``)
139 Like ``Attach``, but break before braces on function, namespace and
140 class definitions.
141 * ``BS_Stroustrup`` (in configuration: ``Stroustrup``)
142 Like ``Attach``, but break before function definitions.
143 * ``BS_Allman`` (in configuration: ``Allman``)
144 Always break before braces.
145
146
147**BreakConstructorInitializersBeforeComma** (``bool``)
148 Always break constructor initializers before commas and align
149 the commas with the colon.
150
151**ColumnLimit** (``unsigned``)
152 The column limit.
153
154 A column limit of ``0`` means that there is no column limit. In this case,
155 clang-format will respect the input's line breaking decisions within
156 statements.
157
158**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``)
159 If the constructor initializers don't fit on a line, put each
160 initializer on its own line.
161
162**ConstructorInitializerIndentWidth** (``unsigned``)
163 The number of characters to use for indentation of constructor
164 initializer lists.
165
166**Cpp11BracedListStyle** (``bool``)
167 If ``true``, format braced lists as best suited for C++11 braced
168 lists.
169
170 Important differences:
171 - No spaces inside the braced list.
172 - No line break before the closing brace.
173 - Indentation with the continuation indent, not with the block indent.
174
175 Fundamentally, C++11 braced lists are formatted exactly like function
176 calls would be formatted in their place. If the braced list follows a name
177 (e.g. a type or variable name), clang-format formats as if the ``{}`` were
178 the parentheses of a function call with that name. If there is no name,
179 a zero-length name is assumed.
180
181**DerivePointerBinding** (``bool``)
182 If ``true``, analyze the formatted file for the most common binding.
183
184**ExperimentalAutoDetectBinPacking** (``bool``)
185 If ``true``, clang-format detects whether function calls and
186 definitions are formatted with one parameter per line.
187
188 Each call can be bin-packed, one-per-line or inconclusive. If it is
189 inconclusive, e.g. completely on one line, but a decision needs to be
190 made, clang-format analyzes whether there are other bin-packed cases in
191 the input file and act accordingly.
192
193 NOTE: This is an experimental flag, that might go away or be renamed. Do
194 not use this in config files, etc. Use at your own risk.
195
196**IndentCaseLabels** (``bool``)
197 Indent case labels one level from the switch statement.
198
199 When ``false``, use the same indentation level as for the switch statement.
200 Switch statement body is always indented one level more than case labels.
201
202**IndentFunctionDeclarationAfterType** (``bool``)
203 If ``true``, indent when breaking function declarations which
204 are not also definitions after the type.
205
206**IndentWidth** (``unsigned``)
207 The number of characters to use for indentation.
208
209**MaxEmptyLinesToKeep** (``unsigned``)
210 The maximum number of consecutive empty lines to keep.
211
212**NamespaceIndentation** (``NamespaceIndentationKind``)
213 The indentation used for namespaces.
214
215 Possible values:
216
217 * ``NI_None`` (in configuration: ``None``)
218 Don't indent in namespaces.
219 * ``NI_Inner`` (in configuration: ``Inner``)
220 Indent only in inner namespaces (nested in other namespaces).
221 * ``NI_All`` (in configuration: ``All``)
222 Indent in all namespaces.
223
224
225**ObjCSpaceBeforeProtocolList** (``bool``)
226 Add a space in front of an Objective-C protocol list, i.e. use
227 ``Foo <Protocol>`` instead of ``Foo<Protocol>``.
228
229**PenaltyBreakComment** (``unsigned``)
230 The penalty for each line break introduced inside a comment.
231
232**PenaltyBreakFirstLessLess** (``unsigned``)
233 The penalty for breaking before the first ``<<``.
234
235**PenaltyBreakString** (``unsigned``)
236 The penalty for each line break introduced inside a string literal.
237
238**PenaltyExcessCharacter** (``unsigned``)
239 The penalty for each character outside of the column limit.
240
241**PenaltyReturnTypeOnItsOwnLine** (``unsigned``)
242 Penalty for putting the return type of a function onto its own
243 line.
244
245**PointerBindsToType** (``bool``)
246 Set whether & and * bind to the type as opposed to the variable.
247
248**SpaceAfterControlStatementKeyword** (``bool``)
249 If ``true``, spaces will be inserted between 'for'/'if'/'while'/...
250 and '('.
251
Daniel Jasper9b4de852013-09-25 15:15:02 +0000252**SpaceBeforeAssignmentOperators** (``bool``)
253 If ``false``, spaces will be removed before '=', '+=', etc.
254
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000255**SpaceInEmptyParentheses** (``bool``)
256 If ``false``, spaces may be inserted into '()'.
257
258**SpacesBeforeTrailingComments** (``unsigned``)
259 The number of spaces to before trailing line comments.
260
261**SpacesInCStyleCastParentheses** (``bool``)
262 If ``false``, spaces may be inserted into C style casts.
263
264**SpacesInParentheses** (``bool``)
265 If ``true``, spaces will be inserted after every '(' and before
266 every ')'.
267
268**Standard** (``LanguageStandard``)
269 Format compatible with this standard, e.g. use
270 ``A<A<int> >`` instead of ``A<A<int>>`` for LS_Cpp03.
271
272 Possible values:
273
274 * ``LS_Cpp03`` (in configuration: ``Cpp03``)
275 Use C++03-compatible syntax.
276 * ``LS_Cpp11`` (in configuration: ``Cpp11``)
277 Use features of C++11 (e.g. ``A<A<int>>`` instead of
278 ``A<A<int> >``).
279 * ``LS_Auto`` (in configuration: ``Auto``)
280 Automatic detection based on the input.
281
282
283**UseTab** (``bool``)
284 If ``true``, ``IndentWidth`` consecutive spaces will be replaced
285 with tab characters.
286
287.. END_FORMAT_STYLE_OPTIONS
288
289Examples
290========
291
292A style similar to the `Linux Kernel style
293<https://www.kernel.org/doc/Documentation/CodingStyle>`_:
294
295.. code-block:: yaml
296
297 BasedOnStyle: LLVM
298 IndentWidth: 8
299 UseTab: true
300 BreakBeforeBraces: Linux
301 AllowShortIfStatementsOnASingleLine: false
302 IndentCaseLabels: false
303
304The result is (imagine that tabs are used for indentation here):
305
306.. code-block:: c++
307
308 void test()
309 {
310 switch (x) {
311 case 0:
312 case 1:
313 do_something();
314 break;
315 case 2:
316 do_something_else();
317 break;
318 default:
319 break;
320 }
321 if (condition)
322 do_something_completely_different();
323
324 if (x == y) {
325 q();
326 } else if (x > y) {
327 w();
328 } else {
329 r();
330 }
331 }
332
333A style similar to the default Visual Studio formatting style:
334
335.. code-block:: yaml
336
337 UseTab: false
338 IndentWidth: 4
339 BreakBeforeBraces: Allman
340 AllowShortIfStatementsOnASingleLine: false
341 IndentCaseLabels: false
342 ColumnLimit: 0
343
344The result is:
345
346.. code-block:: c++
347
348 void test()
349 {
350 switch (suffix)
351 {
352 case 0:
353 case 1:
354 do_something();
355 break;
356 case 2:
357 do_something_else();
358 break;
359 default:
360 break;
361 }
362 if (condition)
363 do_somthing_completely_different();
364
365 if (x == y)
366 {
367 q();
368 }
369 else if (x > y)
370 {
371 w();
372 }
373 else
374 {
375 r();
376 }
377 }
378