13#include "llvm/Support/Debug.h"
16#define DEBUG_TYPE "format-formatter"
23bool startsExternCBlock(
const AnnotatedLine &Line) {
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;
77 Indent = getIndent(Line.Level);
79 if (
static_cast<int>(Indent) +
Offset >= 0)
81 if (Line.IsContinuation)
82 Indent = Line.Level * Style.IndentWidth + Style.ContinuationIndentWidth;
90 void adjustToUnmodifiedLine(
const AnnotatedLine &Line) {
91 unsigned LevelIndent = Line.First->OriginalColumn;
92 if (
static_cast<int>(LevelIndent) -
Offset >= 0)
94 assert(Line.Level < IndentForLevel.size());
95 if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) &&
96 !Line.InPPDirective) {
97 IndentForLevel[Line.Level] = LevelIndent;
106 int getIndentOffset(
const FormatToken &RootToken) {
112 auto IsAccessModifier = [
this, &RootToken]() {
113 if (RootToken.isAccessSpecifier(Style.isCpp())) {
115 }
else if (RootToken.isObjCAccessSpecifier()) {
119 else if ((RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
120 RootToken.Next && RootToken.Next->is(tok::colon))) {
122 }
else if (RootToken.Next &&
123 RootToken.Next->isOneOf(Keywords.kw_slots,
124 Keywords.kw_qslots) &&
125 RootToken.Next->Next && RootToken.Next->Next->is(tok::colon)) {
129 else if (!RootToken.Next && RootToken.isAccessSpecifier(
false)) {
135 if (IsAccessModifier()) {
139 return Style.IndentAccessModifiers ? -Style.IndentWidth
140 : Style.AccessModifierOffset;
150 unsigned getIndent(
unsigned Level)
const {
151 if (IndentForLevel[Level] != -1)
152 return IndentForLevel[Level];
155 return getIndent(Level - 1) + Style.IndentWidth;
158 const FormatStyle &Style;
159 const AdditionalKeywords &Keywords;
160 const unsigned AdditionalIndent;
163 SmallVector<int> IndentForLevel;
175const FormatToken *getMatchingNamespaceToken(
176 const AnnotatedLine *Line,
177 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
183 assert(StartLineIndex < AnnotatedLines.size());
184 return AnnotatedLines[StartLineIndex]->First->getNamespaceToken();
189 return NamespaceToken ? NamespaceToken->
TokenText : StringRef();
192StringRef getMatchingNamespaceTokenText(
193 const AnnotatedLine *Line,
194 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
195 const FormatToken *NamespaceToken =
196 getMatchingNamespaceToken(Line, AnnotatedLines);
197 return NamespaceToken ? NamespaceToken->TokenText : StringRef();
202 LineJoiner(
const FormatStyle &Style,
const AdditionalKeywords &Keywords,
203 const SmallVectorImpl<AnnotatedLine *> &Lines)
204 : Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()),
205 AnnotatedLines(Lines) {}
208 const AnnotatedLine *getNextMergedLine(
bool DryRun,
209 LevelIndentTracker &IndentTracker) {
212 const AnnotatedLine *Current = *Next;
213 IndentTracker.nextLine(*Current);
214 unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End);
215 if (MergedLines > 0 && Style.ColumnLimit == 0) {
218 for (
unsigned i = 0; i < MergedLines; ++i)
219 if (Next[i + 1]->
First->NewlinesBefore > 0)
223 for (
unsigned i = 0; i < MergedLines; ++i)
224 join(*Next[0], *Next[i + 1]);
225 Next = Next + MergedLines + 1;
232 tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
233 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
234 SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
235 const unsigned Indent = IndentTracker.getIndent();
241 const AnnotatedLine *TheLine = *I;
242 if (TheLine->Last->is(TT_LineComment))
244 const auto &NextLine = *I[1];
245 if (NextLine.Type ==
LT_Invalid || NextLine.First->MustBreakBefore)
247 if (TheLine->InPPDirective &&
248 (!NextLine.InPPDirective || NextLine.First->HasUnescapedNewline)) {
252 if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
256 Style.ColumnLimit == 0 ?
UINT_MAX : Style.ColumnLimit - Indent;
259 Limit = TheLine->Last->TotalLength > Limit
261 : Limit - TheLine->Last->TotalLength;
263 if (TheLine->Last->is(TT_FunctionLBrace) &&
264 TheLine->First == TheLine->Last &&
265 !Style.BraceWrapping.SplitEmptyFunction &&
266 NextLine.First->is(tok::r_brace)) {
267 return tryMergeSimpleBlock(I, E, Limit);
270 const auto *PreviousLine = I != AnnotatedLines.begin() ? I[-1] :
nullptr;
272 if (PreviousLine && TheLine->Last->is(tok::l_brace) &&
273 TheLine->First == TheLine->Last) {
274 bool EmptyBlock = NextLine.First->is(tok::r_brace);
276 const FormatToken *Tok = PreviousLine->First;
277 if (Tok && Tok->is(tok::comment))
278 Tok = Tok->getNextNonComment();
280 if (Tok && Tok->getNamespaceToken()) {
281 return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
282 ? tryMergeSimpleBlock(I, E, Limit)
286 if (Tok && Tok->is(tok::kw_typedef))
287 Tok = Tok->getNextNonComment();
288 if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
289 tok::kw_extern, Keywords.kw_interface)) {
290 return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
291 ? tryMergeSimpleBlock(I, E, Limit)
295 if (Tok && Tok->is(tok::kw_template) &&
296 Style.BraceWrapping.SplitEmptyRecord && EmptyBlock) {
301 auto ShouldMergeShortFunctions = [
this, &I, &NextLine, PreviousLine,
306 NextLine.First->is(tok::r_brace)) {
310 if (Style.AllowShortFunctionsOnASingleLine &
314 if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace))
317 if (TheLine->Level != 0) {
323 const AnnotatedLine *Line =
nullptr;
324 for (
auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
326 if (!(*J)->InPPDirective && !(*J)->isComment() &&
327 (*J)->Level < TheLine->Level) {
337 const FormatToken *LastNonComment = Line->Last;
338 assert(LastNonComment);
339 if (LastNonComment->is(tok::comment)) {
340 LastNonComment = LastNonComment->getPreviousNonComment();
343 assert(LastNonComment);
345 return isRecordLBrace(*LastNonComment);
352 bool MergeShortFunctions = ShouldMergeShortFunctions();
354 const FormatToken *FirstNonComment = TheLine->First;
355 if (FirstNonComment->is(tok::comment)) {
356 FirstNonComment = FirstNonComment->getNextNonComment();
357 if (!FirstNonComment)
363 if (Style.CompactNamespaces) {
364 if (
const auto *NSToken = TheLine->First->getNamespaceToken()) {
366 assert(TheLine->MatchingClosingBlockLineIndex > 0);
367 for (
auto ClosingLineIndex = TheLine->MatchingClosingBlockLineIndex - 1;
369 ClosingLineIndex == I[J]->MatchingClosingBlockLineIndex &&
370 I[J]->
Last->TotalLength < Limit;
371 ++J, --ClosingLineIndex) {
372 Limit -= I[J]->Last->TotalLength;
376 auto *ClosingLine = AnnotatedLines.begin() + ClosingLineIndex + 1;
377 auto OutdentBy = I[J]->Level - TheLine->Level;
378 for (
auto *CompactedLine = I + J; CompactedLine <= ClosingLine;
380 if (!(*CompactedLine)->InPPDirective)
381 (*CompactedLine)->Level -= OutdentBy;
387 if (
auto nsToken = getMatchingNamespaceToken(TheLine, AnnotatedLines)) {
389 unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
390 for (; I + 1 + i != E &&
391 nsToken->TokenText ==
392 getMatchingNamespaceTokenText(I[i + 1], AnnotatedLines) &&
393 openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;
394 i++, --openingLine) {
396 I[i + 1]->First->SpacesRequiredBefore = !I[i]->Last->is(tok::r_brace);
399 IndentTracker.nextLine(*I[i + 1]);
406 if (TheLine->Last->is(TT_FunctionLBrace) && TheLine->First != TheLine->Last)
407 return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
409 if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
410 FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
413 ? tryMergeSimpleBlock(I, E, Limit)
417 if (NextLine.First->is(tok::l_brace)) {
418 if ((TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
419 tok::kw_for, tok::kw_switch, tok::kw_try,
420 tok::kw_do, TT_ForEachMacro) ||
421 (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
422 TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
423 Style.BraceWrapping.AfterControlStatement ==
428 return (Style.ColumnLimit == 0 || TheLine->Level * Style.IndentWidth +
429 TheLine->Last->TotalLength <=
434 if (TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
435 tok::kw_for, TT_ForEachMacro)) {
436 return (Style.BraceWrapping.AfterControlStatement ==
438 ? tryMergeSimpleBlock(I, E, Limit)
441 if (TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&
442 Style.BraceWrapping.AfterControlStatement ==
449 return (Style.ColumnLimit == 0 ||
450 TheLine->Last->TotalLength <= Style.ColumnLimit)
455 if (PreviousLine && TheLine->First->is(tok::l_brace)) {
456 switch (PreviousLine->First->Tok.getKind()) {
459 if (PreviousLine->First->Next) {
461 PreviousLine->First->Next->Tok.getObjCKeywordID();
462 if (kwId == tok::objc_autoreleasepool ||
463 kwId == tok::objc_synchronized) {
470 case tok::kw_default:
481 if (PreviousLine && Style.BraceWrapping.SplitEmptyRecord &&
482 TheLine->Last->is(tok::l_brace) && PreviousLine->Last) {
483 const FormatToken *
Previous = PreviousLine->Last;
488 if (
Previous->is(tok::greater) && !PreviousLine->InPPDirective)
490 if (
Previous->is(tok::identifier)) {
491 const FormatToken *PreviousPrevious =
493 if (PreviousPrevious &&
494 PreviousPrevious->isOneOf(tok::kw_class, tok::kw_struct)) {
502 if (TheLine->Last->is(tok::l_brace)) {
503 bool ShouldMerge =
false;
505 if (TheLine->Last->is(TT_EnumLBrace)) {
506 ShouldMerge = Style.AllowShortEnumsOnASingleLine;
507 }
else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
511 ShouldMerge = !Style.BraceWrapping.AfterClass ||
512 (NextLine.First->is(tok::r_brace) &&
513 !Style.BraceWrapping.SplitEmptyRecord);
514 }
else if (TheLine->InPPDirective ||
515 !TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
519 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
520 (NextLine.First->is(tok::r_brace) &&
521 !Style.BraceWrapping.SplitEmptyFunction);
523 return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;
527 if (NextLine.First->is(TT_FunctionLBrace) &&
528 Style.BraceWrapping.AfterFunction) {
529 if (NextLine.Last->is(TT_LineComment))
533 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
537 unsigned MergedLines = 0;
538 if (MergeShortFunctions ||
540 NextLine.First == NextLine.Last && I + 2 != E &&
541 I[2]->First->is(tok::r_brace))) {
542 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
550 auto IsElseLine = [&TheLine]() ->
bool {
551 const FormatToken *
First = TheLine->First;
552 if (
First->is(tok::kw_else))
555 return First->is(tok::r_brace) &&
First->Next &&
556 First->Next->is(tok::kw_else);
558 if (TheLine->First->is(tok::kw_if) ||
559 (IsElseLine() && (Style.AllowShortIfStatementsOnASingleLine ==
561 return Style.AllowShortIfStatementsOnASingleLine
562 ? tryMergeSimpleControlStatement(I, E, Limit)
565 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do,
567 return Style.AllowShortLoopsOnASingleLine
568 ? tryMergeSimpleControlStatement(I, E, Limit)
571 if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
572 return Style.AllowShortCaseLabelsOnASingleLine
573 ? tryMergeShortCaseLabels(I, E, Limit)
576 if (TheLine->InPPDirective &&
577 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
578 return tryMergeSimplePPDirective(I, E, Limit);
584 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
585 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
589 if (I + 2 != E && I[2]->InPPDirective && !I[2]->
First->HasUnescapedNewline)
591 if (1 + I[1]->
Last->TotalLength > Limit)
596 unsigned tryMergeSimpleControlStatement(
597 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
598 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
unsigned Limit) {
601 if (Style.BraceWrapping.AfterControlStatement ==
603 I[1]->First->is(tok::l_brace) &&
607 if (I[1]->InPPDirective != (*I)->InPPDirective ||
608 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) {
611 Limit = limitConsideringMacros(I + 1, E, Limit);
612 AnnotatedLine &Line = **I;
613 if (!Line.First->is(tok::kw_do) && !Line.First->is(tok::kw_else) &&
614 !Line.Last->is(tok::kw_else) && Line.Last->isNot(tok::r_paren)) {
618 if (Line.First->is(tok::kw_do) && !Line.Last->is(tok::kw_do))
620 if (1 + I[1]->
Last->TotalLength > Limit)
623 if (I[1]->
First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
624 TT_ForEachMacro, TT_LineComment)) {
628 if (Style.AllowShortIfStatementsOnASingleLine ==
630 if (I + 2 != E && Line.startsWith(tok::kw_if) &&
631 I[2]->First->is(tok::kw_else)) {
639 tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
640 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
642 if (Limit == 0 || I + 1 == E ||
643 I[1]->
First->isOneOf(tok::kw_case, tok::kw_default)) {
646 if (I[0]->
Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))
648 unsigned NumStmts = 0;
650 bool EndsWithComment =
false;
651 bool InPPDirective = I[0]->InPPDirective;
652 bool InMacroBody = I[0]->InMacroBody;
653 const unsigned Level = I[0]->Level;
654 for (; NumStmts < 3; ++NumStmts) {
655 if (I + 1 + NumStmts == E)
657 const AnnotatedLine *Line = I[1 + NumStmts];
658 if (Line->InPPDirective != InPPDirective)
660 if (Line->InMacroBody != InMacroBody)
662 if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
664 if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
669 if (Line->First->is(tok::comment)) {
670 if (Level != Line->Level)
672 SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts;
673 for (; J != E; ++J) {
675 if (Line->InPPDirective != InPPDirective)
677 if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
679 if (Line->First->isNot(tok::comment) || Level != Line->Level)
684 if (Line->Last->is(tok::comment))
685 EndsWithComment =
true;
686 Length += I[1 + NumStmts]->Last->TotalLength + 1;
688 if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
694 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
695 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
701 AnnotatedLine &Line = **I;
707 Line.First->isOneOf(tok::at, tok::minus, tok::plus)) {
713 if (Line.First->is(tok::kw_case) ||
714 (Line.First->Next && Line.First->Next->is(tok::kw_else))) {
718 if (Line.First->is(tok::kw_default)) {
719 const FormatToken *Tok = Line.First->getNextNonComment();
720 if (Tok && Tok->is(tok::colon))
724 auto IsCtrlStmt = [](
const auto &Line) {
725 return Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
726 tok::kw_do, tok::kw_for, TT_ForEachMacro);
729 const bool IsSplitBlock =
732 I[1]->First->isNot(tok::r_brace));
734 if (IsCtrlStmt(Line) ||
735 Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
736 tok::kw___finally, tok::r_brace,
737 Keywords.kw___except)) {
742 if (!Style.AllowShortIfStatementsOnASingleLine &&
743 Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
744 !Style.BraceWrapping.AfterControlStatement &&
745 !I[1]->First->is(tok::r_brace)) {
748 if (!Style.AllowShortIfStatementsOnASingleLine &&
749 Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
750 Style.BraceWrapping.AfterControlStatement ==
752 I + 2 != E && !I[2]->First->is(tok::r_brace)) {
755 if (!Style.AllowShortLoopsOnASingleLine &&
756 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
758 !Style.BraceWrapping.AfterControlStatement &&
759 !I[1]->First->is(tok::r_brace)) {
762 if (!Style.AllowShortLoopsOnASingleLine &&
763 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
765 Style.BraceWrapping.AfterControlStatement ==
767 I + 2 != E && !I[2]->First->is(tok::r_brace)) {
775 if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
776 Keywords.kw___except, tok::kw___finally)) {
781 if (Line.Last->is(tok::l_brace)) {
782 if (IsSplitBlock && Line.First == Line.Last &&
783 I > AnnotatedLines.begin() &&
784 (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {
787 FormatToken *Tok = I[1]->First;
788 auto ShouldMerge = [Tok]() {
789 if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore)
791 const FormatToken *Next = Tok->getNextNonComment();
792 return !Next || Next->is(tok::semi);
797 Tok->SpacesRequiredBefore = Style.SpaceInEmptyBlock ? 1 : 0;
798 Tok->CanBreakBefore =
true;
800 }
else if (Limit != 0 && !Line.startsWithNamespace() &&
801 !startsExternCBlock(Line)) {
803 if (isRecordLBrace(*Line.Last))
809 Limit = limitConsideringMacros(I + 2, E, Limit);
811 if (!nextTwoLinesFitInto(I, Limit))
816 if (I[1]->
Last->is(TT_LineComment))
826 if (Tok->isNot(tok::r_brace))
830 if (Tok->Next && Tok->Next->is(tok::kw_else))
839 if (Line.First == Line.Last && Line.First->isNot(TT_FunctionLBrace) &&
840 Style.BraceWrapping.AfterControlStatement ==
847 }
else if (I[1]->
First->is(tok::l_brace)) {
848 if (I[1]->
Last->is(TT_LineComment))
852 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I)))
855 unsigned MergedLines = 0;
857 (I[1]->First == I[1]->Last && I + 2 != E &&
858 I[2]->First->is(tok::r_brace))) {
859 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
873 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
874 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
876 if (I[0]->InPPDirective && I + 1 != E &&
877 !I[1]->
First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) {
878 return Limit < 2 ? 0 : Limit - 2;
883 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
885 if (I[1]->
First->MustBreakBefore || I[2]->First->MustBreakBefore)
887 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
890 bool containsMustBreak(
const AnnotatedLine *Line) {
894 for (
const FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next)
895 if (Tok->MustBreakBefore)
900 void join(AnnotatedLine &A,
const AnnotatedLine &B) {
901 assert(!A.Last->Next);
902 assert(!B.First->Previous);
905 A.Last->Next = B.First;
906 B.First->Previous = A.Last;
907 B.First->CanBreakBefore =
true;
908 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
909 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
910 Tok->TotalLength += LengthA;
915 const FormatStyle &Style;
916 const AdditionalKeywords &Keywords;
917 const SmallVectorImpl<AnnotatedLine *>::const_iterator End;
919 SmallVectorImpl<AnnotatedLine *>::const_iterator Next;
920 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines;
923static void markFinalized(FormatToken *Tok) {
924 for (; Tok; Tok = Tok->Next) {
937 Tok->SpacesRequiredBefore = 0;
939 Tok->Finalized =
true;
945static void printLineState(
const LineState &State) {
946 llvm::dbgs() <<
"State: ";
947 for (
const ParenState &
P : State.Stack) {
948 llvm::dbgs() << (
P.Tok ?
P.Tok->TokenText :
"F") <<
"|" <<
P.Indent <<
"|"
949 <<
P.LastSpace <<
"|" <<
P.NestedBlockIndent <<
" ";
951 llvm::dbgs() << State.NextToken->TokenText <<
"\n";
958 LineFormatter(ContinuationIndenter *
Indenter, WhitespaceManager *Whitespaces,
959 const FormatStyle &Style,
960 UnwrappedLineFormatter *BlockFormatter)
962 BlockFormatter(BlockFormatter) {}
963 virtual ~LineFormatter() {}
968 virtual unsigned formatLine(
const AnnotatedLine &Line,
unsigned FirstIndent,
969 unsigned FirstStartColumn,
bool DryRun) = 0;
992 bool formatChildren(LineState &State,
bool NewLine,
bool DryRun,
994 const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
995 bool HasLBrace = LBrace && LBrace->is(tok::l_brace) && LBrace->is(
BK_Block);
996 FormatToken &
Previous = *State.NextToken->Previous;
997 if (
Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent)) {
1004 const ParenState &
P = State.Stack.back();
1006 int AdditionalIndent =
1007 P.Indent -
Previous.Children[0]->Level * Style.IndentWidth;
1009 BlockFormatter->format(
Previous.Children, DryRun, AdditionalIndent,
1014 if (
Previous.Children[0]->First->MustBreakBefore)
1025 const AnnotatedLine *Child =
Previous.Children[0];
1027 if (Child->Last->isTrailingComment())
1032 if (Style.ColumnLimit > 0 &&
1033 Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) {
1038 Whitespaces->replaceWhitespace(
1039 *Child->First, 0, 1,
1040 State.Column,
false,
1041 State.Line->InPPDirective);
1044 formatLine(*Child, State.Column + 1, 0, DryRun);
1046 State.Column += 1 + Child->Last->TotalLength;
1053 WhitespaceManager *Whitespaces;
1054 const FormatStyle &Style;
1055 UnwrappedLineFormatter *BlockFormatter;
1059class NoColumnLimitLineFormatter :
public LineFormatter {
1061 NoColumnLimitLineFormatter(ContinuationIndenter *
Indenter,
1062 WhitespaceManager *Whitespaces,
1063 const FormatStyle &Style,
1064 UnwrappedLineFormatter *BlockFormatter)
1065 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1069 unsigned formatLine(
const AnnotatedLine &Line,
unsigned FirstIndent,
1070 unsigned FirstStartColumn,
bool DryRun)
override {
1072 LineState State =
Indenter->getInitialState(FirstIndent, FirstStartColumn,
1074 while (State.NextToken) {
1077 (
Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
1078 unsigned Penalty = 0;
1079 formatChildren(State, Newline,
false, Penalty);
1080 Indenter->addTokenToState(State, Newline,
false);
1087class NoLineBreakFormatter :
public LineFormatter {
1089 NoLineBreakFormatter(ContinuationIndenter *
Indenter,
1090 WhitespaceManager *Whitespaces,
const FormatStyle &Style,
1091 UnwrappedLineFormatter *BlockFormatter)
1092 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1095 unsigned formatLine(
const AnnotatedLine &Line,
unsigned FirstIndent,
1096 unsigned FirstStartColumn,
bool DryRun)
override {
1097 unsigned Penalty = 0;
1099 Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
1100 while (State.NextToken) {
1101 formatChildren(State,
false, DryRun, Penalty);
1103 State, State.NextToken->MustBreakBefore, DryRun);
1110class OptimizingLineFormatter :
public LineFormatter {
1112 OptimizingLineFormatter(ContinuationIndenter *
Indenter,
1113 WhitespaceManager *Whitespaces,
1114 const FormatStyle &Style,
1115 UnwrappedLineFormatter *BlockFormatter)
1116 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1120 unsigned formatLine(
const AnnotatedLine &Line,
unsigned FirstIndent,
1121 unsigned FirstStartColumn,
bool DryRun)
override {
1123 Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
1128 State.Stack.back().BreakBeforeParameter =
true;
1131 return analyzeSolutionSpace(State, DryRun);
1135 struct CompareLineStatePointers {
1136 bool operator()(LineState *obj1, LineState *obj2)
const {
1137 return *obj1 < *obj2;
1146 typedef std::pair<unsigned, unsigned> OrderedPenalty;
1151 StateNode(
const LineState &State,
bool NewLine, StateNode *Previous)
1160 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
1163 typedef std::priority_queue<QueueItem, SmallVector<QueueItem>,
1164 std::greater<QueueItem>>
1175 unsigned analyzeSolutionSpace(LineState &InitialState,
bool DryRun) {
1176 std::set<LineState *, CompareLineStatePointers> Seen;
1184 StateNode *RootNode =
1185 new (Allocator.Allocate()) StateNode(InitialState,
false,
nullptr);
1186 Queue.push(QueueItem(OrderedPenalty(0, Count), RootNode));
1189 unsigned Penalty = 0;
1192 while (!Queue.empty()) {
1194 if (Count > 25000000)
1197 Penalty = Queue.top().first.first;
1198 StateNode *
Node = Queue.top().second;
1199 if (!
Node->State.NextToken) {
1200 LLVM_DEBUG(llvm::dbgs()
1201 <<
"\n---\nPenalty for line: " << Penalty <<
"\n");
1209 Node->State.IgnoreStackForComparison =
true;
1211 if (!Seen.insert(&
Node->State).second) {
1218 addNextStateToQueue(Penalty,
Node,
false, &Count, &Queue);
1220 addNextStateToQueue(Penalty,
Node,
true, &Count, &Queue);
1223 if (Queue.empty()) {
1226 LLVM_DEBUG(llvm::dbgs() <<
"Could not find a solution.\n");
1232 reconstructPath(InitialState, Queue.top().second);
1234 LLVM_DEBUG(llvm::dbgs()
1235 <<
"Total number of analyzed states: " << Count <<
"\n");
1236 LLVM_DEBUG(llvm::dbgs() <<
"---\n");
1245 void addNextStateToQueue(
unsigned Penalty, StateNode *PreviousNode,
1246 bool NewLine,
unsigned *Count, QueueType *Queue) {
1252 StateNode *
Node =
new (Allocator.Allocate())
1253 StateNode(PreviousNode->State,
NewLine, PreviousNode);
1254 if (!formatChildren(
Node->State,
NewLine,
true, Penalty))
1259 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count),
Node));
1265 void reconstructPath(LineState &State, StateNode *Best) {
1268 while (Best->Previous) {
1269 Path.push_back(Best);
1270 Best = Best->Previous;
1272 for (
const auto &
Node : llvm::reverse(Path)) {
1273 unsigned Penalty = 0;
1274 formatChildren(State,
Node->NewLine,
false, Penalty);
1275 Penalty +=
Indenter->addTokenToState(State,
Node->NewLine,
false);
1278 printLineState(
Node->Previous->State);
1279 if (
Node->NewLine) {
1280 llvm::dbgs() <<
"Penalty for placing "
1281 <<
Node->Previous->State.NextToken->Tok.getName()
1282 <<
" on a new line: " << Penalty <<
"\n";
1288 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1295 int AdditionalIndent,
bool FixBadIndentation,
unsigned FirstStartColumn,
1296 unsigned NextStartColumn,
unsigned LastStartColumn) {
1297 LineJoiner Joiner(Style, Keywords, Lines);
1300 std::pair<const SmallVectorImpl<AnnotatedLine *> *,
unsigned> CacheKey(
1301 &Lines, AdditionalIndent);
1302 auto CacheIt = PenaltyCache.find(CacheKey);
1303 if (DryRun && CacheIt != PenaltyCache.end())
1304 return CacheIt->second;
1306 assert(!Lines.empty());
1307 unsigned Penalty = 0;
1308 LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
1317 bool FirstLine =
true;
1319 Joiner.getNextMergedLine(DryRun, IndentTracker);
1320 Line; PrevPrevLine = PreviousLine, PreviousLine = Line, Line = NextLine,
1321 FirstLine =
false) {
1322 assert(Line->First);
1324 unsigned Indent = IndentTracker.getIndent();
1330 bool PreviousRBrace =
1331 PreviousLine && PreviousLine->
startsWith(tok::r_brace);
1332 bool ContinueFormatting =
1333 TheLine.
Level > RangeMinLevel ||
1334 (TheLine.
Level == RangeMinLevel && !PreviousRBrace &&
1337 bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
1339 bool ShouldFormat = TheLine.
Affected || FixIndentation;
1350 bool LastLine = TheLine.
First->
is(tok::eof);
1351 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,
1352 LastLine ? LastStartColumn : NextStartColumn + Indent);
1355 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1356 unsigned ColumnLimit = getColumnLimit(TheLine.
InPPDirective, NextLine);
1357 bool FitsIntoOneLine =
1361 (!Style.isJavaScript() || !Style.JavaScriptWrapImports)) ||
1362 (Style.isCSharp() &&
1364 if (Style.ColumnLimit == 0) {
1365 NoColumnLimitLineFormatter(
Indenter, Whitespaces, Style,
this)
1366 .formatLine(TheLine, NextStartColumn + Indent,
1367 FirstLine ? FirstStartColumn : 0, DryRun);
1368 }
else if (FitsIntoOneLine) {
1369 Penalty += NoLineBreakFormatter(
Indenter, Whitespaces, Style,
this)
1370 .formatLine(TheLine, NextStartColumn + Indent,
1371 FirstLine ? FirstStartColumn : 0, DryRun);
1373 Penalty += OptimizingLineFormatter(
Indenter, Whitespaces, Style,
this)
1374 .formatLine(TheLine, NextStartColumn + Indent,
1375 FirstLine ? FirstStartColumn : 0, DryRun);
1377 RangeMinLevel = std::min(RangeMinLevel, TheLine.
Level);
1383 if (!Tok->Children.empty())
1384 format(Tok->Children, DryRun);
1389 bool StartsNewLine =
1392 IndentTracker.adjustToUnmodifiedLine(TheLine);
1394 bool ReformatLeadingWhitespace =
1395 StartsNewLine && ((PreviousLine && PreviousLine->
Affected) ||
1398 if (ReformatLeadingWhitespace) {
1399 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines,
1403 Whitespaces->addUntouchableToken(*TheLine.
First,
1409 Whitespaces->addUntouchableToken(*Tok, TheLine.
InPPDirective);
1411 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1415 markFinalized(TheLine.
First);
1417 PenaltyCache[CacheKey] = Penalty;
1426 const auto &RootToken = *Line.
First;
1430 if (RootToken.is(tok::r_brace) &&
1432 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&
1435 Newlines = std::min(Newlines, 1u);
1438 if (!PreviousLine && Line.
Level > 0)
1439 Newlines = std::min(Newlines, 1u);
1440 if (Newlines == 0 && !RootToken.IsFirst)
1442 if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
1447 PreviousLine->
Last->
is(tok::l_brace) &&
1451 !startsExternCBlock(*PreviousLine)) {
1456 if (PreviousLine && RootToken.isAccessSpecifier()) {
1463 Newlines = std::max(RootToken.NewlinesBefore, 1u);
1466 if (PreviousLine->
Last->
isOneOf(tok::semi, tok::r_brace) && Newlines <= 1)
1473 if (PreviousLine->
Last->
is(tok::comment))
1476 previousToken = PreviousLine->
Last;
1477 if ((!previousToken || !previousToken->
is(tok::l_brace)) && Newlines <= 1)
1485 (!PreviousLine->
InPPDirective || !RootToken.HasUnescapedNewline)) {
1488 if (!RootToken.isAccessSpecifier()) {
1494 Newlines = std::max(Newlines, 1u);
1497 if (RootToken.is(tok::r_brace))
1500 Newlines = std::max(Newlines, 2u);
1509void UnwrappedLineFormatter::formatFirstToken(
1510 const AnnotatedLine &Line,
const AnnotatedLine *PreviousLine,
1511 const AnnotatedLine *PrevPrevLine,
1513 unsigned NewlineIndent) {
1514 FormatToken &RootToken = *Line.First;
1515 if (RootToken.is(tok::eof)) {
1516 unsigned Newlines = std::min(RootToken.NewlinesBefore, 1u);
1517 unsigned TokenIndent = Newlines ? NewlineIndent : 0;
1518 Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
1523 const auto Newlines =
1525 ? RootToken.NewlinesBefore
1528 Indent = NewlineIndent;
1532 if (!Style.isJavaScript() &&
1539 Whitespaces->replaceWhitespace(RootToken, Newlines, Indent, Indent,
1541 Line.InPPDirective &&
1542 !RootToken.HasUnescapedNewline);
1546UnwrappedLineFormatter::getColumnLimit(
bool InPPDirective,
1547 const AnnotatedLine *NextLine)
const {
1550 bool ContinuesPPDirective =
1555 (NextLine->InPPDirective &&
1558 !NextLine->First->HasUnescapedNewline));
1559 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()))