1 //===--- UnwrappedLineFormatter.cpp - Format C++ code ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "UnwrappedLineFormatter.h"
10 #include "NamespaceEndCommentsFixer.h"
11 #include "WhitespaceManager.h"
12 #include "llvm/Support/Debug.h"
13 #include <queue>
14 
15 #define DEBUG_TYPE "format-formatter"
16 
17 namespace clang {
18 namespace format {
19 
20 namespace {
21 
22 bool startsExternCBlock(const AnnotatedLine &Line) {
23   const FormatToken *Next = Line.First->getNextNonComment();
24   const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr;
25   return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() &&
26          NextNext && NextNext->is(tok::l_brace);
27 }
28 
29 bool isRecordLBrace(const FormatToken &Tok) {
30   return Tok.isOneOf(TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace,
31                      TT_StructLBrace, TT_UnionLBrace);
32 }
33 
34 /// Tracks the indent level of \c AnnotatedLines across levels.
35 ///
36 /// \c nextLine must be called for each \c AnnotatedLine, after which \c
37 /// getIndent() will return the indent for the last line \c nextLine was called
38 /// with.
39 /// If the line is not formatted (and thus the indent does not change), calling
40 /// \c adjustToUnmodifiedLine after the call to \c nextLine will cause
41 /// subsequent lines on the same level to be indented at the same level as the
42 /// given line.
43 class LevelIndentTracker {
44 public:
45   LevelIndentTracker(const FormatStyle &Style,
46                      const AdditionalKeywords &Keywords, unsigned StartLevel,
47                      int AdditionalIndent)
48       : Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) {
49     for (unsigned i = 0; i != StartLevel; ++i)
50       IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
51   }
52 
53   /// Returns the indent for the current line.
54   unsigned getIndent() const { return Indent; }
55 
56   /// Update the indent state given that \p Line is going to be formatted
57   /// next.
58   void nextLine(const AnnotatedLine &Line) {
59     Offset = getIndentOffset(*Line.First);
60     // Update the indent level cache size so that we can rely on it
61     // having the right size in adjustToUnmodifiedline.
62     while (IndentForLevel.size() <= Line.Level)
63       IndentForLevel.push_back(-1);
64     if (Line.InPPDirective) {
65       unsigned IndentWidth =
66           (Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth;
67       Indent = Line.Level * IndentWidth + AdditionalIndent;
68     } else {
69       IndentForLevel.resize(Line.Level + 1);
70       Indent = getIndent(Line.Level);
71     }
72     if (static_cast<int>(Indent) + Offset >= 0)
73       Indent += Offset;
74     if (Line.First->is(TT_CSharpGenericTypeConstraint))
75       Indent = Line.Level * Style.IndentWidth + Style.ContinuationIndentWidth;
76   }
77 
78   /// Update the indent state given that \p Line indent should be
79   /// skipped.
80   void skipLine(const AnnotatedLine &Line) {
81     while (IndentForLevel.size() <= Line.Level)
82       IndentForLevel.push_back(Indent);
83   }
84 
85   /// Update the level indent to adapt to the given \p Line.
86   ///
87   /// When a line is not formatted, we move the subsequent lines on the same
88   /// level to the same indent.
89   /// Note that \c nextLine must have been called before this method.
90   void adjustToUnmodifiedLine(const AnnotatedLine &Line) {
91     unsigned LevelIndent = Line.First->OriginalColumn;
92     if (static_cast<int>(LevelIndent) - Offset >= 0)
93       LevelIndent -= Offset;
94     if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) &&
95         !Line.InPPDirective) {
96       IndentForLevel[Line.Level] = LevelIndent;
97     }
98   }
99 
100 private:
101   /// Get the offset of the line relatively to the level.
102   ///
103   /// For example, 'public:' labels in classes are offset by 1 or 2
104   /// characters to the left from their level.
105   int getIndentOffset(const FormatToken &RootToken) {
106     if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() ||
107         Style.isCSharp()) {
108       return 0;
109     }
110 
111     auto IsAccessModifier = [this, &RootToken]() {
112       if (RootToken.isAccessSpecifier(Style.isCpp())) {
113         return true;
114       } else if (RootToken.isObjCAccessSpecifier()) {
115         return true;
116       }
117       // Handle Qt signals.
118       else if ((RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
119                 RootToken.Next && RootToken.Next->is(tok::colon))) {
120         return true;
121       } else if (RootToken.Next &&
122                  RootToken.Next->isOneOf(Keywords.kw_slots,
123                                          Keywords.kw_qslots) &&
124                  RootToken.Next->Next && RootToken.Next->Next->is(tok::colon)) {
125         return true;
126       }
127       // Handle malformed access specifier e.g. 'private' without trailing ':'.
128       else if (!RootToken.Next && RootToken.isAccessSpecifier(false)) {
129         return true;
130       }
131       return false;
132     };
133 
134     if (IsAccessModifier()) {
135       // The AccessModifierOffset may be overridden by IndentAccessModifiers,
136       // in which case we take a negative value of the IndentWidth to simulate
137       // the upper indent level.
138       return Style.IndentAccessModifiers ? -Style.IndentWidth
139                                          : Style.AccessModifierOffset;
140     }
141     return 0;
142   }
143 
144   /// Get the indent of \p Level from \p IndentForLevel.
145   ///
146   /// \p IndentForLevel must contain the indent for the level \c l
147   /// at \p IndentForLevel[l], or a value < 0 if the indent for
148   /// that level is unknown.
149   unsigned getIndent(unsigned Level) const {
150     if (IndentForLevel[Level] != -1)
151       return IndentForLevel[Level];
152     if (Level == 0)
153       return 0;
154     return getIndent(Level - 1) + Style.IndentWidth;
155   }
156 
157   const FormatStyle &Style;
158   const AdditionalKeywords &Keywords;
159   const unsigned AdditionalIndent;
160 
161   /// The indent in characters for each level.
162   std::vector<int> IndentForLevel;
163 
164   /// Offset of the current line relative to the indent level.
165   ///
166   /// For example, the 'public' keywords is often indented with a negative
167   /// offset.
168   int Offset = 0;
169 
170   /// The current line's indent.
171   unsigned Indent = 0;
172 };
173 
174 const FormatToken *getMatchingNamespaceToken(
175     const AnnotatedLine *Line,
176     const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
177   if (!Line->startsWith(tok::r_brace))
178     return nullptr;
179   size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
180   if (StartLineIndex == UnwrappedLine::kInvalidIndex)
181     return nullptr;
182   assert(StartLineIndex < AnnotatedLines.size());
183   return AnnotatedLines[StartLineIndex]->First->getNamespaceToken();
184 }
185 
186 StringRef getNamespaceTokenText(const AnnotatedLine *Line) {
187   const FormatToken *NamespaceToken = Line->First->getNamespaceToken();
188   return NamespaceToken ? NamespaceToken->TokenText : StringRef();
189 }
190 
191 StringRef getMatchingNamespaceTokenText(
192     const AnnotatedLine *Line,
193     const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
194   const FormatToken *NamespaceToken =
195       getMatchingNamespaceToken(Line, AnnotatedLines);
196   return NamespaceToken ? NamespaceToken->TokenText : StringRef();
197 }
198 
199 class LineJoiner {
200 public:
201   LineJoiner(const FormatStyle &Style, const AdditionalKeywords &Keywords,
202              const SmallVectorImpl<AnnotatedLine *> &Lines)
203       : Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()),
204         AnnotatedLines(Lines) {}
205 
206   /// Returns the next line, merging multiple lines into one if possible.
207   const AnnotatedLine *getNextMergedLine(bool DryRun,
208                                          LevelIndentTracker &IndentTracker) {
209     if (Next == End)
210       return nullptr;
211     const AnnotatedLine *Current = *Next;
212     IndentTracker.nextLine(*Current);
213     unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End);
214     if (MergedLines > 0 && Style.ColumnLimit == 0) {
215       // Disallow line merging if there is a break at the start of one of the
216       // input lines.
217       for (unsigned i = 0; i < MergedLines; ++i)
218         if (Next[i + 1]->First->NewlinesBefore > 0)
219           MergedLines = 0;
220     }
221     if (!DryRun)
222       for (unsigned i = 0; i < MergedLines; ++i)
223         join(*Next[0], *Next[i + 1]);
224     Next = Next + MergedLines + 1;
225     return Current;
226   }
227 
228 private:
229   /// Calculates how many lines can be merged into 1 starting at \p I.
230   unsigned
231   tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
232                            SmallVectorImpl<AnnotatedLine *>::const_iterator I,
233                            SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
234     const unsigned Indent = IndentTracker.getIndent();
235 
236     // Can't join the last line with anything.
237     if (I + 1 == E)
238       return 0;
239     // We can never merge stuff if there are trailing line comments.
240     const AnnotatedLine *TheLine = *I;
241     if (TheLine->Last->is(TT_LineComment))
242       return 0;
243     const auto &NextLine = *I[1];
244     if (NextLine.Type == LT_Invalid || NextLine.First->MustBreakBefore)
245       return 0;
246     if (TheLine->InPPDirective &&
247         (!NextLine.InPPDirective || NextLine.First->HasUnescapedNewline)) {
248       return 0;
249     }
250 
251     if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
252       return 0;
253 
254     unsigned Limit =
255         Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent;
256     // If we already exceed the column limit, we set 'Limit' to 0. The different
257     // tryMerge..() functions can then decide whether to still do merging.
258     Limit = TheLine->Last->TotalLength > Limit
259                 ? 0
260                 : Limit - TheLine->Last->TotalLength;
261 
262     if (TheLine->Last->is(TT_FunctionLBrace) &&
263         TheLine->First == TheLine->Last &&
264         !Style.BraceWrapping.SplitEmptyFunction &&
265         NextLine.First->is(tok::r_brace)) {
266       return tryMergeSimpleBlock(I, E, Limit);
267     }
268 
269     const auto *PreviousLine = I != AnnotatedLines.begin() ? I[-1] : nullptr;
270     // Handle empty record blocks where the brace has already been wrapped.
271     if (PreviousLine && TheLine->Last->is(tok::l_brace) &&
272         TheLine->First == TheLine->Last) {
273       bool EmptyBlock = NextLine.First->is(tok::r_brace);
274 
275       const FormatToken *Tok = PreviousLine->First;
276       if (Tok && Tok->is(tok::comment))
277         Tok = Tok->getNextNonComment();
278 
279       if (Tok && Tok->getNamespaceToken()) {
280         return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
281                    ? tryMergeSimpleBlock(I, E, Limit)
282                    : 0;
283       }
284 
285       if (Tok && Tok->is(tok::kw_typedef))
286         Tok = Tok->getNextNonComment();
287       if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
288                               tok::kw_extern, Keywords.kw_interface)) {
289         return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
290                    ? tryMergeSimpleBlock(I, E, Limit)
291                    : 0;
292       }
293 
294       if (Tok && Tok->is(tok::kw_template) &&
295           Style.BraceWrapping.SplitEmptyRecord && EmptyBlock) {
296         return 0;
297       }
298     }
299 
300     auto ShouldMergeShortFunctions = [this, &I, &NextLine, PreviousLine,
301                                       TheLine]() {
302       if (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All)
303         return true;
304       if (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
305           NextLine.First->is(tok::r_brace)) {
306         return true;
307       }
308 
309       if (Style.AllowShortFunctionsOnASingleLine &
310           FormatStyle::SFS_InlineOnly) {
311         // Just checking TheLine->Level != 0 is not enough, because it
312         // provokes treating functions inside indented namespaces as short.
313         if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace))
314           return true;
315 
316         if (TheLine->Level != 0) {
317           if (!PreviousLine)
318             return false;
319 
320           // TODO: Use IndentTracker to avoid loop?
321           // Find the last line with lower level.
322           const AnnotatedLine *Line = nullptr;
323           for (auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
324             assert(*J);
325             if (!(*J)->InPPDirective && !(*J)->isComment() &&
326                 (*J)->Level < TheLine->Level) {
327               Line = *J;
328               break;
329             }
330           }
331 
332           if (!Line)
333             return false;
334 
335           // Check if the found line starts a record.
336           const FormatToken *LastNonComment = Line->Last;
337           assert(LastNonComment);
338           if (LastNonComment->is(tok::comment)) {
339             LastNonComment = LastNonComment->getPreviousNonComment();
340             // There must be another token (usually `{`), because we chose a
341             // non-PPDirective and non-comment line that has a smaller level.
342             assert(LastNonComment);
343           }
344           return isRecordLBrace(*LastNonComment);
345         }
346       }
347 
348       return false;
349     };
350 
351     bool MergeShortFunctions = ShouldMergeShortFunctions();
352 
353     const FormatToken *FirstNonComment = TheLine->First;
354     if (FirstNonComment->is(tok::comment)) {
355       FirstNonComment = FirstNonComment->getNextNonComment();
356       if (!FirstNonComment)
357         return 0;
358     }
359     // FIXME: There are probably cases where we should use FirstNonComment
360     // instead of TheLine->First.
361 
362     if (Style.CompactNamespaces) {
363       if (auto nsToken = TheLine->First->getNamespaceToken()) {
364         int i = 0;
365         unsigned closingLine = TheLine->MatchingClosingBlockLineIndex - 1;
366         for (; I + 1 + i != E &&
367                nsToken->TokenText == getNamespaceTokenText(I[i + 1]) &&
368                closingLine == I[i + 1]->MatchingClosingBlockLineIndex &&
369                I[i + 1]->Last->TotalLength < Limit;
370              i++, --closingLine) {
371           // No extra indent for compacted namespaces.
372           IndentTracker.skipLine(*I[i + 1]);
373 
374           Limit -= I[i + 1]->Last->TotalLength;
375         }
376         return i;
377       }
378 
379       if (auto nsToken = getMatchingNamespaceToken(TheLine, AnnotatedLines)) {
380         int i = 0;
381         unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
382         for (; I + 1 + i != E &&
383                nsToken->TokenText ==
384                    getMatchingNamespaceTokenText(I[i + 1], AnnotatedLines) &&
385                openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;
386              i++, --openingLine) {
387           // No space between consecutive braces.
388           I[i + 1]->First->SpacesRequiredBefore = !I[i]->Last->is(tok::r_brace);
389 
390           // Indent like the outer-most namespace.
391           IndentTracker.nextLine(*I[i + 1]);
392         }
393         return i;
394       }
395     }
396 
397     // Try to merge a function block with left brace unwrapped.
398     if (TheLine->Last->is(TT_FunctionLBrace) && TheLine->First != TheLine->Last)
399       return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
400     // Try to merge a control statement block with left brace unwrapped.
401     if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
402         FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
403                                  TT_ForEachMacro)) {
404       return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never
405                  ? tryMergeSimpleBlock(I, E, Limit)
406                  : 0;
407     }
408     // Try to merge a control statement block with left brace wrapped.
409     if (NextLine.First->is(tok::l_brace)) {
410       if ((TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
411                                    tok::kw_for, tok::kw_switch, tok::kw_try,
412                                    tok::kw_do, TT_ForEachMacro) ||
413            (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
414             TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
415           Style.BraceWrapping.AfterControlStatement ==
416               FormatStyle::BWACS_MultiLine) {
417         // If possible, merge the next line's wrapped left brace with the
418         // current line. Otherwise, leave it on the next line, as this is a
419         // multi-line control statement.
420         return (Style.ColumnLimit == 0 || TheLine->Level * Style.IndentWidth +
421                                                   TheLine->Last->TotalLength <=
422                                               Style.ColumnLimit)
423                    ? 1
424                    : 0;
425       }
426       if (TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
427                                   tok::kw_for, TT_ForEachMacro)) {
428         return (Style.BraceWrapping.AfterControlStatement ==
429                 FormatStyle::BWACS_Always)
430                    ? tryMergeSimpleBlock(I, E, Limit)
431                    : 0;
432       }
433       if (TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&
434           Style.BraceWrapping.AfterControlStatement ==
435               FormatStyle::BWACS_MultiLine) {
436         // This case if different from the upper BWACS_MultiLine processing
437         // in that a preceding r_brace is not on the same line as else/catch
438         // most likely because of BeforeElse/BeforeCatch set to true.
439         // If the line length doesn't fit ColumnLimit, leave l_brace on the
440         // next line to respect the BWACS_MultiLine.
441         return (Style.ColumnLimit == 0 ||
442                 TheLine->Last->TotalLength <= Style.ColumnLimit)
443                    ? 1
444                    : 0;
445       }
446     }
447     if (PreviousLine && TheLine->First->is(tok::l_brace)) {
448       switch (PreviousLine->First->Tok.getKind()) {
449       case tok::at:
450         // Don't merge block with left brace wrapped after ObjC special blocks.
451         if (PreviousLine->First->Next) {
452           tok::ObjCKeywordKind kwId =
453               PreviousLine->First->Next->Tok.getObjCKeywordID();
454           if (kwId == tok::objc_autoreleasepool ||
455               kwId == tok::objc_synchronized) {
456             return 0;
457           }
458         }
459         break;
460 
461       case tok::kw_case:
462       case tok::kw_default:
463         // Don't merge block with left brace wrapped after case labels.
464         return 0;
465 
466       default:
467         break;
468       }
469     }
470 
471     // Don't merge an empty template class or struct if SplitEmptyRecords
472     // is defined.
473     if (PreviousLine && Style.BraceWrapping.SplitEmptyRecord &&
474         TheLine->Last->is(tok::l_brace) && PreviousLine->Last) {
475       const FormatToken *Previous = PreviousLine->Last;
476       if (Previous) {
477         if (Previous->is(tok::comment))
478           Previous = Previous->getPreviousNonComment();
479         if (Previous) {
480           if (Previous->is(tok::greater) && !PreviousLine->InPPDirective)
481             return 0;
482           if (Previous->is(tok::identifier)) {
483             const FormatToken *PreviousPrevious =
484                 Previous->getPreviousNonComment();
485             if (PreviousPrevious &&
486                 PreviousPrevious->isOneOf(tok::kw_class, tok::kw_struct)) {
487               return 0;
488             }
489           }
490         }
491       }
492     }
493 
494     if (TheLine->Last->is(tok::l_brace)) {
495       bool ShouldMerge = false;
496       // Try to merge records.
497       if (TheLine->Last->is(TT_EnumLBrace)) {
498         ShouldMerge = Style.AllowShortEnumsOnASingleLine;
499       } else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
500         // NOTE: We use AfterClass (whereas AfterStruct exists) for both classes
501         // and structs, but it seems that wrapping is still handled correctly
502         // elsewhere.
503         ShouldMerge = !Style.BraceWrapping.AfterClass ||
504                       (NextLine.First->is(tok::r_brace) &&
505                        !Style.BraceWrapping.SplitEmptyRecord);
506       } else {
507         // Try to merge a block with left brace unwrapped that wasn't yet
508         // covered.
509         assert(TheLine->InPPDirective ||
510                !TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
511                                         tok::kw_struct));
512         ShouldMerge = !Style.BraceWrapping.AfterFunction ||
513                       (NextLine.First->is(tok::r_brace) &&
514                        !Style.BraceWrapping.SplitEmptyFunction);
515       }
516       return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;
517     }
518 
519     // Try to merge a function block with left brace wrapped.
520     if (NextLine.First->is(TT_FunctionLBrace) &&
521         Style.BraceWrapping.AfterFunction) {
522       if (NextLine.Last->is(TT_LineComment))
523         return 0;
524 
525       // Check for Limit <= 2 to account for the " {".
526       if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
527         return 0;
528       Limit -= 2;
529 
530       unsigned MergedLines = 0;
531       if (MergeShortFunctions ||
532           (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
533            NextLine.First == NextLine.Last && I + 2 != E &&
534            I[2]->First->is(tok::r_brace))) {
535         MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
536         // If we managed to merge the block, count the function header, which is
537         // on a separate line.
538         if (MergedLines > 0)
539           ++MergedLines;
540       }
541       return MergedLines;
542     }
543     auto IsElseLine = [&TheLine]() -> bool {
544       const FormatToken *First = TheLine->First;
545       if (First->is(tok::kw_else))
546         return true;
547 
548       return First->is(tok::r_brace) && First->Next &&
549              First->Next->is(tok::kw_else);
550     };
551     if (TheLine->First->is(tok::kw_if) ||
552         (IsElseLine() && (Style.AllowShortIfStatementsOnASingleLine ==
553                           FormatStyle::SIS_AllIfsAndElse))) {
554       return Style.AllowShortIfStatementsOnASingleLine
555                  ? tryMergeSimpleControlStatement(I, E, Limit)
556                  : 0;
557     }
558     if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do,
559                                 TT_ForEachMacro)) {
560       return Style.AllowShortLoopsOnASingleLine
561                  ? tryMergeSimpleControlStatement(I, E, Limit)
562                  : 0;
563     }
564     if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
565       return Style.AllowShortCaseLabelsOnASingleLine
566                  ? tryMergeShortCaseLabels(I, E, Limit)
567                  : 0;
568     }
569     if (TheLine->InPPDirective &&
570         (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
571       return tryMergeSimplePPDirective(I, E, Limit);
572     }
573     return 0;
574   }
575 
576   unsigned
577   tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
578                             SmallVectorImpl<AnnotatedLine *>::const_iterator E,
579                             unsigned Limit) {
580     if (Limit == 0)
581       return 0;
582     if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline)
583       return 0;
584     if (1 + I[1]->Last->TotalLength > Limit)
585       return 0;
586     return 1;
587   }
588 
589   unsigned tryMergeSimpleControlStatement(
590       SmallVectorImpl<AnnotatedLine *>::const_iterator I,
591       SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
592     if (Limit == 0)
593       return 0;
594     if (Style.BraceWrapping.AfterControlStatement ==
595             FormatStyle::BWACS_Always &&
596         I[1]->First->is(tok::l_brace) &&
597         Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {
598       return 0;
599     }
600     if (I[1]->InPPDirective != (*I)->InPPDirective ||
601         (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) {
602       return 0;
603     }
604     Limit = limitConsideringMacros(I + 1, E, Limit);
605     AnnotatedLine &Line = **I;
606     if (!Line.First->is(tok::kw_do) && !Line.First->is(tok::kw_else) &&
607         !Line.Last->is(tok::kw_else) && Line.Last->isNot(tok::r_paren)) {
608       return 0;
609     }
610     // Only merge `do while` if `do` is the only statement on the line.
611     if (Line.First->is(tok::kw_do) && !Line.Last->is(tok::kw_do))
612       return 0;
613     if (1 + I[1]->Last->TotalLength > Limit)
614       return 0;
615     // Don't merge with loops, ifs, a single semicolon or a line comment.
616     if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
617                              TT_ForEachMacro, TT_LineComment)) {
618       return 0;
619     }
620     // Only inline simple if's (no nested if or else), unless specified
621     if (Style.AllowShortIfStatementsOnASingleLine ==
622         FormatStyle::SIS_WithoutElse) {
623       if (I + 2 != E && Line.startsWith(tok::kw_if) &&
624           I[2]->First->is(tok::kw_else)) {
625         return 0;
626       }
627     }
628     return 1;
629   }
630 
631   unsigned
632   tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
633                           SmallVectorImpl<AnnotatedLine *>::const_iterator E,
634                           unsigned Limit) {
635     if (Limit == 0 || I + 1 == E ||
636         I[1]->First->isOneOf(tok::kw_case, tok::kw_default)) {
637       return 0;
638     }
639     if (I[0]->Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))
640       return 0;
641     unsigned NumStmts = 0;
642     unsigned Length = 0;
643     bool EndsWithComment = false;
644     bool InPPDirective = I[0]->InPPDirective;
645     const unsigned Level = I[0]->Level;
646     for (; NumStmts < 3; ++NumStmts) {
647       if (I + 1 + NumStmts == E)
648         break;
649       const AnnotatedLine *Line = I[1 + NumStmts];
650       if (Line->InPPDirective != InPPDirective)
651         break;
652       if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
653         break;
654       if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
655                                tok::kw_while) ||
656           EndsWithComment) {
657         return 0;
658       }
659       if (Line->First->is(tok::comment)) {
660         if (Level != Line->Level)
661           return 0;
662         SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts;
663         for (; J != E; ++J) {
664           Line = *J;
665           if (Line->InPPDirective != InPPDirective)
666             break;
667           if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
668             break;
669           if (Line->First->isNot(tok::comment) || Level != Line->Level)
670             return 0;
671         }
672         break;
673       }
674       if (Line->Last->is(tok::comment))
675         EndsWithComment = true;
676       Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space.
677     }
678     if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
679       return 0;
680     return NumStmts;
681   }
682 
683   unsigned
684   tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
685                       SmallVectorImpl<AnnotatedLine *>::const_iterator E,
686                       unsigned Limit) {
687     // Don't merge with a preprocessor directive.
688     if (I[1]->Type == LT_PreprocessorDirective)
689       return 0;
690 
691     AnnotatedLine &Line = **I;
692 
693     // Don't merge ObjC @ keywords and methods.
694     // FIXME: If an option to allow short exception handling clauses on a single
695     // line is added, change this to not return for @try and friends.
696     if (Style.Language != FormatStyle::LK_Java &&
697         Line.First->isOneOf(tok::at, tok::minus, tok::plus)) {
698       return 0;
699     }
700 
701     // Check that the current line allows merging. This depends on whether we
702     // are in a control flow statements as well as several style flags.
703     if (Line.First->is(tok::kw_case) ||
704         (Line.First->Next && Line.First->Next->is(tok::kw_else))) {
705       return 0;
706     }
707     // default: in switch statement
708     if (Line.First->is(tok::kw_default)) {
709       const FormatToken *Tok = Line.First->getNextNonComment();
710       if (Tok && Tok->is(tok::colon))
711         return 0;
712     }
713     if (Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while, tok::kw_do,
714                             tok::kw_try, tok::kw___try, tok::kw_catch,
715                             tok::kw___finally, tok::kw_for, TT_ForEachMacro,
716                             tok::r_brace, Keywords.kw___except)) {
717       if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
718         return 0;
719       if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Empty &&
720           !I[1]->First->is(tok::r_brace)) {
721         return 0;
722       }
723       // Don't merge when we can't except the case when
724       // the control statement block is empty
725       if (!Style.AllowShortIfStatementsOnASingleLine &&
726           Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
727           !Style.BraceWrapping.AfterControlStatement &&
728           !I[1]->First->is(tok::r_brace)) {
729         return 0;
730       }
731       if (!Style.AllowShortIfStatementsOnASingleLine &&
732           Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
733           Style.BraceWrapping.AfterControlStatement ==
734               FormatStyle::BWACS_Always &&
735           I + 2 != E && !I[2]->First->is(tok::r_brace)) {
736         return 0;
737       }
738       if (!Style.AllowShortLoopsOnASingleLine &&
739           Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
740                               TT_ForEachMacro) &&
741           !Style.BraceWrapping.AfterControlStatement &&
742           !I[1]->First->is(tok::r_brace)) {
743         return 0;
744       }
745       if (!Style.AllowShortLoopsOnASingleLine &&
746           Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,
747                               TT_ForEachMacro) &&
748           Style.BraceWrapping.AfterControlStatement ==
749               FormatStyle::BWACS_Always &&
750           I + 2 != E && !I[2]->First->is(tok::r_brace)) {
751         return 0;
752       }
753       // FIXME: Consider an option to allow short exception handling clauses on
754       // a single line.
755       // FIXME: This isn't covered by tests.
756       // FIXME: For catch, __except, __finally the first token on the line
757       // is '}', so this isn't correct here.
758       if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
759                               Keywords.kw___except, tok::kw___finally)) {
760         return 0;
761       }
762     }
763 
764     if (Line.Last->is(tok::l_brace)) {
765       FormatToken *Tok = I[1]->First;
766       auto ShouldMerge = [Tok]() {
767         if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore)
768           return false;
769         const FormatToken *Next = Tok->getNextNonComment();
770         return !Next || Next->is(tok::semi);
771       };
772 
773       if (ShouldMerge()) {
774         // We merge empty blocks even if the line exceeds the column limit.
775         Tok->SpacesRequiredBefore = Style.SpaceInEmptyBlock ? 1 : 0;
776         Tok->CanBreakBefore = true;
777         return 1;
778       } else if (Limit != 0 && !Line.startsWithNamespace() &&
779                  !startsExternCBlock(Line)) {
780         // We don't merge short records.
781         if (isRecordLBrace(*Line.Last))
782           return 0;
783 
784         // Check that we still have three lines and they fit into the limit.
785         if (I + 2 == E || I[2]->Type == LT_Invalid)
786           return 0;
787         Limit = limitConsideringMacros(I + 2, E, Limit);
788 
789         if (!nextTwoLinesFitInto(I, Limit))
790           return 0;
791 
792         // Second, check that the next line does not contain any braces - if it
793         // does, readability declines when putting it into a single line.
794         if (I[1]->Last->is(TT_LineComment))
795           return 0;
796         do {
797           if (Tok->is(tok::l_brace) && Tok->isNot(BK_BracedInit))
798             return 0;
799           Tok = Tok->Next;
800         } while (Tok);
801 
802         // Last, check that the third line starts with a closing brace.
803         Tok = I[2]->First;
804         if (Tok->isNot(tok::r_brace))
805           return 0;
806 
807         // Don't merge "if (a) { .. } else {".
808         if (Tok->Next && Tok->Next->is(tok::kw_else))
809           return 0;
810 
811         // Don't merge a trailing multi-line control statement block like:
812         // } else if (foo &&
813         //            bar)
814         // { <-- current Line
815         //   baz();
816         // }
817         if (Line.First == Line.Last && Line.First->isNot(TT_FunctionLBrace) &&
818             Style.BraceWrapping.AfterControlStatement ==
819                 FormatStyle::BWACS_MultiLine) {
820           return 0;
821         }
822 
823         return 2;
824       }
825     } else if (I[1]->First->is(tok::l_brace)) {
826       if (I[1]->Last->is(TT_LineComment))
827         return 0;
828 
829       // Check for Limit <= 2 to account for the " {".
830       if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I)))
831         return 0;
832       Limit -= 2;
833       unsigned MergedLines = 0;
834       if (Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never ||
835           (I[1]->First == I[1]->Last && I + 2 != E &&
836            I[2]->First->is(tok::r_brace))) {
837         MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
838         // If we managed to merge the block, count the statement header, which
839         // is on a separate line.
840         if (MergedLines > 0)
841           ++MergedLines;
842       }
843       return MergedLines;
844     }
845     return 0;
846   }
847 
848   /// Returns the modified column limit for \p I if it is inside a macro and
849   /// needs a trailing '\'.
850   unsigned
851   limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
852                          SmallVectorImpl<AnnotatedLine *>::const_iterator E,
853                          unsigned Limit) {
854     if (I[0]->InPPDirective && I + 1 != E &&
855         !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) {
856       return Limit < 2 ? 0 : Limit - 2;
857     }
858     return Limit;
859   }
860 
861   bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
862                            unsigned Limit) {
863     if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore)
864       return false;
865     return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
866   }
867 
868   bool containsMustBreak(const AnnotatedLine *Line) {
869     for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next)
870       if (Tok->MustBreakBefore)
871         return true;
872     return false;
873   }
874 
875   void join(AnnotatedLine &A, const AnnotatedLine &B) {
876     assert(!A.Last->Next);
877     assert(!B.First->Previous);
878     if (B.Affected)
879       A.Affected = true;
880     A.Last->Next = B.First;
881     B.First->Previous = A.Last;
882     B.First->CanBreakBefore = true;
883     unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
884     for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
885       Tok->TotalLength += LengthA;
886       A.Last = Tok;
887     }
888   }
889 
890   const FormatStyle &Style;
891   const AdditionalKeywords &Keywords;
892   const SmallVectorImpl<AnnotatedLine *>::const_iterator End;
893 
894   SmallVectorImpl<AnnotatedLine *>::const_iterator Next;
895   const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines;
896 };
897 
898 static void markFinalized(FormatToken *Tok) {
899   for (; Tok; Tok = Tok->Next) {
900     Tok->Finalized = true;
901     for (AnnotatedLine *Child : Tok->Children)
902       markFinalized(Child->First);
903   }
904 }
905 
906 #ifndef NDEBUG
907 static void printLineState(const LineState &State) {
908   llvm::dbgs() << "State: ";
909   for (const ParenState &P : State.Stack) {
910     llvm::dbgs() << (P.Tok ? P.Tok->TokenText : "F") << "|" << P.Indent << "|"
911                  << P.LastSpace << "|" << P.NestedBlockIndent << " ";
912   }
913   llvm::dbgs() << State.NextToken->TokenText << "\n";
914 }
915 #endif
916 
917 /// Base class for classes that format one \c AnnotatedLine.
918 class LineFormatter {
919 public:
920   LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces,
921                 const FormatStyle &Style,
922                 UnwrappedLineFormatter *BlockFormatter)
923       : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
924         BlockFormatter(BlockFormatter) {}
925   virtual ~LineFormatter() {}
926 
927   /// Formats an \c AnnotatedLine and returns the penalty.
928   ///
929   /// If \p DryRun is \c false, directly applies the changes.
930   virtual unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
931                               unsigned FirstStartColumn, bool DryRun) = 0;
932 
933 protected:
934   /// If the \p State's next token is an r_brace closing a nested block,
935   /// format the nested block before it.
936   ///
937   /// Returns \c true if all children could be placed successfully and adapts
938   /// \p Penalty as well as \p State. If \p DryRun is false, also directly
939   /// creates changes using \c Whitespaces.
940   ///
941   /// The crucial idea here is that children always get formatted upon
942   /// encountering the closing brace right after the nested block. Now, if we
943   /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
944   /// \c false), the entire block has to be kept on the same line (which is only
945   /// possible if it fits on the line, only contains a single statement, etc.
946   ///
947   /// If \p NewLine is true, we format the nested block on separate lines, i.e.
948   /// break after the "{", format all lines with correct indentation and the put
949   /// the closing "}" on yet another new line.
950   ///
951   /// This enables us to keep the simple structure of the
952   /// \c UnwrappedLineFormatter, where we only have two options for each token:
953   /// break or don't break.
954   bool formatChildren(LineState &State, bool NewLine, bool DryRun,
955                       unsigned &Penalty) {
956     const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
957     FormatToken &Previous = *State.NextToken->Previous;
958     if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->isNot(BK_Block) ||
959         Previous.Children.size() == 0) {
960       // The previous token does not open a block. Nothing to do. We don't
961       // assert so that we can simply call this function for all tokens.
962       return true;
963     }
964 
965     if (NewLine) {
966       const ParenState &P = State.Stack.back();
967 
968       int AdditionalIndent =
969           P.Indent - Previous.Children[0]->Level * Style.IndentWidth;
970 
971       if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
972           P.NestedBlockIndent == P.LastSpace) {
973         if (State.NextToken->MatchingParen &&
974             State.NextToken->MatchingParen->is(TT_LambdaLBrace)) {
975           State.Stack.pop_back();
976         }
977         if (LBrace->is(TT_LambdaLBrace))
978           AdditionalIndent = 0;
979       }
980 
981       Penalty +=
982           BlockFormatter->format(Previous.Children, DryRun, AdditionalIndent,
983                                  /*FixBadIndentation=*/true);
984       return true;
985     }
986 
987     if (Previous.Children[0]->First->MustBreakBefore)
988       return false;
989 
990     // Cannot merge into one line if this line ends on a comment.
991     if (Previous.is(tok::comment))
992       return false;
993 
994     // Cannot merge multiple statements into a single line.
995     if (Previous.Children.size() > 1)
996       return false;
997 
998     const AnnotatedLine *Child = Previous.Children[0];
999     // We can't put the closing "}" on a line with a trailing comment.
1000     if (Child->Last->isTrailingComment())
1001       return false;
1002 
1003     // If the child line exceeds the column limit, we wouldn't want to merge it.
1004     // We add +2 for the trailing " }".
1005     if (Style.ColumnLimit > 0 &&
1006         Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) {
1007       return false;
1008     }
1009 
1010     if (!DryRun) {
1011       Whitespaces->replaceWhitespace(
1012           *Child->First, /*Newlines=*/0, /*Spaces=*/1,
1013           /*StartOfTokenColumn=*/State.Column, /*IsAligned=*/false,
1014           State.Line->InPPDirective);
1015     }
1016     Penalty +=
1017         formatLine(*Child, State.Column + 1, /*FirstStartColumn=*/0, DryRun);
1018 
1019     State.Column += 1 + Child->Last->TotalLength;
1020     return true;
1021   }
1022 
1023   ContinuationIndenter *Indenter;
1024 
1025 private:
1026   WhitespaceManager *Whitespaces;
1027   const FormatStyle &Style;
1028   UnwrappedLineFormatter *BlockFormatter;
1029 };
1030 
1031 /// Formatter that keeps the existing line breaks.
1032 class NoColumnLimitLineFormatter : public LineFormatter {
1033 public:
1034   NoColumnLimitLineFormatter(ContinuationIndenter *Indenter,
1035                              WhitespaceManager *Whitespaces,
1036                              const FormatStyle &Style,
1037                              UnwrappedLineFormatter *BlockFormatter)
1038       : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
1039 
1040   /// Formats the line, simply keeping all of the input's line breaking
1041   /// decisions.
1042   unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
1043                       unsigned FirstStartColumn, bool DryRun) override {
1044     assert(!DryRun);
1045     LineState State = Indenter->getInitialState(FirstIndent, FirstStartColumn,
1046                                                 &Line, /*DryRun=*/false);
1047     while (State.NextToken) {
1048       bool Newline =
1049           Indenter->mustBreak(State) ||
1050           (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
1051       unsigned Penalty = 0;
1052       formatChildren(State, Newline, /*DryRun=*/false, Penalty);
1053       Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
1054     }
1055     return 0;
1056   }
1057 };
1058 
1059 /// Formatter that puts all tokens into a single line without breaks.
1060 class NoLineBreakFormatter : public LineFormatter {
1061 public:
1062   NoLineBreakFormatter(ContinuationIndenter *Indenter,
1063                        WhitespaceManager *Whitespaces, const FormatStyle &Style,
1064                        UnwrappedLineFormatter *BlockFormatter)
1065       : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
1066 
1067   /// Puts all tokens into a single line.
1068   unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
1069                       unsigned FirstStartColumn, bool DryRun) override {
1070     unsigned Penalty = 0;
1071     LineState State =
1072         Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
1073     while (State.NextToken) {
1074       formatChildren(State, /*NewLine=*/false, DryRun, Penalty);
1075       Indenter->addTokenToState(
1076           State, /*Newline=*/State.NextToken->MustBreakBefore, DryRun);
1077     }
1078     return Penalty;
1079   }
1080 };
1081 
1082 /// Finds the best way to break lines.
1083 class OptimizingLineFormatter : public LineFormatter {
1084 public:
1085   OptimizingLineFormatter(ContinuationIndenter *Indenter,
1086                           WhitespaceManager *Whitespaces,
1087                           const FormatStyle &Style,
1088                           UnwrappedLineFormatter *BlockFormatter)
1089       : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
1090 
1091   /// Formats the line by finding the best line breaks with line lengths
1092   /// below the column limit.
1093   unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
1094                       unsigned FirstStartColumn, bool DryRun) override {
1095     LineState State =
1096         Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
1097 
1098     // If the ObjC method declaration does not fit on a line, we should format
1099     // it with one arg per line.
1100     if (State.Line->Type == LT_ObjCMethodDecl)
1101       State.Stack.back().BreakBeforeParameter = true;
1102 
1103     // Find best solution in solution space.
1104     return analyzeSolutionSpace(State, DryRun);
1105   }
1106 
1107 private:
1108   struct CompareLineStatePointers {
1109     bool operator()(LineState *obj1, LineState *obj2) const {
1110       return *obj1 < *obj2;
1111     }
1112   };
1113 
1114   /// A pair of <penalty, count> that is used to prioritize the BFS on.
1115   ///
1116   /// In case of equal penalties, we want to prefer states that were inserted
1117   /// first. During state generation we make sure that we insert states first
1118   /// that break the line as late as possible.
1119   typedef std::pair<unsigned, unsigned> OrderedPenalty;
1120 
1121   /// An edge in the solution space from \c Previous->State to \c State,
1122   /// inserting a newline dependent on the \c NewLine.
1123   struct StateNode {
1124     StateNode(const LineState &State, bool NewLine, StateNode *Previous)
1125         : State(State), NewLine(NewLine), Previous(Previous) {}
1126     LineState State;
1127     bool NewLine;
1128     StateNode *Previous;
1129   };
1130 
1131   /// An item in the prioritized BFS search queue. The \c StateNode's
1132   /// \c State has the given \c OrderedPenalty.
1133   typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
1134 
1135   /// The BFS queue type.
1136   typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
1137                               std::greater<QueueItem>>
1138       QueueType;
1139 
1140   /// Analyze the entire solution space starting from \p InitialState.
1141   ///
1142   /// This implements a variant of Dijkstra's algorithm on the graph that spans
1143   /// the solution space (\c LineStates are the nodes). The algorithm tries to
1144   /// find the shortest path (the one with lowest penalty) from \p InitialState
1145   /// to a state where all tokens are placed. Returns the penalty.
1146   ///
1147   /// If \p DryRun is \c false, directly applies the changes.
1148   unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun) {
1149     std::set<LineState *, CompareLineStatePointers> Seen;
1150 
1151     // Increasing count of \c StateNode items we have created. This is used to
1152     // create a deterministic order independent of the container.
1153     unsigned Count = 0;
1154     QueueType Queue;
1155 
1156     // Insert start element into queue.
1157     StateNode *RootNode =
1158         new (Allocator.Allocate()) StateNode(InitialState, false, nullptr);
1159     Queue.push(QueueItem(OrderedPenalty(0, Count), RootNode));
1160     ++Count;
1161 
1162     unsigned Penalty = 0;
1163 
1164     // While not empty, take first element and follow edges.
1165     while (!Queue.empty()) {
1166       Penalty = Queue.top().first.first;
1167       StateNode *Node = Queue.top().second;
1168       if (!Node->State.NextToken) {
1169         LLVM_DEBUG(llvm::dbgs()
1170                    << "\n---\nPenalty for line: " << Penalty << "\n");
1171         break;
1172       }
1173       Queue.pop();
1174 
1175       // Cut off the analysis of certain solutions if the analysis gets too
1176       // complex. See description of IgnoreStackForComparison.
1177       if (Count > 50000)
1178         Node->State.IgnoreStackForComparison = true;
1179 
1180       if (!Seen.insert(&Node->State).second) {
1181         // State already examined with lower penalty.
1182         continue;
1183       }
1184 
1185       FormatDecision LastFormat = Node->State.NextToken->getDecision();
1186       if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
1187         addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue);
1188       if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
1189         addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue);
1190     }
1191 
1192     if (Queue.empty()) {
1193       // We were unable to find a solution, do nothing.
1194       // FIXME: Add diagnostic?
1195       LLVM_DEBUG(llvm::dbgs() << "Could not find a solution.\n");
1196       return 0;
1197     }
1198 
1199     // Reconstruct the solution.
1200     if (!DryRun)
1201       reconstructPath(InitialState, Queue.top().second);
1202 
1203     LLVM_DEBUG(llvm::dbgs()
1204                << "Total number of analyzed states: " << Count << "\n");
1205     LLVM_DEBUG(llvm::dbgs() << "---\n");
1206 
1207     return Penalty;
1208   }
1209 
1210   /// Add the following state to the analysis queue \c Queue.
1211   ///
1212   /// Assume the current state is \p PreviousNode and has been reached with a
1213   /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
1214   void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
1215                            bool NewLine, unsigned *Count, QueueType *Queue) {
1216     if (NewLine && !Indenter->canBreak(PreviousNode->State))
1217       return;
1218     if (!NewLine && Indenter->mustBreak(PreviousNode->State))
1219       return;
1220 
1221     StateNode *Node = new (Allocator.Allocate())
1222         StateNode(PreviousNode->State, NewLine, PreviousNode);
1223     if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
1224       return;
1225 
1226     Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
1227 
1228     Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node));
1229     ++(*Count);
1230   }
1231 
1232   /// Applies the best formatting by reconstructing the path in the
1233   /// solution space that leads to \c Best.
1234   void reconstructPath(LineState &State, StateNode *Best) {
1235     llvm::SmallVector<StateNode *> Path;
1236     // We do not need a break before the initial token.
1237     while (Best->Previous) {
1238       Path.push_back(Best);
1239       Best = Best->Previous;
1240     }
1241     for (const auto &Node : llvm::reverse(Path)) {
1242       unsigned Penalty = 0;
1243       formatChildren(State, Node->NewLine, /*DryRun=*/false, Penalty);
1244       Penalty += Indenter->addTokenToState(State, Node->NewLine, false);
1245 
1246       LLVM_DEBUG({
1247         printLineState(Node->Previous->State);
1248         if (Node->NewLine) {
1249           llvm::dbgs() << "Penalty for placing "
1250                        << Node->Previous->State.NextToken->Tok.getName()
1251                        << " on a new line: " << Penalty << "\n";
1252         }
1253       });
1254     }
1255   }
1256 
1257   llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1258 };
1259 
1260 } // anonymous namespace
1261 
1262 unsigned UnwrappedLineFormatter::format(
1263     const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,
1264     int AdditionalIndent, bool FixBadIndentation, unsigned FirstStartColumn,
1265     unsigned NextStartColumn, unsigned LastStartColumn) {
1266   LineJoiner Joiner(Style, Keywords, Lines);
1267 
1268   // Try to look up already computed penalty in DryRun-mode.
1269   std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey(
1270       &Lines, AdditionalIndent);
1271   auto CacheIt = PenaltyCache.find(CacheKey);
1272   if (DryRun && CacheIt != PenaltyCache.end())
1273     return CacheIt->second;
1274 
1275   assert(!Lines.empty());
1276   unsigned Penalty = 0;
1277   LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
1278                                    AdditionalIndent);
1279   const AnnotatedLine *PrevPrevLine = nullptr;
1280   const AnnotatedLine *PreviousLine = nullptr;
1281   const AnnotatedLine *NextLine = nullptr;
1282 
1283   // The minimum level of consecutive lines that have been formatted.
1284   unsigned RangeMinLevel = UINT_MAX;
1285 
1286   bool FirstLine = true;
1287   for (const AnnotatedLine *Line =
1288            Joiner.getNextMergedLine(DryRun, IndentTracker);
1289        Line; PrevPrevLine = PreviousLine, PreviousLine = Line, Line = NextLine,
1290                            FirstLine = false) {
1291     assert(Line->First);
1292     const AnnotatedLine &TheLine = *Line;
1293     unsigned Indent = IndentTracker.getIndent();
1294 
1295     // We continue formatting unchanged lines to adjust their indent, e.g. if a
1296     // scope was added. However, we need to carefully stop doing this when we
1297     // exit the scope of affected lines to prevent indenting a the entire
1298     // remaining file if it currently missing a closing brace.
1299     bool PreviousRBrace =
1300         PreviousLine && PreviousLine->startsWith(tok::r_brace);
1301     bool ContinueFormatting =
1302         TheLine.Level > RangeMinLevel ||
1303         (TheLine.Level == RangeMinLevel && !PreviousRBrace &&
1304          !TheLine.startsWith(tok::r_brace));
1305 
1306     bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
1307                           Indent != TheLine.First->OriginalColumn;
1308     bool ShouldFormat = TheLine.Affected || FixIndentation;
1309     // We cannot format this line; if the reason is that the line had a
1310     // parsing error, remember that.
1311     if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {
1312       Status->FormatComplete = false;
1313       Status->Line =
1314           SourceMgr.getSpellingLineNumber(TheLine.First->Tok.getLocation());
1315     }
1316 
1317     if (ShouldFormat && TheLine.Type != LT_Invalid) {
1318       if (!DryRun) {
1319         bool LastLine = TheLine.First->is(tok::eof);
1320         formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,
1321                          LastLine ? LastStartColumn : NextStartColumn + Indent);
1322       }
1323 
1324       NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1325       unsigned ColumnLimit = getColumnLimit(TheLine.InPPDirective, NextLine);
1326       bool FitsIntoOneLine =
1327           TheLine.Last->TotalLength + Indent <= ColumnLimit ||
1328           (TheLine.Type == LT_ImportStatement &&
1329            (!Style.isJavaScript() || !Style.JavaScriptWrapImports)) ||
1330           (Style.isCSharp() &&
1331            TheLine.InPPDirective); // don't split #regions in C#
1332       if (Style.ColumnLimit == 0) {
1333         NoColumnLimitLineFormatter(Indenter, Whitespaces, Style, this)
1334             .formatLine(TheLine, NextStartColumn + Indent,
1335                         FirstLine ? FirstStartColumn : 0, DryRun);
1336       } else if (FitsIntoOneLine) {
1337         Penalty += NoLineBreakFormatter(Indenter, Whitespaces, Style, this)
1338                        .formatLine(TheLine, NextStartColumn + Indent,
1339                                    FirstLine ? FirstStartColumn : 0, DryRun);
1340       } else {
1341         Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)
1342                        .formatLine(TheLine, NextStartColumn + Indent,
1343                                    FirstLine ? FirstStartColumn : 0, DryRun);
1344       }
1345       RangeMinLevel = std::min(RangeMinLevel, TheLine.Level);
1346     } else {
1347       // If no token in the current line is affected, we still need to format
1348       // affected children.
1349       if (TheLine.ChildrenAffected) {
1350         for (const FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next)
1351           if (!Tok->Children.empty())
1352             format(Tok->Children, DryRun);
1353       }
1354 
1355       // Adapt following lines on the current indent level to the same level
1356       // unless the current \c AnnotatedLine is not at the beginning of a line.
1357       bool StartsNewLine =
1358           TheLine.First->NewlinesBefore > 0 || TheLine.First->IsFirst;
1359       if (StartsNewLine)
1360         IndentTracker.adjustToUnmodifiedLine(TheLine);
1361       if (!DryRun) {
1362         bool ReformatLeadingWhitespace =
1363             StartsNewLine && ((PreviousLine && PreviousLine->Affected) ||
1364                               TheLine.LeadingEmptyLinesAffected);
1365         // Format the first token.
1366         if (ReformatLeadingWhitespace) {
1367           formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines,
1368                            TheLine.First->OriginalColumn,
1369                            TheLine.First->OriginalColumn);
1370         } else {
1371           Whitespaces->addUntouchableToken(*TheLine.First,
1372                                            TheLine.InPPDirective);
1373         }
1374 
1375         // Notify the WhitespaceManager about the unchanged whitespace.
1376         for (FormatToken *Tok = TheLine.First->Next; Tok; Tok = Tok->Next)
1377           Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
1378       }
1379       NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1380       RangeMinLevel = UINT_MAX;
1381     }
1382     if (!DryRun)
1383       markFinalized(TheLine.First);
1384   }
1385   PenaltyCache[CacheKey] = Penalty;
1386   return Penalty;
1387 }
1388 
1389 void UnwrappedLineFormatter::formatFirstToken(
1390     const AnnotatedLine &Line, const AnnotatedLine *PreviousLine,
1391     const AnnotatedLine *PrevPrevLine,
1392     const SmallVectorImpl<AnnotatedLine *> &Lines, unsigned Indent,
1393     unsigned NewlineIndent) {
1394   FormatToken &RootToken = *Line.First;
1395   if (RootToken.is(tok::eof)) {
1396     unsigned Newlines = std::min(RootToken.NewlinesBefore, 1u);
1397     unsigned TokenIndent = Newlines ? NewlineIndent : 0;
1398     Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
1399                                    TokenIndent);
1400     return;
1401   }
1402   unsigned Newlines =
1403       std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
1404   // Remove empty lines before "}" where applicable.
1405   if (RootToken.is(tok::r_brace) &&
1406       (!RootToken.Next ||
1407        (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&
1408       // Do not remove empty lines before namespace closing "}".
1409       !getNamespaceToken(&Line, Lines)) {
1410     Newlines = std::min(Newlines, 1u);
1411   }
1412   // Remove empty lines at the start of nested blocks (lambdas/arrow functions)
1413   if (PreviousLine == nullptr && Line.Level > 0)
1414     Newlines = std::min(Newlines, 1u);
1415   if (Newlines == 0 && !RootToken.IsFirst)
1416     Newlines = 1;
1417   if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
1418     Newlines = 0;
1419 
1420   // Remove empty lines after "{".
1421   if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine &&
1422       PreviousLine->Last->is(tok::l_brace) &&
1423       !PreviousLine->startsWithNamespace() &&
1424       !(PrevPrevLine && PrevPrevLine->startsWithNamespace() &&
1425         PreviousLine->startsWith(tok::l_brace)) &&
1426       !startsExternCBlock(*PreviousLine)) {
1427     Newlines = 1;
1428   }
1429 
1430   // Insert or remove empty line before access specifiers.
1431   if (PreviousLine && RootToken.isAccessSpecifier()) {
1432     switch (Style.EmptyLineBeforeAccessModifier) {
1433     case FormatStyle::ELBAMS_Never:
1434       if (Newlines > 1)
1435         Newlines = 1;
1436       break;
1437     case FormatStyle::ELBAMS_Leave:
1438       Newlines = std::max(RootToken.NewlinesBefore, 1u);
1439       break;
1440     case FormatStyle::ELBAMS_LogicalBlock:
1441       if (PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && Newlines <= 1)
1442         Newlines = 2;
1443       if (PreviousLine->First->isAccessSpecifier())
1444         Newlines = 1; // Previous is an access modifier remove all new lines.
1445       break;
1446     case FormatStyle::ELBAMS_Always: {
1447       const FormatToken *previousToken;
1448       if (PreviousLine->Last->is(tok::comment))
1449         previousToken = PreviousLine->Last->getPreviousNonComment();
1450       else
1451         previousToken = PreviousLine->Last;
1452       if ((!previousToken || !previousToken->is(tok::l_brace)) && Newlines <= 1)
1453         Newlines = 2;
1454     } break;
1455     }
1456   }
1457 
1458   // Insert or remove empty line after access specifiers.
1459   if (PreviousLine && PreviousLine->First->isAccessSpecifier() &&
1460       (!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline)) {
1461     // EmptyLineBeforeAccessModifier is handling the case when two access
1462     // modifiers follow each other.
1463     if (!RootToken.isAccessSpecifier()) {
1464       switch (Style.EmptyLineAfterAccessModifier) {
1465       case FormatStyle::ELAAMS_Never:
1466         Newlines = 1;
1467         break;
1468       case FormatStyle::ELAAMS_Leave:
1469         Newlines = std::max(Newlines, 1u);
1470         break;
1471       case FormatStyle::ELAAMS_Always:
1472         if (RootToken.is(tok::r_brace)) // Do not add at end of class.
1473           Newlines = 1u;
1474         else
1475           Newlines = std::max(Newlines, 2u);
1476         break;
1477       }
1478     }
1479   }
1480 
1481   if (Newlines)
1482     Indent = NewlineIndent;
1483 
1484   // Preprocessor directives get indented before the hash only if specified. In
1485   // Javascript import statements are indented like normal statements.
1486   if (!Style.isJavaScript() &&
1487       Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
1488       (Line.Type == LT_PreprocessorDirective ||
1489        Line.Type == LT_ImportStatement)) {
1490     Indent = 0;
1491   }
1492 
1493   Whitespaces->replaceWhitespace(RootToken, Newlines, Indent, Indent,
1494                                  /*IsAligned=*/false,
1495                                  Line.InPPDirective &&
1496                                      !RootToken.HasUnescapedNewline);
1497 }
1498 
1499 unsigned
1500 UnwrappedLineFormatter::getColumnLimit(bool InPPDirective,
1501                                        const AnnotatedLine *NextLine) const {
1502   // In preprocessor directives reserve two chars for trailing " \" if the
1503   // next line continues the preprocessor directive.
1504   bool ContinuesPPDirective =
1505       InPPDirective &&
1506       // If there is no next line, this is likely a child line and the parent
1507       // continues the preprocessor directive.
1508       (!NextLine ||
1509        (NextLine->InPPDirective &&
1510         // If there is an unescaped newline between this line and the next, the
1511         // next line starts a new preprocessor directive.
1512         !NextLine->First->HasUnescapedNewline));
1513   return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0);
1514 }
1515 
1516 } // namespace format
1517 } // namespace clang
1518