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