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