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