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