xref: /minix3/external/bsd/llvm/dist/clang/docs/ClangFormatStyleOptions.rst (revision 0b98e8aad89f2bd4ba80b523d73cf29e9dd82ce1)
1==========================
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
19use ``-style=file`` and put style configuration in the ``.clang-format`` or
20``_clang-format`` file in the project directory.
21
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++
65enumeration member (with a prefix, e.g. ``LS_Auto``), and as a value usable in
66the configuration (without a prefix: ``Auto``).
67
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 columns 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
252**SpaceBeforeAssignmentOperators** (``bool``)
253  If ``false``, spaces will be removed before assignment operators.
254
255**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**TabWidth** (``unsigned``)
284  The number of columns used for tab stops.
285
286**UseTab** (``UseTabStyle``)
287  The way to use tab characters in the resulting file.
288
289  Possible values:
290
291  * ``UT_Never`` (in configuration: ``Never``)
292    Never use tab.
293  * ``UT_ForIndentation`` (in configuration: ``ForIndentation``)
294    Use tabs only for indentation.
295  * ``UT_Always`` (in configuration: ``Always``)
296    Use tabs whenever we need to fill whitespace that spans at least from
297    one tab stop to the next one.
298
299
300.. END_FORMAT_STYLE_OPTIONS
301
302Examples
303========
304
305A style similar to the `Linux Kernel style
306<https://www.kernel.org/doc/Documentation/CodingStyle>`_:
307
308.. code-block:: yaml
309
310  BasedOnStyle: LLVM
311  IndentWidth: 8
312  UseTab: Always
313  BreakBeforeBraces: Linux
314  AllowShortIfStatementsOnASingleLine: false
315  IndentCaseLabels: false
316
317The result is (imagine that tabs are used for indentation here):
318
319.. code-block:: c++
320
321  void test()
322  {
323          switch (x) {
324          case 0:
325          case 1:
326                  do_something();
327                  break;
328          case 2:
329                  do_something_else();
330                  break;
331          default:
332                  break;
333          }
334          if (condition)
335                  do_something_completely_different();
336
337          if (x == y) {
338                  q();
339          } else if (x > y) {
340                  w();
341          } else {
342                  r();
343          }
344  }
345
346A style similar to the default Visual Studio formatting style:
347
348.. code-block:: yaml
349
350  UseTab: Never
351  IndentWidth: 4
352  BreakBeforeBraces: Allman
353  AllowShortIfStatementsOnASingleLine: false
354  IndentCaseLabels: false
355  ColumnLimit: 0
356
357The result is:
358
359.. code-block:: c++
360
361  void test()
362  {
363      switch (suffix)
364      {
365      case 0:
366      case 1:
367          do_something();
368          break;
369      case 2:
370          do_something_else();
371          break;
372      default:
373          break;
374      }
375      if (condition)
376          do_somthing_completely_different();
377
378      if (x == y)
379      {
380          q();
381      }
382      else if (x > y)
383      {
384          w();
385      }
386      else
387      {
388          r();
389      }
390  }
391
392