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**BinPackArguments** (``bool``) 764 If ``false``, a function call's arguments will either be all on the 765 same line or will have one line each. 766 767 .. code-block:: c++ 768 769 true: 770 void f() { 771 f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, 772 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 773 } 774 775 false: 776 void f() { 777 f(aaaaaaaaaaaaaaaaaaaa, 778 aaaaaaaaaaaaaaaaaaaa, 779 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 780 } 781 782**BinPackParameters** (``bool``) 783 If ``false``, a function declaration's or function definition's 784 parameters will either all be on the same line or will have one line each. 785 786 .. code-block:: c++ 787 788 true: 789 void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa, 790 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 791 792 false: 793 void f(int aaaaaaaaaaaaaaaaaaaa, 794 int aaaaaaaaaaaaaaaaaaaa, 795 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 796 797**BitFieldColonSpacing** (``BitFieldColonSpacingStyle``) 798 The BitFieldColonSpacingStyle to use for bitfields. 799 800 Possible values: 801 802 * ``BFCS_Both`` (in configuration: ``Both``) 803 Add one space on each side of the ``:`` 804 805 .. code-block:: c++ 806 807 unsigned bf : 2; 808 809 * ``BFCS_None`` (in configuration: ``None``) 810 Add no space around the ``:`` (except when needed for 811 ``AlignConsecutiveBitFields``). 812 813 .. code-block:: c++ 814 815 unsigned bf:2; 816 817 * ``BFCS_Before`` (in configuration: ``Before``) 818 Add space before the ``:`` only 819 820 .. code-block:: c++ 821 822 unsigned bf :2; 823 824 * ``BFCS_After`` (in configuration: ``After``) 825 Add space after the ``:`` only (space may be added before if 826 needed for ``AlignConsecutiveBitFields``). 827 828 .. code-block:: c++ 829 830 unsigned bf: 2; 831 832 833 834**BraceWrapping** (``BraceWrappingFlags``) 835 Control of individual brace wrapping cases. 836 837 If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how 838 each individual brace case should be handled. Otherwise, this is ignored. 839 840 .. code-block:: yaml 841 842 # Example of usage: 843 BreakBeforeBraces: Custom 844 BraceWrapping: 845 AfterEnum: true 846 AfterStruct: false 847 SplitEmptyFunction: false 848 849 Nested configuration flags: 850 851 852 * ``bool AfterCaseLabel`` Wrap case labels. 853 854 .. code-block:: c++ 855 856 false: true: 857 switch (foo) { vs. switch (foo) { 858 case 1: { case 1: 859 bar(); { 860 break; bar(); 861 } break; 862 default: { } 863 plop(); default: 864 } { 865 } plop(); 866 } 867 } 868 869 * ``bool AfterClass`` Wrap class definitions. 870 871 .. code-block:: c++ 872 873 true: 874 class foo {}; 875 876 false: 877 class foo 878 {}; 879 880 * ``BraceWrappingAfterControlStatementStyle AfterControlStatement`` 881 Wrap control statements (``if``/``for``/``while``/``switch``/..). 882 883 Possible values: 884 885 * ``BWACS_Never`` (in configuration: ``Never``) 886 Never wrap braces after a control statement. 887 888 .. code-block:: c++ 889 890 if (foo()) { 891 } else { 892 } 893 for (int i = 0; i < 10; ++i) { 894 } 895 896 * ``BWACS_MultiLine`` (in configuration: ``MultiLine``) 897 Only wrap braces after a multi-line control statement. 898 899 .. code-block:: c++ 900 901 if (foo && bar && 902 baz) 903 { 904 quux(); 905 } 906 while (foo || bar) { 907 } 908 909 * ``BWACS_Always`` (in configuration: ``Always``) 910 Always wrap braces after a control statement. 911 912 .. code-block:: c++ 913 914 if (foo()) 915 { 916 } else 917 {} 918 for (int i = 0; i < 10; ++i) 919 {} 920 921 922 * ``bool AfterEnum`` Wrap enum definitions. 923 924 .. code-block:: c++ 925 926 true: 927 enum X : int 928 { 929 B 930 }; 931 932 false: 933 enum X : int { B }; 934 935 * ``bool AfterFunction`` Wrap function definitions. 936 937 .. code-block:: c++ 938 939 true: 940 void foo() 941 { 942 bar(); 943 bar2(); 944 } 945 946 false: 947 void foo() { 948 bar(); 949 bar2(); 950 } 951 952 * ``bool AfterNamespace`` Wrap namespace definitions. 953 954 .. code-block:: c++ 955 956 true: 957 namespace 958 { 959 int foo(); 960 int bar(); 961 } 962 963 false: 964 namespace { 965 int foo(); 966 int bar(); 967 } 968 969 * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (interfaces, implementations...). 970 @autoreleasepool and @synchronized blocks are wrapped 971 according to `AfterControlStatement` flag. 972 973 * ``bool AfterStruct`` Wrap struct definitions. 974 975 .. code-block:: c++ 976 977 true: 978 struct foo 979 { 980 int x; 981 }; 982 983 false: 984 struct foo { 985 int x; 986 }; 987 988 * ``bool AfterUnion`` Wrap union definitions. 989 990 .. code-block:: c++ 991 992 true: 993 union foo 994 { 995 int x; 996 } 997 998 false: 999 union foo { 1000 int x; 1001 } 1002 1003 * ``bool AfterExternBlock`` Wrap extern blocks. 1004 1005 .. code-block:: c++ 1006 1007 true: 1008 extern "C" 1009 { 1010 int foo(); 1011 } 1012 1013 false: 1014 extern "C" { 1015 int foo(); 1016 } 1017 1018 * ``bool BeforeCatch`` Wrap before ``catch``. 1019 1020 .. code-block:: c++ 1021 1022 true: 1023 try { 1024 foo(); 1025 } 1026 catch () { 1027 } 1028 1029 false: 1030 try { 1031 foo(); 1032 } catch () { 1033 } 1034 1035 * ``bool BeforeElse`` Wrap before ``else``. 1036 1037 .. code-block:: c++ 1038 1039 true: 1040 if (foo()) { 1041 } 1042 else { 1043 } 1044 1045 false: 1046 if (foo()) { 1047 } else { 1048 } 1049 1050 * ``bool BeforeLambdaBody`` Wrap lambda block. 1051 1052 .. code-block:: c++ 1053 1054 true: 1055 connect( 1056 []() 1057 { 1058 foo(); 1059 bar(); 1060 }); 1061 1062 false: 1063 connect([]() { 1064 foo(); 1065 bar(); 1066 }); 1067 1068 * ``bool BeforeWhile`` Wrap before ``while``. 1069 1070 .. code-block:: c++ 1071 1072 true: 1073 do { 1074 foo(); 1075 } 1076 while (1); 1077 1078 false: 1079 do { 1080 foo(); 1081 } while (1); 1082 1083 * ``bool IndentBraces`` Indent the wrapped braces themselves. 1084 1085 * ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line. 1086 This option is used only if the opening brace of the function has 1087 already been wrapped, i.e. the `AfterFunction` brace wrapping mode is 1088 set, and the function could/should not be put on a single line (as per 1089 `AllowShortFunctionsOnASingleLine` and constructor formatting options). 1090 1091 .. code-block:: c++ 1092 1093 int f() vs. int f() 1094 {} { 1095 } 1096 1097 * ``bool SplitEmptyRecord`` If ``false``, empty record (e.g. class, struct or union) body 1098 can be put on a single line. This option is used only if the opening 1099 brace of the record has already been wrapped, i.e. the `AfterClass` 1100 (for classes) brace wrapping mode is set. 1101 1102 .. code-block:: c++ 1103 1104 class Foo vs. class Foo 1105 {} { 1106 } 1107 1108 * ``bool SplitEmptyNamespace`` If ``false``, empty namespace body can be put on a single line. 1109 This option is used only if the opening brace of the namespace has 1110 already been wrapped, i.e. the `AfterNamespace` brace wrapping mode is 1111 set. 1112 1113 .. code-block:: c++ 1114 1115 namespace Foo vs. namespace Foo 1116 {} { 1117 } 1118 1119 1120**BreakAfterJavaFieldAnnotations** (``bool``) 1121 Break after each annotation on a field in Java files. 1122 1123 .. code-block:: java 1124 1125 true: false: 1126 @Partial vs. @Partial @Mock DataLoad loader; 1127 @Mock 1128 DataLoad loader; 1129 1130**BreakBeforeBinaryOperators** (``BinaryOperatorStyle``) 1131 The way to wrap binary operators. 1132 1133 Possible values: 1134 1135 * ``BOS_None`` (in configuration: ``None``) 1136 Break after operators. 1137 1138 .. code-block:: c++ 1139 1140 LooooooooooongType loooooooooooooooooooooongVariable = 1141 someLooooooooooooooooongFunction(); 1142 1143 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + 1144 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == 1145 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && 1146 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > 1147 ccccccccccccccccccccccccccccccccccccccccc; 1148 1149 * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``) 1150 Break before operators that aren't assignments. 1151 1152 .. code-block:: c++ 1153 1154 LooooooooooongType loooooooooooooooooooooongVariable = 1155 someLooooooooooooooooongFunction(); 1156 1157 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1158 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1159 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1160 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1161 > ccccccccccccccccccccccccccccccccccccccccc; 1162 1163 * ``BOS_All`` (in configuration: ``All``) 1164 Break before operators. 1165 1166 .. code-block:: c++ 1167 1168 LooooooooooongType loooooooooooooooooooooongVariable 1169 = someLooooooooooooooooongFunction(); 1170 1171 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1172 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1173 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1174 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1175 > ccccccccccccccccccccccccccccccccccccccccc; 1176 1177 1178 1179**BreakBeforeBraces** (``BraceBreakingStyle``) 1180 The brace breaking style to use. 1181 1182 Possible values: 1183 1184 * ``BS_Attach`` (in configuration: ``Attach``) 1185 Always attach braces to surrounding context. 1186 1187 .. code-block:: c++ 1188 1189 try { 1190 foo(); 1191 } catch () { 1192 } 1193 void foo() { bar(); } 1194 class foo {}; 1195 if (foo()) { 1196 } else { 1197 } 1198 enum X : int { A, B }; 1199 1200 * ``BS_Linux`` (in configuration: ``Linux``) 1201 Like ``Attach``, but break before braces on function, namespace and 1202 class definitions. 1203 1204 .. code-block:: c++ 1205 1206 try { 1207 foo(); 1208 } catch () { 1209 } 1210 void foo() { bar(); } 1211 class foo 1212 { 1213 }; 1214 if (foo()) { 1215 } else { 1216 } 1217 enum X : int { A, B }; 1218 1219 * ``BS_Mozilla`` (in configuration: ``Mozilla``) 1220 Like ``Attach``, but break before braces on enum, function, and record 1221 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_Stroustrup`` (in configuration: ``Stroustrup``) 1239 Like ``Attach``, but break before function definitions, ``catch``, and 1240 ``else``. 1241 1242 .. code-block:: c++ 1243 1244 try { 1245 foo(); 1246 } 1247 catch () { 1248 } 1249 void foo() { bar(); } 1250 class foo { 1251 }; 1252 if (foo()) { 1253 } 1254 else { 1255 } 1256 enum X : int { A, B }; 1257 1258 * ``BS_Allman`` (in configuration: ``Allman``) 1259 Always break before braces. 1260 1261 .. code-block:: c++ 1262 1263 try 1264 { 1265 foo(); 1266 } 1267 catch () 1268 { 1269 } 1270 void foo() { bar(); } 1271 class foo 1272 { 1273 }; 1274 if (foo()) 1275 { 1276 } 1277 else 1278 { 1279 } 1280 enum X : int 1281 { 1282 A, 1283 B 1284 }; 1285 1286 * ``BS_Whitesmiths`` (in configuration: ``Whitesmiths``) 1287 Like ``Allman`` but always indent braces and line up code with braces. 1288 1289 .. code-block:: c++ 1290 1291 try 1292 { 1293 foo(); 1294 } 1295 catch () 1296 { 1297 } 1298 void foo() { bar(); } 1299 class foo 1300 { 1301 }; 1302 if (foo()) 1303 { 1304 } 1305 else 1306 { 1307 } 1308 enum X : int 1309 { 1310 A, 1311 B 1312 }; 1313 1314 * ``BS_GNU`` (in configuration: ``GNU``) 1315 Always break before braces and add an extra level of indentation to 1316 braces of control statements, not to those of class, function 1317 or other definitions. 1318 1319 .. code-block:: c++ 1320 1321 try 1322 { 1323 foo(); 1324 } 1325 catch () 1326 { 1327 } 1328 void foo() { bar(); } 1329 class foo 1330 { 1331 }; 1332 if (foo()) 1333 { 1334 } 1335 else 1336 { 1337 } 1338 enum X : int 1339 { 1340 A, 1341 B 1342 }; 1343 1344 * ``BS_WebKit`` (in configuration: ``WebKit``) 1345 Like ``Attach``, but break before functions. 1346 1347 .. code-block:: c++ 1348 1349 try { 1350 foo(); 1351 } catch () { 1352 } 1353 void foo() { bar(); } 1354 class foo { 1355 }; 1356 if (foo()) { 1357 } else { 1358 } 1359 enum X : int { A, B }; 1360 1361 * ``BS_Custom`` (in configuration: ``Custom``) 1362 Configure each individual brace in `BraceWrapping`. 1363 1364 1365 1366**BreakBeforeTernaryOperators** (``bool``) 1367 If ``true``, ternary operators will be placed after line breaks. 1368 1369 .. code-block:: c++ 1370 1371 true: 1372 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription 1373 ? firstValue 1374 : SecondValueVeryVeryVeryVeryLong; 1375 1376 false: 1377 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ? 1378 firstValue : 1379 SecondValueVeryVeryVeryVeryLong; 1380 1381**BreakConstructorInitializers** (``BreakConstructorInitializersStyle``) 1382 The constructor initializers style to use. 1383 1384 Possible values: 1385 1386 * ``BCIS_BeforeColon`` (in configuration: ``BeforeColon``) 1387 Break constructor initializers before the colon and after the commas. 1388 1389 .. code-block:: c++ 1390 1391 Constructor() 1392 : initializer1(), 1393 initializer2() 1394 1395 * ``BCIS_BeforeComma`` (in configuration: ``BeforeComma``) 1396 Break constructor initializers before the colon and commas, and align 1397 the commas with the colon. 1398 1399 .. code-block:: c++ 1400 1401 Constructor() 1402 : initializer1() 1403 , initializer2() 1404 1405 * ``BCIS_AfterColon`` (in configuration: ``AfterColon``) 1406 Break constructor initializers after the colon and commas. 1407 1408 .. code-block:: c++ 1409 1410 Constructor() : 1411 initializer1(), 1412 initializer2() 1413 1414 1415 1416**BreakInheritanceList** (``BreakInheritanceListStyle``) 1417 The inheritance list style to use. 1418 1419 Possible values: 1420 1421 * ``BILS_BeforeColon`` (in configuration: ``BeforeColon``) 1422 Break inheritance list before the colon and after the commas. 1423 1424 .. code-block:: c++ 1425 1426 class Foo 1427 : Base1, 1428 Base2 1429 {}; 1430 1431 * ``BILS_BeforeComma`` (in configuration: ``BeforeComma``) 1432 Break inheritance list before the colon and commas, and align 1433 the commas with the colon. 1434 1435 .. code-block:: c++ 1436 1437 class Foo 1438 : Base1 1439 , Base2 1440 {}; 1441 1442 * ``BILS_AfterColon`` (in configuration: ``AfterColon``) 1443 Break inheritance list after the colon and commas. 1444 1445 .. code-block:: c++ 1446 1447 class Foo : 1448 Base1, 1449 Base2 1450 {}; 1451 1452 1453 1454**BreakStringLiterals** (``bool``) 1455 Allow breaking string literals when formatting. 1456 1457 .. code-block:: c++ 1458 1459 true: 1460 const char* x = "veryVeryVeryVeryVeryVe" 1461 "ryVeryVeryVeryVeryVery" 1462 "VeryLongString"; 1463 1464 false: 1465 const char* x = 1466 "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString"; 1467 1468**ColumnLimit** (``unsigned``) 1469 The column limit. 1470 1471 A column limit of ``0`` means that there is no column limit. In this case, 1472 clang-format will respect the input's line breaking decisions within 1473 statements unless they contradict other rules. 1474 1475**CommentPragmas** (``std::string``) 1476 A regular expression that describes comments with special meaning, 1477 which should not be split into lines or otherwise changed. 1478 1479 .. code-block:: c++ 1480 1481 // CommentPragmas: '^ FOOBAR pragma:' 1482 // Will leave the following line unaffected 1483 #include <vector> // FOOBAR pragma: keep 1484 1485**CompactNamespaces** (``bool``) 1486 If ``true``, consecutive namespace declarations will be on the same 1487 line. If ``false``, each namespace is declared on a new line. 1488 1489 .. code-block:: c++ 1490 1491 true: 1492 namespace Foo { namespace Bar { 1493 }} 1494 1495 false: 1496 namespace Foo { 1497 namespace Bar { 1498 } 1499 } 1500 1501 If it does not fit on a single line, the overflowing namespaces get 1502 wrapped: 1503 1504 .. code-block:: c++ 1505 1506 namespace Foo { namespace Bar { 1507 namespace Extra { 1508 }}} 1509 1510**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``) 1511 If the constructor initializers don't fit on a line, put each 1512 initializer on its own line. 1513 1514 .. code-block:: c++ 1515 1516 true: 1517 SomeClass::Constructor() 1518 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 1519 return 0; 1520 } 1521 1522 false: 1523 SomeClass::Constructor() 1524 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), 1525 aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 1526 return 0; 1527 } 1528 1529**ConstructorInitializerIndentWidth** (``unsigned``) 1530 The number of characters to use for indentation of constructor 1531 initializer lists as well as inheritance lists. 1532 1533**ContinuationIndentWidth** (``unsigned``) 1534 Indent width for line continuations. 1535 1536 .. code-block:: c++ 1537 1538 ContinuationIndentWidth: 2 1539 1540 int i = // VeryVeryVeryVeryVeryLongComment 1541 longFunction( // Again a long comment 1542 arg); 1543 1544**Cpp11BracedListStyle** (``bool``) 1545 If ``true``, format braced lists as best suited for C++11 braced 1546 lists. 1547 1548 Important differences: 1549 - No spaces inside the braced list. 1550 - No line break before the closing brace. 1551 - Indentation with the continuation indent, not with the block indent. 1552 1553 Fundamentally, C++11 braced lists are formatted exactly like function 1554 calls would be formatted in their place. If the braced list follows a name 1555 (e.g. a type or variable name), clang-format formats as if the ``{}`` were 1556 the parentheses of a function call with that name. If there is no name, 1557 a zero-length name is assumed. 1558 1559 .. code-block:: c++ 1560 1561 true: false: 1562 vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 }; 1563 vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} }; 1564 f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]); 1565 new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 }; 1566 1567**DeriveLineEnding** (``bool``) 1568 Analyze the formatted file for the most used line ending (``\r\n`` 1569 or ``\n``). ``UseCRLF`` is only used as a fallback if none can be derived. 1570 1571**DerivePointerAlignment** (``bool``) 1572 If ``true``, analyze the formatted file for the most common 1573 alignment of ``&`` and ``*``. 1574 Pointer and reference alignment styles are going to be updated according 1575 to the preferences found in the file. 1576 ``PointerAlignment`` is then used only as fallback. 1577 1578**DisableFormat** (``bool``) 1579 Disables formatting completely. 1580 1581**ExperimentalAutoDetectBinPacking** (``bool``) 1582 If ``true``, clang-format detects whether function calls and 1583 definitions are formatted with one parameter per line. 1584 1585 Each call can be bin-packed, one-per-line or inconclusive. If it is 1586 inconclusive, e.g. completely on one line, but a decision needs to be 1587 made, clang-format analyzes whether there are other bin-packed cases in 1588 the input file and act accordingly. 1589 1590 NOTE: This is an experimental flag, that might go away or be renamed. Do 1591 not use this in config files, etc. Use at your own risk. 1592 1593**FixNamespaceComments** (``bool``) 1594 If ``true``, clang-format adds missing namespace end comments and 1595 fixes invalid existing ones. 1596 1597 .. code-block:: c++ 1598 1599 true: false: 1600 namespace a { vs. namespace a { 1601 foo(); foo(); 1602 } // namespace a } 1603 1604**ForEachMacros** (``std::vector<std::string>``) 1605 A vector of macros that should be interpreted as foreach loops 1606 instead of as function calls. 1607 1608 These are expected to be macros of the form: 1609 1610 .. code-block:: c++ 1611 1612 FOREACH(<variable-declaration>, ...) 1613 <loop-body> 1614 1615 In the .clang-format configuration file, this can be configured like: 1616 1617 .. code-block:: yaml 1618 1619 ForEachMacros: ['RANGES_FOR', 'FOREACH'] 1620 1621 For example: BOOST_FOREACH. 1622 1623**IncludeBlocks** (``IncludeBlocksStyle``) 1624 Dependent on the value, multiple ``#include`` blocks can be sorted 1625 as one and divided based on category. 1626 1627 Possible values: 1628 1629 * ``IBS_Preserve`` (in configuration: ``Preserve``) 1630 Sort each ``#include`` block separately. 1631 1632 .. code-block:: c++ 1633 1634 #include "b.h" into #include "b.h" 1635 1636 #include <lib/main.h> #include "a.h" 1637 #include "a.h" #include <lib/main.h> 1638 1639 * ``IBS_Merge`` (in configuration: ``Merge``) 1640 Merge multiple ``#include`` blocks together and sort as one. 1641 1642 .. code-block:: c++ 1643 1644 #include "b.h" into #include "a.h" 1645 #include "b.h" 1646 #include <lib/main.h> #include <lib/main.h> 1647 #include "a.h" 1648 1649 * ``IBS_Regroup`` (in configuration: ``Regroup``) 1650 Merge multiple ``#include`` blocks together and sort as one. 1651 Then split into groups based on category priority. See 1652 ``IncludeCategories``. 1653 1654 .. code-block:: c++ 1655 1656 #include "b.h" into #include "a.h" 1657 #include "b.h" 1658 #include <lib/main.h> 1659 #include "a.h" #include <lib/main.h> 1660 1661 1662 1663**IncludeCategories** (``std::vector<IncludeCategory>``) 1664 Regular expressions denoting the different ``#include`` categories 1665 used for ordering ``#includes``. 1666 1667 `POSIX extended 1668 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>`_ 1669 regular expressions are supported. 1670 1671 These regular expressions are matched against the filename of an include 1672 (including the <> or "") in order. The value belonging to the first 1673 matching regular expression is assigned and ``#includes`` are sorted first 1674 according to increasing category number and then alphabetically within 1675 each category. 1676 1677 If none of the regular expressions match, INT_MAX is assigned as 1678 category. The main header for a source file automatically gets category 0. 1679 so that it is generally kept at the beginning of the ``#includes`` 1680 (https://llvm.org/docs/CodingStandards.html#include-style). However, you 1681 can also assign negative priorities if you have certain headers that 1682 always need to be first. 1683 1684 There is a third and optional field ``SortPriority`` which can used while 1685 ``IncludeBloks = IBS_Regroup`` to define the priority in which ``#includes`` 1686 should be ordered, and value of ``Priority`` defines the order of 1687 ``#include blocks`` and also enables to group ``#includes`` of different 1688 priority for order.``SortPriority`` is set to the value of ``Priority`` 1689 as default if it is not assigned. 1690 1691 To configure this in the .clang-format file, use: 1692 1693 .. code-block:: yaml 1694 1695 IncludeCategories: 1696 - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 1697 Priority: 2 1698 SortPriority: 2 1699 - Regex: '^(<|"(gtest|gmock|isl|json)/)' 1700 Priority: 3 1701 - Regex: '<[[:alnum:].]+>' 1702 Priority: 4 1703 - Regex: '.*' 1704 Priority: 1 1705 SortPriority: 0 1706 1707**IncludeIsMainRegex** (``std::string``) 1708 Specify a regular expression of suffixes that are allowed in the 1709 file-to-main-include mapping. 1710 1711 When guessing whether a #include is the "main" include (to assign 1712 category 0, see above), use this regex of allowed suffixes to the header 1713 stem. A partial match is done, so that: 1714 - "" means "arbitrary suffix" 1715 - "$" means "no suffix" 1716 1717 For example, if configured to "(_test)?$", then a header a.h would be seen 1718 as the "main" include in both a.cc and a_test.cc. 1719 1720**IncludeIsMainSourceRegex** (``std::string``) 1721 Specify a regular expression for files being formatted 1722 that are allowed to be considered "main" in the 1723 file-to-main-include mapping. 1724 1725 By default, clang-format considers files as "main" only when they end 1726 with: ``.c``, ``.cc``, ``.cpp``, ``.c++``, ``.cxx``, ``.m`` or ``.mm`` 1727 extensions. 1728 For these files a guessing of "main" include takes place 1729 (to assign category 0, see above). This config option allows for 1730 additional suffixes and extensions for files to be considered as "main". 1731 1732 For example, if this option is configured to ``(Impl\.hpp)$``, 1733 then a file ``ClassImpl.hpp`` is considered "main" (in addition to 1734 ``Class.c``, ``Class.cc``, ``Class.cpp`` and so on) and "main 1735 include file" logic will be executed (with *IncludeIsMainRegex* setting 1736 also being respected in later phase). Without this option set, 1737 ``ClassImpl.hpp`` would not have the main include file put on top 1738 before any other include. 1739 1740**IndentCaseBlocks** (``bool``) 1741 Indent case label blocks one level from the case label. 1742 1743 When ``false``, the block following the case label uses the same 1744 indentation level as for the case label, treating the case label the same 1745 as an if-statement. 1746 When ``true``, the block gets indented as a scope block. 1747 1748 .. code-block:: c++ 1749 1750 false: true: 1751 switch (fool) { vs. switch (fool) { 1752 case 1: { case 1: 1753 bar(); { 1754 } break; bar(); 1755 default: { } 1756 plop(); break; 1757 } default: 1758 } { 1759 plop(); 1760 } 1761 } 1762 1763**IndentCaseLabels** (``bool``) 1764 Indent case labels one level from the switch statement. 1765 1766 When ``false``, use the same indentation level as for the switch 1767 statement. Switch statement body is always indented one level more than 1768 case labels (except the first block following the case label, which 1769 itself indents the code - unless IndentCaseBlocks is enabled). 1770 1771 .. code-block:: c++ 1772 1773 false: true: 1774 switch (fool) { vs. switch (fool) { 1775 case 1: case 1: 1776 bar(); bar(); 1777 break; break; 1778 default: default: 1779 plop(); plop(); 1780 } } 1781 1782**IndentExternBlock** (``IndentExternBlockStyle``) 1783 IndentExternBlockStyle is the type of indenting of extern blocks. 1784 1785 Possible values: 1786 1787 * ``IEBS_AfterExternBlock`` (in configuration: ``AfterExternBlock``) 1788 Backwards compatible with AfterExternBlock's indenting. 1789 1790 .. code-block:: c++ 1791 1792 IndentExternBlock: AfterExternBlock 1793 BraceWrapping.AfterExternBlock: true 1794 extern "C" 1795 { 1796 void foo(); 1797 } 1798 1799 1800 .. code-block:: c++ 1801 1802 IndentExternBlock: AfterExternBlock 1803 BraceWrapping.AfterExternBlock: false 1804 extern "C" { 1805 void foo(); 1806 } 1807 1808 * ``IEBS_NoIndent`` (in configuration: ``NoIndent``) 1809 Does not indent extern blocks. 1810 1811 .. code-block:: c++ 1812 1813 extern "C" { 1814 void foo(); 1815 } 1816 1817 * ``IEBS_Indent`` (in configuration: ``Indent``) 1818 Indents extern blocks. 1819 1820 .. code-block:: c++ 1821 1822 extern "C" { 1823 void foo(); 1824 } 1825 1826 1827 1828**IndentGotoLabels** (``bool``) 1829 Indent goto labels. 1830 1831 When ``false``, goto labels are flushed left. 1832 1833 .. code-block:: c++ 1834 1835 true: false: 1836 int f() { vs. int f() { 1837 if (foo()) { if (foo()) { 1838 label1: label1: 1839 bar(); bar(); 1840 } } 1841 label2: label2: 1842 return 1; return 1; 1843 } } 1844 1845**IndentPPDirectives** (``PPDirectiveIndentStyle``) 1846 The preprocessor directive indenting style to use. 1847 1848 Possible values: 1849 1850 * ``PPDIS_None`` (in configuration: ``None``) 1851 Does not indent any directives. 1852 1853 .. code-block:: c++ 1854 1855 #if FOO 1856 #if BAR 1857 #include <foo> 1858 #endif 1859 #endif 1860 1861 * ``PPDIS_AfterHash`` (in configuration: ``AfterHash``) 1862 Indents directives after the hash. 1863 1864 .. code-block:: c++ 1865 1866 #if FOO 1867 # if BAR 1868 # include <foo> 1869 # endif 1870 #endif 1871 1872 * ``PPDIS_BeforeHash`` (in configuration: ``BeforeHash``) 1873 Indents directives before the hash. 1874 1875 .. code-block:: c++ 1876 1877 #if FOO 1878 #if BAR 1879 #include <foo> 1880 #endif 1881 #endif 1882 1883 1884 1885**IndentWidth** (``unsigned``) 1886 The number of columns to use for indentation. 1887 1888 .. code-block:: c++ 1889 1890 IndentWidth: 3 1891 1892 void f() { 1893 someFunction(); 1894 if (true, false) { 1895 f(); 1896 } 1897 } 1898 1899**IndentWrappedFunctionNames** (``bool``) 1900 Indent if a function definition or declaration is wrapped after the 1901 type. 1902 1903 .. code-block:: c++ 1904 1905 true: 1906 LoooooooooooooooooooooooooooooooooooooooongReturnType 1907 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1908 1909 false: 1910 LoooooooooooooooooooooooooooooooooooooooongReturnType 1911 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1912 1913**InsertTrailingCommas** (``TrailingCommaStyle``) 1914 If set to ``TCS_Wrapped`` will insert trailing commas in container 1915 literals (arrays and objects) that wrap across multiple lines. 1916 It is currently only available for JavaScript 1917 and disabled by default ``TCS_None``. 1918 ``InsertTrailingCommas`` cannot be used together with ``BinPackArguments`` 1919 as inserting the comma disables bin-packing. 1920 1921 .. code-block:: c++ 1922 1923 TSC_Wrapped: 1924 const someArray = [ 1925 aaaaaaaaaaaaaaaaaaaaaaaaaa, 1926 aaaaaaaaaaaaaaaaaaaaaaaaaa, 1927 aaaaaaaaaaaaaaaaaaaaaaaaaa, 1928 // ^ inserted 1929 ] 1930 1931 Possible values: 1932 1933 * ``TCS_None`` (in configuration: ``None``) 1934 Do not insert trailing commas. 1935 1936 * ``TCS_Wrapped`` (in configuration: ``Wrapped``) 1937 Insert trailing commas in container literals that were wrapped over 1938 multiple lines. Note that this is conceptually incompatible with 1939 bin-packing, because the trailing comma is used as an indicator 1940 that a container should be formatted one-per-line (i.e. not bin-packed). 1941 So inserting a trailing comma counteracts bin-packing. 1942 1943 1944 1945**JavaImportGroups** (``std::vector<std::string>``) 1946 A vector of prefixes ordered by the desired groups for Java imports. 1947 1948 Each group is separated by a newline. Static imports will also follow the 1949 same grouping convention above all non-static imports. One group's prefix 1950 can be a subset of another - the longest prefix is always matched. Within 1951 a group, the imports are ordered lexicographically. 1952 1953 In the .clang-format configuration file, this can be configured like 1954 in the following yaml example. This will result in imports being 1955 formatted as in the Java example below. 1956 1957 .. code-block:: yaml 1958 1959 JavaImportGroups: ['com.example', 'com', 'org'] 1960 1961 1962 .. code-block:: java 1963 1964 import static com.example.function1; 1965 1966 import static com.test.function2; 1967 1968 import static org.example.function3; 1969 1970 import com.example.ClassA; 1971 import com.example.Test; 1972 import com.example.a.ClassB; 1973 1974 import com.test.ClassC; 1975 1976 import org.example.ClassD; 1977 1978**JavaScriptQuotes** (``JavaScriptQuoteStyle``) 1979 The JavaScriptQuoteStyle to use for JavaScript strings. 1980 1981 Possible values: 1982 1983 * ``JSQS_Leave`` (in configuration: ``Leave``) 1984 Leave string quotes as they are. 1985 1986 .. code-block:: js 1987 1988 string1 = "foo"; 1989 string2 = 'bar'; 1990 1991 * ``JSQS_Single`` (in configuration: ``Single``) 1992 Always use single quotes. 1993 1994 .. code-block:: js 1995 1996 string1 = 'foo'; 1997 string2 = 'bar'; 1998 1999 * ``JSQS_Double`` (in configuration: ``Double``) 2000 Always use double quotes. 2001 2002 .. code-block:: js 2003 2004 string1 = "foo"; 2005 string2 = "bar"; 2006 2007 2008 2009**JavaScriptWrapImports** (``bool``) 2010 Whether to wrap JavaScript import/export statements. 2011 2012 .. code-block:: js 2013 2014 true: 2015 import { 2016 VeryLongImportsAreAnnoying, 2017 VeryLongImportsAreAnnoying, 2018 VeryLongImportsAreAnnoying, 2019 } from 'some/module.js' 2020 2021 false: 2022 import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,} from "some/module.js" 2023 2024**KeepEmptyLinesAtTheStartOfBlocks** (``bool``) 2025 If true, the empty line at the start of blocks is kept. 2026 2027 .. code-block:: c++ 2028 2029 true: false: 2030 if (foo) { vs. if (foo) { 2031 bar(); 2032 bar(); } 2033 } 2034 2035**Language** (``LanguageKind``) 2036 Language, this format style is targeted at. 2037 2038 Possible values: 2039 2040 * ``LK_None`` (in configuration: ``None``) 2041 Do not use. 2042 2043 * ``LK_Cpp`` (in configuration: ``Cpp``) 2044 Should be used for C, C++. 2045 2046 * ``LK_CSharp`` (in configuration: ``CSharp``) 2047 Should be used for C#. 2048 2049 * ``LK_Java`` (in configuration: ``Java``) 2050 Should be used for Java. 2051 2052 * ``LK_JavaScript`` (in configuration: ``JavaScript``) 2053 Should be used for JavaScript. 2054 2055 * ``LK_ObjC`` (in configuration: ``ObjC``) 2056 Should be used for Objective-C, Objective-C++. 2057 2058 * ``LK_Proto`` (in configuration: ``Proto``) 2059 Should be used for Protocol Buffers 2060 (https://developers.google.com/protocol-buffers/). 2061 2062 * ``LK_TableGen`` (in configuration: ``TableGen``) 2063 Should be used for TableGen code. 2064 2065 * ``LK_TextProto`` (in configuration: ``TextProto``) 2066 Should be used for Protocol Buffer messages in text format 2067 (https://developers.google.com/protocol-buffers/). 2068 2069 2070 2071**MacroBlockBegin** (``std::string``) 2072 A regular expression matching macros that start a block. 2073 2074 .. code-block:: c++ 2075 2076 # With: 2077 MacroBlockBegin: "^NS_MAP_BEGIN|\ 2078 NS_TABLE_HEAD$" 2079 MacroBlockEnd: "^\ 2080 NS_MAP_END|\ 2081 NS_TABLE_.*_END$" 2082 2083 NS_MAP_BEGIN 2084 foo(); 2085 NS_MAP_END 2086 2087 NS_TABLE_HEAD 2088 bar(); 2089 NS_TABLE_FOO_END 2090 2091 # Without: 2092 NS_MAP_BEGIN 2093 foo(); 2094 NS_MAP_END 2095 2096 NS_TABLE_HEAD 2097 bar(); 2098 NS_TABLE_FOO_END 2099 2100**MacroBlockEnd** (``std::string``) 2101 A regular expression matching macros that end a block. 2102 2103**MaxEmptyLinesToKeep** (``unsigned``) 2104 The maximum number of consecutive empty lines to keep. 2105 2106 .. code-block:: c++ 2107 2108 MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0 2109 int f() { int f() { 2110 int = 1; int i = 1; 2111 i = foo(); 2112 i = foo(); return i; 2113 } 2114 return i; 2115 } 2116 2117**NamespaceIndentation** (``NamespaceIndentationKind``) 2118 The indentation used for namespaces. 2119 2120 Possible values: 2121 2122 * ``NI_None`` (in configuration: ``None``) 2123 Don't indent in namespaces. 2124 2125 .. code-block:: c++ 2126 2127 namespace out { 2128 int i; 2129 namespace in { 2130 int i; 2131 } 2132 } 2133 2134 * ``NI_Inner`` (in configuration: ``Inner``) 2135 Indent only in inner namespaces (nested in other namespaces). 2136 2137 .. code-block:: c++ 2138 2139 namespace out { 2140 int i; 2141 namespace in { 2142 int i; 2143 } 2144 } 2145 2146 * ``NI_All`` (in configuration: ``All``) 2147 Indent in all namespaces. 2148 2149 .. code-block:: c++ 2150 2151 namespace out { 2152 int i; 2153 namespace in { 2154 int i; 2155 } 2156 } 2157 2158 2159 2160**NamespaceMacros** (``std::vector<std::string>``) 2161 A vector of macros which are used to open namespace blocks. 2162 2163 These are expected to be macros of the form: 2164 2165 .. code-block:: c++ 2166 2167 NAMESPACE(<namespace-name>, ...) { 2168 <namespace-content> 2169 } 2170 2171 For example: TESTSUITE 2172 2173**ObjCBinPackProtocolList** (``BinPackStyle``) 2174 Controls bin-packing Objective-C protocol conformance list 2175 items into as few lines as possible when they go over ``ColumnLimit``. 2176 2177 If ``Auto`` (the default), delegates to the value in 2178 ``BinPackParameters``. If that is ``true``, bin-packs Objective-C 2179 protocol conformance list items into as few lines as possible 2180 whenever they go over ``ColumnLimit``. 2181 2182 If ``Always``, always bin-packs Objective-C protocol conformance 2183 list items into as few lines as possible whenever they go over 2184 ``ColumnLimit``. 2185 2186 If ``Never``, lays out Objective-C protocol conformance list items 2187 onto individual lines whenever they go over ``ColumnLimit``. 2188 2189 2190 .. code-block:: objc 2191 2192 Always (or Auto, if BinPackParameters=true): 2193 @interface ccccccccccccc () < 2194 ccccccccccccc, ccccccccccccc, 2195 ccccccccccccc, ccccccccccccc> { 2196 } 2197 2198 Never (or Auto, if BinPackParameters=false): 2199 @interface ddddddddddddd () < 2200 ddddddddddddd, 2201 ddddddddddddd, 2202 ddddddddddddd, 2203 ddddddddddddd> { 2204 } 2205 2206 Possible values: 2207 2208 * ``BPS_Auto`` (in configuration: ``Auto``) 2209 Automatically determine parameter bin-packing behavior. 2210 2211 * ``BPS_Always`` (in configuration: ``Always``) 2212 Always bin-pack parameters. 2213 2214 * ``BPS_Never`` (in configuration: ``Never``) 2215 Never bin-pack parameters. 2216 2217 2218 2219**ObjCBlockIndentWidth** (``unsigned``) 2220 The number of characters to use for indentation of ObjC blocks. 2221 2222 .. code-block:: objc 2223 2224 ObjCBlockIndentWidth: 4 2225 2226 [operation setCompletionBlock:^{ 2227 [self onOperationDone]; 2228 }]; 2229 2230**ObjCBreakBeforeNestedBlockParam** (``bool``) 2231 Break parameters list into lines when there is nested block 2232 parameters in a fuction call. 2233 2234 .. code-block:: c++ 2235 2236 false: 2237 - (void)_aMethod 2238 { 2239 [self.test1 t:self w:self callback:^(typeof(self) self, NSNumber 2240 *u, NSNumber *v) { 2241 u = c; 2242 }] 2243 } 2244 true: 2245 - (void)_aMethod 2246 { 2247 [self.test1 t:self 2248 w:self 2249 callback:^(typeof(self) self, NSNumber *u, NSNumber *v) { 2250 u = c; 2251 }] 2252 } 2253 2254**ObjCSpaceAfterProperty** (``bool``) 2255 Add a space after ``@property`` in Objective-C, i.e. use 2256 ``@property (readonly)`` instead of ``@property(readonly)``. 2257 2258**ObjCSpaceBeforeProtocolList** (``bool``) 2259 Add a space in front of an Objective-C protocol list, i.e. use 2260 ``Foo <Protocol>`` instead of ``Foo<Protocol>``. 2261 2262**PenaltyBreakAssignment** (``unsigned``) 2263 The penalty for breaking around an assignment operator. 2264 2265**PenaltyBreakBeforeFirstCallParameter** (``unsigned``) 2266 The penalty for breaking a function call after ``call(``. 2267 2268**PenaltyBreakComment** (``unsigned``) 2269 The penalty for each line break introduced inside a comment. 2270 2271**PenaltyBreakFirstLessLess** (``unsigned``) 2272 The penalty for breaking before the first ``<<``. 2273 2274**PenaltyBreakString** (``unsigned``) 2275 The penalty for each line break introduced inside a string literal. 2276 2277**PenaltyBreakTemplateDeclaration** (``unsigned``) 2278 The penalty for breaking after template declaration. 2279 2280**PenaltyExcessCharacter** (``unsigned``) 2281 The penalty for each character outside of the column limit. 2282 2283**PenaltyReturnTypeOnItsOwnLine** (``unsigned``) 2284 Penalty for putting the return type of a function onto its own 2285 line. 2286 2287**PointerAlignment** (``PointerAlignmentStyle``) 2288 Pointer and reference alignment style. 2289 2290 Possible values: 2291 2292 * ``PAS_Left`` (in configuration: ``Left``) 2293 Align pointer to the left. 2294 2295 .. code-block:: c++ 2296 2297 int* a; 2298 2299 * ``PAS_Right`` (in configuration: ``Right``) 2300 Align pointer to the right. 2301 2302 .. code-block:: c++ 2303 2304 int *a; 2305 2306 * ``PAS_Middle`` (in configuration: ``Middle``) 2307 Align pointer in the middle. 2308 2309 .. code-block:: c++ 2310 2311 int * a; 2312 2313 2314 2315**RawStringFormats** (``std::vector<RawStringFormat>``) 2316 Defines hints for detecting supported languages code blocks in raw 2317 strings. 2318 2319 A raw string with a matching delimiter or a matching enclosing function 2320 name will be reformatted assuming the specified language based on the 2321 style for that language defined in the .clang-format file. If no style has 2322 been defined in the .clang-format file for the specific language, a 2323 predefined style given by 'BasedOnStyle' is used. If 'BasedOnStyle' is not 2324 found, the formatting is based on llvm style. A matching delimiter takes 2325 precedence over a matching enclosing function name for determining the 2326 language of the raw string contents. 2327 2328 If a canonical delimiter is specified, occurrences of other delimiters for 2329 the same language will be updated to the canonical if possible. 2330 2331 There should be at most one specification per language and each delimiter 2332 and enclosing function should not occur in multiple specifications. 2333 2334 To configure this in the .clang-format file, use: 2335 2336 .. code-block:: yaml 2337 2338 RawStringFormats: 2339 - Language: TextProto 2340 Delimiters: 2341 - 'pb' 2342 - 'proto' 2343 EnclosingFunctions: 2344 - 'PARSE_TEXT_PROTO' 2345 BasedOnStyle: google 2346 - Language: Cpp 2347 Delimiters: 2348 - 'cc' 2349 - 'cpp' 2350 BasedOnStyle: llvm 2351 CanonicalDelimiter: 'cc' 2352 2353**ReflowComments** (``bool``) 2354 If ``true``, clang-format will attempt to re-flow comments. 2355 2356 .. code-block:: c++ 2357 2358 false: 2359 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information 2360 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */ 2361 2362 true: 2363 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 2364 // information 2365 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 2366 * information */ 2367 2368**SortIncludes** (``bool``) 2369 If ``true``, clang-format will sort ``#includes``. 2370 2371 .. code-block:: c++ 2372 2373 false: true: 2374 #include "b.h" vs. #include "a.h" 2375 #include "a.h" #include "b.h" 2376 2377**SortUsingDeclarations** (``bool``) 2378 If ``true``, clang-format will sort using declarations. 2379 2380 The order of using declarations is defined as follows: 2381 Split the strings by "::" and discard any initial empty strings. The last 2382 element of each list is a non-namespace name; all others are namespace 2383 names. Sort the lists of names lexicographically, where the sort order of 2384 individual names is that all non-namespace names come before all namespace 2385 names, and within those groups, names are in case-insensitive 2386 lexicographic order. 2387 2388 .. code-block:: c++ 2389 2390 false: true: 2391 using std::cout; vs. using std::cin; 2392 using std::cin; using std::cout; 2393 2394**SpaceAfterCStyleCast** (``bool``) 2395 If ``true``, a space is inserted after C style casts. 2396 2397 .. code-block:: c++ 2398 2399 true: false: 2400 (int) i; vs. (int)i; 2401 2402**SpaceAfterLogicalNot** (``bool``) 2403 If ``true``, a space is inserted after the logical not operator (``!``). 2404 2405 .. code-block:: c++ 2406 2407 true: false: 2408 ! someExpression(); vs. !someExpression(); 2409 2410**SpaceAfterTemplateKeyword** (``bool``) 2411 If ``true``, a space will be inserted after the 'template' keyword. 2412 2413 .. code-block:: c++ 2414 2415 true: false: 2416 template <int> void foo(); vs. template<int> void foo(); 2417 2418**SpaceBeforeAssignmentOperators** (``bool``) 2419 If ``false``, spaces will be removed before assignment operators. 2420 2421 .. code-block:: c++ 2422 2423 true: false: 2424 int a = 5; vs. int a= 5; 2425 a += 42; a+= 42; 2426 2427**SpaceBeforeCpp11BracedList** (``bool``) 2428 If ``true``, a space will be inserted before a C++11 braced list 2429 used to initialize an object (after the preceding identifier or type). 2430 2431 .. code-block:: c++ 2432 2433 true: false: 2434 Foo foo { bar }; vs. Foo foo{ bar }; 2435 Foo {}; Foo{}; 2436 vector<int> { 1, 2, 3 }; vector<int>{ 1, 2, 3 }; 2437 new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 }; 2438 2439**SpaceBeforeCtorInitializerColon** (``bool``) 2440 If ``false``, spaces will be removed before constructor initializer 2441 colon. 2442 2443 .. code-block:: c++ 2444 2445 true: false: 2446 Foo::Foo() : a(a) {} Foo::Foo(): a(a) {} 2447 2448**SpaceBeforeInheritanceColon** (``bool``) 2449 If ``false``, spaces will be removed before inheritance colon. 2450 2451 .. code-block:: c++ 2452 2453 true: false: 2454 class Foo : Bar {} vs. class Foo: Bar {} 2455 2456**SpaceBeforeParens** (``SpaceBeforeParensOptions``) 2457 Defines in which cases to put a space before opening parentheses. 2458 2459 Possible values: 2460 2461 * ``SBPO_Never`` (in configuration: ``Never``) 2462 Never put a space before opening parentheses. 2463 2464 .. code-block:: c++ 2465 2466 void f() { 2467 if(true) { 2468 f(); 2469 } 2470 } 2471 2472 * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) 2473 Put a space before opening parentheses only after control statement 2474 keywords (``for/if/while...``). 2475 2476 .. code-block:: c++ 2477 2478 void f() { 2479 if (true) { 2480 f(); 2481 } 2482 } 2483 2484 * ``SBPO_ControlStatementsExceptForEachMacros`` (in configuration: ``ControlStatementsExceptForEachMacros``) 2485 Same as ``SBPO_ControlStatements`` except this option doesn't apply to 2486 ForEach macros. This is useful in projects where ForEach macros are 2487 treated as function calls instead of control statements. 2488 2489 .. code-block:: c++ 2490 2491 void f() { 2492 Q_FOREACH(...) { 2493 f(); 2494 } 2495 } 2496 2497 * ``SBPO_NonEmptyParentheses`` (in configuration: ``NonEmptyParentheses``) 2498 Put a space before opening parentheses only if the parentheses are not 2499 empty i.e. '()' 2500 2501 .. code-block:: c++ 2502 2503 void() { 2504 if (true) { 2505 f(); 2506 g (x, y, z); 2507 } 2508 } 2509 2510 * ``SBPO_Always`` (in configuration: ``Always``) 2511 Always put a space before opening parentheses, except when it's 2512 prohibited by the syntax rules (in function-like macro definitions) or 2513 when determined by other style rules (after unary operators, opening 2514 parentheses, etc.) 2515 2516 .. code-block:: c++ 2517 2518 void f () { 2519 if (true) { 2520 f (); 2521 } 2522 } 2523 2524 2525 2526**SpaceBeforeRangeBasedForLoopColon** (``bool``) 2527 If ``false``, spaces will be removed before range-based for loop 2528 colon. 2529 2530 .. code-block:: c++ 2531 2532 true: false: 2533 for (auto v : values) {} vs. for(auto v: values) {} 2534 2535**SpaceBeforeSquareBrackets** (``bool``) 2536 If ``true``, spaces will be before ``[``. 2537 Lambdas will not be affected. Only the first ``[`` will get a space added. 2538 2539 .. code-block:: c++ 2540 2541 true: false: 2542 int a [5]; vs. int a[5]; 2543 int a [5][5]; vs. int a[5][5]; 2544 2545**SpaceInEmptyBlock** (``bool``) 2546 If ``true``, spaces will be inserted into ``{}``. 2547 2548 .. code-block:: c++ 2549 2550 true: false: 2551 void f() { } vs. void f() {} 2552 while (true) { } while (true) {} 2553 2554**SpaceInEmptyParentheses** (``bool``) 2555 If ``true``, spaces may be inserted into ``()``. 2556 2557 .. code-block:: c++ 2558 2559 true: false: 2560 void f( ) { vs. void f() { 2561 int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; 2562 if (true) { if (true) { 2563 f( ); f(); 2564 } } 2565 } } 2566 2567**SpacesBeforeTrailingComments** (``unsigned``) 2568 The number of spaces before trailing line comments 2569 (``//`` - comments). 2570 2571 This does not affect trailing block comments (``/*`` - comments) as 2572 those commonly have different usage patterns and a number of special 2573 cases. 2574 2575 .. code-block:: c++ 2576 2577 SpacesBeforeTrailingComments: 3 2578 void f() { 2579 if (true) { // foo1 2580 f(); // bar 2581 } // foo 2582 } 2583 2584**SpacesInAngles** (``bool``) 2585 If ``true``, spaces will be inserted after ``<`` and before ``>`` 2586 in template argument lists. 2587 2588 .. code-block:: c++ 2589 2590 true: false: 2591 static_cast< int >(arg); vs. static_cast<int>(arg); 2592 std::function< void(int) > fct; std::function<void(int)> fct; 2593 2594**SpacesInCStyleCastParentheses** (``bool``) 2595 If ``true``, spaces may be inserted into C style casts. 2596 2597 .. code-block:: c++ 2598 2599 true: false: 2600 x = ( int32 )y vs. x = (int32)y 2601 2602**SpacesInConditionalStatement** (``bool``) 2603 If ``true``, spaces will be inserted around if/for/switch/while 2604 conditions. 2605 2606 .. code-block:: c++ 2607 2608 true: false: 2609 if ( a ) { ... } vs. if (a) { ... } 2610 while ( i < 5 ) { ... } while (i < 5) { ... } 2611 2612**SpacesInContainerLiterals** (``bool``) 2613 If ``true``, spaces are inserted inside container literals (e.g. 2614 ObjC and Javascript array and dict literals). 2615 2616 .. code-block:: js 2617 2618 true: false: 2619 var arr = [ 1, 2, 3 ]; vs. var arr = [1, 2, 3]; 2620 f({a : 1, b : 2, c : 3}); f({a: 1, b: 2, c: 3}); 2621 2622**SpacesInParentheses** (``bool``) 2623 If ``true``, spaces will be inserted after ``(`` and before ``)``. 2624 2625 .. code-block:: c++ 2626 2627 true: false: 2628 t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; 2629 2630**SpacesInSquareBrackets** (``bool``) 2631 If ``true``, spaces will be inserted after ``[`` and before ``]``. 2632 Lambdas without arguments or unspecified size array declarations will not 2633 be affected. 2634 2635 .. code-block:: c++ 2636 2637 true: false: 2638 int a[ 5 ]; vs. int a[5]; 2639 std::unique_ptr<int[]> foo() {} // Won't be affected 2640 2641**Standard** (``LanguageStandard``) 2642 Parse and format C++ constructs compatible with this standard. 2643 2644 .. code-block:: c++ 2645 2646 c++03: latest: 2647 vector<set<int> > x; vs. vector<set<int>> x; 2648 2649 Possible values: 2650 2651 * ``LS_Cpp03`` (in configuration: ``c++03``) 2652 Parse and format as C++03. 2653 ``Cpp03`` is a deprecated alias for ``c++03`` 2654 2655 * ``LS_Cpp11`` (in configuration: ``c++11``) 2656 Parse and format as C++11. 2657 2658 * ``LS_Cpp14`` (in configuration: ``c++14``) 2659 Parse and format as C++14. 2660 2661 * ``LS_Cpp17`` (in configuration: ``c++17``) 2662 Parse and format as C++17. 2663 2664 * ``LS_Cpp20`` (in configuration: ``c++20``) 2665 Parse and format as C++20. 2666 2667 * ``LS_Latest`` (in configuration: ``Latest``) 2668 Parse and format using the latest supported language version. 2669 ``Cpp11`` is a deprecated alias for ``Latest`` 2670 2671 * ``LS_Auto`` (in configuration: ``Auto``) 2672 Automatic detection based on the input. 2673 2674 2675 2676**StatementMacros** (``std::vector<std::string>``) 2677 A vector of macros that should be interpreted as complete 2678 statements. 2679 2680 Typical macros are expressions, and require a semi-colon to be 2681 added; sometimes this is not the case, and this allows to make 2682 clang-format aware of such cases. 2683 2684 For example: Q_UNUSED 2685 2686**TabWidth** (``unsigned``) 2687 The number of columns used for tab stops. 2688 2689**TypenameMacros** (``std::vector<std::string>``) 2690 A vector of macros that should be interpreted as type declarations 2691 instead of as function calls. 2692 2693 These are expected to be macros of the form: 2694 2695 .. code-block:: c++ 2696 2697 STACK_OF(...) 2698 2699 In the .clang-format configuration file, this can be configured like: 2700 2701 .. code-block:: yaml 2702 2703 TypenameMacros: ['STACK_OF', 'LIST'] 2704 2705 For example: OpenSSL STACK_OF, BSD LIST_ENTRY. 2706 2707**UseCRLF** (``bool``) 2708 Use ``\r\n`` instead of ``\n`` for line breaks. 2709 Also used as fallback if ``DeriveLineEnding`` is true. 2710 2711**UseTab** (``UseTabStyle``) 2712 The way to use tab characters in the resulting file. 2713 2714 Possible values: 2715 2716 * ``UT_Never`` (in configuration: ``Never``) 2717 Never use tab. 2718 2719 * ``UT_ForIndentation`` (in configuration: ``ForIndentation``) 2720 Use tabs only for indentation. 2721 2722 * ``UT_ForContinuationAndIndentation`` (in configuration: ``ForContinuationAndIndentation``) 2723 Fill all leading whitespace with tabs, and use spaces for alignment that 2724 appears within a line (e.g. consecutive assignments and declarations). 2725 2726 * ``UT_AlignWithSpaces`` (in configuration: ``AlignWithSpaces``) 2727 Use tabs for line continuation and indentation, and spaces for 2728 alignment. 2729 2730 * ``UT_Always`` (in configuration: ``Always``) 2731 Use tabs whenever we need to fill whitespace that spans at least from 2732 one tab stop to the next one. 2733 2734 2735 2736**WhitespaceSensitiveMacros** (``std::vector<std::string>``) 2737 A vector of macros which are whitespace-sensitive and should not 2738 be touched. 2739 2740 These are expected to be macros of the form: 2741 2742 .. code-block:: c++ 2743 2744 STRINGIZE(...) 2745 2746 In the .clang-format configuration file, this can be configured like: 2747 2748 .. code-block:: yaml 2749 2750 WhitespaceSensitiveMacros: ['STRINGIZE', 'PP_STRINGIZE'] 2751 2752 For example: BOOST_PP_STRINGIZE 2753 2754.. END_FORMAT_STYLE_OPTIONS 2755 2756Adding additional style options 2757=============================== 2758 2759Each additional style option adds costs to the clang-format project. Some of 2760these costs affect the clang-format development itself, as we need to make 2761sure that any given combination of options work and that new features don't 2762break any of the existing options in any way. There are also costs for end users 2763as options become less discoverable and people have to think about and make a 2764decision on options they don't really care about. 2765 2766The goal of the clang-format project is more on the side of supporting a 2767limited set of styles really well as opposed to supporting every single style 2768used by a codebase somewhere in the wild. Of course, we do want to support all 2769major projects and thus have established the following bar for adding style 2770options. Each new style option must .. 2771 2772 * be used in a project of significant size (have dozens of contributors) 2773 * have a publicly accessible style guide 2774 * have a person willing to contribute and maintain patches 2775 2776Examples 2777======== 2778 2779A style similar to the `Linux Kernel style 2780<https://www.kernel.org/doc/Documentation/CodingStyle>`_: 2781 2782.. code-block:: yaml 2783 2784 BasedOnStyle: LLVM 2785 IndentWidth: 8 2786 UseTab: Always 2787 BreakBeforeBraces: Linux 2788 AllowShortIfStatementsOnASingleLine: false 2789 IndentCaseLabels: false 2790 2791The result is (imagine that tabs are used for indentation here): 2792 2793.. code-block:: c++ 2794 2795 void test() 2796 { 2797 switch (x) { 2798 case 0: 2799 case 1: 2800 do_something(); 2801 break; 2802 case 2: 2803 do_something_else(); 2804 break; 2805 default: 2806 break; 2807 } 2808 if (condition) 2809 do_something_completely_different(); 2810 2811 if (x == y) { 2812 q(); 2813 } else if (x > y) { 2814 w(); 2815 } else { 2816 r(); 2817 } 2818 } 2819 2820A style similar to the default Visual Studio formatting style: 2821 2822.. code-block:: yaml 2823 2824 UseTab: Never 2825 IndentWidth: 4 2826 BreakBeforeBraces: Allman 2827 AllowShortIfStatementsOnASingleLine: false 2828 IndentCaseLabels: false 2829 ColumnLimit: 0 2830 2831The result is: 2832 2833.. code-block:: c++ 2834 2835 void test() 2836 { 2837 switch (suffix) 2838 { 2839 case 0: 2840 case 1: 2841 do_something(); 2842 break; 2843 case 2: 2844 do_something_else(); 2845 break; 2846 default: 2847 break; 2848 } 2849 if (condition) 2850 do_somthing_completely_different(); 2851 2852 if (x == y) 2853 { 2854 q(); 2855 } 2856 else if (x > y) 2857 { 2858 w(); 2859 } 2860 else 2861 { 2862 r(); 2863 } 2864 } 2865