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** (``BreakTemplateDeclarationsStyle``) 494 The template declaration breaking style to use. 495 496 Possible values: 497 498 * ``BTDS_No`` (in configuration: ``No``) 499 Do not force break before declaration. 500 ``PenaltyBreakTemplateDeclaration`` is taken into account. 501 502 .. code-block:: c++ 503 504 template <typename T> T foo() { 505 } 506 template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa, 507 int bbbbbbbbbbbbbbbbbbbbb) { 508 } 509 510 * ``BTDS_MultiLine`` (in configuration: ``MultiLine``) 511 Force break after template declaration only when the following 512 declaration spans multiple lines. 513 514 .. code-block:: c++ 515 516 template <typename T> T foo() { 517 } 518 template <typename T> 519 T foo(int aaaaaaaaaaaaaaaaaaaaa, 520 int bbbbbbbbbbbbbbbbbbbbb) { 521 } 522 523 * ``BTDS_Yes`` (in configuration: ``Yes``) 524 Always break after template declaration. 525 526 .. code-block:: c++ 527 528 template <typename T> 529 T foo() { 530 } 531 template <typename T> 532 T foo(int aaaaaaaaaaaaaaaaaaaaa, 533 int bbbbbbbbbbbbbbbbbbbbb) { 534 } 535 536 537 538**BinPackArguments** (``bool``) 539 If ``false``, a function call's arguments will either be all on the 540 same line or will have one line each. 541 542 .. code-block:: c++ 543 544 true: 545 void f() { 546 f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, 547 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 548 } 549 550 false: 551 void f() { 552 f(aaaaaaaaaaaaaaaaaaaa, 553 aaaaaaaaaaaaaaaaaaaa, 554 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 555 } 556 557**BinPackParameters** (``bool``) 558 If ``false``, a function declaration's or function definition's 559 parameters will either all be on the same line or will have one line each. 560 561 .. code-block:: c++ 562 563 true: 564 void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa, 565 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 566 567 false: 568 void f(int aaaaaaaaaaaaaaaaaaaa, 569 int aaaaaaaaaaaaaaaaaaaa, 570 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 571 572**BraceWrapping** (``BraceWrappingFlags``) 573 Control of individual brace wrapping cases. 574 575 If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how 576 each individual brace case should be handled. Otherwise, this is ignored. 577 578 .. code-block:: yaml 579 580 # Example of usage: 581 BreakBeforeBraces: Custom 582 BraceWrapping: 583 AfterEnum: true 584 AfterStruct: false 585 SplitEmptyFunction: false 586 587 Nested configuration flags: 588 589 590 * ``bool AfterClass`` Wrap class definitions. 591 592 .. code-block:: c++ 593 594 true: 595 class foo {}; 596 597 false: 598 class foo 599 {}; 600 601 * ``bool AfterControlStatement`` Wrap control statements (``if``/``for``/``while``/``switch``/..). 602 603 .. code-block:: c++ 604 605 true: 606 if (foo()) 607 { 608 } else 609 {} 610 for (int i = 0; i < 10; ++i) 611 {} 612 613 false: 614 if (foo()) { 615 } else { 616 } 617 for (int i = 0; i < 10; ++i) { 618 } 619 620 * ``bool AfterEnum`` Wrap enum definitions. 621 622 .. code-block:: c++ 623 624 true: 625 enum X : int 626 { 627 B 628 }; 629 630 false: 631 enum X : int { B }; 632 633 * ``bool AfterFunction`` Wrap function definitions. 634 635 .. code-block:: c++ 636 637 true: 638 void foo() 639 { 640 bar(); 641 bar2(); 642 } 643 644 false: 645 void foo() { 646 bar(); 647 bar2(); 648 } 649 650 * ``bool AfterNamespace`` Wrap namespace definitions. 651 652 .. code-block:: c++ 653 654 true: 655 namespace 656 { 657 int foo(); 658 int bar(); 659 } 660 661 false: 662 namespace { 663 int foo(); 664 int bar(); 665 } 666 667 * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (interfaces, implementations...). 668 @autoreleasepool and @synchronized blocks are wrapped 669 according to `AfterControlStatement` flag. 670 671 * ``bool AfterStruct`` Wrap struct definitions. 672 673 .. code-block:: c++ 674 675 true: 676 struct foo 677 { 678 int x; 679 }; 680 681 false: 682 struct foo { 683 int x; 684 }; 685 686 * ``bool AfterUnion`` Wrap union definitions. 687 688 .. code-block:: c++ 689 690 true: 691 union foo 692 { 693 int x; 694 } 695 696 false: 697 union foo { 698 int x; 699 } 700 701 * ``bool AfterExternBlock`` Wrap extern blocks. 702 703 .. code-block:: c++ 704 705 true: 706 extern "C" 707 { 708 int foo(); 709 } 710 711 false: 712 extern "C" { 713 int foo(); 714 } 715 716 * ``bool BeforeCatch`` Wrap before ``catch``. 717 718 .. code-block:: c++ 719 720 true: 721 try { 722 foo(); 723 } 724 catch () { 725 } 726 727 false: 728 try { 729 foo(); 730 } catch () { 731 } 732 733 * ``bool BeforeElse`` Wrap before ``else``. 734 735 .. code-block:: c++ 736 737 true: 738 if (foo()) { 739 } 740 else { 741 } 742 743 false: 744 if (foo()) { 745 } else { 746 } 747 748 * ``bool IndentBraces`` Indent the wrapped braces themselves. 749 750 * ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line. 751 This option is used only if the opening brace of the function has 752 already been wrapped, i.e. the `AfterFunction` brace wrapping mode is 753 set, and the function could/should not be put on a single line (as per 754 `AllowShortFunctionsOnASingleLine` and constructor formatting options). 755 756 .. code-block:: c++ 757 758 int f() vs. inf f() 759 {} { 760 } 761 762 * ``bool SplitEmptyRecord`` If ``false``, empty record (e.g. class, struct or union) body 763 can be put on a single line. This option is used only if the opening 764 brace of the record has already been wrapped, i.e. the `AfterClass` 765 (for classes) brace wrapping mode is set. 766 767 .. code-block:: c++ 768 769 class Foo vs. class Foo 770 {} { 771 } 772 773 * ``bool SplitEmptyNamespace`` If ``false``, empty namespace body can be put on a single line. 774 This option is used only if the opening brace of the namespace has 775 already been wrapped, i.e. the `AfterNamespace` brace wrapping mode is 776 set. 777 778 .. code-block:: c++ 779 780 namespace Foo vs. namespace Foo 781 {} { 782 } 783 784 785**BreakAfterJavaFieldAnnotations** (``bool``) 786 Break after each annotation on a field in Java files. 787 788 .. code-block:: java 789 790 true: false: 791 @Partial vs. @Partial @Mock DataLoad loader; 792 @Mock 793 DataLoad loader; 794 795**BreakBeforeBinaryOperators** (``BinaryOperatorStyle``) 796 The way to wrap binary operators. 797 798 Possible values: 799 800 * ``BOS_None`` (in configuration: ``None``) 801 Break after operators. 802 803 .. code-block:: c++ 804 805 LooooooooooongType loooooooooooooooooooooongVariable = 806 someLooooooooooooooooongFunction(); 807 808 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + 809 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == 810 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && 811 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > 812 ccccccccccccccccccccccccccccccccccccccccc; 813 814 * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``) 815 Break before operators that aren't assignments. 816 817 .. code-block:: c++ 818 819 LooooooooooongType loooooooooooooooooooooongVariable = 820 someLooooooooooooooooongFunction(); 821 822 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 823 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 824 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 825 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 826 > ccccccccccccccccccccccccccccccccccccccccc; 827 828 * ``BOS_All`` (in configuration: ``All``) 829 Break before operators. 830 831 .. code-block:: c++ 832 833 LooooooooooongType loooooooooooooooooooooongVariable 834 = someLooooooooooooooooongFunction(); 835 836 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 837 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 838 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 839 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 840 > ccccccccccccccccccccccccccccccccccccccccc; 841 842 843 844**BreakBeforeBraces** (``BraceBreakingStyle``) 845 The brace breaking style to use. 846 847 Possible values: 848 849 * ``BS_Attach`` (in configuration: ``Attach``) 850 Always attach braces to surrounding context. 851 852 .. code-block:: c++ 853 854 try { 855 foo(); 856 } catch () { 857 } 858 void foo() { bar(); } 859 class foo {}; 860 if (foo()) { 861 } else { 862 } 863 enum X : int { A, B }; 864 865 * ``BS_Linux`` (in configuration: ``Linux``) 866 Like ``Attach``, but break before braces on function, namespace and 867 class definitions. 868 869 .. code-block:: c++ 870 871 try { 872 foo(); 873 } catch () { 874 } 875 void foo() { bar(); } 876 class foo 877 { 878 }; 879 if (foo()) { 880 } else { 881 } 882 enum X : int { A, B }; 883 884 * ``BS_Mozilla`` (in configuration: ``Mozilla``) 885 Like ``Attach``, but break before braces on enum, function, and record 886 definitions. 887 888 .. code-block:: c++ 889 890 try { 891 foo(); 892 } catch () { 893 } 894 void foo() { bar(); } 895 class foo 896 { 897 }; 898 if (foo()) { 899 } else { 900 } 901 enum X : int { A, B }; 902 903 * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) 904 Like ``Attach``, but break before function definitions, ``catch``, and 905 ``else``. 906 907 .. code-block:: c++ 908 909 try { 910 foo(); 911 } catch () { 912 } 913 void foo() { bar(); } 914 class foo 915 { 916 }; 917 if (foo()) { 918 } else { 919 } 920 enum X : int 921 { 922 A, 923 B 924 }; 925 926 * ``BS_Allman`` (in configuration: ``Allman``) 927 Always break before braces. 928 929 .. code-block:: c++ 930 931 try { 932 foo(); 933 } 934 catch () { 935 } 936 void foo() { bar(); } 937 class foo { 938 }; 939 if (foo()) { 940 } 941 else { 942 } 943 enum X : int { A, B }; 944 945 * ``BS_GNU`` (in configuration: ``GNU``) 946 Always break before braces and add an extra level of indentation to 947 braces of control statements, not to those of class, function 948 or other definitions. 949 950 .. code-block:: c++ 951 952 try 953 { 954 foo(); 955 } 956 catch () 957 { 958 } 959 void foo() { bar(); } 960 class foo 961 { 962 }; 963 if (foo()) 964 { 965 } 966 else 967 { 968 } 969 enum X : int 970 { 971 A, 972 B 973 }; 974 975 * ``BS_WebKit`` (in configuration: ``WebKit``) 976 Like ``Attach``, but break before functions. 977 978 .. code-block:: c++ 979 980 try { 981 foo(); 982 } catch () { 983 } 984 void foo() { bar(); } 985 class foo { 986 }; 987 if (foo()) { 988 } else { 989 } 990 enum X : int { A, B }; 991 992 * ``BS_Custom`` (in configuration: ``Custom``) 993 Configure each individual brace in `BraceWrapping`. 994 995 996 997**BreakBeforeInheritanceComma** (``bool``) 998 If ``true``, in the class inheritance expression clang-format will 999 break before ``:`` and ``,`` if there is multiple inheritance. 1000 1001 .. code-block:: c++ 1002 1003 true: false: 1004 class MyClass vs. class MyClass : public X, public Y { 1005 : public X }; 1006 , public Y { 1007 }; 1008 1009**BreakBeforeTernaryOperators** (``bool``) 1010 If ``true``, ternary operators will be placed after line breaks. 1011 1012 .. code-block:: c++ 1013 1014 true: 1015 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription 1016 ? firstValue 1017 : SecondValueVeryVeryVeryVeryLong; 1018 1019 false: 1020 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ? 1021 firstValue : 1022 SecondValueVeryVeryVeryVeryLong; 1023 1024**BreakConstructorInitializers** (``BreakConstructorInitializersStyle``) 1025 The constructor initializers style to use. 1026 1027 Possible values: 1028 1029 * ``BCIS_BeforeColon`` (in configuration: ``BeforeColon``) 1030 Break constructor initializers before the colon and after the commas. 1031 1032 .. code-block:: c++ 1033 1034 Constructor() 1035 : initializer1(), 1036 initializer2() 1037 1038 * ``BCIS_BeforeComma`` (in configuration: ``BeforeComma``) 1039 Break constructor initializers before the colon and commas, and align 1040 the commas with the colon. 1041 1042 .. code-block:: c++ 1043 1044 Constructor() 1045 : initializer1() 1046 , initializer2() 1047 1048 * ``BCIS_AfterColon`` (in configuration: ``AfterColon``) 1049 Break constructor initializers after the colon and commas. 1050 1051 .. code-block:: c++ 1052 1053 Constructor() : 1054 initializer1(), 1055 initializer2() 1056 1057 1058 1059**BreakStringLiterals** (``bool``) 1060 Allow breaking string literals when formatting. 1061 1062**ColumnLimit** (``unsigned``) 1063 The column limit. 1064 1065 A column limit of ``0`` means that there is no column limit. In this case, 1066 clang-format will respect the input's line breaking decisions within 1067 statements unless they contradict other rules. 1068 1069**CommentPragmas** (``std::string``) 1070 A regular expression that describes comments with special meaning, 1071 which should not be split into lines or otherwise changed. 1072 1073 .. code-block:: c++ 1074 1075 // CommentPragmas: '^ FOOBAR pragma:' 1076 // Will leave the following line unaffected 1077 #include <vector> // FOOBAR pragma: keep 1078 1079**CompactNamespaces** (``bool``) 1080 If ``true``, consecutive namespace declarations will be on the same 1081 line. If ``false``, each namespace is declared on a new line. 1082 1083 .. code-block:: c++ 1084 1085 true: 1086 namespace Foo { namespace Bar { 1087 }} 1088 1089 false: 1090 namespace Foo { 1091 namespace Bar { 1092 } 1093 } 1094 1095 If it does not fit on a single line, the overflowing namespaces get 1096 wrapped: 1097 1098 .. code-block:: c++ 1099 1100 namespace Foo { namespace Bar { 1101 namespace Extra { 1102 }}} 1103 1104**ConstructorInitializerAllOnOneLineOrOnePerLine** (``bool``) 1105 If the constructor initializers don't fit on a line, put each 1106 initializer on its own line. 1107 1108 .. code-block:: c++ 1109 1110 true: 1111 SomeClass::Constructor() 1112 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 1113 return 0; 1114 } 1115 1116 false: 1117 SomeClass::Constructor() 1118 : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), 1119 aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa) { 1120 return 0; 1121 } 1122 1123**ConstructorInitializerIndentWidth** (``unsigned``) 1124 The number of characters to use for indentation of constructor 1125 initializer lists. 1126 1127**ContinuationIndentWidth** (``unsigned``) 1128 Indent width for line continuations. 1129 1130 .. code-block:: c++ 1131 1132 ContinuationIndentWidth: 2 1133 1134 int i = // VeryVeryVeryVeryVeryLongComment 1135 longFunction( // Again a long comment 1136 arg); 1137 1138**Cpp11BracedListStyle** (``bool``) 1139 If ``true``, format braced lists as best suited for C++11 braced 1140 lists. 1141 1142 Important differences: 1143 - No spaces inside the braced list. 1144 - No line break before the closing brace. 1145 - Indentation with the continuation indent, not with the block indent. 1146 1147 Fundamentally, C++11 braced lists are formatted exactly like function 1148 calls would be formatted in their place. If the braced list follows a name 1149 (e.g. a type or variable name), clang-format formats as if the ``{}`` were 1150 the parentheses of a function call with that name. If there is no name, 1151 a zero-length name is assumed. 1152 1153 .. code-block:: c++ 1154 1155 true: false: 1156 vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 }; 1157 vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} }; 1158 f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]); 1159 new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 }; 1160 1161**DerivePointerAlignment** (``bool``) 1162 If ``true``, analyze the formatted file for the most common 1163 alignment of ``&`` and ``*``. 1164 Pointer and reference alignment styles are going to be updated according 1165 to the preferences found in the file. 1166 ``PointerAlignment`` is then used only as fallback. 1167 1168**DisableFormat** (``bool``) 1169 Disables formatting completely. 1170 1171**ExperimentalAutoDetectBinPacking** (``bool``) 1172 If ``true``, clang-format detects whether function calls and 1173 definitions are formatted with one parameter per line. 1174 1175 Each call can be bin-packed, one-per-line or inconclusive. If it is 1176 inconclusive, e.g. completely on one line, but a decision needs to be 1177 made, clang-format analyzes whether there are other bin-packed cases in 1178 the input file and act accordingly. 1179 1180 NOTE: This is an experimental flag, that might go away or be renamed. Do 1181 not use this in config files, etc. Use at your own risk. 1182 1183**FixNamespaceComments** (``bool``) 1184 If ``true``, clang-format adds missing namespace end comments and 1185 fixes invalid existing ones. 1186 1187 .. code-block:: c++ 1188 1189 true: false: 1190 namespace a { vs. namespace a { 1191 foo(); foo(); 1192 } // namespace a; } 1193 1194**ForEachMacros** (``std::vector<std::string>``) 1195 A vector of macros that should be interpreted as foreach loops 1196 instead of as function calls. 1197 1198 These are expected to be macros of the form: 1199 1200 .. code-block:: c++ 1201 1202 FOREACH(<variable-declaration>, ...) 1203 <loop-body> 1204 1205 In the .clang-format configuration file, this can be configured like: 1206 1207 .. code-block:: yaml 1208 1209 ForEachMacros: ['RANGES_FOR', 'FOREACH'] 1210 1211 For example: BOOST_FOREACH. 1212 1213**IncludeBlocks** (``IncludeBlocksStyle``) 1214 Dependent on the value, multiple ``#include`` blocks can be sorted 1215 as one and divided based on category. 1216 1217 Possible values: 1218 1219 * ``IBS_Preserve`` (in configuration: ``Preserve``) 1220 Sort each ``#include`` block separately. 1221 1222 .. code-block:: c++ 1223 1224 #include "b.h" into #include "b.h" 1225 1226 #include <lib/main.h> #include "a.h" 1227 #include "a.h" #include <lib/main.h> 1228 1229 * ``IBS_Merge`` (in configuration: ``Merge``) 1230 Merge multiple ``#include`` blocks together and sort as one. 1231 1232 .. code-block:: c++ 1233 1234 #include "b.h" into #include "a.h" 1235 #include "b.h" 1236 #include <lib/main.h> #include <lib/main.h> 1237 #include "a.h" 1238 1239 * ``IBS_Regroup`` (in configuration: ``Regroup``) 1240 Merge multiple ``#include`` blocks together and sort as one. 1241 Then split into groups based on category priority. See 1242 ``IncludeCategories``. 1243 1244 .. code-block:: c++ 1245 1246 #include "b.h" into #include "a.h" 1247 #include "b.h" 1248 #include <lib/main.h> 1249 #include "a.h" #include <lib/main.h> 1250 1251 1252 1253**IncludeCategories** (``std::vector<IncludeCategory>``) 1254 Regular expressions denoting the different ``#include`` categories 1255 used for ordering ``#includes``. 1256 1257 These regular expressions are matched against the filename of an include 1258 (including the <> or "") in order. The value belonging to the first 1259 matching regular expression is assigned and ``#includes`` are sorted first 1260 according to increasing category number and then alphabetically within 1261 each category. 1262 1263 If none of the regular expressions match, INT_MAX is assigned as 1264 category. The main header for a source file automatically gets category 0. 1265 so that it is generally kept at the beginning of the ``#includes`` 1266 (http://llvm.org/docs/CodingStandards.html#include-style). However, you 1267 can also assign negative priorities if you have certain headers that 1268 always need to be first. 1269 1270 To configure this in the .clang-format file, use: 1271 1272 .. code-block:: yaml 1273 1274 IncludeCategories: 1275 - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 1276 Priority: 2 1277 - Regex: '^(<|"(gtest|gmock|isl|json)/)' 1278 Priority: 3 1279 - Regex: '.*' 1280 Priority: 1 1281 1282**IncludeIsMainRegex** (``std::string``) 1283 Specify a regular expression of suffixes that are allowed in the 1284 file-to-main-include mapping. 1285 1286 When guessing whether a #include is the "main" include (to assign 1287 category 0, see above), use this regex of allowed suffixes to the header 1288 stem. A partial match is done, so that: 1289 - "" means "arbitrary suffix" 1290 - "$" means "no suffix" 1291 1292 For example, if configured to "(_test)?$", then a header a.h would be seen 1293 as the "main" include in both a.cc and a_test.cc. 1294 1295**IndentCaseLabels** (``bool``) 1296 Indent case labels one level from the switch statement. 1297 1298 When ``false``, use the same indentation level as for the switch statement. 1299 Switch statement body is always indented one level more than case labels. 1300 1301 .. code-block:: c++ 1302 1303 false: true: 1304 switch (fool) { vs. switch (fool) { 1305 case 1: case 1: 1306 bar(); bar(); 1307 break; break; 1308 default: default: 1309 plop(); plop(); 1310 } } 1311 1312**IndentPPDirectives** (``PPDirectiveIndentStyle``) 1313 The preprocessor directive indenting style to use. 1314 1315 Possible values: 1316 1317 * ``PPDIS_None`` (in configuration: ``None``) 1318 Does not indent any directives. 1319 1320 .. code-block:: c++ 1321 1322 #if FOO 1323 #if BAR 1324 #include <foo> 1325 #endif 1326 #endif 1327 1328 * ``PPDIS_AfterHash`` (in configuration: ``AfterHash``) 1329 Indents directives after the hash. 1330 1331 .. code-block:: c++ 1332 1333 #if FOO 1334 # if BAR 1335 # include <foo> 1336 # endif 1337 #endif 1338 1339 1340 1341**IndentWidth** (``unsigned``) 1342 The number of columns to use for indentation. 1343 1344 .. code-block:: c++ 1345 1346 IndentWidth: 3 1347 1348 void f() { 1349 someFunction(); 1350 if (true, false) { 1351 f(); 1352 } 1353 } 1354 1355**IndentWrappedFunctionNames** (``bool``) 1356 Indent if a function definition or declaration is wrapped after the 1357 type. 1358 1359 .. code-block:: c++ 1360 1361 true: 1362 LoooooooooooooooooooooooooooooooooooooooongReturnType 1363 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1364 1365 false: 1366 LoooooooooooooooooooooooooooooooooooooooongReturnType 1367 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 1368 1369**JavaScriptQuotes** (``JavaScriptQuoteStyle``) 1370 The JavaScriptQuoteStyle to use for JavaScript strings. 1371 1372 Possible values: 1373 1374 * ``JSQS_Leave`` (in configuration: ``Leave``) 1375 Leave string quotes as they are. 1376 1377 .. code-block:: js 1378 1379 string1 = "foo"; 1380 string2 = 'bar'; 1381 1382 * ``JSQS_Single`` (in configuration: ``Single``) 1383 Always use single quotes. 1384 1385 .. code-block:: js 1386 1387 string1 = 'foo'; 1388 string2 = 'bar'; 1389 1390 * ``JSQS_Double`` (in configuration: ``Double``) 1391 Always use double quotes. 1392 1393 .. code-block:: js 1394 1395 string1 = "foo"; 1396 string2 = "bar"; 1397 1398 1399 1400**JavaScriptWrapImports** (``bool``) 1401 Whether to wrap JavaScript import/export statements. 1402 1403 .. code-block:: js 1404 1405 true: 1406 import { 1407 VeryLongImportsAreAnnoying, 1408 VeryLongImportsAreAnnoying, 1409 VeryLongImportsAreAnnoying, 1410 } from 'some/module.js' 1411 1412 false: 1413 import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,} from "some/module.js" 1414 1415**KeepEmptyLinesAtTheStartOfBlocks** (``bool``) 1416 If true, the empty line at the start of blocks is kept. 1417 1418 .. code-block:: c++ 1419 1420 true: false: 1421 if (foo) { vs. if (foo) { 1422 bar(); 1423 bar(); } 1424 } 1425 1426**Language** (``LanguageKind``) 1427 Language, this format style is targeted at. 1428 1429 Possible values: 1430 1431 * ``LK_None`` (in configuration: ``None``) 1432 Do not use. 1433 1434 * ``LK_Cpp`` (in configuration: ``Cpp``) 1435 Should be used for C, C++. 1436 1437 * ``LK_Java`` (in configuration: ``Java``) 1438 Should be used for Java. 1439 1440 * ``LK_JavaScript`` (in configuration: ``JavaScript``) 1441 Should be used for JavaScript. 1442 1443 * ``LK_ObjC`` (in configuration: ``ObjC``) 1444 Should be used for Objective-C, Objective-C++. 1445 1446 * ``LK_Proto`` (in configuration: ``Proto``) 1447 Should be used for Protocol Buffers 1448 (https://developers.google.com/protocol-buffers/). 1449 1450 * ``LK_TableGen`` (in configuration: ``TableGen``) 1451 Should be used for TableGen code. 1452 1453 * ``LK_TextProto`` (in configuration: ``TextProto``) 1454 Should be used for Protocol Buffer messages in text format 1455 (https://developers.google.com/protocol-buffers/). 1456 1457 1458 1459**MacroBlockBegin** (``std::string``) 1460 A regular expression matching macros that start a block. 1461 1462 .. code-block:: c++ 1463 1464 # With: 1465 MacroBlockBegin: "^NS_MAP_BEGIN|\ 1466 NS_TABLE_HEAD$" 1467 MacroBlockEnd: "^\ 1468 NS_MAP_END|\ 1469 NS_TABLE_.*_END$" 1470 1471 NS_MAP_BEGIN 1472 foo(); 1473 NS_MAP_END 1474 1475 NS_TABLE_HEAD 1476 bar(); 1477 NS_TABLE_FOO_END 1478 1479 # Without: 1480 NS_MAP_BEGIN 1481 foo(); 1482 NS_MAP_END 1483 1484 NS_TABLE_HEAD 1485 bar(); 1486 NS_TABLE_FOO_END 1487 1488**MacroBlockEnd** (``std::string``) 1489 A regular expression matching macros that end a block. 1490 1491**MaxEmptyLinesToKeep** (``unsigned``) 1492 The maximum number of consecutive empty lines to keep. 1493 1494 .. code-block:: c++ 1495 1496 MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0 1497 int f() { int f() { 1498 int = 1; int i = 1; 1499 i = foo(); 1500 i = foo(); return i; 1501 } 1502 return i; 1503 } 1504 1505**NamespaceIndentation** (``NamespaceIndentationKind``) 1506 The indentation used for namespaces. 1507 1508 Possible values: 1509 1510 * ``NI_None`` (in configuration: ``None``) 1511 Don't indent in namespaces. 1512 1513 .. code-block:: c++ 1514 1515 namespace out { 1516 int i; 1517 namespace in { 1518 int i; 1519 } 1520 } 1521 1522 * ``NI_Inner`` (in configuration: ``Inner``) 1523 Indent only in inner namespaces (nested in other namespaces). 1524 1525 .. code-block:: c++ 1526 1527 namespace out { 1528 int i; 1529 namespace in { 1530 int i; 1531 } 1532 } 1533 1534 * ``NI_All`` (in configuration: ``All``) 1535 Indent in all namespaces. 1536 1537 .. code-block:: c++ 1538 1539 namespace out { 1540 int i; 1541 namespace in { 1542 int i; 1543 } 1544 } 1545 1546 1547 1548**ObjCBinPackProtocolList** (``BinPackStyle``) 1549 Controls bin-packing Objective-C protocol conformance list 1550 items into as few lines as possible when they go over ``ColumnLimit``. 1551 1552 If ``Auto`` (the default), delegates to the value in 1553 ``BinPackParameters``. If that is ``true``, bin-packs Objective-C 1554 protocol conformance list items into as few lines as possible 1555 whenever they go over ``ColumnLimit``. 1556 1557 If ``Always``, always bin-packs Objective-C protocol conformance 1558 list items into as few lines as possible whenever they go over 1559 ``ColumnLimit``. 1560 1561 If ``Never``, lays out Objective-C protocol conformance list items 1562 onto individual lines whenever they go over ``ColumnLimit``. 1563 1564 1565 .. code-block:: c++ 1566 1567 Always (or Auto, if BinPackParameters=true): 1568 @interface ccccccccccccc () < 1569 ccccccccccccc, ccccccccccccc, 1570 ccccccccccccc, ccccccccccccc> { 1571 } 1572 1573 Never (or Auto, if BinPackParameters=false): 1574 @interface ddddddddddddd () < 1575 ddddddddddddd, 1576 ddddddddddddd, 1577 ddddddddddddd, 1578 ddddddddddddd> { 1579 } 1580 1581 Possible values: 1582 1583 * ``BPS_Auto`` (in configuration: ``Auto``) 1584 Automatically determine parameter bin-packing behavior. 1585 1586 * ``BPS_Always`` (in configuration: ``Always``) 1587 Always bin-pack parameters. 1588 1589 * ``BPS_Never`` (in configuration: ``Never``) 1590 Never bin-pack parameters. 1591 1592 1593 1594**ObjCBlockIndentWidth** (``unsigned``) 1595 The number of characters to use for indentation of ObjC blocks. 1596 1597 .. code-block:: objc 1598 1599 ObjCBlockIndentWidth: 4 1600 1601 [operation setCompletionBlock:^{ 1602 [self onOperationDone]; 1603 }]; 1604 1605**ObjCSpaceAfterProperty** (``bool``) 1606 Add a space after ``@property`` in Objective-C, i.e. use 1607 ``@property (readonly)`` instead of ``@property(readonly)``. 1608 1609**ObjCSpaceBeforeProtocolList** (``bool``) 1610 Add a space in front of an Objective-C protocol list, i.e. use 1611 ``Foo <Protocol>`` instead of ``Foo<Protocol>``. 1612 1613**PenaltyBreakAssignment** (``unsigned``) 1614 The penalty for breaking around an assignment operator. 1615 1616**PenaltyBreakBeforeFirstCallParameter** (``unsigned``) 1617 The penalty for breaking a function call after ``call(``. 1618 1619**PenaltyBreakComment** (``unsigned``) 1620 The penalty for each line break introduced inside a comment. 1621 1622**PenaltyBreakFirstLessLess** (``unsigned``) 1623 The penalty for breaking before the first ``<<``. 1624 1625**PenaltyBreakString** (``unsigned``) 1626 The penalty for each line break introduced inside a string literal. 1627 1628**PenaltyBreakTemplateDeclaration** (``unsigned``) 1629 The penalty for breaking after template declaration. 1630 1631**PenaltyExcessCharacter** (``unsigned``) 1632 The penalty for each character outside of the column limit. 1633 1634**PenaltyReturnTypeOnItsOwnLine** (``unsigned``) 1635 Penalty for putting the return type of a function onto its own 1636 line. 1637 1638**PointerAlignment** (``PointerAlignmentStyle``) 1639 Pointer and reference alignment style. 1640 1641 Possible values: 1642 1643 * ``PAS_Left`` (in configuration: ``Left``) 1644 Align pointer to the left. 1645 1646 .. code-block:: c++ 1647 1648 int* a; 1649 1650 * ``PAS_Right`` (in configuration: ``Right``) 1651 Align pointer to the right. 1652 1653 .. code-block:: c++ 1654 1655 int *a; 1656 1657 * ``PAS_Middle`` (in configuration: ``Middle``) 1658 Align pointer in the middle. 1659 1660 .. code-block:: c++ 1661 1662 int * a; 1663 1664 1665 1666**RawStringFormats** (``std::vector<RawStringFormat>``) 1667 Defines hints for detecting supported languages code blocks in raw 1668 strings. 1669 1670 A raw string with a matching delimiter or a matching enclosing function 1671 name will be reformatted assuming the specified language based on the 1672 style for that language defined in the .clang-format file. If no style has 1673 been defined in the .clang-format file for the specific language, a 1674 predefined style given by 'BasedOnStyle' is used. If 'BasedOnStyle' is not 1675 found, the formatting is based on llvm style. A matching delimiter takes 1676 precedence over a matching enclosing function name for determining the 1677 language of the raw string contents. 1678 1679 If a canonical delimiter is specified, occurrences of other delimiters for 1680 the same language will be updated to the canonical if possible. 1681 1682 There should be at most one specification per language and each delimiter 1683 and enclosing function should not occur in multiple specifications. 1684 1685 To configure this in the .clang-format file, use: 1686 1687 .. code-block:: yaml 1688 1689 RawStringFormats: 1690 - Language: TextProto 1691 Delimiters: 1692 - 'pb' 1693 - 'proto' 1694 EnclosingFunctions: 1695 - 'PARSE_TEXT_PROTO' 1696 BasedOnStyle: google 1697 - Language: Cpp 1698 Delimiters: 1699 - 'cc' 1700 - 'cpp' 1701 BasedOnStyle: llvm 1702 CanonicalDelimiter: 'cc' 1703 1704**ReflowComments** (``bool``) 1705 If ``true``, clang-format will attempt to re-flow comments. 1706 1707 .. code-block:: c++ 1708 1709 false: 1710 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information 1711 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */ 1712 1713 true: 1714 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 1715 // information 1716 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 1717 * information */ 1718 1719**SortIncludes** (``bool``) 1720 If ``true``, clang-format will sort ``#includes``. 1721 1722 .. code-block:: c++ 1723 1724 false: true: 1725 #include "b.h" vs. #include "a.h" 1726 #include "a.h" #include "b.h" 1727 1728**SortUsingDeclarations** (``bool``) 1729 If ``true``, clang-format will sort using declarations. 1730 1731 The order of using declarations is defined as follows: 1732 Split the strings by "::" and discard any initial empty strings. The last 1733 element of each list is a non-namespace name; all others are namespace 1734 names. Sort the lists of names lexicographically, where the sort order of 1735 individual names is that all non-namespace names come before all namespace 1736 names, and within those groups, names are in case-insensitive 1737 lexicographic order. 1738 1739 .. code-block:: c++ 1740 1741 false: true: 1742 using std::cout; vs. using std::cin; 1743 using std::cin; using std::cout; 1744 1745**SpaceAfterCStyleCast** (``bool``) 1746 If ``true``, a space is inserted after C style casts. 1747 1748 .. code-block:: c++ 1749 1750 true: false: 1751 (int) i; vs. (int)i; 1752 1753**SpaceAfterTemplateKeyword** (``bool``) 1754 If ``true``, a space will be inserted after the 'template' keyword. 1755 1756 .. code-block:: c++ 1757 1758 true: false: 1759 template <int> void foo(); vs. template<int> void foo(); 1760 1761**SpaceBeforeAssignmentOperators** (``bool``) 1762 If ``false``, spaces will be removed before assignment operators. 1763 1764 .. code-block:: c++ 1765 1766 true: false: 1767 int a = 5; vs. int a=5; 1768 a += 42 a+=42; 1769 1770**SpaceBeforeCtorInitializerColon** (``bool``) 1771 If ``false``, spaces will be removed before constructor initializer 1772 colon. 1773 1774 .. code-block:: c++ 1775 1776 true: false: 1777 Foo::Foo() : a(a) {} Foo::Foo(): a(a) {} 1778 1779**SpaceBeforeInheritanceColon** (``bool``) 1780 If ``false``, spaces will be removed before inheritance colon. 1781 1782 .. code-block:: c++ 1783 1784 true: false: 1785 class Foo : Bar {} vs. class Foo: Bar {} 1786 1787**SpaceBeforeParens** (``SpaceBeforeParensOptions``) 1788 Defines in which cases to put a space before opening parentheses. 1789 1790 Possible values: 1791 1792 * ``SBPO_Never`` (in configuration: ``Never``) 1793 Never put a space before opening parentheses. 1794 1795 .. code-block:: c++ 1796 1797 void f() { 1798 if(true) { 1799 f(); 1800 } 1801 } 1802 1803 * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) 1804 Put a space before opening parentheses only after control statement 1805 keywords (``for/if/while...``). 1806 1807 .. code-block:: c++ 1808 1809 void f() { 1810 if (true) { 1811 f(); 1812 } 1813 } 1814 1815 * ``SBPO_Always`` (in configuration: ``Always``) 1816 Always put a space before opening parentheses, except when it's 1817 prohibited by the syntax rules (in function-like macro definitions) or 1818 when determined by other style rules (after unary operators, opening 1819 parentheses, etc.) 1820 1821 .. code-block:: c++ 1822 1823 void f () { 1824 if (true) { 1825 f (); 1826 } 1827 } 1828 1829 1830 1831**SpaceBeforeRangeBasedForLoopColon** (``bool``) 1832 If ``false``, spaces will be removed before range-based for loop 1833 colon. 1834 1835 .. code-block:: c++ 1836 1837 true: false: 1838 for (auto v : values) {} vs. for(auto v: values) {} 1839 1840**SpaceInEmptyParentheses** (``bool``) 1841 If ``true``, spaces may be inserted into ``()``. 1842 1843 .. code-block:: c++ 1844 1845 true: false: 1846 void f( ) { vs. void f() { 1847 int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; 1848 if (true) { if (true) { 1849 f( ); f(); 1850 } } 1851 } } 1852 1853**SpacesBeforeTrailingComments** (``unsigned``) 1854 The number of spaces before trailing line comments 1855 (``//`` - comments). 1856 1857 This does not affect trailing block comments (``/*`` - comments) as 1858 those commonly have different usage patterns and a number of special 1859 cases. 1860 1861 .. code-block:: c++ 1862 1863 SpacesBeforeTrailingComments: 3 1864 void f() { 1865 if (true) { // foo1 1866 f(); // bar 1867 } // foo 1868 } 1869 1870**SpacesInAngles** (``bool``) 1871 If ``true``, spaces will be inserted after ``<`` and before ``>`` 1872 in template argument lists. 1873 1874 .. code-block:: c++ 1875 1876 true: false: 1877 static_cast< int >(arg); vs. static_cast<int>(arg); 1878 std::function< void(int) > fct; std::function<void(int)> fct; 1879 1880**SpacesInCStyleCastParentheses** (``bool``) 1881 If ``true``, spaces may be inserted into C style casts. 1882 1883 .. code-block:: c++ 1884 1885 true: false: 1886 x = ( int32 )y vs. x = (int32)y 1887 1888**SpacesInContainerLiterals** (``bool``) 1889 If ``true``, spaces are inserted inside container literals (e.g. 1890 ObjC and Javascript array and dict literals). 1891 1892 .. code-block:: js 1893 1894 true: false: 1895 var arr = [ 1, 2, 3 ]; vs. var arr = [1, 2, 3]; 1896 f({a : 1, b : 2, c : 3}); f({a: 1, b: 2, c: 3}); 1897 1898**SpacesInParentheses** (``bool``) 1899 If ``true``, spaces will be inserted after ``(`` and before ``)``. 1900 1901 .. code-block:: c++ 1902 1903 true: false: 1904 t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; 1905 1906**SpacesInSquareBrackets** (``bool``) 1907 If ``true``, spaces will be inserted after ``[`` and before ``]``. 1908 Lambdas or unspecified size array declarations will not be affected. 1909 1910 .. code-block:: c++ 1911 1912 true: false: 1913 int a[ 5 ]; vs. int a[5]; 1914 std::unique_ptr<int[]> foo() {} // Won't be affected 1915 1916**Standard** (``LanguageStandard``) 1917 Format compatible with this standard, e.g. use ``A<A<int> >`` 1918 instead of ``A<A<int>>`` for ``LS_Cpp03``. 1919 1920 Possible values: 1921 1922 * ``LS_Cpp03`` (in configuration: ``Cpp03``) 1923 Use C++03-compatible syntax. 1924 1925 * ``LS_Cpp11`` (in configuration: ``Cpp11``) 1926 Use features of C++11, C++14 and C++1z (e.g. ``A<A<int>>`` instead of 1927 ``A<A<int> >``). 1928 1929 * ``LS_Auto`` (in configuration: ``Auto``) 1930 Automatic detection based on the input. 1931 1932 1933 1934**TabWidth** (``unsigned``) 1935 The number of columns used for tab stops. 1936 1937**UseTab** (``UseTabStyle``) 1938 The way to use tab characters in the resulting file. 1939 1940 Possible values: 1941 1942 * ``UT_Never`` (in configuration: ``Never``) 1943 Never use tab. 1944 1945 * ``UT_ForIndentation`` (in configuration: ``ForIndentation``) 1946 Use tabs only for indentation. 1947 1948 * ``UT_ForContinuationAndIndentation`` (in configuration: ``ForContinuationAndIndentation``) 1949 Use tabs only for line continuation and indentation. 1950 1951 * ``UT_Always`` (in configuration: ``Always``) 1952 Use tabs whenever we need to fill whitespace that spans at least from 1953 one tab stop to the next one. 1954 1955 1956 1957.. END_FORMAT_STYLE_OPTIONS 1958 1959Adding additional style options 1960=============================== 1961 1962Each additional style option adds costs to the clang-format project. Some of 1963these costs affect the clang-format development itself, as we need to make 1964sure that any given combination of options work and that new features don't 1965break any of the existing options in any way. There are also costs for end users 1966as options become less discoverable and people have to think about and make a 1967decision on options they don't really care about. 1968 1969The goal of the clang-format project is more on the side of supporting a 1970limited set of styles really well as opposed to supporting every single style 1971used by a codebase somewhere in the wild. Of course, we do want to support all 1972major projects and thus have established the following bar for adding style 1973options. Each new style option must .. 1974 1975 * be used in a project of significant size (have dozens of contributors) 1976 * have a publicly accessible style guide 1977 * have a person willing to contribute and maintain patches 1978 1979Examples 1980======== 1981 1982A style similar to the `Linux Kernel style 1983<https://www.kernel.org/doc/Documentation/CodingStyle>`_: 1984 1985.. code-block:: yaml 1986 1987 BasedOnStyle: LLVM 1988 IndentWidth: 8 1989 UseTab: Always 1990 BreakBeforeBraces: Linux 1991 AllowShortIfStatementsOnASingleLine: false 1992 IndentCaseLabels: false 1993 1994The result is (imagine that tabs are used for indentation here): 1995 1996.. code-block:: c++ 1997 1998 void test() 1999 { 2000 switch (x) { 2001 case 0: 2002 case 1: 2003 do_something(); 2004 break; 2005 case 2: 2006 do_something_else(); 2007 break; 2008 default: 2009 break; 2010 } 2011 if (condition) 2012 do_something_completely_different(); 2013 2014 if (x == y) { 2015 q(); 2016 } else if (x > y) { 2017 w(); 2018 } else { 2019 r(); 2020 } 2021 } 2022 2023A style similar to the default Visual Studio formatting style: 2024 2025.. code-block:: yaml 2026 2027 UseTab: Never 2028 IndentWidth: 4 2029 BreakBeforeBraces: Allman 2030 AllowShortIfStatementsOnASingleLine: false 2031 IndentCaseLabels: false 2032 ColumnLimit: 0 2033 2034The result is: 2035 2036.. code-block:: c++ 2037 2038 void test() 2039 { 2040 switch (suffix) 2041 { 2042 case 0: 2043 case 1: 2044 do_something(); 2045 break; 2046 case 2: 2047 do_something_else(); 2048 break; 2049 default: 2050 break; 2051 } 2052 if (condition) 2053 do_somthing_completely_different(); 2054 2055 if (x == y) 2056 { 2057 q(); 2058 } 2059 else if (x > y) 2060 { 2061 w(); 2062 } 2063 else 2064 { 2065 r(); 2066 } 2067 } 2068