1========================== 2Clang-Format Style Options 3========================== 4 5:doc:`ClangFormatStyleOptions` describes configurable formatting style options 6supported by :doc:`LibFormat` and :doc:`ClangFormat`. 7 8When using :program:`clang-format` command line utility or 9``clang::format::reformat(...)`` functions from code, one can either use one of 10the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft) or 11create a custom style by configuring specific style options. 12 13 14Configuring Style with clang-format 15=================================== 16 17:program:`clang-format` supports two ways to provide custom style options: 18directly specify style configuration in the ``-style=`` command line option or 19use ``-style=file`` and put style configuration in the ``.clang-format`` or 20``_clang-format`` file in the project directory. 21 22When using ``-style=file``, :program:`clang-format` for each input file will 23try to find the ``.clang-format`` file located in the closest parent directory 24of the input file. When the standard input is used, the search is started from 25the current directory. 26 27The ``.clang-format`` file uses YAML format: 28 29.. code-block:: yaml 30 31 key1: value1 32 key2: value2 33 # A comment. 34 ... 35 36The configuration file can consist of several sections each having different 37``Language:`` parameter denoting the programming language this section of the 38configuration is targeted at. See the description of the **Language** option 39below for the list of supported languages. The first section may have no 40language set, it will set the default style options for all lanugages. 41Configuration sections for specific language will override options set in the 42default section. 43 44When :program:`clang-format` formats a file, it auto-detects the language using 45the file name. When formatting standard input or a file that doesn't have the 46extension corresponding to its language, ``-assume-filename=`` option can be 47used to override the file name :program:`clang-format` uses to detect the 48language. 49 50An example of a configuration file for multiple languages: 51 52.. code-block:: yaml 53 54 --- 55 # We'll use defaults from the LLVM style, but with 4 columns indentation. 56 BasedOnStyle: LLVM 57 IndentWidth: 4 58 --- 59 Language: Cpp 60 # Force pointers to the type for C++. 61 DerivePointerAlignment: false 62 PointerAlignment: Left 63 --- 64 Language: JavaScript 65 # Use 100 columns for JS. 66 ColumnLimit: 100 67 --- 68 Language: Proto 69 # Don't format .proto files. 70 DisableFormat: true 71 --- 72 Language: CSharp 73 # Use 100 columns for C#. 74 ColumnLimit: 100 75 ... 76 77An easy way to get a valid ``.clang-format`` file containing all configuration 78options of a certain predefined style is: 79 80.. code-block:: console 81 82 clang-format -style=llvm -dump-config > .clang-format 83 84When specifying configuration in the ``-style=`` option, the same configuration 85is applied for all input files. The format of the configuration is: 86 87.. code-block:: console 88 89 -style='{key1: value1, key2: value2, ...}' 90 91 92Disabling Formatting on a Piece of Code 93======================================= 94 95Clang-format understands also special comments that switch formatting in a 96delimited range. The code between a comment ``// clang-format off`` or 97``/* clang-format off */`` up to a comment ``// clang-format on`` or 98``/* clang-format on */`` will not be formatted. The comments themselves 99will be formatted (aligned) normally. 100 101.. code-block:: c++ 102 103 int formatted_code; 104 // clang-format off 105 void unformatted_code ; 106 // clang-format on 107 void formatted_code_again; 108 109 110Configuring Style in Code 111========================= 112 113When using ``clang::format::reformat(...)`` functions, the format is specified 114by supplying the `clang::format::FormatStyle 115<https://clang.llvm.org/doxygen/structclang_1_1format_1_1FormatStyle.html>`_ 116structure. 117 118 119Configurable Format Style Options 120================================= 121 122This section lists the supported style options. Value type is specified for 123each option. For enumeration types possible values are specified both as a C++ 124enumeration member (with a prefix, e.g. ``LS_Auto``), and as a value usable in 125the configuration (without a prefix: ``Auto``). 126 127 128**BasedOnStyle** (``string``) 129 The style used for all options not specifically set in the configuration. 130 131 This option is supported only in the :program:`clang-format` configuration 132 (both within ``-style='{...}'`` and the ``.clang-format`` file). 133 134 Possible values: 135 136 * ``LLVM`` 137 A style complying with the `LLVM coding standards 138 <https://llvm.org/docs/CodingStandards.html>`_ 139 * ``Google`` 140 A style complying with `Google's C++ style guide 141 <https://google.github.io/styleguide/cppguide.html>`_ 142 * ``Chromium`` 143 A style complying with `Chromium's style guide 144 <https://chromium.googlesource.com/chromium/src/+/master/styleguide/styleguide.md>`_ 145 * ``Mozilla`` 146 A style complying with `Mozilla's style guide 147 <https://developer.mozilla.org/en-US/docs/Developer_Guide/Coding_Style>`_ 148 * ``WebKit`` 149 A style complying with `WebKit's style guide 150 <https://www.webkit.org/coding/coding-style.html>`_ 151 * ``Microsoft`` 152 A style complying with `Microsoft's style guide 153 <https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017>`_ 154 * ``GNU`` 155 A style complying with the `GNU coding standards 156 <https://www.gnu.org/prep/standards/standards.html>`_ 157 158.. START_FORMAT_STYLE_OPTIONS 159 160**AccessModifierOffset** (``int``) 161 The extra indent or outdent of access modifiers, e.g. ``public:``. 162 163**AlignAfterOpenBracket** (``BracketAlignmentStyle``) 164 If ``true``, horizontally aligns arguments after an open bracket. 165 166 This applies to round brackets (parentheses), angle brackets and square 167 brackets. 168 169 Possible values: 170 171 * ``BAS_Align`` (in configuration: ``Align``) 172 Align parameters on the open bracket, e.g.: 173 174 .. code-block:: c++ 175 176 someLongFunction(argument1, 177 argument2); 178 179 * ``BAS_DontAlign`` (in configuration: ``DontAlign``) 180 Don't align, instead use ``ContinuationIndentWidth``, e.g.: 181 182 .. code-block:: c++ 183 184 someLongFunction(argument1, 185 argument2); 186 187 * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``) 188 Always break after an open bracket, if the parameters don't fit 189 on a single line, e.g.: 190 191 .. code-block:: c++ 192 193 someLongFunction( 194 argument1, argument2); 195 196 197 198**AlignConsecutiveAssignments** (``bool``) 199 If ``true``, aligns consecutive assignments. 200 201 This will align the assignment operators of consecutive lines. This 202 will result in formattings like 203 204 .. code-block:: c++ 205 206 int aaaa = 12; 207 int b = 23; 208 int ccc = 23; 209 210**AlignConsecutiveBitFields** (``bool``) 211 If ``true``, aligns consecutive bitfield members. 212 213 This will align the bitfield separators of consecutive lines. This 214 will result in formattings like 215 216 .. code-block:: c++ 217 218 int aaaa : 1; 219 int b : 12; 220 int ccc : 8; 221 222**AlignConsecutiveDeclarations** (``bool``) 223 If ``true``, aligns consecutive declarations. 224 225 This will align the declaration names of consecutive lines. This 226 will result in formattings like 227 228 .. code-block:: c++ 229 230 int aaaa = 12; 231 float b = 23; 232 std::string ccc = 23; 233 234**AlignConsecutiveMacros** (``bool``) 235 If ``true``, aligns consecutive C/C++ preprocessor macros. 236 237 This will align C/C++ preprocessor macros of consecutive lines. 238 Will result in formattings like 239 240 .. code-block:: c++ 241 242 #define SHORT_NAME 42 243 #define LONGER_NAME 0x007f 244 #define EVEN_LONGER_NAME (2) 245 #define foo(x) (x * x) 246 #define bar(y, z) (y + z) 247 248**AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``) 249 Options for aligning backslashes in escaped newlines. 250 251 Possible values: 252 253 * ``ENAS_DontAlign`` (in configuration: ``DontAlign``) 254 Don't align escaped newlines. 255 256 .. code-block:: c++ 257 258 #define A \ 259 int aaaa; \ 260 int b; \ 261 int dddddddddd; 262 263 * ``ENAS_Left`` (in configuration: ``Left``) 264 Align escaped newlines as far left as possible. 265 266 .. code-block:: c++ 267 268 true: 269 #define A \ 270 int aaaa; \ 271 int b; \ 272 int dddddddddd; 273 274 false: 275 276 * ``ENAS_Right`` (in configuration: ``Right``) 277 Align escaped newlines in the right-most column. 278 279 .. code-block:: c++ 280 281 #define A \ 282 int aaaa; \ 283 int b; \ 284 int dddddddddd; 285 286 287 288**AlignOperands** (``OperandAlignmentStyle``) 289 If ``true``, horizontally align operands of binary and ternary 290 expressions. 291 292 Possible values: 293 294 * ``OAS_DontAlign`` (in configuration: ``DontAlign``) 295 Do not align operands of binary and ternary expressions. 296 The wrapped lines are indented ``ContinuationIndentWidth`` spaces from 297 the start of the line. 298 299 * ``OAS_Align`` (in configuration: ``Align``) 300 Horizontally align operands of binary and ternary expressions. 301 302 Specifically, this aligns operands of a single expression that needs 303 to be split over multiple lines, e.g.: 304 305 .. code-block:: c++ 306 307 int aaa = bbbbbbbbbbbbbbb + 308 ccccccccccccccc; 309 310 When ``BreakBeforeBinaryOperators`` is set, the wrapped operator is 311 aligned with the operand on the first line. 312 313 .. code-block:: c++ 314 315 int aaa = bbbbbbbbbbbbbbb 316 + ccccccccccccccc; 317 318 * ``OAS_AlignAfterOperator`` (in configuration: ``AlignAfterOperator``) 319 Horizontally align operands of binary and ternary expressions. 320 321 This is similar to ``AO_Align``, except when 322 ``BreakBeforeBinaryOperators`` is set, the operator is un-indented so 323 that the wrapped operand is aligned with the operand on the first line. 324 325 .. code-block:: c++ 326 327 int aaa = bbbbbbbbbbbbbbb 328 + ccccccccccccccc; 329 330 331 332**AlignTrailingComments** (``bool``) 333 If ``true``, aligns trailing comments. 334 335 .. code-block:: c++ 336 337 true: false: 338 int a; // My comment a vs. int a; // My comment a 339 int b = 2; // comment b int b = 2; // comment about b 340 341**AllowAllArgumentsOnNextLine** (``bool``) 342 If a function call or braced initializer list doesn't fit on a 343 line, allow putting all arguments onto the next line, even if 344 ``BinPackArguments`` is ``false``. 345 346 .. code-block:: c++ 347 348 true: 349 callFunction( 350 a, b, c, d); 351 352 false: 353 callFunction(a, 354 b, 355 c, 356 d); 357 358**AllowAllConstructorInitializersOnNextLine** (``bool``) 359 If a constructor definition with a member initializer list doesn't 360 fit on a single line, allow putting all member initializers onto the next 361 line, if ```ConstructorInitializerAllOnOneLineOrOnePerLine``` is true. 362 Note that this parameter has no effect if 363 ```ConstructorInitializerAllOnOneLineOrOnePerLine``` is false. 364 365 .. code-block:: c++ 366 367 true: 368 MyClass::MyClass() : 369 member0(0), member1(2) {} 370 371 false: 372 MyClass::MyClass() : 373 member0(0), 374 member1(2) {} 375 376**AllowAllParametersOfDeclarationOnNextLine** (``bool``) 377 If the function declaration doesn't fit on a line, 378 allow putting all parameters of a function declaration onto 379 the next line even if ``BinPackParameters`` is ``false``. 380 381 .. code-block:: c++ 382 383 true: 384 void myFunction( 385 int a, int b, int c, int d, int e); 386 387 false: 388 void myFunction(int a, 389 int b, 390 int c, 391 int d, 392 int e); 393 394**AllowShortBlocksOnASingleLine** (``ShortBlockStyle``) 395 Dependent on the value, ``while (true) { continue; }`` can be put on a 396 single line. 397 398 Possible values: 399 400 * ``SBS_Never`` (in configuration: ``Never``) 401 Never merge blocks into a single line. 402 403 .. code-block:: c++ 404 405 while (true) { 406 } 407 while (true) { 408 continue; 409 } 410 411 * ``SBS_Empty`` (in configuration: ``Empty``) 412 Only merge empty blocks. 413 414 .. code-block:: c++ 415 416 while (true) {} 417 while (true) { 418 continue; 419 } 420 421 * ``SBS_Always`` (in configuration: ``Always``) 422 Always merge short blocks into a single line. 423 424 .. code-block:: c++ 425 426 while (true) {} 427 while (true) { continue; } 428 429 430 431**AllowShortCaseLabelsOnASingleLine** (``bool``) 432 If ``true``, short case labels will be contracted to a single line. 433 434 .. code-block:: c++ 435 436 true: false: 437 switch (a) { vs. switch (a) { 438 case 1: x = 1; break; case 1: 439 case 2: return; x = 1; 440 } break; 441 case 2: 442 return; 443 } 444 445**AllowShortEnumsOnASingleLine** (``bool``) 446 Allow short enums on a single line. 447 448 .. code-block:: c++ 449 450 true: 451 enum { A, B } myEnum; 452 453 false: 454 enum 455 { 456 A, 457 B 458 } myEnum; 459 460**AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``) 461 Dependent on the value, ``int f() { return 0; }`` can be put on a 462 single line. 463 464 Possible values: 465 466 * ``SFS_None`` (in configuration: ``None``) 467 Never merge functions into a single line. 468 469 * ``SFS_InlineOnly`` (in configuration: ``InlineOnly``) 470 Only merge functions defined inside a class. Same as "inline", 471 except it does not implies "empty": i.e. top level empty functions 472 are not merged either. 473 474 .. code-block:: c++ 475 476 class Foo { 477 void f() { foo(); } 478 }; 479 void f() { 480 foo(); 481 } 482 void f() { 483 } 484 485 * ``SFS_Empty`` (in configuration: ``Empty``) 486 Only merge empty functions. 487 488 .. code-block:: c++ 489 490 void f() {} 491 void f2() { 492 bar2(); 493 } 494 495 * ``SFS_Inline`` (in configuration: ``Inline``) 496 Only merge functions defined inside a class. Implies "empty". 497 498 .. code-block:: c++ 499 500 class Foo { 501 void f() { foo(); } 502 }; 503 void f() { 504 foo(); 505 } 506 void f() {} 507 508 * ``SFS_All`` (in configuration: ``All``) 509 Merge all functions fitting on a single line. 510 511 .. code-block:: c++ 512 513 class Foo { 514 void f() { foo(); } 515 }; 516 void f() { bar(); } 517 518 519 520**AllowShortIfStatementsOnASingleLine** (``ShortIfStyle``) 521 If ``true``, ``if (a) return;`` can be put on a single line. 522 523 Possible values: 524 525 * ``SIS_Never`` (in configuration: ``Never``) 526 Never put short ifs on the same line. 527 528 .. code-block:: c++ 529 530 if (a) 531 return ; 532 else { 533 return; 534 } 535 536 * ``SIS_WithoutElse`` (in configuration: ``WithoutElse``) 537 Without else put short ifs on the same line only if 538 the else is not a compound statement. 539 540 .. code-block:: c++ 541 542 if (a) return; 543 else 544 return; 545 546 * ``SIS_Always`` (in configuration: ``Always``) 547 Always put short ifs on the same line if 548 the else is not a compound statement or not. 549 550 .. code-block:: c++ 551 552 if (a) return; 553 else { 554 return; 555 } 556 557 558 559**AllowShortLambdasOnASingleLine** (``ShortLambdaStyle``) 560 Dependent on the value, ``auto lambda []() { return 0; }`` can be put on a 561 single line. 562 563 Possible values: 564 565 * ``SLS_None`` (in configuration: ``None``) 566 Never merge lambdas into a single line. 567 568 * ``SLS_Empty`` (in configuration: ``Empty``) 569 Only merge empty lambdas. 570 571 .. code-block:: c++ 572 573 auto lambda = [](int a) {} 574 auto lambda2 = [](int a) { 575 return a; 576 }; 577 578 * ``SLS_Inline`` (in configuration: ``Inline``) 579 Merge lambda into a single line if argument of a function. 580 581 .. code-block:: c++ 582 583 auto lambda = [](int a) { 584 return a; 585 }; 586 sort(a.begin(), a.end(), ()[] { return x < y; }) 587 588 * ``SLS_All`` (in configuration: ``All``) 589 Merge all lambdas fitting on a single line. 590 591 .. code-block:: c++ 592 593 auto lambda = [](int a) {} 594 auto lambda2 = [](int a) { return a; }; 595 596 597 598**AllowShortLoopsOnASingleLine** (``bool``) 599 If ``true``, ``while (true) continue;`` can be put on a single 600 line. 601 602**AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``) 603 The function definition return type breaking style to use. This 604 option is **deprecated** and is retained for backwards compatibility. 605 606 Possible values: 607 608 * ``DRTBS_None`` (in configuration: ``None``) 609 Break after return type automatically. 610 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. 611 612 * ``DRTBS_All`` (in configuration: ``All``) 613 Always break after the return type. 614 615 * ``DRTBS_TopLevel`` (in configuration: ``TopLevel``) 616 Always break after the return types of top-level functions. 617 618 619 620**AlwaysBreakAfterReturnType** (``ReturnTypeBreakingStyle``) 621 The function declaration return type breaking style to use. 622 623 Possible values: 624 625 * ``RTBS_None`` (in configuration: ``None``) 626 Break after return type automatically. 627 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. 628 629 .. code-block:: c++ 630 631 class A { 632 int f() { return 0; }; 633 }; 634 int f(); 635 int f() { return 1; } 636 637 * ``RTBS_All`` (in configuration: ``All``) 638 Always break after the return type. 639 640 .. code-block:: c++ 641 642 class A { 643 int 644 f() { 645 return 0; 646 }; 647 }; 648 int 649 f(); 650 int 651 f() { 652 return 1; 653 } 654 655 * ``RTBS_TopLevel`` (in configuration: ``TopLevel``) 656 Always break after the return types of top-level functions. 657 658 .. code-block:: c++ 659 660 class A { 661 int f() { return 0; }; 662 }; 663 int 664 f(); 665 int 666 f() { 667 return 1; 668 } 669 670 * ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``) 671 Always break after the return type of function definitions. 672 673 .. code-block:: c++ 674 675 class A { 676 int 677 f() { 678 return 0; 679 }; 680 }; 681 int f(); 682 int 683 f() { 684 return 1; 685 } 686 687 * ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``) 688 Always break after the return type of top-level definitions. 689 690 .. code-block:: c++ 691 692 class A { 693 int f() { return 0; }; 694 }; 695 int f(); 696 int 697 f() { 698 return 1; 699 } 700 701 702 703**AlwaysBreakBeforeMultilineStrings** (``bool``) 704 If ``true``, always break before multiline string literals. 705 706 This flag is mean to make cases where there are multiple multiline strings 707 in a file look more consistent. Thus, it will only take effect if wrapping 708 the string at that point leads to it being indented 709 ``ContinuationIndentWidth`` spaces from the start of the line. 710 711 .. code-block:: c++ 712 713 true: false: 714 aaaa = vs. aaaa = "bbbb" 715 "bbbb" "cccc"; 716 "cccc"; 717 718**AlwaysBreakTemplateDeclarations** (``BreakTemplateDeclarationsStyle``) 719 The template declaration breaking style to use. 720 721 Possible values: 722 723 * ``BTDS_No`` (in configuration: ``No``) 724 Do not force break before declaration. 725 ``PenaltyBreakTemplateDeclaration`` is taken into account. 726 727 .. code-block:: c++ 728 729 template <typename T> T foo() { 730 } 731 template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa, 732 int bbbbbbbbbbbbbbbbbbbbb) { 733 } 734 735 * ``BTDS_MultiLine`` (in configuration: ``MultiLine``) 736 Force break after template declaration only when the following 737 declaration spans multiple lines. 738 739 .. code-block:: c++ 740 741 template <typename T> T foo() { 742 } 743 template <typename T> 744 T foo(int aaaaaaaaaaaaaaaaaaaaa, 745 int bbbbbbbbbbbbbbbbbbbbb) { 746 } 747 748 * ``BTDS_Yes`` (in configuration: ``Yes``) 749 Always break after template declaration. 750 751 .. code-block:: c++ 752 753 template <typename T> 754 T foo() { 755 } 756 template <typename T> 757 T foo(int aaaaaaaaaaaaaaaaaaaaa, 758 int bbbbbbbbbbbbbbbbbbbbb) { 759 } 760 761 762 763**AttributeMacros** (``std::vector<std::string>``) 764 A vector of strings that should be interpreted as attributes/qualifiers 765 instead of identifiers. This can be useful for language extensions or 766 static analyzer annotations. 767 768 For example: 769 770 .. code-block:: c++ 771 772 x = (char *__capability)&y; 773 int function(void) __ununsed; 774 void only_writes_to_buffer(char *__output buffer); 775 776 In the .clang-format configuration file, this can be configured like: 777 778 .. code-block:: yaml 779 780 AttributeMacros: ['__capability', '__output', '__ununsed'] 781 782**BinPackArguments** (``bool``) 783 If ``false``, a function call's arguments will either be all on the 784 same line or will have one line each. 785 786 .. code-block:: c++ 787 788 true: 789 void f() { 790 f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, 791 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 792 } 793 794 false: 795 void f() { 796 f(aaaaaaaaaaaaaaaaaaaa, 797 aaaaaaaaaaaaaaaaaaaa, 798 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 799 } 800 801**BinPackParameters** (``bool``) 802 If ``false``, a function declaration's or function definition's 803 parameters will either all be on the same line or will have one line each. 804 805 .. code-block:: c++ 806 807 true: 808 void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa, 809 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 810 811 false: 812 void f(int aaaaaaaaaaaaaaaaaaaa, 813 int aaaaaaaaaaaaaaaaaaaa, 814 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 815 816**BitFieldColonSpacing** (``BitFieldColonSpacingStyle``) 817 The BitFieldColonSpacingStyle to use for bitfields. 818 819 Possible values: 820 821 * ``BFCS_Both`` (in configuration: ``Both``) 822 Add one space on each side of the ``:`` 823 824 .. code-block:: c++ 825 826 unsigned bf : 2; 827 828 * ``BFCS_None`` (in configuration: ``None``) 829 Add no space around the ``:`` (except when needed for 830 ``AlignConsecutiveBitFields``). 831 832 .. code-block:: c++ 833 834 unsigned bf:2; 835 836 * ``BFCS_Before`` (in configuration: ``Before``) 837 Add space before the ``:`` only 838 839 .. code-block:: c++ 840 841 unsigned bf :2; 842 843 * ``BFCS_After`` (in configuration: ``After``) 844 Add space after the ``:`` only (space may be added before if 845 needed for ``AlignConsecutiveBitFields``). 846 847 .. code-block:: c++ 848 849 unsigned bf: 2; 850 851 852 853**BraceWrapping** (``BraceWrappingFlags``) 854 Control of individual brace wrapping cases. 855 856 If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how 857 each individual brace case should be handled. Otherwise, this is ignored. 858 859 .. code-block:: yaml 860 861 # Example of usage: 862 BreakBeforeBraces: Custom 863 BraceWrapping: 864 AfterEnum: true 865 AfterStruct: false 866 SplitEmptyFunction: false 867 868 Nested configuration flags: 869 870 871 * ``bool AfterCaseLabel`` Wrap case labels. 872 873 .. code-block:: c++ 874 875 false: true: 876 switch (foo) { vs. switch (foo) { 877 case 1: { case 1: 878 bar(); { 879 break; bar(); 880 } break; 881 default: { } 882 plop(); default: 883 } { 884 } plop(); 885 } 886 } 887 888 * ``bool AfterClass`` Wrap class definitions. 889 890 .. code-block:: c++ 891 892 true: 893 class foo {}; 894 895 false: 896 class foo 897 {}; 898 899 * ``BraceWrappingAfterControlStatementStyle AfterControlStatement`` 900 Wrap control statements (``if``/``for``/``while``/``switch``/..). 901 902 Possible values: 903 904 * ``BWACS_Never`` (in configuration: ``Never``) 905 Never wrap braces after a control statement. 906 907 .. code-block:: c++ 908 909 if (foo()) { 910 } else { 911 } 912 for (int i = 0; i < 10; ++i) { 913 } 914 915 * ``BWACS_MultiLine`` (in configuration: ``MultiLine``) 916 Only wrap braces after a multi-line control statement. 917 918 .. code-block:: c++ 919 920 if (foo && bar && 921 baz) 922 { 923 quux(); 924 } 925 while (foo || bar) { 926 } 927 928 * ``BWACS_Always`` (in configuration: ``Always``) 929 Always wrap braces after a control statement. 930 931 .. code-block:: c++ 932 933 if (foo()) 934 { 935 } else 936 {} 937 for (int i = 0; i < 10; ++i) 938 {} 939 940 941 * ``bool AfterEnum`` Wrap enum definitions. 942 943 .. code-block:: c++ 944 945 true: 946 enum X : int 947 { 948 B 949 }; 950 951 false: 952 enum X : int { B }; 953 954 * ``bool AfterFunction`` Wrap function definitions. 955 956 .. code-block:: c++ 957 958 true: 959 void foo() 960 { 961 bar(); 962 bar2(); 963 } 964 965 false: 966 void foo() { 967 bar(); 968 bar2(); 969 } 970 971 * ``bool AfterNamespace`` Wrap namespace definitions. 972 973 .. code-block:: c++ 974 975 true: 976 namespace 977 { 978 int foo(); 979 int bar(); 980 } 981 982 false: 983 namespace { 984 int foo(); 985 int bar(); 986 } 987 988 * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (interfaces, implementations...). 989 @autoreleasepool and @synchronized blocks are wrapped 990 according to `AfterControlStatement` flag. 991 992 * ``bool AfterStruct`` Wrap struct definitions. 993 994 .. code-block:: c++ 995 996 true: 997 struct foo 998 { 999 int x; 1000 }; 1001 1002 false: 1003 struct foo { 1004 int x; 1005 }; 1006 1007 * ``bool AfterUnion`` Wrap union definitions. 1008 1009 .. code-block:: c++ 1010 1011 true: 1012 union foo 1013 { 1014 int x; 1015 } 1016 1017 false: 1018 union foo { 1019 int x; 1020 } 1021 1022 * ``bool AfterExternBlock`` Wrap extern blocks. 1023 1024 .. code-block:: c++ 1025 1026 true: 1027 extern "C" 1028 { 1029 int foo(); 1030 } 1031 1032 false: 1033 extern "C" { 1034 int foo(); 1035 } 1036 1037 * ``bool BeforeCatch`` Wrap before ``catch``. 1038 1039 .. code-block:: c++ 1040 1041 true: 1042 try { 1043 foo(); 1044 } 1045 catch () { 1046 } 1047 1048 false: 1049 try { 1050 foo(); 1051 } catch () { 1052 } 1053 1054 * ``bool BeforeElse`` Wrap before ``else``. 1055 1056 .. code-block:: c++ 1057 1058 true: 1059 if (foo()) { 1060 } 1061 else { 1062 } 1063 1064 false: 1065 if (foo()) { 1066 } else { 1067 } 1068 1069 * ``bool BeforeLambdaBody`` Wrap lambda block. 1070 1071 .. code-block:: c++ 1072 1073 true: 1074 connect( 1075 []() 1076 { 1077 foo(); 1078 bar(); 1079 }); 1080 1081 false: 1082 connect([]() { 1083 foo(); 1084 bar(); 1085 }); 1086 1087 * ``bool BeforeWhile`` Wrap before ``while``. 1088 1089 .. code-block:: c++ 1090 1091 true: 1092 do { 1093 foo(); 1094 } 1095 while (1); 1096 1097 false: 1098 do { 1099 foo(); 1100 } while (1); 1101 1102 * ``bool IndentBraces`` Indent the wrapped braces themselves. 1103 1104 * ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line. 1105 This option is used only if the opening brace of the function has 1106 already been wrapped, i.e. the `AfterFunction` brace wrapping mode is 1107 set, and the function could/should not be put on a single line (as per 1108 `AllowShortFunctionsOnASingleLine` and constructor formatting options). 1109 1110 .. code-block:: c++ 1111 1112 int f() vs. int f() 1113 {} { 1114 } 1115 1116 * ``bool SplitEmptyRecord`` If ``false``, empty record (e.g. class, struct or union) body 1117 can be put on a single line. This option is used only if the opening 1118 brace of the record has already been wrapped, i.e. the `AfterClass` 1119 (for classes) brace wrapping mode is set. 1120 1121 .. code-block:: c++ 1122 1123 class Foo vs. class Foo 1124 {} { 1125 } 1126 1127 * ``bool SplitEmptyNamespace`` If ``false``, empty namespace body can be put on a single line. 1128 This option is used only if the opening brace of the namespace has 1129 already been wrapped, i.e. the `AfterNamespace` brace wrapping mode is 1130 set. 1131 1132 .. code-block:: c++ 1133 1134 namespace Foo vs. namespace Foo 1135 {} { 1136 } 1137 1138 1139**BreakAfterJavaFieldAnnotations** (``bool``) 1140 Break after each annotation on a field in Java files. 1141 1142 .. code-block:: java 1143 1144 true: false: 1145 @Partial vs. @Partial @Mock DataLoad loader; 1146 @Mock 1147 DataLoad loader; 1148 1149**BreakBeforeBinaryOperators** (``BinaryOperatorStyle``) 1150 The way to wrap binary operators. 1151 1152 Possible values: 1153 1154 * ``BOS_None`` (in configuration: ``None``) 1155 Break after operators. 1156 1157 .. code-block:: c++ 1158 1159 LooooooooooongType loooooooooooooooooooooongVariable = 1160 someLooooooooooooooooongFunction(); 1161 1162 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + 1163 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == 1164 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && 1165 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > 1166 ccccccccccccccccccccccccccccccccccccccccc; 1167 1168 * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``) 1169 Break before operators that aren't assignments. 1170 1171 .. code-block:: c++ 1172 1173 LooooooooooongType loooooooooooooooooooooongVariable = 1174 someLooooooooooooooooongFunction(); 1175 1176 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1177 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1178 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1179 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1180 > ccccccccccccccccccccccccccccccccccccccccc; 1181 1182 * ``BOS_All`` (in configuration: ``All``) 1183 Break before operators. 1184 1185 .. code-block:: c++ 1186 1187 LooooooooooongType loooooooooooooooooooooongVariable 1188 = someLooooooooooooooooongFunction(); 1189 1190 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1191 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1192 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1193 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1194 > ccccccccccccccccccccccccccccccccccccccccc; 1195 1196 1197 1198**BreakBeforeBraces** (``BraceBreakingStyle``) 1199 The brace breaking style to use. 1200 1201 Possible values: 1202 1203 * ``BS_Attach`` (in configuration: ``Attach``) 1204 Always attach braces to surrounding context. 1205 1206 .. code-block:: c++ 1207 1208 try { 1209 foo(); 1210 } catch () { 1211 } 1212 void foo() { bar(); } 1213 class foo {}; 1214 if (foo()) { 1215 } else { 1216 } 1217 enum X : int { A, B }; 1218 1219 * ``BS_Linux`` (in configuration: ``Linux``) 1220 Like ``Attach``, but break before braces on function, namespace and 1221 class definitions. 1222 1223 .. code-block:: c++ 1224 1225 try { 1226 foo(); 1227 } catch () { 1228 } 1229 void foo() { bar(); } 1230 class foo 1231 { 1232 }; 1233 if (foo()) { 1234 } else { 1235 } 1236 enum X : int { A, B }; 1237 1238 * ``BS_Mozilla`` (in configuration: ``Mozilla``) 1239 Like ``Attach``, but break before braces on enum, function, and record 1240 definitions. 1241 1242 .. code-block:: c++ 1243 1244 try { 1245 foo(); 1246 } catch () { 1247 } 1248 void foo() { bar(); } 1249 class foo 1250 { 1251 }; 1252 if (foo()) { 1253 } else { 1254 } 1255 enum X : int { A, B }; 1256 1257 * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) 1258 Like ``Attach``, but break before function definitions, ``catch``, and 1259 ``else``. 1260 1261 .. code-block:: c++ 1262 1263 try { 1264 foo(); 1265 } 1266 catch () { 1267 } 1268 void foo() { bar(); } 1269 class foo { 1270 }; 1271 if (foo()) { 1272 } 1273 else { 1274 } 1275 enum X : int { A, B }; 1276 1277 * ``BS_Allman`` (in configuration: ``Allman``) 1278 Always break before braces. 1279 1280 .. code-block:: c++ 1281 1282 try 1283 { 1284 foo(); 1285 } 1286 catch () 1287 { 1288 } 1289 void foo() { bar(); } 1290 class foo 1291 { 1292 }; 1293 if (foo()) 1294 { 1295 } 1296 else 1297 { 1298 } 1299 enum X : int 1300 { 1301 A, 1302 B 1303 }; 1304 1305 * ``BS_Whitesmiths`` (in configuration: ``Whitesmiths``) 1306 Like ``Allman`` but always indent braces and line up code with braces. 1307 1308 .. code-block:: c++ 1309 1310 try 1311 { 1312 foo(); 1313 } 1314 catch () 1315 { 1316 } 1317 void foo() { bar(); } 1318 class foo 1319 { 1320 }; 1321 if (foo()) 1322 { 1323 } 1324 else 1325 { 1326 } 1327 enum X : int 1328 { 1329 A, 1330 B 1331 }; 1332 1333 * ``BS_GNU`` (in configuration: ``GNU``) 1334 Always break before braces and add an extra level of indentation to 1335 braces of control statements, not to those of class, function 1336 or other definitions. 1337 1338 .. code-block:: c++ 1339 1340 try 1341 { 1342 foo(); 1343 } 1344 catch () 1345 { 1346 } 1347 void foo() { bar(); } 1348 class foo 1349 { 1350 }; 1351 if (foo()) 1352 { 1353 } 1354 else 1355 { 1356 } 1357 enum X : int 1358 { 1359 A, 1360 B 1361 }; 1362 1363 * ``BS_WebKit`` (in configuration: ``WebKit``) 1364 Like ``Attach``, but break before functions. 1365 1366 .. code-block:: c++ 1367 1368 try { 1369 foo(); 1370 } catch () { 1371 } 1372 void foo() { bar(); } 1373 class foo { 1374 }; 1375 if (foo()) { 1376 } else { 1377 } 1378 enum X : int { A, B }; 1379 1380 * ``BS_Custom`` (in configuration: ``Custom``) 1381 Configure each individual brace in `BraceWrapping`. 1382 1383 1384 1385**BreakBeforeTernaryOperators** (``bool``) 1386 If ``true``, ternary operators will be placed after line breaks. 1387 1388 .. code-block:: c++ 1389 1390 true: 1391 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription 1392 ? firstValue 1393 : SecondValueVeryVeryVeryVeryLong; 1394 1395 false: 1396 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ? 1397 firstValue : 1398 SecondValueVeryVeryVeryVeryLong; 1399 1400**BreakConstructorInitializers** (``BreakConstructorInitializersStyle``) 1401 The constructor initializers style to use. 1402 1403 Possible values: 1404 1405 * ``BCIS_BeforeColon`` (in configuration: ``BeforeColon``) 1406 Break constructor initializers before the colon and after the commas. 1407 1408 .. code-block:: c++ 1409 1410 Constructor() 1411 : initializer1(), 1412 initializer2() 1413 1414 * ``BCIS_BeforeComma`` (in configuration: ``BeforeComma``) 1415 Break constructor initializers before the colon and commas, and align 1416 the commas with the colon. 1417 1418 .. code-block:: c++ 1419 1420 Constructor() 1421 : initializer1() 1422 , initializer2() 1423 1424 * ``BCIS_AfterColon`` (in configuration: ``AfterColon``) 1425 Break constructor initializers after the colon and commas. 1426 1427 .. code-block:: c++ 1428 1429 Constructor() : 1430 initializer1(), 1431 initializer2() 1432 1433 1434 1435**BreakInheritanceList** (``BreakInheritanceListStyle``) 1436 The inheritance list style to use. 1437 1438 Possible values: 1439 1440 * ``BILS_BeforeColon`` (in configuration: ``BeforeColon``) 1441 Break inheritance list before the colon and after the commas. 1442 1443 .. code-block:: c++ 1444 1445 class Foo 1446 : Base1, 1447 Base2 1448 {}; 1449 1450 * ``BILS_BeforeComma`` (in configuration: ``BeforeComma``) 1451 Break inheritance list before the colon and commas, and align 1452 the commas with the colon. 1453 1454 .. code-block:: c++ 1455 1456 class Foo 1457 : Base1 1458 , Base2 1459 {}; 1460 1461 * ``BILS_AfterColon`` (in configuration: ``AfterColon``) 1462 Break inheritance list after the colon and commas. 1463 1464 .. code-block:: c++ 1465 1466 class Foo : 1467 Base1, 1468 Base2 1469 {}; 1470 1471 1472 1473**BreakStringLiterals** (``bool``) 1474 Allow breaking string literals when formatting. 1475 1476 .. code-block:: c++ 1477 1478 true: 1479 const char* x = "veryVeryVeryVeryVeryVe" 1480 "ryVeryVeryVeryVeryVery" 1481 "VeryLongString"; 1482 1483 false: 1484 const char* x = 1485 "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString"; 1486 1487**ColumnLimit** (``unsigned``) 1488 The column limit. 1489 1490 A column limit of ``0`` means that there is no column limit. In this case, 1491 clang-format will respect the input's line breaking decisions within 1492 statements unless they contradict other rules. 1493 1494**CommentPragmas** (``std::string``) 1495 A regular expression that describes comments with special meaning, 1496 which should not be split into lines or otherwise changed. 1497 1498 .. code-block:: c++ 1499 1500 // CommentPragmas: '^ FOOBAR pragma:' 1501 // Will leave the following line unaffected 1502 #include <vector> // FOOBAR pragma: keep 1503 1504**CompactNamespaces** (``bool``) 1505 If ``true``, consecutive namespace declarations will be on the same 1506 line. If ``false``, each namespace is declared on a new line. 1507 1508 .. code-block:: c++ 1509 1510 true: 1511 namespace Foo { namespace Bar { 1512 }} 1513 1514 false: 1515 namespace Foo { 1516 namespace Bar { 1517 } 1518 } 1519 1520 If it does not fit on a single line, the overflowing namespaces get 1521 wrapped: 1522 1523 .. code-block:: c++ 1524 1525 namespace Foo { namespace Bar { 1526 namespace Extra { 1527 }}} 1528 1529**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``) 1530 If the constructor initializers don't fit on a line, put each 1531 initializer on its own line. 1532 1533 .. code-block:: c++ 1534 1535 true: 1536 SomeClass::Constructor() 1537 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 1538 return 0; 1539 } 1540 1541 false: 1542 SomeClass::Constructor() 1543 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), 1544 aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 1545 return 0; 1546 } 1547 1548**ConstructorInitializerIndentWidth** (``unsigned``) 1549 The number of characters to use for indentation of constructor 1550 initializer lists as well as inheritance lists. 1551 1552**ContinuationIndentWidth** (``unsigned``) 1553 Indent width for line continuations. 1554 1555 .. code-block:: c++ 1556 1557 ContinuationIndentWidth: 2 1558 1559 int i = // VeryVeryVeryVeryVeryLongComment 1560 longFunction( // Again a long comment 1561 arg); 1562 1563**Cpp11BracedListStyle** (``bool``) 1564 If ``true``, format braced lists as best suited for C++11 braced 1565 lists. 1566 1567 Important differences: 1568 - No spaces inside the braced list. 1569 - No line break before the closing brace. 1570 - Indentation with the continuation indent, not with the block indent. 1571 1572 Fundamentally, C++11 braced lists are formatted exactly like function 1573 calls would be formatted in their place. If the braced list follows a name 1574 (e.g. a type or variable name), clang-format formats as if the ``{}`` were 1575 the parentheses of a function call with that name. If there is no name, 1576 a zero-length name is assumed. 1577 1578 .. code-block:: c++ 1579 1580 true: false: 1581 vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 }; 1582 vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} }; 1583 f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]); 1584 new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 }; 1585 1586**DeriveLineEnding** (``bool``) 1587 Analyze the formatted file for the most used line ending (``\r\n`` 1588 or ``\n``). ``UseCRLF`` is only used as a fallback if none can be derived. 1589 1590**DerivePointerAlignment** (``bool``) 1591 If ``true``, analyze the formatted file for the most common 1592 alignment of ``&`` and ``*``. 1593 Pointer and reference alignment styles are going to be updated according 1594 to the preferences found in the file. 1595 ``PointerAlignment`` is then used only as fallback. 1596 1597**DisableFormat** (``bool``) 1598 Disables formatting completely. 1599 1600**ExperimentalAutoDetectBinPacking** (``bool``) 1601 If ``true``, clang-format detects whether function calls and 1602 definitions are formatted with one parameter per line. 1603 1604 Each call can be bin-packed, one-per-line or inconclusive. If it is 1605 inconclusive, e.g. completely on one line, but a decision needs to be 1606 made, clang-format analyzes whether there are other bin-packed cases in 1607 the input file and act accordingly. 1608 1609 NOTE: This is an experimental flag, that might go away or be renamed. Do 1610 not use this in config files, etc. Use at your own risk. 1611 1612**FixNamespaceComments** (``bool``) 1613 If ``true``, clang-format adds missing namespace end comments and 1614 fixes invalid existing ones. 1615 1616 .. code-block:: c++ 1617 1618 true: false: 1619 namespace a { vs. namespace a { 1620 foo(); foo(); 1621 } // namespace a } 1622 1623**ForEachMacros** (``std::vector<std::string>``) 1624 A vector of macros that should be interpreted as foreach loops 1625 instead of as function calls. 1626 1627 These are expected to be macros of the form: 1628 1629 .. code-block:: c++ 1630 1631 FOREACH(<variable-declaration>, ...) 1632 <loop-body> 1633 1634 In the .clang-format configuration file, this can be configured like: 1635 1636 .. code-block:: yaml 1637 1638 ForEachMacros: ['RANGES_FOR', 'FOREACH'] 1639 1640 For example: BOOST_FOREACH. 1641 1642**IncludeBlocks** (``IncludeBlocksStyle``) 1643 Dependent on the value, multiple ``#include`` blocks can be sorted 1644 as one and divided based on category. 1645 1646 Possible values: 1647 1648 * ``IBS_Preserve`` (in configuration: ``Preserve``) 1649 Sort each ``#include`` block separately. 1650 1651 .. code-block:: c++ 1652 1653 #include "b.h" into #include "b.h" 1654 1655 #include <lib/main.h> #include "a.h" 1656 #include "a.h" #include <lib/main.h> 1657 1658 * ``IBS_Merge`` (in configuration: ``Merge``) 1659 Merge multiple ``#include`` blocks together and sort as one. 1660 1661 .. code-block:: c++ 1662 1663 #include "b.h" into #include "a.h" 1664 #include "b.h" 1665 #include <lib/main.h> #include <lib/main.h> 1666 #include "a.h" 1667 1668 * ``IBS_Regroup`` (in configuration: ``Regroup``) 1669 Merge multiple ``#include`` blocks together and sort as one. 1670 Then split into groups based on category priority. See 1671 ``IncludeCategories``. 1672 1673 .. code-block:: c++ 1674 1675 #include "b.h" into #include "a.h" 1676 #include "b.h" 1677 #include <lib/main.h> 1678 #include "a.h" #include <lib/main.h> 1679 1680 1681 1682**IncludeCategories** (``std::vector<IncludeCategory>``) 1683 Regular expressions denoting the different ``#include`` categories 1684 used for ordering ``#includes``. 1685 1686 `POSIX extended 1687 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>`_ 1688 regular expressions are supported. 1689 1690 These regular expressions are matched against the filename of an include 1691 (including the <> or "") in order. The value belonging to the first 1692 matching regular expression is assigned and ``#includes`` are sorted first 1693 according to increasing category number and then alphabetically within 1694 each category. 1695 1696 If none of the regular expressions match, INT_MAX is assigned as 1697 category. The main header for a source file automatically gets category 0. 1698 so that it is generally kept at the beginning of the ``#includes`` 1699 (https://llvm.org/docs/CodingStandards.html#include-style). However, you 1700 can also assign negative priorities if you have certain headers that 1701 always need to be first. 1702 1703 There is a third and optional field ``SortPriority`` which can used while 1704 ``IncludeBloks = IBS_Regroup`` to define the priority in which ``#includes`` 1705 should be ordered, and value of ``Priority`` defines the order of 1706 ``#include blocks`` and also enables to group ``#includes`` of different 1707 priority for order.``SortPriority`` is set to the value of ``Priority`` 1708 as default if it is not assigned. 1709 1710 To configure this in the .clang-format file, use: 1711 1712 .. code-block:: yaml 1713 1714 IncludeCategories: 1715 - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 1716 Priority: 2 1717 SortPriority: 2 1718 - Regex: '^(<|"(gtest|gmock|isl|json)/)' 1719 Priority: 3 1720 - Regex: '<[[:alnum:].]+>' 1721 Priority: 4 1722 - Regex: '.*' 1723 Priority: 1 1724 SortPriority: 0 1725 1726**IncludeIsMainRegex** (``std::string``) 1727 Specify a regular expression of suffixes that are allowed in the 1728 file-to-main-include mapping. 1729 1730 When guessing whether a #include is the "main" include (to assign 1731 category 0, see above), use this regex of allowed suffixes to the header 1732 stem. A partial match is done, so that: 1733 - "" means "arbitrary suffix" 1734 - "$" means "no suffix" 1735 1736 For example, if configured to "(_test)?$", then a header a.h would be seen 1737 as the "main" include in both a.cc and a_test.cc. 1738 1739**IncludeIsMainSourceRegex** (``std::string``) 1740 Specify a regular expression for files being formatted 1741 that are allowed to be considered "main" in the 1742 file-to-main-include mapping. 1743 1744 By default, clang-format considers files as "main" only when they end 1745 with: ``.c``, ``.cc``, ``.cpp``, ``.c++``, ``.cxx``, ``.m`` or ``.mm`` 1746 extensions. 1747 For these files a guessing of "main" include takes place 1748 (to assign category 0, see above). This config option allows for 1749 additional suffixes and extensions for files to be considered as "main". 1750 1751 For example, if this option is configured to ``(Impl\.hpp)$``, 1752 then a file ``ClassImpl.hpp`` is considered "main" (in addition to 1753 ``Class.c``, ``Class.cc``, ``Class.cpp`` and so on) and "main 1754 include file" logic will be executed (with *IncludeIsMainRegex* setting 1755 also being respected in later phase). Without this option set, 1756 ``ClassImpl.hpp`` would not have the main include file put on top 1757 before any other include. 1758 1759**IndentCaseBlocks** (``bool``) 1760 Indent case label blocks one level from the case label. 1761 1762 When ``false``, the block following the case label uses the same 1763 indentation level as for the case label, treating the case label the same 1764 as an if-statement. 1765 When ``true``, the block gets indented as a scope block. 1766 1767 .. code-block:: c++ 1768 1769 false: true: 1770 switch (fool) { vs. switch (fool) { 1771 case 1: { case 1: 1772 bar(); { 1773 } break; bar(); 1774 default: { } 1775 plop(); break; 1776 } default: 1777 } { 1778 plop(); 1779 } 1780 } 1781 1782**IndentCaseLabels** (``bool``) 1783 Indent case labels one level from the switch statement. 1784 1785 When ``false``, use the same indentation level as for the switch 1786 statement. Switch statement body is always indented one level more than 1787 case labels (except the first block following the case label, which 1788 itself indents the code - unless IndentCaseBlocks is enabled). 1789 1790 .. code-block:: c++ 1791 1792 false: true: 1793 switch (fool) { vs. switch (fool) { 1794 case 1: case 1: 1795 bar(); bar(); 1796 break; break; 1797 default: default: 1798 plop(); plop(); 1799 } } 1800 1801**IndentExternBlock** (``IndentExternBlockStyle``) 1802 IndentExternBlockStyle is the type of indenting of extern blocks. 1803 1804 Possible values: 1805 1806 * ``IEBS_AfterExternBlock`` (in configuration: ``AfterExternBlock``) 1807 Backwards compatible with AfterExternBlock's indenting. 1808 1809 .. code-block:: c++ 1810 1811 IndentExternBlock: AfterExternBlock 1812 BraceWrapping.AfterExternBlock: true 1813 extern "C" 1814 { 1815 void foo(); 1816 } 1817 1818 1819 .. code-block:: c++ 1820 1821 IndentExternBlock: AfterExternBlock 1822 BraceWrapping.AfterExternBlock: false 1823 extern "C" { 1824 void foo(); 1825 } 1826 1827 * ``IEBS_NoIndent`` (in configuration: ``NoIndent``) 1828 Does not indent extern blocks. 1829 1830 .. code-block:: c++ 1831 1832 extern "C" { 1833 void foo(); 1834 } 1835 1836 * ``IEBS_Indent`` (in configuration: ``Indent``) 1837 Indents extern blocks. 1838 1839 .. code-block:: c++ 1840 1841 extern "C" { 1842 void foo(); 1843 } 1844 1845 1846 1847**IndentGotoLabels** (``bool``) 1848 Indent goto labels. 1849 1850 When ``false``, goto labels are flushed left. 1851 1852 .. code-block:: c++ 1853 1854 true: false: 1855 int f() { vs. int f() { 1856 if (foo()) { if (foo()) { 1857 label1: label1: 1858 bar(); bar(); 1859 } } 1860 label2: label2: 1861 return 1; return 1; 1862 } } 1863 1864**IndentPPDirectives** (``PPDirectiveIndentStyle``) 1865 The preprocessor directive indenting style to use. 1866 1867 Possible values: 1868 1869 * ``PPDIS_None`` (in configuration: ``None``) 1870 Does not indent any directives. 1871 1872 .. code-block:: c++ 1873 1874 #if FOO 1875 #if BAR 1876 #include <foo> 1877 #endif 1878 #endif 1879 1880 * ``PPDIS_AfterHash`` (in configuration: ``AfterHash``) 1881 Indents directives after the hash. 1882 1883 .. code-block:: c++ 1884 1885 #if FOO 1886 # if BAR 1887 # include <foo> 1888 # endif 1889 #endif 1890 1891 * ``PPDIS_BeforeHash`` (in configuration: ``BeforeHash``) 1892 Indents directives before the hash. 1893 1894 .. code-block:: c++ 1895 1896 #if FOO 1897 #if BAR 1898 #include <foo> 1899 #endif 1900 #endif 1901 1902 1903 1904**IndentWidth** (``unsigned``) 1905 The number of columns to use for indentation. 1906 1907 .. code-block:: c++ 1908 1909 IndentWidth: 3 1910 1911 void f() { 1912 someFunction(); 1913 if (true, false) { 1914 f(); 1915 } 1916 } 1917 1918**IndentWrappedFunctionNames** (``bool``) 1919 Indent if a function definition or declaration is wrapped after the 1920 type. 1921 1922 .. code-block:: c++ 1923 1924 true: 1925 LoooooooooooooooooooooooooooooooooooooooongReturnType 1926 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1927 1928 false: 1929 LoooooooooooooooooooooooooooooooooooooooongReturnType 1930 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1931 1932**InsertTrailingCommas** (``TrailingCommaStyle``) 1933 If set to ``TCS_Wrapped`` will insert trailing commas in container 1934 literals (arrays and objects) that wrap across multiple lines. 1935 It is currently only available for JavaScript 1936 and disabled by default ``TCS_None``. 1937 ``InsertTrailingCommas`` cannot be used together with ``BinPackArguments`` 1938 as inserting the comma disables bin-packing. 1939 1940 .. code-block:: c++ 1941 1942 TSC_Wrapped: 1943 const someArray = [ 1944 aaaaaaaaaaaaaaaaaaaaaaaaaa, 1945 aaaaaaaaaaaaaaaaaaaaaaaaaa, 1946 aaaaaaaaaaaaaaaaaaaaaaaaaa, 1947 // ^ inserted 1948 ] 1949 1950 Possible values: 1951 1952 * ``TCS_None`` (in configuration: ``None``) 1953 Do not insert trailing commas. 1954 1955 * ``TCS_Wrapped`` (in configuration: ``Wrapped``) 1956 Insert trailing commas in container literals that were wrapped over 1957 multiple lines. Note that this is conceptually incompatible with 1958 bin-packing, because the trailing comma is used as an indicator 1959 that a container should be formatted one-per-line (i.e. not bin-packed). 1960 So inserting a trailing comma counteracts bin-packing. 1961 1962 1963 1964**JavaImportGroups** (``std::vector<std::string>``) 1965 A vector of prefixes ordered by the desired groups for Java imports. 1966 1967 One group's prefix can be a subset of another - the longest prefix is 1968 always matched. Within a group, the imports are ordered lexicographically. 1969 Static imports are grouped separately and follow the same group rules. 1970 By default, static imports are placed before non-static imports, 1971 but this behavior is changed by another option, 1972 ``SortJavaStaticImport``. 1973 1974 In the .clang-format configuration file, this can be configured like 1975 in the following yaml example. This will result in imports being 1976 formatted as in the Java example below. 1977 1978 .. code-block:: yaml 1979 1980 JavaImportGroups: ['com.example', 'com', 'org'] 1981 1982 1983 .. code-block:: java 1984 1985 import static com.example.function1; 1986 1987 import static com.test.function2; 1988 1989 import static org.example.function3; 1990 1991 import com.example.ClassA; 1992 import com.example.Test; 1993 import com.example.a.ClassB; 1994 1995 import com.test.ClassC; 1996 1997 import org.example.ClassD; 1998 1999**JavaScriptQuotes** (``JavaScriptQuoteStyle``) 2000 The JavaScriptQuoteStyle to use for JavaScript strings. 2001 2002 Possible values: 2003 2004 * ``JSQS_Leave`` (in configuration: ``Leave``) 2005 Leave string quotes as they are. 2006 2007 .. code-block:: js 2008 2009 string1 = "foo"; 2010 string2 = 'bar'; 2011 2012 * ``JSQS_Single`` (in configuration: ``Single``) 2013 Always use single quotes. 2014 2015 .. code-block:: js 2016 2017 string1 = 'foo'; 2018 string2 = 'bar'; 2019 2020 * ``JSQS_Double`` (in configuration: ``Double``) 2021 Always use double quotes. 2022 2023 .. code-block:: js 2024 2025 string1 = "foo"; 2026 string2 = "bar"; 2027 2028 2029 2030**JavaScriptWrapImports** (``bool``) 2031 Whether to wrap JavaScript import/export statements. 2032 2033 .. code-block:: js 2034 2035 true: 2036 import { 2037 VeryLongImportsAreAnnoying, 2038 VeryLongImportsAreAnnoying, 2039 VeryLongImportsAreAnnoying, 2040 } from 'some/module.js' 2041 2042 false: 2043 import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,} from "some/module.js" 2044 2045**KeepEmptyLinesAtTheStartOfBlocks** (``bool``) 2046 If true, the empty line at the start of blocks is kept. 2047 2048 .. code-block:: c++ 2049 2050 true: false: 2051 if (foo) { vs. if (foo) { 2052 bar(); 2053 bar(); } 2054 } 2055 2056**Language** (``LanguageKind``) 2057 Language, this format style is targeted at. 2058 2059 Possible values: 2060 2061 * ``LK_None`` (in configuration: ``None``) 2062 Do not use. 2063 2064 * ``LK_Cpp`` (in configuration: ``Cpp``) 2065 Should be used for C, C++. 2066 2067 * ``LK_CSharp`` (in configuration: ``CSharp``) 2068 Should be used for C#. 2069 2070 * ``LK_Java`` (in configuration: ``Java``) 2071 Should be used for Java. 2072 2073 * ``LK_JavaScript`` (in configuration: ``JavaScript``) 2074 Should be used for JavaScript. 2075 2076 * ``LK_ObjC`` (in configuration: ``ObjC``) 2077 Should be used for Objective-C, Objective-C++. 2078 2079 * ``LK_Proto`` (in configuration: ``Proto``) 2080 Should be used for Protocol Buffers 2081 (https://developers.google.com/protocol-buffers/). 2082 2083 * ``LK_TableGen`` (in configuration: ``TableGen``) 2084 Should be used for TableGen code. 2085 2086 * ``LK_TextProto`` (in configuration: ``TextProto``) 2087 Should be used for Protocol Buffer messages in text format 2088 (https://developers.google.com/protocol-buffers/). 2089 2090 2091 2092**MacroBlockBegin** (``std::string``) 2093 A regular expression matching macros that start a block. 2094 2095 .. code-block:: c++ 2096 2097 # With: 2098 MacroBlockBegin: "^NS_MAP_BEGIN|\ 2099 NS_TABLE_HEAD$" 2100 MacroBlockEnd: "^\ 2101 NS_MAP_END|\ 2102 NS_TABLE_.*_END$" 2103 2104 NS_MAP_BEGIN 2105 foo(); 2106 NS_MAP_END 2107 2108 NS_TABLE_HEAD 2109 bar(); 2110 NS_TABLE_FOO_END 2111 2112 # Without: 2113 NS_MAP_BEGIN 2114 foo(); 2115 NS_MAP_END 2116 2117 NS_TABLE_HEAD 2118 bar(); 2119 NS_TABLE_FOO_END 2120 2121**MacroBlockEnd** (``std::string``) 2122 A regular expression matching macros that end a block. 2123 2124**MaxEmptyLinesToKeep** (``unsigned``) 2125 The maximum number of consecutive empty lines to keep. 2126 2127 .. code-block:: c++ 2128 2129 MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0 2130 int f() { int f() { 2131 int = 1; int i = 1; 2132 i = foo(); 2133 i = foo(); return i; 2134 } 2135 return i; 2136 } 2137 2138**NamespaceIndentation** (``NamespaceIndentationKind``) 2139 The indentation used for namespaces. 2140 2141 Possible values: 2142 2143 * ``NI_None`` (in configuration: ``None``) 2144 Don't indent in namespaces. 2145 2146 .. code-block:: c++ 2147 2148 namespace out { 2149 int i; 2150 namespace in { 2151 int i; 2152 } 2153 } 2154 2155 * ``NI_Inner`` (in configuration: ``Inner``) 2156 Indent only in inner namespaces (nested in other namespaces). 2157 2158 .. code-block:: c++ 2159 2160 namespace out { 2161 int i; 2162 namespace in { 2163 int i; 2164 } 2165 } 2166 2167 * ``NI_All`` (in configuration: ``All``) 2168 Indent in all namespaces. 2169 2170 .. code-block:: c++ 2171 2172 namespace out { 2173 int i; 2174 namespace in { 2175 int i; 2176 } 2177 } 2178 2179 2180 2181**NamespaceMacros** (``std::vector<std::string>``) 2182 A vector of macros which are used to open namespace blocks. 2183 2184 These are expected to be macros of the form: 2185 2186 .. code-block:: c++ 2187 2188 NAMESPACE(<namespace-name>, ...) { 2189 <namespace-content> 2190 } 2191 2192 For example: TESTSUITE 2193 2194**ObjCBinPackProtocolList** (``BinPackStyle``) 2195 Controls bin-packing Objective-C protocol conformance list 2196 items into as few lines as possible when they go over ``ColumnLimit``. 2197 2198 If ``Auto`` (the default), delegates to the value in 2199 ``BinPackParameters``. If that is ``true``, bin-packs Objective-C 2200 protocol conformance list items into as few lines as possible 2201 whenever they go over ``ColumnLimit``. 2202 2203 If ``Always``, always bin-packs Objective-C protocol conformance 2204 list items into as few lines as possible whenever they go over 2205 ``ColumnLimit``. 2206 2207 If ``Never``, lays out Objective-C protocol conformance list items 2208 onto individual lines whenever they go over ``ColumnLimit``. 2209 2210 2211 .. code-block:: objc 2212 2213 Always (or Auto, if BinPackParameters=true): 2214 @interface ccccccccccccc () < 2215 ccccccccccccc, ccccccccccccc, 2216 ccccccccccccc, ccccccccccccc> { 2217 } 2218 2219 Never (or Auto, if BinPackParameters=false): 2220 @interface ddddddddddddd () < 2221 ddddddddddddd, 2222 ddddddddddddd, 2223 ddddddddddddd, 2224 ddddddddddddd> { 2225 } 2226 2227 Possible values: 2228 2229 * ``BPS_Auto`` (in configuration: ``Auto``) 2230 Automatically determine parameter bin-packing behavior. 2231 2232 * ``BPS_Always`` (in configuration: ``Always``) 2233 Always bin-pack parameters. 2234 2235 * ``BPS_Never`` (in configuration: ``Never``) 2236 Never bin-pack parameters. 2237 2238 2239 2240**ObjCBlockIndentWidth** (``unsigned``) 2241 The number of characters to use for indentation of ObjC blocks. 2242 2243 .. code-block:: objc 2244 2245 ObjCBlockIndentWidth: 4 2246 2247 [operation setCompletionBlock:^{ 2248 [self onOperationDone]; 2249 }]; 2250 2251**ObjCBreakBeforeNestedBlockParam** (``bool``) 2252 Break parameters list into lines when there is nested block 2253 parameters in a function call. 2254 2255 .. code-block:: c++ 2256 2257 false: 2258 - (void)_aMethod 2259 { 2260 [self.test1 t:self w:self callback:^(typeof(self) self, NSNumber 2261 *u, NSNumber *v) { 2262 u = c; 2263 }] 2264 } 2265 true: 2266 - (void)_aMethod 2267 { 2268 [self.test1 t:self 2269 w:self 2270 callback:^(typeof(self) self, NSNumber *u, NSNumber *v) { 2271 u = c; 2272 }] 2273 } 2274 2275**ObjCSpaceAfterProperty** (``bool``) 2276 Add a space after ``@property`` in Objective-C, i.e. use 2277 ``@property (readonly)`` instead of ``@property(readonly)``. 2278 2279**ObjCSpaceBeforeProtocolList** (``bool``) 2280 Add a space in front of an Objective-C protocol list, i.e. use 2281 ``Foo <Protocol>`` instead of ``Foo<Protocol>``. 2282 2283**PenaltyBreakAssignment** (``unsigned``) 2284 The penalty for breaking around an assignment operator. 2285 2286**PenaltyBreakBeforeFirstCallParameter** (``unsigned``) 2287 The penalty for breaking a function call after ``call(``. 2288 2289**PenaltyBreakComment** (``unsigned``) 2290 The penalty for each line break introduced inside a comment. 2291 2292**PenaltyBreakFirstLessLess** (``unsigned``) 2293 The penalty for breaking before the first ``<<``. 2294 2295**PenaltyBreakString** (``unsigned``) 2296 The penalty for each line break introduced inside a string literal. 2297 2298**PenaltyBreakTemplateDeclaration** (``unsigned``) 2299 The penalty for breaking after template declaration. 2300 2301**PenaltyExcessCharacter** (``unsigned``) 2302 The penalty for each character outside of the column limit. 2303 2304**PenaltyReturnTypeOnItsOwnLine** (``unsigned``) 2305 Penalty for putting the return type of a function onto its own 2306 line. 2307 2308**PointerAlignment** (``PointerAlignmentStyle``) 2309 Pointer and reference alignment style. 2310 2311 Possible values: 2312 2313 * ``PAS_Left`` (in configuration: ``Left``) 2314 Align pointer to the left. 2315 2316 .. code-block:: c++ 2317 2318 int* a; 2319 2320 * ``PAS_Right`` (in configuration: ``Right``) 2321 Align pointer to the right. 2322 2323 .. code-block:: c++ 2324 2325 int *a; 2326 2327 * ``PAS_Middle`` (in configuration: ``Middle``) 2328 Align pointer in the middle. 2329 2330 .. code-block:: c++ 2331 2332 int * a; 2333 2334 2335 2336**RawStringFormats** (``std::vector<RawStringFormat>``) 2337 Defines hints for detecting supported languages code blocks in raw 2338 strings. 2339 2340 A raw string with a matching delimiter or a matching enclosing function 2341 name will be reformatted assuming the specified language based on the 2342 style for that language defined in the .clang-format file. If no style has 2343 been defined in the .clang-format file for the specific language, a 2344 predefined style given by 'BasedOnStyle' is used. If 'BasedOnStyle' is not 2345 found, the formatting is based on llvm style. A matching delimiter takes 2346 precedence over a matching enclosing function name for determining the 2347 language of the raw string contents. 2348 2349 If a canonical delimiter is specified, occurrences of other delimiters for 2350 the same language will be updated to the canonical if possible. 2351 2352 There should be at most one specification per language and each delimiter 2353 and enclosing function should not occur in multiple specifications. 2354 2355 To configure this in the .clang-format file, use: 2356 2357 .. code-block:: yaml 2358 2359 RawStringFormats: 2360 - Language: TextProto 2361 Delimiters: 2362 - 'pb' 2363 - 'proto' 2364 EnclosingFunctions: 2365 - 'PARSE_TEXT_PROTO' 2366 BasedOnStyle: google 2367 - Language: Cpp 2368 Delimiters: 2369 - 'cc' 2370 - 'cpp' 2371 BasedOnStyle: llvm 2372 CanonicalDelimiter: 'cc' 2373 2374**ReflowComments** (``bool``) 2375 If ``true``, clang-format will attempt to re-flow comments. 2376 2377 .. code-block:: c++ 2378 2379 false: 2380 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information 2381 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */ 2382 2383 true: 2384 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 2385 // information 2386 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 2387 * information */ 2388 2389**SortIncludes** (``bool``) 2390 If ``true``, clang-format will sort ``#includes``. 2391 2392 .. code-block:: c++ 2393 2394 false: true: 2395 #include "b.h" vs. #include "a.h" 2396 #include "a.h" #include "b.h" 2397 2398**SortJavaStaticImport** (``SortJavaStaticImportOptions``) 2399 When sorting Java imports, by default static imports are placed before 2400 non-static imports. If ``JavaStaticImportAfterImport`` is ``After``, 2401 static imports are placed after non-static imports. 2402 2403 Possible values: 2404 2405 * ``SJSIO_Before`` (in configuration: ``Before``) 2406 Static imports are placed before non-static imports. 2407 2408 .. code-block:: java 2409 2410 import static org.example.function1; 2411 2412 import org.example.ClassA; 2413 2414 * ``SJSIO_After`` (in configuration: ``After``) 2415 Static imports are placed after non-static imports. 2416 2417 .. code-block:: java 2418 2419 import org.example.ClassA; 2420 2421 import static org.example.function1; 2422 2423 2424 2425**SortUsingDeclarations** (``bool``) 2426 If ``true``, clang-format will sort using declarations. 2427 2428 The order of using declarations is defined as follows: 2429 Split the strings by "::" and discard any initial empty strings. The last 2430 element of each list is a non-namespace name; all others are namespace 2431 names. Sort the lists of names lexicographically, where the sort order of 2432 individual names is that all non-namespace names come before all namespace 2433 names, and within those groups, names are in case-insensitive 2434 lexicographic order. 2435 2436 .. code-block:: c++ 2437 2438 false: true: 2439 using std::cout; vs. using std::cin; 2440 using std::cin; using std::cout; 2441 2442**SpaceAfterCStyleCast** (``bool``) 2443 If ``true``, a space is inserted after C style casts. 2444 2445 .. code-block:: c++ 2446 2447 true: false: 2448 (int) i; vs. (int)i; 2449 2450**SpaceAfterLogicalNot** (``bool``) 2451 If ``true``, a space is inserted after the logical not operator (``!``). 2452 2453 .. code-block:: c++ 2454 2455 true: false: 2456 ! someExpression(); vs. !someExpression(); 2457 2458**SpaceAfterTemplateKeyword** (``bool``) 2459 If ``true``, a space will be inserted after the 'template' keyword. 2460 2461 .. code-block:: c++ 2462 2463 true: false: 2464 template <int> void foo(); vs. template<int> void foo(); 2465 2466**SpaceAroundPointerQualifiers** (``SpaceAroundPointerQualifiersStyle``) 2467 Defines in which cases to put a space before or after pointer qualifiers 2468 2469 Possible values: 2470 2471 * ``SAPQ_Default`` (in configuration: ``Default``) 2472 Don't ensure spaces around pointer qualifiers and use PointerAlignment 2473 instead. 2474 2475 .. code-block:: c++ 2476 2477 PointerAlignment: Left PointerAlignment: Right 2478 void* const* x = NULL; vs. void *const *x = NULL; 2479 2480 * ``SAPQ_Before`` (in configuration: ``Before``) 2481 Ensure that there is a space before pointer qualifiers. 2482 2483 .. code-block:: c++ 2484 2485 PointerAlignment: Left PointerAlignment: Right 2486 void* const* x = NULL; vs. void * const *x = NULL; 2487 2488 * ``SAPQ_After`` (in configuration: ``After``) 2489 Ensure that there is a space after pointer qualifiers. 2490 2491 .. code-block:: c++ 2492 2493 PointerAlignment: Left PointerAlignment: Right 2494 void* const * x = NULL; vs. void *const *x = NULL; 2495 2496 * ``SAPQ_Both`` (in configuration: ``Both``) 2497 Ensure that there is a space both before and after pointer qualifiers. 2498 2499 .. code-block:: c++ 2500 2501 PointerAlignment: Left PointerAlignment: Right 2502 void* const * x = NULL; vs. void * const *x = NULL; 2503 2504 2505 2506**SpaceBeforeAssignmentOperators** (``bool``) 2507 If ``false``, spaces will be removed before assignment operators. 2508 2509 .. code-block:: c++ 2510 2511 true: false: 2512 int a = 5; vs. int a= 5; 2513 a += 42; a+= 42; 2514 2515**SpaceBeforeCpp11BracedList** (``bool``) 2516 If ``true``, a space will be inserted before a C++11 braced list 2517 used to initialize an object (after the preceding identifier or type). 2518 2519 .. code-block:: c++ 2520 2521 true: false: 2522 Foo foo { bar }; vs. Foo foo{ bar }; 2523 Foo {}; Foo{}; 2524 vector<int> { 1, 2, 3 }; vector<int>{ 1, 2, 3 }; 2525 new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 }; 2526 2527**SpaceBeforeCtorInitializerColon** (``bool``) 2528 If ``false``, spaces will be removed before constructor initializer 2529 colon. 2530 2531 .. code-block:: c++ 2532 2533 true: false: 2534 Foo::Foo() : a(a) {} Foo::Foo(): a(a) {} 2535 2536**SpaceBeforeInheritanceColon** (``bool``) 2537 If ``false``, spaces will be removed before inheritance colon. 2538 2539 .. code-block:: c++ 2540 2541 true: false: 2542 class Foo : Bar {} vs. class Foo: Bar {} 2543 2544**SpaceBeforeParens** (``SpaceBeforeParensOptions``) 2545 Defines in which cases to put a space before opening parentheses. 2546 2547 Possible values: 2548 2549 * ``SBPO_Never`` (in configuration: ``Never``) 2550 Never put a space before opening parentheses. 2551 2552 .. code-block:: c++ 2553 2554 void f() { 2555 if(true) { 2556 f(); 2557 } 2558 } 2559 2560 * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) 2561 Put a space before opening parentheses only after control statement 2562 keywords (``for/if/while...``). 2563 2564 .. code-block:: c++ 2565 2566 void f() { 2567 if (true) { 2568 f(); 2569 } 2570 } 2571 2572 * ``SBPO_ControlStatementsExceptForEachMacros`` (in configuration: ``ControlStatementsExceptForEachMacros``) 2573 Same as ``SBPO_ControlStatements`` except this option doesn't apply to 2574 ForEach macros. This is useful in projects where ForEach macros are 2575 treated as function calls instead of control statements. 2576 2577 .. code-block:: c++ 2578 2579 void f() { 2580 Q_FOREACH(...) { 2581 f(); 2582 } 2583 } 2584 2585 * ``SBPO_NonEmptyParentheses`` (in configuration: ``NonEmptyParentheses``) 2586 Put a space before opening parentheses only if the parentheses are not 2587 empty i.e. '()' 2588 2589 .. code-block:: c++ 2590 2591 void() { 2592 if (true) { 2593 f(); 2594 g (x, y, z); 2595 } 2596 } 2597 2598 * ``SBPO_Always`` (in configuration: ``Always``) 2599 Always put a space before opening parentheses, except when it's 2600 prohibited by the syntax rules (in function-like macro definitions) or 2601 when determined by other style rules (after unary operators, opening 2602 parentheses, etc.) 2603 2604 .. code-block:: c++ 2605 2606 void f () { 2607 if (true) { 2608 f (); 2609 } 2610 } 2611 2612 2613 2614**SpaceBeforeRangeBasedForLoopColon** (``bool``) 2615 If ``false``, spaces will be removed before range-based for loop 2616 colon. 2617 2618 .. code-block:: c++ 2619 2620 true: false: 2621 for (auto v : values) {} vs. for(auto v: values) {} 2622 2623**SpaceBeforeSquareBrackets** (``bool``) 2624 If ``true``, spaces will be before ``[``. 2625 Lambdas will not be affected. Only the first ``[`` will get a space added. 2626 2627 .. code-block:: c++ 2628 2629 true: false: 2630 int a [5]; vs. int a[5]; 2631 int a [5][5]; vs. int a[5][5]; 2632 2633**SpaceInEmptyBlock** (``bool``) 2634 If ``true``, spaces will be inserted into ``{}``. 2635 2636 .. code-block:: c++ 2637 2638 true: false: 2639 void f() { } vs. void f() {} 2640 while (true) { } while (true) {} 2641 2642**SpaceInEmptyParentheses** (``bool``) 2643 If ``true``, spaces may be inserted into ``()``. 2644 2645 .. code-block:: c++ 2646 2647 true: false: 2648 void f( ) { vs. void f() { 2649 int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; 2650 if (true) { if (true) { 2651 f( ); f(); 2652 } } 2653 } } 2654 2655**SpacesBeforeTrailingComments** (``unsigned``) 2656 The number of spaces before trailing line comments 2657 (``//`` - comments). 2658 2659 This does not affect trailing block comments (``/*`` - comments) as 2660 those commonly have different usage patterns and a number of special 2661 cases. 2662 2663 .. code-block:: c++ 2664 2665 SpacesBeforeTrailingComments: 3 2666 void f() { 2667 if (true) { // foo1 2668 f(); // bar 2669 } // foo 2670 } 2671 2672**SpacesInAngles** (``bool``) 2673 If ``true``, spaces will be inserted after ``<`` and before ``>`` 2674 in template argument lists. 2675 2676 .. code-block:: c++ 2677 2678 true: false: 2679 static_cast< int >(arg); vs. static_cast<int>(arg); 2680 std::function< void(int) > fct; std::function<void(int)> fct; 2681 2682**SpacesInCStyleCastParentheses** (``bool``) 2683 If ``true``, spaces may be inserted into C style casts. 2684 2685 .. code-block:: c++ 2686 2687 true: false: 2688 x = ( int32 )y vs. x = (int32)y 2689 2690**SpacesInConditionalStatement** (``bool``) 2691 If ``true``, spaces will be inserted around if/for/switch/while 2692 conditions. 2693 2694 .. code-block:: c++ 2695 2696 true: false: 2697 if ( a ) { ... } vs. if (a) { ... } 2698 while ( i < 5 ) { ... } while (i < 5) { ... } 2699 2700**SpacesInContainerLiterals** (``bool``) 2701 If ``true``, spaces are inserted inside container literals (e.g. 2702 ObjC and Javascript array and dict literals). 2703 2704 .. code-block:: js 2705 2706 true: false: 2707 var arr = [ 1, 2, 3 ]; vs. var arr = [1, 2, 3]; 2708 f({a : 1, b : 2, c : 3}); f({a: 1, b: 2, c: 3}); 2709 2710**SpacesInParentheses** (``bool``) 2711 If ``true``, spaces will be inserted after ``(`` and before ``)``. 2712 2713 .. code-block:: c++ 2714 2715 true: false: 2716 t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; 2717 2718**SpacesInSquareBrackets** (``bool``) 2719 If ``true``, spaces will be inserted after ``[`` and before ``]``. 2720 Lambdas without arguments or unspecified size array declarations will not 2721 be affected. 2722 2723 .. code-block:: c++ 2724 2725 true: false: 2726 int a[ 5 ]; vs. int a[5]; 2727 std::unique_ptr<int[]> foo() {} // Won't be affected 2728 2729**Standard** (``LanguageStandard``) 2730 Parse and format C++ constructs compatible with this standard. 2731 2732 .. code-block:: c++ 2733 2734 c++03: latest: 2735 vector<set<int> > x; vs. vector<set<int>> x; 2736 2737 Possible values: 2738 2739 * ``LS_Cpp03`` (in configuration: ``c++03``) 2740 Parse and format as C++03. 2741 ``Cpp03`` is a deprecated alias for ``c++03`` 2742 2743 * ``LS_Cpp11`` (in configuration: ``c++11``) 2744 Parse and format as C++11. 2745 2746 * ``LS_Cpp14`` (in configuration: ``c++14``) 2747 Parse and format as C++14. 2748 2749 * ``LS_Cpp17`` (in configuration: ``c++17``) 2750 Parse and format as C++17. 2751 2752 * ``LS_Cpp20`` (in configuration: ``c++20``) 2753 Parse and format as C++20. 2754 2755 * ``LS_Latest`` (in configuration: ``Latest``) 2756 Parse and format using the latest supported language version. 2757 ``Cpp11`` is a deprecated alias for ``Latest`` 2758 2759 * ``LS_Auto`` (in configuration: ``Auto``) 2760 Automatic detection based on the input. 2761 2762 2763 2764**StatementMacros** (``std::vector<std::string>``) 2765 A vector of macros that should be interpreted as complete 2766 statements. 2767 2768 Typical macros are expressions, and require a semi-colon to be 2769 added; sometimes this is not the case, and this allows to make 2770 clang-format aware of such cases. 2771 2772 For example: Q_UNUSED 2773 2774**TabWidth** (``unsigned``) 2775 The number of columns used for tab stops. 2776 2777**TypenameMacros** (``std::vector<std::string>``) 2778 A vector of macros that should be interpreted as type declarations 2779 instead of as function calls. 2780 2781 These are expected to be macros of the form: 2782 2783 .. code-block:: c++ 2784 2785 STACK_OF(...) 2786 2787 In the .clang-format configuration file, this can be configured like: 2788 2789 .. code-block:: yaml 2790 2791 TypenameMacros: ['STACK_OF', 'LIST'] 2792 2793 For example: OpenSSL STACK_OF, BSD LIST_ENTRY. 2794 2795**UseCRLF** (``bool``) 2796 Use ``\r\n`` instead of ``\n`` for line breaks. 2797 Also used as fallback if ``DeriveLineEnding`` is true. 2798 2799**UseTab** (``UseTabStyle``) 2800 The way to use tab characters in the resulting file. 2801 2802 Possible values: 2803 2804 * ``UT_Never`` (in configuration: ``Never``) 2805 Never use tab. 2806 2807 * ``UT_ForIndentation`` (in configuration: ``ForIndentation``) 2808 Use tabs only for indentation. 2809 2810 * ``UT_ForContinuationAndIndentation`` (in configuration: ``ForContinuationAndIndentation``) 2811 Fill all leading whitespace with tabs, and use spaces for alignment that 2812 appears within a line (e.g. consecutive assignments and declarations). 2813 2814 * ``UT_AlignWithSpaces`` (in configuration: ``AlignWithSpaces``) 2815 Use tabs for line continuation and indentation, and spaces for 2816 alignment. 2817 2818 * ``UT_Always`` (in configuration: ``Always``) 2819 Use tabs whenever we need to fill whitespace that spans at least from 2820 one tab stop to the next one. 2821 2822 2823 2824**WhitespaceSensitiveMacros** (``std::vector<std::string>``) 2825 A vector of macros which are whitespace-sensitive and should not 2826 be touched. 2827 2828 These are expected to be macros of the form: 2829 2830 .. code-block:: c++ 2831 2832 STRINGIZE(...) 2833 2834 In the .clang-format configuration file, this can be configured like: 2835 2836 .. code-block:: yaml 2837 2838 WhitespaceSensitiveMacros: ['STRINGIZE', 'PP_STRINGIZE'] 2839 2840 For example: BOOST_PP_STRINGIZE 2841 2842.. END_FORMAT_STYLE_OPTIONS 2843 2844Adding additional style options 2845=============================== 2846 2847Each additional style option adds costs to the clang-format project. Some of 2848these costs affect the clang-format development itself, as we need to make 2849sure that any given combination of options work and that new features don't 2850break any of the existing options in any way. There are also costs for end users 2851as options become less discoverable and people have to think about and make a 2852decision on options they don't really care about. 2853 2854The goal of the clang-format project is more on the side of supporting a 2855limited set of styles really well as opposed to supporting every single style 2856used by a codebase somewhere in the wild. Of course, we do want to support all 2857major projects and thus have established the following bar for adding style 2858options. Each new style option must .. 2859 2860 * be used in a project of significant size (have dozens of contributors) 2861 * have a publicly accessible style guide 2862 * have a person willing to contribute and maintain patches 2863 2864Examples 2865======== 2866 2867A style similar to the `Linux Kernel style 2868<https://www.kernel.org/doc/Documentation/CodingStyle>`_: 2869 2870.. code-block:: yaml 2871 2872 BasedOnStyle: LLVM 2873 IndentWidth: 8 2874 UseTab: Always 2875 BreakBeforeBraces: Linux 2876 AllowShortIfStatementsOnASingleLine: false 2877 IndentCaseLabels: false 2878 2879The result is (imagine that tabs are used for indentation here): 2880 2881.. code-block:: c++ 2882 2883 void test() 2884 { 2885 switch (x) { 2886 case 0: 2887 case 1: 2888 do_something(); 2889 break; 2890 case 2: 2891 do_something_else(); 2892 break; 2893 default: 2894 break; 2895 } 2896 if (condition) 2897 do_something_completely_different(); 2898 2899 if (x == y) { 2900 q(); 2901 } else if (x > y) { 2902 w(); 2903 } else { 2904 r(); 2905 } 2906 } 2907 2908A style similar to the default Visual Studio formatting style: 2909 2910.. code-block:: yaml 2911 2912 UseTab: Never 2913 IndentWidth: 4 2914 BreakBeforeBraces: Allman 2915 AllowShortIfStatementsOnASingleLine: false 2916 IndentCaseLabels: false 2917 ColumnLimit: 0 2918 2919The result is: 2920 2921.. code-block:: c++ 2922 2923 void test() 2924 { 2925 switch (suffix) 2926 { 2927 case 0: 2928 case 1: 2929 do_something(); 2930 break; 2931 case 2: 2932 do_something_else(); 2933 break; 2934 default: 2935 break; 2936 } 2937 if (condition) 2938 do_somthing_completely_different(); 2939 2940 if (x == y) 2941 { 2942 q(); 2943 } 2944 else if (x > y) 2945 { 2946 w(); 2947 } 2948 else 2949 { 2950 r(); 2951 } 2952 } 2953