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