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