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**AlignEscapedNewlinesLeft** (``bool``) 213 If ``true``, aligns escaped newlines as far left as possible. 214 Otherwise puts them into the right-most column. 215 216 .. code-block:: c++ 217 218 true: 219 #define A \ 220 int aaaa; \ 221 int b; \ 222 int dddddddddd; 223 224 false: 225 #define A \ 226 int aaaa; \ 227 int b; \ 228 int dddddddddd; 229 230**AlignOperands** (``bool``) 231 If ``true``, horizontally align operands of binary and ternary 232 expressions. 233 234 Specifically, this aligns operands of a single expression that needs to be 235 split over multiple lines, e.g.: 236 237 .. code-block:: c++ 238 239 int aaa = bbbbbbbbbbbbbbb + 240 ccccccccccccccc; 241 242**AlignTrailingComments** (``bool``) 243 If ``true``, aligns trailing comments. 244 245 .. code-block:: c++ 246 247 true: false: 248 int a; // My comment a vs. int a; // My comment a 249 int b = 2; // comment b int b = 2; // comment about b 250 251**AllowAllParametersOfDeclarationOnNextLine** (``bool``) 252 Allow putting all parameters of a function declaration onto 253 the next line even if ``BinPackParameters`` is ``false``. 254 255 .. code-block:: c++ 256 257 true: false: 258 myFunction(foo, vs. myFunction(foo, bar, plop); 259 bar, 260 plop); 261 262**AllowShortBlocksOnASingleLine** (``bool``) 263 Allows contracting simple braced statements to a single line. 264 265 E.g., this allows ``if (a) { return; }`` to be put on a single line. 266 267**AllowShortCaseLabelsOnASingleLine** (``bool``) 268 If ``true``, short case labels will be contracted to a single line. 269 270 .. code-block:: c++ 271 272 true: false: 273 switch (a) { vs. switch (a) { 274 case 1: x = 1; break; case 1: 275 case 2: return; x = 1; 276 } break; 277 case 2: 278 return; 279 } 280 281**AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``) 282 Dependent on the value, ``int f() { return 0; }`` can be put on a 283 single line. 284 285 Possible values: 286 287 * ``SFS_None`` (in configuration: ``None``) 288 Never merge functions into a single line. 289 290 * ``SFS_Empty`` (in configuration: ``Empty``) 291 Only merge empty functions. 292 293 .. code-block:: c++ 294 295 void f() { bar(); } 296 void f2() { 297 bar2(); 298 } 299 300 * ``SFS_Inline`` (in configuration: ``Inline``) 301 Only merge functions defined inside a class. Implies "empty". 302 303 .. code-block:: c++ 304 305 class Foo { 306 void f() { foo(); } 307 }; 308 309 * ``SFS_All`` (in configuration: ``All``) 310 Merge all functions fitting on a single line. 311 312 .. code-block:: c++ 313 314 class Foo { 315 void f() { foo(); } 316 }; 317 void f() { bar(); } 318 319 320 321**AllowShortIfStatementsOnASingleLine** (``bool``) 322 If ``true``, ``if (a) return;`` can be put on a single line. 323 324**AllowShortLoopsOnASingleLine** (``bool``) 325 If ``true``, ``while (true) continue;`` can be put on a single 326 line. 327 328**AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``) 329 The function definition return type breaking style to use. This 330 option is **deprecated** and is retained for backwards compatibility. 331 332 Possible values: 333 334 * ``DRTBS_None`` (in configuration: ``None``) 335 Break after return type automatically. 336 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. 337 338 * ``DRTBS_All`` (in configuration: ``All``) 339 Always break after the return type. 340 341 * ``DRTBS_TopLevel`` (in configuration: ``TopLevel``) 342 Always break after the return types of top-level functions. 343 344 345 346**AlwaysBreakAfterReturnType** (``ReturnTypeBreakingStyle``) 347 The function declaration return type breaking style to use. 348 349 Possible values: 350 351 * ``RTBS_None`` (in configuration: ``None``) 352 Break after return type automatically. 353 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. 354 355 .. code-block:: c++ 356 357 class A { 358 int f() { return 0; }; 359 }; 360 int f(); 361 int f() { return 1; } 362 363 * ``RTBS_All`` (in configuration: ``All``) 364 Always break after the return type. 365 366 .. code-block:: c++ 367 368 class A { 369 int 370 f() { 371 return 0; 372 }; 373 }; 374 int 375 f(); 376 int 377 f() { 378 return 1; 379 } 380 381 * ``RTBS_TopLevel`` (in configuration: ``TopLevel``) 382 Always break after the return types of top-level functions. 383 384 .. code-block:: c++ 385 386 class A { 387 int f() { return 0; }; 388 }; 389 int 390 f(); 391 int 392 f() { 393 return 1; 394 } 395 396 * ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``) 397 Always break after the return type of function definitions. 398 399 .. code-block:: c++ 400 401 class A { 402 int 403 f() { 404 return 0; 405 }; 406 }; 407 int f(); 408 int 409 f() { 410 return 1; 411 } 412 413 * ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``) 414 Always break after the return type of top-level definitions. 415 416 .. code-block:: c++ 417 418 class A { 419 int f() { return 0; }; 420 }; 421 int f(); 422 int 423 f() { 424 return 1; 425 } 426 427 428 429**AlwaysBreakBeforeMultilineStrings** (``bool``) 430 If ``true``, always break before multiline string literals. 431 432 This flag is mean to make cases where there are multiple multiline strings 433 in a file look more consistent. Thus, it will only take effect if wrapping 434 the string at that point leads to it being indented 435 ``ContinuationIndentWidth`` spaces from the start of the line. 436 437 .. code-block:: c++ 438 439 true: false: 440 aaaa = vs. aaaa = "bbbb" 441 "bbbb" "cccc"; 442 "cccc"; 443 444**AlwaysBreakTemplateDeclarations** (``bool``) 445 If ``true``, always break after the ``template<...>`` of a template 446 declaration. 447 448 .. code-block:: c++ 449 450 true: false: 451 template <typename T> vs. template <typename T> class C {}; 452 class C {}; 453 454**BinPackArguments** (``bool``) 455 If ``false``, a function call's arguments will either be all on the 456 same line or will have one line each. 457 458 .. code-block:: c++ 459 460 true: 461 void f() { 462 f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, 463 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 464 } 465 466 false: 467 void f() { 468 f(aaaaaaaaaaaaaaaaaaaa, 469 aaaaaaaaaaaaaaaaaaaa, 470 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 471 } 472 473**BinPackParameters** (``bool``) 474 If ``false``, a function declaration's or function definition's 475 parameters will either all be on the same line or will have one line each. 476 477 .. code-block:: c++ 478 479 true: 480 void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa, 481 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 482 483 false: 484 void f(int aaaaaaaaaaaaaaaaaaaa, 485 int aaaaaaaaaaaaaaaaaaaa, 486 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 487 488**BraceWrapping** (``BraceWrappingFlags``) 489 Control of individual brace wrapping cases. 490 491 If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how 492 each individual brace case should be handled. Otherwise, this is ignored. 493 494 Nested configuration flags: 495 496 497 * ``bool AfterClass`` Wrap class definitions. 498 499 .. code-block:: c++ 500 501 true: 502 class foo {}; 503 504 false: 505 class foo 506 {}; 507 508 * ``bool AfterControlStatement`` Wrap control statements (``if``/``for``/``while``/``switch``/..). 509 510 .. code-block:: c++ 511 512 true: 513 if (foo()) 514 { 515 } else 516 {} 517 for (int i = 0; i < 10; ++i) 518 {} 519 520 false: 521 if (foo()) { 522 } else { 523 } 524 for (int i = 0; i < 10; ++i) { 525 } 526 527 * ``bool AfterEnum`` Wrap enum definitions. 528 529 .. code-block:: c++ 530 531 true: 532 enum X : int 533 { 534 B 535 }; 536 537 false: 538 enum X : int { B }; 539 540 * ``bool AfterFunction`` Wrap function definitions. 541 542 .. code-block:: c++ 543 544 true: 545 void foo() 546 { 547 bar(); 548 bar2(); 549 } 550 551 false: 552 void foo() { 553 bar(); 554 bar2(); 555 } 556 557 * ``bool AfterNamespace`` Wrap namespace definitions. 558 559 .. code-block:: c++ 560 561 true: 562 namespace 563 { 564 int foo(); 565 int bar(); 566 } 567 568 false: 569 namespace { 570 int foo(); 571 int bar(); 572 } 573 574 * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (``@autoreleasepool``, interfaces, ..). 575 576 * ``bool AfterStruct`` Wrap struct definitions. 577 578 .. code-block:: c++ 579 580 true: 581 struct foo 582 { 583 int x; 584 } 585 586 false: 587 struct foo { 588 int x; 589 } 590 591 * ``bool AfterUnion`` Wrap union definitions. 592 593 .. code-block:: c++ 594 595 true: 596 union foo 597 { 598 int x; 599 } 600 601 false: 602 union foo { 603 int x; 604 } 605 606 * ``bool BeforeCatch`` Wrap before ``catch``. 607 608 .. code-block:: c++ 609 610 true: 611 try { 612 foo(); 613 } 614 catch () { 615 } 616 617 false: 618 try { 619 foo(); 620 } catch () { 621 } 622 623 * ``bool BeforeElse`` Wrap before ``else``. 624 625 .. code-block:: c++ 626 627 true: 628 if (foo()) { 629 } 630 else { 631 } 632 633 false: 634 if (foo()) { 635 } else { 636 } 637 638 * ``bool IndentBraces`` Indent the wrapped braces themselves. 639 640 641**BreakAfterJavaFieldAnnotations** (``bool``) 642 Break after each annotation on a field in Java files. 643 644 .. code-block:: java 645 646 true: false: 647 @Partial vs. @Partial @Mock DataLoad loader; 648 @Mock 649 DataLoad loader; 650 651**BreakBeforeBinaryOperators** (``BinaryOperatorStyle``) 652 The way to wrap binary operators. 653 654 Possible values: 655 656 * ``BOS_None`` (in configuration: ``None``) 657 Break after operators. 658 659 .. code-block:: c++ 660 661 LooooooooooongType loooooooooooooooooooooongVariable = 662 someLooooooooooooooooongFunction(); 663 664 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + 665 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == 666 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && 667 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > 668 ccccccccccccccccccccccccccccccccccccccccc; 669 670 * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``) 671 Break before operators that aren't assignments. 672 673 .. code-block:: c++ 674 675 LooooooooooongType loooooooooooooooooooooongVariable = 676 someLooooooooooooooooongFunction(); 677 678 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 679 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 680 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 681 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 682 > ccccccccccccccccccccccccccccccccccccccccc; 683 684 * ``BOS_All`` (in configuration: ``All``) 685 Break before operators. 686 687 .. code-block:: c++ 688 689 LooooooooooongType loooooooooooooooooooooongVariable 690 = someLooooooooooooooooongFunction(); 691 692 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 693 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 694 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 695 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 696 > ccccccccccccccccccccccccccccccccccccccccc; 697 698 699 700**BreakBeforeBraces** (``BraceBreakingStyle``) 701 The brace breaking style to use. 702 703 Possible values: 704 705 * ``BS_Attach`` (in configuration: ``Attach``) 706 Always attach braces to surrounding context. 707 708 .. code-block:: c++ 709 710 try { 711 foo(); 712 } catch () { 713 } 714 void foo() { bar(); } 715 class foo {}; 716 if (foo()) { 717 } else { 718 } 719 enum X : int { A, B }; 720 721 * ``BS_Linux`` (in configuration: ``Linux``) 722 Like ``Attach``, but break before braces on function, namespace and 723 class definitions. 724 725 .. code-block:: c++ 726 727 try { 728 foo(); 729 } catch () { 730 } 731 void foo() { bar(); } 732 class foo 733 { 734 }; 735 if (foo()) { 736 } else { 737 } 738 enum X : int { A, B }; 739 740 * ``BS_Mozilla`` (in configuration: ``Mozilla``) 741 Like ``Attach``, but break before braces on enum, function, and record 742 definitions. 743 744 .. code-block:: c++ 745 746 try { 747 foo(); 748 } catch () { 749 } 750 void foo() { bar(); } 751 class foo 752 { 753 }; 754 if (foo()) { 755 } else { 756 } 757 enum X : int { A, B }; 758 759 * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) 760 Like ``Attach``, but break before function definitions, ``catch``, and 761 ``else``. 762 763 .. code-block:: c++ 764 765 try { 766 foo(); 767 } catch () { 768 } 769 void foo() { bar(); } 770 class foo 771 { 772 }; 773 if (foo()) { 774 } else { 775 } 776 enum X : int 777 { 778 A, 779 B 780 }; 781 782 * ``BS_Allman`` (in configuration: ``Allman``) 783 Always break before braces. 784 785 .. code-block:: c++ 786 787 try { 788 foo(); 789 } 790 catch () { 791 } 792 void foo() { bar(); } 793 class foo { 794 }; 795 if (foo()) { 796 } 797 else { 798 } 799 enum X : int { A, B }; 800 801 * ``BS_GNU`` (in configuration: ``GNU``) 802 Always break before braces and add an extra level of indentation to 803 braces of control statements, not to those of class, function 804 or other definitions. 805 806 .. code-block:: c++ 807 808 try 809 { 810 foo(); 811 } 812 catch () 813 { 814 } 815 void foo() { bar(); } 816 class foo 817 { 818 }; 819 if (foo()) 820 { 821 } 822 else 823 { 824 } 825 enum X : int 826 { 827 A, 828 B 829 }; 830 831 * ``BS_WebKit`` (in configuration: ``WebKit``) 832 Like ``Attach``, but break before functions. 833 834 .. code-block:: c++ 835 836 try { 837 foo(); 838 } catch () { 839 } 840 void foo() { bar(); } 841 class foo { 842 }; 843 if (foo()) { 844 } else { 845 } 846 enum X : int { A, B }; 847 848 * ``BS_Custom`` (in configuration: ``Custom``) 849 Configure each individual brace in `BraceWrapping`. 850 851 852 853**BreakBeforeInheritanceComma** (``bool``) 854 If ``true``, in the class inheritance expression clang-format will 855 break before ``:`` and ``,`` if there is multiple inheritance. 856 857 .. code-block:: c++ 858 859 true: false: 860 class MyClass vs. class MyClass : public X, public Y { 861 : public X }; 862 , public Y { 863 }; 864 865**BreakBeforeTernaryOperators** (``bool``) 866 If ``true``, ternary operators will be placed after line breaks. 867 868 .. code-block:: c++ 869 870 true: 871 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription 872 ? firstValue 873 : SecondValueVeryVeryVeryVeryLong; 874 875 true: 876 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ? 877 firstValue : 878 SecondValueVeryVeryVeryVeryLong; 879 880**BreakConstructorInitializersBeforeComma** (``bool``) 881 Always break constructor initializers before commas and align 882 the commas with the colon. 883 884 .. code-block:: c++ 885 886 true: false: 887 SomeClass::Constructor() vs. SomeClass::Constructor() : a(a), 888 : a(a) b(b), 889 , b(b) c(c) {} 890 , c(c) {} 891 892**BreakStringLiterals** (``bool``) 893 Allow breaking string literals when formatting. 894 895**ColumnLimit** (``unsigned``) 896 The column limit. 897 898 A column limit of ``0`` means that there is no column limit. In this case, 899 clang-format will respect the input's line breaking decisions within 900 statements unless they contradict other rules. 901 902**CommentPragmas** (``std::string``) 903 A regular expression that describes comments with special meaning, 904 which should not be split into lines or otherwise changed. 905 906 .. code-block:: c++ 907 908 // CommentPragmas: '^ FOOBAR pragma:' 909 // Will leave the following line unaffected 910 #include <vector> // FOOBAR pragma: keep 911 912**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``) 913 If the constructor initializers don't fit on a line, put each 914 initializer on its own line. 915 916 .. code-block:: c++ 917 918 true: 919 SomeClass::Constructor() 920 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 921 return 0; 922 } 923 924 false: 925 SomeClass::Constructor() 926 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), 927 aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 928 return 0; 929 } 930 931**ConstructorInitializerIndentWidth** (``unsigned``) 932 The number of characters to use for indentation of constructor 933 initializer lists. 934 935**ContinuationIndentWidth** (``unsigned``) 936 Indent width for line continuations. 937 938 .. code-block:: c++ 939 940 ContinuationIndentWidth: 2 941 942 int i = // VeryVeryVeryVeryVeryLongComment 943 longFunction( // Again a long comment 944 arg); 945 946**Cpp11BracedListStyle** (``bool``) 947 If ``true``, format braced lists as best suited for C++11 braced 948 lists. 949 950 Important differences: 951 - No spaces inside the braced list. 952 - No line break before the closing brace. 953 - Indentation with the continuation indent, not with the block indent. 954 955 Fundamentally, C++11 braced lists are formatted exactly like function 956 calls would be formatted in their place. If the braced list follows a name 957 (e.g. a type or variable name), clang-format formats as if the ``{}`` were 958 the parentheses of a function call with that name. If there is no name, 959 a zero-length name is assumed. 960 961 .. code-block:: c++ 962 963 true: false: 964 vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 }; 965 vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} }; 966 f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]); 967 new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 }; 968 969**DerivePointerAlignment** (``bool``) 970 If ``true``, analyze the formatted file for the most common 971 alignment of ``&`` and ``*``. 972 Pointer and reference alignment styles are going to be updated according 973 to the preferences found in the file. 974 ``PointerAlignment`` is then used only as fallback. 975 976**DisableFormat** (``bool``) 977 Disables formatting completely. 978 979**ExperimentalAutoDetectBinPacking** (``bool``) 980 If ``true``, clang-format detects whether function calls and 981 definitions are formatted with one parameter per line. 982 983 Each call can be bin-packed, one-per-line or inconclusive. If it is 984 inconclusive, e.g. completely on one line, but a decision needs to be 985 made, clang-format analyzes whether there are other bin-packed cases in 986 the input file and act accordingly. 987 988 NOTE: This is an experimental flag, that might go away or be renamed. Do 989 not use this in config files, etc. Use at your own risk. 990 991**FixNamespaceComments** (``bool``) 992 If ``true``, clang-format adds missing namespace end comments and 993 fixes invalid existing ones. 994 995 .. code-block:: c++ 996 997 true: false: 998 namespace a { vs. namespace a { 999 foo(); foo(); 1000 } // namespace a; } 1001 1002**ForEachMacros** (``std::vector<std::string>``) 1003 A vector of macros that should be interpreted as foreach loops 1004 instead of as function calls. 1005 1006 These are expected to be macros of the form: 1007 1008 .. code-block:: c++ 1009 1010 FOREACH(<variable-declaration>, ...) 1011 <loop-body> 1012 1013 In the .clang-format configuration file, this can be configured like: 1014 1015 .. code-block:: yaml 1016 1017 ForEachMacros: ['RANGES_FOR', 'FOREACH'] 1018 1019 For example: BOOST_FOREACH. 1020 1021**IncludeCategories** (``std::vector<IncludeCategory>``) 1022 Regular expressions denoting the different ``#include`` categories 1023 used for ordering ``#includes``. 1024 1025 These regular expressions are matched against the filename of an include 1026 (including the <> or "") in order. The value belonging to the first 1027 matching regular expression is assigned and ``#includes`` are sorted first 1028 according to increasing category number and then alphabetically within 1029 each category. 1030 1031 If none of the regular expressions match, INT_MAX is assigned as 1032 category. The main header for a source file automatically gets category 0. 1033 so that it is generally kept at the beginning of the ``#includes`` 1034 (http://llvm.org/docs/CodingStandards.html#include-style). However, you 1035 can also assign negative priorities if you have certain headers that 1036 always need to be first. 1037 1038 To configure this in the .clang-format file, use: 1039 1040 .. code-block:: yaml 1041 1042 IncludeCategories: 1043 - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 1044 Priority: 2 1045 - Regex: '^(<|"(gtest|isl|json)/)' 1046 Priority: 3 1047 - Regex: '.*' 1048 Priority: 1 1049 1050**IncludeIsMainRegex** (``std::string``) 1051 Specify a regular expression of suffixes that are allowed in the 1052 file-to-main-include mapping. 1053 1054 When guessing whether a #include is the "main" include (to assign 1055 category 0, see above), use this regex of allowed suffixes to the header 1056 stem. A partial match is done, so that: 1057 - "" means "arbitrary suffix" 1058 - "$" means "no suffix" 1059 1060 For example, if configured to "(_test)?$", then a header a.h would be seen 1061 as the "main" include in both a.cc and a_test.cc. 1062 1063**IndentCaseLabels** (``bool``) 1064 Indent case labels one level from the switch statement. 1065 1066 When ``false``, use the same indentation level as for the switch statement. 1067 Switch statement body is always indented one level more than case labels. 1068 1069 .. code-block:: c++ 1070 1071 false: true: 1072 switch (fool) { vs. switch (fool) { 1073 case 1: case 1: 1074 bar(); bar(); 1075 break; break; 1076 default: default: 1077 plop(); plop(); 1078 } } 1079 1080**IndentWidth** (``unsigned``) 1081 The number of columns to use for indentation. 1082 1083 .. code-block:: c++ 1084 1085 IndentWidth: 3 1086 1087 void f() { 1088 someFunction(); 1089 if (true, false) { 1090 f(); 1091 } 1092 } 1093 1094**IndentWrappedFunctionNames** (``bool``) 1095 Indent if a function definition or declaration is wrapped after the 1096 type. 1097 1098 .. code-block:: c++ 1099 1100 true: 1101 LoooooooooooooooooooooooooooooooooooooooongReturnType 1102 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1103 1104 false: 1105 LoooooooooooooooooooooooooooooooooooooooongReturnType 1106 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1107 1108**JavaScriptQuotes** (``JavaScriptQuoteStyle``) 1109 The JavaScriptQuoteStyle to use for JavaScript strings. 1110 1111 Possible values: 1112 1113 * ``JSQS_Leave`` (in configuration: ``Leave``) 1114 Leave string quotes as they are. 1115 1116 .. code-block:: js 1117 1118 string1 = "foo"; 1119 string2 = 'bar'; 1120 1121 * ``JSQS_Single`` (in configuration: ``Single``) 1122 Always use single quotes. 1123 1124 .. code-block:: js 1125 1126 string1 = 'foo'; 1127 string2 = 'bar'; 1128 1129 * ``JSQS_Double`` (in configuration: ``Double``) 1130 Always use double quotes. 1131 1132 .. code-block:: js 1133 1134 string1 = "foo"; 1135 string2 = "bar"; 1136 1137 1138 1139**JavaScriptWrapImports** (``bool``) 1140 Whether to wrap JavaScript import/export statements. 1141 1142 .. code-block:: js 1143 1144 true: 1145 import { 1146 VeryLongImportsAreAnnoying, 1147 VeryLongImportsAreAnnoying, 1148 VeryLongImportsAreAnnoying, 1149 } from 'some/module.js' 1150 1151 false: 1152 import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,} from "some/module.js" 1153 1154**KeepEmptyLinesAtTheStartOfBlocks** (``bool``) 1155 If true, the empty line at the start of blocks is kept. 1156 1157 .. code-block:: c++ 1158 1159 true: false: 1160 if (foo) { vs. if (foo) { 1161 bar(); 1162 bar(); } 1163 } 1164 1165**Language** (``LanguageKind``) 1166 Language, this format style is targeted at. 1167 1168 Possible values: 1169 1170 * ``LK_None`` (in configuration: ``None``) 1171 Do not use. 1172 1173 * ``LK_Cpp`` (in configuration: ``Cpp``) 1174 Should be used for C, C++. 1175 1176 * ``LK_Java`` (in configuration: ``Java``) 1177 Should be used for Java. 1178 1179 * ``LK_JavaScript`` (in configuration: ``JavaScript``) 1180 Should be used for JavaScript. 1181 1182 * ``LK_ObjC`` (in configuration: ``ObjC``) 1183 Should be used for Objective-C, Objective-C++. 1184 1185 * ``LK_Proto`` (in configuration: ``Proto``) 1186 Should be used for Protocol Buffers 1187 (https://developers.google.com/protocol-buffers/). 1188 1189 * ``LK_TableGen`` (in configuration: ``TableGen``) 1190 Should be used for TableGen code. 1191 1192 1193 1194**MacroBlockBegin** (``std::string``) 1195 A regular expression matching macros that start a block. 1196 1197 .. code-block:: c++ 1198 1199 # With: 1200 MacroBlockBegin: "^NS_MAP_BEGIN|\ 1201 NS_TABLE_HEAD$" 1202 MacroBlockEnd: "^\ 1203 NS_MAP_END|\ 1204 NS_TABLE_.*_END$" 1205 1206 NS_MAP_BEGIN 1207 foo(); 1208 NS_MAP_END 1209 1210 NS_TABLE_HEAD 1211 bar(); 1212 NS_TABLE_FOO_END 1213 1214 # Without: 1215 NS_MAP_BEGIN 1216 foo(); 1217 NS_MAP_END 1218 1219 NS_TABLE_HEAD 1220 bar(); 1221 NS_TABLE_FOO_END 1222 1223**MacroBlockEnd** (``std::string``) 1224 A regular expression matching macros that end a block. 1225 1226**MaxEmptyLinesToKeep** (``unsigned``) 1227 The maximum number of consecutive empty lines to keep. 1228 1229 .. code-block:: c++ 1230 1231 MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0 1232 int f() { int f() { 1233 int = 1; int i = 1; 1234 i = foo(); 1235 i = foo(); return i; 1236 } 1237 return i; 1238 } 1239 1240**NamespaceIndentation** (``NamespaceIndentationKind``) 1241 The indentation used for namespaces. 1242 1243 Possible values: 1244 1245 * ``NI_None`` (in configuration: ``None``) 1246 Don't indent in namespaces. 1247 1248 .. code-block:: c++ 1249 1250 namespace out { 1251 int i; 1252 namespace in { 1253 int i; 1254 } 1255 } 1256 1257 * ``NI_Inner`` (in configuration: ``Inner``) 1258 Indent only in inner namespaces (nested in other namespaces). 1259 1260 .. code-block:: c++ 1261 1262 namespace out { 1263 int i; 1264 namespace in { 1265 int i; 1266 } 1267 } 1268 1269 * ``NI_All`` (in configuration: ``All``) 1270 Indent in all namespaces. 1271 1272 .. code-block:: c++ 1273 1274 namespace out { 1275 int i; 1276 namespace in { 1277 int i; 1278 } 1279 } 1280 1281 1282 1283**ObjCBlockIndentWidth** (``unsigned``) 1284 The number of characters to use for indentation of ObjC blocks. 1285 1286 .. code-block:: objc 1287 1288 ObjCBlockIndentWidth: 4 1289 1290 [operation setCompletionBlock:^{ 1291 [self onOperationDone]; 1292 }]; 1293 1294**ObjCSpaceAfterProperty** (``bool``) 1295 Add a space after ``@property`` in Objective-C, i.e. use 1296 ``@property (readonly)`` instead of ``@property(readonly)``. 1297 1298**ObjCSpaceBeforeProtocolList** (``bool``) 1299 Add a space in front of an Objective-C protocol list, i.e. use 1300 ``Foo <Protocol>`` instead of ``Foo<Protocol>``. 1301 1302**PenaltyBreakBeforeFirstCallParameter** (``unsigned``) 1303 The penalty for breaking a function call after ``call(``. 1304 1305**PenaltyBreakComment** (``unsigned``) 1306 The penalty for each line break introduced inside a comment. 1307 1308**PenaltyBreakFirstLessLess** (``unsigned``) 1309 The penalty for breaking before the first ``<<``. 1310 1311**PenaltyBreakString** (``unsigned``) 1312 The penalty for each line break introduced inside a string literal. 1313 1314**PenaltyExcessCharacter** (``unsigned``) 1315 The penalty for each character outside of the column limit. 1316 1317**PenaltyReturnTypeOnItsOwnLine** (``unsigned``) 1318 Penalty for putting the return type of a function onto its own 1319 line. 1320 1321**PointerAlignment** (``PointerAlignmentStyle``) 1322 Pointer and reference alignment style. 1323 1324 Possible values: 1325 1326 * ``PAS_Left`` (in configuration: ``Left``) 1327 Align pointer to the left. 1328 1329 .. code-block:: c++ 1330 1331 int* a; 1332 1333 * ``PAS_Right`` (in configuration: ``Right``) 1334 Align pointer to the right. 1335 1336 .. code-block:: c++ 1337 1338 int *a; 1339 1340 * ``PAS_Middle`` (in configuration: ``Middle``) 1341 Align pointer in the middle. 1342 1343 .. code-block:: c++ 1344 1345 int * a; 1346 1347 1348 1349**ReflowComments** (``bool``) 1350 If ``true``, clang-format will attempt to re-flow comments. 1351 1352 .. code-block:: c++ 1353 1354 false: 1355 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information 1356 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */ 1357 1358 true: 1359 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 1360 // information 1361 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 1362 * information */ 1363 1364**SortIncludes** (``bool``) 1365 If ``true``, clang-format will sort ``#includes``. 1366 1367 .. code-block:: c++ 1368 1369 false: true: 1370 #include "b.h" vs. #include "a.h" 1371 #include "a.h" #include "b.h" 1372 1373**SpaceAfterCStyleCast** (``bool``) 1374 If ``true``, a space is inserted after C style casts. 1375 1376 .. code-block:: c++ 1377 1378 true: false: 1379 (int)i; vs. (int) i; 1380 1381**SpaceAfterTemplateKeyword** (``bool``) 1382 If ``true``, a space will be inserted after the 'template' keyword. 1383 1384 .. code-block:: c++ 1385 1386 true: false: 1387 template <int> void foo(); vs. template<int> void foo(); 1388 1389**SpaceBeforeAssignmentOperators** (``bool``) 1390 If ``false``, spaces will be removed before assignment operators. 1391 1392 .. code-block:: c++ 1393 1394 true: false: 1395 int a = 5; vs. int a=5; 1396 a += 42 a+=42; 1397 1398**SpaceBeforeParens** (``SpaceBeforeParensOptions``) 1399 Defines in which cases to put a space before opening parentheses. 1400 1401 Possible values: 1402 1403 * ``SBPO_Never`` (in configuration: ``Never``) 1404 Never put a space before opening parentheses. 1405 1406 .. code-block:: c++ 1407 1408 void f() { 1409 if(true) { 1410 f(); 1411 } 1412 } 1413 1414 * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) 1415 Put a space before opening parentheses only after control statement 1416 keywords (``for/if/while...``). 1417 1418 .. code-block:: c++ 1419 1420 void f() { 1421 if (true) { 1422 f(); 1423 } 1424 } 1425 1426 * ``SBPO_Always`` (in configuration: ``Always``) 1427 Always put a space before opening parentheses, except when it's 1428 prohibited by the syntax rules (in function-like macro definitions) or 1429 when determined by other style rules (after unary operators, opening 1430 parentheses, etc.) 1431 1432 .. code-block:: c++ 1433 1434 void f () { 1435 if (true) { 1436 f (); 1437 } 1438 } 1439 1440 1441 1442**SpaceInEmptyParentheses** (``bool``) 1443 If ``true``, spaces may be inserted into ``()``. 1444 1445 .. code-block:: c++ 1446 1447 true: false: 1448 void f( ) { vs. void f() { 1449 int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; 1450 if (true) { if (true) { 1451 f( ); f(); 1452 } } 1453 } } 1454 1455**SpacesBeforeTrailingComments** (``unsigned``) 1456 The number of spaces before trailing line comments 1457 (``//`` - comments). 1458 1459 This does not affect trailing block comments (``/*`` - comments) as 1460 those commonly have different usage patterns and a number of special 1461 cases. 1462 1463 .. code-block:: c++ 1464 1465 SpacesBeforeTrailingComments: 3 1466 void f() { 1467 if (true) { // foo1 1468 f(); // bar 1469 } // foo 1470 } 1471 1472**SpacesInAngles** (``bool``) 1473 If ``true``, spaces will be inserted after ``<`` and before ``>`` 1474 in template argument lists. 1475 1476 .. code-block:: c++ 1477 1478 true: false: 1479 static_cast< int >(arg); vs. static_cast<int>(arg); 1480 std::function< void(int) > fct; std::function<void(int)> fct; 1481 1482**SpacesInCStyleCastParentheses** (``bool``) 1483 If ``true``, spaces may be inserted into C style casts. 1484 1485 .. code-block:: c++ 1486 1487 true: false: 1488 x = ( int32 )y vs. x = (int32)y 1489 1490**SpacesInContainerLiterals** (``bool``) 1491 If ``true``, spaces are inserted inside container literals (e.g. 1492 ObjC and Javascript array and dict literals). 1493 1494 .. code-block:: js 1495 1496 true: false: 1497 var arr = [ 1, 2, 3 ]; vs. var arr = [1, 2, 3]; 1498 f({a : 1, b : 2, c : 3}); f({a: 1, b: 2, c: 3}); 1499 1500**SpacesInParentheses** (``bool``) 1501 If ``true``, spaces will be inserted after ``(`` and before ``)``. 1502 1503 .. code-block:: c++ 1504 1505 true: false: 1506 t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; 1507 1508**SpacesInSquareBrackets** (``bool``) 1509 If ``true``, spaces will be inserted after ``[`` and before ``]``. 1510 Lambdas or unspecified size array declarations will not be affected. 1511 1512 .. code-block:: c++ 1513 1514 true: false: 1515 int a[ 5 ]; vs. int a[5]; 1516 std::unique_ptr<int[]> foo() {} // Won't be affected 1517 1518**Standard** (``LanguageStandard``) 1519 Format compatible with this standard, e.g. use ``A<A<int> >`` 1520 instead of ``A<A<int>>`` for ``LS_Cpp03``. 1521 1522 Possible values: 1523 1524 * ``LS_Cpp03`` (in configuration: ``Cpp03``) 1525 Use C++03-compatible syntax. 1526 1527 * ``LS_Cpp11`` (in configuration: ``Cpp11``) 1528 Use features of C++11, C++14 and C++1z (e.g. ``A<A<int>>`` instead of 1529 ``A<A<int> >``). 1530 1531 * ``LS_Auto`` (in configuration: ``Auto``) 1532 Automatic detection based on the input. 1533 1534 1535 1536**TabWidth** (``unsigned``) 1537 The number of columns used for tab stops. 1538 1539**UseTab** (``UseTabStyle``) 1540 The way to use tab characters in the resulting file. 1541 1542 Possible values: 1543 1544 * ``UT_Never`` (in configuration: ``Never``) 1545 Never use tab. 1546 1547 * ``UT_ForIndentation`` (in configuration: ``ForIndentation``) 1548 Use tabs only for indentation. 1549 1550 * ``UT_ForContinuationAndIndentation`` (in configuration: ``ForContinuationAndIndentation``) 1551 Use tabs only for line continuation and indentation. 1552 1553 * ``UT_Always`` (in configuration: ``Always``) 1554 Use tabs whenever we need to fill whitespace that spans at least from 1555 one tab stop to the next one. 1556 1557 1558 1559.. END_FORMAT_STYLE_OPTIONS 1560 1561Adding additional style options 1562=============================== 1563 1564Each additional style option adds costs to the clang-format project. Some of 1565these costs affect the clang-format development itself, as we need to make 1566sure that any given combination of options work and that new features don't 1567break any of the existing options in any way. There are also costs for end users 1568as options become less discoverable and people have to think about and make a 1569decision on options they don't really care about. 1570 1571The goal of the clang-format project is more on the side of supporting a 1572limited set of styles really well as opposed to supporting every single style 1573used by a codebase somewhere in the wild. Of course, we do want to support all 1574major projects and thus have established the following bar for adding style 1575options. Each new style option must .. 1576 1577 * be used in a project of significant size (have dozens of contributors) 1578 * have a publicly accessible style guide 1579 * have a person willing to contribute and maintain patches 1580 1581Examples 1582======== 1583 1584A style similar to the `Linux Kernel style 1585<https://www.kernel.org/doc/Documentation/CodingStyle>`_: 1586 1587.. code-block:: yaml 1588 1589 BasedOnStyle: LLVM 1590 IndentWidth: 8 1591 UseTab: Always 1592 BreakBeforeBraces: Linux 1593 AllowShortIfStatementsOnASingleLine: false 1594 IndentCaseLabels: false 1595 1596The result is (imagine that tabs are used for indentation here): 1597 1598.. code-block:: c++ 1599 1600 void test() 1601 { 1602 switch (x) { 1603 case 0: 1604 case 1: 1605 do_something(); 1606 break; 1607 case 2: 1608 do_something_else(); 1609 break; 1610 default: 1611 break; 1612 } 1613 if (condition) 1614 do_something_completely_different(); 1615 1616 if (x == y) { 1617 q(); 1618 } else if (x > y) { 1619 w(); 1620 } else { 1621 r(); 1622 } 1623 } 1624 1625A style similar to the default Visual Studio formatting style: 1626 1627.. code-block:: yaml 1628 1629 UseTab: Never 1630 IndentWidth: 4 1631 BreakBeforeBraces: Allman 1632 AllowShortIfStatementsOnASingleLine: false 1633 IndentCaseLabels: false 1634 ColumnLimit: 0 1635 1636The result is: 1637 1638.. code-block:: c++ 1639 1640 void test() 1641 { 1642 switch (suffix) 1643 { 1644 case 0: 1645 case 1: 1646 do_something(); 1647 break; 1648 case 2: 1649 do_something_else(); 1650 break; 1651 default: 1652 break; 1653 } 1654 if (condition) 1655 do_somthing_completely_different(); 1656 1657 if (x == y) 1658 { 1659 q(); 1660 } 1661 else if (x > y) 1662 { 1663 w(); 1664 } 1665 else 1666 { 1667 r(); 1668 } 1669 } 1670