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->First->is(TT_SwitchExpressionLabel)) {
519 return Style.AllowShortCaseExpressionOnASingleLine
520 ? tryMergeShortCaseLabels(I, E, Limit)
524 if (TheLine->Last->is(tok::l_brace)) {
525 bool ShouldMerge =
false;
527 if (TheLine->Last->is(TT_EnumLBrace)) {
528 ShouldMerge = Style.AllowShortEnumsOnASingleLine;
529 }
else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) {
530 ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
531 }
else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
535 ShouldMerge = !Style.BraceWrapping.AfterClass ||
536 (NextLine.First->is(tok::r_brace) &&
537 !Style.BraceWrapping.SplitEmptyRecord);
538 }
else if (TheLine->InPPDirective ||
539 !TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
543 ShouldMerge = !Style.BraceWrapping.AfterFunction ||
544 (NextLine.First->is(tok::r_brace) &&
545 !Style.BraceWrapping.SplitEmptyFunction);
547 return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;
551 if (NextLine.First->is(TT_FunctionLBrace) &&
552 Style.BraceWrapping.AfterFunction) {
553 if (NextLine.Last->is(TT_LineComment))
557 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
561 unsigned MergedLines = 0;
562 if (MergeShortFunctions ||
564 NextLine.First == NextLine.Last && I + 2 != E &&
565 I[2]->First->is(tok::r_brace))) {
566 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
574 auto IsElseLine = [&TheLine]() ->
bool {
575 const FormatToken *
First = TheLine->First;
576 if (
First->is(tok::kw_else))
579 return First->is(tok::r_brace) &&
First->Next &&
580 First->Next->is(tok::kw_else);
582 if (TheLine->First->is(tok::kw_if) ||
583 (IsElseLine() && (Style.AllowShortIfStatementsOnASingleLine ==
585 return Style.AllowShortIfStatementsOnASingleLine
586 ? tryMergeSimpleControlStatement(I, E, Limit)
589 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do,
591 return Style.AllowShortLoopsOnASingleLine
592 ? tryMergeSimpleControlStatement(I, E, Limit)
595 if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
596 return Style.AllowShortCaseLabelsOnASingleLine
597 ? tryMergeShortCaseLabels(I, E, Limit)
600 if (TheLine->InPPDirective &&
601 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
602 return tryMergeSimplePPDirective(I, E, Limit);
608 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
609 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
613 if (I + 2 != E && I[2]->InPPDirective && !I[2]->
First->HasUnescapedNewline)
615 if (1 + I[1]->
Last->TotalLength > Limit)
620 unsigned tryMergeSimpleControlStatement(
621 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
622 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
unsigned Limit) {
625 if (Style.BraceWrapping.AfterControlStatement ==
627 I[1]->First->is(tok::l_brace) &&
631 if (I[1]->InPPDirective != (*I)->InPPDirective ||
632 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) {
635 Limit = limitConsideringMacros(I + 1, E, Limit);
636 AnnotatedLine &
Line = **I;
637 if (
Line.First->isNot(tok::kw_do) &&
Line.First->isNot(tok::kw_else) &&
638 Line.Last->isNot(tok::kw_else) &&
Line.Last->isNot(tok::r_paren)) {
642 if (
Line.First->is(tok::kw_do) &&
Line.Last->isNot(tok::kw_do))
644 if (1 + I[1]->
Last->TotalLength > Limit)
647 if (I[1]->
First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
648 TT_ForEachMacro, TT_LineComment)) {
652 if (Style.AllowShortIfStatementsOnASingleLine ==
654 if (I + 2 != E &&
Line.startsWith(tok::kw_if) &&
655 I[2]->First->is(tok::kw_else)) {
663 tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
664 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
666 if (Limit == 0 || I + 1 == E ||
667 I[1]->
First->isOneOf(tok::kw_case, tok::kw_default)) {
670 if (I[0]->
Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))
672 unsigned NumStmts = 0;
674 bool EndsWithComment =
false;
675 bool InPPDirective = I[0]->InPPDirective;
676 bool InMacroBody = I[0]->InMacroBody;
677 const unsigned Level = I[0]->Level;
678 for (; NumStmts < 3; ++NumStmts) {
679 if (I + 1 + NumStmts == E)
681 const AnnotatedLine *
Line = I[1 + NumStmts];
682 if (
Line->InPPDirective != InPPDirective)
684 if (
Line->InMacroBody != InMacroBody)
686 if (
Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
688 if (
Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
693 if (
Line->First->is(tok::comment)) {
694 if (Level !=
Line->Level)
696 SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts;
697 for (; J != E; ++J) {
699 if (
Line->InPPDirective != InPPDirective)
701 if (
Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
703 if (
Line->First->isNot(tok::comment) || Level !=
Line->Level)
708 if (
Line->Last->is(tok::comment))
709 EndsWithComment =
true;
710 Length += I[1 + NumStmts]->Last->TotalLength + 1;
712 if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
718 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
719 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
725 AnnotatedLine &
Line = **I;
731 Line.First->isOneOf(tok::at, tok::minus, tok::plus)) {
737 if (
Line.First->is(tok::kw_case) ||
738 (
Line.First->Next &&
Line.First->Next->is(tok::kw_else))) {
742 if (
Line.First->is(tok::kw_default)) {
743 const FormatToken *Tok =
Line.First->getNextNonComment();
744 if (Tok && Tok->is(tok::colon))
748 auto IsCtrlStmt = [](
const auto &
Line) {
749 return Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
750 tok::kw_do, tok::kw_for, TT_ForEachMacro);
753 const bool IsSplitBlock =
756 I[1]->First->isNot(tok::r_brace));
758 if (IsCtrlStmt(
Line) ||
759 Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
760 tok::kw___finally, tok::r_brace,
761 Keywords.kw___except)) {
766 if (!Style.AllowShortIfStatementsOnASingleLine &&
767 Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
768 !Style.BraceWrapping.AfterControlStatement &&
769 I[1]->First->isNot(tok::r_brace)) {
772 if (!Style.AllowShortIfStatementsOnASingleLine &&
773 Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
774 Style.BraceWrapping.AfterControlStatement ==
776 I + 2 != E && I[2]->First->isNot(tok::r_brace)) {
779 if (!Style.AllowShortLoopsOnASingleLine &&
780 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
782 !Style.BraceWrapping.AfterControlStatement &&
783 I[1]->First->isNot(tok::r_brace)) {
786 if (!Style.AllowShortLoopsOnASingleLine &&
787 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
789 Style.BraceWrapping.AfterControlStatement ==
791 I + 2 != E && I[2]->First->isNot(tok::r_brace)) {
799 if (
Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
800 Keywords.kw___except, tok::kw___finally)) {
805 if (
Line.endsWith(tok::l_brace)) {
807 Line.First->is(TT_BlockLBrace)) {
811 if (IsSplitBlock &&
Line.First ==
Line.Last &&
812 I > AnnotatedLines.begin() &&
813 (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {
816 FormatToken *Tok = I[1]->First;
817 auto ShouldMerge = [Tok]() {
818 if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore)
820 const FormatToken *Next = Tok->getNextNonComment();
821 return !Next || Next->is(tok::semi);
826 Tok->SpacesRequiredBefore =
827 (Style.SpaceInEmptyBlock ||
Line.Last->is(tok::comment)) ? 1 : 0;
828 Tok->CanBreakBefore =
true;
830 }
else if (Limit != 0 && !
Line.startsWithNamespace() &&
831 !startsExternCBlock(
Line)) {
833 if (isRecordLBrace(*
Line.Last))
839 Limit = limitConsideringMacros(I + 2, E, Limit);
841 if (!nextTwoLinesFitInto(I, Limit))
846 if (I[1]->
Last->is(TT_LineComment))
856 if (Tok->isNot(tok::r_brace))
860 if (Tok->Next && Tok->Next->is(tok::kw_else))
869 if (
Line.First ==
Line.Last &&
Line.First->isNot(TT_FunctionLBrace) &&
870 Style.BraceWrapping.AfterControlStatement ==
877 }
else if (I[1]->
First->is(tok::l_brace)) {
878 if (I[1]->
Last->is(TT_LineComment))
882 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I)))
885 unsigned MergedLines = 0;
887 (I[1]->First == I[1]->Last && I + 2 != E &&
888 I[2]->First->is(tok::r_brace))) {
889 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
903 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
904 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
906 if (I[0]->InPPDirective && I + 1 != E &&
907 !I[1]->
First->HasUnescapedNewline && I[1]->First->isNot(tok::eof)) {
908 return Limit < 2 ? 0 : Limit - 2;
913 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
915 if (I[1]->
First->MustBreakBefore || I[2]->First->MustBreakBefore)
917 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
920 bool containsMustBreak(
const AnnotatedLine *
Line) {
924 for (
const FormatToken *Tok =
Line->First->Next; Tok; Tok = Tok->Next)
925 if (Tok->MustBreakBefore)
930 void join(AnnotatedLine &A,
const AnnotatedLine &B) {
931 assert(!A.Last->Next);
932 assert(!B.First->Previous);
935 A.Last->Next = B.First;
936 B.First->Previous = A.Last;
937 B.First->CanBreakBefore =
true;
938 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
939 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
940 Tok->TotalLength += LengthA;
945 const FormatStyle &Style;
946 const AdditionalKeywords &Keywords;
947 const SmallVectorImpl<AnnotatedLine *>::const_iterator End;
949 SmallVectorImpl<AnnotatedLine *>::const_iterator Next;
950 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines;
953static void markFinalized(FormatToken *Tok) {
954 if (Tok->is(tok::hash) && !Tok->Previous && Tok->Next &&
955 Tok->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,
956 tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,
957 tok::pp_else, tok::pp_endif)) {
960 for (; Tok; Tok = Tok->Next) {
973 Tok->SpacesRequiredBefore = 0;
974 if (!Tok->MustBreakBeforeFinalized)
975 Tok->MustBreakBefore = 0;
977 Tok->Finalized =
true;
983static void printLineState(
const LineState &State) {
984 llvm::dbgs() <<
"State: ";
985 for (
const ParenState &
P : State.Stack) {
986 llvm::dbgs() << (
P.Tok ?
P.Tok->TokenText :
"F") <<
"|" <<
P.Indent <<
"|"
987 <<
P.LastSpace <<
"|" <<
P.NestedBlockIndent <<
" ";
989 llvm::dbgs() << State.NextToken->TokenText <<
"\n";
996 LineFormatter(ContinuationIndenter *
Indenter, WhitespaceManager *Whitespaces,
997 const FormatStyle &Style,
998 UnwrappedLineFormatter *BlockFormatter)
1000 BlockFormatter(BlockFormatter) {}
1001 virtual ~LineFormatter() {}
1006 virtual unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1007 unsigned FirstStartColumn,
bool DryRun) = 0;
1030 bool formatChildren(LineState &State,
bool NewLine,
bool DryRun,
1031 unsigned &Penalty) {
1032 const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
1033 bool HasLBrace = LBrace && LBrace->is(tok::l_brace) && LBrace->is(
BK_Block);
1034 FormatToken &
Previous = *State.NextToken->Previous;
1035 if (
Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent)) {
1042 const ParenState &
P = State.Stack.back();
1044 int AdditionalIndent =
1045 P.Indent -
Previous.Children[0]->Level * Style.IndentWidth;
1047 BlockFormatter->format(
Previous.Children, DryRun, AdditionalIndent,
1052 if (
Previous.Children[0]->First->MustBreakBefore)
1063 const AnnotatedLine *Child =
Previous.Children[0];
1065 if (Child->Last->isTrailingComment())
1070 if (Style.ColumnLimit > 0 &&
1071 Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) {
1076 Whitespaces->replaceWhitespace(
1077 *Child->First, 0, 1,
1078 State.Column,
false,
1079 State.Line->InPPDirective);
1082 formatLine(*Child, State.Column + 1, 0, DryRun);
1084 markFinalized(Child->First);
1086 State.Column += 1 + Child->Last->TotalLength;
1093 WhitespaceManager *Whitespaces;
1094 const FormatStyle &Style;
1095 UnwrappedLineFormatter *BlockFormatter;
1099class NoColumnLimitLineFormatter :
public LineFormatter {
1101 NoColumnLimitLineFormatter(ContinuationIndenter *
Indenter,
1102 WhitespaceManager *Whitespaces,
1103 const FormatStyle &Style,
1104 UnwrappedLineFormatter *BlockFormatter)
1105 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1109 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1110 unsigned FirstStartColumn,
bool DryRun)
override {
1112 LineState State =
Indenter->getInitialState(FirstIndent, FirstStartColumn,
1114 while (State.NextToken) {
1117 (
Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
1118 unsigned Penalty = 0;
1119 formatChildren(State, Newline,
false, Penalty);
1120 Indenter->addTokenToState(State, Newline,
false);
1127class NoLineBreakFormatter :
public LineFormatter {
1129 NoLineBreakFormatter(ContinuationIndenter *
Indenter,
1130 WhitespaceManager *Whitespaces,
const FormatStyle &Style,
1131 UnwrappedLineFormatter *BlockFormatter)
1132 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1135 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1136 unsigned FirstStartColumn,
bool DryRun)
override {
1137 unsigned Penalty = 0;
1139 Indenter->getInitialState(FirstIndent, FirstStartColumn, &
Line, DryRun);
1140 while (State.NextToken) {
1141 formatChildren(State,
false, DryRun, Penalty);
1143 State, State.NextToken->MustBreakBefore, DryRun);
1150class OptimizingLineFormatter :
public LineFormatter {
1152 OptimizingLineFormatter(ContinuationIndenter *
Indenter,
1153 WhitespaceManager *Whitespaces,
1154 const FormatStyle &Style,
1155 UnwrappedLineFormatter *BlockFormatter)
1156 : LineFormatter(
Indenter, Whitespaces, Style, BlockFormatter) {}
1160 unsigned formatLine(
const AnnotatedLine &
Line,
unsigned FirstIndent,
1161 unsigned FirstStartColumn,
bool DryRun)
override {
1163 Indenter->getInitialState(FirstIndent, FirstStartColumn, &
Line, DryRun);
1168 State.Stack.back().BreakBeforeParameter =
true;
1171 return analyzeSolutionSpace(State, DryRun);
1175 struct CompareLineStatePointers {
1176 bool operator()(LineState *obj1, LineState *obj2)
const {
1177 return *obj1 < *obj2;
1186 typedef std::pair<unsigned, unsigned> OrderedPenalty;
1191 StateNode(
const LineState &State,
bool NewLine, StateNode *Previous)
1200 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
1203 typedef std::priority_queue<QueueItem, SmallVector<QueueItem>,
1204 std::greater<QueueItem>>
1215 unsigned analyzeSolutionSpace(LineState &InitialState,
bool DryRun) {
1216 std::set<LineState *, CompareLineStatePointers> Seen;
1224 StateNode *RootNode =
1225 new (Allocator.Allocate()) StateNode(InitialState,
false,
nullptr);
1226 Queue.push(QueueItem(OrderedPenalty(0, Count), RootNode));
1229 unsigned Penalty = 0;
1232 while (!Queue.empty()) {
1234 if (Count > 25'000'000)
1237 Penalty = Queue.top().first.first;
1238 StateNode *
Node = Queue.top().second;
1239 if (!
Node->State.NextToken) {
1240 LLVM_DEBUG(llvm::dbgs()
1241 <<
"\n---\nPenalty for line: " << Penalty <<
"\n");
1249 Node->State.IgnoreStackForComparison =
true;
1251 if (!Seen.insert(&
Node->State).second) {
1258 addNextStateToQueue(Penalty,
Node,
false, &Count, &Queue);
1260 addNextStateToQueue(Penalty,
Node,
true, &Count, &Queue);
1263 if (Queue.empty()) {
1266 LLVM_DEBUG(llvm::dbgs() <<
"Could not find a solution.\n");
1272 reconstructPath(InitialState, Queue.top().second);
1274 LLVM_DEBUG(llvm::dbgs()
1275 <<
"Total number of analyzed states: " << Count <<
"\n");
1276 LLVM_DEBUG(llvm::dbgs() <<
"---\n");
1285 void addNextStateToQueue(
unsigned Penalty, StateNode *PreviousNode,
1286 bool NewLine,
unsigned *Count, QueueType *Queue) {
1292 StateNode *
Node =
new (Allocator.Allocate())
1293 StateNode(PreviousNode->State,
NewLine, PreviousNode);
1294 if (!formatChildren(
Node->State,
NewLine,
true, Penalty))
1299 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count),
Node));
1305 void reconstructPath(LineState &State, StateNode *Best) {
1308 while (Best->Previous) {
1309 Path.push_back(Best);
1310 Best = Best->Previous;
1312 for (
const auto &
Node : llvm::reverse(Path)) {
1313 unsigned Penalty = 0;
1314 formatChildren(State,
Node->NewLine,
false, Penalty);
1315 Penalty +=
Indenter->addTokenToState(State,
Node->NewLine,
false);
1318 printLineState(
Node->Previous->State);
1319 if (
Node->NewLine) {
1320 llvm::dbgs() <<
"Penalty for placing "
1321 <<
Node->Previous->State.NextToken->Tok.getName()
1322 <<
" on a new line: " << Penalty <<
"\n";
1328 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1335 int AdditionalIndent,
bool FixBadIndentation,
unsigned FirstStartColumn,
1336 unsigned NextStartColumn,
unsigned LastStartColumn) {
1337 LineJoiner Joiner(Style, Keywords, Lines);
1340 std::pair<const SmallVectorImpl<AnnotatedLine *> *,
unsigned> CacheKey(
1341 &Lines, AdditionalIndent);
1342 auto CacheIt = PenaltyCache.find(CacheKey);
1343 if (DryRun && CacheIt != PenaltyCache.end())
1344 return CacheIt->second;
1346 assert(!Lines.empty());
1347 unsigned Penalty = 0;
1348 LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
1357 bool FirstLine =
true;
1359 Joiner.getNextMergedLine(DryRun, IndentTracker);
1360 Line; PrevPrevLine = PreviousLine, PreviousLine =
Line,
Line = NextLine,
1361 FirstLine =
false) {
1362 assert(
Line->First);
1364 unsigned Indent = IndentTracker.getIndent();
1370 bool PreviousRBrace =
1371 PreviousLine && PreviousLine->
startsWith(tok::r_brace);
1372 bool ContinueFormatting =
1373 TheLine.
Level > RangeMinLevel ||
1374 (TheLine.
Level == RangeMinLevel && !PreviousRBrace &&
1377 bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
1379 bool ShouldFormat = TheLine.
Affected || FixIndentation;
1390 bool LastLine = TheLine.
First->
is(tok::eof);
1391 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,
1392 LastLine ? LastStartColumn : NextStartColumn + Indent);
1395 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1396 unsigned ColumnLimit = getColumnLimit(TheLine.
InPPDirective, NextLine);
1397 bool FitsIntoOneLine =
1401 (!Style.isJavaScript() || !Style.JavaScriptWrapImports)) ||
1402 (Style.isCSharp() &&
1404 if (Style.ColumnLimit == 0) {
1405 NoColumnLimitLineFormatter(
Indenter, Whitespaces, Style,
this)
1406 .formatLine(TheLine, NextStartColumn + Indent,
1407 FirstLine ? FirstStartColumn : 0, DryRun);
1408 }
else if (FitsIntoOneLine) {
1409 Penalty += NoLineBreakFormatter(
Indenter, Whitespaces, Style,
this)
1410 .formatLine(TheLine, NextStartColumn + Indent,
1411 FirstLine ? FirstStartColumn : 0, DryRun);
1413 Penalty += OptimizingLineFormatter(
Indenter, Whitespaces, Style,
this)
1414 .formatLine(TheLine, NextStartColumn + Indent,
1415 FirstLine ? FirstStartColumn : 0, DryRun);
1417 RangeMinLevel = std::min(RangeMinLevel, TheLine.
Level);
1423 if (!Tok->Children.empty())
1424 format(Tok->Children, DryRun);
1429 bool StartsNewLine =
1432 IndentTracker.adjustToUnmodifiedLine(TheLine);
1434 bool ReformatLeadingWhitespace =
1435 StartsNewLine && ((PreviousLine && PreviousLine->
Affected) ||
1438 if (ReformatLeadingWhitespace) {
1439 formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines,
1443 Whitespaces->addUntouchableToken(*TheLine.
First,
1449 Whitespaces->addUntouchableToken(*Tok, TheLine.
InPPDirective);
1451 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1455 markFinalized(TheLine.
First);
1457 PenaltyCache[CacheKey] = Penalty;
1466 const auto &RootToken = *
Line.First;
1470 if (RootToken.is(tok::r_brace) &&
1472 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&
1475 Newlines = std::min(Newlines, 1u);
1478 if (!PreviousLine &&
Line.Level > 0)
1479 Newlines = std::min(Newlines, 1u);
1480 if (Newlines == 0 && !RootToken.IsFirst)
1482 if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
1487 PreviousLine->
Last->
is(tok::l_brace) &&
1491 !startsExternCBlock(*PreviousLine)) {
1496 if (PreviousLine && RootToken.isAccessSpecifier()) {
1503 Newlines = std::max(RootToken.NewlinesBefore, 1u);
1506 if (PreviousLine->
Last->
isOneOf(tok::semi, tok::r_brace) && Newlines <= 1)
1513 if (PreviousLine->
Last->
is(tok::comment))
1516 previousToken = PreviousLine->
Last;
1517 if ((!previousToken || previousToken->
isNot(tok::l_brace)) &&
1527 (!PreviousLine->
InPPDirective || !RootToken.HasUnescapedNewline)) {
1530 if (!RootToken.isAccessSpecifier()) {
1536 Newlines = std::max(Newlines, 1u);
1539 if (RootToken.is(tok::r_brace))
1542 Newlines = std::max(Newlines, 2u);
1551void UnwrappedLineFormatter::formatFirstToken(
1552 const AnnotatedLine &
Line,
const AnnotatedLine *PreviousLine,
1553 const AnnotatedLine *PrevPrevLine,
1555 unsigned NewlineIndent) {
1556 FormatToken &RootToken = *
Line.First;
1557 if (RootToken.is(tok::eof)) {
1559 std::min(RootToken.NewlinesBefore,
1560 Style.KeepEmptyLinesAtEOF ? Style.MaxEmptyLinesToKeep + 1 : 1);
1561 unsigned TokenIndent = Newlines ? NewlineIndent : 0;
1562 Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
1567 if (RootToken.Newlines < 0) {
1568 RootToken.Newlines =
1570 assert(RootToken.Newlines >= 0);
1573 if (RootToken.Newlines > 0)
1574 Indent = NewlineIndent;
1578 if (!Style.isJavaScript() &&
1585 Whitespaces->replaceWhitespace(RootToken, RootToken.Newlines, Indent, Indent,
1587 Line.InPPDirective &&
1588 !RootToken.HasUnescapedNewline);
1592UnwrappedLineFormatter::getColumnLimit(
bool InPPDirective,
1593 const AnnotatedLine *NextLine)
const {
1596 bool ContinuesPPDirective =
1601 (NextLine->InPPDirective &&
1604 !NextLine->First->HasUnescapedNewline));
1605 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())))