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