Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 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 |
Hans Wennborg | 9f6581b | 2013-09-10 15:41:12 +0000 | [diff] [blame] | 19 | use ``-style=file`` and put style configuration in the ``.clang-format`` or |
| 20 | ``_clang-format`` file in the project directory. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 092cb21 | 2014-08-12 13:34:22 +0000 | [diff] [blame] | 36 | The configuration file can consist of several sections each having different |
| 37 | ``Language:`` parameter denoting the programming language this section of the |
| 38 | configuration is targeted at. See the description of the **Language** option |
| 39 | below for the list of supported languages. The first section may have no |
| 40 | language set, it will set the default style options for all lanugages. |
| 41 | Configuration sections for specific language will override options set in the |
| 42 | default section. |
| 43 | |
| 44 | When :program:`clang-format` formats a file, it auto-detects the language using |
| 45 | the file name. When formatting standard input or a file that doesn't have the |
| 46 | extension corresponding to its language, ``-assume-filename=`` option can be |
| 47 | used to override the file name :program:`clang-format` uses to detect the |
| 48 | language. |
| 49 | |
| 50 | An 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 Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 73 | An easy way to get a valid ``.clang-format`` file containing all configuration |
| 74 | options of a certain predefined style is: |
| 75 | |
| 76 | .. code-block:: console |
| 77 | |
| 78 | clang-format -style=llvm -dump-config > .clang-format |
| 79 | |
| 80 | When specifying configuration in the ``-style=`` option, the same configuration |
| 81 | is 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 Jasper | d8b4ec0 | 2014-10-07 12:15:15 +0000 | [diff] [blame] | 88 | Disabling Formatting on a Piece of Code |
| 89 | ======================================= |
| 90 | |
| 91 | Clang-format understands also special comments that switch formatting in a |
| 92 | delimited 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 |
| 95 | will 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 Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 106 | Configuring Style in Code |
| 107 | ========================= |
| 108 | |
| 109 | When using ``clang::format::reformat(...)`` functions, the format is specified |
| 110 | by supplying the `clang::format::FormatStyle |
| 111 | <http://clang.llvm.org/doxygen/structclang_1_1format_1_1FormatStyle.html>`_ |
| 112 | structure. |
| 113 | |
| 114 | |
| 115 | Configurable Format Style Options |
| 116 | ================================= |
| 117 | |
| 118 | This section lists the supported style options. Value type is specified for |
| 119 | each option. For enumeration types possible values are specified both as a C++ |
Alexander Kornienko | 472d27a | 2013-09-04 15:14:18 +0000 | [diff] [blame] | 120 | enumeration member (with a prefix, e.g. ``LS_Auto``), and as a value usable in |
| 121 | the configuration (without a prefix: ``Auto``). |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 122 | |
| 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``) |
| 151 | The extra indent or outdent of access modifiers, e.g. ``public:``. |
| 152 | |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 153 | **AlignAfterOpenBracket** (``bool``) |
| 154 | If ``true``, horizontally aligns arguments after an open bracket. |
| 155 | |
| 156 | This applies to round brackets (parentheses), angle brackets and square |
| 157 | brackets. This will result in formattings like |
Daniel Jasper | b5bda7c | 2015-10-06 12:11:51 +0000 | [diff] [blame] | 158 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 159 | .. code-block:: c++ |
| 160 | someLongFunction(argument1, |
| 161 | argument2); |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 162 | |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 163 | **AlignConsecutiveAssignments** (``bool``) |
| 164 | If ``true``, aligns consecutive assignments. |
| 165 | |
| 166 | This will align the assignment operators of consecutive lines. This |
| 167 | will result in formattings like |
Daniel Jasper | b5bda7c | 2015-10-06 12:11:51 +0000 | [diff] [blame] | 168 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 169 | .. code-block:: c++ |
| 170 | int aaaa = 12; |
| 171 | int b = 23; |
| 172 | int ccc = 23; |
| 173 | |
| 174 | **AlignConsecutiveDeclarations** (``bool``) |
| 175 | If ``true``, aligns consecutive declarations. |
| 176 | |
| 177 | This will align the declaration names of consecutive lines. This |
| 178 | will result in formattings like |
Daniel Jasper | b5bda7c | 2015-10-06 12:11:51 +0000 | [diff] [blame] | 179 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 180 | .. code-block:: c++ |
| 181 | int aaaa = 12; |
| 182 | float b = 23; |
| 183 | std::string ccc = 23; |
Daniel Jasper | a4499133 | 2015-04-29 13:06:49 +0000 | [diff] [blame] | 184 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 185 | **AlignEscapedNewlinesLeft** (``bool``) |
| 186 | If ``true``, aligns escaped newlines as far left as possible. |
| 187 | Otherwise puts them into the right-most column. |
| 188 | |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 189 | **AlignOperands** (``bool``) |
| 190 | If ``true``, horizontally align operands of binary and ternary |
| 191 | expressions. |
| 192 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 193 | **AlignTrailingComments** (``bool``) |
| 194 | If ``true``, aligns trailing comments. |
| 195 | |
| 196 | **AllowAllParametersOfDeclarationOnNextLine** (``bool``) |
| 197 | Allow putting all parameters of a function declaration onto |
| 198 | the next line even if ``BinPackParameters`` is ``false``. |
| 199 | |
Daniel Jasper | 17605d3 | 2014-05-14 09:33:35 +0000 | [diff] [blame] | 200 | **AllowShortBlocksOnASingleLine** (``bool``) |
| 201 | Allows contracting simple braced statements to a single line. |
| 202 | |
| 203 | E.g., this allows ``if (a) { return; }`` to be put on a single line. |
| 204 | |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 205 | **AllowShortCaseLabelsOnASingleLine** (``bool``) |
| 206 | If ``true``, short case labels will be contracted to a single line. |
| 207 | |
Daniel Jasper | b552482 | 2014-04-09 14:05:49 +0000 | [diff] [blame] | 208 | **AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``) |
| 209 | Dependent on the value, ``int f() { return 0; }`` can be put |
| 210 | on a single line. |
| 211 | |
| 212 | Possible values: |
| 213 | |
| 214 | * ``SFS_None`` (in configuration: ``None``) |
| 215 | Never merge functions into a single line. |
Daniel Jasper | 3219e43 | 2014-12-02 13:24:51 +0000 | [diff] [blame] | 216 | * ``SFS_Empty`` (in configuration: ``Empty``) |
| 217 | Only merge empty functions. |
Daniel Jasper | 20580fd | 2015-06-11 13:31:45 +0000 | [diff] [blame] | 218 | * ``SFS_Inline`` (in configuration: ``Inline``) |
| 219 | Only merge functions defined inside a class. Implies "empty". |
Daniel Jasper | b552482 | 2014-04-09 14:05:49 +0000 | [diff] [blame] | 220 | * ``SFS_All`` (in configuration: ``All``) |
| 221 | Merge all functions fitting on a single line. |
| 222 | |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 223 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 224 | **AllowShortIfStatementsOnASingleLine** (``bool``) |
| 225 | If ``true``, ``if (a) return;`` can be put on a single |
| 226 | line. |
| 227 | |
| 228 | **AllowShortLoopsOnASingleLine** (``bool``) |
| 229 | If ``true``, ``while (true) continue;`` can be put on a |
| 230 | single line. |
| 231 | |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 232 | **AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``) |
| 233 | The function definition return type breaking style to use. |
Daniel Jasper | ca4ea1c | 2014-08-05 12:16:31 +0000 | [diff] [blame] | 234 | |
Birunthan Mohanathas | a0388a8 | 2015-06-29 15:30:42 +0000 | [diff] [blame] | 235 | Possible values: |
| 236 | |
| 237 | * ``DRTBS_None`` (in configuration: ``None``) |
| 238 | Break after return type automatically. |
| 239 | ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. |
| 240 | * ``DRTBS_All`` (in configuration: ``All``) |
| 241 | Always break after the return type. |
| 242 | * ``DRTBS_TopLevel`` (in configuration: ``TopLevel``) |
| 243 | Always break after the return types of top level functions. |
| 244 | |
Daniel Jasper | ca4ea1c | 2014-08-05 12:16:31 +0000 | [diff] [blame] | 245 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 246 | **AlwaysBreakBeforeMultilineStrings** (``bool``) |
| 247 | If ``true``, always break before multiline string literals. |
| 248 | |
Daniel Jasper | 2aaedd3 | 2015-06-18 09:12:47 +0000 | [diff] [blame] | 249 | This flag is mean to make cases where there are multiple multiline strings |
| 250 | in a file look more consistent. Thus, it will only take effect if wrapping |
| 251 | the string at that point leads to it being indented |
| 252 | ``ContinuationIndentWidth`` spaces from the start of the line. |
| 253 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 254 | **AlwaysBreakTemplateDeclarations** (``bool``) |
| 255 | If ``true``, always break after the ``template<...>`` of a |
| 256 | template declaration. |
| 257 | |
Daniel Jasper | 18210d7 | 2014-10-09 09:52:05 +0000 | [diff] [blame] | 258 | **BinPackArguments** (``bool``) |
| 259 | If ``false``, a function call's arguments will either be all on the |
| 260 | same line or will have one line each. |
| 261 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 262 | **BinPackParameters** (``bool``) |
Daniel Jasper | 18210d7 | 2014-10-09 09:52:05 +0000 | [diff] [blame] | 263 | If ``false``, a function declaration's or function definition's |
| 264 | parameters will either all be on the same line or will have one line each. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 265 | |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 266 | **BraceWrapping** (``BraceWrappingFlags``) |
| 267 | Control of individual brace wrapping cases. |
| 268 | |
| 269 | If ``BreakBeforeBraces`` is set to ``custom``, use this to specify how each |
| 270 | individual brace case should be handled. Otherwise, this is ignored. |
| 271 | |
| 272 | Nested configuration flags: |
| 273 | |
| 274 | * ``bool AfterClass`` Wrap class definitions. |
| 275 | * ``bool AfterControlStatement`` Wrap control statements (if/for/while/switch/..). |
| 276 | * ``bool AfterEnum`` Wrap enum definitions. |
| 277 | * ``bool AfterFunction`` Wrap function definitions. |
| 278 | * ``bool AfterNamespace`` Wrap namespace definitions. |
| 279 | * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (@autoreleasepool, interfaces, ..). |
| 280 | * ``bool AfterStruct`` Wrap struct definitions. |
| 281 | * ``bool AfterUnion`` Wrap union definitions. |
| 282 | * ``bool BeforeCatch`` Wrap before ``catch``. |
| 283 | * ``bool BeforeElse`` Wrap before ``else``. |
| 284 | * ``bool IndentBraces`` Indent the wrapped braces themselves. |
| 285 | |
| 286 | |
Daniel Jasper | ac043c9 | 2014-09-15 11:11:00 +0000 | [diff] [blame] | 287 | **BreakBeforeBinaryOperators** (``BinaryOperatorStyle``) |
| 288 | The way to wrap binary operators. |
| 289 | |
| 290 | Possible values: |
| 291 | |
| 292 | * ``BOS_None`` (in configuration: ``None``) |
| 293 | Break after operators. |
| 294 | * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``) |
| 295 | Break before operators that aren't assignments. |
| 296 | * ``BOS_All`` (in configuration: ``All``) |
| 297 | Break before operators. |
| 298 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 299 | |
| 300 | **BreakBeforeBraces** (``BraceBreakingStyle``) |
| 301 | The brace breaking style to use. |
| 302 | |
| 303 | Possible values: |
| 304 | |
| 305 | * ``BS_Attach`` (in configuration: ``Attach``) |
| 306 | Always attach braces to surrounding context. |
| 307 | * ``BS_Linux`` (in configuration: ``Linux``) |
| 308 | Like ``Attach``, but break before braces on function, namespace and |
| 309 | class definitions. |
Birunthan Mohanathas | 305fa9c | 2015-07-12 03:13:54 +0000 | [diff] [blame] | 310 | * ``BS_Mozilla`` (in configuration: ``Mozilla``) |
| 311 | Like ``Attach``, but break before braces on enum, function, and record |
| 312 | definitions. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 313 | * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) |
Roman Kashitsyn | 291f64f | 2015-08-10 13:43:19 +0000 | [diff] [blame] | 314 | Like ``Attach``, but break before function definitions, 'catch', and 'else'. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 315 | * ``BS_Allman`` (in configuration: ``Allman``) |
| 316 | Always break before braces. |
Daniel Jasper | ee107ad | 2014-02-13 12:51:50 +0000 | [diff] [blame] | 317 | * ``BS_GNU`` (in configuration: ``GNU``) |
| 318 | Always break before braces and add an extra level of indentation to |
| 319 | braces of control statements, not to those of class, function |
| 320 | or other definitions. |
Roman Kashitsyn | 291f64f | 2015-08-10 13:43:19 +0000 | [diff] [blame] | 321 | * ``BS_WebKit`` (in configuration: ``WebKit``) |
| 322 | Like ``Attach``, but break before functions. |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 323 | * ``BS_Custom`` (in configuration: ``Custom``) |
| 324 | Configure each individual brace in ``BraceWrapping``. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 325 | |
| 326 | |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 327 | **BreakBeforeTernaryOperators** (``bool``) |
| 328 | If ``true``, ternary operators will be placed after line breaks. |
| 329 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 330 | **BreakConstructorInitializersBeforeComma** (``bool``) |
| 331 | Always break constructor initializers before commas and align |
| 332 | the commas with the colon. |
| 333 | |
| 334 | **ColumnLimit** (``unsigned``) |
| 335 | The column limit. |
| 336 | |
| 337 | A column limit of ``0`` means that there is no column limit. In this case, |
| 338 | clang-format will respect the input's line breaking decisions within |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 339 | statements unless they contradict other rules. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 340 | |
Daniel Jasper | ee107ad | 2014-02-13 12:51:50 +0000 | [diff] [blame] | 341 | **CommentPragmas** (``std::string``) |
| 342 | A regular expression that describes comments with special meaning, |
| 343 | which should not be split into lines or otherwise changed. |
| 344 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 345 | **ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``) |
| 346 | If the constructor initializers don't fit on a line, put each |
| 347 | initializer on its own line. |
| 348 | |
| 349 | **ConstructorInitializerIndentWidth** (``unsigned``) |
| 350 | The number of characters to use for indentation of constructor |
| 351 | initializer lists. |
| 352 | |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 353 | **ContinuationIndentWidth** (``unsigned``) |
| 354 | Indent width for line continuations. |
| 355 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 356 | **Cpp11BracedListStyle** (``bool``) |
| 357 | If ``true``, format braced lists as best suited for C++11 braced |
| 358 | lists. |
| 359 | |
| 360 | Important differences: |
| 361 | - No spaces inside the braced list. |
| 362 | - No line break before the closing brace. |
| 363 | - Indentation with the continuation indent, not with the block indent. |
| 364 | |
| 365 | Fundamentally, C++11 braced lists are formatted exactly like function |
| 366 | calls would be formatted in their place. If the braced list follows a name |
| 367 | (e.g. a type or variable name), clang-format formats as if the ``{}`` were |
| 368 | the parentheses of a function call with that name. If there is no name, |
| 369 | a zero-length name is assumed. |
| 370 | |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 371 | **DerivePointerAlignment** (``bool``) |
| 372 | If ``true``, analyze the formatted file for the most common |
Daniel Jasper | 7f43266 | 2014-12-02 14:21:16 +0000 | [diff] [blame] | 373 | alignment of & and \*. ``PointerAlignment`` is then used only as fallback. |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 374 | |
| 375 | **DisableFormat** (``bool``) |
Birunthan Mohanathas | b744e72 | 2015-06-27 09:25:28 +0000 | [diff] [blame] | 376 | Disables formatting completely. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 377 | |
| 378 | **ExperimentalAutoDetectBinPacking** (``bool``) |
| 379 | If ``true``, clang-format detects whether function calls and |
| 380 | definitions are formatted with one parameter per line. |
| 381 | |
| 382 | Each call can be bin-packed, one-per-line or inconclusive. If it is |
| 383 | inconclusive, e.g. completely on one line, but a decision needs to be |
| 384 | made, clang-format analyzes whether there are other bin-packed cases in |
| 385 | the input file and act accordingly. |
| 386 | |
| 387 | NOTE: This is an experimental flag, that might go away or be renamed. Do |
| 388 | not use this in config files, etc. Use at your own risk. |
| 389 | |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 390 | **ForEachMacros** (``std::vector<std::string>``) |
Daniel Jasper | b552482 | 2014-04-09 14:05:49 +0000 | [diff] [blame] | 391 | A vector of macros that should be interpreted as foreach loops |
| 392 | instead of as function calls. |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 393 | |
Daniel Jasper | b552482 | 2014-04-09 14:05:49 +0000 | [diff] [blame] | 394 | These are expected to be macros of the form: |
Daniel Jasper | b5bda7c | 2015-10-06 12:11:51 +0000 | [diff] [blame] | 395 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 396 | .. code-block:: c++ |
| 397 | FOREACH(<variable-declaration>, ...) |
| 398 | <loop-body> |
| 399 | |
| 400 | In the .clang-format configuration file, this can be configured like: |
Daniel Jasper | b5bda7c | 2015-10-06 12:11:51 +0000 | [diff] [blame] | 401 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 402 | .. code-block:: c++ |
| 403 | ForEachMacros: ['RANGES_FOR', 'FOREACH'] |
Daniel Jasper | b552482 | 2014-04-09 14:05:49 +0000 | [diff] [blame] | 404 | |
| 405 | For example: BOOST_FOREACH. |
Daniel Jasper | e1e4319 | 2014-04-01 12:55:11 +0000 | [diff] [blame] | 406 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 407 | **IncludeCategories** (``std::vector<IncludeCategory>``) |
Daniel Jasper | c1bc38e | 2015-09-29 14:57:55 +0000 | [diff] [blame] | 408 | Regular expressions denoting the different #include categories used |
| 409 | for ordering #includes. |
| 410 | |
| 411 | These regular expressions are matched against the filename of an include |
| 412 | (including the <> or "") in order. The value belonging to the first |
| 413 | matching regular expression is assigned and #includes are sorted first |
| 414 | according to increasing category number and then alphabetically within |
| 415 | each category. |
| 416 | |
| 417 | If none of the regular expressions match, UINT_MAX is assigned as |
| 418 | category. The main header for a source file automatically gets category 0, |
| 419 | so that it is kept at the beginning of the #includes |
| 420 | (http://llvm.org/docs/CodingStandards.html#include-style). |
| 421 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 422 | To configure this in the .clang-format file, use: |
Daniel Jasper | b5bda7c | 2015-10-06 12:11:51 +0000 | [diff] [blame] | 423 | |
Daniel Jasper | 8ce1b8d | 2015-10-06 11:54:18 +0000 | [diff] [blame] | 424 | .. code-block:: c++ |
| 425 | IncludeCategories: |
| 426 | - Regex: '^"(llvm|llvm-c|clang|clang-c)/' |
| 427 | Priority: 2 |
| 428 | - Regex: '^(<|"(gtest|isl|json)/)' |
| 429 | Priority: 3 |
| 430 | - Regex: '.\*' |
| 431 | Priority: 1 |
| 432 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 433 | **IndentCaseLabels** (``bool``) |
| 434 | Indent case labels one level from the switch statement. |
| 435 | |
| 436 | When ``false``, use the same indentation level as for the switch statement. |
| 437 | Switch statement body is always indented one level more than case labels. |
| 438 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 439 | **IndentWidth** (``unsigned``) |
Alexander Kornienko | 45ca09b | 2013-09-27 16:16:55 +0000 | [diff] [blame] | 440 | The number of columns to use for indentation. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 441 | |
Daniel Jasper | c75e1ef | 2014-07-09 08:42:42 +0000 | [diff] [blame] | 442 | **IndentWrappedFunctionNames** (``bool``) |
| 443 | Indent if a function definition or declaration is wrapped after the |
| 444 | type. |
| 445 | |
Daniel Jasper | b552482 | 2014-04-09 14:05:49 +0000 | [diff] [blame] | 446 | **KeepEmptyLinesAtTheStartOfBlocks** (``bool``) |
| 447 | If true, empty lines at the start of blocks are kept. |
| 448 | |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 449 | **Language** (``LanguageKind``) |
| 450 | Language, this format style is targeted at. |
| 451 | |
| 452 | Possible values: |
| 453 | |
| 454 | * ``LK_None`` (in configuration: ``None``) |
| 455 | Do not use. |
| 456 | * ``LK_Cpp`` (in configuration: ``Cpp``) |
| 457 | Should be used for C, C++, ObjectiveC, ObjectiveC++. |
Daniel Jasper | 18210d7 | 2014-10-09 09:52:05 +0000 | [diff] [blame] | 458 | * ``LK_Java`` (in configuration: ``Java``) |
| 459 | Should be used for Java. |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 460 | * ``LK_JavaScript`` (in configuration: ``JavaScript``) |
| 461 | Should be used for JavaScript. |
Daniel Jasper | ee107ad | 2014-02-13 12:51:50 +0000 | [diff] [blame] | 462 | * ``LK_Proto`` (in configuration: ``Proto``) |
| 463 | Should be used for Protocol Buffers |
| 464 | (https://developers.google.com/protocol-buffers/). |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 465 | |
| 466 | |
Birunthan Mohanathas | b001a0b | 2015-07-03 17:25:16 +0000 | [diff] [blame] | 467 | **MacroBlockBegin** (``std::string``) |
| 468 | A regular expression matching macros that start a block. |
| 469 | |
| 470 | **MacroBlockEnd** (``std::string``) |
| 471 | A regular expression matching macros that end a block. |
| 472 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 473 | **MaxEmptyLinesToKeep** (``unsigned``) |
| 474 | The maximum number of consecutive empty lines to keep. |
| 475 | |
| 476 | **NamespaceIndentation** (``NamespaceIndentationKind``) |
| 477 | The indentation used for namespaces. |
| 478 | |
| 479 | Possible values: |
| 480 | |
| 481 | * ``NI_None`` (in configuration: ``None``) |
| 482 | Don't indent in namespaces. |
| 483 | * ``NI_Inner`` (in configuration: ``Inner``) |
| 484 | Indent only in inner namespaces (nested in other namespaces). |
| 485 | * ``NI_All`` (in configuration: ``All``) |
| 486 | Indent in all namespaces. |
| 487 | |
| 488 | |
Daniel Jasper | eb2226e | 2014-10-28 16:56:37 +0000 | [diff] [blame] | 489 | **ObjCBlockIndentWidth** (``unsigned``) |
| 490 | The number of characters to use for indentation of ObjC blocks. |
| 491 | |
Daniel Jasper | ee107ad | 2014-02-13 12:51:50 +0000 | [diff] [blame] | 492 | **ObjCSpaceAfterProperty** (``bool``) |
| 493 | Add a space after ``@property`` in Objective-C, i.e. use |
Daniel Jasper | 17605d3 | 2014-05-14 09:33:35 +0000 | [diff] [blame] | 494 | ``\@property (readonly)`` instead of ``\@property(readonly)``. |
Daniel Jasper | ee107ad | 2014-02-13 12:51:50 +0000 | [diff] [blame] | 495 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 496 | **ObjCSpaceBeforeProtocolList** (``bool``) |
| 497 | Add a space in front of an Objective-C protocol list, i.e. use |
| 498 | ``Foo <Protocol>`` instead of ``Foo<Protocol>``. |
| 499 | |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 500 | **PenaltyBreakBeforeFirstCallParameter** (``unsigned``) |
| 501 | The penalty for breaking a function call after "call(". |
| 502 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 503 | **PenaltyBreakComment** (``unsigned``) |
| 504 | The penalty for each line break introduced inside a comment. |
| 505 | |
| 506 | **PenaltyBreakFirstLessLess** (``unsigned``) |
| 507 | The penalty for breaking before the first ``<<``. |
| 508 | |
| 509 | **PenaltyBreakString** (``unsigned``) |
| 510 | The penalty for each line break introduced inside a string literal. |
| 511 | |
| 512 | **PenaltyExcessCharacter** (``unsigned``) |
| 513 | The penalty for each character outside of the column limit. |
| 514 | |
| 515 | **PenaltyReturnTypeOnItsOwnLine** (``unsigned``) |
| 516 | Penalty for putting the return type of a function onto its own |
| 517 | line. |
| 518 | |
Daniel Jasper | 553d487 | 2014-06-17 12:40:34 +0000 | [diff] [blame] | 519 | **PointerAlignment** (``PointerAlignmentStyle``) |
| 520 | Pointer and reference alignment style. |
| 521 | |
| 522 | Possible values: |
| 523 | |
| 524 | * ``PAS_Left`` (in configuration: ``Left``) |
| 525 | Align pointer to the left. |
| 526 | * ``PAS_Right`` (in configuration: ``Right``) |
| 527 | Align pointer to the right. |
| 528 | * ``PAS_Middle`` (in configuration: ``Middle``) |
| 529 | Align pointer in the middle. |
| 530 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 531 | |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 532 | **SpaceAfterCStyleCast** (``bool``) |
| 533 | If ``true``, a space may be inserted after C style casts. |
| 534 | |
Daniel Jasper | d94bff3 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 535 | **SpaceBeforeAssignmentOperators** (``bool``) |
Alexander Kornienko | 45ca09b | 2013-09-27 16:16:55 +0000 | [diff] [blame] | 536 | If ``false``, spaces will be removed before assignment operators. |
Daniel Jasper | d94bff3 | 2013-09-25 15:15:02 +0000 | [diff] [blame] | 537 | |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 538 | **SpaceBeforeParens** (``SpaceBeforeParensOptions``) |
| 539 | Defines in which cases to put a space before opening parentheses. |
| 540 | |
| 541 | Possible values: |
| 542 | |
| 543 | * ``SBPO_Never`` (in configuration: ``Never``) |
| 544 | Never put a space before opening parentheses. |
| 545 | * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) |
| 546 | Put a space before opening parentheses only after control statement |
| 547 | keywords (``for/if/while...``). |
| 548 | * ``SBPO_Always`` (in configuration: ``Always``) |
| 549 | Always put a space before opening parentheses, except when it's |
| 550 | prohibited by the syntax rules (in function-like macro definitions) or |
| 551 | when determined by other style rules (after unary operators, opening |
| 552 | parentheses, etc.) |
| 553 | |
| 554 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 555 | **SpaceInEmptyParentheses** (``bool``) |
Daniel Jasper | ee107ad | 2014-02-13 12:51:50 +0000 | [diff] [blame] | 556 | If ``true``, spaces may be inserted into '()'. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 557 | |
| 558 | **SpacesBeforeTrailingComments** (``unsigned``) |
Daniel Jasper | c0be760 | 2014-05-15 13:55:19 +0000 | [diff] [blame] | 559 | The number of spaces before trailing line comments |
| 560 | (``//`` - comments). |
Daniel Jasper | b552482 | 2014-04-09 14:05:49 +0000 | [diff] [blame] | 561 | |
Daniel Jasper | c0be760 | 2014-05-15 13:55:19 +0000 | [diff] [blame] | 562 | This does not affect trailing block comments (``/**/`` - comments) as those |
Daniel Jasper | b552482 | 2014-04-09 14:05:49 +0000 | [diff] [blame] | 563 | commonly have different usage patterns and a number of special cases. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 564 | |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 565 | **SpacesInAngles** (``bool``) |
| 566 | If ``true``, spaces will be inserted after '<' and before '>' in |
| 567 | template argument lists |
| 568 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 569 | **SpacesInCStyleCastParentheses** (``bool``) |
Daniel Jasper | ee107ad | 2014-02-13 12:51:50 +0000 | [diff] [blame] | 570 | If ``true``, spaces may be inserted into C style casts. |
| 571 | |
| 572 | **SpacesInContainerLiterals** (``bool``) |
| 573 | If ``true``, spaces are inserted inside container literals (e.g. |
| 574 | ObjC and Javascript array and dict literals). |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 575 | |
| 576 | **SpacesInParentheses** (``bool``) |
Alexander Kornienko | fdca83d | 2013-12-10 10:18:34 +0000 | [diff] [blame] | 577 | If ``true``, spaces will be inserted after '(' and before ')'. |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 578 | |
Daniel Jasper | ad981f8 | 2014-08-26 11:41:14 +0000 | [diff] [blame] | 579 | **SpacesInSquareBrackets** (``bool``) |
Daniel Jasper | b87899b | 2014-09-10 13:11:45 +0000 | [diff] [blame] | 580 | If ``true``, spaces will be inserted after '[' and before ']'. |
Daniel Jasper | ad981f8 | 2014-08-26 11:41:14 +0000 | [diff] [blame] | 581 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 582 | **Standard** (``LanguageStandard``) |
| 583 | Format compatible with this standard, e.g. use |
| 584 | ``A<A<int> >`` instead of ``A<A<int>>`` for LS_Cpp03. |
| 585 | |
| 586 | Possible values: |
| 587 | |
| 588 | * ``LS_Cpp03`` (in configuration: ``Cpp03``) |
| 589 | Use C++03-compatible syntax. |
| 590 | * ``LS_Cpp11`` (in configuration: ``Cpp11``) |
| 591 | Use features of C++11 (e.g. ``A<A<int>>`` instead of |
| 592 | ``A<A<int> >``). |
| 593 | * ``LS_Auto`` (in configuration: ``Auto``) |
| 594 | Automatic detection based on the input. |
| 595 | |
| 596 | |
Alexander Kornienko | 45ca09b | 2013-09-27 16:16:55 +0000 | [diff] [blame] | 597 | **TabWidth** (``unsigned``) |
| 598 | The number of columns used for tab stops. |
| 599 | |
| 600 | **UseTab** (``UseTabStyle``) |
| 601 | The way to use tab characters in the resulting file. |
| 602 | |
| 603 | Possible values: |
| 604 | |
| 605 | * ``UT_Never`` (in configuration: ``Never``) |
| 606 | Never use tab. |
| 607 | * ``UT_ForIndentation`` (in configuration: ``ForIndentation``) |
| 608 | Use tabs only for indentation. |
| 609 | * ``UT_Always`` (in configuration: ``Always``) |
| 610 | Use tabs whenever we need to fill whitespace that spans at least from |
| 611 | one tab stop to the next one. |
| 612 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 613 | |
| 614 | .. END_FORMAT_STYLE_OPTIONS |
| 615 | |
Daniel Jasper | 49d3d58 | 2015-10-05 07:24:55 +0000 | [diff] [blame] | 616 | Adding additional style options |
| 617 | =============================== |
| 618 | |
| 619 | Each additional style option adds costs to the clang-format project. Some of |
| 620 | these costs affect the clang-format developement itself, as we need to make |
| 621 | sure that any given combination of options work and that new features don't |
| 622 | break any of the existing options in any way. There are also costs for end users |
| 623 | as options become less discoverable and people have to think about and make a |
| 624 | decision on options they don't really care about. |
| 625 | |
| 626 | The goal of the clang-format project is more on the side of supporting a |
| 627 | limited set of styles really well as opposed to supporting every single style |
| 628 | used by a codebase somewhere in the wild. Of course, we do want to support all |
| 629 | major projects and thus have established the following bar for adding style |
| 630 | options. Each new style option must .. |
| 631 | |
Daniel Jasper | fcbea71 | 2015-10-05 13:30:42 +0000 | [diff] [blame] | 632 | * be used in a project of significant size (have dozens of contributors) |
| 633 | * have a publicly accessible style guide |
| 634 | * have a person willing to contribute and maintain patches |
Daniel Jasper | 49d3d58 | 2015-10-05 07:24:55 +0000 | [diff] [blame] | 635 | |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 636 | Examples |
| 637 | ======== |
| 638 | |
| 639 | A style similar to the `Linux Kernel style |
| 640 | <https://www.kernel.org/doc/Documentation/CodingStyle>`_: |
| 641 | |
| 642 | .. code-block:: yaml |
| 643 | |
| 644 | BasedOnStyle: LLVM |
| 645 | IndentWidth: 8 |
Alexander Kornienko | 8ba68f6 | 2013-09-27 16:19:25 +0000 | [diff] [blame] | 646 | UseTab: Always |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 647 | BreakBeforeBraces: Linux |
| 648 | AllowShortIfStatementsOnASingleLine: false |
| 649 | IndentCaseLabels: false |
| 650 | |
| 651 | The result is (imagine that tabs are used for indentation here): |
| 652 | |
| 653 | .. code-block:: c++ |
| 654 | |
| 655 | void test() |
| 656 | { |
| 657 | switch (x) { |
| 658 | case 0: |
| 659 | case 1: |
| 660 | do_something(); |
| 661 | break; |
| 662 | case 2: |
| 663 | do_something_else(); |
| 664 | break; |
| 665 | default: |
| 666 | break; |
| 667 | } |
| 668 | if (condition) |
| 669 | do_something_completely_different(); |
| 670 | |
| 671 | if (x == y) { |
| 672 | q(); |
| 673 | } else if (x > y) { |
| 674 | w(); |
| 675 | } else { |
| 676 | r(); |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | A style similar to the default Visual Studio formatting style: |
| 681 | |
| 682 | .. code-block:: yaml |
| 683 | |
Alexander Kornienko | 8ba68f6 | 2013-09-27 16:19:25 +0000 | [diff] [blame] | 684 | UseTab: Never |
Alexander Kornienko | d278e0e | 2013-09-04 15:09:13 +0000 | [diff] [blame] | 685 | IndentWidth: 4 |
| 686 | BreakBeforeBraces: Allman |
| 687 | AllowShortIfStatementsOnASingleLine: false |
| 688 | IndentCaseLabels: false |
| 689 | ColumnLimit: 0 |
| 690 | |
| 691 | The result is: |
| 692 | |
| 693 | .. code-block:: c++ |
| 694 | |
| 695 | void test() |
| 696 | { |
| 697 | switch (suffix) |
| 698 | { |
| 699 | case 0: |
| 700 | case 1: |
| 701 | do_something(); |
| 702 | break; |
| 703 | case 2: |
| 704 | do_something_else(); |
| 705 | break; |
| 706 | default: |
| 707 | break; |
| 708 | } |
| 709 | if (condition) |
| 710 | do_somthing_completely_different(); |
| 711 | |
| 712 | if (x == y) |
| 713 | { |
| 714 | q(); |
| 715 | } |
| 716 | else if (x > y) |
| 717 | { |
| 718 | w(); |
| 719 | } |
| 720 | else |
| 721 | { |
| 722 | r(); |
| 723 | } |
| 724 | } |
| 725 | |