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