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