blob: f713e3a6d2c51684c1d551c8e6455fb535a6baec [file] [log] [blame]
Alexander Kornienkod278e0e2013-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 Wennborg9f6581b2013-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 Kornienkod278e0e2013-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
Alexander Kornienko092cb212014-08-12 13:34:22 +000036The configuration file can consist of several sections each having different
37``Language:`` parameter denoting the programming language this section of the
38configuration is targeted at. See the description of the **Language** option
39below for the list of supported languages. The first section may have no
40language set, it will set the default style options for all lanugages.
41Configuration sections for specific language will override options set in the
42default section.
43
44When :program:`clang-format` formats a file, it auto-detects the language using
45the file name. When formatting standard input or a file that doesn't have the
46extension corresponding to its language, ``-assume-filename=`` option can be
47used to override the file name :program:`clang-format` uses to detect the
48language.
49
50An example of a configuration file for multiple languages:
51
52.. code-block:: yaml
53
54 ---
55 # We'll use defaults from the LLVM style, but with 4 columns indentation.
56 BasedOnStyle: LLVM
57 IndentWidth: 4
58 ---
59 Language: Cpp
60 # Force pointers to the type for C++.
61 DerivePointerAlignment: false
62 PointerAlignment: Left
63 ---
64 Language: JavaScript
65 # Use 100 columns for JS.
66 ColumnLimit: 100
67 ---
68 Language: Proto
69 # Don't format .proto files.
70 DisableFormat: true
71 ...
72
Alexander Kornienkod278e0e2013-09-04 15:09:13 +000073An easy way to get a valid ``.clang-format`` file containing all configuration
74options of a certain predefined style is:
75
76.. code-block:: console
77
78 clang-format -style=llvm -dump-config > .clang-format
79
80When specifying configuration in the ``-style=`` option, the same configuration
81is applied for all input files. The format of the configuration is:
82
83.. code-block:: console
84
85 -style='{key1: value1, key2: value2, ...}'
86
87
Daniel Jasperd8b4ec02014-10-07 12:15:15 +000088Disabling Formatting on a Piece of Code
89=======================================
90
91Clang-format understands also special comments that switch formatting in a
92delimited range. The code between a comment ``// clang-format off`` or
93``/* clang-format off */`` up to a comment ``// clang-format on`` or
94``/* clang-format on */`` will not be formatted. The comments themselves
95will be formatted (aligned) normally.
96
97.. code-block:: c++
98
99 int formatted_code;
100 // clang-format off
101 void unformatted_code ;
102 // clang-format on
103 void formatted_code_again;
104
105
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000106Configuring Style in Code
107=========================
108
109When using ``clang::format::reformat(...)`` functions, the format is specified
110by supplying the `clang::format::FormatStyle
111<http://clang.llvm.org/doxygen/structclang_1_1format_1_1FormatStyle.html>`_
112structure.
113
114
115Configurable Format Style Options
116=================================
117
118This section lists the supported style options. Value type is specified for
119each option. For enumeration types possible values are specified both as a C++
Alexander Kornienko472d27a2013-09-04 15:14:18 +0000120enumeration member (with a prefix, e.g. ``LS_Auto``), and as a value usable in
121the configuration (without a prefix: ``Auto``).
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000122
123
124**BasedOnStyle** (``string``)
125 The style used for all options not specifically set in the configuration.
126
127 This option is supported only in the :program:`clang-format` configuration
128 (both within ``-style='{...}'`` and the ``.clang-format`` file).
129
130 Possible values:
131
132 * ``LLVM``
133 A style complying with the `LLVM coding standards
134 <http://llvm.org/docs/CodingStandards.html>`_
135 * ``Google``
136 A style complying with `Google's C++ style guide
137 <http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml>`_
138 * ``Chromium``
139 A style complying with `Chromium's style guide
140 <http://www.chromium.org/developers/coding-style>`_
141 * ``Mozilla``
142 A style complying with `Mozilla's style guide
143 <https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style>`_
144 * ``WebKit``
145 A style complying with `WebKit's style guide
146 <http://www.webkit.org/coding/coding-style.html>`_
147
148.. START_FORMAT_STYLE_OPTIONS
149
150**AccessModifierOffset** (``int``)
Alexander Kornienkoe99a3a32016-02-23 16:12:08 +0000151 The extra indent or outdent of access modifiers, e.g. ``public:``.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000152
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000153**AlignAfterOpenBracket** (``BracketAlignmentStyle``)
Daniel Jasper3219e432014-12-02 13:24:51 +0000154 If ``true``, horizontally aligns arguments after an open bracket.
155
156 This applies to round brackets (parentheses), angle brackets and square
Alexander Kornienko1e048232016-02-23 16:11:51 +0000157 brackets.
Daniel Jasperb5bda7c2015-10-06 12:11:51 +0000158
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000159 Possible values:
Daniel Jasper8e1ecca2015-10-07 13:02:45 +0000160
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000161 * ``BAS_Align`` (in configuration: ``Align``)
162 Align parameters on the open bracket, e.g.:
163
164 .. code-block:: c++
165
166 someLongFunction(argument1,
167 argument2);
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000168
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000169 * ``BAS_DontAlign`` (in configuration: ``DontAlign``)
170 Don't align, instead use ``ContinuationIndentWidth``, e.g.:
171
172 .. code-block:: c++
173
174 someLongFunction(argument1,
175 argument2);
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000176
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000177 * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``)
178 Always break after an open bracket, if the parameters don't fit
179 on a single line, e.g.:
180
181 .. code-block:: c++
182
183 someLongFunction(
184 argument1, argument2);
185
Daniel Jasper3219e432014-12-02 13:24:51 +0000186
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000187
Daniel Jaspera44991332015-04-29 13:06:49 +0000188**AlignConsecutiveAssignments** (``bool``)
189 If ``true``, aligns consecutive assignments.
190
191 This will align the assignment operators of consecutive lines. This
192 will result in formattings like
Daniel Jasperb5bda7c2015-10-06 12:11:51 +0000193
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000194 .. code-block:: c++
Daniel Jasper8e1ecca2015-10-07 13:02:45 +0000195
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000196 int aaaa = 12;
197 int b = 23;
198 int ccc = 23;
199
200**AlignConsecutiveDeclarations** (``bool``)
201 If ``true``, aligns consecutive declarations.
202
203 This will align the declaration names of consecutive lines. This
204 will result in formattings like
Daniel Jasperb5bda7c2015-10-06 12:11:51 +0000205
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000206 .. code-block:: c++
Daniel Jasper8e1ecca2015-10-07 13:02:45 +0000207
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000208 int aaaa = 12;
209 float b = 23;
210 std::string ccc = 23;
Daniel Jaspera44991332015-04-29 13:06:49 +0000211
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000212**AlignEscapedNewlinesLeft** (``bool``)
213 If ``true``, aligns escaped newlines as far left as possible.
214 Otherwise puts them into the right-most column.
215
Daniel Jasper3219e432014-12-02 13:24:51 +0000216**AlignOperands** (``bool``)
217 If ``true``, horizontally align operands of binary and ternary
218 expressions.
219
Alexander Kornienko1e048232016-02-23 16:11:51 +0000220 Specifically, this aligns operands of a single expression that needs to be
221 split over multiple lines, e.g.:
222
223 .. code-block:: c++
224
225 int aaa = bbbbbbbbbbbbbbb +
226 ccccccccccccccc;
227
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000228**AlignTrailingComments** (``bool``)
229 If ``true``, aligns trailing comments.
230
231**AllowAllParametersOfDeclarationOnNextLine** (``bool``)
232 Allow putting all parameters of a function declaration onto
233 the next line even if ``BinPackParameters`` is ``false``.
234
Daniel Jasper17605d32014-05-14 09:33:35 +0000235**AllowShortBlocksOnASingleLine** (``bool``)
236 Allows contracting simple braced statements to a single line.
237
238 E.g., this allows ``if (a) { return; }`` to be put on a single line.
239
Daniel Jasperb87899b2014-09-10 13:11:45 +0000240**AllowShortCaseLabelsOnASingleLine** (``bool``)
241 If ``true``, short case labels will be contracted to a single line.
242
Daniel Jasperb5524822014-04-09 14:05:49 +0000243**AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000244 Dependent on the value, ``int f() { return 0; }`` can be put on a
245 single line.
Daniel Jasperb5524822014-04-09 14:05:49 +0000246
247 Possible values:
248
249 * ``SFS_None`` (in configuration: ``None``)
250 Never merge functions into a single line.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000251
Daniel Jasper3219e432014-12-02 13:24:51 +0000252 * ``SFS_Empty`` (in configuration: ``Empty``)
253 Only merge empty functions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000254
Daniel Jasper20580fd2015-06-11 13:31:45 +0000255 * ``SFS_Inline`` (in configuration: ``Inline``)
256 Only merge functions defined inside a class. Implies "empty".
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000257
Daniel Jasperb5524822014-04-09 14:05:49 +0000258 * ``SFS_All`` (in configuration: ``All``)
259 Merge all functions fitting on a single line.
260
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000261
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000262
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000263**AllowShortIfStatementsOnASingleLine** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000264 If ``true``, ``if (a) return;`` can be put on a single line.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000265
266**AllowShortLoopsOnASingleLine** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000267 If ``true``, ``while (true) continue;`` can be put on a single
268 line.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000269
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000270**AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``)
Zachary Turner448592e2015-12-18 22:20:15 +0000271 The function definition return type breaking style to use. This
272 option is deprecated and is retained for backwards compatibility.
Daniel Jasperca4ea1c2014-08-05 12:16:31 +0000273
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000274 Possible values:
275
276 * ``DRTBS_None`` (in configuration: ``None``)
277 Break after return type automatically.
278 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000279
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000280 * ``DRTBS_All`` (in configuration: ``All``)
281 Always break after the return type.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000282
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000283 * ``DRTBS_TopLevel`` (in configuration: ``TopLevel``)
Zachary Turner448592e2015-12-18 22:20:15 +0000284 Always break after the return types of top-level functions.
285
286
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000287
Zachary Turner448592e2015-12-18 22:20:15 +0000288**AlwaysBreakAfterReturnType** (``ReturnTypeBreakingStyle``)
289 The function declaration return type breaking style to use.
290
291 Possible values:
292
293 * ``RTBS_None`` (in configuration: ``None``)
294 Break after return type automatically.
295 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000296
Zachary Turner448592e2015-12-18 22:20:15 +0000297 * ``RTBS_All`` (in configuration: ``All``)
298 Always break after the return type.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000299
Zachary Turner448592e2015-12-18 22:20:15 +0000300 * ``RTBS_TopLevel`` (in configuration: ``TopLevel``)
301 Always break after the return types of top-level functions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000302
Zachary Turner448592e2015-12-18 22:20:15 +0000303 * ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``)
304 Always break after the return type of function definitions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000305
Zachary Turner448592e2015-12-18 22:20:15 +0000306 * ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``)
307 Always break after the return type of top-level definitions.
Birunthan Mohanathasa0388a82015-06-29 15:30:42 +0000308
Daniel Jasperca4ea1c2014-08-05 12:16:31 +0000309
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000310
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000311**AlwaysBreakBeforeMultilineStrings** (``bool``)
312 If ``true``, always break before multiline string literals.
313
Daniel Jasper2aaedd32015-06-18 09:12:47 +0000314 This flag is mean to make cases where there are multiple multiline strings
315 in a file look more consistent. Thus, it will only take effect if wrapping
316 the string at that point leads to it being indented
317 ``ContinuationIndentWidth`` spaces from the start of the line.
318
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000319**AlwaysBreakTemplateDeclarations** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000320 If ``true``, always break after the ``template<...>`` of a template
321 declaration.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000322
Daniel Jasper18210d72014-10-09 09:52:05 +0000323**BinPackArguments** (``bool``)
324 If ``false``, a function call's arguments will either be all on the
325 same line or will have one line each.
326
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000327**BinPackParameters** (``bool``)
Daniel Jasper18210d72014-10-09 09:52:05 +0000328 If ``false``, a function declaration's or function definition's
329 parameters will either all be on the same line or will have one line each.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000330
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000331**BraceWrapping** (``BraceWrappingFlags``)
332 Control of individual brace wrapping cases.
333
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000334 If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how
335 each individual brace case should be handled. Otherwise, this is ignored.
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000336
337 Nested configuration flags:
338
339 * ``bool AfterClass`` Wrap class definitions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000340 * ``bool AfterControlStatement`` Wrap control statements (``if``/``for``/``while``/``switch``/..).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000341 * ``bool AfterEnum`` Wrap enum definitions.
342 * ``bool AfterFunction`` Wrap function definitions.
343 * ``bool AfterNamespace`` Wrap namespace definitions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000344 * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (``@autoreleasepool``, interfaces, ..).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000345 * ``bool AfterStruct`` Wrap struct definitions.
346 * ``bool AfterUnion`` Wrap union definitions.
347 * ``bool BeforeCatch`` Wrap before ``catch``.
348 * ``bool BeforeElse`` Wrap before ``else``.
349 * ``bool IndentBraces`` Indent the wrapped braces themselves.
350
351
Daniel Jasper6501f7e2015-10-27 12:38:37 +0000352**BreakAfterJavaFieldAnnotations** (``bool``)
353 Break after each annotation on a field in Java files.
354
Daniel Jasperac043c92014-09-15 11:11:00 +0000355**BreakBeforeBinaryOperators** (``BinaryOperatorStyle``)
356 The way to wrap binary operators.
357
358 Possible values:
359
360 * ``BOS_None`` (in configuration: ``None``)
361 Break after operators.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000362
Daniel Jasperac043c92014-09-15 11:11:00 +0000363 * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``)
364 Break before operators that aren't assignments.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000365
Daniel Jasperac043c92014-09-15 11:11:00 +0000366 * ``BOS_All`` (in configuration: ``All``)
367 Break before operators.
368
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000369
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000370
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000371**BreakBeforeBraces** (``BraceBreakingStyle``)
372 The brace breaking style to use.
373
374 Possible values:
375
376 * ``BS_Attach`` (in configuration: ``Attach``)
377 Always attach braces to surrounding context.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000378
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000379 * ``BS_Linux`` (in configuration: ``Linux``)
380 Like ``Attach``, but break before braces on function, namespace and
381 class definitions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000382
Birunthan Mohanathas305fa9c2015-07-12 03:13:54 +0000383 * ``BS_Mozilla`` (in configuration: ``Mozilla``)
384 Like ``Attach``, but break before braces on enum, function, and record
385 definitions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000386
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000387 * ``BS_Stroustrup`` (in configuration: ``Stroustrup``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000388 Like ``Attach``, but break before function definitions, ``catch``, and
389 ``else``.
390
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000391 * ``BS_Allman`` (in configuration: ``Allman``)
392 Always break before braces.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000393
Daniel Jasperee107ad2014-02-13 12:51:50 +0000394 * ``BS_GNU`` (in configuration: ``GNU``)
395 Always break before braces and add an extra level of indentation to
396 braces of control statements, not to those of class, function
397 or other definitions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000398
Roman Kashitsyn291f64f2015-08-10 13:43:19 +0000399 * ``BS_WebKit`` (in configuration: ``WebKit``)
400 Like ``Attach``, but break before functions.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000401
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000402 * ``BS_Custom`` (in configuration: ``Custom``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000403 Configure each individual brace in `BraceWrapping`.
404
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000405
406
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000407**BreakBeforeTernaryOperators** (``bool``)
408 If ``true``, ternary operators will be placed after line breaks.
409
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000410**BreakConstructorInitializersBeforeComma** (``bool``)
411 Always break constructor initializers before commas and align
412 the commas with the colon.
413
Alexander Kornienko1e048232016-02-23 16:11:51 +0000414**BreakStringLiterals** (``bool``)
415 Allow breaking string literals when formatting.
416
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000417**ColumnLimit** (``unsigned``)
418 The column limit.
419
420 A column limit of ``0`` means that there is no column limit. In this case,
421 clang-format will respect the input's line breaking decisions within
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000422 statements unless they contradict other rules.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000423
Daniel Jasperee107ad2014-02-13 12:51:50 +0000424**CommentPragmas** (``std::string``)
425 A regular expression that describes comments with special meaning,
426 which should not be split into lines or otherwise changed.
427
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000428**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``)
429 If the constructor initializers don't fit on a line, put each
430 initializer on its own line.
431
432**ConstructorInitializerIndentWidth** (``unsigned``)
433 The number of characters to use for indentation of constructor
434 initializer lists.
435
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000436**ContinuationIndentWidth** (``unsigned``)
437 Indent width for line continuations.
438
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000439**Cpp11BracedListStyle** (``bool``)
440 If ``true``, format braced lists as best suited for C++11 braced
441 lists.
442
443 Important differences:
444 - No spaces inside the braced list.
445 - No line break before the closing brace.
446 - Indentation with the continuation indent, not with the block indent.
447
448 Fundamentally, C++11 braced lists are formatted exactly like function
449 calls would be formatted in their place. If the braced list follows a name
450 (e.g. a type or variable name), clang-format formats as if the ``{}`` were
451 the parentheses of a function call with that name. If there is no name,
452 a zero-length name is assumed.
453
Daniel Jasper553d4872014-06-17 12:40:34 +0000454**DerivePointerAlignment** (``bool``)
455 If ``true``, analyze the formatted file for the most common
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000456 alignment of ``&`` and ``\*``. ``PointerAlignment`` is then used only as
457 fallback.
Daniel Jasper553d4872014-06-17 12:40:34 +0000458
459**DisableFormat** (``bool``)
Birunthan Mohanathasb744e722015-06-27 09:25:28 +0000460 Disables formatting completely.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000461
462**ExperimentalAutoDetectBinPacking** (``bool``)
463 If ``true``, clang-format detects whether function calls and
464 definitions are formatted with one parameter per line.
465
466 Each call can be bin-packed, one-per-line or inconclusive. If it is
467 inconclusive, e.g. completely on one line, but a decision needs to be
468 made, clang-format analyzes whether there are other bin-packed cases in
469 the input file and act accordingly.
470
471 NOTE: This is an experimental flag, that might go away or be renamed. Do
472 not use this in config files, etc. Use at your own risk.
473
Daniel Jaspere1e43192014-04-01 12:55:11 +0000474**ForEachMacros** (``std::vector<std::string>``)
Daniel Jasperb5524822014-04-09 14:05:49 +0000475 A vector of macros that should be interpreted as foreach loops
476 instead of as function calls.
Daniel Jaspere1e43192014-04-01 12:55:11 +0000477
Daniel Jasperb5524822014-04-09 14:05:49 +0000478 These are expected to be macros of the form:
Daniel Jasperb5bda7c2015-10-06 12:11:51 +0000479
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000480 .. code-block:: c++
Daniel Jasper8e1ecca2015-10-07 13:02:45 +0000481
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000482 FOREACH(<variable-declaration>, ...)
483 <loop-body>
484
485 In the .clang-format configuration file, this can be configured like:
Daniel Jasperb5bda7c2015-10-06 12:11:51 +0000486
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000487 .. code-block:: yaml
Daniel Jasper8e1ecca2015-10-07 13:02:45 +0000488
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000489 ForEachMacros: ['RANGES_FOR', 'FOREACH']
Daniel Jasperb5524822014-04-09 14:05:49 +0000490
491 For example: BOOST_FOREACH.
Daniel Jaspere1e43192014-04-01 12:55:11 +0000492
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000493**IncludeCategories** (``std::vector<IncludeCategory>``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000494 Regular expressions denoting the different ``#include`` categories
495 used for ordering ``#includes``.
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000496
497 These regular expressions are matched against the filename of an include
498 (including the <> or "") in order. The value belonging to the first
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000499 matching regular expression is assigned and ``#includes`` are sorted first
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000500 according to increasing category number and then alphabetically within
501 each category.
502
Alexander Kornienko1e048232016-02-23 16:11:51 +0000503 If none of the regular expressions match, INT_MAX is assigned as
504 category. The main header for a source file automatically gets category 0.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000505 so that it is generally kept at the beginning of the ``#includes``
Alexander Kornienko1e048232016-02-23 16:11:51 +0000506 (http://llvm.org/docs/CodingStandards.html#include-style). However, you
507 can also assign negative priorities if you have certain headers that
508 always need to be first.
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000509
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000510 To configure this in the .clang-format file, use:
Daniel Jasperb5bda7c2015-10-06 12:11:51 +0000511
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000512 .. code-block:: yaml
Daniel Jasper8e1ecca2015-10-07 13:02:45 +0000513
Daniel Jasper8ce1b8d2015-10-06 11:54:18 +0000514 IncludeCategories:
515 - Regex: '^"(llvm|llvm-c|clang|clang-c)/'
516 Priority: 2
517 - Regex: '^(<|"(gtest|isl|json)/)'
518 Priority: 3
519 - Regex: '.\*'
520 Priority: 1
521
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000522**IndentCaseLabels** (``bool``)
523 Indent case labels one level from the switch statement.
524
525 When ``false``, use the same indentation level as for the switch statement.
526 Switch statement body is always indented one level more than case labels.
527
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000528**IndentWidth** (``unsigned``)
Alexander Kornienko45ca09b2013-09-27 16:16:55 +0000529 The number of columns to use for indentation.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000530
Daniel Jasperc75e1ef2014-07-09 08:42:42 +0000531**IndentWrappedFunctionNames** (``bool``)
532 Indent if a function definition or declaration is wrapped after the
533 type.
534
Daniel Jasperb5524822014-04-09 14:05:49 +0000535**KeepEmptyLinesAtTheStartOfBlocks** (``bool``)
536 If true, empty lines at the start of blocks are kept.
537
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000538**Language** (``LanguageKind``)
539 Language, this format style is targeted at.
540
541 Possible values:
542
543 * ``LK_None`` (in configuration: ``None``)
544 Do not use.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000545
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000546 * ``LK_Cpp`` (in configuration: ``Cpp``)
547 Should be used for C, C++, ObjectiveC, ObjectiveC++.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000548
Daniel Jasper18210d72014-10-09 09:52:05 +0000549 * ``LK_Java`` (in configuration: ``Java``)
550 Should be used for Java.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000551
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000552 * ``LK_JavaScript`` (in configuration: ``JavaScript``)
553 Should be used for JavaScript.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000554
Daniel Jasperee107ad2014-02-13 12:51:50 +0000555 * ``LK_Proto`` (in configuration: ``Proto``)
556 Should be used for Protocol Buffers
557 (https://developers.google.com/protocol-buffers/).
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000558
Alexander Kornienko1e048232016-02-23 16:11:51 +0000559 * ``LK_TableGen`` (in configuration: ``TableGen``)
560 Should be used for TableGen code.
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000561
562
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000563
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000564**MacroBlockBegin** (``std::string``)
565 A regular expression matching macros that start a block.
566
567**MacroBlockEnd** (``std::string``)
568 A regular expression matching macros that end a block.
569
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000570**MaxEmptyLinesToKeep** (``unsigned``)
571 The maximum number of consecutive empty lines to keep.
572
573**NamespaceIndentation** (``NamespaceIndentationKind``)
574 The indentation used for namespaces.
575
576 Possible values:
577
578 * ``NI_None`` (in configuration: ``None``)
579 Don't indent in namespaces.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000580
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000581 * ``NI_Inner`` (in configuration: ``Inner``)
582 Indent only in inner namespaces (nested in other namespaces).
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000583
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000584 * ``NI_All`` (in configuration: ``All``)
585 Indent in all namespaces.
586
587
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000588
Daniel Jaspereb2226e2014-10-28 16:56:37 +0000589**ObjCBlockIndentWidth** (``unsigned``)
590 The number of characters to use for indentation of ObjC blocks.
591
Daniel Jasperee107ad2014-02-13 12:51:50 +0000592**ObjCSpaceAfterProperty** (``bool``)
593 Add a space after ``@property`` in Objective-C, i.e. use
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000594 ``@property (readonly)`` instead of ``@property(readonly)``.
Daniel Jasperee107ad2014-02-13 12:51:50 +0000595
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000596**ObjCSpaceBeforeProtocolList** (``bool``)
597 Add a space in front of an Objective-C protocol list, i.e. use
598 ``Foo <Protocol>`` instead of ``Foo<Protocol>``.
599
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000600**PenaltyBreakBeforeFirstCallParameter** (``unsigned``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000601 The penalty for breaking a function call after ``call(``.
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000602
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000603**PenaltyBreakComment** (``unsigned``)
604 The penalty for each line break introduced inside a comment.
605
606**PenaltyBreakFirstLessLess** (``unsigned``)
607 The penalty for breaking before the first ``<<``.
608
609**PenaltyBreakString** (``unsigned``)
610 The penalty for each line break introduced inside a string literal.
611
612**PenaltyExcessCharacter** (``unsigned``)
613 The penalty for each character outside of the column limit.
614
615**PenaltyReturnTypeOnItsOwnLine** (``unsigned``)
616 Penalty for putting the return type of a function onto its own
617 line.
618
Daniel Jasper553d4872014-06-17 12:40:34 +0000619**PointerAlignment** (``PointerAlignmentStyle``)
620 Pointer and reference alignment style.
621
622 Possible values:
623
624 * ``PAS_Left`` (in configuration: ``Left``)
625 Align pointer to the left.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000626
Daniel Jasper553d4872014-06-17 12:40:34 +0000627 * ``PAS_Right`` (in configuration: ``Right``)
628 Align pointer to the right.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000629
Daniel Jasper553d4872014-06-17 12:40:34 +0000630 * ``PAS_Middle`` (in configuration: ``Middle``)
631 Align pointer in the middle.
632
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000633
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000634
Alexander Kornienko1e048232016-02-23 16:11:51 +0000635**ReflowComments** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000636 If ``true``, clang-format will attempt to re-flow comments.
Alexander Kornienko1e048232016-02-23 16:11:51 +0000637
638**SortIncludes** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000639 If ``true``, clang-format will sort ``#includes``.
Alexander Kornienko1e048232016-02-23 16:11:51 +0000640
Daniel Jasperb87899b2014-09-10 13:11:45 +0000641**SpaceAfterCStyleCast** (``bool``)
642 If ``true``, a space may be inserted after C style casts.
643
Daniel Jasperd94bff32013-09-25 15:15:02 +0000644**SpaceBeforeAssignmentOperators** (``bool``)
Alexander Kornienko45ca09b2013-09-27 16:16:55 +0000645 If ``false``, spaces will be removed before assignment operators.
Daniel Jasperd94bff32013-09-25 15:15:02 +0000646
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000647**SpaceBeforeParens** (``SpaceBeforeParensOptions``)
648 Defines in which cases to put a space before opening parentheses.
649
650 Possible values:
651
652 * ``SBPO_Never`` (in configuration: ``Never``)
653 Never put a space before opening parentheses.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000654
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000655 * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``)
656 Put a space before opening parentheses only after control statement
657 keywords (``for/if/while...``).
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000658
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000659 * ``SBPO_Always`` (in configuration: ``Always``)
660 Always put a space before opening parentheses, except when it's
661 prohibited by the syntax rules (in function-like macro definitions) or
662 when determined by other style rules (after unary operators, opening
663 parentheses, etc.)
664
665
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000666
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000667**SpaceInEmptyParentheses** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000668 If ``true``, spaces may be inserted into ``()``.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000669
670**SpacesBeforeTrailingComments** (``unsigned``)
Daniel Jasperc0be7602014-05-15 13:55:19 +0000671 The number of spaces before trailing line comments
672 (``//`` - comments).
Daniel Jasperb5524822014-04-09 14:05:49 +0000673
Alexander Kornienko70f97c92016-02-27 14:02:08 +0000674 This does not affect trailing block comments (``/*`` - comments) as
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000675 those commonly have different usage patterns and a number of special
676 cases.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000677
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000678**SpacesInAngles** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000679 If ``true``, spaces will be inserted after ``<`` and before ``>``
680 in template argument lists.
Alexander Kornienkofdca83d2013-12-10 10:18:34 +0000681
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000682**SpacesInCStyleCastParentheses** (``bool``)
Daniel Jasperee107ad2014-02-13 12:51:50 +0000683 If ``true``, spaces may be inserted into C style casts.
684
685**SpacesInContainerLiterals** (``bool``)
686 If ``true``, spaces are inserted inside container literals (e.g.
687 ObjC and Javascript array and dict literals).
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000688
689**SpacesInParentheses** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000690 If ``true``, spaces will be inserted after ``(`` and before ``)``.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000691
Daniel Jasperad981f82014-08-26 11:41:14 +0000692**SpacesInSquareBrackets** (``bool``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000693 If ``true``, spaces will be inserted after ``[`` and before ``]``.
Daniel Jasperad981f82014-08-26 11:41:14 +0000694
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000695**Standard** (``LanguageStandard``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000696 Format compatible with this standard, e.g. use ``A<A<int> >``
697 instead of ``A<A<int>>`` for ``LS_Cpp03``.
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000698
699 Possible values:
700
701 * ``LS_Cpp03`` (in configuration: ``Cpp03``)
702 Use C++03-compatible syntax.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000703
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000704 * ``LS_Cpp11`` (in configuration: ``Cpp11``)
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000705 Use features of C++11 (e.g. ``A<A<int>>`` instead of ``A<A<int> >``).
706
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000707 * ``LS_Auto`` (in configuration: ``Auto``)
708 Automatic detection based on the input.
709
710
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000711
Alexander Kornienko45ca09b2013-09-27 16:16:55 +0000712**TabWidth** (``unsigned``)
713 The number of columns used for tab stops.
714
715**UseTab** (``UseTabStyle``)
716 The way to use tab characters in the resulting file.
717
718 Possible values:
719
720 * ``UT_Never`` (in configuration: ``Never``)
721 Never use tab.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000722
Alexander Kornienko45ca09b2013-09-27 16:16:55 +0000723 * ``UT_ForIndentation`` (in configuration: ``ForIndentation``)
724 Use tabs only for indentation.
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000725
Alexander Kornienko45ca09b2013-09-27 16:16:55 +0000726 * ``UT_Always`` (in configuration: ``Always``)
727 Use tabs whenever we need to fill whitespace that spans at least from
728 one tab stop to the next one.
729
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000730
Alexander Kornienkob00a7d22016-02-23 16:12:00 +0000731
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000732.. END_FORMAT_STYLE_OPTIONS
733
Daniel Jasper49d3d582015-10-05 07:24:55 +0000734Adding additional style options
735===============================
736
737Each additional style option adds costs to the clang-format project. Some of
Sylvestre Ledrube8f3962016-02-14 20:20:58 +0000738these costs affect the clang-format development itself, as we need to make
Daniel Jasper49d3d582015-10-05 07:24:55 +0000739sure that any given combination of options work and that new features don't
740break any of the existing options in any way. There are also costs for end users
741as options become less discoverable and people have to think about and make a
742decision on options they don't really care about.
743
744The goal of the clang-format project is more on the side of supporting a
745limited set of styles really well as opposed to supporting every single style
746used by a codebase somewhere in the wild. Of course, we do want to support all
747major projects and thus have established the following bar for adding style
748options. Each new style option must ..
749
Daniel Jasperfcbea712015-10-05 13:30:42 +0000750 * be used in a project of significant size (have dozens of contributors)
751 * have a publicly accessible style guide
752 * have a person willing to contribute and maintain patches
Daniel Jasper49d3d582015-10-05 07:24:55 +0000753
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000754Examples
755========
756
757A style similar to the `Linux Kernel style
758<https://www.kernel.org/doc/Documentation/CodingStyle>`_:
759
760.. code-block:: yaml
761
762 BasedOnStyle: LLVM
763 IndentWidth: 8
Alexander Kornienko8ba68f62013-09-27 16:19:25 +0000764 UseTab: Always
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000765 BreakBeforeBraces: Linux
766 AllowShortIfStatementsOnASingleLine: false
767 IndentCaseLabels: false
768
769The result is (imagine that tabs are used for indentation here):
770
771.. code-block:: c++
772
773 void test()
774 {
775 switch (x) {
776 case 0:
777 case 1:
778 do_something();
779 break;
780 case 2:
781 do_something_else();
782 break;
783 default:
784 break;
785 }
786 if (condition)
787 do_something_completely_different();
788
789 if (x == y) {
790 q();
791 } else if (x > y) {
792 w();
793 } else {
794 r();
795 }
796 }
797
798A style similar to the default Visual Studio formatting style:
799
800.. code-block:: yaml
801
Alexander Kornienko8ba68f62013-09-27 16:19:25 +0000802 UseTab: Never
Alexander Kornienkod278e0e2013-09-04 15:09:13 +0000803 IndentWidth: 4
804 BreakBeforeBraces: Allman
805 AllowShortIfStatementsOnASingleLine: false
806 IndentCaseLabels: false
807 ColumnLimit: 0
808
809The result is:
810
811.. code-block:: c++
812
813 void test()
814 {
815 switch (suffix)
816 {
817 case 0:
818 case 1:
819 do_something();
820 break;
821 case 2:
822 do_something_else();
823 break;
824 default:
825 break;
826 }
827 if (condition)
828 do_somthing_completely_different();
829
830 if (x == y)
831 {
832 q();
833 }
834 else if (x > y)
835 {
836 w();
837 }
838 else
839 {
840 r();
841 }
842 }
843