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