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