blob: 0483bd715eb2e53318905bfeb3302db279f4a04b [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
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700109**AllowShortBlocksOnASingleLine** (``bool``)
110 Allows contracting simple braced statements to a single line.
111
112 E.g., this allows ``if (a) { return; }`` to be put on a single line.
113
114**AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``)
115 Dependent on the value, ``int f() { return 0; }`` can be put
116 on a single line.
117
118 Possible values:
119
120 * ``SFS_None`` (in configuration: ``None``)
121 Never merge functions into a single line.
122 * ``SFS_Inline`` (in configuration: ``Inline``)
123 Only merge functions defined inside a class.
124 * ``SFS_All`` (in configuration: ``All``)
125 Merge all functions fitting on a single line.
126
Stephen Hines651f13c2014-04-23 16:59:28 -0700127
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000128**AllowShortIfStatementsOnASingleLine** (``bool``)
129 If ``true``, ``if (a) return;`` can be put on a single
130 line.
131
132**AllowShortLoopsOnASingleLine** (``bool``)
133 If ``true``, ``while (true) continue;`` can be put on a
134 single line.
135
136**AlwaysBreakBeforeMultilineStrings** (``bool``)
137 If ``true``, always break before multiline string literals.
138
139**AlwaysBreakTemplateDeclarations** (``bool``)
140 If ``true``, always break after the ``template<...>`` of a
141 template declaration.
142
143**BinPackParameters** (``bool``)
144 If ``false``, a function call's or function definition's parameters
145 will either all be on the same line or will have one line each.
146
147**BreakBeforeBinaryOperators** (``bool``)
148 If ``true``, binary operators will be placed after line breaks.
149
150**BreakBeforeBraces** (``BraceBreakingStyle``)
151 The brace breaking style to use.
152
153 Possible values:
154
155 * ``BS_Attach`` (in configuration: ``Attach``)
156 Always attach braces to surrounding context.
157 * ``BS_Linux`` (in configuration: ``Linux``)
158 Like ``Attach``, but break before braces on function, namespace and
159 class definitions.
160 * ``BS_Stroustrup`` (in configuration: ``Stroustrup``)
161 Like ``Attach``, but break before function definitions.
162 * ``BS_Allman`` (in configuration: ``Allman``)
163 Always break before braces.
Stephen Hines651f13c2014-04-23 16:59:28 -0700164 * ``BS_GNU`` (in configuration: ``GNU``)
165 Always break before braces and add an extra level of indentation to
166 braces of control statements, not to those of class, function
167 or other definitions.
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000168
169
Stephen Hines651f13c2014-04-23 16:59:28 -0700170**BreakBeforeTernaryOperators** (``bool``)
171 If ``true``, ternary operators will be placed after line breaks.
172
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000173**BreakConstructorInitializersBeforeComma** (``bool``)
174 Always break constructor initializers before commas and align
175 the commas with the colon.
176
177**ColumnLimit** (``unsigned``)
178 The column limit.
179
180 A column limit of ``0`` means that there is no column limit. In this case,
181 clang-format will respect the input's line breaking decisions within
Stephen Hines651f13c2014-04-23 16:59:28 -0700182 statements unless they contradict other rules.
183
184**CommentPragmas** (``std::string``)
185 A regular expression that describes comments with special meaning,
186 which should not be split into lines or otherwise changed.
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000187
188**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``)
189 If the constructor initializers don't fit on a line, put each
190 initializer on its own line.
191
192**ConstructorInitializerIndentWidth** (``unsigned``)
193 The number of characters to use for indentation of constructor
194 initializer lists.
195
Stephen Hines651f13c2014-04-23 16:59:28 -0700196**ContinuationIndentWidth** (``unsigned``)
197 Indent width for line continuations.
198
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000199**Cpp11BracedListStyle** (``bool``)
200 If ``true``, format braced lists as best suited for C++11 braced
201 lists.
202
203 Important differences:
204 - No spaces inside the braced list.
205 - No line break before the closing brace.
206 - Indentation with the continuation indent, not with the block indent.
207
208 Fundamentally, C++11 braced lists are formatted exactly like function
209 calls would be formatted in their place. If the braced list follows a name
210 (e.g. a type or variable name), clang-format formats as if the ``{}`` were
211 the parentheses of a function call with that name. If there is no name,
212 a zero-length name is assumed.
213
214**DerivePointerBinding** (``bool``)
Stephen Hines651f13c2014-04-23 16:59:28 -0700215 If ``true``, analyze the formatted file for the most common binding
216 and use ``PointerBindsToType`` only as fallback.
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000217
218**ExperimentalAutoDetectBinPacking** (``bool``)
219 If ``true``, clang-format detects whether function calls and
220 definitions are formatted with one parameter per line.
221
222 Each call can be bin-packed, one-per-line or inconclusive. If it is
223 inconclusive, e.g. completely on one line, but a decision needs to be
224 made, clang-format analyzes whether there are other bin-packed cases in
225 the input file and act accordingly.
226
227 NOTE: This is an experimental flag, that might go away or be renamed. Do
228 not use this in config files, etc. Use at your own risk.
229
Stephen Hines651f13c2014-04-23 16:59:28 -0700230**ForEachMacros** (``std::vector<std::string>``)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700231 A vector of macros that should be interpreted as foreach loops
232 instead of as function calls.
Stephen Hines651f13c2014-04-23 16:59:28 -0700233
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700234 These are expected to be macros of the form:
235 \code
236 FOREACH(<variable-declaration>, ...)
237 <loop-body>
238 \endcode
239
240 For example: BOOST_FOREACH.
Stephen Hines651f13c2014-04-23 16:59:28 -0700241
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000242**IndentCaseLabels** (``bool``)
243 Indent case labels one level from the switch statement.
244
245 When ``false``, use the same indentation level as for the switch statement.
246 Switch statement body is always indented one level more than case labels.
247
248**IndentFunctionDeclarationAfterType** (``bool``)
249 If ``true``, indent when breaking function declarations which
250 are not also definitions after the type.
251
252**IndentWidth** (``unsigned``)
Alexander Kornienkof6a68822013-09-27 16:16:55 +0000253 The number of columns to use for indentation.
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000254
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700255**KeepEmptyLinesAtTheStartOfBlocks** (``bool``)
256 If true, empty lines at the start of blocks are kept.
257
Stephen Hines651f13c2014-04-23 16:59:28 -0700258**Language** (``LanguageKind``)
259 Language, this format style is targeted at.
260
261 Possible values:
262
263 * ``LK_None`` (in configuration: ``None``)
264 Do not use.
265 * ``LK_Cpp`` (in configuration: ``Cpp``)
266 Should be used for C, C++, ObjectiveC, ObjectiveC++.
267 * ``LK_JavaScript`` (in configuration: ``JavaScript``)
268 Should be used for JavaScript.
269 * ``LK_Proto`` (in configuration: ``Proto``)
270 Should be used for Protocol Buffers
271 (https://developers.google.com/protocol-buffers/).
272
273
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000274**MaxEmptyLinesToKeep** (``unsigned``)
275 The maximum number of consecutive empty lines to keep.
276
277**NamespaceIndentation** (``NamespaceIndentationKind``)
278 The indentation used for namespaces.
279
280 Possible values:
281
282 * ``NI_None`` (in configuration: ``None``)
283 Don't indent in namespaces.
284 * ``NI_Inner`` (in configuration: ``Inner``)
285 Indent only in inner namespaces (nested in other namespaces).
286 * ``NI_All`` (in configuration: ``All``)
287 Indent in all namespaces.
288
289
Stephen Hines651f13c2014-04-23 16:59:28 -0700290**ObjCSpaceAfterProperty** (``bool``)
291 Add a space after ``@property`` in Objective-C, i.e. use
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700292 ``\@property (readonly)`` instead of ``\@property(readonly)``.
Stephen Hines651f13c2014-04-23 16:59:28 -0700293
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000294**ObjCSpaceBeforeProtocolList** (``bool``)
295 Add a space in front of an Objective-C protocol list, i.e. use
296 ``Foo <Protocol>`` instead of ``Foo<Protocol>``.
297
Stephen Hines651f13c2014-04-23 16:59:28 -0700298**PenaltyBreakBeforeFirstCallParameter** (``unsigned``)
299 The penalty for breaking a function call after "call(".
300
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000301**PenaltyBreakComment** (``unsigned``)
302 The penalty for each line break introduced inside a comment.
303
304**PenaltyBreakFirstLessLess** (``unsigned``)
305 The penalty for breaking before the first ``<<``.
306
307**PenaltyBreakString** (``unsigned``)
308 The penalty for each line break introduced inside a string literal.
309
310**PenaltyExcessCharacter** (``unsigned``)
311 The penalty for each character outside of the column limit.
312
313**PenaltyReturnTypeOnItsOwnLine** (``unsigned``)
314 Penalty for putting the return type of a function onto its own
315 line.
316
317**PointerBindsToType** (``bool``)
318 Set whether & and * bind to the type as opposed to the variable.
319
Daniel Jasper9b4de852013-09-25 15:15:02 +0000320**SpaceBeforeAssignmentOperators** (``bool``)
Alexander Kornienkof6a68822013-09-27 16:16:55 +0000321 If ``false``, spaces will be removed before assignment operators.
Daniel Jasper9b4de852013-09-25 15:15:02 +0000322
Stephen Hines651f13c2014-04-23 16:59:28 -0700323**SpaceBeforeParens** (``SpaceBeforeParensOptions``)
324 Defines in which cases to put a space before opening parentheses.
325
326 Possible values:
327
328 * ``SBPO_Never`` (in configuration: ``Never``)
329 Never put a space before opening parentheses.
330 * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``)
331 Put a space before opening parentheses only after control statement
332 keywords (``for/if/while...``).
333 * ``SBPO_Always`` (in configuration: ``Always``)
334 Always put a space before opening parentheses, except when it's
335 prohibited by the syntax rules (in function-like macro definitions) or
336 when determined by other style rules (after unary operators, opening
337 parentheses, etc.)
338
339
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000340**SpaceInEmptyParentheses** (``bool``)
Stephen Hines651f13c2014-04-23 16:59:28 -0700341 If ``true``, spaces may be inserted into '()'.
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000342
343**SpacesBeforeTrailingComments** (``unsigned``)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700344 The number of spaces before trailing line comments
345 (``//`` - comments).
346
347 This does not affect trailing block comments (``/**/`` - comments) as those
348 commonly have different usage patterns and a number of special cases.
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000349
Stephen Hines651f13c2014-04-23 16:59:28 -0700350**SpacesInAngles** (``bool``)
351 If ``true``, spaces will be inserted after '<' and before '>' in
352 template argument lists
353
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000354**SpacesInCStyleCastParentheses** (``bool``)
Stephen Hines651f13c2014-04-23 16:59:28 -0700355 If ``true``, spaces may be inserted into C style casts.
356
357**SpacesInContainerLiterals** (``bool``)
358 If ``true``, spaces are inserted inside container literals (e.g.
359 ObjC and Javascript array and dict literals).
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000360
361**SpacesInParentheses** (``bool``)
Stephen Hines651f13c2014-04-23 16:59:28 -0700362 If ``true``, spaces will be inserted after '(' and before ')'.
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000363
364**Standard** (``LanguageStandard``)
365 Format compatible with this standard, e.g. use
366 ``A<A<int> >`` instead of ``A<A<int>>`` for LS_Cpp03.
367
368 Possible values:
369
370 * ``LS_Cpp03`` (in configuration: ``Cpp03``)
371 Use C++03-compatible syntax.
372 * ``LS_Cpp11`` (in configuration: ``Cpp11``)
373 Use features of C++11 (e.g. ``A<A<int>>`` instead of
374 ``A<A<int> >``).
375 * ``LS_Auto`` (in configuration: ``Auto``)
376 Automatic detection based on the input.
377
378
Alexander Kornienkof6a68822013-09-27 16:16:55 +0000379**TabWidth** (``unsigned``)
380 The number of columns used for tab stops.
381
382**UseTab** (``UseTabStyle``)
383 The way to use tab characters in the resulting file.
384
385 Possible values:
386
387 * ``UT_Never`` (in configuration: ``Never``)
388 Never use tab.
389 * ``UT_ForIndentation`` (in configuration: ``ForIndentation``)
390 Use tabs only for indentation.
391 * ``UT_Always`` (in configuration: ``Always``)
392 Use tabs whenever we need to fill whitespace that spans at least from
393 one tab stop to the next one.
394
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000395
396.. END_FORMAT_STYLE_OPTIONS
397
398Examples
399========
400
401A style similar to the `Linux Kernel style
402<https://www.kernel.org/doc/Documentation/CodingStyle>`_:
403
404.. code-block:: yaml
405
406 BasedOnStyle: LLVM
407 IndentWidth: 8
Alexander Kornienko62a55652013-09-27 16:19:25 +0000408 UseTab: Always
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000409 BreakBeforeBraces: Linux
410 AllowShortIfStatementsOnASingleLine: false
411 IndentCaseLabels: false
412
413The result is (imagine that tabs are used for indentation here):
414
415.. code-block:: c++
416
417 void test()
418 {
419 switch (x) {
420 case 0:
421 case 1:
422 do_something();
423 break;
424 case 2:
425 do_something_else();
426 break;
427 default:
428 break;
429 }
430 if (condition)
431 do_something_completely_different();
432
433 if (x == y) {
434 q();
435 } else if (x > y) {
436 w();
437 } else {
438 r();
439 }
440 }
441
442A style similar to the default Visual Studio formatting style:
443
444.. code-block:: yaml
445
Alexander Kornienko62a55652013-09-27 16:19:25 +0000446 UseTab: Never
Alexander Kornienko62d06b72013-09-04 15:09:13 +0000447 IndentWidth: 4
448 BreakBeforeBraces: Allman
449 AllowShortIfStatementsOnASingleLine: false
450 IndentCaseLabels: false
451 ColumnLimit: 0
452
453The result is:
454
455.. code-block:: c++
456
457 void test()
458 {
459 switch (suffix)
460 {
461 case 0:
462 case 1:
463 do_something();
464 break;
465 case 2:
466 do_something_else();
467 break;
468 default:
469 break;
470 }
471 if (condition)
472 do_somthing_completely_different();
473
474 if (x == y)
475 {
476 q();
477 }
478 else if (x > y)
479 {
480 w();
481 }
482 else
483 {
484 r();
485 }
486 }
487