Skip to content

Commit d278e0e

Browse files
committed
Added documentation for clang-format style options.
Summary: The main contents is in the ClangFormatStyleOptions.rst, which can be updated from the Format.h by the dump_format_style.py script. Reviewers: djasper, klimek Reviewed By: klimek CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D1597 llvm-svn: 189946
1 parent fd9a941 commit d278e0e

File tree

4 files changed

+533
-0
lines changed

4 files changed

+533
-0
lines changed

clang/docs/ClangFormat.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ to format C/C++/Obj-C code.
7070
-version - Display the version of this program
7171
7272
73+
When the desired code formatting style is different from the available options,
74+
the style can be customized using the ``-style="{key: value, ...}"`` option or
75+
by putting your style configuration to the ``.clang-format`` file in your
76+
project's directory and using ``clang-format -style=file``.
77+
78+
An easy way to create the ``.clang-format`` file is:
79+
80+
.. code-block:: console
81+
82+
clang-format -style=llvm -dump-config > .clang-format
83+
84+
Available style options are described in :doc:`ClangFormatStyleOptions`.
85+
86+
7387
Vim Integration
7488
===============
7589

Lines changed: 375 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,375 @@
1+
==========================
2+
Clang-Format Style Options
3+
==========================
4+
5+
:doc:`ClangFormatStyleOptions` describes configurable formatting style options
6+
supported by :doc:`LibFormat` and :doc:`ClangFormat`.
7+
8+
When using :program:`clang-format` command line utility or
9+
``clang::format::reformat(...)`` functions from code, one can either use one of
10+
the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit) or create a
11+
custom style by configuring specific style options.
12+
13+
14+
Configuring Style with clang-format
15+
===================================
16+
17+
:program:`clang-format` supports two ways to provide custom style options:
18+
directly specify style configuration in the ``-style=`` command line option or
19+
use ``-style=file`` and put style configuration in the ``.clang-format`` file
20+
in the project directory.
21+
22+
When using ``-style=file``, :program:`clang-format` for each input file will
23+
try to find the ``.clang-format`` file located in the closest parent directory
24+
of the input file. When the standard input is used, the search is started from
25+
the current directory.
26+
27+
The ``.clang-format`` file uses YAML format:
28+
29+
.. code-block:: yaml
30+
31+
key1: value1
32+
key2: value2
33+
# A comment.
34+
...
35+
36+
An easy way to get a valid ``.clang-format`` file containing all configuration
37+
options of a certain predefined style is:
38+
39+
.. code-block:: console
40+
41+
clang-format -style=llvm -dump-config > .clang-format
42+
43+
When specifying configuration in the ``-style=`` option, the same configuration
44+
is 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+
51+
Configuring Style in Code
52+
=========================
53+
54+
When using ``clang::format::reformat(...)`` functions, the format is specified
55+
by supplying the `clang::format::FormatStyle
56+
<http://clang.llvm.org/doxygen/structclang_1_1format_1_1FormatStyle.html>`_
57+
structure.
58+
59+
60+
Configurable Format Style Options
61+
=================================
62+
63+
This section lists the supported style options. Value type is specified for
64+
each option. For enumeration types possible values are specified both as a C++
65+
enumeration member (with a prefix, e.g. LS_Auto), and as a value usable in the
66+
configuration (without a prefix).
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 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+
252+
**SpaceInEmptyParentheses** (``bool``)
253+
If ``false``, spaces may be inserted into '()'.
254+
255+
**SpacesBeforeTrailingComments** (``unsigned``)
256+
The number of spaces to before trailing line comments.
257+
258+
**SpacesInCStyleCastParentheses** (``bool``)
259+
If ``false``, spaces may be inserted into C style casts.
260+
261+
**SpacesInParentheses** (``bool``)
262+
If ``true``, spaces will be inserted after every '(' and before
263+
every ')'.
264+
265+
**Standard** (``LanguageStandard``)
266+
Format compatible with this standard, e.g. use
267+
``A<A<int> >`` instead of ``A<A<int>>`` for LS_Cpp03.
268+
269+
Possible values:
270+
271+
* ``LS_Cpp03`` (in configuration: ``Cpp03``)
272+
Use C++03-compatible syntax.
273+
* ``LS_Cpp11`` (in configuration: ``Cpp11``)
274+
Use features of C++11 (e.g. ``A<A<int>>`` instead of
275+
``A<A<int> >``).
276+
* ``LS_Auto`` (in configuration: ``Auto``)
277+
Automatic detection based on the input.
278+
279+
280+
**UseTab** (``bool``)
281+
If ``true``, ``IndentWidth`` consecutive spaces will be replaced
282+
with tab characters.
283+
284+
.. END_FORMAT_STYLE_OPTIONS
285+
286+
Examples
287+
========
288+
289+
A style similar to the `Linux Kernel style
290+
<https://www.kernel.org/doc/Documentation/CodingStyle>`_:
291+
292+
.. code-block:: yaml
293+
294+
BasedOnStyle: LLVM
295+
IndentWidth: 8
296+
UseTab: true
297+
BreakBeforeBraces: Linux
298+
AllowShortIfStatementsOnASingleLine: false
299+
IndentCaseLabels: false
300+
301+
The result is (imagine that tabs are used for indentation here):
302+
303+
.. code-block:: c++
304+
305+
void test()
306+
{
307+
switch (x) {
308+
case 0:
309+
case 1:
310+
do_something();
311+
break;
312+
case 2:
313+
do_something_else();
314+
break;
315+
default:
316+
break;
317+
}
318+
if (condition)
319+
do_something_completely_different();
320+
321+
if (x == y) {
322+
q();
323+
} else if (x > y) {
324+
w();
325+
} else {
326+
r();
327+
}
328+
}
329+
330+
A style similar to the default Visual Studio formatting style:
331+
332+
.. code-block:: yaml
333+
334+
UseTab: false
335+
IndentWidth: 4
336+
BreakBeforeBraces: Allman
337+
AllowShortIfStatementsOnASingleLine: false
338+
IndentCaseLabels: false
339+
ColumnLimit: 0
340+
341+
The result is:
342+
343+
.. code-block:: c++
344+
345+
void test()
346+
{
347+
switch (suffix)
348+
{
349+
case 0:
350+
case 1:
351+
do_something();
352+
break;
353+
case 2:
354+
do_something_else();
355+
break;
356+
default:
357+
break;
358+
}
359+
if (condition)
360+
do_somthing_completely_different();
361+
362+
if (x == y)
363+
{
364+
q();
365+
}
366+
else if (x > y)
367+
{
368+
w();
369+
}
370+
else
371+
{
372+
r();
373+
}
374+
}
375+

0 commit comments

Comments
 (0)