14#define DEBUG_TYPE "format-formatter"
21bool startsExternCBlock(
const AnnotatedLine &
Line) {
22 const FormatToken *Next =
Line.First->getNextNonComment();
23 const FormatToken *NextNext = Next ? Next->getNextNonComment() :
nullptr;
24 return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() &&
25 NextNext && NextNext->is(tok::l_brace);
28bool isRecordLBrace(
const FormatToken &Tok) {
29 return Tok.isOneOf(TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace,
30 TT_StructLBrace, TT_UnionLBrace);
42class LevelIndentTracker {
44 LevelIndentTracker(
const FormatStyle &Style,
45 const AdditionalKeywords &Keywords,
unsigned StartLevel,
47 : Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) {
48 for (
unsigned i = 0; i != StartLevel; ++i)
49 IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
53 unsigned getIndent()
const {
return Indent; }
57 void nextLine(
const AnnotatedLine &
Line) {
58 Offset = getIndentOffset(*
Line.First);
61 if (
Line.Level >= IndentForLevel.size())
62 IndentForLevel.resize(
Line.Level + 1, -1);
64 (
Line.InPPDirective ||
67 unsigned PPIndentWidth =
68 (Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth;
69 Indent =
Line.InMacroBody
70 ?
Line.PPLevel * PPIndentWidth +
71 (
Line.Level -
Line.PPLevel) * Style.IndentWidth
72 :
Line.Level * PPIndentWidth;
73 Indent += AdditionalIndent;
78 if (!
Line.InPPDirective) {
79 assert(
Line.Level <= IndentForLevel.size());
80 IndentForLevel.resize(
Line.Level + 1);
82 Indent = getIndent(
Line.Level);
84 if (
static_cast<int>(Indent) + Offset >= 0)
86 if (
Line.IsContinuation)
87 Indent =
Line.Level * Style.IndentWidth + Style.ContinuationIndentWidth;
95 void adjustToUnmodifiedLine(
const AnnotatedLine &
Line) {
96 if (
Line.InPPDirective ||
Line.IsContinuation)
98 assert(
Line.Level < IndentForLevel.size());
99 if (
Line.First->is(tok::comment) && IndentForLevel[
Line.Level] != -1)
101 unsigned LevelIndent =
Line.First->OriginalColumn;
102 if (
static_cast<int>(LevelIndent) - Offset >= 0)
103 LevelIndent -= Offset;
104 IndentForLevel[
Line.Level] = LevelIndent;
112 int getIndentOffset(
const FormatToken &RootToken) {
118 auto IsAccessModifier = [
this, &RootToken]() {
119 if (RootToken.isAccessSpecifier(Style.isCpp())) {
121 }
else if (RootToken.isObjCAccessSpecifier()) {
125 else if (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
126 RootToken.Next && RootToken.Next->is(tok::colon)) {
128 }
else if (RootToken.Next &&
129 RootToken.Next->isOneOf(Keywords.kw_slots,
130 Keywords.kw_qslots) &&
131 RootToken.Next->Next && RootToken.Next->Next->is(tok::colon)) {
135 else if (!RootToken.Next && RootToken.isAccessSpecifier(
false)) {
141 if (IsAccessModifier()) {
145 return Style.IndentAccessModifiers ? -Style.IndentWidth
146 : Style.AccessModifierOffset;
156 unsigned getIndent(
unsigned Level)
const {
157 assert(Level < IndentForLevel.size());
158 if (IndentForLevel[Level] != -1)
159 return IndentForLevel[Level];
162 return getIndent(Level - 1) + Style.IndentWidth;
165 const FormatStyle &Style;
166 const AdditionalKeywords &Keywords;
167 const unsigned AdditionalIndent;
173 SmallVector<int> IndentForLevel;
185const FormatToken *getMatchingNamespaceToken(
186 const AnnotatedLine *
Line,
187 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
188 if (!
Line->startsWith(tok::r_brace))
190 size_t StartLineIndex =
Line->MatchingOpeningBlockLineIndex;
193 assert(StartLineIndex < AnnotatedLines.size());
194 return AnnotatedLines[StartLineIndex]->First->getNamespaceToken();
198 const FormatToken *NamespaceToken =
Line->First->getNamespaceToken();
199 return NamespaceToken ? NamespaceToken->TokenText : StringRef();
202StringRef getMatchingNamespaceTokenText(
203 const AnnotatedLine *
Line,
204 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
205 const FormatToken *NamespaceToken =
206 getMatchingNamespaceToken(
Line, AnnotatedLines);
207 return NamespaceToken ? NamespaceToken->TokenText : StringRef();
212 LineJoiner(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
213 const SmallVectorImpl<AnnotatedLine *> &Lines)
214 : Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()),
215 AnnotatedLines(Lines) {}
218 const AnnotatedLine *getNextMergedLine(
bool DryRun,
219 LevelIndentTracker &IndentTracker) {
222 const AnnotatedLine *Current = *Next;
223 IndentTracker.nextLine(*Current);
224 unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End);
225 if (MergedLines > 0 && Style.ColumnLimit == 0) {
228 for (
unsigned i = 0; i < MergedLines; ++i)
229 if (Next[i + 1]->
First->NewlinesBefore > 0)
233 for (
unsigned i = 0; i < MergedLines; ++i)
234 join(*Next[0], *Next[i + 1]);
235 Next = Next + MergedLines + 1;
242 tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
243 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
244 SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
245 const unsigned Indent = IndentTracker.getIndent();
251 const AnnotatedLine *TheLine = *I;
252 if (TheLine->Last->is(TT_LineComment))
254 const auto &NextLine = *I[1];
255 if (NextLine.Type ==
LT_Invalid || NextLine.First->MustBreakBefore)
257 if (TheLine->InPPDirective &&
258 (!NextLine.InPPDirective || NextLine.First->HasUnescapedNewline)) {
262 if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
266 Style.ColumnLimit == 0 ?
UINT_MAX : Style.ColumnLimit - Indent;
269 Limit = TheLine->Last->TotalLength > Limit
271 : Limit - TheLine->Last->TotalLength;
273 if (TheLine->Last->is(TT_FunctionLBrace) &&
274 TheLine->First == TheLine->Last &&
275 !Style.BraceWrapping.SplitEmptyFunction &&
276 NextLine.First->is(tok::r_brace)) {
277 return tryMergeSimpleBlock(I, E, Limit);
280 const auto *PreviousLine = I != AnnotatedLines.begin() ? I[-1] :
nullptr;
282 if (PreviousLine && TheLine->Last->is(tok::l_brace) &&
283 TheLine->First == TheLine->Last) {
284 bool EmptyBlock = NextLine.First->is(tok::r_brace);
286 const FormatToken *Tok = PreviousLine->First;
287 if (Tok && Tok->is(tok::comment))
288 Tok = Tok->getNextNonComment();
290 if (Tok && Tok->getNamespaceToken()) {
291 return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
292 ? tryMergeSimpleBlock(I, E, Limit)
296 if (Tok && Tok->is(tok::kw_typedef))
297 Tok = Tok->getNextNonComment();
298 if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
299 tok::kw_extern, Keywords.kw_interface)) {
300 return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
301 ? tryMergeSimpleBlock(I, E, Limit)
305 if (Tok && Tok->is(tok::kw_template) &&
306 Style.BraceWrapping.SplitEmptyRecord && EmptyBlock) {
311 auto ShouldMergeShortFunctions = [
this, &I, &NextLine, PreviousLine,
316 NextLine.First->is(tok::r_brace)) {
320 if (Style.AllowShortFunctionsOnASingleLine &
324 if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace))
327 if (TheLine->Level != 0) {
333 const AnnotatedLine *
Line =
nullptr;
334 for (
auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
336 if (!(*J)->InPPDirective && !(*J)->isComment() &&
337 (*J)->Level < TheLine->Level) {
347 const auto *LastNonComment =
Line->getLastNonComment();
350 assert(LastNonComment);
351 return isRecordLBrace(*LastNonComment);
358 bool MergeShortFunctions = ShouldMergeShortFunctions();
360 const auto *FirstNonComment = TheLine->getFirstNonComment();
361 if (!FirstNonComment)
366 if (Style.CompactNamespaces) {
367 if (
const auto *NSToken = TheLine->First->getNamespaceToken()) {
369 assert(TheLine->MatchingClosingBlockLineIndex > 0);
370 for (
auto ClosingLineIndex = TheLine->MatchingClosingBlockLineIndex - 1;
372 ClosingLineIndex == I[J]->MatchingClosingBlockLineIndex &&
373 I[J]->
Last->TotalLength < Limit;
374 ++J, --ClosingLineIndex) {
375 Limit -= I[J]->Last->TotalLength;
379 auto *ClosingLine = AnnotatedLines.begin() + ClosingLineIndex + 1;
380 const int OutdentBy = I[J]->Level - TheLine->Level;
381 assert(OutdentBy >= 0);
382 for (
auto *CompactedLine = I + J; CompactedLine <= ClosingLine;
384 if (!(*CompactedLine)->InPPDirective) {
385 const int Level = (*CompactedLine)->Level;
386 (*CompactedLine)->Level = std::max(Level - OutdentBy, 0);
393 if (
auto nsToken = getMatchingNamespaceToken(TheLine, AnnotatedLines)) {
395 unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
396 for (; I + 1 + i != E &&
397 nsToken->TokenText ==
398 getMatchingNamespaceTokenText(I[i + 1], AnnotatedLines) &&
399 openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;
400 i++, --openingLine) {
402 I[i + 1]->First->SpacesRequiredBefore =
403 I[i]->Last->isNot(tok::r_brace);
406 IndentTracker.nextLine(*I[i + 1]);
412 const auto *LastNonComment = TheLine->getLastNonComment();
413 assert(LastNonComment);
418 if (LastNonComment->is(TT_FunctionLBrace) &&
419 TheLine->First != LastNonComment) {
420 return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
423 if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
424 FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
427 ? tryMergeSimpleBlock(I, E, Limit)
431 if (NextLine.First->is(tok::l_brace)) {
432 if ((TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
433 tok::kw_for, tok::kw_switch, tok::kw_try,
434 tok::kw_do, TT_ForEachMacro) ||
435 (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
436 TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
437 Style.BraceWrapping.AfterControlStatement ==
442 return (Style.ColumnLimit == 0 || TheLine->Level * Style.IndentWidth +
443 TheLine->Last->TotalLength <=
448 if (TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
449 tok::kw_for, TT_ForEachMacro)) {
450 return (Style.BraceWrapping.AfterControlStatement ==
452 ? tryMergeSimpleBlock(I, E, Limit)
455 if (TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&
456 Style.BraceWrapping.AfterControlStatement ==
463 return (Style.ColumnLimit == 0 ||
464 TheLine->Last->TotalLength <= Style.ColumnLimit)
469 if (PreviousLine && TheLine->First->is(tok::l_brace)) {
470 switch (PreviousLine->First->Tok.getKind()) {
473 if (PreviousLine->First->Next) {
475 PreviousLine->First->Next->Tok.getObjCKeywordID();
476 if (kwId == tok::objc_autoreleasepool ||
477 kwId == tok::objc_synchronized) {
484 case tok::kw_default:
495 if (PreviousLine && Style.BraceWrapping.SplitEmptyRecord &&
496 TheLine->Last->is(tok::l_brace) && PreviousLine->Last) {
497 const FormatToken *
Previous = PreviousLine->Last;
502 if (
Previous->is(tok::greater) && !PreviousLine->InPPDirective)
504 if (
Previous->is(tok::identifier)) {
505 const FormatToken *PreviousPrevious =
507 if (PreviousPrevious &&
508 PreviousPrevious->isOneOf(tok::kw_class, tok::kw_struct)) {
516 if (TheLine->Last->is(tok::l_brace)) {
517 bool ShouldMerge =
false;
519 if (TheLine->Last->is(TT_EnumLBrace)) {
520 ShouldMerge = Style.AllowShortEnumsOnASingleLine;
521 }
else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) {
522 ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
523 }
else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
527 ShouldMerge = !Style.BraceWrapping.AfterClass ||
528 (NextLine.First->is(tok::r_brace) &&
529 !Style.BraceWrapping.SplitEmptyRecord);
530 }
else if (TheLine->InPPDirective ||
531 !TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
535 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
536 (NextLine.First->is(tok::r_brace) &&
537 !Style.BraceWrapping.SplitEmptyFunction);
539 return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;
543 if (NextLine.First->is(TT_FunctionLBrace) &&
544 Style.BraceWrapping.AfterFunction) {
545 if (NextLine.Last->is(TT_LineComment))
549 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
553 unsigned MergedLines = 0;
554 if (MergeShortFunctions ||
556 NextLine.First == NextLine.Last && I + 2 != E &&
557 I[2]->First->is(tok::r_brace))) {
558 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
566 auto IsElseLine = [&TheLine]() ->
bool {
567 const FormatToken *
First = TheLine->First;
568 if (
First->is(tok::kw_else))
571 return First->is(tok::r_brace) &&
First->Next &&
572 First->Next->is(tok::kw_else);
574 if (TheLine->First->is(tok::kw_if) ||
575 (IsElseLine() && (Style.AllowShortIfStatementsOnASingleLine ==
577 return Style.AllowShortIfStatementsOnASingleLine
578 ? tryMergeSimpleControlStatement(I, E, Limit)
581 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do,
583 return Style.AllowShortLoopsOnASingleLine
584 ? tryMergeSimpleControlStatement(I, E, Limit)
587 if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
588 return Style.AllowShortCaseLabelsOnASingleLine
589 ? tryMergeShortCaseLabels(I, E, Limit)
592 if (TheLine->InPPDirective &&
593 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
594 return tryMergeSimplePPDirective(I, E, Limit);
600 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
601 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
605 if (I + 2 != E && I[2]->InPPDirective && !I[2]->
First->HasUnescapedNewline)
607 if (1 + I[1]->
Last->TotalLength > Limit)
612 unsigned tryMergeSimpleControlStatement(
613 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
614 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
unsigned Limit) {
617 if (Style.BraceWrapping.AfterControlStatement ==
619 I[1]->First->is(tok::l_brace) &&
623 if (I[1]->InPPDirective != (*I)->InPPDirective ||
624 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) {
627 Limit = limitConsideringMacros(I + 1, E, Limit);
628 AnnotatedLine &
Line = **I;
629 if (
Line.First->isNot(tok::kw_do) &&
Line.First->isNot(tok::kw_else) &&
630 Line.Last->isNot(tok::kw_else) &&
Line.Last->isNot(tok::r_paren)) {
634 if (
Line.First->is(tok::kw_do) &&
Line.Last->isNot(tok::kw_do))
636 if (1 + I[1]->
Last->TotalLength > Limit)
639 if (I[1]->
First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
640 TT_ForEachMacro, TT_LineComment)) {
644 if (Style.AllowShortIfStatementsOnASingleLine ==
646 if (I + 2 != E &&
Line.startsWith(tok::kw_if) &&
647 I[2]->First->is(tok::kw_else)) {
655 tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
656 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
658 if (Limit == 0 || I + 1 == E ||
659 I[1]->
First->isOneOf(tok::kw_case, tok::kw_default)) {
662 if (I[0]->
Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))
664 unsigned NumStmts = 0;
666 bool EndsWithComment =
false;
667 bool InPPDirective = I[0]->InPPDirective;
668 bool InMacroBody = I[0]->InMacroBody;
669 const unsigned Level = I[0]->Level;
670 for (; NumStmts < 3; ++NumStmts) {
671 if (I + 1 + NumStmts == E)
673 const AnnotatedLine *
Line = I[1 + NumStmts];
674 if (
Line->InPPDirective != InPPDirective)
676 if (
Line->InMacroBody != InMacroBody)
678 if (
Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
680 if (
Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
685 if (
Line->First->is(tok::comment)) {
686 if (Level !=
Line->Level)
688 SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts;
689 for (; J != E; ++J) {
691 if (
Line->InPPDirective != InPPDirective)
693 if (
Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
695 if (
Line->First->isNot(tok::comment) || Level !=
Line->Level)
700 if (
Line->Last->is(tok::comment))
701 EndsWithComment =
true;
702 Length += I[1 + NumStmts]->Last->TotalLength + 1;
704 if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
710 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
711 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
717 AnnotatedLine &
Line = **I;
723 Line.First->isOneOf(tok::at, tok::minus, tok::plus)) {
729 if (
Line.First->is(tok::kw_case) ||
730 (
Line.First->Next &&
Line.First->Next->is(tok::kw_else))) {
734 if (
Line.First->is(tok::kw_default)) {
735 const FormatToken *Tok =
Line.First->getNextNonComment();
736 if (Tok && Tok->is(tok::colon))
740 auto IsCtrlStmt = [](
const auto &
Line) {
741 return Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
742 tok::kw_do, tok::kw_for, TT_ForEachMacro);
745 const bool IsSplitBlock =
748 I[1]->First->isNot(tok::r_brace));
750 if (IsCtrlStmt(
Line) ||
751 Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
752 tok::kw___finally, tok::r_brace,
753 Keywords.kw___except)) {
758 if (!Style.AllowShortIfStatementsOnASingleLine &&
759 Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
760 !Style.BraceWrapping.AfterControlStatement &&
761 I[1]->First->isNot(tok::r_brace)) {
764 if (!Style.AllowShortIfStatementsOnASingleLine &&
765 Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
766 Style.BraceWrapping.AfterControlStatement ==
768 I + 2 != E && I[2]->First->isNot(tok::r_brace)) {
771 if (!Style.AllowShortLoopsOnASingleLine &&
772 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
774 !Style.BraceWrapping.AfterControlStatement &&
775 I[1]->First->isNot(tok::r_brace)) {
778 if (!Style.AllowShortLoopsOnASingleLine &&
779 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
781 Style.BraceWrapping.AfterControlStatement ==
783 I + 2 != E && I[2]->First->isNot(tok::r_brace)) {
791 if (
Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
792 Keywords.kw___except, tok::kw___finally)) {
797 if (
const auto *LastNonComment =
Line.getLastNonComment();
798 LastNonComment && LastNonComment->is(tok::l_brace)) {
799 if (IsSplitBlock &&
Line.First ==
Line.Last &&
800 I > AnnotatedLines.begin() &&
801 (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {
804 FormatToken *Tok = I[1]->First;
805 auto ShouldMerge = [Tok]() {
806 if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore)
808 const FormatToken *Next = Tok->getNextNonComment();
809 return !Next || Next->is(tok::semi);
814 Tok->SpacesRequiredBefore =
815 (Style.SpaceInEmptyBlock ||
Line.Last->is(tok::comment)) ? 1 : 0;
816 Tok->CanBreakBefore =
true;
818 }
else if (Limit != 0 && !
Line.startsWithNamespace() &&
819 !startsExternCBlock(
Line)) {
821 if (isRecordLBrace(*
Line.Last))
827 Limit = limitConsideringMacros(I + 2, E, Limit);
829 if (!nextTwoLinesFitInto(I, Limit))
834 if (I[1]->
Last->is(TT_LineComment))
844 if (Tok->isNot(tok::r_brace))
848 if (Tok->Next && Tok->Next->is(tok::kw_else))
857 if (
Line.First ==
Line.Last &&
Line.First->isNot(TT_FunctionLBrace) &&
858 Style.BraceWrapping.AfterControlStatement ==
865 }
else if (I[1]->
First->is(tok::l_brace)) {
866 if (I[1]->
Last->is(TT_LineComment))
870 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I)))
873 unsigned MergedLines = 0;
875 (I[1]->First == I[1]->Last && I + 2 != E &&
876 I[2]->First->is(tok::r_brace))) {
877 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
891 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
892 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
894 if (I[0]->InPPDirective && I + 1 != E &&
895 !I[1]->
First->HasUnescapedNewline && I[1]->First->isNot(tok::eof)) {
896 return Limit < 2 ? 0 : Limit - 2;
901 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
903 if (I[1]->
First->MustBreakBefore || I[2]->First->MustBreakBefore)
905 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
908 bool containsMustBreak(
const AnnotatedLine *
Line) {
912 for (
const FormatToken *Tok =
Line->First->Next; Tok; Tok = Tok->Next)
913 if (Tok->MustBreakBefore)
918 void join(AnnotatedLine &A,
const AnnotatedLine &B) {
919 assert(!A.Last->Next);
920 assert(!B.First->Previous);
923 A.Last->Next = B.First;
924 B.First->Previous = A.Last;
925 B.First->CanBreakBefore =
true;
926 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
927 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
928 Tok->TotalLength += LengthA;
933 const FormatStyle &Style;
934 const AdditionalKeywords &Keywords;
935 const SmallVectorImpl<AnnotatedLine *>::const_iterator End;
937 SmallVectorImpl<AnnotatedLine *>::const_iterator Next;
938 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines;
941static void markFinalized(FormatToken *Tok) {
942 if (Tok->is(tok::hash) && !Tok->Previous && Tok->Next &&
943 Tok->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,
944 tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,
945 tok::pp_else, tok::pp_endif)) {
948 for (; Tok; Tok = Tok->Next) {
961 Tok->SpacesRequiredBefore = 0;
962 if (!Tok->MustBreakBeforeFinalized)
963 Tok->MustBreakBefore = 0;
965 Tok->Finalized =
true;
971static void printLineState(
const LineState &State) {
972 llvm::dbgs() <<
"State: ";
973 for (
const ParenState &
P : State.Stack) {
974 llvm::dbgs() << (
P.Tok ?
P.Tok->TokenText :
"F") <<
"|" <<
P.Indent <<
"|"
975 <<
P.LastSpace <<
"|" <<
P.NestedBlockIndent <<
" ";
977 llvm::dbgs() << State.NextToken->TokenText <<
"\n";
984 LineFormatter(ContinuationIndenter *
Indenter, WhitespaceManager *Whitespaces,
985 const FormatStyle &Style,
986 UnwrappedLineFormatter *BlockFormatter)
988 BlockFormatter(BlockFormatter) {}
989 virtual ~LineFormatter() {}
994 virtual unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
995 unsigned FirstStartColumn,
bool DryRun) = 0;
1018 bool formatChildren(LineState &State,
bool NewLine,
bool DryRun,
1019 unsigned &Penalty) {
1020 const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
1021 bool HasLBrace = LBrace && LBrace->is(tok::l_brace) && LBrace->is(
BK_Block);
1022 FormatToken &
Previous = *State.NextToken->Previous;
1023 if (
Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent)) {
1030 const ParenState &
P = State.Stack.back();
1032 int AdditionalIndent =
1033 P.Indent -
Previous.Children[0]->Level * Style.IndentWidth;
1035 BlockFormatter->format(
Previous.Children, DryRun, AdditionalIndent,
1040 if (
Previous.Children[0]->First->MustBreakBefore)
1051 const AnnotatedLine *Child =
Previous.Children[0];
1053 if (Child->Last->isTrailingComment())
1058 if (Style.ColumnLimit > 0 &&
1059 Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) {
1064 Whitespaces->replaceWhitespace(
1065 *Child->First, 0, 1,
1066 State.Column,
false,
1067 State.Line->InPPDirective);
1070 formatLine(*Child, State.Column + 1, 0, DryRun);
1072 markFinalized(Child->First);
1074 State.Column += 1 + Child->Last->TotalLength;
1081 WhitespaceManager *Whitespaces;
1082 const FormatStyle &Style;
1083 UnwrappedLineFormatter *BlockFormatter;
1087class NoColumnLimitLineFormatter :
public LineFormatter {
1089 NoColumnLimitLineFormatter(ContinuationIndenter *
Indenter,
1090 WhitespaceManager *Whitespaces,
1091 const FormatStyle &Style,
1092 UnwrappedLineFormatter *BlockFormatter)
1093 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1097 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1098 unsigned FirstStartColumn,
bool DryRun)
override {
1100 LineState State =
Indenter->getInitialState(FirstIndent, FirstStartColumn,
1102 while (State.NextToken) {
1105 (
Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
1106 unsigned Penalty = 0;
1107 formatChildren(State, Newline,
false, Penalty);
1108 Indenter->addTokenToState(State, Newline,
false);
1115class NoLineBreakFormatter :
public LineFormatter {
1117 NoLineBreakFormatter(ContinuationIndenter *
Indenter,
1118 WhitespaceManager *Whitespaces,
const FormatStyle &Style,
1119 UnwrappedLineFormatter *BlockFormatter)
1120 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1123 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1124 unsigned FirstStartColumn,
bool DryRun)
override {
1125 unsigned Penalty = 0;
1127 Indenter->getInitialState(FirstIndent, FirstStartColumn, &
Line, DryRun);
1128 while (State.NextToken) {
1129 formatChildren(State,
false, DryRun, Penalty);
1131 State, State.NextToken->MustBreakBefore, DryRun);
1138class OptimizingLineFormatter :
public LineFormatter {
1140 OptimizingLineFormatter(ContinuationIndenter *
Indenter,
1141 WhitespaceManager *Whitespaces,
1142 const FormatStyle &Style,
1143 UnwrappedLineFormatter *BlockFormatter)
1144 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1148 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1149 unsigned FirstStartColumn,
bool DryRun)
override {
1151 Indenter->getInitialState(FirstIndent, FirstStartColumn, &
Line, DryRun);
1156 State.Stack.back().BreakBeforeParameter =
true;
1159 return analyzeSolutionSpace(State, DryRun);
1163 struct CompareLineStatePointers {
1164 bool operator()(LineState *obj1, LineState *obj2)
const {
1165 return *obj1 < *obj2;
1174 typedef std::pair<unsigned, unsigned> OrderedPenalty;
1179 StateNode(
const LineState &State,
bool NewLine, StateNode *Previous)
1188 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
1191 typedef std::priority_queue<QueueItem, SmallVector<QueueItem>,
1192 std::greater<QueueItem>>
1203 unsigned analyzeSolutionSpace(LineState &InitialState,
bool DryRun) {
1204 std::set<LineState *, CompareLineStatePointers> Seen;
1212 StateNode *RootNode =
1213 new (Allocator.Allocate()) StateNode(InitialState,
false,
nullptr);
1214 Queue.push(QueueItem(OrderedPenalty(0, Count), RootNode));
1217 unsigned Penalty = 0;
1220 while (!Queue.empty()) {
1222 if (Count > 25'000'000)
1225 Penalty = Queue.top().first.first;
1226 StateNode *
Node = Queue.top().second;
1227 if (!
Node->State.NextToken) {
1228 LLVM_DEBUG(llvm::dbgs()
1229 <<
"\n---\nPenalty for line: " << Penalty <<
"\n");
1237 Node->State.IgnoreStackForComparison =
true;
1239 if (!Seen.insert(&
Node->State).second) {
1246 addNextStateToQueue(Penalty,
Node,
false, &Count, &Queue);
1248 addNextStateToQueue(Penalty,
Node,
true, &Count, &Queue);
1251 if (Queue.empty()) {
1254 LLVM_DEBUG(llvm::dbgs() <<
"Could not find a solution.\n");
1260 reconstructPath(InitialState, Queue.top().second);
1262 LLVM_DEBUG(llvm::dbgs()
1263 <<
"Total number of analyzed states: " << Count <<
"\n");
1264 LLVM_DEBUG(llvm::dbgs() <<
"---\n");
1273 void addNextStateToQueue(
unsigned Penalty, StateNode *PreviousNode,
1274 bool NewLine,
unsigned *Count, QueueType *Queue) {
1280 StateNode *
Node =
new (Allocator.Allocate())
1281 StateNode(PreviousNode->State,
NewLine, PreviousNode);
1282 if (!formatChildren(
Node->State,
NewLine,
true, Penalty))
1287 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count),
Node));
1293 void reconstructPath(LineState &State, StateNode *Best) {
1296 while (Best->Previous) {
1297 Path.push_back(Best);
1298 Best = Best->Previous;
1300 for (
const auto &
Node : llvm::reverse(Path)) {
1301 unsigned Penalty = 0;
1302 formatChildren(State,
Node->NewLine,
false, Penalty);
1303 Penalty +=
Indenter->addTokenToState(State,
Node->NewLine,
false);
1306 printLineState(
Node->Previous->State);
1307 if (
Node->NewLine) {
1308 llvm::dbgs() <<
"Penalty for placing "
1309 <<
Node->Previous->State.NextToken->Tok.getName()
1310 <<
" on a new line: " << Penalty <<
"\n";
1316 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1323 int AdditionalIndent,
bool FixBadIndentation,
unsigned FirstStartColumn,
1324 unsigned NextStartColumn,
unsigned LastStartColumn) {
1325 LineJoiner Joiner(Style, Keywords, Lines);
1328 std::pair<const SmallVectorImpl<AnnotatedLine *> *,
unsigned> CacheKey(
1329 &Lines, AdditionalIndent);
1330 auto CacheIt = PenaltyCache.find(CacheKey);
1331 if (DryRun && CacheIt != PenaltyCache.end())
1332 return CacheIt->second;
1334 assert(!Lines.empty());
1335 unsigned Penalty = 0;
1336 LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
1345 bool FirstLine =
true;
1347 Joiner.getNextMergedLine(DryRun, IndentTracker);
1348 Line; PrevPrevLine = PreviousLine, PreviousLine =
Line,
Line = NextLine,
1349 FirstLine =
false) {
1350 assert(
Line->First);
1352 unsigned Indent = IndentTracker.getIndent();
1358 bool PreviousRBrace =
1359 PreviousLine && PreviousLine->
startsWith(tok::r_brace);
1360 bool ContinueFormatting =
1361 TheLine.
Level > RangeMinLevel ||
1362 (TheLine.
Level == RangeMinLevel && !PreviousRBrace &&
1365 bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
1367 bool ShouldFormat = TheLine.
Affected || FixIndentation;
1378 bool LastLine = TheLine.
First->
is(tok::eof);
1379 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,
1380 LastLine ? LastStartColumn : NextStartColumn + Indent);
1383 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1384 unsigned ColumnLimit = getColumnLimit(TheLine.
InPPDirective, NextLine);
1385 bool FitsIntoOneLine =
1389 (!Style.isJavaScript() || !Style.JavaScriptWrapImports)) ||
1390 (Style.isCSharp() &&
1392 if (Style.ColumnLimit == 0) {
1393 NoColumnLimitLineFormatter(
Indenter, Whitespaces, Style,
this)
1394 .formatLine(TheLine, NextStartColumn + Indent,
1395 FirstLine ? FirstStartColumn : 0, DryRun);
1396 }
else if (FitsIntoOneLine) {
1397 Penalty += NoLineBreakFormatter(
Indenter, Whitespaces, Style,
this)
1398 .formatLine(TheLine, NextStartColumn + Indent,
1399 FirstLine ? FirstStartColumn : 0, DryRun);
1401 Penalty += OptimizingLineFormatter(
Indenter, Whitespaces, Style,
this)
1402 .formatLine(TheLine, NextStartColumn + Indent,
1403 FirstLine ? FirstStartColumn : 0, DryRun);
1405 RangeMinLevel = std::min(RangeMinLevel, TheLine.
Level);
1411 if (!Tok->Children.empty())
1412 format(Tok->Children, DryRun);
1417 bool StartsNewLine =
1420 IndentTracker.adjustToUnmodifiedLine(TheLine);
1422 bool ReformatLeadingWhitespace =
1423 StartsNewLine && ((PreviousLine && PreviousLine->
Affected) ||
1426 if (ReformatLeadingWhitespace) {
1427 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines,
1431 Whitespaces->addUntouchableToken(*TheLine.
First,
1437 Whitespaces->addUntouchableToken(*Tok, TheLine.
InPPDirective);
1439 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1443 markFinalized(TheLine.
First);
1445 PenaltyCache[CacheKey] = Penalty;
1454 const auto &RootToken = *
Line.First;
1458 if (RootToken.is(tok::r_brace) &&
1460 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&
1463 Newlines = std::min(Newlines, 1u);
1466 if (!PreviousLine &&
Line.Level > 0)
1467 Newlines = std::min(Newlines, 1u);
1468 if (Newlines == 0 && !RootToken.IsFirst)
1470 if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
1475 PreviousLine->
Last->
is(tok::l_brace) &&
1479 !startsExternCBlock(*PreviousLine)) {
1484 if (PreviousLine && RootToken.isAccessSpecifier()) {
1491 Newlines = std::max(RootToken.NewlinesBefore, 1u);
1494 if (PreviousLine->
Last->
isOneOf(tok::semi, tok::r_brace) && Newlines <= 1)
1501 if (PreviousLine->
Last->
is(tok::comment))
1504 previousToken = PreviousLine->
Last;
1505 if ((!previousToken || previousToken->
isNot(tok::l_brace)) &&
1515 (!PreviousLine->
InPPDirective || !RootToken.HasUnescapedNewline)) {
1518 if (!RootToken.isAccessSpecifier()) {
1524 Newlines = std::max(Newlines, 1u);
1527 if (RootToken.is(tok::r_brace))
1530 Newlines = std::max(Newlines, 2u);
1539void UnwrappedLineFormatter::formatFirstToken(
1540 const AnnotatedLine &
Line,
const AnnotatedLine *PreviousLine,
1541 const AnnotatedLine *PrevPrevLine,
1543 unsigned NewlineIndent) {
1544 FormatToken &RootToken = *
Line.First;
1545 if (RootToken.is(tok::eof)) {
1547 std::min(RootToken.NewlinesBefore,
1548 Style.KeepEmptyLinesAtEOF ? Style.MaxEmptyLinesToKeep + 1 : 1);
1549 unsigned TokenIndent = Newlines ? NewlineIndent : 0;
1550 Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
1555 if (RootToken.Newlines < 0) {
1556 RootToken.Newlines =
1558 assert(RootToken.Newlines >= 0);
1561 if (RootToken.Newlines > 0)
1562 Indent = NewlineIndent;
1566 if (!Style.isJavaScript() &&
1573 Whitespaces->replaceWhitespace(RootToken, RootToken.Newlines, Indent, Indent,
1575 Line.InPPDirective &&
1576 !RootToken.HasUnescapedNewline);
1580UnwrappedLineFormatter::getColumnLimit(
bool InPPDirective,
1581 const AnnotatedLine *NextLine)
const {
1584 bool ContinuesPPDirective =
1589 (NextLine->InPPDirective &&
1592 !NextLine->First->HasUnescapedNewline));
1593 return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0);
WhitespaceManager class manages whitespace around tokens and their replacements.
unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid=nullptr) const
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
ObjCKeywordKind
Provides a namespace for Objective-C keywords which start with an '@'.
The JSON file list parser is used to communicate input to InstallAPI.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))