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 <http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml>`_ 142 * ``Chromium`` 143 A style complying with `Chromium's style guide 144 <https://www.chromium.org/developers/coding-style>`_ 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 155.. START_FORMAT_STYLE_OPTIONS 156 157**AccessModifierOffset** (``int``) 158 The extra indent or outdent of access modifiers, e.g. ``public:``. 159 160**AlignAfterOpenBracket** (``BracketAlignmentStyle``) 161 If ``true``, horizontally aligns arguments after an open bracket. 162 163 This applies to round brackets (parentheses), angle brackets and square 164 brackets. 165 166 Possible values: 167 168 * ``BAS_Align`` (in configuration: ``Align``) 169 Align parameters on the open bracket, e.g.: 170 171 .. code-block:: c++ 172 173 someLongFunction(argument1, 174 argument2); 175 176 * ``BAS_DontAlign`` (in configuration: ``DontAlign``) 177 Don't align, instead use ``ContinuationIndentWidth``, e.g.: 178 179 .. code-block:: c++ 180 181 someLongFunction(argument1, 182 argument2); 183 184 * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``) 185 Always break after an open bracket, if the parameters don't fit 186 on a single line, e.g.: 187 188 .. code-block:: c++ 189 190 someLongFunction( 191 argument1, argument2); 192 193 194 195**AlignConsecutiveAssignments** (``bool``) 196 If ``true``, aligns consecutive assignments. 197 198 This will align the assignment operators of consecutive lines. This 199 will result in formattings like 200 201 .. code-block:: c++ 202 203 int aaaa = 12; 204 int b = 23; 205 int ccc = 23; 206 207**AlignConsecutiveDeclarations** (``bool``) 208 If ``true``, aligns consecutive declarations. 209 210 This will align the declaration names of consecutive lines. This 211 will result in formattings like 212 213 .. code-block:: c++ 214 215 int aaaa = 12; 216 float b = 23; 217 std::string ccc = 23; 218 219**AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``) 220 Options for aligning backslashes in escaped newlines. 221 222 Possible values: 223 224 * ``ENAS_DontAlign`` (in configuration: ``DontAlign``) 225 Don't align escaped newlines. 226 227 .. code-block:: c++ 228 229 #define A \ 230 int aaaa; \ 231 int b; \ 232 int dddddddddd; 233 234 * ``ENAS_Left`` (in configuration: ``Left``) 235 Align escaped newlines as far left as possible. 236 237 .. code-block:: c++ 238 239 true: 240 #define A \ 241 int aaaa; \ 242 int b; \ 243 int dddddddddd; 244 245 false: 246 247 * ``ENAS_Right`` (in configuration: ``Right``) 248 Align escaped newlines in the right-most column. 249 250 .. code-block:: c++ 251 252 #define A \ 253 int aaaa; \ 254 int b; \ 255 int dddddddddd; 256 257 258 259**AlignOperands** (``bool``) 260 If ``true``, horizontally align operands of binary and ternary 261 expressions. 262 263 Specifically, this aligns operands of a single expression that needs to be 264 split over multiple lines, e.g.: 265 266 .. code-block:: c++ 267 268 int aaa = bbbbbbbbbbbbbbb + 269 ccccccccccccccc; 270 271**AlignTrailingComments** (``bool``) 272 If ``true``, aligns trailing comments. 273 274 .. code-block:: c++ 275 276 true: false: 277 int a; // My comment a vs. int a; // My comment a 278 int b = 2; // comment b int b = 2; // comment about b 279 280**AllowAllArgumentsOnNextLine** (``bool``) 281 If a function call or braced initializer list doesn't fit on a 282 line, allow putting all arguments onto the next line, even if 283 ``BinPackArguments`` is ``false``. 284 285 .. code-block:: c++ 286 287 true: 288 callFunction( 289 a, b, c, d); 290 291 false: 292 callFunction(a, 293 b, 294 c, 295 d); 296 297**AllowAllConstructorInitializersOnNextLine** (``bool``) 298 If a constructor definition with a member initializer list doesn't 299 fit on a single line, allow putting all member initializers onto the next 300 line, if ```ConstructorInitializerAllOnOneLineOrOnePerLine``` is true. 301 Note that this parameter has no effect if 302 ```ConstructorInitializerAllOnOneLineOrOnePerLine``` is false. 303 304 .. code-block:: c++ 305 306 true: 307 MyClass::MyClass() : 308 member0(0), member1(2) {} 309 310 false: 311 MyClass::MyClass() : 312 member0(0), 313 member1(2) {} 314 315**AllowAllParametersOfDeclarationOnNextLine** (``bool``) 316 If the function declaration doesn't fit on a line, 317 allow putting all parameters of a function declaration onto 318 the next line even if ``BinPackParameters`` is ``false``. 319 320 .. code-block:: c++ 321 322 true: 323 void myFunction( 324 int a, int b, int c, int d, int e); 325 326 false: 327 void myFunction(int a, 328 int b, 329 int c, 330 int d, 331 int e); 332 333**AllowShortBlocksOnASingleLine** (``bool``) 334 Allows contracting simple braced statements to a single line. 335 336 E.g., this allows ``if (a) { return; }`` to be put on a single line. 337 338**AllowShortCaseLabelsOnASingleLine** (``bool``) 339 If ``true``, short case labels will be contracted to a single line. 340 341 .. code-block:: c++ 342 343 true: false: 344 switch (a) { vs. switch (a) { 345 case 1: x = 1; break; case 1: 346 case 2: return; x = 1; 347 } break; 348 case 2: 349 return; 350 } 351 352**AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``) 353 Dependent on the value, ``int f() { return 0; }`` can be put on a 354 single line. 355 356 Possible values: 357 358 * ``SFS_None`` (in configuration: ``None``) 359 Never merge functions into a single line. 360 361 * ``SFS_InlineOnly`` (in configuration: ``InlineOnly``) 362 Only merge functions defined inside a class. Same as "inline", 363 except it does not implies "empty": i.e. top level empty functions 364 are not merged either. 365 366 .. code-block:: c++ 367 368 class Foo { 369 void f() { foo(); } 370 }; 371 void f() { 372 foo(); 373 } 374 void f() { 375 } 376 377 * ``SFS_Empty`` (in configuration: ``Empty``) 378 Only merge empty functions. 379 380 .. code-block:: c++ 381 382 void f() {} 383 void f2() { 384 bar2(); 385 } 386 387 * ``SFS_Inline`` (in configuration: ``Inline``) 388 Only merge functions defined inside a class. Implies "empty". 389 390 .. code-block:: c++ 391 392 class Foo { 393 void f() { foo(); } 394 }; 395 void f() { 396 foo(); 397 } 398 void f() {} 399 400 * ``SFS_All`` (in configuration: ``All``) 401 Merge all functions fitting on a single line. 402 403 .. code-block:: c++ 404 405 class Foo { 406 void f() { foo(); } 407 }; 408 void f() { bar(); } 409 410**AllowShortIfStatementsOnASingleLine** (``ShortIfStyle``) 411 Dependent on the value, ``if (a) return 0;`` can be put on a 412 single line. 413 414 Possible values: 415 416 * ``SIS_Never`` (in configuration: ``Never``) 417 Do not allow short if functions. 418 419 .. code-block:: c++ 420 421 if (a) 422 return; 423 else 424 return; 425 426 * ``SIS_WithoutElse`` (in configuration: ``WithoutElse``) 427 Allow short if functions on the same line, as long as else 428 is not a compound statement. 429 430 .. code-block:: c++ 431 432 if (a) return; 433 else 434 return; 435 436 if (a) 437 return; 438 else { 439 return; 440 } 441 442 * ``SIS_Always`` (in configuration: ``Always``) 443 Allow short if statements even if the else is a compound statement. 444 445 .. code-block:: c++ 446 447 if (a) return; 448 else { 449 return; 450 } 451 452**AllowShortLoopsOnASingleLine** (``bool``) 453 If ``true``, ``while (true) continue;`` can be put on a single 454 line. 455 456**AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``) 457 The function definition return type breaking style to use. This 458 option is **deprecated** and is retained for backwards compatibility. 459 460 Possible values: 461 462 * ``DRTBS_None`` (in configuration: ``None``) 463 Break after return type automatically. 464 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. 465 466 * ``DRTBS_All`` (in configuration: ``All``) 467 Always break after the return type. 468 469 * ``DRTBS_TopLevel`` (in configuration: ``TopLevel``) 470 Always break after the return types of top-level functions. 471 472 473 474**AlwaysBreakAfterReturnType** (``ReturnTypeBreakingStyle``) 475 The function declaration return type breaking style to use. 476 477 Possible values: 478 479 * ``RTBS_None`` (in configuration: ``None``) 480 Break after return type automatically. 481 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. 482 483 .. code-block:: c++ 484 485 class A { 486 int f() { return 0; }; 487 }; 488 int f(); 489 int f() { return 1; } 490 491 * ``RTBS_All`` (in configuration: ``All``) 492 Always break after the return type. 493 494 .. code-block:: c++ 495 496 class A { 497 int 498 f() { 499 return 0; 500 }; 501 }; 502 int 503 f(); 504 int 505 f() { 506 return 1; 507 } 508 509 * ``RTBS_TopLevel`` (in configuration: ``TopLevel``) 510 Always break after the return types of top-level functions. 511 512 .. code-block:: c++ 513 514 class A { 515 int f() { return 0; }; 516 }; 517 int 518 f(); 519 int 520 f() { 521 return 1; 522 } 523 524 * ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``) 525 Always break after the return type of function definitions. 526 527 .. code-block:: c++ 528 529 class A { 530 int 531 f() { 532 return 0; 533 }; 534 }; 535 int f(); 536 int 537 f() { 538 return 1; 539 } 540 541 * ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``) 542 Always break after the return type of top-level definitions. 543 544 .. code-block:: c++ 545 546 class A { 547 int f() { return 0; }; 548 }; 549 int f(); 550 int 551 f() { 552 return 1; 553 } 554 555 556 557**AlwaysBreakBeforeMultilineStrings** (``bool``) 558 If ``true``, always break before multiline string literals. 559 560 This flag is mean to make cases where there are multiple multiline strings 561 in a file look more consistent. Thus, it will only take effect if wrapping 562 the string at that point leads to it being indented 563 ``ContinuationIndentWidth`` spaces from the start of the line. 564 565 .. code-block:: c++ 566 567 true: false: 568 aaaa = vs. aaaa = "bbbb" 569 "bbbb" "cccc"; 570 "cccc"; 571 572**AlwaysBreakTemplateDeclarations** (``BreakTemplateDeclarationsStyle``) 573 The template declaration breaking style to use. 574 575 Possible values: 576 577 * ``BTDS_No`` (in configuration: ``No``) 578 Do not force break before declaration. 579 ``PenaltyBreakTemplateDeclaration`` is taken into account. 580 581 .. code-block:: c++ 582 583 template <typename T> T foo() { 584 } 585 template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa, 586 int bbbbbbbbbbbbbbbbbbbbb) { 587 } 588 589 * ``BTDS_MultiLine`` (in configuration: ``MultiLine``) 590 Force break after template declaration only when the following 591 declaration spans multiple lines. 592 593 .. code-block:: c++ 594 595 template <typename T> T foo() { 596 } 597 template <typename T> 598 T foo(int aaaaaaaaaaaaaaaaaaaaa, 599 int bbbbbbbbbbbbbbbbbbbbb) { 600 } 601 602 * ``BTDS_Yes`` (in configuration: ``Yes``) 603 Always break after template declaration. 604 605 .. code-block:: c++ 606 607 template <typename T> 608 T foo() { 609 } 610 template <typename T> 611 T foo(int aaaaaaaaaaaaaaaaaaaaa, 612 int bbbbbbbbbbbbbbbbbbbbb) { 613 } 614 615 616 617**BinPackArguments** (``bool``) 618 If ``false``, a function call's arguments will either be all on the 619 same line or will have one line each. 620 621 .. code-block:: c++ 622 623 true: 624 void f() { 625 f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, 626 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 627 } 628 629 false: 630 void f() { 631 f(aaaaaaaaaaaaaaaaaaaa, 632 aaaaaaaaaaaaaaaaaaaa, 633 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 634 } 635 636**BinPackParameters** (``bool``) 637 If ``false``, a function declaration's or function definition's 638 parameters will either all be on the same line or will have one line each. 639 640 .. code-block:: c++ 641 642 true: 643 void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa, 644 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 645 646 false: 647 void f(int aaaaaaaaaaaaaaaaaaaa, 648 int aaaaaaaaaaaaaaaaaaaa, 649 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 650 651**BraceWrapping** (``BraceWrappingFlags``) 652 Control of individual brace wrapping cases. 653 654 If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how 655 each individual brace case should be handled. Otherwise, this is ignored. 656 657 .. code-block:: yaml 658 659 # Example of usage: 660 BreakBeforeBraces: Custom 661 BraceWrapping: 662 AfterEnum: true 663 AfterStruct: false 664 SplitEmptyFunction: false 665 666 Nested configuration flags: 667 668 669 * ``bool AfterClass`` Wrap class definitions. 670 671 .. code-block:: c++ 672 673 true: 674 class foo {}; 675 676 false: 677 class foo 678 {}; 679 680 * ``bool AfterControlStatement`` Wrap control statements (``if``/``for``/``while``/``switch``/..). 681 682 .. code-block:: c++ 683 684 true: 685 if (foo()) 686 { 687 } else 688 {} 689 for (int i = 0; i < 10; ++i) 690 {} 691 692 false: 693 if (foo()) { 694 } else { 695 } 696 for (int i = 0; i < 10; ++i) { 697 } 698 699 * ``bool AfterEnum`` Wrap enum definitions. 700 701 .. code-block:: c++ 702 703 true: 704 enum X : int 705 { 706 B 707 }; 708 709 false: 710 enum X : int { B }; 711 712 * ``bool AfterFunction`` Wrap function definitions. 713 714 .. code-block:: c++ 715 716 true: 717 void foo() 718 { 719 bar(); 720 bar2(); 721 } 722 723 false: 724 void foo() { 725 bar(); 726 bar2(); 727 } 728 729 * ``bool AfterNamespace`` Wrap namespace definitions. 730 731 .. code-block:: c++ 732 733 true: 734 namespace 735 { 736 int foo(); 737 int bar(); 738 } 739 740 false: 741 namespace { 742 int foo(); 743 int bar(); 744 } 745 746 * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (interfaces, implementations...). 747 @autoreleasepool and @synchronized blocks are wrapped 748 according to `AfterControlStatement` flag. 749 750 * ``bool AfterStruct`` Wrap struct definitions. 751 752 .. code-block:: c++ 753 754 true: 755 struct foo 756 { 757 int x; 758 }; 759 760 false: 761 struct foo { 762 int x; 763 }; 764 765 * ``bool AfterUnion`` Wrap union definitions. 766 767 .. code-block:: c++ 768 769 true: 770 union foo 771 { 772 int x; 773 } 774 775 false: 776 union foo { 777 int x; 778 } 779 780 * ``bool AfterExternBlock`` Wrap extern blocks. 781 782 .. code-block:: c++ 783 784 true: 785 extern "C" 786 { 787 int foo(); 788 } 789 790 false: 791 extern "C" { 792 int foo(); 793 } 794 795 * ``bool BeforeCatch`` Wrap before ``catch``. 796 797 .. code-block:: c++ 798 799 true: 800 try { 801 foo(); 802 } 803 catch () { 804 } 805 806 false: 807 try { 808 foo(); 809 } catch () { 810 } 811 812 * ``bool BeforeElse`` Wrap before ``else``. 813 814 .. code-block:: c++ 815 816 true: 817 if (foo()) { 818 } 819 else { 820 } 821 822 false: 823 if (foo()) { 824 } else { 825 } 826 827 * ``bool IndentBraces`` Indent the wrapped braces themselves. 828 829 * ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line. 830 This option is used only if the opening brace of the function has 831 already been wrapped, i.e. the `AfterFunction` brace wrapping mode is 832 set, and the function could/should not be put on a single line (as per 833 `AllowShortFunctionsOnASingleLine` and constructor formatting options). 834 835 .. code-block:: c++ 836 837 int f() vs. inf f() 838 {} { 839 } 840 841 * ``bool SplitEmptyRecord`` If ``false``, empty record (e.g. class, struct or union) body 842 can be put on a single line. This option is used only if the opening 843 brace of the record has already been wrapped, i.e. the `AfterClass` 844 (for classes) brace wrapping mode is set. 845 846 .. code-block:: c++ 847 848 class Foo vs. class Foo 849 {} { 850 } 851 852 * ``bool SplitEmptyNamespace`` If ``false``, empty namespace body can be put on a single line. 853 This option is used only if the opening brace of the namespace has 854 already been wrapped, i.e. the `AfterNamespace` brace wrapping mode is 855 set. 856 857 .. code-block:: c++ 858 859 namespace Foo vs. namespace Foo 860 {} { 861 } 862 863 864**BreakAfterJavaFieldAnnotations** (``bool``) 865 Break after each annotation on a field in Java files. 866 867 .. code-block:: java 868 869 true: false: 870 @Partial vs. @Partial @Mock DataLoad loader; 871 @Mock 872 DataLoad loader; 873 874**BreakBeforeBinaryOperators** (``BinaryOperatorStyle``) 875 The way to wrap binary operators. 876 877 Possible values: 878 879 * ``BOS_None`` (in configuration: ``None``) 880 Break after operators. 881 882 .. code-block:: c++ 883 884 LooooooooooongType loooooooooooooooooooooongVariable = 885 someLooooooooooooooooongFunction(); 886 887 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + 888 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == 889 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && 890 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > 891 ccccccccccccccccccccccccccccccccccccccccc; 892 893 * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``) 894 Break before operators that aren't assignments. 895 896 .. code-block:: c++ 897 898 LooooooooooongType loooooooooooooooooooooongVariable = 899 someLooooooooooooooooongFunction(); 900 901 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 902 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 903 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 904 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 905 > ccccccccccccccccccccccccccccccccccccccccc; 906 907 * ``BOS_All`` (in configuration: ``All``) 908 Break before operators. 909 910 .. code-block:: c++ 911 912 LooooooooooongType loooooooooooooooooooooongVariable 913 = someLooooooooooooooooongFunction(); 914 915 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 916 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 917 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 918 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 919 > ccccccccccccccccccccccccccccccccccccccccc; 920 921 922 923**BreakBeforeBraces** (``BraceBreakingStyle``) 924 The brace breaking style to use. 925 926 Possible values: 927 928 * ``BS_Attach`` (in configuration: ``Attach``) 929 Always attach braces to surrounding context. 930 931 .. code-block:: c++ 932 933 try { 934 foo(); 935 } catch () { 936 } 937 void foo() { bar(); } 938 class foo {}; 939 if (foo()) { 940 } else { 941 } 942 enum X : int { A, B }; 943 944 * ``BS_Linux`` (in configuration: ``Linux``) 945 Like ``Attach``, but break before braces on function, namespace and 946 class definitions. 947 948 .. code-block:: c++ 949 950 try { 951 foo(); 952 } catch () { 953 } 954 void foo() { bar(); } 955 class foo 956 { 957 }; 958 if (foo()) { 959 } else { 960 } 961 enum X : int { A, B }; 962 963 * ``BS_Mozilla`` (in configuration: ``Mozilla``) 964 Like ``Attach``, but break before braces on enum, function, and record 965 definitions. 966 967 .. code-block:: c++ 968 969 try { 970 foo(); 971 } catch () { 972 } 973 void foo() { bar(); } 974 class foo 975 { 976 }; 977 if (foo()) { 978 } else { 979 } 980 enum X : int { A, B }; 981 982 * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) 983 Like ``Attach``, but break before function definitions, ``catch``, and 984 ``else``. 985 986 .. code-block:: c++ 987 988 try { 989 foo(); 990 } 991 catch () { 992 } 993 void foo() { bar(); } 994 class foo { 995 }; 996 if (foo()) { 997 } 998 else { 999 } 1000 enum X : int { A, B }; 1001 1002 * ``BS_Allman`` (in configuration: ``Allman``) 1003 Always break before braces. 1004 1005 .. code-block:: c++ 1006 1007 try 1008 { 1009 foo(); 1010 } 1011 catch () 1012 { 1013 } 1014 void foo() { bar(); } 1015 class foo 1016 { 1017 }; 1018 if (foo()) 1019 { 1020 } 1021 else 1022 { 1023 } 1024 enum X : int 1025 { 1026 A, 1027 B 1028 }; 1029 1030 * ``BS_GNU`` (in configuration: ``GNU``) 1031 Always break before braces and add an extra level of indentation to 1032 braces of control statements, not to those of class, function 1033 or other definitions. 1034 1035 .. code-block:: c++ 1036 1037 try 1038 { 1039 foo(); 1040 } 1041 catch () 1042 { 1043 } 1044 void foo() { bar(); } 1045 class foo 1046 { 1047 }; 1048 if (foo()) 1049 { 1050 } 1051 else 1052 { 1053 } 1054 enum X : int 1055 { 1056 A, 1057 B 1058 }; 1059 1060 * ``BS_WebKit`` (in configuration: ``WebKit``) 1061 Like ``Attach``, but break before functions. 1062 1063 .. code-block:: c++ 1064 1065 try { 1066 foo(); 1067 } catch () { 1068 } 1069 void foo() { bar(); } 1070 class foo { 1071 }; 1072 if (foo()) { 1073 } else { 1074 } 1075 enum X : int { A, B }; 1076 1077 * ``BS_Custom`` (in configuration: ``Custom``) 1078 Configure each individual brace in `BraceWrapping`. 1079 1080 1081 1082**BreakBeforeTernaryOperators** (``bool``) 1083 If ``true``, ternary operators will be placed after line breaks. 1084 1085 .. code-block:: c++ 1086 1087 true: 1088 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription 1089 ? firstValue 1090 : SecondValueVeryVeryVeryVeryLong; 1091 1092 false: 1093 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ? 1094 firstValue : 1095 SecondValueVeryVeryVeryVeryLong; 1096 1097**BreakConstructorInitializers** (``BreakConstructorInitializersStyle``) 1098 The constructor initializers style to use. 1099 1100 Possible values: 1101 1102 * ``BCIS_BeforeColon`` (in configuration: ``BeforeColon``) 1103 Break constructor initializers before the colon and after the commas. 1104 1105 .. code-block:: c++ 1106 1107 Constructor() 1108 : initializer1(), 1109 initializer2() 1110 1111 * ``BCIS_BeforeComma`` (in configuration: ``BeforeComma``) 1112 Break constructor initializers before the colon and commas, and align 1113 the commas with the colon. 1114 1115 .. code-block:: c++ 1116 1117 Constructor() 1118 : initializer1() 1119 , initializer2() 1120 1121 * ``BCIS_AfterColon`` (in configuration: ``AfterColon``) 1122 Break constructor initializers after the colon and commas. 1123 1124 .. code-block:: c++ 1125 1126 Constructor() : 1127 initializer1(), 1128 initializer2() 1129 1130 1131 1132**BreakInheritanceList** (``BreakInheritanceListStyle``) 1133 The inheritance list style to use. 1134 1135 Possible values: 1136 1137 * ``BILS_BeforeColon`` (in configuration: ``BeforeColon``) 1138 Break inheritance list before the colon and after the commas. 1139 1140 .. code-block:: c++ 1141 1142 class Foo 1143 : Base1, 1144 Base2 1145 {}; 1146 1147 * ``BILS_BeforeComma`` (in configuration: ``BeforeComma``) 1148 Break inheritance list before the colon and commas, and align 1149 the commas with the colon. 1150 1151 .. code-block:: c++ 1152 1153 class Foo 1154 : Base1 1155 , Base2 1156 {}; 1157 1158 * ``BILS_AfterColon`` (in configuration: ``AfterColon``) 1159 Break inheritance list after the colon and commas. 1160 1161 .. code-block:: c++ 1162 1163 class Foo : 1164 Base1, 1165 Base2 1166 {}; 1167 1168 1169 1170**BreakStringLiterals** (``bool``) 1171 Allow breaking string literals when formatting. 1172 1173**ColumnLimit** (``unsigned``) 1174 The column limit. 1175 1176 A column limit of ``0`` means that there is no column limit. In this case, 1177 clang-format will respect the input's line breaking decisions within 1178 statements unless they contradict other rules. 1179 1180**CommentPragmas** (``std::string``) 1181 A regular expression that describes comments with special meaning, 1182 which should not be split into lines or otherwise changed. 1183 1184 .. code-block:: c++ 1185 1186 // CommentPragmas: '^ FOOBAR pragma:' 1187 // Will leave the following line unaffected 1188 #include <vector> // FOOBAR pragma: keep 1189 1190**CompactNamespaces** (``bool``) 1191 If ``true``, consecutive namespace declarations will be on the same 1192 line. If ``false``, each namespace is declared on a new line. 1193 1194 .. code-block:: c++ 1195 1196 true: 1197 namespace Foo { namespace Bar { 1198 }} 1199 1200 false: 1201 namespace Foo { 1202 namespace Bar { 1203 } 1204 } 1205 1206 If it does not fit on a single line, the overflowing namespaces get 1207 wrapped: 1208 1209 .. code-block:: c++ 1210 1211 namespace Foo { namespace Bar { 1212 namespace Extra { 1213 }}} 1214 1215**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``) 1216 If the constructor initializers don't fit on a line, put each 1217 initializer on its own line. 1218 1219 .. code-block:: c++ 1220 1221 true: 1222 SomeClass::Constructor() 1223 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 1224 return 0; 1225 } 1226 1227 false: 1228 SomeClass::Constructor() 1229 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), 1230 aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 1231 return 0; 1232 } 1233 1234**ConstructorInitializerIndentWidth** (``unsigned``) 1235 The number of characters to use for indentation of constructor 1236 initializer lists as well as inheritance lists. 1237 1238**ContinuationIndentWidth** (``unsigned``) 1239 Indent width for line continuations. 1240 1241 .. code-block:: c++ 1242 1243 ContinuationIndentWidth: 2 1244 1245 int i = // VeryVeryVeryVeryVeryLongComment 1246 longFunction( // Again a long comment 1247 arg); 1248 1249**Cpp11BracedListStyle** (``bool``) 1250 If ``true``, format braced lists as best suited for C++11 braced 1251 lists. 1252 1253 Important differences: 1254 - No spaces inside the braced list. 1255 - No line break before the closing brace. 1256 - Indentation with the continuation indent, not with the block indent. 1257 1258 Fundamentally, C++11 braced lists are formatted exactly like function 1259 calls would be formatted in their place. If the braced list follows a name 1260 (e.g. a type or variable name), clang-format formats as if the ``{}`` were 1261 the parentheses of a function call with that name. If there is no name, 1262 a zero-length name is assumed. 1263 1264 .. code-block:: c++ 1265 1266 true: false: 1267 vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 }; 1268 vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} }; 1269 f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]); 1270 new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 }; 1271 1272**DerivePointerAlignment** (``bool``) 1273 If ``true``, analyze the formatted file for the most common 1274 alignment of ``&`` and ``*``. 1275 Pointer and reference alignment styles are going to be updated according 1276 to the preferences found in the file. 1277 ``PointerAlignment`` is then used only as fallback. 1278 1279**DisableFormat** (``bool``) 1280 Disables formatting completely. 1281 1282**ExperimentalAutoDetectBinPacking** (``bool``) 1283 If ``true``, clang-format detects whether function calls and 1284 definitions are formatted with one parameter per line. 1285 1286 Each call can be bin-packed, one-per-line or inconclusive. If it is 1287 inconclusive, e.g. completely on one line, but a decision needs to be 1288 made, clang-format analyzes whether there are other bin-packed cases in 1289 the input file and act accordingly. 1290 1291 NOTE: This is an experimental flag, that might go away or be renamed. Do 1292 not use this in config files, etc. Use at your own risk. 1293 1294**FixNamespaceComments** (``bool``) 1295 If ``true``, clang-format adds missing namespace end comments and 1296 fixes invalid existing ones. 1297 1298 .. code-block:: c++ 1299 1300 true: false: 1301 namespace a { vs. namespace a { 1302 foo(); foo(); 1303 } // namespace a; } 1304 1305**ForEachMacros** (``std::vector<std::string>``) 1306 A vector of macros that should be interpreted as foreach loops 1307 instead of as function calls. 1308 1309 These are expected to be macros of the form: 1310 1311 .. code-block:: c++ 1312 1313 FOREACH(<variable-declaration>, ...) 1314 <loop-body> 1315 1316 In the .clang-format configuration file, this can be configured like: 1317 1318 .. code-block:: yaml 1319 1320 ForEachMacros: ['RANGES_FOR', 'FOREACH'] 1321 1322 For example: BOOST_FOREACH. 1323 1324**IncludeBlocks** (``IncludeBlocksStyle``) 1325 Dependent on the value, multiple ``#include`` blocks can be sorted 1326 as one and divided based on category. 1327 1328 Possible values: 1329 1330 * ``IBS_Preserve`` (in configuration: ``Preserve``) 1331 Sort each ``#include`` block separately. 1332 1333 .. code-block:: c++ 1334 1335 #include "b.h" into #include "b.h" 1336 1337 #include <lib/main.h> #include "a.h" 1338 #include "a.h" #include <lib/main.h> 1339 1340 * ``IBS_Merge`` (in configuration: ``Merge``) 1341 Merge multiple ``#include`` blocks together and sort as one. 1342 1343 .. code-block:: c++ 1344 1345 #include "b.h" into #include "a.h" 1346 #include "b.h" 1347 #include <lib/main.h> #include <lib/main.h> 1348 #include "a.h" 1349 1350 * ``IBS_Regroup`` (in configuration: ``Regroup``) 1351 Merge multiple ``#include`` blocks together and sort as one. 1352 Then split into groups based on category priority. See 1353 ``IncludeCategories``. 1354 1355 .. code-block:: c++ 1356 1357 #include "b.h" into #include "a.h" 1358 #include "b.h" 1359 #include <lib/main.h> 1360 #include "a.h" #include <lib/main.h> 1361 1362 1363 1364**IncludeCategories** (``std::vector<IncludeCategory>``) 1365 Regular expressions denoting the different ``#include`` categories 1366 used for ordering ``#includes``. 1367 1368 `POSIX extended 1369 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>`_ 1370 regular expressions are supported. 1371 1372 These regular expressions are matched against the filename of an include 1373 (including the <> or "") in order. The value belonging to the first 1374 matching regular expression is assigned and ``#includes`` are sorted first 1375 according to increasing category number and then alphabetically within 1376 each category. 1377 1378 If none of the regular expressions match, INT_MAX is assigned as 1379 category. The main header for a source file automatically gets category 0. 1380 so that it is generally kept at the beginning of the ``#includes`` 1381 (https://llvm.org/docs/CodingStandards.html#include-style). However, you 1382 can also assign negative priorities if you have certain headers that 1383 always need to be first. 1384 1385 To configure this in the .clang-format file, use: 1386 1387 .. code-block:: yaml 1388 1389 IncludeCategories: 1390 - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 1391 Priority: 2 1392 - Regex: '^(<|"(gtest|gmock|isl|json)/)' 1393 Priority: 3 1394 - Regex: '<[[:alnum:].]+>' 1395 Priority: 4 1396 - Regex: '.*' 1397 Priority: 1 1398 1399**IncludeIsMainRegex** (``std::string``) 1400 Specify a regular expression of suffixes that are allowed in the 1401 file-to-main-include mapping. 1402 1403 When guessing whether a #include is the "main" include (to assign 1404 category 0, see above), use this regex of allowed suffixes to the header 1405 stem. A partial match is done, so that: 1406 - "" means "arbitrary suffix" 1407 - "$" means "no suffix" 1408 1409 For example, if configured to "(_test)?$", then a header a.h would be seen 1410 as the "main" include in both a.cc and a_test.cc. 1411 1412**IndentCaseLabels** (``bool``) 1413 Indent case labels one level from the switch statement. 1414 1415 When ``false``, use the same indentation level as for the switch statement. 1416 Switch statement body is always indented one level more than case labels. 1417 1418 .. code-block:: c++ 1419 1420 false: true: 1421 switch (fool) { vs. switch (fool) { 1422 case 1: case 1: 1423 bar(); bar(); 1424 break; break; 1425 default: default: 1426 plop(); plop(); 1427 } } 1428 1429**IndentPPDirectives** (``PPDirectiveIndentStyle``) 1430 The preprocessor directive indenting style to use. 1431 1432 Possible values: 1433 1434 * ``PPDIS_None`` (in configuration: ``None``) 1435 Does not indent any directives. 1436 1437 .. code-block:: c++ 1438 1439 #if FOO 1440 #if BAR 1441 #include <foo> 1442 #endif 1443 #endif 1444 1445 * ``PPDIS_AfterHash`` (in configuration: ``AfterHash``) 1446 Indents directives after the hash. 1447 1448 .. code-block:: c++ 1449 1450 #if FOO 1451 # if BAR 1452 # include <foo> 1453 # endif 1454 #endif 1455 1456 * ``PPDIS_BeforeHash`` (in configuration: ``BeforeHash``) 1457 Indents directives before the hash. 1458 1459 .. code-block:: c++ 1460 1461 #if FOO 1462 #if BAR 1463 #include <foo> 1464 #endif 1465 #endif 1466 1467 1468**IndentWidth** (``unsigned``) 1469 The number of columns to use for indentation. 1470 1471 .. code-block:: c++ 1472 1473 IndentWidth: 3 1474 1475 void f() { 1476 someFunction(); 1477 if (true, false) { 1478 f(); 1479 } 1480 } 1481 1482**IndentWrappedFunctionNames** (``bool``) 1483 Indent if a function definition or declaration is wrapped after the 1484 type. 1485 1486 .. code-block:: c++ 1487 1488 true: 1489 LoooooooooooooooooooooooooooooooooooooooongReturnType 1490 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1491 1492 false: 1493 LoooooooooooooooooooooooooooooooooooooooongReturnType 1494 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1495 1496**JavaImportGroups** (``std::vector<std::string>``) 1497 A vector of prefixes ordered by the desired groups for Java imports. 1498 1499 Each group is separated by a newline. Static imports will also follow the 1500 same grouping convention above all non-static imports. One group's prefix 1501 can be a subset of another - the longest prefix is always matched. Within 1502 a group, the imports are ordered lexicographically. 1503 1504 In the .clang-format configuration file, this can be configured like 1505 in the following yaml example. This will result in imports being 1506 formatted as in the Java example below. 1507 1508 .. code-block:: yaml 1509 1510 JavaImportGroups: ['com.example', 'com', 'org'] 1511 1512 1513 .. code-block:: java 1514 1515 import static com.example.function1; 1516 1517 import static com.test.function2; 1518 1519 import static org.example.function3; 1520 1521 import com.example.ClassA; 1522 import com.example.Test; 1523 import com.example.a.ClassB; 1524 1525 import com.test.ClassC; 1526 1527 import org.example.ClassD; 1528 1529**JavaScriptQuotes** (``JavaScriptQuoteStyle``) 1530 The JavaScriptQuoteStyle to use for JavaScript strings. 1531 1532 Possible values: 1533 1534 * ``JSQS_Leave`` (in configuration: ``Leave``) 1535 Leave string quotes as they are. 1536 1537 .. code-block:: js 1538 1539 string1 = "foo"; 1540 string2 = 'bar'; 1541 1542 * ``JSQS_Single`` (in configuration: ``Single``) 1543 Always use single quotes. 1544 1545 .. code-block:: js 1546 1547 string1 = 'foo'; 1548 string2 = 'bar'; 1549 1550 * ``JSQS_Double`` (in configuration: ``Double``) 1551 Always use double quotes. 1552 1553 .. code-block:: js 1554 1555 string1 = "foo"; 1556 string2 = "bar"; 1557 1558 1559 1560**JavaScriptWrapImports** (``bool``) 1561 Whether to wrap JavaScript import/export statements. 1562 1563 .. code-block:: js 1564 1565 true: 1566 import { 1567 VeryLongImportsAreAnnoying, 1568 VeryLongImportsAreAnnoying, 1569 VeryLongImportsAreAnnoying, 1570 } from 'some/module.js' 1571 1572 false: 1573 import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,} from "some/module.js" 1574 1575**KeepEmptyLinesAtTheStartOfBlocks** (``bool``) 1576 If true, the empty line at the start of blocks is kept. 1577 1578 .. code-block:: c++ 1579 1580 true: false: 1581 if (foo) { vs. if (foo) { 1582 bar(); 1583 bar(); } 1584 } 1585 1586**Language** (``LanguageKind``) 1587 Language, this format style is targeted at. 1588 1589 Possible values: 1590 1591 * ``LK_None`` (in configuration: ``None``) 1592 Do not use. 1593 1594 * ``LK_Cpp`` (in configuration: ``Cpp``) 1595 Should be used for C, C++. 1596 1597 * ``LK_CSharp`` (in configuration: ``CSharp``) 1598 Should be used for C#. 1599 1600 * ``LK_Java`` (in configuration: ``Java``) 1601 Should be used for Java. 1602 1603 * ``LK_JavaScript`` (in configuration: ``JavaScript``) 1604 Should be used for JavaScript. 1605 1606 * ``LK_ObjC`` (in configuration: ``ObjC``) 1607 Should be used for Objective-C, Objective-C++. 1608 1609 * ``LK_Proto`` (in configuration: ``Proto``) 1610 Should be used for Protocol Buffers 1611 (https://developers.google.com/protocol-buffers/). 1612 1613 * ``LK_TableGen`` (in configuration: ``TableGen``) 1614 Should be used for TableGen code. 1615 1616 * ``LK_TextProto`` (in configuration: ``TextProto``) 1617 Should be used for Protocol Buffer messages in text format 1618 (https://developers.google.com/protocol-buffers/). 1619 1620 1621 1622**MacroBlockBegin** (``std::string``) 1623 A regular expression matching macros that start a block. 1624 1625 .. code-block:: c++ 1626 1627 # With: 1628 MacroBlockBegin: "^NS_MAP_BEGIN|\ 1629 NS_TABLE_HEAD$" 1630 MacroBlockEnd: "^\ 1631 NS_MAP_END|\ 1632 NS_TABLE_.*_END$" 1633 1634 NS_MAP_BEGIN 1635 foo(); 1636 NS_MAP_END 1637 1638 NS_TABLE_HEAD 1639 bar(); 1640 NS_TABLE_FOO_END 1641 1642 # Without: 1643 NS_MAP_BEGIN 1644 foo(); 1645 NS_MAP_END 1646 1647 NS_TABLE_HEAD 1648 bar(); 1649 NS_TABLE_FOO_END 1650 1651**MacroBlockEnd** (``std::string``) 1652 A regular expression matching macros that end a block. 1653 1654**MaxEmptyLinesToKeep** (``unsigned``) 1655 The maximum number of consecutive empty lines to keep. 1656 1657 .. code-block:: c++ 1658 1659 MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0 1660 int f() { int f() { 1661 int = 1; int i = 1; 1662 i = foo(); 1663 i = foo(); return i; 1664 } 1665 return i; 1666 } 1667 1668**NamespaceIndentation** (``NamespaceIndentationKind``) 1669 The indentation used for namespaces. 1670 1671 Possible values: 1672 1673 * ``NI_None`` (in configuration: ``None``) 1674 Don't indent in namespaces. 1675 1676 .. code-block:: c++ 1677 1678 namespace out { 1679 int i; 1680 namespace in { 1681 int i; 1682 } 1683 } 1684 1685 * ``NI_Inner`` (in configuration: ``Inner``) 1686 Indent only in inner namespaces (nested in other namespaces). 1687 1688 .. code-block:: c++ 1689 1690 namespace out { 1691 int i; 1692 namespace in { 1693 int i; 1694 } 1695 } 1696 1697 * ``NI_All`` (in configuration: ``All``) 1698 Indent in all namespaces. 1699 1700 .. code-block:: c++ 1701 1702 namespace out { 1703 int i; 1704 namespace in { 1705 int i; 1706 } 1707 } 1708 1709 1710 1711**ObjCBinPackProtocolList** (``BinPackStyle``) 1712 Controls bin-packing Objective-C protocol conformance list 1713 items into as few lines as possible when they go over ``ColumnLimit``. 1714 1715 If ``Auto`` (the default), delegates to the value in 1716 ``BinPackParameters``. If that is ``true``, bin-packs Objective-C 1717 protocol conformance list items into as few lines as possible 1718 whenever they go over ``ColumnLimit``. 1719 1720 If ``Always``, always bin-packs Objective-C protocol conformance 1721 list items into as few lines as possible whenever they go over 1722 ``ColumnLimit``. 1723 1724 If ``Never``, lays out Objective-C protocol conformance list items 1725 onto individual lines whenever they go over ``ColumnLimit``. 1726 1727 1728 .. code-block:: objc 1729 1730 Always (or Auto, if BinPackParameters=true): 1731 @interface ccccccccccccc () < 1732 ccccccccccccc, ccccccccccccc, 1733 ccccccccccccc, ccccccccccccc> { 1734 } 1735 1736 Never (or Auto, if BinPackParameters=false): 1737 @interface ddddddddddddd () < 1738 ddddddddddddd, 1739 ddddddddddddd, 1740 ddddddddddddd, 1741 ddddddddddddd> { 1742 } 1743 1744 Possible values: 1745 1746 * ``BPS_Auto`` (in configuration: ``Auto``) 1747 Automatically determine parameter bin-packing behavior. 1748 1749 * ``BPS_Always`` (in configuration: ``Always``) 1750 Always bin-pack parameters. 1751 1752 * ``BPS_Never`` (in configuration: ``Never``) 1753 Never bin-pack parameters. 1754 1755 1756 1757**ObjCBlockIndentWidth** (``unsigned``) 1758 The number of characters to use for indentation of ObjC blocks. 1759 1760 .. code-block:: objc 1761 1762 ObjCBlockIndentWidth: 4 1763 1764 [operation setCompletionBlock:^{ 1765 [self onOperationDone]; 1766 }]; 1767 1768**ObjCSpaceAfterProperty** (``bool``) 1769 Add a space after ``@property`` in Objective-C, i.e. use 1770 ``@property (readonly)`` instead of ``@property(readonly)``. 1771 1772**ObjCSpaceBeforeProtocolList** (``bool``) 1773 Add a space in front of an Objective-C protocol list, i.e. use 1774 ``Foo <Protocol>`` instead of ``Foo<Protocol>``. 1775 1776**PenaltyBreakAssignment** (``unsigned``) 1777 The penalty for breaking around an assignment operator. 1778 1779**PenaltyBreakBeforeFirstCallParameter** (``unsigned``) 1780 The penalty for breaking a function call after ``call(``. 1781 1782**PenaltyBreakComment** (``unsigned``) 1783 The penalty for each line break introduced inside a comment. 1784 1785**PenaltyBreakFirstLessLess** (``unsigned``) 1786 The penalty for breaking before the first ``<<``. 1787 1788**PenaltyBreakString** (``unsigned``) 1789 The penalty for each line break introduced inside a string literal. 1790 1791**PenaltyBreakTemplateDeclaration** (``unsigned``) 1792 The penalty for breaking after template declaration. 1793 1794**PenaltyExcessCharacter** (``unsigned``) 1795 The penalty for each character outside of the column limit. 1796 1797**PenaltyReturnTypeOnItsOwnLine** (``unsigned``) 1798 Penalty for putting the return type of a function onto its own 1799 line. 1800 1801**PointerAlignment** (``PointerAlignmentStyle``) 1802 Pointer and reference alignment style. 1803 1804 Possible values: 1805 1806 * ``PAS_Left`` (in configuration: ``Left``) 1807 Align pointer to the left. 1808 1809 .. code-block:: c++ 1810 1811 int* a; 1812 1813 * ``PAS_Right`` (in configuration: ``Right``) 1814 Align pointer to the right. 1815 1816 .. code-block:: c++ 1817 1818 int *a; 1819 1820 * ``PAS_Middle`` (in configuration: ``Middle``) 1821 Align pointer in the middle. 1822 1823 .. code-block:: c++ 1824 1825 int * a; 1826 1827 1828 1829**RawStringFormats** (``std::vector<RawStringFormat>``) 1830 Defines hints for detecting supported languages code blocks in raw 1831 strings. 1832 1833 A raw string with a matching delimiter or a matching enclosing function 1834 name will be reformatted assuming the specified language based on the 1835 style for that language defined in the .clang-format file. If no style has 1836 been defined in the .clang-format file for the specific language, a 1837 predefined style given by 'BasedOnStyle' is used. If 'BasedOnStyle' is not 1838 found, the formatting is based on llvm style. A matching delimiter takes 1839 precedence over a matching enclosing function name for determining the 1840 language of the raw string contents. 1841 1842 If a canonical delimiter is specified, occurrences of other delimiters for 1843 the same language will be updated to the canonical if possible. 1844 1845 There should be at most one specification per language and each delimiter 1846 and enclosing function should not occur in multiple specifications. 1847 1848 To configure this in the .clang-format file, use: 1849 1850 .. code-block:: yaml 1851 1852 RawStringFormats: 1853 - Language: TextProto 1854 Delimiters: 1855 - 'pb' 1856 - 'proto' 1857 EnclosingFunctions: 1858 - 'PARSE_TEXT_PROTO' 1859 BasedOnStyle: google 1860 - Language: Cpp 1861 Delimiters: 1862 - 'cc' 1863 - 'cpp' 1864 BasedOnStyle: llvm 1865 CanonicalDelimiter: 'cc' 1866 1867**ReflowComments** (``bool``) 1868 If ``true``, clang-format will attempt to re-flow comments. 1869 1870 .. code-block:: c++ 1871 1872 false: 1873 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information 1874 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */ 1875 1876 true: 1877 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 1878 // information 1879 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 1880 * information */ 1881 1882**SortIncludes** (``bool``) 1883 If ``true``, clang-format will sort ``#includes``. 1884 1885 .. code-block:: c++ 1886 1887 false: true: 1888 #include "b.h" vs. #include "a.h" 1889 #include "a.h" #include "b.h" 1890 1891**SortUsingDeclarations** (``bool``) 1892 If ``true``, clang-format will sort using declarations. 1893 1894 The order of using declarations is defined as follows: 1895 Split the strings by "::" and discard any initial empty strings. The last 1896 element of each list is a non-namespace name; all others are namespace 1897 names. Sort the lists of names lexicographically, where the sort order of 1898 individual names is that all non-namespace names come before all namespace 1899 names, and within those groups, names are in case-insensitive 1900 lexicographic order. 1901 1902 .. code-block:: c++ 1903 1904 false: true: 1905 using std::cout; vs. using std::cin; 1906 using std::cin; using std::cout; 1907 1908**SpaceAfterCStyleCast** (``bool``) 1909 If ``true``, a space is inserted after C style casts. 1910 1911 .. code-block:: c++ 1912 1913 true: false: 1914 (int) i; vs. (int)i; 1915 1916**SpaceAfterTemplateKeyword** (``bool``) 1917 If ``true``, a space will be inserted after the 'template' keyword. 1918 1919 .. code-block:: c++ 1920 1921 true: false: 1922 template <int> void foo(); vs. template<int> void foo(); 1923 1924**SpaceBeforeAssignmentOperators** (``bool``) 1925 If ``false``, spaces will be removed before assignment operators. 1926 1927 .. code-block:: c++ 1928 1929 true: false: 1930 int a = 5; vs. int a=5; 1931 a += 42 a+=42; 1932 1933**SpaceBeforeCpp11BracedList** (``bool``) 1934 If ``true``, a space will be inserted before a C++11 braced list 1935 used to initialize an object (after the preceding identifier or type). 1936 1937 .. code-block:: c++ 1938 1939 true: false: 1940 Foo foo { bar }; vs. Foo foo{ bar }; 1941 Foo {}; Foo{}; 1942 vector<int> { 1, 2, 3 }; vector<int>{ 1, 2, 3 }; 1943 new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 }; 1944 1945**SpaceBeforeCtorInitializerColon** (``bool``) 1946 If ``false``, spaces will be removed before constructor initializer 1947 colon. 1948 1949 .. code-block:: c++ 1950 1951 true: false: 1952 Foo::Foo() : a(a) {} Foo::Foo(): a(a) {} 1953 1954**SpaceBeforeInheritanceColon** (``bool``) 1955 If ``false``, spaces will be removed before inheritance colon. 1956 1957 .. code-block:: c++ 1958 1959 true: false: 1960 class Foo : Bar {} vs. class Foo: Bar {} 1961 1962**SpaceBeforeParens** (``SpaceBeforeParensOptions``) 1963 Defines in which cases to put a space before opening parentheses. 1964 1965 Possible values: 1966 1967 * ``SBPO_Never`` (in configuration: ``Never``) 1968 Never put a space before opening parentheses. 1969 1970 .. code-block:: c++ 1971 1972 void f() { 1973 if(true) { 1974 f(); 1975 } 1976 } 1977 1978 * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) 1979 Put a space before opening parentheses only after control statement 1980 keywords (``for/if/while...``). 1981 1982 .. code-block:: c++ 1983 1984 void f() { 1985 if (true) { 1986 f(); 1987 } 1988 } 1989 1990 * ``SBPO_Always`` (in configuration: ``Always``) 1991 Always put a space before opening parentheses, except when it's 1992 prohibited by the syntax rules (in function-like macro definitions) or 1993 when determined by other style rules (after unary operators, opening 1994 parentheses, etc.) 1995 1996 .. code-block:: c++ 1997 1998 void f () { 1999 if (true) { 2000 f (); 2001 } 2002 } 2003 2004 2005 2006**SpaceBeforeRangeBasedForLoopColon** (``bool``) 2007 If ``false``, spaces will be removed before range-based for loop 2008 colon. 2009 2010 .. code-block:: c++ 2011 2012 true: false: 2013 for (auto v : values) {} vs. for(auto v: values) {} 2014 2015**SpaceInEmptyParentheses** (``bool``) 2016 If ``true``, spaces may be inserted into ``()``. 2017 2018 .. code-block:: c++ 2019 2020 true: false: 2021 void f( ) { vs. void f() { 2022 int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; 2023 if (true) { if (true) { 2024 f( ); f(); 2025 } } 2026 } } 2027 2028**SpacesBeforeTrailingComments** (``unsigned``) 2029 The number of spaces before trailing line comments 2030 (``//`` - comments). 2031 2032 This does not affect trailing block comments (``/*`` - comments) as 2033 those commonly have different usage patterns and a number of special 2034 cases. 2035 2036 .. code-block:: c++ 2037 2038 SpacesBeforeTrailingComments: 3 2039 void f() { 2040 if (true) { // foo1 2041 f(); // bar 2042 } // foo 2043 } 2044 2045**SpacesInAngles** (``bool``) 2046 If ``true``, spaces will be inserted after ``<`` and before ``>`` 2047 in template argument lists. 2048 2049 .. code-block:: c++ 2050 2051 true: false: 2052 static_cast< int >(arg); vs. static_cast<int>(arg); 2053 std::function< void(int) > fct; std::function<void(int)> fct; 2054 2055**SpacesInCStyleCastParentheses** (``bool``) 2056 If ``true``, spaces may be inserted into C style casts. 2057 2058 .. code-block:: c++ 2059 2060 true: false: 2061 x = ( int32 )y vs. x = (int32)y 2062 2063**SpacesInContainerLiterals** (``bool``) 2064 If ``true``, spaces are inserted inside container literals (e.g. 2065 ObjC and Javascript array and dict literals). 2066 2067 .. code-block:: js 2068 2069 true: false: 2070 var arr = [ 1, 2, 3 ]; vs. var arr = [1, 2, 3]; 2071 f({a : 1, b : 2, c : 3}); f({a: 1, b: 2, c: 3}); 2072 2073**SpacesInParentheses** (``bool``) 2074 If ``true``, spaces will be inserted after ``(`` and before ``)``. 2075 2076 .. code-block:: c++ 2077 2078 true: false: 2079 t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; 2080 2081**SpacesInSquareBrackets** (``bool``) 2082 If ``true``, spaces will be inserted after ``[`` and before ``]``. 2083 Lambdas or unspecified size array declarations will not be affected. 2084 2085 .. code-block:: c++ 2086 2087 true: false: 2088 int a[ 5 ]; vs. int a[5]; 2089 std::unique_ptr<int[]> foo() {} // Won't be affected 2090 2091**Standard** (``LanguageStandard``) 2092 Format compatible with this standard, e.g. use ``A<A<int> >`` 2093 instead of ``A<A<int>>`` for ``LS_Cpp03``. 2094 2095 Possible values: 2096 2097 * ``LS_Cpp03`` (in configuration: ``Cpp03``) 2098 Use C++03-compatible syntax. 2099 2100 * ``LS_Cpp11`` (in configuration: ``Cpp11``) 2101 Use features of C++11, C++14 and C++1z (e.g. ``A<A<int>>`` instead of 2102 ``A<A<int> >``). 2103 2104 * ``LS_Auto`` (in configuration: ``Auto``) 2105 Automatic detection based on the input. 2106 2107 2108 2109**StatementMacros** (``std::vector<std::string>``) 2110 A vector of macros that should be interpreted as complete 2111 statements. 2112 2113 Typical macros are expressions, and require a semi-colon to be 2114 added; sometimes this is not the case, and this allows to make 2115 clang-format aware of such cases. 2116 2117 For example: Q_UNUSED 2118 2119**TabWidth** (``unsigned``) 2120 The number of columns used for tab stops. 2121 2122**UseTab** (``UseTabStyle``) 2123 The way to use tab characters in the resulting file. 2124 2125 Possible values: 2126 2127 * ``UT_Never`` (in configuration: ``Never``) 2128 Never use tab. 2129 2130 * ``UT_ForIndentation`` (in configuration: ``ForIndentation``) 2131 Use tabs only for indentation. 2132 2133 * ``UT_ForContinuationAndIndentation`` (in configuration: ``ForContinuationAndIndentation``) 2134 Use tabs only for line continuation and indentation. 2135 2136 * ``UT_Always`` (in configuration: ``Always``) 2137 Use tabs whenever we need to fill whitespace that spans at least from 2138 one tab stop to the next one. 2139 2140 2141 2142.. END_FORMAT_STYLE_OPTIONS 2143 2144Adding additional style options 2145=============================== 2146 2147Each additional style option adds costs to the clang-format project. Some of 2148these costs affect the clang-format development itself, as we need to make 2149sure that any given combination of options work and that new features don't 2150break any of the existing options in any way. There are also costs for end users 2151as options become less discoverable and people have to think about and make a 2152decision on options they don't really care about. 2153 2154The goal of the clang-format project is more on the side of supporting a 2155limited set of styles really well as opposed to supporting every single style 2156used by a codebase somewhere in the wild. Of course, we do want to support all 2157major projects and thus have established the following bar for adding style 2158options. Each new style option must .. 2159 2160 * be used in a project of significant size (have dozens of contributors) 2161 * have a publicly accessible style guide 2162 * have a person willing to contribute and maintain patches 2163 2164Examples 2165======== 2166 2167A style similar to the `Linux Kernel style 2168<https://www.kernel.org/doc/Documentation/CodingStyle>`_: 2169 2170.. code-block:: yaml 2171 2172 BasedOnStyle: LLVM 2173 IndentWidth: 8 2174 UseTab: Always 2175 BreakBeforeBraces: Linux 2176 AllowShortIfStatementsOnASingleLine: false 2177 IndentCaseLabels: false 2178 2179The result is (imagine that tabs are used for indentation here): 2180 2181.. code-block:: c++ 2182 2183 void test() 2184 { 2185 switch (x) { 2186 case 0: 2187 case 1: 2188 do_something(); 2189 break; 2190 case 2: 2191 do_something_else(); 2192 break; 2193 default: 2194 break; 2195 } 2196 if (condition) 2197 do_something_completely_different(); 2198 2199 if (x == y) { 2200 q(); 2201 } else if (x > y) { 2202 w(); 2203 } else { 2204 r(); 2205 } 2206 } 2207 2208A style similar to the default Visual Studio formatting style: 2209 2210.. code-block:: yaml 2211 2212 UseTab: Never 2213 IndentWidth: 4 2214 BreakBeforeBraces: Allman 2215 AllowShortIfStatementsOnASingleLine: false 2216 IndentCaseLabels: false 2217 ColumnLimit: 0 2218 2219The result is: 2220 2221.. code-block:: c++ 2222 2223 void test() 2224 { 2225 switch (suffix) 2226 { 2227 case 0: 2228 case 1: 2229 do_something(); 2230 break; 2231 case 2: 2232 do_something_else(); 2233 break; 2234 default: 2235 break; 2236 } 2237 if (condition) 2238 do_somthing_completely_different(); 2239 2240 if (x == y) 2241 { 2242 q(); 2243 } 2244 else if (x > y) 2245 { 2246 w(); 2247 } 2248 else 2249 { 2250 r(); 2251 } 2252 } 2253