13#include "llvm/Support/Debug.h"
16#define DEBUG_TYPE "format-formatter"
23bool startsExternCBlock(
const AnnotatedLine &
Line) {
24 const FormatToken *Next =
Line.First->getNextNonComment();
25 const FormatToken *NextNext = Next ? Next->getNextNonComment() :
nullptr;
26 return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() &&
27 NextNext && NextNext->is(tok::l_brace);
30bool isRecordLBrace(
const FormatToken &Tok) {
31 return Tok.isOneOf(TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace,
32 TT_StructLBrace, TT_UnionLBrace);
44class LevelIndentTracker {
46 LevelIndentTracker(
const FormatStyle &Style,
47 const AdditionalKeywords &Keywords,
unsigned StartLevel,
49 : Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) {
50 for (
unsigned i = 0; i != StartLevel; ++i)
51 IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
55 unsigned getIndent()
const {
return Indent; }
59 void nextLine(
const AnnotatedLine &
Line) {
60 Offset = getIndentOffset(*
Line.First);
63 if (
Line.Level >= IndentForLevel.size())
64 IndentForLevel.resize(
Line.Level + 1, -1);
66 (
Line.InPPDirective ||
69 unsigned PPIndentWidth =
70 (Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth;
71 Indent =
Line.InMacroBody
72 ?
Line.PPLevel * PPIndentWidth +
73 (
Line.Level -
Line.PPLevel) * Style.IndentWidth
74 :
Line.Level * PPIndentWidth;
75 Indent += AdditionalIndent;
80 if (!
Line.InPPDirective) {
81 assert(
Line.Level <= IndentForLevel.size());
82 IndentForLevel.resize(
Line.Level + 1);
84 Indent = getIndent(
Line.Level);
86 if (
static_cast<int>(Indent) + Offset >= 0)
88 if (
Line.IsContinuation)
89 Indent =
Line.Level * Style.IndentWidth + Style.ContinuationIndentWidth;
97 void adjustToUnmodifiedLine(
const AnnotatedLine &
Line) {
98 if (
Line.InPPDirective)
100 assert(
Line.Level < IndentForLevel.size());
101 if (
Line.First->is(tok::comment) && IndentForLevel[
Line.Level] != -1)
103 unsigned LevelIndent =
Line.First->OriginalColumn;
104 if (
static_cast<int>(LevelIndent) - Offset >= 0)
105 LevelIndent -= Offset;
106 IndentForLevel[
Line.Level] = LevelIndent;
114 int getIndentOffset(
const FormatToken &RootToken) {
120 auto IsAccessModifier = [
this, &RootToken]() {
121 if (RootToken.isAccessSpecifier(Style.isCpp())) {
123 }
else if (RootToken.isObjCAccessSpecifier()) {
127 else if (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
128 RootToken.Next && RootToken.Next->is(tok::colon)) {
130 }
else if (RootToken.Next &&
131 RootToken.Next->isOneOf(Keywords.kw_slots,
132 Keywords.kw_qslots) &&
133 RootToken.Next->Next && RootToken.Next->Next->is(tok::colon)) {
137 else if (!RootToken.Next && RootToken.isAccessSpecifier(
false)) {
143 if (IsAccessModifier()) {
147 return Style.IndentAccessModifiers ? -Style.IndentWidth
148 : Style.AccessModifierOffset;
158 unsigned getIndent(
unsigned Level)
const {
159 assert(Level < IndentForLevel.size());
160 if (IndentForLevel[Level] != -1)
161 return IndentForLevel[Level];
164 return getIndent(Level - 1) + Style.IndentWidth;
167 const FormatStyle &Style;
168 const AdditionalKeywords &Keywords;
169 const unsigned AdditionalIndent;
175 SmallVector<int> IndentForLevel;
187const FormatToken *getMatchingNamespaceToken(
188 const AnnotatedLine *
Line,
189 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
190 if (!
Line->startsWith(tok::r_brace))
192 size_t StartLineIndex =
Line->MatchingOpeningBlockLineIndex;
195 assert(StartLineIndex < AnnotatedLines.size());
196 return AnnotatedLines[StartLineIndex]->First->getNamespaceToken();
200 const FormatToken *NamespaceToken =
Line->First->getNamespaceToken();
201 return NamespaceToken ? NamespaceToken->TokenText : StringRef();
204StringRef getMatchingNamespaceTokenText(
205 const AnnotatedLine *
Line,
206 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
207 const FormatToken *NamespaceToken =
208 getMatchingNamespaceToken(
Line, AnnotatedLines);
209 return NamespaceToken ? NamespaceToken->TokenText : StringRef();
214 LineJoiner(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
215 const SmallVectorImpl<AnnotatedLine *> &Lines)
216 : Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()),
217 AnnotatedLines(Lines) {}
220 const AnnotatedLine *getNextMergedLine(
bool DryRun,
221 LevelIndentTracker &IndentTracker) {
224 const AnnotatedLine *Current = *Next;
225 IndentTracker.nextLine(*Current);
226 unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End);
227 if (MergedLines > 0 && Style.ColumnLimit == 0) {
230 for (
unsigned i = 0; i < MergedLines; ++i)
231 if (Next[i + 1]->
First->NewlinesBefore > 0)
235 for (
unsigned i = 0; i < MergedLines; ++i)
236 join(*Next[0], *Next[i + 1]);
237 Next = Next + MergedLines + 1;
244 tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
245 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
246 SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
247 const unsigned Indent = IndentTracker.getIndent();
253 const AnnotatedLine *TheLine = *I;
254 if (TheLine->Last->is(TT_LineComment))
256 const auto &NextLine = *I[1];
257 if (NextLine.Type ==
LT_Invalid || NextLine.First->MustBreakBefore)
259 if (TheLine->InPPDirective &&
260 (!NextLine.InPPDirective || NextLine.First->HasUnescapedNewline)) {
264 if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
268 Style.ColumnLimit == 0 ?
UINT_MAX : Style.ColumnLimit - Indent;
271 Limit = TheLine->Last->TotalLength > Limit
273 : Limit - TheLine->Last->TotalLength;
275 if (TheLine->Last->is(TT_FunctionLBrace) &&
276 TheLine->First == TheLine->Last &&
277 !Style.BraceWrapping.SplitEmptyFunction &&
278 NextLine.First->is(tok::r_brace)) {
279 return tryMergeSimpleBlock(I, E, Limit);
282 const auto *PreviousLine = I != AnnotatedLines.begin() ? I[-1] :
nullptr;
284 if (PreviousLine && TheLine->Last->is(tok::l_brace) &&
285 TheLine->First == TheLine->Last) {
286 bool EmptyBlock = NextLine.First->is(tok::r_brace);
288 const FormatToken *Tok = PreviousLine->First;
289 if (Tok && Tok->is(tok::comment))
290 Tok = Tok->getNextNonComment();
292 if (Tok && Tok->getNamespaceToken()) {
293 return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
294 ? tryMergeSimpleBlock(I, E, Limit)
298 if (Tok && Tok->is(tok::kw_typedef))
299 Tok = Tok->getNextNonComment();
300 if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
301 tok::kw_extern, Keywords.kw_interface)) {
302 return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
303 ? tryMergeSimpleBlock(I, E, Limit)
307 if (Tok && Tok->is(tok::kw_template) &&
308 Style.BraceWrapping.SplitEmptyRecord && EmptyBlock) {
313 auto ShouldMergeShortFunctions = [
this, &I, &NextLine, PreviousLine,
318 NextLine.First->is(tok::r_brace)) {
322 if (Style.AllowShortFunctionsOnASingleLine &
326 if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace))
329 if (TheLine->Level != 0) {
335 const AnnotatedLine *
Line =
nullptr;
336 for (
auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
338 if (!(*J)->InPPDirective && !(*J)->isComment() &&
339 (*J)->Level < TheLine->Level) {
349 const FormatToken *LastNonComment =
Line->Last;
350 assert(LastNonComment);
351 if (LastNonComment->is(tok::comment)) {
352 LastNonComment = LastNonComment->getPreviousNonComment();
355 assert(LastNonComment);
357 return isRecordLBrace(*LastNonComment);
364 bool MergeShortFunctions = ShouldMergeShortFunctions();
366 const FormatToken *FirstNonComment = TheLine->First;
367 if (FirstNonComment->is(tok::comment)) {
368 FirstNonComment = FirstNonComment->getNextNonComment();
369 if (!FirstNonComment)
375 if (Style.CompactNamespaces) {
376 if (
const auto *NSToken = TheLine->First->getNamespaceToken()) {
378 assert(TheLine->MatchingClosingBlockLineIndex > 0);
379 for (
auto ClosingLineIndex = TheLine->MatchingClosingBlockLineIndex - 1;
381 ClosingLineIndex == I[J]->MatchingClosingBlockLineIndex &&
382 I[J]->
Last->TotalLength < Limit;
383 ++J, --ClosingLineIndex) {
384 Limit -= I[J]->Last->TotalLength;
388 auto *ClosingLine = AnnotatedLines.begin() + ClosingLineIndex + 1;
389 const int OutdentBy = I[J]->Level - TheLine->Level;
390 assert(OutdentBy >= 0);
391 for (
auto *CompactedLine = I + J; CompactedLine <= ClosingLine;
393 if (!(*CompactedLine)->InPPDirective) {
394 const int Level = (*CompactedLine)->Level;
395 (*CompactedLine)->Level = std::max(Level - OutdentBy, 0);
402 if (
auto nsToken = getMatchingNamespaceToken(TheLine, AnnotatedLines)) {
404 unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
405 for (; I + 1 + i != E &&
406 nsToken->TokenText ==
407 getMatchingNamespaceTokenText(I[i + 1], AnnotatedLines) &&
408 openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;
409 i++, --openingLine) {
411 I[i + 1]->First->SpacesRequiredBefore =
412 I[i]->Last->isNot(tok::r_brace);
415 IndentTracker.nextLine(*I[i + 1]);
422 if (TheLine->Last->is(TT_FunctionLBrace) && TheLine->First != TheLine->Last)
423 return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
425 if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
426 FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
429 ? tryMergeSimpleBlock(I, E, Limit)
433 if (NextLine.First->is(tok::l_brace)) {
434 if ((TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
435 tok::kw_for, tok::kw_switch, tok::kw_try,
436 tok::kw_do, TT_ForEachMacro) ||
437 (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
438 TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
439 Style.BraceWrapping.AfterControlStatement ==
444 return (Style.ColumnLimit == 0 || TheLine->Level * Style.IndentWidth +
445 TheLine->Last->TotalLength <=
450 if (TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
451 tok::kw_for, TT_ForEachMacro)) {
452 return (Style.BraceWrapping.AfterControlStatement ==
454 ? tryMergeSimpleBlock(I, E, Limit)
457 if (TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&
458 Style.BraceWrapping.AfterControlStatement ==
465 return (Style.ColumnLimit == 0 ||
466 TheLine->Last->TotalLength <= Style.ColumnLimit)
471 if (PreviousLine && TheLine->First->is(tok::l_brace)) {
472 switch (PreviousLine->First->Tok.getKind()) {
475 if (PreviousLine->First->Next) {
477 PreviousLine->First->Next->Tok.getObjCKeywordID();
478 if (kwId == tok::objc_autoreleasepool ||
479 kwId == tok::objc_synchronized) {
486 case tok::kw_default:
497 if (PreviousLine && Style.BraceWrapping.SplitEmptyRecord &&
498 TheLine->Last->is(tok::l_brace) && PreviousLine->Last) {
499 const FormatToken *
Previous = PreviousLine->Last;
504 if (
Previous->is(tok::greater) && !PreviousLine->InPPDirective)
506 if (
Previous->is(tok::identifier)) {
507 const FormatToken *PreviousPrevious =
509 if (PreviousPrevious &&
510 PreviousPrevious->isOneOf(tok::kw_class, tok::kw_struct)) {
518 if (TheLine->Last->is(tok::l_brace)) {
519 bool ShouldMerge =
false;
521 if (TheLine->Last->is(TT_EnumLBrace)) {
522 ShouldMerge = Style.AllowShortEnumsOnASingleLine;
523 }
else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) {
524 ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
525 }
else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
529 ShouldMerge = !Style.BraceWrapping.AfterClass ||
530 (NextLine.First->is(tok::r_brace) &&
531 !Style.BraceWrapping.SplitEmptyRecord);
532 }
else if (TheLine->InPPDirective ||
533 !TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
537 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
538 (NextLine.First->is(tok::r_brace) &&
539 !Style.BraceWrapping.SplitEmptyFunction);
541 return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;
545 if (NextLine.First->is(TT_FunctionLBrace) &&
546 Style.BraceWrapping.AfterFunction) {
547 if (NextLine.Last->is(TT_LineComment))
551 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
555 unsigned MergedLines = 0;
556 if (MergeShortFunctions ||
558 NextLine.First == NextLine.Last && I + 2 != E &&
559 I[2]->First->is(tok::r_brace))) {
560 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
568 auto IsElseLine = [&TheLine]() ->
bool {
569 const FormatToken *
First = TheLine->First;
570 if (
First->is(tok::kw_else))
573 return First->is(tok::r_brace) &&
First->Next &&
574 First->Next->is(tok::kw_else);
576 if (TheLine->First->is(tok::kw_if) ||
577 (IsElseLine() && (Style.AllowShortIfStatementsOnASingleLine ==
579 return Style.AllowShortIfStatementsOnASingleLine
580 ? tryMergeSimpleControlStatement(I, E, Limit)
583 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do,
585 return Style.AllowShortLoopsOnASingleLine
586 ? tryMergeSimpleControlStatement(I, E, Limit)
589 if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
590 return Style.AllowShortCaseLabelsOnASingleLine
591 ? tryMergeShortCaseLabels(I, E, Limit)
594 if (TheLine->InPPDirective &&
595 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
596 return tryMergeSimplePPDirective(I, E, Limit);
602 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
603 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
607 if (I + 2 != E && I[2]->InPPDirective && !I[2]->
First->HasUnescapedNewline)
609 if (1 + I[1]->
Last->TotalLength > Limit)
614 unsigned tryMergeSimpleControlStatement(
615 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
616 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
unsigned Limit) {
619 if (Style.BraceWrapping.AfterControlStatement ==
621 I[1]->First->is(tok::l_brace) &&
625 if (I[1]->InPPDirective != (*I)->InPPDirective ||
626 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) {
629 Limit = limitConsideringMacros(I + 1, E, Limit);
630 AnnotatedLine &
Line = **I;
631 if (
Line.First->isNot(tok::kw_do) &&
Line.First->isNot(tok::kw_else) &&
632 Line.Last->isNot(tok::kw_else) &&
Line.Last->isNot(tok::r_paren)) {
636 if (
Line.First->is(tok::kw_do) &&
Line.Last->isNot(tok::kw_do))
638 if (1 + I[1]->
Last->TotalLength > Limit)
641 if (I[1]->
First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
642 TT_ForEachMacro, TT_LineComment)) {
646 if (Style.AllowShortIfStatementsOnASingleLine ==
648 if (I + 2 != E &&
Line.startsWith(tok::kw_if) &&
649 I[2]->First->is(tok::kw_else)) {
657 tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
658 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
660 if (Limit == 0 || I + 1 == E ||
661 I[1]->
First->isOneOf(tok::kw_case, tok::kw_default)) {
664 if (I[0]->
Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))
666 unsigned NumStmts = 0;
668 bool EndsWithComment =
false;
669 bool InPPDirective = I[0]->InPPDirective;
670 bool InMacroBody = I[0]->InMacroBody;
671 const unsigned Level = I[0]->Level;
672 for (; NumStmts < 3; ++NumStmts) {
673 if (I + 1 + NumStmts == E)
675 const AnnotatedLine *
Line = I[1 + NumStmts];
676 if (
Line->InPPDirective != InPPDirective)
678 if (
Line->InMacroBody != InMacroBody)
680 if (
Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
682 if (
Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
687 if (
Line->First->is(tok::comment)) {
688 if (Level !=
Line->Level)
690 SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts;
691 for (; J != E; ++J) {
693 if (
Line->InPPDirective != InPPDirective)
695 if (
Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
697 if (
Line->First->isNot(tok::comment) || Level !=
Line->Level)
702 if (
Line->Last->is(tok::comment))
703 EndsWithComment =
true;
704 Length += I[1 + NumStmts]->Last->TotalLength + 1;
706 if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
712 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
713 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
719 AnnotatedLine &
Line = **I;
725 Line.First->isOneOf(tok::at, tok::minus, tok::plus)) {
731 if (
Line.First->is(tok::kw_case) ||
732 (
Line.First->Next &&
Line.First->Next->is(tok::kw_else))) {
736 if (
Line.First->is(tok::kw_default)) {
737 const FormatToken *Tok =
Line.First->getNextNonComment();
738 if (Tok && Tok->is(tok::colon))
742 auto IsCtrlStmt = [](
const auto &
Line) {
743 return Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
744 tok::kw_do, tok::kw_for, TT_ForEachMacro);
747 const bool IsSplitBlock =
750 I[1]->First->isNot(tok::r_brace));
752 if (IsCtrlStmt(
Line) ||
753 Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
754 tok::kw___finally, tok::r_brace,
755 Keywords.kw___except)) {
760 if (!Style.AllowShortIfStatementsOnASingleLine &&
761 Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
762 !Style.BraceWrapping.AfterControlStatement &&
763 I[1]->First->isNot(tok::r_brace)) {
766 if (!Style.AllowShortIfStatementsOnASingleLine &&
767 Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
768 Style.BraceWrapping.AfterControlStatement ==
770 I + 2 != E && I[2]->First->isNot(tok::r_brace)) {
773 if (!Style.AllowShortLoopsOnASingleLine &&
774 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
776 !Style.BraceWrapping.AfterControlStatement &&
777 I[1]->First->isNot(tok::r_brace)) {
780 if (!Style.AllowShortLoopsOnASingleLine &&
781 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
783 Style.BraceWrapping.AfterControlStatement ==
785 I + 2 != E && I[2]->First->isNot(tok::r_brace)) {
793 if (
Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
794 Keywords.kw___except, tok::kw___finally)) {
799 if (
Line.Last->is(tok::l_brace)) {
800 if (IsSplitBlock &&
Line.First ==
Line.Last &&
801 I > AnnotatedLines.begin() &&
802 (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {
805 FormatToken *Tok = I[1]->First;
806 auto ShouldMerge = [Tok]() {
807 if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore)
809 const FormatToken *Next = Tok->getNextNonComment();
810 return !Next || Next->is(tok::semi);
815 Tok->SpacesRequiredBefore = Style.SpaceInEmptyBlock ? 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 for (; Tok; Tok = Tok->Next) {
955 Tok->SpacesRequiredBefore = 0;
957 Tok->Finalized =
true;
963static void printLineState(
const LineState &State) {
964 llvm::dbgs() <<
"State: ";
965 for (
const ParenState &
P : State.Stack) {
966 llvm::dbgs() << (
P.Tok ?
P.Tok->TokenText :
"F") <<
"|" <<
P.Indent <<
"|"
967 <<
P.LastSpace <<
"|" <<
P.NestedBlockIndent <<
" ";
969 llvm::dbgs() << State.NextToken->TokenText <<
"\n";
976 LineFormatter(ContinuationIndenter *
Indenter, WhitespaceManager *Whitespaces,
977 const FormatStyle &Style,
978 UnwrappedLineFormatter *BlockFormatter)
980 BlockFormatter(BlockFormatter) {}
981 virtual ~LineFormatter() {}
986 virtual unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
987 unsigned FirstStartColumn,
bool DryRun) = 0;
1010 bool formatChildren(LineState &State,
bool NewLine,
bool DryRun,
1011 unsigned &Penalty) {
1012 const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
1013 bool HasLBrace = LBrace && LBrace->is(tok::l_brace) && LBrace->is(
BK_Block);
1014 FormatToken &
Previous = *State.NextToken->Previous;
1015 if (
Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent)) {
1022 const ParenState &
P = State.Stack.back();
1024 int AdditionalIndent =
1025 P.Indent -
Previous.Children[0]->Level * Style.IndentWidth;
1027 BlockFormatter->format(
Previous.Children, DryRun, AdditionalIndent,
1032 if (
Previous.Children[0]->First->MustBreakBefore)
1043 const AnnotatedLine *Child =
Previous.Children[0];
1045 if (Child->Last->isTrailingComment())
1050 if (Style.ColumnLimit > 0 &&
1051 Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) {
1056 Whitespaces->replaceWhitespace(
1057 *Child->First, 0, 1,
1058 State.Column,
false,
1059 State.Line->InPPDirective);
1062 formatLine(*Child, State.Column + 1, 0, DryRun);
1064 State.Column += 1 + Child->Last->TotalLength;
1071 WhitespaceManager *Whitespaces;
1072 const FormatStyle &Style;
1073 UnwrappedLineFormatter *BlockFormatter;
1077class NoColumnLimitLineFormatter :
public LineFormatter {
1079 NoColumnLimitLineFormatter(ContinuationIndenter *
Indenter,
1080 WhitespaceManager *Whitespaces,
1081 const FormatStyle &Style,
1082 UnwrappedLineFormatter *BlockFormatter)
1083 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1087 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1088 unsigned FirstStartColumn,
bool DryRun)
override {
1090 LineState State =
Indenter->getInitialState(FirstIndent, FirstStartColumn,
1092 while (State.NextToken) {
1095 (
Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
1096 unsigned Penalty = 0;
1097 formatChildren(State, Newline,
false, Penalty);
1098 Indenter->addTokenToState(State, Newline,
false);
1105class NoLineBreakFormatter :
public LineFormatter {
1107 NoLineBreakFormatter(ContinuationIndenter *
Indenter,
1108 WhitespaceManager *Whitespaces,
const FormatStyle &Style,
1109 UnwrappedLineFormatter *BlockFormatter)
1110 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1113 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1114 unsigned FirstStartColumn,
bool DryRun)
override {
1115 unsigned Penalty = 0;
1117 Indenter->getInitialState(FirstIndent, FirstStartColumn, &
Line, DryRun);
1118 while (State.NextToken) {
1119 formatChildren(State,
false, DryRun, Penalty);
1121 State, State.NextToken->MustBreakBefore, DryRun);
1128class OptimizingLineFormatter :
public LineFormatter {
1130 OptimizingLineFormatter(ContinuationIndenter *
Indenter,
1131 WhitespaceManager *Whitespaces,
1132 const FormatStyle &Style,
1133 UnwrappedLineFormatter *BlockFormatter)
1134 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1138 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1139 unsigned FirstStartColumn,
bool DryRun)
override {
1141 Indenter->getInitialState(FirstIndent, FirstStartColumn, &
Line, DryRun);
1146 State.Stack.back().BreakBeforeParameter =
true;
1149 return analyzeSolutionSpace(State, DryRun);
1153 struct CompareLineStatePointers {
1154 bool operator()(LineState *obj1, LineState *obj2)
const {
1155 return *obj1 < *obj2;
1164 typedef std::pair<unsigned, unsigned> OrderedPenalty;
1169 StateNode(
const LineState &State,
bool NewLine, StateNode *Previous)
1178 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
1181 typedef std::priority_queue<QueueItem, SmallVector<QueueItem>,
1182 std::greater<QueueItem>>
1193 unsigned analyzeSolutionSpace(LineState &InitialState,
bool DryRun) {
1194 std::set<LineState *, CompareLineStatePointers> Seen;
1202 StateNode *RootNode =
1203 new (Allocator.Allocate()) StateNode(InitialState,
false,
nullptr);
1204 Queue.push(QueueItem(OrderedPenalty(0, Count), RootNode));
1207 unsigned Penalty = 0;
1210 while (!Queue.empty()) {
1212 if (Count > 25000000)
1215 Penalty = Queue.top().first.first;
1216 StateNode *
Node = Queue.top().second;
1217 if (!
Node->State.NextToken) {
1218 LLVM_DEBUG(llvm::dbgs()
1219 <<
"\n---\nPenalty for line: " << Penalty <<
"\n");
1227 Node->State.IgnoreStackForComparison =
true;
1229 if (!Seen.insert(&
Node->State).second) {
1236 addNextStateToQueue(Penalty,
Node,
false, &Count, &Queue);
1238 addNextStateToQueue(Penalty,
Node,
true, &Count, &Queue);
1241 if (Queue.empty()) {
1244 LLVM_DEBUG(llvm::dbgs() <<
"Could not find a solution.\n");
1250 reconstructPath(InitialState, Queue.top().second);
1252 LLVM_DEBUG(llvm::dbgs()
1253 <<
"Total number of analyzed states: " << Count <<
"\n");
1254 LLVM_DEBUG(llvm::dbgs() <<
"---\n");
1263 void addNextStateToQueue(
unsigned Penalty, StateNode *PreviousNode,
1264 bool NewLine,
unsigned *Count, QueueType *Queue) {
1270 StateNode *
Node =
new (Allocator.Allocate())
1271 StateNode(PreviousNode->State,
NewLine, PreviousNode);
1272 if (!formatChildren(
Node->State,
NewLine,
true, Penalty))
1277 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count),
Node));
1283 void reconstructPath(LineState &State, StateNode *Best) {
1286 while (Best->Previous) {
1287 Path.push_back(Best);
1288 Best = Best->Previous;
1290 for (
const auto &
Node : llvm::reverse(Path)) {
1291 unsigned Penalty = 0;
1292 formatChildren(State,
Node->NewLine,
false, Penalty);
1293 Penalty +=
Indenter->addTokenToState(State,
Node->NewLine,
false);
1296 printLineState(
Node->Previous->State);
1297 if (
Node->NewLine) {
1298 llvm::dbgs() <<
"Penalty for placing "
1299 <<
Node->Previous->State.NextToken->Tok.getName()
1300 <<
" on a new line: " << Penalty <<
"\n";
1306 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1313 int AdditionalIndent,
bool FixBadIndentation,
unsigned FirstStartColumn,
1314 unsigned NextStartColumn,
unsigned LastStartColumn) {
1315 LineJoiner Joiner(Style, Keywords, Lines);
1318 std::pair<const SmallVectorImpl<AnnotatedLine *> *,
unsigned> CacheKey(
1319 &Lines, AdditionalIndent);
1320 auto CacheIt = PenaltyCache.find(CacheKey);
1321 if (DryRun && CacheIt != PenaltyCache.end())
1322 return CacheIt->second;
1324 assert(!Lines.empty());
1325 unsigned Penalty = 0;
1326 LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
1335 bool FirstLine =
true;
1337 Joiner.getNextMergedLine(DryRun, IndentTracker);
1338 Line; PrevPrevLine = PreviousLine, PreviousLine =
Line,
Line = NextLine,
1339 FirstLine =
false) {
1340 assert(
Line->First);
1342 unsigned Indent = IndentTracker.getIndent();
1348 bool PreviousRBrace =
1349 PreviousLine && PreviousLine->
startsWith(tok::r_brace);
1350 bool ContinueFormatting =
1351 TheLine.
Level > RangeMinLevel ||
1352 (TheLine.
Level == RangeMinLevel && !PreviousRBrace &&
1355 bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
1357 bool ShouldFormat = TheLine.
Affected || FixIndentation;
1368 bool LastLine = TheLine.
First->
is(tok::eof);
1369 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,
1370 LastLine ? LastStartColumn : NextStartColumn + Indent);
1373 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1374 unsigned ColumnLimit = getColumnLimit(TheLine.
InPPDirective, NextLine);
1375 bool FitsIntoOneLine =
1379 (!Style.isJavaScript() || !Style.JavaScriptWrapImports)) ||
1380 (Style.isCSharp() &&
1382 if (Style.ColumnLimit == 0) {
1383 NoColumnLimitLineFormatter(
Indenter, Whitespaces, Style,
this)
1384 .formatLine(TheLine, NextStartColumn + Indent,
1385 FirstLine ? FirstStartColumn : 0, DryRun);
1386 }
else if (FitsIntoOneLine) {
1387 Penalty += NoLineBreakFormatter(
Indenter, Whitespaces, Style,
this)
1388 .formatLine(TheLine, NextStartColumn + Indent,
1389 FirstLine ? FirstStartColumn : 0, DryRun);
1391 Penalty += OptimizingLineFormatter(
Indenter, Whitespaces, Style,
this)
1392 .formatLine(TheLine, NextStartColumn + Indent,
1393 FirstLine ? FirstStartColumn : 0, DryRun);
1395 RangeMinLevel = std::min(RangeMinLevel, TheLine.
Level);
1401 if (!Tok->Children.empty())
1402 format(Tok->Children, DryRun);
1407 bool StartsNewLine =
1410 IndentTracker.adjustToUnmodifiedLine(TheLine);
1412 bool ReformatLeadingWhitespace =
1413 StartsNewLine && ((PreviousLine && PreviousLine->
Affected) ||
1416 if (ReformatLeadingWhitespace) {
1417 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines,
1421 Whitespaces->addUntouchableToken(*TheLine.
First,
1427 Whitespaces->addUntouchableToken(*Tok, TheLine.
InPPDirective);
1429 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1433 auto *Tok = TheLine.
First;
1434 if (Tok->is(tok::hash) && !Tok->Previous && Tok->Next &&
1435 Tok->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,
1436 tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,
1437 tok::pp_else, tok::pp_endif)) {
1443 PenaltyCache[CacheKey] = Penalty;
1452 const auto &RootToken = *
Line.First;
1456 if (RootToken.is(tok::r_brace) &&
1458 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&
1461 Newlines = std::min(Newlines, 1u);
1464 if (!PreviousLine &&
Line.Level > 0)
1465 Newlines = std::min(Newlines, 1u);
1466 if (Newlines == 0 && !RootToken.IsFirst)
1468 if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
1473 PreviousLine->
Last->
is(tok::l_brace) &&
1477 !startsExternCBlock(*PreviousLine)) {
1482 if (PreviousLine && RootToken.isAccessSpecifier()) {
1489 Newlines = std::max(RootToken.NewlinesBefore, 1u);
1492 if (PreviousLine->
Last->
isOneOf(tok::semi, tok::r_brace) && Newlines <= 1)
1499 if (PreviousLine->
Last->
is(tok::comment))
1502 previousToken = PreviousLine->
Last;
1503 if ((!previousToken || previousToken->
isNot(tok::l_brace)) &&
1513 (!PreviousLine->
InPPDirective || !RootToken.HasUnescapedNewline)) {
1516 if (!RootToken.isAccessSpecifier()) {
1522 Newlines = std::max(Newlines, 1u);
1525 if (RootToken.is(tok::r_brace))
1528 Newlines = std::max(Newlines, 2u);
1537void UnwrappedLineFormatter::formatFirstToken(
1538 const AnnotatedLine &
Line,
const AnnotatedLine *PreviousLine,
1539 const AnnotatedLine *PrevPrevLine,
1541 unsigned NewlineIndent) {
1542 FormatToken &RootToken = *
Line.First;
1543 if (RootToken.is(tok::eof)) {
1545 std::min(RootToken.NewlinesBefore,
1546 Style.KeepEmptyLinesAtEOF ? Style.MaxEmptyLinesToKeep + 1 : 1);
1547 unsigned TokenIndent = Newlines ? NewlineIndent : 0;
1548 Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
1553 if (RootToken.Newlines < 0) {
1554 RootToken.Newlines =
1556 assert(RootToken.Newlines >= 0);
1559 if (RootToken.Newlines > 0)
1560 Indent = NewlineIndent;
1564 if (!Style.isJavaScript() &&
1571 Whitespaces->replaceWhitespace(RootToken, RootToken.Newlines, Indent, Indent,
1573 Line.InPPDirective &&
1574 !RootToken.HasUnescapedNewline);
1578UnwrappedLineFormatter::getColumnLimit(
bool InPPDirective,
1579 const AnnotatedLine *NextLine)
const {
1582 bool ContinuesPPDirective =
1587 (NextLine->InPPDirective &&
1590 !NextLine->First->HasUnescapedNewline));
1591 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 '@'.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))