1.. raw:: html 2 3 <style type="text/css"> 4 .versionbadge { background-color: #1c913d; height: 20px; display: inline-block; width: 120px; text-align: center; border-radius: 5px; color: #FFFFFF; font-family="Verdana,Geneva,DejaVu Sans,sans-serif" } 5 </style> 6 7.. role:: versionbadge 8 9========================== 10Clang-Format Style Options 11========================== 12 13:doc:`ClangFormatStyleOptions` describes configurable formatting style options 14supported by :doc:`LibFormat` and :doc:`ClangFormat`. 15 16When using :program:`clang-format` command line utility or 17``clang::format::reformat(...)`` functions from code, one can either use one of 18the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft) or 19create a custom style by configuring specific style options. 20 21 22Configuring Style with clang-format 23=================================== 24 25:program:`clang-format` supports two ways to provide custom style options: 26directly specify style configuration in the ``-style=`` command line option or 27use ``-style=file`` and put style configuration in the ``.clang-format`` or 28``_clang-format`` file in the project directory. 29 30When using ``-style=file``, :program:`clang-format` for each input file will 31try to find the ``.clang-format`` file located in the closest parent directory 32of the input file. When the standard input is used, the search is started from 33the current directory. 34 35When using ``-style=file:<format_file_path>``, :program:`clang-format` for 36each input file will use the format file located at `<format_file_path>`. 37The path may be absolute or relative to the working directory. 38 39The ``.clang-format`` file uses YAML format: 40 41.. code-block:: yaml 42 43 key1: value1 44 key2: value2 45 # A comment. 46 ... 47 48The configuration file can consist of several sections each having different 49``Language:`` parameter denoting the programming language this section of the 50configuration is targeted at. See the description of the **Language** option 51below for the list of supported languages. The first section may have no 52language set, it will set the default style options for all languages. 53Configuration sections for specific language will override options set in the 54default section. 55 56When :program:`clang-format` formats a file, it auto-detects the language using 57the file name. When formatting standard input or a file that doesn't have the 58extension corresponding to its language, ``-assume-filename=`` option can be 59used to override the file name :program:`clang-format` uses to detect the 60language. 61 62An example of a configuration file for multiple languages: 63 64.. code-block:: yaml 65 66 --- 67 # We'll use defaults from the LLVM style, but with 4 columns indentation. 68 BasedOnStyle: LLVM 69 IndentWidth: 4 70 --- 71 Language: Cpp 72 # Force pointers to the type for C++. 73 DerivePointerAlignment: false 74 PointerAlignment: Left 75 --- 76 Language: JavaScript 77 # Use 100 columns for JS. 78 ColumnLimit: 100 79 --- 80 Language: Proto 81 # Don't format .proto files. 82 DisableFormat: true 83 --- 84 Language: CSharp 85 # Use 100 columns for C#. 86 ColumnLimit: 100 87 ... 88 89An easy way to get a valid ``.clang-format`` file containing all configuration 90options of a certain predefined style is: 91 92.. code-block:: console 93 94 clang-format -style=llvm -dump-config > .clang-format 95 96When specifying configuration in the ``-style=`` option, the same configuration 97is applied for all input files. The format of the configuration is: 98 99.. code-block:: console 100 101 -style='{key1: value1, key2: value2, ...}' 102 103 104Disabling Formatting on a Piece of Code 105======================================= 106 107Clang-format understands also special comments that switch formatting in a 108delimited range. The code between a comment ``// clang-format off`` or 109``/* clang-format off */`` up to a comment ``// clang-format on`` or 110``/* clang-format on */`` will not be formatted. The comments themselves 111will be formatted (aligned) normally. 112 113.. code-block:: c++ 114 115 int formatted_code; 116 // clang-format off 117 void unformatted_code ; 118 // clang-format on 119 void formatted_code_again; 120 121 122Configuring Style in Code 123========================= 124 125When using ``clang::format::reformat(...)`` functions, the format is specified 126by supplying the `clang::format::FormatStyle 127<https://clang.llvm.org/doxygen/structclang_1_1format_1_1FormatStyle.html>`_ 128structure. 129 130 131Configurable Format Style Options 132================================= 133 134This section lists the supported style options. Value type is specified for 135each option. For enumeration types possible values are specified both as a C++ 136enumeration member (with a prefix, e.g. ``LS_Auto``), and as a value usable in 137the configuration (without a prefix: ``Auto``). 138 139 140**BasedOnStyle** (``String``) 141 The style used for all options not specifically set in the configuration. 142 143 This option is supported only in the :program:`clang-format` configuration 144 (both within ``-style='{...}'`` and the ``.clang-format`` file). 145 146 Possible values: 147 148 * ``LLVM`` 149 A style complying with the `LLVM coding standards 150 <https://llvm.org/docs/CodingStandards.html>`_ 151 * ``Google`` 152 A style complying with `Google's C++ style guide 153 <https://google.github.io/styleguide/cppguide.html>`_ 154 * ``Chromium`` 155 A style complying with `Chromium's style guide 156 <https://chromium.googlesource.com/chromium/src/+/refs/heads/main/styleguide/styleguide.md>`_ 157 * ``Mozilla`` 158 A style complying with `Mozilla's style guide 159 <https://firefox-source-docs.mozilla.org/code-quality/coding-style/index.html>`_ 160 * ``WebKit`` 161 A style complying with `WebKit's style guide 162 <https://www.webkit.org/coding/coding-style.html>`_ 163 * ``Microsoft`` 164 A style complying with `Microsoft's style guide 165 <https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference>`_ 166 * ``GNU`` 167 A style complying with the `GNU coding standards 168 <https://www.gnu.org/prep/standards/standards.html>`_ 169 * ``InheritParentConfig`` 170 Not a real style, but allows to use the ``.clang-format`` file from the 171 parent directory (or its parent if there is none). If there is no parent 172 file found it falls back to the ``fallback`` style, and applies the changes 173 to that. 174 175 With this option you can overwrite some parts of your main style for your 176 subdirectories. This is also possible through the command line, e.g.: 177 ``--style={BasedOnStyle: InheritParentConfig, ColumnLimit: 20}`` 178 179.. START_FORMAT_STYLE_OPTIONS 180 181**AccessModifierOffset** (``Integer``) :versionbadge:`clang-format 3.3` 182 The extra indent or outdent of access modifiers, e.g. ``public:``. 183 184**AlignAfterOpenBracket** (``BracketAlignmentStyle``) :versionbadge:`clang-format 3.8` 185 If ``true``, horizontally aligns arguments after an open bracket. 186 187 This applies to round brackets (parentheses), angle brackets and square 188 brackets. 189 190 Possible values: 191 192 * ``BAS_Align`` (in configuration: ``Align``) 193 Align parameters on the open bracket, e.g.: 194 195 .. code-block:: c++ 196 197 someLongFunction(argument1, 198 argument2); 199 200 * ``BAS_DontAlign`` (in configuration: ``DontAlign``) 201 Don't align, instead use ``ContinuationIndentWidth``, e.g.: 202 203 .. code-block:: c++ 204 205 someLongFunction(argument1, 206 argument2); 207 208 * ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``) 209 Always break after an open bracket, if the parameters don't fit 210 on a single line, e.g.: 211 212 .. code-block:: c++ 213 214 someLongFunction( 215 argument1, argument2); 216 217 * ``BAS_BlockIndent`` (in configuration: ``BlockIndent``) 218 Always break after an open bracket, if the parameters don't fit 219 on a single line. Closing brackets will be placed on a new line. 220 E.g.: 221 222 .. code-block:: c++ 223 224 someLongFunction( 225 argument1, argument2 226 ) 227 228 229 .. warning:: 230 231 Note: This currently only applies to parentheses. 232 233 234 235**AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` 236 if not ``None``, when using initialization for an array of structs 237 aligns the fields into columns. 238 239 Possible values: 240 241 * ``AIAS_Left`` (in configuration: ``Left``) 242 Align array column and left justify the columns e.g.: 243 244 .. code-block:: c++ 245 246 struct test demo[] = 247 { 248 {56, 23, "hello"}, 249 {-1, 93463, "world"}, 250 {7, 5, "!!" } 251 }; 252 253 * ``AIAS_Right`` (in configuration: ``Right``) 254 Align array column and right justify the columns e.g.: 255 256 .. code-block:: c++ 257 258 struct test demo[] = 259 { 260 {56, 23, "hello"}, 261 {-1, 93463, "world"}, 262 { 7, 5, "!!"} 263 }; 264 265 * ``AIAS_None`` (in configuration: ``None``) 266 Don't align array initializer columns. 267 268 269 270**AlignConsecutiveAssignments** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 3.8` 271 Style of aligning consecutive assignments. 272 273 ``Consecutive`` will result in formattings like: 274 275 .. code-block:: c++ 276 277 int a = 1; 278 int somelongname = 2; 279 double c = 3; 280 281 Possible values: 282 283 * ``ACS_None`` (in configuration: ``None``) 284 Do not align assignments on consecutive lines. 285 286 * ``ACS_Consecutive`` (in configuration: ``Consecutive``) 287 Align assignments on consecutive lines. This will result in 288 formattings like: 289 290 .. code-block:: c++ 291 292 int a = 1; 293 int somelongname = 2; 294 double c = 3; 295 296 int d = 3; 297 /* A comment. */ 298 double e = 4; 299 300 * ``ACS_AcrossEmptyLines`` (in configuration: ``AcrossEmptyLines``) 301 Same as ACS_Consecutive, but also spans over empty lines, e.g. 302 303 .. code-block:: c++ 304 305 int a = 1; 306 int somelongname = 2; 307 double c = 3; 308 309 int d = 3; 310 /* A comment. */ 311 double e = 4; 312 313 * ``ACS_AcrossComments`` (in configuration: ``AcrossComments``) 314 Same as ACS_Consecutive, but also spans over lines only containing 315 comments, e.g. 316 317 .. code-block:: c++ 318 319 int a = 1; 320 int somelongname = 2; 321 double c = 3; 322 323 int d = 3; 324 /* A comment. */ 325 double e = 4; 326 327 * ``ACS_AcrossEmptyLinesAndComments`` 328 (in configuration: ``AcrossEmptyLinesAndComments``) 329 330 Same as ACS_Consecutive, but also spans over lines only containing 331 comments and empty lines, e.g. 332 333 .. code-block:: c++ 334 335 int a = 1; 336 int somelongname = 2; 337 double c = 3; 338 339 int d = 3; 340 /* A comment. */ 341 double e = 4; 342 343**AlignConsecutiveBitFields** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 11` 344 Style of aligning consecutive bit field. 345 346 ``Consecutive`` will align the bitfield separators of consecutive lines. 347 This will result in formattings like: 348 349 .. code-block:: c++ 350 351 int aaaa : 1; 352 int b : 12; 353 int ccc : 8; 354 355 Possible values: 356 357 * ``ACS_None`` (in configuration: ``None``) 358 Do not align bit fields on consecutive lines. 359 360 * ``ACS_Consecutive`` (in configuration: ``Consecutive``) 361 Align bit fields on consecutive lines. This will result in 362 formattings like: 363 364 .. code-block:: c++ 365 366 int aaaa : 1; 367 int b : 12; 368 int ccc : 8; 369 370 int d : 2; 371 /* A comment. */ 372 int ee : 3; 373 374 * ``ACS_AcrossEmptyLines`` (in configuration: ``AcrossEmptyLines``) 375 Same as ACS_Consecutive, but also spans over empty lines, e.g. 376 377 .. code-block:: c++ 378 379 int aaaa : 1; 380 int b : 12; 381 int ccc : 8; 382 383 int d : 2; 384 /* A comment. */ 385 int ee : 3; 386 387 * ``ACS_AcrossComments`` (in configuration: ``AcrossComments``) 388 Same as ACS_Consecutive, but also spans over lines only containing 389 comments, e.g. 390 391 .. code-block:: c++ 392 393 int aaaa : 1; 394 int b : 12; 395 int ccc : 8; 396 397 int d : 2; 398 /* A comment. */ 399 int ee : 3; 400 401 * ``ACS_AcrossEmptyLinesAndComments`` 402 (in configuration: ``AcrossEmptyLinesAndComments``) 403 404 Same as ACS_Consecutive, but also spans over lines only containing 405 comments and empty lines, e.g. 406 407 .. code-block:: c++ 408 409 int aaaa : 1; 410 int b : 12; 411 int ccc : 8; 412 413 int d : 2; 414 /* A comment. */ 415 int ee : 3; 416 417**AlignConsecutiveDeclarations** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 3.8` 418 Style of aligning consecutive declarations. 419 420 ``Consecutive`` will align the declaration names of consecutive lines. 421 This will result in formattings like: 422 423 .. code-block:: c++ 424 425 int aaaa = 12; 426 float b = 23; 427 std::string ccc; 428 429 Possible values: 430 431 * ``ACS_None`` (in configuration: ``None``) 432 Do not align bit declarations on consecutive lines. 433 434 * ``ACS_Consecutive`` (in configuration: ``Consecutive``) 435 Align declarations on consecutive lines. This will result in 436 formattings like: 437 438 .. code-block:: c++ 439 440 int aaaa = 12; 441 float b = 23; 442 std::string ccc; 443 444 int a = 42; 445 /* A comment. */ 446 bool c = false; 447 448 * ``ACS_AcrossEmptyLines`` (in configuration: ``AcrossEmptyLines``) 449 Same as ACS_Consecutive, but also spans over empty lines, e.g. 450 451 .. code-block:: c++ 452 453 int aaaa = 12; 454 float b = 23; 455 std::string ccc; 456 457 int a = 42; 458 /* A comment. */ 459 bool c = false; 460 461 * ``ACS_AcrossComments`` (in configuration: ``AcrossComments``) 462 Same as ACS_Consecutive, but also spans over lines only containing 463 comments, e.g. 464 465 .. code-block:: c++ 466 467 int aaaa = 12; 468 float b = 23; 469 std::string ccc; 470 471 int a = 42; 472 /* A comment. */ 473 bool c = false; 474 475 * ``ACS_AcrossEmptyLinesAndComments`` 476 (in configuration: ``AcrossEmptyLinesAndComments``) 477 478 Same as ACS_Consecutive, but also spans over lines only containing 479 comments and empty lines, e.g. 480 481 .. code-block:: c++ 482 483 int aaaa = 12; 484 float b = 23; 485 std::string ccc; 486 487 int a = 42; 488 /* A comment. */ 489 bool c = false; 490 491**AlignConsecutiveMacros** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 9` 492 Style of aligning consecutive macro definitions. 493 494 ``Consecutive`` will result in formattings like: 495 496 .. code-block:: c++ 497 498 #define SHORT_NAME 42 499 #define LONGER_NAME 0x007f 500 #define EVEN_LONGER_NAME (2) 501 #define foo(x) (x * x) 502 #define bar(y, z) (y + z) 503 504 Possible values: 505 506 * ``ACS_None`` (in configuration: ``None``) 507 Do not align macro definitions on consecutive lines. 508 509 * ``ACS_Consecutive`` (in configuration: ``Consecutive``) 510 Align macro definitions on consecutive lines. This will result in 511 formattings like: 512 513 .. code-block:: c++ 514 515 #define SHORT_NAME 42 516 #define LONGER_NAME 0x007f 517 #define EVEN_LONGER_NAME (2) 518 519 #define foo(x) (x * x) 520 /* some comment */ 521 #define bar(y, z) (y + z) 522 523 * ``ACS_AcrossEmptyLines`` (in configuration: ``AcrossEmptyLines``) 524 Same as ACS_Consecutive, but also spans over empty lines, e.g. 525 526 .. code-block:: c++ 527 528 #define SHORT_NAME 42 529 #define LONGER_NAME 0x007f 530 #define EVEN_LONGER_NAME (2) 531 532 #define foo(x) (x * x) 533 /* some comment */ 534 #define bar(y, z) (y + z) 535 536 * ``ACS_AcrossComments`` (in configuration: ``AcrossComments``) 537 Same as ACS_Consecutive, but also spans over lines only containing 538 comments, e.g. 539 540 .. code-block:: c++ 541 542 #define SHORT_NAME 42 543 #define LONGER_NAME 0x007f 544 #define EVEN_LONGER_NAME (2) 545 546 #define foo(x) (x * x) 547 /* some comment */ 548 #define bar(y, z) (y + z) 549 550 * ``ACS_AcrossEmptyLinesAndComments`` 551 (in configuration: ``AcrossEmptyLinesAndComments``) 552 553 Same as ACS_Consecutive, but also spans over lines only containing 554 comments and empty lines, e.g. 555 556 .. code-block:: c++ 557 558 #define SHORT_NAME 42 559 #define LONGER_NAME 0x007f 560 #define EVEN_LONGER_NAME (2) 561 562 #define foo(x) (x * x) 563 /* some comment */ 564 #define bar(y, z) (y + z) 565 566**AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``) :versionbadge:`clang-format 5` 567 Options for aligning backslashes in escaped newlines. 568 569 Possible values: 570 571 * ``ENAS_DontAlign`` (in configuration: ``DontAlign``) 572 Don't align escaped newlines. 573 574 .. code-block:: c++ 575 576 #define A \ 577 int aaaa; \ 578 int b; \ 579 int dddddddddd; 580 581 * ``ENAS_Left`` (in configuration: ``Left``) 582 Align escaped newlines as far left as possible. 583 584 .. code-block:: c++ 585 586 true: 587 #define A \ 588 int aaaa; \ 589 int b; \ 590 int dddddddddd; 591 592 false: 593 594 * ``ENAS_Right`` (in configuration: ``Right``) 595 Align escaped newlines in the right-most column. 596 597 .. code-block:: c++ 598 599 #define A \ 600 int aaaa; \ 601 int b; \ 602 int dddddddddd; 603 604 605 606**AlignOperands** (``OperandAlignmentStyle``) :versionbadge:`clang-format 12` 607 If ``true``, horizontally align operands of binary and ternary 608 expressions. 609 610 Possible values: 611 612 * ``OAS_DontAlign`` (in configuration: ``DontAlign``) 613 Do not align operands of binary and ternary expressions. 614 The wrapped lines are indented ``ContinuationIndentWidth`` spaces from 615 the start of the line. 616 617 * ``OAS_Align`` (in configuration: ``Align``) 618 Horizontally align operands of binary and ternary expressions. 619 620 Specifically, this aligns operands of a single expression that needs 621 to be split over multiple lines, e.g.: 622 623 .. code-block:: c++ 624 625 int aaa = bbbbbbbbbbbbbbb + 626 ccccccccccccccc; 627 628 When ``BreakBeforeBinaryOperators`` is set, the wrapped operator is 629 aligned with the operand on the first line. 630 631 .. code-block:: c++ 632 633 int aaa = bbbbbbbbbbbbbbb 634 + ccccccccccccccc; 635 636 * ``OAS_AlignAfterOperator`` (in configuration: ``AlignAfterOperator``) 637 Horizontally align operands of binary and ternary expressions. 638 639 This is similar to ``AO_Align``, except when 640 ``BreakBeforeBinaryOperators`` is set, the operator is un-indented so 641 that the wrapped operand is aligned with the operand on the first line. 642 643 .. code-block:: c++ 644 645 int aaa = bbbbbbbbbbbbbbb 646 + ccccccccccccccc; 647 648 649 650**AlignTrailingComments** (``Boolean``) :versionbadge:`clang-format 3.7` 651 If ``true``, aligns trailing comments. 652 653 .. code-block:: c++ 654 655 true: false: 656 int a; // My comment a vs. int a; // My comment a 657 int b = 2; // comment b int b = 2; // comment about b 658 659**AllowAllArgumentsOnNextLine** (``Boolean``) :versionbadge:`clang-format 9` 660 If a function call or braced initializer list doesn't fit on a 661 line, allow putting all arguments onto the next line, even if 662 ``BinPackArguments`` is ``false``. 663 664 .. code-block:: c++ 665 666 true: 667 callFunction( 668 a, b, c, d); 669 670 false: 671 callFunction(a, 672 b, 673 c, 674 d); 675 676**AllowAllConstructorInitializersOnNextLine** (``Boolean``) :versionbadge:`clang-format 9` 677 This option is **deprecated**. See ``NextLine`` of 678 ``PackConstructorInitializers``. 679 680**AllowAllParametersOfDeclarationOnNextLine** (``Boolean``) :versionbadge:`clang-format 3.3` 681 If the function declaration doesn't fit on a line, 682 allow putting all parameters of a function declaration onto 683 the next line even if ``BinPackParameters`` is ``false``. 684 685 .. code-block:: c++ 686 687 true: 688 void myFunction( 689 int a, int b, int c, int d, int e); 690 691 false: 692 void myFunction(int a, 693 int b, 694 int c, 695 int d, 696 int e); 697 698**AllowShortBlocksOnASingleLine** (``ShortBlockStyle``) :versionbadge:`clang-format 11` 699 Dependent on the value, ``while (true) { continue; }`` can be put on a 700 single line. 701 702 Possible values: 703 704 * ``SBS_Never`` (in configuration: ``Never``) 705 Never merge blocks into a single line. 706 707 .. code-block:: c++ 708 709 while (true) { 710 } 711 while (true) { 712 continue; 713 } 714 715 * ``SBS_Empty`` (in configuration: ``Empty``) 716 Only merge empty blocks. 717 718 .. code-block:: c++ 719 720 while (true) {} 721 while (true) { 722 continue; 723 } 724 725 * ``SBS_Always`` (in configuration: ``Always``) 726 Always merge short blocks into a single line. 727 728 .. code-block:: c++ 729 730 while (true) {} 731 while (true) { continue; } 732 733 734 735**AllowShortCaseLabelsOnASingleLine** (``Boolean``) :versionbadge:`clang-format 3.6` 736 If ``true``, short case labels will be contracted to a single line. 737 738 .. code-block:: c++ 739 740 true: false: 741 switch (a) { vs. switch (a) { 742 case 1: x = 1; break; case 1: 743 case 2: return; x = 1; 744 } break; 745 case 2: 746 return; 747 } 748 749**AllowShortEnumsOnASingleLine** (``Boolean``) :versionbadge:`clang-format 12` 750 Allow short enums on a single line. 751 752 .. code-block:: c++ 753 754 true: 755 enum { A, B } myEnum; 756 757 false: 758 enum { 759 A, 760 B 761 } myEnum; 762 763**AllowShortFunctionsOnASingleLine** (``ShortFunctionStyle``) :versionbadge:`clang-format 3.5` 764 Dependent on the value, ``int f() { return 0; }`` can be put on a 765 single line. 766 767 Possible values: 768 769 * ``SFS_None`` (in configuration: ``None``) 770 Never merge functions into a single line. 771 772 * ``SFS_InlineOnly`` (in configuration: ``InlineOnly``) 773 Only merge functions defined inside a class. Same as "inline", 774 except it does not implies "empty": i.e. top level empty functions 775 are not merged either. 776 777 .. code-block:: c++ 778 779 class Foo { 780 void f() { foo(); } 781 }; 782 void f() { 783 foo(); 784 } 785 void f() { 786 } 787 788 * ``SFS_Empty`` (in configuration: ``Empty``) 789 Only merge empty functions. 790 791 .. code-block:: c++ 792 793 void f() {} 794 void f2() { 795 bar2(); 796 } 797 798 * ``SFS_Inline`` (in configuration: ``Inline``) 799 Only merge functions defined inside a class. Implies "empty". 800 801 .. code-block:: c++ 802 803 class Foo { 804 void f() { foo(); } 805 }; 806 void f() { 807 foo(); 808 } 809 void f() {} 810 811 * ``SFS_All`` (in configuration: ``All``) 812 Merge all functions fitting on a single line. 813 814 .. code-block:: c++ 815 816 class Foo { 817 void f() { foo(); } 818 }; 819 void f() { bar(); } 820 821 822 823**AllowShortIfStatementsOnASingleLine** (``ShortIfStyle``) :versionbadge:`clang-format 9` 824 Dependent on the value, ``if (a) return;`` can be put on a single line. 825 826 Possible values: 827 828 * ``SIS_Never`` (in configuration: ``Never``) 829 Never put short ifs on the same line. 830 831 .. code-block:: c++ 832 833 if (a) 834 return; 835 836 if (b) 837 return; 838 else 839 return; 840 841 if (c) 842 return; 843 else { 844 return; 845 } 846 847 * ``SIS_WithoutElse`` (in configuration: ``WithoutElse``) 848 Put short ifs on the same line only if there is no else statement. 849 850 .. code-block:: c++ 851 852 if (a) return; 853 854 if (b) 855 return; 856 else 857 return; 858 859 if (c) 860 return; 861 else { 862 return; 863 } 864 865 * ``SIS_OnlyFirstIf`` (in configuration: ``OnlyFirstIf``) 866 Put short ifs, but not else ifs nor else statements, on the same line. 867 868 .. code-block:: c++ 869 870 if (a) return; 871 872 if (b) return; 873 else if (b) 874 return; 875 else 876 return; 877 878 if (c) return; 879 else { 880 return; 881 } 882 883 * ``SIS_AllIfsAndElse`` (in configuration: ``AllIfsAndElse``) 884 Always put short ifs, else ifs and else statements on the same 885 line. 886 887 .. code-block:: c++ 888 889 if (a) return; 890 891 if (b) return; 892 else return; 893 894 if (c) return; 895 else { 896 return; 897 } 898 899 900 901**AllowShortLambdasOnASingleLine** (``ShortLambdaStyle``) :versionbadge:`clang-format 9` 902 Dependent on the value, ``auto lambda []() { return 0; }`` can be put on a 903 single line. 904 905 Possible values: 906 907 * ``SLS_None`` (in configuration: ``None``) 908 Never merge lambdas into a single line. 909 910 * ``SLS_Empty`` (in configuration: ``Empty``) 911 Only merge empty lambdas. 912 913 .. code-block:: c++ 914 915 auto lambda = [](int a) {} 916 auto lambda2 = [](int a) { 917 return a; 918 }; 919 920 * ``SLS_Inline`` (in configuration: ``Inline``) 921 Merge lambda into a single line if argument of a function. 922 923 .. code-block:: c++ 924 925 auto lambda = [](int a) { 926 return a; 927 }; 928 sort(a.begin(), a.end(), ()[] { return x < y; }) 929 930 * ``SLS_All`` (in configuration: ``All``) 931 Merge all lambdas fitting on a single line. 932 933 .. code-block:: c++ 934 935 auto lambda = [](int a) {} 936 auto lambda2 = [](int a) { return a; }; 937 938 939 940**AllowShortLoopsOnASingleLine** (``Boolean``) :versionbadge:`clang-format 3.7` 941 If ``true``, ``while (true) continue;`` can be put on a single 942 line. 943 944**AlwaysBreakAfterDefinitionReturnType** (``DefinitionReturnTypeBreakingStyle``) :versionbadge:`clang-format 3.7` 945 The function definition return type breaking style to use. This 946 option is **deprecated** and is retained for backwards compatibility. 947 948 Possible values: 949 950 * ``DRTBS_None`` (in configuration: ``None``) 951 Break after return type automatically. 952 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. 953 954 * ``DRTBS_All`` (in configuration: ``All``) 955 Always break after the return type. 956 957 * ``DRTBS_TopLevel`` (in configuration: ``TopLevel``) 958 Always break after the return types of top-level functions. 959 960 961 962**AlwaysBreakAfterReturnType** (``ReturnTypeBreakingStyle``) :versionbadge:`clang-format 3.8` 963 The function declaration return type breaking style to use. 964 965 Possible values: 966 967 * ``RTBS_None`` (in configuration: ``None``) 968 Break after return type automatically. 969 ``PenaltyReturnTypeOnItsOwnLine`` is taken into account. 970 971 .. code-block:: c++ 972 973 class A { 974 int f() { return 0; }; 975 }; 976 int f(); 977 int f() { return 1; } 978 979 * ``RTBS_All`` (in configuration: ``All``) 980 Always break after the return type. 981 982 .. code-block:: c++ 983 984 class A { 985 int 986 f() { 987 return 0; 988 }; 989 }; 990 int 991 f(); 992 int 993 f() { 994 return 1; 995 } 996 997 * ``RTBS_TopLevel`` (in configuration: ``TopLevel``) 998 Always break after the return types of top-level functions. 999 1000 .. code-block:: c++ 1001 1002 class A { 1003 int f() { return 0; }; 1004 }; 1005 int 1006 f(); 1007 int 1008 f() { 1009 return 1; 1010 } 1011 1012 * ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``) 1013 Always break after the return type of function definitions. 1014 1015 .. code-block:: c++ 1016 1017 class A { 1018 int 1019 f() { 1020 return 0; 1021 }; 1022 }; 1023 int f(); 1024 int 1025 f() { 1026 return 1; 1027 } 1028 1029 * ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``) 1030 Always break after the return type of top-level definitions. 1031 1032 .. code-block:: c++ 1033 1034 class A { 1035 int f() { return 0; }; 1036 }; 1037 int f(); 1038 int 1039 f() { 1040 return 1; 1041 } 1042 1043 1044 1045**AlwaysBreakBeforeMultilineStrings** (``Boolean``) :versionbadge:`clang-format 3.4` 1046 If ``true``, always break before multiline string literals. 1047 1048 This flag is mean to make cases where there are multiple multiline strings 1049 in a file look more consistent. Thus, it will only take effect if wrapping 1050 the string at that point leads to it being indented 1051 ``ContinuationIndentWidth`` spaces from the start of the line. 1052 1053 .. code-block:: c++ 1054 1055 true: false: 1056 aaaa = vs. aaaa = "bbbb" 1057 "bbbb" "cccc"; 1058 "cccc"; 1059 1060**AlwaysBreakTemplateDeclarations** (``BreakTemplateDeclarationsStyle``) :versionbadge:`clang-format 7` 1061 The template declaration breaking style to use. 1062 1063 Possible values: 1064 1065 * ``BTDS_No`` (in configuration: ``No``) 1066 Do not force break before declaration. 1067 ``PenaltyBreakTemplateDeclaration`` is taken into account. 1068 1069 .. code-block:: c++ 1070 1071 template <typename T> T foo() { 1072 } 1073 template <typename T> T foo(int aaaaaaaaaaaaaaaaaaaaa, 1074 int bbbbbbbbbbbbbbbbbbbbb) { 1075 } 1076 1077 * ``BTDS_MultiLine`` (in configuration: ``MultiLine``) 1078 Force break after template declaration only when the following 1079 declaration spans multiple lines. 1080 1081 .. code-block:: c++ 1082 1083 template <typename T> T foo() { 1084 } 1085 template <typename T> 1086 T foo(int aaaaaaaaaaaaaaaaaaaaa, 1087 int bbbbbbbbbbbbbbbbbbbbb) { 1088 } 1089 1090 * ``BTDS_Yes`` (in configuration: ``Yes``) 1091 Always break after template declaration. 1092 1093 .. code-block:: c++ 1094 1095 template <typename T> 1096 T foo() { 1097 } 1098 template <typename T> 1099 T foo(int aaaaaaaaaaaaaaaaaaaaa, 1100 int bbbbbbbbbbbbbbbbbbbbb) { 1101 } 1102 1103 1104 1105**AttributeMacros** (``List of Strings``) :versionbadge:`clang-format 12` 1106 A vector of strings that should be interpreted as attributes/qualifiers 1107 instead of identifiers. This can be useful for language extensions or 1108 static analyzer annotations. 1109 1110 For example: 1111 1112 .. code-block:: c++ 1113 1114 x = (char *__capability)&y; 1115 int function(void) __ununsed; 1116 void only_writes_to_buffer(char *__output buffer); 1117 1118 In the .clang-format configuration file, this can be configured like: 1119 1120 .. code-block:: yaml 1121 1122 AttributeMacros: ['__capability', '__output', '__ununsed'] 1123 1124**BinPackArguments** (``Boolean``) :versionbadge:`clang-format 3.7` 1125 If ``false``, a function call's arguments will either be all on the 1126 same line or will have one line each. 1127 1128 .. code-block:: c++ 1129 1130 true: 1131 void f() { 1132 f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa, 1133 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 1134 } 1135 1136 false: 1137 void f() { 1138 f(aaaaaaaaaaaaaaaaaaaa, 1139 aaaaaaaaaaaaaaaaaaaa, 1140 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa); 1141 } 1142 1143**BinPackParameters** (``Boolean``) :versionbadge:`clang-format 3.7` 1144 If ``false``, a function declaration's or function definition's 1145 parameters will either all be on the same line or will have one line each. 1146 1147 .. code-block:: c++ 1148 1149 true: 1150 void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa, 1151 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 1152 1153 false: 1154 void f(int aaaaaaaaaaaaaaaaaaaa, 1155 int aaaaaaaaaaaaaaaaaaaa, 1156 int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {} 1157 1158**BitFieldColonSpacing** (``BitFieldColonSpacingStyle``) :versionbadge:`clang-format 12` 1159 The BitFieldColonSpacingStyle to use for bitfields. 1160 1161 Possible values: 1162 1163 * ``BFCS_Both`` (in configuration: ``Both``) 1164 Add one space on each side of the ``:`` 1165 1166 .. code-block:: c++ 1167 1168 unsigned bf : 2; 1169 1170 * ``BFCS_None`` (in configuration: ``None``) 1171 Add no space around the ``:`` (except when needed for 1172 ``AlignConsecutiveBitFields``). 1173 1174 .. code-block:: c++ 1175 1176 unsigned bf:2; 1177 1178 * ``BFCS_Before`` (in configuration: ``Before``) 1179 Add space before the ``:`` only 1180 1181 .. code-block:: c++ 1182 1183 unsigned bf :2; 1184 1185 * ``BFCS_After`` (in configuration: ``After``) 1186 Add space after the ``:`` only (space may be added before if 1187 needed for ``AlignConsecutiveBitFields``). 1188 1189 .. code-block:: c++ 1190 1191 unsigned bf: 2; 1192 1193 1194 1195**BraceWrapping** (``BraceWrappingFlags``) :versionbadge:`clang-format 3.8` 1196 Control of individual brace wrapping cases. 1197 1198 If ``BreakBeforeBraces`` is set to ``BS_Custom``, use this to specify how 1199 each individual brace case should be handled. Otherwise, this is ignored. 1200 1201 .. code-block:: yaml 1202 1203 # Example of usage: 1204 BreakBeforeBraces: Custom 1205 BraceWrapping: 1206 AfterEnum: true 1207 AfterStruct: false 1208 SplitEmptyFunction: false 1209 1210 Nested configuration flags: 1211 1212 1213 * ``bool AfterCaseLabel`` Wrap case labels. 1214 1215 .. code-block:: c++ 1216 1217 false: true: 1218 switch (foo) { vs. switch (foo) { 1219 case 1: { case 1: 1220 bar(); { 1221 break; bar(); 1222 } break; 1223 default: { } 1224 plop(); default: 1225 } { 1226 } plop(); 1227 } 1228 } 1229 1230 * ``bool AfterClass`` Wrap class definitions. 1231 1232 .. code-block:: c++ 1233 1234 true: 1235 class foo {}; 1236 1237 false: 1238 class foo 1239 {}; 1240 1241 * ``BraceWrappingAfterControlStatementStyle AfterControlStatement`` 1242 Wrap control statements (``if``/``for``/``while``/``switch``/..). 1243 1244 Possible values: 1245 1246 * ``BWACS_Never`` (in configuration: ``Never``) 1247 Never wrap braces after a control statement. 1248 1249 .. code-block:: c++ 1250 1251 if (foo()) { 1252 } else { 1253 } 1254 for (int i = 0; i < 10; ++i) { 1255 } 1256 1257 * ``BWACS_MultiLine`` (in configuration: ``MultiLine``) 1258 Only wrap braces after a multi-line control statement. 1259 1260 .. code-block:: c++ 1261 1262 if (foo && bar && 1263 baz) 1264 { 1265 quux(); 1266 } 1267 while (foo || bar) { 1268 } 1269 1270 * ``BWACS_Always`` (in configuration: ``Always``) 1271 Always wrap braces after a control statement. 1272 1273 .. code-block:: c++ 1274 1275 if (foo()) 1276 { 1277 } else 1278 {} 1279 for (int i = 0; i < 10; ++i) 1280 {} 1281 1282 1283 * ``bool AfterEnum`` Wrap enum definitions. 1284 1285 .. code-block:: c++ 1286 1287 true: 1288 enum X : int 1289 { 1290 B 1291 }; 1292 1293 false: 1294 enum X : int { B }; 1295 1296 * ``bool AfterFunction`` Wrap function definitions. 1297 1298 .. code-block:: c++ 1299 1300 true: 1301 void foo() 1302 { 1303 bar(); 1304 bar2(); 1305 } 1306 1307 false: 1308 void foo() { 1309 bar(); 1310 bar2(); 1311 } 1312 1313 * ``bool AfterNamespace`` Wrap namespace definitions. 1314 1315 .. code-block:: c++ 1316 1317 true: 1318 namespace 1319 { 1320 int foo(); 1321 int bar(); 1322 } 1323 1324 false: 1325 namespace { 1326 int foo(); 1327 int bar(); 1328 } 1329 1330 * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (interfaces, implementations...). 1331 @autoreleasepool and @synchronized blocks are wrapped 1332 according to `AfterControlStatement` flag. 1333 1334 * ``bool AfterStruct`` Wrap struct definitions. 1335 1336 .. code-block:: c++ 1337 1338 true: 1339 struct foo 1340 { 1341 int x; 1342 }; 1343 1344 false: 1345 struct foo { 1346 int x; 1347 }; 1348 1349 * ``bool AfterUnion`` Wrap union definitions. 1350 1351 .. code-block:: c++ 1352 1353 true: 1354 union foo 1355 { 1356 int x; 1357 } 1358 1359 false: 1360 union foo { 1361 int x; 1362 } 1363 1364 * ``bool AfterExternBlock`` Wrap extern blocks. 1365 1366 .. code-block:: c++ 1367 1368 true: 1369 extern "C" 1370 { 1371 int foo(); 1372 } 1373 1374 false: 1375 extern "C" { 1376 int foo(); 1377 } 1378 1379 * ``bool BeforeCatch`` Wrap before ``catch``. 1380 1381 .. code-block:: c++ 1382 1383 true: 1384 try { 1385 foo(); 1386 } 1387 catch () { 1388 } 1389 1390 false: 1391 try { 1392 foo(); 1393 } catch () { 1394 } 1395 1396 * ``bool BeforeElse`` Wrap before ``else``. 1397 1398 .. code-block:: c++ 1399 1400 true: 1401 if (foo()) { 1402 } 1403 else { 1404 } 1405 1406 false: 1407 if (foo()) { 1408 } else { 1409 } 1410 1411 * ``bool BeforeLambdaBody`` Wrap lambda block. 1412 1413 .. code-block:: c++ 1414 1415 true: 1416 connect( 1417 []() 1418 { 1419 foo(); 1420 bar(); 1421 }); 1422 1423 false: 1424 connect([]() { 1425 foo(); 1426 bar(); 1427 }); 1428 1429 * ``bool BeforeWhile`` Wrap before ``while``. 1430 1431 .. code-block:: c++ 1432 1433 true: 1434 do { 1435 foo(); 1436 } 1437 while (1); 1438 1439 false: 1440 do { 1441 foo(); 1442 } while (1); 1443 1444 * ``bool IndentBraces`` Indent the wrapped braces themselves. 1445 1446 * ``bool SplitEmptyFunction`` If ``false``, empty function body can be put on a single line. 1447 This option is used only if the opening brace of the function has 1448 already been wrapped, i.e. the `AfterFunction` brace wrapping mode is 1449 set, and the function could/should not be put on a single line (as per 1450 `AllowShortFunctionsOnASingleLine` and constructor formatting options). 1451 1452 .. code-block:: c++ 1453 1454 int f() vs. int f() 1455 {} { 1456 } 1457 1458 * ``bool SplitEmptyRecord`` If ``false``, empty record (e.g. class, struct or union) body 1459 can be put on a single line. This option is used only if the opening 1460 brace of the record has already been wrapped, i.e. the `AfterClass` 1461 (for classes) brace wrapping mode is set. 1462 1463 .. code-block:: c++ 1464 1465 class Foo vs. class Foo 1466 {} { 1467 } 1468 1469 * ``bool SplitEmptyNamespace`` If ``false``, empty namespace body can be put on a single line. 1470 This option is used only if the opening brace of the namespace has 1471 already been wrapped, i.e. the `AfterNamespace` brace wrapping mode is 1472 set. 1473 1474 .. code-block:: c++ 1475 1476 namespace Foo vs. namespace Foo 1477 {} { 1478 } 1479 1480 1481**BreakAfterJavaFieldAnnotations** (``Boolean``) :versionbadge:`clang-format 3.8` 1482 Break after each annotation on a field in Java files. 1483 1484 .. code-block:: java 1485 1486 true: false: 1487 @Partial vs. @Partial @Mock DataLoad loader; 1488 @Mock 1489 DataLoad loader; 1490 1491**BreakBeforeBinaryOperators** (``BinaryOperatorStyle``) :versionbadge:`clang-format 3.6` 1492 The way to wrap binary operators. 1493 1494 Possible values: 1495 1496 * ``BOS_None`` (in configuration: ``None``) 1497 Break after operators. 1498 1499 .. code-block:: c++ 1500 1501 LooooooooooongType loooooooooooooooooooooongVariable = 1502 someLooooooooooooooooongFunction(); 1503 1504 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + 1505 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == 1506 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && 1507 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa > 1508 ccccccccccccccccccccccccccccccccccccccccc; 1509 1510 * ``BOS_NonAssignment`` (in configuration: ``NonAssignment``) 1511 Break before operators that aren't assignments. 1512 1513 .. code-block:: c++ 1514 1515 LooooooooooongType loooooooooooooooooooooongVariable = 1516 someLooooooooooooooooongFunction(); 1517 1518 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1519 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1520 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1521 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1522 > ccccccccccccccccccccccccccccccccccccccccc; 1523 1524 * ``BOS_All`` (in configuration: ``All``) 1525 Break before operators. 1526 1527 .. code-block:: c++ 1528 1529 LooooooooooongType loooooooooooooooooooooongVariable 1530 = someLooooooooooooooooongFunction(); 1531 1532 bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1533 + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1534 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1535 && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1536 > ccccccccccccccccccccccccccccccccccccccccc; 1537 1538 1539 1540**BreakBeforeBraces** (``BraceBreakingStyle``) :versionbadge:`clang-format 3.7` 1541 The brace breaking style to use. 1542 1543 Possible values: 1544 1545 * ``BS_Attach`` (in configuration: ``Attach``) 1546 Always attach braces to surrounding context. 1547 1548 .. code-block:: c++ 1549 1550 namespace N { 1551 enum E { 1552 E1, 1553 E2, 1554 }; 1555 1556 class C { 1557 public: 1558 C(); 1559 }; 1560 1561 bool baz(int i) { 1562 try { 1563 do { 1564 switch (i) { 1565 case 1: { 1566 foobar(); 1567 break; 1568 } 1569 default: { 1570 break; 1571 } 1572 } 1573 } while (--i); 1574 return true; 1575 } catch (...) { 1576 handleError(); 1577 return false; 1578 } 1579 } 1580 1581 void foo(bool b) { 1582 if (b) { 1583 baz(2); 1584 } else { 1585 baz(5); 1586 } 1587 } 1588 1589 void bar() { foo(true); } 1590 } // namespace N 1591 1592 * ``BS_Linux`` (in configuration: ``Linux``) 1593 Like ``Attach``, but break before braces on function, namespace and 1594 class definitions. 1595 1596 .. code-block:: c++ 1597 1598 namespace N 1599 { 1600 enum E { 1601 E1, 1602 E2, 1603 }; 1604 1605 class C 1606 { 1607 public: 1608 C(); 1609 }; 1610 1611 bool baz(int i) 1612 { 1613 try { 1614 do { 1615 switch (i) { 1616 case 1: { 1617 foobar(); 1618 break; 1619 } 1620 default: { 1621 break; 1622 } 1623 } 1624 } while (--i); 1625 return true; 1626 } catch (...) { 1627 handleError(); 1628 return false; 1629 } 1630 } 1631 1632 void foo(bool b) 1633 { 1634 if (b) { 1635 baz(2); 1636 } else { 1637 baz(5); 1638 } 1639 } 1640 1641 void bar() { foo(true); } 1642 } // namespace N 1643 1644 * ``BS_Mozilla`` (in configuration: ``Mozilla``) 1645 Like ``Attach``, but break before braces on enum, function, and record 1646 definitions. 1647 1648 .. code-block:: c++ 1649 1650 namespace N { 1651 enum E 1652 { 1653 E1, 1654 E2, 1655 }; 1656 1657 class C 1658 { 1659 public: 1660 C(); 1661 }; 1662 1663 bool baz(int i) 1664 { 1665 try { 1666 do { 1667 switch (i) { 1668 case 1: { 1669 foobar(); 1670 break; 1671 } 1672 default: { 1673 break; 1674 } 1675 } 1676 } while (--i); 1677 return true; 1678 } catch (...) { 1679 handleError(); 1680 return false; 1681 } 1682 } 1683 1684 void foo(bool b) 1685 { 1686 if (b) { 1687 baz(2); 1688 } else { 1689 baz(5); 1690 } 1691 } 1692 1693 void bar() { foo(true); } 1694 } // namespace N 1695 1696 * ``BS_Stroustrup`` (in configuration: ``Stroustrup``) 1697 Like ``Attach``, but break before function definitions, ``catch``, and 1698 ``else``. 1699 1700 .. code-block:: c++ 1701 1702 namespace N { 1703 enum E { 1704 E1, 1705 E2, 1706 }; 1707 1708 class C { 1709 public: 1710 C(); 1711 }; 1712 1713 bool baz(int i) 1714 { 1715 try { 1716 do { 1717 switch (i) { 1718 case 1: { 1719 foobar(); 1720 break; 1721 } 1722 default: { 1723 break; 1724 } 1725 } 1726 } while (--i); 1727 return true; 1728 } 1729 catch (...) { 1730 handleError(); 1731 return false; 1732 } 1733 } 1734 1735 void foo(bool b) 1736 { 1737 if (b) { 1738 baz(2); 1739 } 1740 else { 1741 baz(5); 1742 } 1743 } 1744 1745 void bar() { foo(true); } 1746 } // namespace N 1747 1748 * ``BS_Allman`` (in configuration: ``Allman``) 1749 Always break before braces. 1750 1751 .. code-block:: c++ 1752 1753 namespace N 1754 { 1755 enum E 1756 { 1757 E1, 1758 E2, 1759 }; 1760 1761 class C 1762 { 1763 public: 1764 C(); 1765 }; 1766 1767 bool baz(int i) 1768 { 1769 try 1770 { 1771 do 1772 { 1773 switch (i) 1774 { 1775 case 1: 1776 { 1777 foobar(); 1778 break; 1779 } 1780 default: 1781 { 1782 break; 1783 } 1784 } 1785 } while (--i); 1786 return true; 1787 } 1788 catch (...) 1789 { 1790 handleError(); 1791 return false; 1792 } 1793 } 1794 1795 void foo(bool b) 1796 { 1797 if (b) 1798 { 1799 baz(2); 1800 } 1801 else 1802 { 1803 baz(5); 1804 } 1805 } 1806 1807 void bar() { foo(true); } 1808 } // namespace N 1809 1810 * ``BS_Whitesmiths`` (in configuration: ``Whitesmiths``) 1811 Like ``Allman`` but always indent braces and line up code with braces. 1812 1813 .. code-block:: c++ 1814 1815 namespace N 1816 { 1817 enum E 1818 { 1819 E1, 1820 E2, 1821 }; 1822 1823 class C 1824 { 1825 public: 1826 C(); 1827 }; 1828 1829 bool baz(int i) 1830 { 1831 try 1832 { 1833 do 1834 { 1835 switch (i) 1836 { 1837 case 1: 1838 { 1839 foobar(); 1840 break; 1841 } 1842 default: 1843 { 1844 break; 1845 } 1846 } 1847 } while (--i); 1848 return true; 1849 } 1850 catch (...) 1851 { 1852 handleError(); 1853 return false; 1854 } 1855 } 1856 1857 void foo(bool b) 1858 { 1859 if (b) 1860 { 1861 baz(2); 1862 } 1863 else 1864 { 1865 baz(5); 1866 } 1867 } 1868 1869 void bar() { foo(true); } 1870 } // namespace N 1871 1872 * ``BS_GNU`` (in configuration: ``GNU``) 1873 Always break before braces and add an extra level of indentation to 1874 braces of control statements, not to those of class, function 1875 or other definitions. 1876 1877 .. code-block:: c++ 1878 1879 namespace N 1880 { 1881 enum E 1882 { 1883 E1, 1884 E2, 1885 }; 1886 1887 class C 1888 { 1889 public: 1890 C(); 1891 }; 1892 1893 bool baz(int i) 1894 { 1895 try 1896 { 1897 do 1898 { 1899 switch (i) 1900 { 1901 case 1: 1902 { 1903 foobar(); 1904 break; 1905 } 1906 default: 1907 { 1908 break; 1909 } 1910 } 1911 } 1912 while (--i); 1913 return true; 1914 } 1915 catch (...) 1916 { 1917 handleError(); 1918 return false; 1919 } 1920 } 1921 1922 void foo(bool b) 1923 { 1924 if (b) 1925 { 1926 baz(2); 1927 } 1928 else 1929 { 1930 baz(5); 1931 } 1932 } 1933 1934 void bar() { foo(true); } 1935 } // namespace N 1936 1937 * ``BS_WebKit`` (in configuration: ``WebKit``) 1938 Like ``Attach``, but break before functions. 1939 1940 .. code-block:: c++ 1941 1942 namespace N { 1943 enum E { 1944 E1, 1945 E2, 1946 }; 1947 1948 class C { 1949 public: 1950 C(); 1951 }; 1952 1953 bool baz(int i) 1954 { 1955 try { 1956 do { 1957 switch (i) { 1958 case 1: { 1959 foobar(); 1960 break; 1961 } 1962 default: { 1963 break; 1964 } 1965 } 1966 } while (--i); 1967 return true; 1968 } catch (...) { 1969 handleError(); 1970 return false; 1971 } 1972 } 1973 1974 void foo(bool b) 1975 { 1976 if (b) { 1977 baz(2); 1978 } else { 1979 baz(5); 1980 } 1981 } 1982 1983 void bar() { foo(true); } 1984 } // namespace N 1985 1986 * ``BS_Custom`` (in configuration: ``Custom``) 1987 Configure each individual brace in `BraceWrapping`. 1988 1989 1990 1991**BreakBeforeConceptDeclarations** (``BreakBeforeConceptDeclarationsStyle``) :versionbadge:`clang-format 12` 1992 The concept declaration style to use. 1993 1994 Possible values: 1995 1996 * ``BBCDS_Never`` (in configuration: ``Never``) 1997 Keep the template declaration line together with ``concept``. 1998 1999 .. code-block:: c++ 2000 2001 template <typename T> concept C = ...; 2002 2003 * ``BBCDS_Allowed`` (in configuration: ``Allowed``) 2004 Breaking between template declaration and ``concept`` is allowed. The 2005 actual behavior depends on the content and line breaking rules and 2006 penalities. 2007 2008 * ``BBCDS_Always`` (in configuration: ``Always``) 2009 Always break before ``concept``, putting it in the line after the 2010 template declaration. 2011 2012 .. code-block:: c++ 2013 2014 template <typename T> 2015 concept C = ...; 2016 2017 2018 2019**BreakBeforeTernaryOperators** (``Boolean``) :versionbadge:`clang-format 3.7` 2020 If ``true``, ternary operators will be placed after line breaks. 2021 2022 .. code-block:: c++ 2023 2024 true: 2025 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription 2026 ? firstValue 2027 : SecondValueVeryVeryVeryVeryLong; 2028 2029 false: 2030 veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongDescription ? 2031 firstValue : 2032 SecondValueVeryVeryVeryVeryLong; 2033 2034**BreakConstructorInitializers** (``BreakConstructorInitializersStyle``) :versionbadge:`clang-format 5` 2035 The break constructor initializers style to use. 2036 2037 Possible values: 2038 2039 * ``BCIS_BeforeColon`` (in configuration: ``BeforeColon``) 2040 Break constructor initializers before the colon and after the commas. 2041 2042 .. code-block:: c++ 2043 2044 Constructor() 2045 : initializer1(), 2046 initializer2() 2047 2048 * ``BCIS_BeforeComma`` (in configuration: ``BeforeComma``) 2049 Break constructor initializers before the colon and commas, and align 2050 the commas with the colon. 2051 2052 .. code-block:: c++ 2053 2054 Constructor() 2055 : initializer1() 2056 , initializer2() 2057 2058 * ``BCIS_AfterColon`` (in configuration: ``AfterColon``) 2059 Break constructor initializers after the colon and commas. 2060 2061 .. code-block:: c++ 2062 2063 Constructor() : 2064 initializer1(), 2065 initializer2() 2066 2067 2068 2069**BreakInheritanceList** (``BreakInheritanceListStyle``) :versionbadge:`clang-format 7` 2070 The inheritance list style to use. 2071 2072 Possible values: 2073 2074 * ``BILS_BeforeColon`` (in configuration: ``BeforeColon``) 2075 Break inheritance list before the colon and after the commas. 2076 2077 .. code-block:: c++ 2078 2079 class Foo 2080 : Base1, 2081 Base2 2082 {}; 2083 2084 * ``BILS_BeforeComma`` (in configuration: ``BeforeComma``) 2085 Break inheritance list before the colon and commas, and align 2086 the commas with the colon. 2087 2088 .. code-block:: c++ 2089 2090 class Foo 2091 : Base1 2092 , Base2 2093 {}; 2094 2095 * ``BILS_AfterColon`` (in configuration: ``AfterColon``) 2096 Break inheritance list after the colon and commas. 2097 2098 .. code-block:: c++ 2099 2100 class Foo : 2101 Base1, 2102 Base2 2103 {}; 2104 2105 * ``BILS_AfterComma`` (in configuration: ``AfterComma``) 2106 Break inheritance list only after the commas. 2107 2108 .. code-block:: c++ 2109 2110 class Foo : Base1, 2111 Base2 2112 {}; 2113 2114 2115 2116**BreakStringLiterals** (``Boolean``) :versionbadge:`clang-format 3.9` 2117 Allow breaking string literals when formatting. 2118 2119 .. code-block:: c++ 2120 2121 true: 2122 const char* x = "veryVeryVeryVeryVeryVe" 2123 "ryVeryVeryVeryVeryVery" 2124 "VeryLongString"; 2125 2126 false: 2127 const char* x = 2128 "veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString"; 2129 2130**ColumnLimit** (``Unsigned``) :versionbadge:`clang-format 3.7` 2131 The column limit. 2132 2133 A column limit of ``0`` means that there is no column limit. In this case, 2134 clang-format will respect the input's line breaking decisions within 2135 statements unless they contradict other rules. 2136 2137**CommentPragmas** (``String``) :versionbadge:`clang-format 3.7` 2138 A regular expression that describes comments with special meaning, 2139 which should not be split into lines or otherwise changed. 2140 2141 .. code-block:: c++ 2142 2143 // CommentPragmas: '^ FOOBAR pragma:' 2144 // Will leave the following line unaffected 2145 #include <vector> // FOOBAR pragma: keep 2146 2147**CompactNamespaces** (``Boolean``) :versionbadge:`clang-format 5` 2148 If ``true``, consecutive namespace declarations will be on the same 2149 line. If ``false``, each namespace is declared on a new line. 2150 2151 .. code-block:: c++ 2152 2153 true: 2154 namespace Foo { namespace Bar { 2155 }} 2156 2157 false: 2158 namespace Foo { 2159 namespace Bar { 2160 } 2161 } 2162 2163 If it does not fit on a single line, the overflowing namespaces get 2164 wrapped: 2165 2166 .. code-block:: c++ 2167 2168 namespace Foo { namespace Bar { 2169 namespace Extra { 2170 }}} 2171 2172**ConstructorInitializerAllOnOneLineOrOnePerLine** (``Boolean``) :versionbadge:`clang-format 3.7` 2173 This option is **deprecated**. See ``CurrentLine`` of 2174 ``PackConstructorInitializers``. 2175 2176**ConstructorInitializerIndentWidth** (``Unsigned``) :versionbadge:`clang-format 3.7` 2177 The number of characters to use for indentation of constructor 2178 initializer lists as well as inheritance lists. 2179 2180**ContinuationIndentWidth** (``Unsigned``) :versionbadge:`clang-format 3.7` 2181 Indent width for line continuations. 2182 2183 .. code-block:: c++ 2184 2185 ContinuationIndentWidth: 2 2186 2187 int i = // VeryVeryVeryVeryVeryLongComment 2188 longFunction( // Again a long comment 2189 arg); 2190 2191**Cpp11BracedListStyle** (``Boolean``) :versionbadge:`clang-format 3.4` 2192 If ``true``, format braced lists as best suited for C++11 braced 2193 lists. 2194 2195 Important differences: 2196 - No spaces inside the braced list. 2197 - No line break before the closing brace. 2198 - Indentation with the continuation indent, not with the block indent. 2199 2200 Fundamentally, C++11 braced lists are formatted exactly like function 2201 calls would be formatted in their place. If the braced list follows a name 2202 (e.g. a type or variable name), clang-format formats as if the ``{}`` were 2203 the parentheses of a function call with that name. If there is no name, 2204 a zero-length name is assumed. 2205 2206 .. code-block:: c++ 2207 2208 true: false: 2209 vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 }; 2210 vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} }; 2211 f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]); 2212 new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 }; 2213 2214**DeriveLineEnding** (``Boolean``) :versionbadge:`clang-format 11` 2215 Analyze the formatted file for the most used line ending (``\r\n`` 2216 or ``\n``). ``UseCRLF`` is only used as a fallback if none can be derived. 2217 2218**DerivePointerAlignment** (``Boolean``) :versionbadge:`clang-format 3.7` 2219 If ``true``, analyze the formatted file for the most common 2220 alignment of ``&`` and ``*``. 2221 Pointer and reference alignment styles are going to be updated according 2222 to the preferences found in the file. 2223 ``PointerAlignment`` is then used only as fallback. 2224 2225**DisableFormat** (``Boolean``) :versionbadge:`clang-format 3.7` 2226 Disables formatting completely. 2227 2228**EmptyLineAfterAccessModifier** (``EmptyLineAfterAccessModifierStyle``) :versionbadge:`clang-format 13` 2229 Defines when to put an empty line after access modifiers. 2230 ``EmptyLineBeforeAccessModifier`` configuration handles the number of 2231 empty lines between two access modifiers. 2232 2233 Possible values: 2234 2235 * ``ELAAMS_Never`` (in configuration: ``Never``) 2236 Remove all empty lines after access modifiers. 2237 2238 .. code-block:: c++ 2239 2240 struct foo { 2241 private: 2242 int i; 2243 protected: 2244 int j; 2245 /* comment */ 2246 public: 2247 foo() {} 2248 private: 2249 protected: 2250 }; 2251 2252 * ``ELAAMS_Leave`` (in configuration: ``Leave``) 2253 Keep existing empty lines after access modifiers. 2254 MaxEmptyLinesToKeep is applied instead. 2255 2256 * ``ELAAMS_Always`` (in configuration: ``Always``) 2257 Always add empty line after access modifiers if there are none. 2258 MaxEmptyLinesToKeep is applied also. 2259 2260 .. code-block:: c++ 2261 2262 struct foo { 2263 private: 2264 2265 int i; 2266 protected: 2267 2268 int j; 2269 /* comment */ 2270 public: 2271 2272 foo() {} 2273 private: 2274 2275 protected: 2276 2277 }; 2278 2279 2280 2281**EmptyLineBeforeAccessModifier** (``EmptyLineBeforeAccessModifierStyle``) :versionbadge:`clang-format 12` 2282 Defines in which cases to put empty line before access modifiers. 2283 2284 Possible values: 2285 2286 * ``ELBAMS_Never`` (in configuration: ``Never``) 2287 Remove all empty lines before access modifiers. 2288 2289 .. code-block:: c++ 2290 2291 struct foo { 2292 private: 2293 int i; 2294 protected: 2295 int j; 2296 /* comment */ 2297 public: 2298 foo() {} 2299 private: 2300 protected: 2301 }; 2302 2303 * ``ELBAMS_Leave`` (in configuration: ``Leave``) 2304 Keep existing empty lines before access modifiers. 2305 2306 * ``ELBAMS_LogicalBlock`` (in configuration: ``LogicalBlock``) 2307 Add empty line only when access modifier starts a new logical block. 2308 Logical block is a group of one or more member fields or functions. 2309 2310 .. code-block:: c++ 2311 2312 struct foo { 2313 private: 2314 int i; 2315 2316 protected: 2317 int j; 2318 /* comment */ 2319 public: 2320 foo() {} 2321 2322 private: 2323 protected: 2324 }; 2325 2326 * ``ELBAMS_Always`` (in configuration: ``Always``) 2327 Always add empty line before access modifiers unless access modifier 2328 is at the start of struct or class definition. 2329 2330 .. code-block:: c++ 2331 2332 struct foo { 2333 private: 2334 int i; 2335 2336 protected: 2337 int j; 2338 /* comment */ 2339 2340 public: 2341 foo() {} 2342 2343 private: 2344 2345 protected: 2346 }; 2347 2348 2349 2350**ExperimentalAutoDetectBinPacking** (``Boolean``) :versionbadge:`clang-format 3.7` 2351 If ``true``, clang-format detects whether function calls and 2352 definitions are formatted with one parameter per line. 2353 2354 Each call can be bin-packed, one-per-line or inconclusive. If it is 2355 inconclusive, e.g. completely on one line, but a decision needs to be 2356 made, clang-format analyzes whether there are other bin-packed cases in 2357 the input file and act accordingly. 2358 2359 NOTE: This is an experimental flag, that might go away or be renamed. Do 2360 not use this in config files, etc. Use at your own risk. 2361 2362**FixNamespaceComments** (``Boolean``) :versionbadge:`clang-format 5` 2363 If ``true``, clang-format adds missing namespace end comments for 2364 short namespaces and fixes invalid existing ones. Short ones are 2365 controlled by "ShortNamespaceLines". 2366 2367 .. code-block:: c++ 2368 2369 true: false: 2370 namespace a { vs. namespace a { 2371 foo(); foo(); 2372 bar(); bar(); 2373 } // namespace a } 2374 2375**ForEachMacros** (``List of Strings``) :versionbadge:`clang-format 3.7` 2376 A vector of macros that should be interpreted as foreach loops 2377 instead of as function calls. 2378 2379 These are expected to be macros of the form: 2380 2381 .. code-block:: c++ 2382 2383 FOREACH(<variable-declaration>, ...) 2384 <loop-body> 2385 2386 In the .clang-format configuration file, this can be configured like: 2387 2388 .. code-block:: yaml 2389 2390 ForEachMacros: ['RANGES_FOR', 'FOREACH'] 2391 2392 For example: BOOST_FOREACH. 2393 2394**IfMacros** (``List of Strings``) :versionbadge:`clang-format 13` 2395 A vector of macros that should be interpreted as conditionals 2396 instead of as function calls. 2397 2398 These are expected to be macros of the form: 2399 2400 .. code-block:: c++ 2401 2402 IF(...) 2403 <conditional-body> 2404 else IF(...) 2405 <conditional-body> 2406 2407 In the .clang-format configuration file, this can be configured like: 2408 2409 .. code-block:: yaml 2410 2411 IfMacros: ['IF'] 2412 2413 For example: `KJ_IF_MAYBE 2414 <https://github.com/capnproto/capnproto/blob/master/kjdoc/tour.md#maybes>`_ 2415 2416**IncludeBlocks** (``IncludeBlocksStyle``) :versionbadge:`clang-format 7` 2417 Dependent on the value, multiple ``#include`` blocks can be sorted 2418 as one and divided based on category. 2419 2420 Possible values: 2421 2422 * ``IBS_Preserve`` (in configuration: ``Preserve``) 2423 Sort each ``#include`` block separately. 2424 2425 .. code-block:: c++ 2426 2427 #include "b.h" into #include "b.h" 2428 2429 #include <lib/main.h> #include "a.h" 2430 #include "a.h" #include <lib/main.h> 2431 2432 * ``IBS_Merge`` (in configuration: ``Merge``) 2433 Merge multiple ``#include`` blocks together and sort as one. 2434 2435 .. code-block:: c++ 2436 2437 #include "b.h" into #include "a.h" 2438 #include "b.h" 2439 #include <lib/main.h> #include <lib/main.h> 2440 #include "a.h" 2441 2442 * ``IBS_Regroup`` (in configuration: ``Regroup``) 2443 Merge multiple ``#include`` blocks together and sort as one. 2444 Then split into groups based on category priority. See 2445 ``IncludeCategories``. 2446 2447 .. code-block:: c++ 2448 2449 #include "b.h" into #include "a.h" 2450 #include "b.h" 2451 #include <lib/main.h> 2452 #include "a.h" #include <lib/main.h> 2453 2454 2455 2456**IncludeCategories** (``List of IncludeCategories``) :versionbadge:`clang-format 7` 2457 Regular expressions denoting the different ``#include`` categories 2458 used for ordering ``#includes``. 2459 2460 `POSIX extended 2461 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html>`_ 2462 regular expressions are supported. 2463 2464 These regular expressions are matched against the filename of an include 2465 (including the <> or "") in order. The value belonging to the first 2466 matching regular expression is assigned and ``#includes`` are sorted first 2467 according to increasing category number and then alphabetically within 2468 each category. 2469 2470 If none of the regular expressions match, INT_MAX is assigned as 2471 category. The main header for a source file automatically gets category 0. 2472 so that it is generally kept at the beginning of the ``#includes`` 2473 (https://llvm.org/docs/CodingStandards.html#include-style). However, you 2474 can also assign negative priorities if you have certain headers that 2475 always need to be first. 2476 2477 There is a third and optional field ``SortPriority`` which can used while 2478 ``IncludeBlocks = IBS_Regroup`` to define the priority in which 2479 ``#includes`` should be ordered. The value of ``Priority`` defines the 2480 order of ``#include blocks`` and also allows the grouping of ``#includes`` 2481 of different priority. ``SortPriority`` is set to the value of 2482 ``Priority`` as default if it is not assigned. 2483 2484 Each regular expression can be marked as case sensitive with the field 2485 ``CaseSensitive``, per default it is not. 2486 2487 To configure this in the .clang-format file, use: 2488 2489 .. code-block:: yaml 2490 2491 IncludeCategories: 2492 - Regex: '^"(llvm|llvm-c|clang|clang-c)/' 2493 Priority: 2 2494 SortPriority: 2 2495 CaseSensitive: true 2496 - Regex: '^((<|")(gtest|gmock|isl|json)/)' 2497 Priority: 3 2498 - Regex: '<[[:alnum:].]+>' 2499 Priority: 4 2500 - Regex: '.*' 2501 Priority: 1 2502 SortPriority: 0 2503 2504**IncludeIsMainRegex** (``String``) :versionbadge:`clang-format 7` 2505 Specify a regular expression of suffixes that are allowed in the 2506 file-to-main-include mapping. 2507 2508 When guessing whether a #include is the "main" include (to assign 2509 category 0, see above), use this regex of allowed suffixes to the header 2510 stem. A partial match is done, so that: 2511 - "" means "arbitrary suffix" 2512 - "$" means "no suffix" 2513 2514 For example, if configured to "(_test)?$", then a header a.h would be seen 2515 as the "main" include in both a.cc and a_test.cc. 2516 2517**IncludeIsMainSourceRegex** (``String``) :versionbadge:`clang-format 7` 2518 Specify a regular expression for files being formatted 2519 that are allowed to be considered "main" in the 2520 file-to-main-include mapping. 2521 2522 By default, clang-format considers files as "main" only when they end 2523 with: ``.c``, ``.cc``, ``.cpp``, ``.c++``, ``.cxx``, ``.m`` or ``.mm`` 2524 extensions. 2525 For these files a guessing of "main" include takes place 2526 (to assign category 0, see above). This config option allows for 2527 additional suffixes and extensions for files to be considered as "main". 2528 2529 For example, if this option is configured to ``(Impl\.hpp)$``, 2530 then a file ``ClassImpl.hpp`` is considered "main" (in addition to 2531 ``Class.c``, ``Class.cc``, ``Class.cpp`` and so on) and "main 2532 include file" logic will be executed (with *IncludeIsMainRegex* setting 2533 also being respected in later phase). Without this option set, 2534 ``ClassImpl.hpp`` would not have the main include file put on top 2535 before any other include. 2536 2537**IndentAccessModifiers** (``Boolean``) :versionbadge:`clang-format 13` 2538 Specify whether access modifiers should have their own indentation level. 2539 2540 When ``false``, access modifiers are indented (or outdented) relative to 2541 the record members, respecting the ``AccessModifierOffset``. Record 2542 members are indented one level below the record. 2543 When ``true``, access modifiers get their own indentation level. As a 2544 consequence, record members are always indented 2 levels below the record, 2545 regardless of the access modifier presence. Value of the 2546 ``AccessModifierOffset`` is ignored. 2547 2548 .. code-block:: c++ 2549 2550 false: true: 2551 class C { vs. class C { 2552 class D { class D { 2553 void bar(); void bar(); 2554 protected: protected: 2555 D(); D(); 2556 }; }; 2557 public: public: 2558 C(); C(); 2559 }; }; 2560 void foo() { void foo() { 2561 return 1; return 1; 2562 } } 2563 2564**IndentCaseBlocks** (``Boolean``) :versionbadge:`clang-format 11` 2565 Indent case label blocks one level from the case label. 2566 2567 When ``false``, the block following the case label uses the same 2568 indentation level as for the case label, treating the case label the same 2569 as an if-statement. 2570 When ``true``, the block gets indented as a scope block. 2571 2572 .. code-block:: c++ 2573 2574 false: true: 2575 switch (fool) { vs. switch (fool) { 2576 case 1: { case 1: 2577 bar(); { 2578 } break; bar(); 2579 default: { } 2580 plop(); break; 2581 } default: 2582 } { 2583 plop(); 2584 } 2585 } 2586 2587**IndentCaseLabels** (``Boolean``) :versionbadge:`clang-format 3.3` 2588 Indent case labels one level from the switch statement. 2589 2590 When ``false``, use the same indentation level as for the switch 2591 statement. Switch statement body is always indented one level more than 2592 case labels (except the first block following the case label, which 2593 itself indents the code - unless IndentCaseBlocks is enabled). 2594 2595 .. code-block:: c++ 2596 2597 false: true: 2598 switch (fool) { vs. switch (fool) { 2599 case 1: case 1: 2600 bar(); bar(); 2601 break; break; 2602 default: default: 2603 plop(); plop(); 2604 } } 2605 2606**IndentExternBlock** (``IndentExternBlockStyle``) :versionbadge:`clang-format 12` 2607 IndentExternBlockStyle is the type of indenting of extern blocks. 2608 2609 Possible values: 2610 2611 * ``IEBS_AfterExternBlock`` (in configuration: ``AfterExternBlock``) 2612 Backwards compatible with AfterExternBlock's indenting. 2613 2614 .. code-block:: c++ 2615 2616 IndentExternBlock: AfterExternBlock 2617 BraceWrapping.AfterExternBlock: true 2618 extern "C" 2619 { 2620 void foo(); 2621 } 2622 2623 2624 .. code-block:: c++ 2625 2626 IndentExternBlock: AfterExternBlock 2627 BraceWrapping.AfterExternBlock: false 2628 extern "C" { 2629 void foo(); 2630 } 2631 2632 * ``IEBS_NoIndent`` (in configuration: ``NoIndent``) 2633 Does not indent extern blocks. 2634 2635 .. code-block:: c++ 2636 2637 extern "C" { 2638 void foo(); 2639 } 2640 2641 * ``IEBS_Indent`` (in configuration: ``Indent``) 2642 Indents extern blocks. 2643 2644 .. code-block:: c++ 2645 2646 extern "C" { 2647 void foo(); 2648 } 2649 2650 2651 2652**IndentGotoLabels** (``Boolean``) :versionbadge:`clang-format 10` 2653 Indent goto labels. 2654 2655 When ``false``, goto labels are flushed left. 2656 2657 .. code-block:: c++ 2658 2659 true: false: 2660 int f() { vs. int f() { 2661 if (foo()) { if (foo()) { 2662 label1: label1: 2663 bar(); bar(); 2664 } } 2665 label2: label2: 2666 return 1; return 1; 2667 } } 2668 2669**IndentPPDirectives** (``PPDirectiveIndentStyle``) :versionbadge:`clang-format 6` 2670 The preprocessor directive indenting style to use. 2671 2672 Possible values: 2673 2674 * ``PPDIS_None`` (in configuration: ``None``) 2675 Does not indent any directives. 2676 2677 .. code-block:: c++ 2678 2679 #if FOO 2680 #if BAR 2681 #include <foo> 2682 #endif 2683 #endif 2684 2685 * ``PPDIS_AfterHash`` (in configuration: ``AfterHash``) 2686 Indents directives after the hash. 2687 2688 .. code-block:: c++ 2689 2690 #if FOO 2691 # if BAR 2692 # include <foo> 2693 # endif 2694 #endif 2695 2696 * ``PPDIS_BeforeHash`` (in configuration: ``BeforeHash``) 2697 Indents directives before the hash. 2698 2699 .. code-block:: c++ 2700 2701 #if FOO 2702 #if BAR 2703 #include <foo> 2704 #endif 2705 #endif 2706 2707 2708 2709**IndentRequiresClause** (``Boolean``) :versionbadge:`clang-format 15` 2710 Indent the requires clause in a template. This only applies when 2711 ``RequiresClausePosition`` is ``OwnLine``, or ``WithFollowing``. 2712 2713 In clang-format 12, 13 and 14 it was named ``IndentRequires``. 2714 2715 .. code-block:: c++ 2716 2717 true: 2718 template <typename It> 2719 requires Iterator<It> 2720 void sort(It begin, It end) { 2721 //.... 2722 } 2723 2724 false: 2725 template <typename It> 2726 requires Iterator<It> 2727 void sort(It begin, It end) { 2728 //.... 2729 } 2730 2731**IndentWidth** (``Unsigned``) :versionbadge:`clang-format 3.7` 2732 The number of columns to use for indentation. 2733 2734 .. code-block:: c++ 2735 2736 IndentWidth: 3 2737 2738 void f() { 2739 someFunction(); 2740 if (true, false) { 2741 f(); 2742 } 2743 } 2744 2745**IndentWrappedFunctionNames** (``Boolean``) :versionbadge:`clang-format 3.7` 2746 Indent if a function definition or declaration is wrapped after the 2747 type. 2748 2749 .. code-block:: c++ 2750 2751 true: 2752 LoooooooooooooooooooooooooooooooooooooooongReturnType 2753 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 2754 2755 false: 2756 LoooooooooooooooooooooooooooooooooooooooongReturnType 2757 LoooooooooooooooooooooooooooooooongFunctionDeclaration(); 2758 2759**InsertBraces** (``Boolean``) :versionbadge:`clang-format 15` 2760 Insert braces after control statements (``if``, ``else``, ``for``, ``do``, 2761 and ``while``) in C++ unless the control statements are inside macro 2762 definitions or the braces would enclose preprocessor directives. 2763 2764 .. warning:: 2765 2766 Setting this option to `true` could lead to incorrect code formatting due 2767 to clang-format's lack of complete semantic information. As such, extra 2768 care should be taken to review code changes made by this option. 2769 2770 .. code-block:: c++ 2771 2772 false: true: 2773 2774 if (isa<FunctionDecl>(D)) vs. if (isa<FunctionDecl>(D)) { 2775 handleFunctionDecl(D); handleFunctionDecl(D); 2776 else if (isa<VarDecl>(D)) } else if (isa<VarDecl>(D)) { 2777 handleVarDecl(D); handleVarDecl(D); 2778 else } else { 2779 return; return; 2780 } 2781 2782 while (i--) vs. while (i--) { 2783 for (auto *A : D.attrs()) for (auto *A : D.attrs()) { 2784 handleAttr(A); handleAttr(A); 2785 } 2786 } 2787 2788 do vs. do { 2789 --i; --i; 2790 while (i); } while (i); 2791 2792**InsertTrailingCommas** (``TrailingCommaStyle``) :versionbadge:`clang-format 12` 2793 If set to ``TCS_Wrapped`` will insert trailing commas in container 2794 literals (arrays and objects) that wrap across multiple lines. 2795 It is currently only available for JavaScript 2796 and disabled by default ``TCS_None``. 2797 ``InsertTrailingCommas`` cannot be used together with ``BinPackArguments`` 2798 as inserting the comma disables bin-packing. 2799 2800 .. code-block:: c++ 2801 2802 TSC_Wrapped: 2803 const someArray = [ 2804 aaaaaaaaaaaaaaaaaaaaaaaaaa, 2805 aaaaaaaaaaaaaaaaaaaaaaaaaa, 2806 aaaaaaaaaaaaaaaaaaaaaaaaaa, 2807 // ^ inserted 2808 ] 2809 2810 Possible values: 2811 2812 * ``TCS_None`` (in configuration: ``None``) 2813 Do not insert trailing commas. 2814 2815 * ``TCS_Wrapped`` (in configuration: ``Wrapped``) 2816 Insert trailing commas in container literals that were wrapped over 2817 multiple lines. Note that this is conceptually incompatible with 2818 bin-packing, because the trailing comma is used as an indicator 2819 that a container should be formatted one-per-line (i.e. not bin-packed). 2820 So inserting a trailing comma counteracts bin-packing. 2821 2822 2823 2824**JavaImportGroups** (``List of Strings``) :versionbadge:`clang-format 8` 2825 A vector of prefixes ordered by the desired groups for Java imports. 2826 2827 One group's prefix can be a subset of another - the longest prefix is 2828 always matched. Within a group, the imports are ordered lexicographically. 2829 Static imports are grouped separately and follow the same group rules. 2830 By default, static imports are placed before non-static imports, 2831 but this behavior is changed by another option, 2832 ``SortJavaStaticImport``. 2833 2834 In the .clang-format configuration file, this can be configured like 2835 in the following yaml example. This will result in imports being 2836 formatted as in the Java example below. 2837 2838 .. code-block:: yaml 2839 2840 JavaImportGroups: ['com.example', 'com', 'org'] 2841 2842 2843 .. code-block:: java 2844 2845 import static com.example.function1; 2846 2847 import static com.test.function2; 2848 2849 import static org.example.function3; 2850 2851 import com.example.ClassA; 2852 import com.example.Test; 2853 import com.example.a.ClassB; 2854 2855 import com.test.ClassC; 2856 2857 import org.example.ClassD; 2858 2859**JavaScriptQuotes** (``JavaScriptQuoteStyle``) :versionbadge:`clang-format 3.9` 2860 The JavaScriptQuoteStyle to use for JavaScript strings. 2861 2862 Possible values: 2863 2864 * ``JSQS_Leave`` (in configuration: ``Leave``) 2865 Leave string quotes as they are. 2866 2867 .. code-block:: js 2868 2869 string1 = "foo"; 2870 string2 = 'bar'; 2871 2872 * ``JSQS_Single`` (in configuration: ``Single``) 2873 Always use single quotes. 2874 2875 .. code-block:: js 2876 2877 string1 = 'foo'; 2878 string2 = 'bar'; 2879 2880 * ``JSQS_Double`` (in configuration: ``Double``) 2881 Always use double quotes. 2882 2883 .. code-block:: js 2884 2885 string1 = "foo"; 2886 string2 = "bar"; 2887 2888 2889 2890**JavaScriptWrapImports** (``Boolean``) :versionbadge:`clang-format 3.9` 2891 Whether to wrap JavaScript import/export statements. 2892 2893 .. code-block:: js 2894 2895 true: 2896 import { 2897 VeryLongImportsAreAnnoying, 2898 VeryLongImportsAreAnnoying, 2899 VeryLongImportsAreAnnoying, 2900 } from 'some/module.js' 2901 2902 false: 2903 import {VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying, VeryLongImportsAreAnnoying,} from "some/module.js" 2904 2905**KeepEmptyLinesAtTheStartOfBlocks** (``Boolean``) :versionbadge:`clang-format 3.7` 2906 If true, the empty line at the start of blocks is kept. 2907 2908 .. code-block:: c++ 2909 2910 true: false: 2911 if (foo) { vs. if (foo) { 2912 bar(); 2913 bar(); } 2914 } 2915 2916**LambdaBodyIndentation** (``LambdaBodyIndentationKind``) :versionbadge:`clang-format 13` 2917 The indentation style of lambda bodies. ``Signature`` (the default) 2918 causes the lambda body to be indented one additional level relative to 2919 the indentation level of the signature. ``OuterScope`` forces the lambda 2920 body to be indented one additional level relative to the parent scope 2921 containing the lambda signature. For callback-heavy code, it may improve 2922 readability to have the signature indented two levels and to use 2923 ``OuterScope``. The KJ style guide requires ``OuterScope``. 2924 `KJ style guide 2925 <https://github.com/capnproto/capnproto/blob/master/style-guide.md>`_ 2926 2927 Possible values: 2928 2929 * ``LBI_Signature`` (in configuration: ``Signature``) 2930 Align lambda body relative to the lambda signature. This is the default. 2931 2932 .. code-block:: c++ 2933 2934 someMethod( 2935 [](SomeReallyLongLambdaSignatureArgument foo) { 2936 return; 2937 }); 2938 2939 * ``LBI_OuterScope`` (in configuration: ``OuterScope``) 2940 Align lambda body relative to the indentation level of the outer scope 2941 the lambda signature resides in. 2942 2943 .. code-block:: c++ 2944 2945 someMethod( 2946 [](SomeReallyLongLambdaSignatureArgument foo) { 2947 return; 2948 }); 2949 2950 2951 2952**Language** (``LanguageKind``) :versionbadge:`clang-format 3.5` 2953 Language, this format style is targeted at. 2954 2955 Possible values: 2956 2957 * ``LK_None`` (in configuration: ``None``) 2958 Do not use. 2959 2960 * ``LK_Cpp`` (in configuration: ``Cpp``) 2961 Should be used for C, C++. 2962 2963 * ``LK_CSharp`` (in configuration: ``CSharp``) 2964 Should be used for C#. 2965 2966 * ``LK_Java`` (in configuration: ``Java``) 2967 Should be used for Java. 2968 2969 * ``LK_JavaScript`` (in configuration: ``JavaScript``) 2970 Should be used for JavaScript. 2971 2972 * ``LK_Json`` (in configuration: ``Json``) 2973 Should be used for JSON. 2974 2975 * ``LK_ObjC`` (in configuration: ``ObjC``) 2976 Should be used for Objective-C, Objective-C++. 2977 2978 * ``LK_Proto`` (in configuration: ``Proto``) 2979 Should be used for Protocol Buffers 2980 (https://developers.google.com/protocol-buffers/). 2981 2982 * ``LK_TableGen`` (in configuration: ``TableGen``) 2983 Should be used for TableGen code. 2984 2985 * ``LK_TextProto`` (in configuration: ``TextProto``) 2986 Should be used for Protocol Buffer messages in text format 2987 (https://developers.google.com/protocol-buffers/). 2988 2989 2990 2991**MacroBlockBegin** (``String``) :versionbadge:`clang-format 3.7` 2992 A regular expression matching macros that start a block. 2993 2994 .. code-block:: c++ 2995 2996 # With: 2997 MacroBlockBegin: "^NS_MAP_BEGIN|\ 2998 NS_TABLE_HEAD$" 2999 MacroBlockEnd: "^\ 3000 NS_MAP_END|\ 3001 NS_TABLE_.*_END$" 3002 3003 NS_MAP_BEGIN 3004 foo(); 3005 NS_MAP_END 3006 3007 NS_TABLE_HEAD 3008 bar(); 3009 NS_TABLE_FOO_END 3010 3011 # Without: 3012 NS_MAP_BEGIN 3013 foo(); 3014 NS_MAP_END 3015 3016 NS_TABLE_HEAD 3017 bar(); 3018 NS_TABLE_FOO_END 3019 3020**MacroBlockEnd** (``String``) :versionbadge:`clang-format 3.7` 3021 A regular expression matching macros that end a block. 3022 3023**MaxEmptyLinesToKeep** (``Unsigned``) :versionbadge:`clang-format 3.7` 3024 The maximum number of consecutive empty lines to keep. 3025 3026 .. code-block:: c++ 3027 3028 MaxEmptyLinesToKeep: 1 vs. MaxEmptyLinesToKeep: 0 3029 int f() { int f() { 3030 int = 1; int i = 1; 3031 i = foo(); 3032 i = foo(); return i; 3033 } 3034 return i; 3035 } 3036 3037**NamespaceIndentation** (``NamespaceIndentationKind``) :versionbadge:`clang-format 3.7` 3038 The indentation used for namespaces. 3039 3040 Possible values: 3041 3042 * ``NI_None`` (in configuration: ``None``) 3043 Don't indent in namespaces. 3044 3045 .. code-block:: c++ 3046 3047 namespace out { 3048 int i; 3049 namespace in { 3050 int i; 3051 } 3052 } 3053 3054 * ``NI_Inner`` (in configuration: ``Inner``) 3055 Indent only in inner namespaces (nested in other namespaces). 3056 3057 .. code-block:: c++ 3058 3059 namespace out { 3060 int i; 3061 namespace in { 3062 int i; 3063 } 3064 } 3065 3066 * ``NI_All`` (in configuration: ``All``) 3067 Indent in all namespaces. 3068 3069 .. code-block:: c++ 3070 3071 namespace out { 3072 int i; 3073 namespace in { 3074 int i; 3075 } 3076 } 3077 3078 3079 3080**NamespaceMacros** (``List of Strings``) :versionbadge:`clang-format 9` 3081 A vector of macros which are used to open namespace blocks. 3082 3083 These are expected to be macros of the form: 3084 3085 .. code-block:: c++ 3086 3087 NAMESPACE(<namespace-name>, ...) { 3088 <namespace-content> 3089 } 3090 3091 For example: TESTSUITE 3092 3093**ObjCBinPackProtocolList** (``BinPackStyle``) :versionbadge:`clang-format 7` 3094 Controls bin-packing Objective-C protocol conformance list 3095 items into as few lines as possible when they go over ``ColumnLimit``. 3096 3097 If ``Auto`` (the default), delegates to the value in 3098 ``BinPackParameters``. If that is ``true``, bin-packs Objective-C 3099 protocol conformance list items into as few lines as possible 3100 whenever they go over ``ColumnLimit``. 3101 3102 If ``Always``, always bin-packs Objective-C protocol conformance 3103 list items into as few lines as possible whenever they go over 3104 ``ColumnLimit``. 3105 3106 If ``Never``, lays out Objective-C protocol conformance list items 3107 onto individual lines whenever they go over ``ColumnLimit``. 3108 3109 3110 .. code-block:: objc 3111 3112 Always (or Auto, if BinPackParameters=true): 3113 @interface ccccccccccccc () < 3114 ccccccccccccc, ccccccccccccc, 3115 ccccccccccccc, ccccccccccccc> { 3116 } 3117 3118 Never (or Auto, if BinPackParameters=false): 3119 @interface ddddddddddddd () < 3120 ddddddddddddd, 3121 ddddddddddddd, 3122 ddddddddddddd, 3123 ddddddddddddd> { 3124 } 3125 3126 Possible values: 3127 3128 * ``BPS_Auto`` (in configuration: ``Auto``) 3129 Automatically determine parameter bin-packing behavior. 3130 3131 * ``BPS_Always`` (in configuration: ``Always``) 3132 Always bin-pack parameters. 3133 3134 * ``BPS_Never`` (in configuration: ``Never``) 3135 Never bin-pack parameters. 3136 3137 3138 3139**ObjCBlockIndentWidth** (``Unsigned``) :versionbadge:`clang-format 3.7` 3140 The number of characters to use for indentation of ObjC blocks. 3141 3142 .. code-block:: objc 3143 3144 ObjCBlockIndentWidth: 4 3145 3146 [operation setCompletionBlock:^{ 3147 [self onOperationDone]; 3148 }]; 3149 3150**ObjCBreakBeforeNestedBlockParam** (``Boolean``) :versionbadge:`clang-format 12` 3151 Break parameters list into lines when there is nested block 3152 parameters in a function call. 3153 3154 .. code-block:: c++ 3155 3156 false: 3157 - (void)_aMethod 3158 { 3159 [self.test1 t:self w:self callback:^(typeof(self) self, NSNumber 3160 *u, NSNumber *v) { 3161 u = c; 3162 }] 3163 } 3164 true: 3165 - (void)_aMethod 3166 { 3167 [self.test1 t:self 3168 w:self 3169 callback:^(typeof(self) self, NSNumber *u, NSNumber *v) { 3170 u = c; 3171 }] 3172 } 3173 3174**ObjCSpaceAfterProperty** (``Boolean``) :versionbadge:`clang-format 3.7` 3175 Add a space after ``@property`` in Objective-C, i.e. use 3176 ``@property (readonly)`` instead of ``@property(readonly)``. 3177 3178**ObjCSpaceBeforeProtocolList** (``Boolean``) :versionbadge:`clang-format 3.7` 3179 Add a space in front of an Objective-C protocol list, i.e. use 3180 ``Foo <Protocol>`` instead of ``Foo<Protocol>``. 3181 3182**PPIndentWidth** (``Integer``) :versionbadge:`clang-format 13` 3183 The number of columns to use for indentation of preprocessor statements. 3184 When set to -1 (default) ``IndentWidth`` is used also for preprocessor 3185 statements. 3186 3187 .. code-block:: c++ 3188 3189 PPIndentWidth: 1 3190 3191 #ifdef __linux__ 3192 # define FOO 3193 #else 3194 # define BAR 3195 #endif 3196 3197**PackConstructorInitializers** (``PackConstructorInitializersStyle``) :versionbadge:`clang-format 14` 3198 The pack constructor initializers style to use. 3199 3200 Possible values: 3201 3202 * ``PCIS_Never`` (in configuration: ``Never``) 3203 Always put each constructor initializer on its own line. 3204 3205 .. code-block:: c++ 3206 3207 Constructor() 3208 : a(), 3209 b() 3210 3211 * ``PCIS_BinPack`` (in configuration: ``BinPack``) 3212 Bin-pack constructor initializers. 3213 3214 .. code-block:: c++ 3215 3216 Constructor() 3217 : aaaaaaaaaaaaaaaaaaaa(), bbbbbbbbbbbbbbbbbbbb(), 3218 cccccccccccccccccccc() 3219 3220 * ``PCIS_CurrentLine`` (in configuration: ``CurrentLine``) 3221 Put all constructor initializers on the current line if they fit. 3222 Otherwise, put each one on its own line. 3223 3224 .. code-block:: c++ 3225 3226 Constructor() : a(), b() 3227 3228 Constructor() 3229 : aaaaaaaaaaaaaaaaaaaa(), 3230 bbbbbbbbbbbbbbbbbbbb(), 3231 ddddddddddddd() 3232 3233 * ``PCIS_NextLine`` (in configuration: ``NextLine``) 3234 Same as ``PCIS_CurrentLine`` except that if all constructor initializers 3235 do not fit on the current line, try to fit them on the next line. 3236 3237 .. code-block:: c++ 3238 3239 Constructor() : a(), b() 3240 3241 Constructor() 3242 : aaaaaaaaaaaaaaaaaaaa(), bbbbbbbbbbbbbbbbbbbb(), ddddddddddddd() 3243 3244 Constructor() 3245 : aaaaaaaaaaaaaaaaaaaa(), 3246 bbbbbbbbbbbbbbbbbbbb(), 3247 cccccccccccccccccccc() 3248 3249 3250 3251**PenaltyBreakAssignment** (``Unsigned``) :versionbadge:`clang-format 5` 3252 The penalty for breaking around an assignment operator. 3253 3254**PenaltyBreakBeforeFirstCallParameter** (``Unsigned``) :versionbadge:`clang-format 3.7` 3255 The penalty for breaking a function call after ``call(``. 3256 3257**PenaltyBreakComment** (``Unsigned``) :versionbadge:`clang-format 3.7` 3258 The penalty for each line break introduced inside a comment. 3259 3260**PenaltyBreakFirstLessLess** (``Unsigned``) :versionbadge:`clang-format 3.7` 3261 The penalty for breaking before the first ``<<``. 3262 3263**PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` 3264 The penalty for breaking after ``(``. 3265 3266**PenaltyBreakString** (``Unsigned``) :versionbadge:`clang-format 3.7` 3267 The penalty for each line break introduced inside a string literal. 3268 3269**PenaltyBreakTemplateDeclaration** (``Unsigned``) :versionbadge:`clang-format 7` 3270 The penalty for breaking after template declaration. 3271 3272**PenaltyExcessCharacter** (``Unsigned``) :versionbadge:`clang-format 3.7` 3273 The penalty for each character outside of the column limit. 3274 3275**PenaltyIndentedWhitespace** (``Unsigned``) :versionbadge:`clang-format 12` 3276 Penalty for each character of whitespace indentation 3277 (counted relative to leading non-whitespace column). 3278 3279**PenaltyReturnTypeOnItsOwnLine** (``Unsigned``) :versionbadge:`clang-format 3.7` 3280 Penalty for putting the return type of a function onto its own 3281 line. 3282 3283**PointerAlignment** (``PointerAlignmentStyle``) :versionbadge:`clang-format 3.7` 3284 Pointer and reference alignment style. 3285 3286 Possible values: 3287 3288 * ``PAS_Left`` (in configuration: ``Left``) 3289 Align pointer to the left. 3290 3291 .. code-block:: c++ 3292 3293 int* a; 3294 3295 * ``PAS_Right`` (in configuration: ``Right``) 3296 Align pointer to the right. 3297 3298 .. code-block:: c++ 3299 3300 int *a; 3301 3302 * ``PAS_Middle`` (in configuration: ``Middle``) 3303 Align pointer in the middle. 3304 3305 .. code-block:: c++ 3306 3307 int * a; 3308 3309 3310 3311**QualifierAlignment** (``QualifierAlignmentStyle``) :versionbadge:`clang-format 14` 3312 Different ways to arrange specifiers and qualifiers (e.g. const/volatile). 3313 3314 .. warning:: 3315 3316 Setting ``QualifierAlignment`` to something other than `Leave`, COULD 3317 lead to incorrect code formatting due to incorrect decisions made due to 3318 clang-formats lack of complete semantic information. 3319 As such extra care should be taken to review code changes made by the use 3320 of this option. 3321 3322 Possible values: 3323 3324 * ``QAS_Leave`` (in configuration: ``Leave``) 3325 Don't change specifiers/qualifiers to either Left or Right alignment 3326 (default). 3327 3328 .. code-block:: c++ 3329 3330 int const a; 3331 const int *a; 3332 3333 * ``QAS_Left`` (in configuration: ``Left``) 3334 Change specifiers/qualifiers to be left-aligned. 3335 3336 .. code-block:: c++ 3337 3338 const int a; 3339 const int *a; 3340 3341 * ``QAS_Right`` (in configuration: ``Right``) 3342 Change specifiers/qualifiers to be right-aligned. 3343 3344 .. code-block:: c++ 3345 3346 int const a; 3347 int const *a; 3348 3349 * ``QAS_Custom`` (in configuration: ``Custom``) 3350 Change specifiers/qualifiers to be aligned based on ``QualifierOrder``. 3351 With: 3352 3353 .. code-block:: yaml 3354 3355 QualifierOrder: ['inline', 'static' , 'type', 'const'] 3356 3357 3358 .. code-block:: c++ 3359 3360 3361 int const a; 3362 int const *a; 3363 3364 3365 3366**QualifierOrder** (``List of Strings``) :versionbadge:`clang-format 14` 3367 The order in which the qualifiers appear. 3368 Order is an array that can contain any of the following: 3369 3370 * const 3371 * inline 3372 * static 3373 * constexpr 3374 * volatile 3375 * restrict 3376 * type 3377 3378 Note: it MUST contain 'type'. 3379 Items to the left of 'type' will be placed to the left of the type and 3380 aligned in the order supplied. Items to the right of 'type' will be placed 3381 to the right of the type and aligned in the order supplied. 3382 3383 3384 .. code-block:: yaml 3385 3386 QualifierOrder: ['inline', 'static', 'type', 'const', 'volatile' ] 3387 3388**RawStringFormats** (``List of RawStringFormats``) :versionbadge:`clang-format 6` 3389 Defines hints for detecting supported languages code blocks in raw 3390 strings. 3391 3392 A raw string with a matching delimiter or a matching enclosing function 3393 name will be reformatted assuming the specified language based on the 3394 style for that language defined in the .clang-format file. If no style has 3395 been defined in the .clang-format file for the specific language, a 3396 predefined style given by 'BasedOnStyle' is used. If 'BasedOnStyle' is not 3397 found, the formatting is based on llvm style. A matching delimiter takes 3398 precedence over a matching enclosing function name for determining the 3399 language of the raw string contents. 3400 3401 If a canonical delimiter is specified, occurrences of other delimiters for 3402 the same language will be updated to the canonical if possible. 3403 3404 There should be at most one specification per language and each delimiter 3405 and enclosing function should not occur in multiple specifications. 3406 3407 To configure this in the .clang-format file, use: 3408 3409 .. code-block:: yaml 3410 3411 RawStringFormats: 3412 - Language: TextProto 3413 Delimiters: 3414 - 'pb' 3415 - 'proto' 3416 EnclosingFunctions: 3417 - 'PARSE_TEXT_PROTO' 3418 BasedOnStyle: google 3419 - Language: Cpp 3420 Delimiters: 3421 - 'cc' 3422 - 'cpp' 3423 BasedOnStyle: llvm 3424 CanonicalDelimiter: 'cc' 3425 3426**ReferenceAlignment** (``ReferenceAlignmentStyle``) :versionbadge:`clang-format 13` 3427 Reference alignment style (overrides ``PointerAlignment`` for 3428 references). 3429 3430 Possible values: 3431 3432 * ``RAS_Pointer`` (in configuration: ``Pointer``) 3433 Align reference like ``PointerAlignment``. 3434 3435 * ``RAS_Left`` (in configuration: ``Left``) 3436 Align reference to the left. 3437 3438 .. code-block:: c++ 3439 3440 int& a; 3441 3442 * ``RAS_Right`` (in configuration: ``Right``) 3443 Align reference to the right. 3444 3445 .. code-block:: c++ 3446 3447 int &a; 3448 3449 * ``RAS_Middle`` (in configuration: ``Middle``) 3450 Align reference in the middle. 3451 3452 .. code-block:: c++ 3453 3454 int & a; 3455 3456 3457 3458**ReflowComments** (``Boolean``) :versionbadge:`clang-format 4` 3459 If ``true``, clang-format will attempt to re-flow comments. 3460 3461 .. code-block:: c++ 3462 3463 false: 3464 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information 3465 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of information */ 3466 3467 true: 3468 // veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 3469 // information 3470 /* second veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongComment with plenty of 3471 * information */ 3472 3473**RemoveBracesLLVM** (``Boolean``) :versionbadge:`clang-format 14` 3474 Remove optional braces of control statements (``if``, ``else``, ``for``, 3475 and ``while``) in C++ according to the LLVM coding style. 3476 3477 .. warning:: 3478 3479 This option will be renamed and expanded to support other styles. 3480 3481 .. warning:: 3482 3483 Setting this option to `true` could lead to incorrect code formatting due 3484 to clang-format's lack of complete semantic information. As such, extra 3485 care should be taken to review code changes made by this option. 3486 3487 .. code-block:: c++ 3488 3489 false: true: 3490 3491 if (isa<FunctionDecl>(D)) { vs. if (isa<FunctionDecl>(D)) 3492 handleFunctionDecl(D); handleFunctionDecl(D); 3493 } else if (isa<VarDecl>(D)) { else if (isa<VarDecl>(D)) 3494 handleVarDecl(D); handleVarDecl(D); 3495 } 3496 3497 if (isa<VarDecl>(D)) { vs. if (isa<VarDecl>(D)) { 3498 for (auto *A : D.attrs()) { for (auto *A : D.attrs()) 3499 if (shouldProcessAttr(A)) { if (shouldProcessAttr(A)) 3500 handleAttr(A); handleAttr(A); 3501 } } 3502 } 3503 } 3504 3505 if (isa<FunctionDecl>(D)) { vs. if (isa<FunctionDecl>(D)) 3506 for (auto *A : D.attrs()) { for (auto *A : D.attrs()) 3507 handleAttr(A); handleAttr(A); 3508 } 3509 } 3510 3511 if (auto *D = (T)(D)) { vs. if (auto *D = (T)(D)) { 3512 if (shouldProcess(D)) { if (shouldProcess(D)) 3513 handleVarDecl(D); handleVarDecl(D); 3514 } else { else 3515 markAsIgnored(D); markAsIgnored(D); 3516 } } 3517 } 3518 3519 if (a) { vs. if (a) 3520 b(); b(); 3521 } else { else if (c) 3522 if (c) { d(); 3523 d(); else 3524 } else { e(); 3525 e(); 3526 } 3527 } 3528 3529**RequiresClausePosition** (``RequiresClausePositionStyle``) :versionbadge:`clang-format 15` 3530 The position of the ``requires`` clause. 3531 3532 Possible values: 3533 3534 * ``RCPS_OwnLine`` (in configuration: ``OwnLine``) 3535 Always put the ``requires`` clause on its own line. 3536 3537 .. code-block:: c++ 3538 3539 template <typename T> 3540 requires C<T> 3541 struct Foo {... 3542 3543 template <typename T> 3544 requires C<T> 3545 void bar(T t) {... 3546 3547 template <typename T> 3548 void baz(T t) 3549 requires C<T> 3550 {... 3551 3552 * ``RCPS_WithPreceding`` (in configuration: ``WithPreceding``) 3553 Try to put the clause together with the preceding part of a declaration. 3554 For class templates: stick to the template declaration. 3555 For function templates: stick to the template declaration. 3556 For function declaration followed by a requires clause: stick to the 3557 parameter list. 3558 3559 .. code-block:: c++ 3560 3561 template <typename T> requires C<T> 3562 struct Foo {... 3563 3564 template <typename T> requires C<T> 3565 void bar(T t) {... 3566 3567 template <typename T> 3568 void baz(T t) requires C<T> 3569 {... 3570 3571 * ``RCPS_WithFollowing`` (in configuration: ``WithFollowing``) 3572 Try to put the ``requires`` clause together with the class or function 3573 declaration. 3574 3575 .. code-block:: c++ 3576 3577 template <typename T> 3578 requires C<T> struct Foo {... 3579 3580 template <typename T> 3581 requires C<T> void bar(T t) {... 3582 3583 template <typename T> 3584 void baz(T t) 3585 requires C<T> {... 3586 3587 * ``RCPS_SingleLine`` (in configuration: ``SingleLine``) 3588 Try to put everything in the same line if possible. Otherwise normal 3589 line breaking rules take over. 3590 3591 .. code-block:: c++ 3592 3593 // Fitting: 3594 template <typename T> requires C<T> struct Foo {... 3595 3596 template <typename T> requires C<T> void bar(T t) {... 3597 3598 template <typename T> void bar(T t) requires C<T> {... 3599 3600 // Not fitting, one possible example: 3601 template <typename LongName> 3602 requires C<LongName> 3603 struct Foo {... 3604 3605 template <typename LongName> 3606 requires C<LongName> 3607 void bar(LongName ln) { 3608 3609 template <typename LongName> 3610 void bar(LongName ln) 3611 requires C<LongName> { 3612 3613 3614 3615**SeparateDefinitionBlocks** (``SeparateDefinitionStyle``) :versionbadge:`clang-format 14` 3616 Specifies the use of empty lines to separate definition blocks, including 3617 classes, structs, enums, and functions. 3618 3619 .. code-block:: c++ 3620 3621 Never v.s. Always 3622 #include <cstring> #include <cstring> 3623 struct Foo { 3624 int a, b, c; struct Foo { 3625 }; int a, b, c; 3626 namespace Ns { }; 3627 class Bar { 3628 public: namespace Ns { 3629 struct Foobar { class Bar { 3630 int a; public: 3631 int b; struct Foobar { 3632 }; int a; 3633 private: int b; 3634 int t; }; 3635 int method1() { 3636 // ... private: 3637 } int t; 3638 enum List { 3639 ITEM1, int method1() { 3640 ITEM2 // ... 3641 }; } 3642 template<typename T> 3643 int method2(T x) { enum List { 3644 // ... ITEM1, 3645 } ITEM2 3646 int i, j, k; }; 3647 int method3(int par) { 3648 // ... template<typename T> 3649 } int method2(T x) { 3650 }; // ... 3651 class C {}; } 3652 } 3653 int i, j, k; 3654 3655 int method3(int par) { 3656 // ... 3657 } 3658 }; 3659 3660 class C {}; 3661 } 3662 3663 Possible values: 3664 3665 * ``SDS_Leave`` (in configuration: ``Leave``) 3666 Leave definition blocks as they are. 3667 3668 * ``SDS_Always`` (in configuration: ``Always``) 3669 Insert an empty line between definition blocks. 3670 3671 * ``SDS_Never`` (in configuration: ``Never``) 3672 Remove any empty line between definition blocks. 3673 3674 3675 3676**ShortNamespaceLines** (``Unsigned``) :versionbadge:`clang-format 13` 3677 The maximal number of unwrapped lines that a short namespace spans. 3678 Defaults to 1. 3679 3680 This determines the maximum length of short namespaces by counting 3681 unwrapped lines (i.e. containing neither opening nor closing 3682 namespace brace) and makes "FixNamespaceComments" omit adding 3683 end comments for those. 3684 3685 .. code-block:: c++ 3686 3687 ShortNamespaceLines: 1 vs. ShortNamespaceLines: 0 3688 namespace a { namespace a { 3689 int foo; int foo; 3690 } } // namespace a 3691 3692 ShortNamespaceLines: 1 vs. ShortNamespaceLines: 0 3693 namespace b { namespace b { 3694 int foo; int foo; 3695 int bar; int bar; 3696 } // namespace b } // namespace b 3697 3698**SortIncludes** (``SortIncludesOptions``) :versionbadge:`clang-format 4` 3699 Controls if and how clang-format will sort ``#includes``. 3700 If ``Never``, includes are never sorted. 3701 If ``CaseInsensitive``, includes are sorted in an ASCIIbetical or case 3702 insensitive fashion. 3703 If ``CaseSensitive``, includes are sorted in an alphabetical or case 3704 sensitive fashion. 3705 3706 Possible values: 3707 3708 * ``SI_Never`` (in configuration: ``Never``) 3709 Includes are never sorted. 3710 3711 .. code-block:: c++ 3712 3713 #include "B/A.h" 3714 #include "A/B.h" 3715 #include "a/b.h" 3716 #include "A/b.h" 3717 #include "B/a.h" 3718 3719 * ``SI_CaseSensitive`` (in configuration: ``CaseSensitive``) 3720 Includes are sorted in an ASCIIbetical or case sensitive fashion. 3721 3722 .. code-block:: c++ 3723 3724 #include "A/B.h" 3725 #include "A/b.h" 3726 #include "B/A.h" 3727 #include "B/a.h" 3728 #include "a/b.h" 3729 3730 * ``SI_CaseInsensitive`` (in configuration: ``CaseInsensitive``) 3731 Includes are sorted in an alphabetical or case insensitive fashion. 3732 3733 .. code-block:: c++ 3734 3735 #include "A/B.h" 3736 #include "A/b.h" 3737 #include "a/b.h" 3738 #include "B/A.h" 3739 #include "B/a.h" 3740 3741 3742 3743**SortJavaStaticImport** (``SortJavaStaticImportOptions``) :versionbadge:`clang-format 12` 3744 When sorting Java imports, by default static imports are placed before 3745 non-static imports. If ``JavaStaticImportAfterImport`` is ``After``, 3746 static imports are placed after non-static imports. 3747 3748 Possible values: 3749 3750 * ``SJSIO_Before`` (in configuration: ``Before``) 3751 Static imports are placed before non-static imports. 3752 3753 .. code-block:: java 3754 3755 import static org.example.function1; 3756 3757 import org.example.ClassA; 3758 3759 * ``SJSIO_After`` (in configuration: ``After``) 3760 Static imports are placed after non-static imports. 3761 3762 .. code-block:: java 3763 3764 import org.example.ClassA; 3765 3766 import static org.example.function1; 3767 3768 3769 3770**SortUsingDeclarations** (``Boolean``) :versionbadge:`clang-format 5` 3771 If ``true``, clang-format will sort using declarations. 3772 3773 The order of using declarations is defined as follows: 3774 Split the strings by "::" and discard any initial empty strings. The last 3775 element of each list is a non-namespace name; all others are namespace 3776 names. Sort the lists of names lexicographically, where the sort order of 3777 individual names is that all non-namespace names come before all namespace 3778 names, and within those groups, names are in case-insensitive 3779 lexicographic order. 3780 3781 .. code-block:: c++ 3782 3783 false: true: 3784 using std::cout; vs. using std::cin; 3785 using std::cin; using std::cout; 3786 3787**SpaceAfterCStyleCast** (``Boolean``) :versionbadge:`clang-format 3.5` 3788 If ``true``, a space is inserted after C style casts. 3789 3790 .. code-block:: c++ 3791 3792 true: false: 3793 (int) i; vs. (int)i; 3794 3795**SpaceAfterLogicalNot** (``Boolean``) :versionbadge:`clang-format 9` 3796 If ``true``, a space is inserted after the logical not operator (``!``). 3797 3798 .. code-block:: c++ 3799 3800 true: false: 3801 ! someExpression(); vs. !someExpression(); 3802 3803**SpaceAfterTemplateKeyword** (``Boolean``) :versionbadge:`clang-format 4` 3804 If ``true``, a space will be inserted after the 'template' keyword. 3805 3806 .. code-block:: c++ 3807 3808 true: false: 3809 template <int> void foo(); vs. template<int> void foo(); 3810 3811**SpaceAroundPointerQualifiers** (``SpaceAroundPointerQualifiersStyle``) :versionbadge:`clang-format 12` 3812 Defines in which cases to put a space before or after pointer qualifiers 3813 3814 Possible values: 3815 3816 * ``SAPQ_Default`` (in configuration: ``Default``) 3817 Don't ensure spaces around pointer qualifiers and use PointerAlignment 3818 instead. 3819 3820 .. code-block:: c++ 3821 3822 PointerAlignment: Left PointerAlignment: Right 3823 void* const* x = NULL; vs. void *const *x = NULL; 3824 3825 * ``SAPQ_Before`` (in configuration: ``Before``) 3826 Ensure that there is a space before pointer qualifiers. 3827 3828 .. code-block:: c++ 3829 3830 PointerAlignment: Left PointerAlignment: Right 3831 void* const* x = NULL; vs. void * const *x = NULL; 3832 3833 * ``SAPQ_After`` (in configuration: ``After``) 3834 Ensure that there is a space after pointer qualifiers. 3835 3836 .. code-block:: c++ 3837 3838 PointerAlignment: Left PointerAlignment: Right 3839 void* const * x = NULL; vs. void *const *x = NULL; 3840 3841 * ``SAPQ_Both`` (in configuration: ``Both``) 3842 Ensure that there is a space both before and after pointer qualifiers. 3843 3844 .. code-block:: c++ 3845 3846 PointerAlignment: Left PointerAlignment: Right 3847 void* const * x = NULL; vs. void * const *x = NULL; 3848 3849 3850 3851**SpaceBeforeAssignmentOperators** (``Boolean``) :versionbadge:`clang-format 3.7` 3852 If ``false``, spaces will be removed before assignment operators. 3853 3854 .. code-block:: c++ 3855 3856 true: false: 3857 int a = 5; vs. int a= 5; 3858 a += 42; a+= 42; 3859 3860**SpaceBeforeCaseColon** (``Boolean``) :versionbadge:`clang-format 12` 3861 If ``false``, spaces will be removed before case colon. 3862 3863 .. code-block:: c++ 3864 3865 true: false 3866 switch (x) { vs. switch (x) { 3867 case 1 : break; case 1: break; 3868 } } 3869 3870**SpaceBeforeCpp11BracedList** (``Boolean``) :versionbadge:`clang-format 7` 3871 If ``true``, a space will be inserted before a C++11 braced list 3872 used to initialize an object (after the preceding identifier or type). 3873 3874 .. code-block:: c++ 3875 3876 true: false: 3877 Foo foo { bar }; vs. Foo foo{ bar }; 3878 Foo {}; Foo{}; 3879 vector<int> { 1, 2, 3 }; vector<int>{ 1, 2, 3 }; 3880 new int[3] { 1, 2, 3 }; new int[3]{ 1, 2, 3 }; 3881 3882**SpaceBeforeCtorInitializerColon** (``Boolean``) :versionbadge:`clang-format 7` 3883 If ``false``, spaces will be removed before constructor initializer 3884 colon. 3885 3886 .. code-block:: c++ 3887 3888 true: false: 3889 Foo::Foo() : a(a) {} Foo::Foo(): a(a) {} 3890 3891**SpaceBeforeInheritanceColon** (``Boolean``) :versionbadge:`clang-format 7` 3892 If ``false``, spaces will be removed before inheritance colon. 3893 3894 .. code-block:: c++ 3895 3896 true: false: 3897 class Foo : Bar {} vs. class Foo: Bar {} 3898 3899**SpaceBeforeParens** (``SpaceBeforeParensStyle``) :versionbadge:`clang-format 3.5` 3900 Defines in which cases to put a space before opening parentheses. 3901 3902 Possible values: 3903 3904 * ``SBPO_Never`` (in configuration: ``Never``) 3905 Never put a space before opening parentheses. 3906 3907 .. code-block:: c++ 3908 3909 void f() { 3910 if(true) { 3911 f(); 3912 } 3913 } 3914 3915 * ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``) 3916 Put a space before opening parentheses only after control statement 3917 keywords (``for/if/while...``). 3918 3919 .. code-block:: c++ 3920 3921 void f() { 3922 if (true) { 3923 f(); 3924 } 3925 } 3926 3927 * ``SBPO_ControlStatementsExceptControlMacros`` (in configuration: ``ControlStatementsExceptControlMacros``) 3928 Same as ``SBPO_ControlStatements`` except this option doesn't apply to 3929 ForEach and If macros. This is useful in projects where ForEach/If 3930 macros are treated as function calls instead of control statements. 3931 ``SBPO_ControlStatementsExceptForEachMacros`` remains an alias for 3932 backward compatibility. 3933 3934 .. code-block:: c++ 3935 3936 void f() { 3937 Q_FOREACH(...) { 3938 f(); 3939 } 3940 } 3941 3942 * ``SBPO_NonEmptyParentheses`` (in configuration: ``NonEmptyParentheses``) 3943 Put a space before opening parentheses only if the parentheses are not 3944 empty i.e. '()' 3945 3946 .. code-block:: c++ 3947 3948 void() { 3949 if (true) { 3950 f(); 3951 g (x, y, z); 3952 } 3953 } 3954 3955 * ``SBPO_Always`` (in configuration: ``Always``) 3956 Always put a space before opening parentheses, except when it's 3957 prohibited by the syntax rules (in function-like macro definitions) or 3958 when determined by other style rules (after unary operators, opening 3959 parentheses, etc.) 3960 3961 .. code-block:: c++ 3962 3963 void f () { 3964 if (true) { 3965 f (); 3966 } 3967 } 3968 3969 * ``SBPO_Custom`` (in configuration: ``Custom``) 3970 Configure each individual space before parentheses in 3971 `SpaceBeforeParensOptions`. 3972 3973 3974 3975**SpaceBeforeParensOptions** (``SpaceBeforeParensCustom``) :versionbadge:`clang-format 14` 3976 Control of individual space before parentheses. 3977 3978 If ``SpaceBeforeParens`` is set to ``Custom``, use this to specify 3979 how each individual space before parentheses case should be handled. 3980 Otherwise, this is ignored. 3981 3982 .. code-block:: yaml 3983 3984 # Example of usage: 3985 SpaceBeforeParens: Custom 3986 SpaceBeforeParensOptions: 3987 AfterControlStatements: true 3988 AfterFunctionDefinitionName: true 3989 3990 Nested configuration flags: 3991 3992 3993 * ``bool AfterControlStatements`` If ``true``, put space betwee control statement keywords 3994 (for/if/while...) and opening parentheses. 3995 3996 .. code-block:: c++ 3997 3998 true: false: 3999 if (...) {} vs. if(...) {} 4000 4001 * ``bool AfterForeachMacros`` If ``true``, put space between foreach macros and opening parentheses. 4002 4003 .. code-block:: c++ 4004 4005 true: false: 4006 FOREACH (...) vs. FOREACH(...) 4007 <loop-body> <loop-body> 4008 4009 * ``bool AfterFunctionDeclarationName`` If ``true``, put a space between function declaration name and opening 4010 parentheses. 4011 4012 .. code-block:: c++ 4013 4014 true: false: 4015 void f (); vs. void f(); 4016 4017 * ``bool AfterFunctionDefinitionName`` If ``true``, put a space between function definition name and opening 4018 parentheses. 4019 4020 .. code-block:: c++ 4021 4022 true: false: 4023 void f () {} vs. void f() {} 4024 4025 * ``bool AfterIfMacros`` If ``true``, put space between if macros and opening parentheses. 4026 4027 .. code-block:: c++ 4028 4029 true: false: 4030 IF (...) vs. IF(...) 4031 <conditional-body> <conditional-body> 4032 4033 * ``bool AfterOverloadedOperator`` If ``true``, put a space between operator overloading and opening 4034 parentheses. 4035 4036 .. code-block:: c++ 4037 4038 true: false: 4039 void operator++ (int a); vs. void operator++(int a); 4040 object.operator++ (10); object.operator++(10); 4041 4042 * ``bool AfterRequiresInClause`` If ``true``, put space between requires keyword in a requires clause and 4043 opening parentheses, if there is one. 4044 4045 .. code-block:: c++ 4046 4047 true: false: 4048 template<typename T> vs. template<typename T> 4049 requires (A<T> && B<T>) requires(A<T> && B<T>) 4050 ... ... 4051 4052 * ``bool AfterRequiresInExpression`` If ``true``, put space between requires keyword in a requires expression 4053 and opening parentheses. 4054 4055 .. code-block:: c++ 4056 4057 true: false: 4058 template<typename T> vs. template<typename T> 4059 concept C = requires (T t) { concept C = requires(T t) { 4060 ... ... 4061 } } 4062 4063 * ``bool BeforeNonEmptyParentheses`` If ``true``, put a space before opening parentheses only if the 4064 parentheses are not empty. 4065 4066 .. code-block:: c++ 4067 4068 true: false: 4069 void f (int a); vs. void f(); 4070 f (a); f(); 4071 4072 4073**SpaceBeforeRangeBasedForLoopColon** (``Boolean``) :versionbadge:`clang-format 7` 4074 If ``false``, spaces will be removed before range-based for loop 4075 colon. 4076 4077 .. code-block:: c++ 4078 4079 true: false: 4080 for (auto v : values) {} vs. for(auto v: values) {} 4081 4082**SpaceBeforeSquareBrackets** (``Boolean``) :versionbadge:`clang-format 11` 4083 If ``true``, spaces will be before ``[``. 4084 Lambdas will not be affected. Only the first ``[`` will get a space added. 4085 4086 .. code-block:: c++ 4087 4088 true: false: 4089 int a [5]; vs. int a[5]; 4090 int a [5][5]; vs. int a[5][5]; 4091 4092**SpaceInEmptyBlock** (``Boolean``) :versionbadge:`clang-format 11` 4093 If ``true``, spaces will be inserted into ``{}``. 4094 4095 .. code-block:: c++ 4096 4097 true: false: 4098 void f() { } vs. void f() {} 4099 while (true) { } while (true) {} 4100 4101**SpaceInEmptyParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` 4102 If ``true``, spaces may be inserted into ``()``. 4103 4104 .. code-block:: c++ 4105 4106 true: false: 4107 void f( ) { vs. void f() { 4108 int x[] = {foo( ), bar( )}; int x[] = {foo(), bar()}; 4109 if (true) { if (true) { 4110 f( ); f(); 4111 } } 4112 } } 4113 4114**SpacesBeforeTrailingComments** (``Unsigned``) :versionbadge:`clang-format 3.7` 4115 The number of spaces before trailing line comments 4116 (``//`` - comments). 4117 4118 This does not affect trailing block comments (``/*`` - comments) as 4119 those commonly have different usage patterns and a number of special 4120 cases. 4121 4122 .. code-block:: c++ 4123 4124 SpacesBeforeTrailingComments: 3 4125 void f() { 4126 if (true) { // foo1 4127 f(); // bar 4128 } // foo 4129 } 4130 4131**SpacesInAngles** (``SpacesInAnglesStyle``) :versionbadge:`clang-format 3.4` 4132 The SpacesInAnglesStyle to use for template argument lists. 4133 4134 Possible values: 4135 4136 * ``SIAS_Never`` (in configuration: ``Never``) 4137 Remove spaces after ``<`` and before ``>``. 4138 4139 .. code-block:: c++ 4140 4141 static_cast<int>(arg); 4142 std::function<void(int)> fct; 4143 4144 * ``SIAS_Always`` (in configuration: ``Always``) 4145 Add spaces after ``<`` and before ``>``. 4146 4147 .. code-block:: c++ 4148 4149 static_cast< int >(arg); 4150 std::function< void(int) > fct; 4151 4152 * ``SIAS_Leave`` (in configuration: ``Leave``) 4153 Keep a single space after ``<`` and before ``>`` if any spaces were 4154 present. Option ``Standard: Cpp03`` takes precedence. 4155 4156 4157 4158**SpacesInCStyleCastParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` 4159 If ``true``, spaces may be inserted into C style casts. 4160 4161 .. code-block:: c++ 4162 4163 true: false: 4164 x = ( int32 )y vs. x = (int32)y 4165 4166**SpacesInConditionalStatement** (``Boolean``) :versionbadge:`clang-format 11` 4167 If ``true``, spaces will be inserted around if/for/switch/while 4168 conditions. 4169 4170 .. code-block:: c++ 4171 4172 true: false: 4173 if ( a ) { ... } vs. if (a) { ... } 4174 while ( i < 5 ) { ... } while (i < 5) { ... } 4175 4176**SpacesInContainerLiterals** (``Boolean``) :versionbadge:`clang-format 3.7` 4177 If ``true``, spaces are inserted inside container literals (e.g. 4178 ObjC and Javascript array and dict literals). 4179 4180 .. code-block:: js 4181 4182 true: false: 4183 var arr = [ 1, 2, 3 ]; vs. var arr = [1, 2, 3]; 4184 f({a : 1, b : 2, c : 3}); f({a: 1, b: 2, c: 3}); 4185 4186**SpacesInLineCommentPrefix** (``SpacesInLineComment``) :versionbadge:`clang-format 13` 4187 How many spaces are allowed at the start of a line comment. To disable the 4188 maximum set it to ``-1``, apart from that the maximum takes precedence 4189 over the minimum. 4190 4191 .. code-block:: c++ 4192 4193 Minimum = 1 4194 Maximum = -1 4195 // One space is forced 4196 4197 // but more spaces are possible 4198 4199 Minimum = 0 4200 Maximum = 0 4201 //Forces to start every comment directly after the slashes 4202 4203 Note that in line comment sections the relative indent of the subsequent 4204 lines is kept, that means the following: 4205 4206 .. code-block:: c++ 4207 4208 before: after: 4209 Minimum: 1 4210 //if (b) { // if (b) { 4211 // return true; // return true; 4212 //} // } 4213 4214 Maximum: 0 4215 /// List: ///List: 4216 /// - Foo /// - Foo 4217 /// - Bar /// - Bar 4218 4219 Nested configuration flags: 4220 4221 4222 * ``unsigned Minimum`` The minimum number of spaces at the start of the comment. 4223 4224 * ``unsigned Maximum`` The maximum number of spaces at the start of the comment. 4225 4226 4227**SpacesInParentheses** (``Boolean``) :versionbadge:`clang-format 3.7` 4228 If ``true``, spaces will be inserted after ``(`` and before ``)``. 4229 4230 .. code-block:: c++ 4231 4232 true: false: 4233 t f( Deleted & ) & = delete; vs. t f(Deleted &) & = delete; 4234 4235**SpacesInSquareBrackets** (``Boolean``) :versionbadge:`clang-format 3.7` 4236 If ``true``, spaces will be inserted after ``[`` and before ``]``. 4237 Lambdas without arguments or unspecified size array declarations will not 4238 be affected. 4239 4240 .. code-block:: c++ 4241 4242 true: false: 4243 int a[ 5 ]; vs. int a[5]; 4244 std::unique_ptr<int[]> foo() {} // Won't be affected 4245 4246**Standard** (``LanguageStandard``) :versionbadge:`clang-format 3.7` 4247 Parse and format C++ constructs compatible with this standard. 4248 4249 .. code-block:: c++ 4250 4251 c++03: latest: 4252 vector<set<int> > x; vs. vector<set<int>> x; 4253 4254 Possible values: 4255 4256 * ``LS_Cpp03`` (in configuration: ``c++03``) 4257 Parse and format as C++03. 4258 ``Cpp03`` is a deprecated alias for ``c++03`` 4259 4260 * ``LS_Cpp11`` (in configuration: ``c++11``) 4261 Parse and format as C++11. 4262 4263 * ``LS_Cpp14`` (in configuration: ``c++14``) 4264 Parse and format as C++14. 4265 4266 * ``LS_Cpp17`` (in configuration: ``c++17``) 4267 Parse and format as C++17. 4268 4269 * ``LS_Cpp20`` (in configuration: ``c++20``) 4270 Parse and format as C++20. 4271 4272 * ``LS_Latest`` (in configuration: ``Latest``) 4273 Parse and format using the latest supported language version. 4274 ``Cpp11`` is a deprecated alias for ``Latest`` 4275 4276 * ``LS_Auto`` (in configuration: ``Auto``) 4277 Automatic detection based on the input. 4278 4279 4280 4281**StatementAttributeLikeMacros** (``List of Strings``) :versionbadge:`clang-format 12` 4282 Macros which are ignored in front of a statement, as if they were an 4283 attribute. So that they are not parsed as identifier, for example for Qts 4284 emit. 4285 4286 .. code-block:: c++ 4287 4288 AlignConsecutiveDeclarations: true 4289 StatementAttributeLikeMacros: [] 4290 unsigned char data = 'x'; 4291 emit signal(data); // This is parsed as variable declaration. 4292 4293 AlignConsecutiveDeclarations: true 4294 StatementAttributeLikeMacros: [emit] 4295 unsigned char data = 'x'; 4296 emit signal(data); // Now it's fine again. 4297 4298**StatementMacros** (``List of Strings``) :versionbadge:`clang-format 8` 4299 A vector of macros that should be interpreted as complete 4300 statements. 4301 4302 Typical macros are expressions, and require a semi-colon to be 4303 added; sometimes this is not the case, and this allows to make 4304 clang-format aware of such cases. 4305 4306 For example: Q_UNUSED 4307 4308**TabWidth** (``Unsigned``) :versionbadge:`clang-format 3.7` 4309 The number of columns used for tab stops. 4310 4311**TypenameMacros** (``List of Strings``) :versionbadge:`clang-format 9` 4312 A vector of macros that should be interpreted as type declarations 4313 instead of as function calls. 4314 4315 These are expected to be macros of the form: 4316 4317 .. code-block:: c++ 4318 4319 STACK_OF(...) 4320 4321 In the .clang-format configuration file, this can be configured like: 4322 4323 .. code-block:: yaml 4324 4325 TypenameMacros: ['STACK_OF', 'LIST'] 4326 4327 For example: OpenSSL STACK_OF, BSD LIST_ENTRY. 4328 4329**UseCRLF** (``Boolean``) :versionbadge:`clang-format 11` 4330 Use ``\r\n`` instead of ``\n`` for line breaks. 4331 Also used as fallback if ``DeriveLineEnding`` is true. 4332 4333**UseTab** (``UseTabStyle``) :versionbadge:`clang-format 3.7` 4334 The way to use tab characters in the resulting file. 4335 4336 Possible values: 4337 4338 * ``UT_Never`` (in configuration: ``Never``) 4339 Never use tab. 4340 4341 * ``UT_ForIndentation`` (in configuration: ``ForIndentation``) 4342 Use tabs only for indentation. 4343 4344 * ``UT_ForContinuationAndIndentation`` (in configuration: ``ForContinuationAndIndentation``) 4345 Fill all leading whitespace with tabs, and use spaces for alignment that 4346 appears within a line (e.g. consecutive assignments and declarations). 4347 4348 * ``UT_AlignWithSpaces`` (in configuration: ``AlignWithSpaces``) 4349 Use tabs for line continuation and indentation, and spaces for 4350 alignment. 4351 4352 * ``UT_Always`` (in configuration: ``Always``) 4353 Use tabs whenever we need to fill whitespace that spans at least from 4354 one tab stop to the next one. 4355 4356 4357 4358**WhitespaceSensitiveMacros** (``List of Strings``) :versionbadge:`clang-format 12` 4359 A vector of macros which are whitespace-sensitive and should not 4360 be touched. 4361 4362 These are expected to be macros of the form: 4363 4364 .. code-block:: c++ 4365 4366 STRINGIZE(...) 4367 4368 In the .clang-format configuration file, this can be configured like: 4369 4370 .. code-block:: yaml 4371 4372 WhitespaceSensitiveMacros: ['STRINGIZE', 'PP_STRINGIZE'] 4373 4374 For example: BOOST_PP_STRINGIZE 4375 4376.. END_FORMAT_STYLE_OPTIONS 4377 4378Adding additional style options 4379=============================== 4380 4381Each additional style option adds costs to the clang-format project. Some of 4382these costs affect the clang-format development itself, as we need to make 4383sure that any given combination of options work and that new features don't 4384break any of the existing options in any way. There are also costs for end users 4385as options become less discoverable and people have to think about and make a 4386decision on options they don't really care about. 4387 4388The goal of the clang-format project is more on the side of supporting a 4389limited set of styles really well as opposed to supporting every single style 4390used by a codebase somewhere in the wild. Of course, we do want to support all 4391major projects and thus have established the following bar for adding style 4392options. Each new style option must .. 4393 4394 * be used in a project of significant size (have dozens of contributors) 4395 * have a publicly accessible style guide 4396 * have a person willing to contribute and maintain patches 4397 4398Examples 4399======== 4400 4401A style similar to the `Linux Kernel style 4402<https://www.kernel.org/doc/Documentation/CodingStyle>`_: 4403 4404.. code-block:: yaml 4405 4406 BasedOnStyle: LLVM 4407 IndentWidth: 8 4408 UseTab: Always 4409 BreakBeforeBraces: Linux 4410 AllowShortIfStatementsOnASingleLine: false 4411 IndentCaseLabels: false 4412 4413The result is (imagine that tabs are used for indentation here): 4414 4415.. code-block:: c++ 4416 4417 void test() 4418 { 4419 switch (x) { 4420 case 0: 4421 case 1: 4422 do_something(); 4423 break; 4424 case 2: 4425 do_something_else(); 4426 break; 4427 default: 4428 break; 4429 } 4430 if (condition) 4431 do_something_completely_different(); 4432 4433 if (x == y) { 4434 q(); 4435 } else if (x > y) { 4436 w(); 4437 } else { 4438 r(); 4439 } 4440 } 4441 4442A style similar to the default Visual Studio formatting style: 4443 4444.. code-block:: yaml 4445 4446 UseTab: Never 4447 IndentWidth: 4 4448 BreakBeforeBraces: Allman 4449 AllowShortIfStatementsOnASingleLine: false 4450 IndentCaseLabels: false 4451 ColumnLimit: 0 4452 4453The result is: 4454 4455.. code-block:: c++ 4456 4457 void test() 4458 { 4459 switch (suffix) 4460 { 4461 case 0: 4462 case 1: 4463 do_something(); 4464 break; 4465 case 2: 4466 do_something_else(); 4467 break; 4468 default: 4469 break; 4470 } 4471 if (condition) 4472 do_something_completely_different(); 4473 4474 if (x == y) 4475 { 4476 q(); 4477 } 4478 else if (x > y) 4479 { 4480 w(); 4481 } 4482 else 4483 { 4484 r(); 4485 } 4486 } 4487