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 ||
Line.IsContinuation)
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 auto *LastNonComment =
Line->getLastNonComment();
352 assert(LastNonComment);
353 return isRecordLBrace(*LastNonComment);
360 bool MergeShortFunctions = ShouldMergeShortFunctions();
362 const auto *FirstNonComment = TheLine->getFirstNonComment();
363 if (!FirstNonComment)
368 if (Style.CompactNamespaces) {
369 if (
const auto *NSToken = TheLine->First->getNamespaceToken()) {
371 assert(TheLine->MatchingClosingBlockLineIndex > 0);
372 for (
auto ClosingLineIndex = TheLine->MatchingClosingBlockLineIndex - 1;
374 ClosingLineIndex == I[J]->MatchingClosingBlockLineIndex &&
375 I[J]->
Last->TotalLength < Limit;
376 ++J, --ClosingLineIndex) {
377 Limit -= I[J]->Last->TotalLength;
381 auto *ClosingLine = AnnotatedLines.begin() + ClosingLineIndex + 1;
382 const int OutdentBy = I[J]->Level - TheLine->Level;
383 assert(OutdentBy >= 0);
384 for (
auto *CompactedLine = I + J; CompactedLine <= ClosingLine;
386 if (!(*CompactedLine)->InPPDirective) {
387 const int Level = (*CompactedLine)->Level;
388 (*CompactedLine)->Level = std::max(Level - OutdentBy, 0);
395 if (
auto nsToken = getMatchingNamespaceToken(TheLine, AnnotatedLines)) {
397 unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
398 for (; I + 1 + i != E &&
399 nsToken->TokenText ==
400 getMatchingNamespaceTokenText(I[i + 1], AnnotatedLines) &&
401 openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;
402 i++, --openingLine) {
404 I[i + 1]->First->SpacesRequiredBefore =
405 I[i]->Last->isNot(tok::r_brace);
408 IndentTracker.nextLine(*I[i + 1]);
414 const auto *LastNonComment = TheLine->getLastNonComment();
415 assert(LastNonComment);
420 if (LastNonComment->is(TT_FunctionLBrace) &&
421 TheLine->First != LastNonComment) {
422 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.endsWith(tok::l_brace)) {
801 Line.First->is(TT_BlockLBrace)) {
805 if (IsSplitBlock &&
Line.First ==
Line.Last &&
806 I > AnnotatedLines.begin() &&
807 (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {
810 FormatToken *Tok = I[1]->First;
811 auto ShouldMerge = [Tok]() {
812 if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore)
814 const FormatToken *Next = Tok->getNextNonComment();
815 return !Next || Next->is(tok::semi);
820 Tok->SpacesRequiredBefore =
821 (Style.SpaceInEmptyBlock ||
Line.Last->is(tok::comment)) ? 1 : 0;
822 Tok->CanBreakBefore =
true;
824 }
else if (Limit != 0 && !
Line.startsWithNamespace() &&
825 !startsExternCBlock(
Line)) {
827 if (isRecordLBrace(*
Line.Last))
833 Limit = limitConsideringMacros(I + 2, E, Limit);
835 if (!nextTwoLinesFitInto(I, Limit))
840 if (I[1]->
Last->is(TT_LineComment))
850 if (Tok->isNot(tok::r_brace))
854 if (Tok->Next && Tok->Next->is(tok::kw_else))
863 if (
Line.First ==
Line.Last &&
Line.First->isNot(TT_FunctionLBrace) &&
864 Style.BraceWrapping.AfterControlStatement ==
871 }
else if (I[1]->
First->is(tok::l_brace)) {
872 if (I[1]->
Last->is(TT_LineComment))
876 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I)))
879 unsigned MergedLines = 0;
881 (I[1]->First == I[1]->Last && I + 2 != E &&
882 I[2]->First->is(tok::r_brace))) {
883 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
897 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
898 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
900 if (I[0]->InPPDirective && I + 1 != E &&
901 !I[1]->
First->HasUnescapedNewline && I[1]->First->isNot(tok::eof)) {
902 return Limit < 2 ? 0 : Limit - 2;
907 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
909 if (I[1]->
First->MustBreakBefore || I[2]->First->MustBreakBefore)
911 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
914 bool containsMustBreak(
const AnnotatedLine *
Line) {
918 for (
const FormatToken *Tok =
Line->First->Next; Tok; Tok = Tok->Next)
919 if (Tok->MustBreakBefore)
924 void join(AnnotatedLine &A,
const AnnotatedLine &B) {
925 assert(!A.Last->Next);
926 assert(!B.First->Previous);
929 A.Last->Next = B.First;
930 B.First->Previous = A.Last;
931 B.First->CanBreakBefore =
true;
932 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
933 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
934 Tok->TotalLength += LengthA;
939 const FormatStyle &Style;
940 const AdditionalKeywords &Keywords;
941 const SmallVectorImpl<AnnotatedLine *>::const_iterator End;
943 SmallVectorImpl<AnnotatedLine *>::const_iterator Next;
944 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines;
947static void markFinalized(FormatToken *Tok) {
948 if (Tok->is(tok::hash) && !Tok->Previous && Tok->Next &&
949 Tok->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,
950 tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,
951 tok::pp_else, tok::pp_endif)) {
954 for (; Tok; Tok = Tok->Next) {
967 Tok->SpacesRequiredBefore = 0;
968 if (!Tok->MustBreakBeforeFinalized)
969 Tok->MustBreakBefore = 0;
971 Tok->Finalized =
true;
977static void printLineState(
const LineState &State) {
978 llvm::dbgs() <<
"State: ";
979 for (
const ParenState &
P : State.Stack) {
980 llvm::dbgs() << (
P.Tok ?
P.Tok->TokenText :
"F") <<
"|" <<
P.Indent <<
"|"
981 <<
P.LastSpace <<
"|" <<
P.NestedBlockIndent <<
" ";
983 llvm::dbgs() << State.NextToken->TokenText <<
"\n";
990 LineFormatter(ContinuationIndenter *
Indenter, WhitespaceManager *Whitespaces,
991 const FormatStyle &Style,
992 UnwrappedLineFormatter *BlockFormatter)
994 BlockFormatter(BlockFormatter) {}
995 virtual ~LineFormatter() {}
1000 virtual unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1001 unsigned FirstStartColumn,
bool DryRun) = 0;
1024 bool formatChildren(LineState &State,
bool NewLine,
bool DryRun,
1025 unsigned &Penalty) {
1026 const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
1027 bool HasLBrace = LBrace && LBrace->is(tok::l_brace) && LBrace->is(
BK_Block);
1028 FormatToken &
Previous = *State.NextToken->Previous;
1029 if (
Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent)) {
1036 const ParenState &
P = State.Stack.back();
1038 int AdditionalIndent =
1039 P.Indent -
Previous.Children[0]->Level * Style.IndentWidth;
1041 BlockFormatter->format(
Previous.Children, DryRun, AdditionalIndent,
1046 if (
Previous.Children[0]->First->MustBreakBefore)
1057 const AnnotatedLine *Child =
Previous.Children[0];
1059 if (Child->Last->isTrailingComment())
1064 if (Style.ColumnLimit > 0 &&
1065 Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) {
1070 Whitespaces->replaceWhitespace(
1071 *Child->First, 0, 1,
1072 State.Column,
false,
1073 State.Line->InPPDirective);
1076 formatLine(*Child, State.Column + 1, 0, DryRun);
1078 markFinalized(Child->First);
1080 State.Column += 1 + Child->Last->TotalLength;
1087 WhitespaceManager *Whitespaces;
1088 const FormatStyle &Style;
1089 UnwrappedLineFormatter *BlockFormatter;
1093class NoColumnLimitLineFormatter :
public LineFormatter {
1095 NoColumnLimitLineFormatter(ContinuationIndenter *
Indenter,
1096 WhitespaceManager *Whitespaces,
1097 const FormatStyle &Style,
1098 UnwrappedLineFormatter *BlockFormatter)
1099 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1103 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1104 unsigned FirstStartColumn,
bool DryRun)
override {
1106 LineState State =
Indenter->getInitialState(FirstIndent, FirstStartColumn,
1108 while (State.NextToken) {
1111 (
Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
1112 unsigned Penalty = 0;
1113 formatChildren(State, Newline,
false, Penalty);
1114 Indenter->addTokenToState(State, Newline,
false);
1121class NoLineBreakFormatter :
public LineFormatter {
1123 NoLineBreakFormatter(ContinuationIndenter *
Indenter,
1124 WhitespaceManager *Whitespaces,
const FormatStyle &Style,
1125 UnwrappedLineFormatter *BlockFormatter)
1126 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1129 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1130 unsigned FirstStartColumn,
bool DryRun)
override {
1131 unsigned Penalty = 0;
1133 Indenter->getInitialState(FirstIndent, FirstStartColumn, &
Line, DryRun);
1134 while (State.NextToken) {
1135 formatChildren(State,
false, DryRun, Penalty);
1137 State, State.NextToken->MustBreakBefore, DryRun);
1144class OptimizingLineFormatter :
public LineFormatter {
1146 OptimizingLineFormatter(ContinuationIndenter *
Indenter,
1147 WhitespaceManager *Whitespaces,
1148 const FormatStyle &Style,
1149 UnwrappedLineFormatter *BlockFormatter)
1150 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1154 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1155 unsigned FirstStartColumn,
bool DryRun)
override {
1157 Indenter->getInitialState(FirstIndent, FirstStartColumn, &
Line, DryRun);
1162 State.Stack.back().BreakBeforeParameter =
true;
1165 return analyzeSolutionSpace(State, DryRun);
1169 struct CompareLineStatePointers {
1170 bool operator()(LineState *obj1, LineState *obj2)
const {
1171 return *obj1 < *obj2;
1180 typedef std::pair<unsigned, unsigned> OrderedPenalty;
1185 StateNode(
const LineState &State,
bool NewLine, StateNode *Previous)
1194 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
1197 typedef std::priority_queue<QueueItem, SmallVector<QueueItem>,
1198 std::greater<QueueItem>>
1209 unsigned analyzeSolutionSpace(LineState &InitialState,
bool DryRun) {
1210 std::set<LineState *, CompareLineStatePointers> Seen;
1218 StateNode *RootNode =
1219 new (Allocator.Allocate()) StateNode(InitialState,
false,
nullptr);
1220 Queue.push(QueueItem(OrderedPenalty(0, Count), RootNode));
1223 unsigned Penalty = 0;
1226 while (!Queue.empty()) {
1228 if (Count > 25'000'000)
1231 Penalty = Queue.top().first.first;
1232 StateNode *
Node = Queue.top().second;
1233 if (!
Node->State.NextToken) {
1234 LLVM_DEBUG(llvm::dbgs()
1235 <<
"\n---\nPenalty for line: " << Penalty <<
"\n");
1243 Node->State.IgnoreStackForComparison =
true;
1245 if (!Seen.insert(&
Node->State).second) {
1252 addNextStateToQueue(Penalty,
Node,
false, &Count, &Queue);
1254 addNextStateToQueue(Penalty,
Node,
true, &Count, &Queue);
1257 if (Queue.empty()) {
1260 LLVM_DEBUG(llvm::dbgs() <<
"Could not find a solution.\n");
1266 reconstructPath(InitialState, Queue.top().second);
1268 LLVM_DEBUG(llvm::dbgs()
1269 <<
"Total number of analyzed states: " << Count <<
"\n");
1270 LLVM_DEBUG(llvm::dbgs() <<
"---\n");
1279 void addNextStateToQueue(
unsigned Penalty, StateNode *PreviousNode,
1280 bool NewLine,
unsigned *Count, QueueType *Queue) {
1286 StateNode *
Node =
new (Allocator.Allocate())
1287 StateNode(PreviousNode->State,
NewLine, PreviousNode);
1288 if (!formatChildren(
Node->State,
NewLine,
true, Penalty))
1293 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count),
Node));
1299 void reconstructPath(LineState &State, StateNode *Best) {
1302 while (Best->Previous) {
1303 Path.push_back(Best);
1304 Best = Best->Previous;
1306 for (
const auto &
Node : llvm::reverse(Path)) {
1307 unsigned Penalty = 0;
1308 formatChildren(State,
Node->NewLine,
false, Penalty);
1309 Penalty +=
Indenter->addTokenToState(State,
Node->NewLine,
false);
1312 printLineState(
Node->Previous->State);
1313 if (
Node->NewLine) {
1314 llvm::dbgs() <<
"Penalty for placing "
1315 <<
Node->Previous->State.NextToken->Tok.getName()
1316 <<
" on a new line: " << Penalty <<
"\n";
1322 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1329 int AdditionalIndent,
bool FixBadIndentation,
unsigned FirstStartColumn,
1330 unsigned NextStartColumn,
unsigned LastStartColumn) {
1331 LineJoiner Joiner(Style, Keywords, Lines);
1334 std::pair<const SmallVectorImpl<AnnotatedLine *> *,
unsigned> CacheKey(
1335 &Lines, AdditionalIndent);
1336 auto CacheIt = PenaltyCache.find(CacheKey);
1337 if (DryRun && CacheIt != PenaltyCache.end())
1338 return CacheIt->second;
1340 assert(!Lines.empty());
1341 unsigned Penalty = 0;
1342 LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
1351 bool FirstLine =
true;
1353 Joiner.getNextMergedLine(DryRun, IndentTracker);
1354 Line; PrevPrevLine = PreviousLine, PreviousLine =
Line,
Line = NextLine,
1355 FirstLine =
false) {
1356 assert(
Line->First);
1358 unsigned Indent = IndentTracker.getIndent();
1364 bool PreviousRBrace =
1365 PreviousLine && PreviousLine->
startsWith(tok::r_brace);
1366 bool ContinueFormatting =
1367 TheLine.
Level > RangeMinLevel ||
1368 (TheLine.
Level == RangeMinLevel && !PreviousRBrace &&
1371 bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
1373 bool ShouldFormat = TheLine.
Affected || FixIndentation;
1384 bool LastLine = TheLine.
First->
is(tok::eof);
1385 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,
1386 LastLine ? LastStartColumn : NextStartColumn + Indent);
1389 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1390 unsigned ColumnLimit = getColumnLimit(TheLine.
InPPDirective, NextLine);
1391 bool FitsIntoOneLine =
1395 (!Style.isJavaScript() || !Style.JavaScriptWrapImports)) ||
1396 (Style.isCSharp() &&
1398 if (Style.ColumnLimit == 0) {
1399 NoColumnLimitLineFormatter(
Indenter, Whitespaces, Style,
this)
1400 .formatLine(TheLine, NextStartColumn + Indent,
1401 FirstLine ? FirstStartColumn : 0, DryRun);
1402 }
else if (FitsIntoOneLine) {
1403 Penalty += NoLineBreakFormatter(
Indenter, Whitespaces, Style,
this)
1404 .formatLine(TheLine, NextStartColumn + Indent,
1405 FirstLine ? FirstStartColumn : 0, DryRun);
1407 Penalty += OptimizingLineFormatter(
Indenter, Whitespaces, Style,
this)
1408 .formatLine(TheLine, NextStartColumn + Indent,
1409 FirstLine ? FirstStartColumn : 0, DryRun);
1411 RangeMinLevel = std::min(RangeMinLevel, TheLine.
Level);
1417 if (!Tok->Children.empty())
1418 format(Tok->Children, DryRun);
1423 bool StartsNewLine =
1426 IndentTracker.adjustToUnmodifiedLine(TheLine);
1428 bool ReformatLeadingWhitespace =
1429 StartsNewLine && ((PreviousLine && PreviousLine->
Affected) ||
1432 if (ReformatLeadingWhitespace) {
1433 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines,
1437 Whitespaces->addUntouchableToken(*TheLine.
First,
1443 Whitespaces->addUntouchableToken(*Tok, TheLine.
InPPDirective);
1445 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1449 markFinalized(TheLine.
First);
1451 PenaltyCache[CacheKey] = Penalty;
1460 const auto &RootToken = *
Line.First;
1464 if (RootToken.is(tok::r_brace) &&
1466 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&
1469 Newlines = std::min(Newlines, 1u);
1472 if (!PreviousLine &&
Line.Level > 0)
1473 Newlines = std::min(Newlines, 1u);
1474 if (Newlines == 0 && !RootToken.IsFirst)
1476 if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
1481 PreviousLine->
Last->
is(tok::l_brace) &&
1485 !startsExternCBlock(*PreviousLine)) {
1490 if (PreviousLine && RootToken.isAccessSpecifier()) {
1497 Newlines = std::max(RootToken.NewlinesBefore, 1u);
1500 if (PreviousLine->
Last->
isOneOf(tok::semi, tok::r_brace) && Newlines <= 1)
1507 if (PreviousLine->
Last->
is(tok::comment))
1510 previousToken = PreviousLine->
Last;
1511 if ((!previousToken || previousToken->
isNot(tok::l_brace)) &&
1521 (!PreviousLine->
InPPDirective || !RootToken.HasUnescapedNewline)) {
1524 if (!RootToken.isAccessSpecifier()) {
1530 Newlines = std::max(Newlines, 1u);
1533 if (RootToken.is(tok::r_brace))
1536 Newlines = std::max(Newlines, 2u);
1545void UnwrappedLineFormatter::formatFirstToken(
1546 const AnnotatedLine &
Line,
const AnnotatedLine *PreviousLine,
1547 const AnnotatedLine *PrevPrevLine,
1549 unsigned NewlineIndent) {
1550 FormatToken &RootToken = *
Line.First;
1551 if (RootToken.is(tok::eof)) {
1553 std::min(RootToken.NewlinesBefore,
1554 Style.KeepEmptyLinesAtEOF ? Style.MaxEmptyLinesToKeep + 1 : 1);
1555 unsigned TokenIndent = Newlines ? NewlineIndent : 0;
1556 Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
1561 if (RootToken.Newlines < 0) {
1562 RootToken.Newlines =
1564 assert(RootToken.Newlines >= 0);
1567 if (RootToken.Newlines > 0)
1568 Indent = NewlineIndent;
1572 if (!Style.isJavaScript() &&
1579 Whitespaces->replaceWhitespace(RootToken, RootToken.Newlines, Indent, Indent,
1581 Line.InPPDirective &&
1582 !RootToken.HasUnescapedNewline);
1586UnwrappedLineFormatter::getColumnLimit(
bool InPPDirective,
1587 const AnnotatedLine *NextLine)
const {
1590 bool ContinuesPPDirective =
1595 (NextLine->InPPDirective &&
1598 !NextLine->First->HasUnescapedNewline));
1599 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(const_cast< Expr * >(T -> getSizeExpr())))