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 "UnwrappedLineFormatter.h"
11 #include "WhitespaceManager.h"
12 #include "llvm/Support/Debug.h"
13 
14 #define DEBUG_TYPE "format-formatter"
15 
16 namespace clang {
17 namespace format {
18 
19 namespace {
20 
startsExternCBlock(const AnnotatedLine & Line)21 bool startsExternCBlock(const AnnotatedLine &Line) {
22   const FormatToken *Next = Line.First->getNextNonComment();
23   const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr;
24   return Line.First->is(tok::kw_extern) && Next && Next->isStringLiteral() &&
25          NextNext && NextNext->is(tok::l_brace);
26 }
27 
28 class LineJoiner {
29 public:
LineJoiner(const FormatStyle & Style)30   LineJoiner(const FormatStyle &Style) : Style(Style) {}
31 
32   /// \brief Calculates how many lines can be merged into 1 starting at \p I.
33   unsigned
tryFitMultipleLinesInOne(unsigned Indent,SmallVectorImpl<AnnotatedLine * >::const_iterator I,SmallVectorImpl<AnnotatedLine * >::const_iterator E)34   tryFitMultipleLinesInOne(unsigned Indent,
35                            SmallVectorImpl<AnnotatedLine *>::const_iterator I,
36                            SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
37     // We can never merge stuff if there are trailing line comments.
38     const AnnotatedLine *TheLine = *I;
39     if (TheLine->Last->is(TT_LineComment))
40       return 0;
41 
42     if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
43       return 0;
44 
45     unsigned Limit =
46         Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent;
47     // If we already exceed the column limit, we set 'Limit' to 0. The different
48     // tryMerge..() functions can then decide whether to still do merging.
49     Limit = TheLine->Last->TotalLength > Limit
50                 ? 0
51                 : Limit - TheLine->Last->TotalLength;
52 
53     if (I + 1 == E || I[1]->Type == LT_Invalid || I[1]->First->MustBreakBefore)
54       return 0;
55 
56     // FIXME: TheLine->Level != 0 might or might not be the right check to do.
57     // If necessary, change to something smarter.
58     bool MergeShortFunctions =
59         Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All ||
60         (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty &&
61          I[1]->First->is(tok::r_brace)) ||
62         (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline &&
63          TheLine->Level != 0);
64 
65     if (TheLine->Last->is(TT_FunctionLBrace) &&
66         TheLine->First != TheLine->Last) {
67       return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
68     }
69     if (TheLine->Last->is(tok::l_brace)) {
70       return Style.BreakBeforeBraces == FormatStyle::BS_Attach
71                  ? tryMergeSimpleBlock(I, E, Limit)
72                  : 0;
73     }
74     if (I[1]->First->is(TT_FunctionLBrace) &&
75         Style.BreakBeforeBraces != FormatStyle::BS_Attach) {
76       if (I[1]->Last->is(TT_LineComment))
77         return 0;
78 
79       // Check for Limit <= 2 to account for the " {".
80       if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
81         return 0;
82       Limit -= 2;
83 
84       unsigned MergedLines = 0;
85       if (MergeShortFunctions) {
86         MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
87         // If we managed to merge the block, count the function header, which is
88         // on a separate line.
89         if (MergedLines > 0)
90           ++MergedLines;
91       }
92       return MergedLines;
93     }
94     if (TheLine->First->is(tok::kw_if)) {
95       return Style.AllowShortIfStatementsOnASingleLine
96                  ? tryMergeSimpleControlStatement(I, E, Limit)
97                  : 0;
98     }
99     if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
100       return Style.AllowShortLoopsOnASingleLine
101                  ? tryMergeSimpleControlStatement(I, E, Limit)
102                  : 0;
103     }
104     if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
105       return Style.AllowShortCaseLabelsOnASingleLine
106                  ? tryMergeShortCaseLabels(I, E, Limit)
107                  : 0;
108     }
109     if (TheLine->InPPDirective &&
110         (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
111       return tryMergeSimplePPDirective(I, E, Limit);
112     }
113     return 0;
114   }
115 
116 private:
117   unsigned
tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine * >::const_iterator I,SmallVectorImpl<AnnotatedLine * >::const_iterator E,unsigned Limit)118   tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
119                             SmallVectorImpl<AnnotatedLine *>::const_iterator E,
120                             unsigned Limit) {
121     if (Limit == 0)
122       return 0;
123     if (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline)
124       return 0;
125     if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline)
126       return 0;
127     if (1 + I[1]->Last->TotalLength > Limit)
128       return 0;
129     return 1;
130   }
131 
tryMergeSimpleControlStatement(SmallVectorImpl<AnnotatedLine * >::const_iterator I,SmallVectorImpl<AnnotatedLine * >::const_iterator E,unsigned Limit)132   unsigned tryMergeSimpleControlStatement(
133       SmallVectorImpl<AnnotatedLine *>::const_iterator I,
134       SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
135     if (Limit == 0)
136       return 0;
137     if ((Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
138          Style.BreakBeforeBraces == FormatStyle::BS_GNU) &&
139         (I[1]->First->is(tok::l_brace) && !Style.AllowShortBlocksOnASingleLine))
140       return 0;
141     if (I[1]->InPPDirective != (*I)->InPPDirective ||
142         (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
143       return 0;
144     Limit = limitConsideringMacros(I + 1, E, Limit);
145     AnnotatedLine &Line = **I;
146     if (Line.Last->isNot(tok::r_paren))
147       return 0;
148     if (1 + I[1]->Last->TotalLength > Limit)
149       return 0;
150     if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for,
151                              tok::kw_while, TT_LineComment))
152       return 0;
153     // Only inline simple if's (no nested if or else).
154     if (I + 2 != E && Line.First->is(tok::kw_if) &&
155         I[2]->First->is(tok::kw_else))
156       return 0;
157     return 1;
158   }
159 
tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine * >::const_iterator I,SmallVectorImpl<AnnotatedLine * >::const_iterator E,unsigned Limit)160   unsigned tryMergeShortCaseLabels(
161       SmallVectorImpl<AnnotatedLine *>::const_iterator I,
162       SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
163     if (Limit == 0 || I + 1 == E ||
164         I[1]->First->isOneOf(tok::kw_case, tok::kw_default))
165       return 0;
166     unsigned NumStmts = 0;
167     unsigned Length = 0;
168     bool InPPDirective = I[0]->InPPDirective;
169     for (; NumStmts < 3; ++NumStmts) {
170       if (I + 1 + NumStmts == E)
171         break;
172       const AnnotatedLine *Line = I[1 + NumStmts];
173       if (Line->InPPDirective != InPPDirective)
174         break;
175       if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
176         break;
177       if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
178                                tok::kw_while, tok::comment))
179         return 0;
180       Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space.
181     }
182     if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
183       return 0;
184     return NumStmts;
185   }
186 
187   unsigned
tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine * >::const_iterator I,SmallVectorImpl<AnnotatedLine * >::const_iterator E,unsigned Limit)188   tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
189                       SmallVectorImpl<AnnotatedLine *>::const_iterator E,
190                       unsigned Limit) {
191     AnnotatedLine &Line = **I;
192 
193     // Don't merge ObjC @ keywords and methods.
194     if (Style.Language != FormatStyle::LK_Java &&
195         Line.First->isOneOf(tok::at, tok::minus, tok::plus))
196       return 0;
197 
198     // Check that the current line allows merging. This depends on whether we
199     // are in a control flow statements as well as several style flags.
200     if (Line.First->isOneOf(tok::kw_else, tok::kw_case))
201       return 0;
202     if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
203                             tok::kw_catch, tok::kw_for, tok::r_brace)) {
204       if (!Style.AllowShortBlocksOnASingleLine)
205         return 0;
206       if (!Style.AllowShortIfStatementsOnASingleLine &&
207           Line.First->is(tok::kw_if))
208         return 0;
209       if (!Style.AllowShortLoopsOnASingleLine &&
210           Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for))
211         return 0;
212       // FIXME: Consider an option to allow short exception handling clauses on
213       // a single line.
214       if (Line.First->isOneOf(tok::kw_try, tok::kw_catch))
215         return 0;
216     }
217 
218     FormatToken *Tok = I[1]->First;
219     if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
220         (Tok->getNextNonComment() == nullptr ||
221          Tok->getNextNonComment()->is(tok::semi))) {
222       // We merge empty blocks even if the line exceeds the column limit.
223       Tok->SpacesRequiredBefore = 0;
224       Tok->CanBreakBefore = true;
225       return 1;
226     } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace) &&
227                !startsExternCBlock(Line)) {
228       // We don't merge short records.
229       if (Line.First->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct))
230         return 0;
231 
232       // Check that we still have three lines and they fit into the limit.
233       if (I + 2 == E || I[2]->Type == LT_Invalid)
234         return 0;
235       Limit = limitConsideringMacros(I + 2, E, Limit);
236 
237       if (!nextTwoLinesFitInto(I, Limit))
238         return 0;
239 
240       // Second, check that the next line does not contain any braces - if it
241       // does, readability declines when putting it into a single line.
242       if (I[1]->Last->is(TT_LineComment))
243         return 0;
244       do {
245         if (Tok->is(tok::l_brace) && Tok->BlockKind != BK_BracedInit)
246           return 0;
247         Tok = Tok->Next;
248       } while (Tok);
249 
250       // Last, check that the third line starts with a closing brace.
251       Tok = I[2]->First;
252       if (Tok->isNot(tok::r_brace))
253         return 0;
254 
255       return 2;
256     }
257     return 0;
258   }
259 
260   /// Returns the modified column limit for \p I if it is inside a macro and
261   /// needs a trailing '\'.
262   unsigned
limitConsideringMacros(SmallVectorImpl<AnnotatedLine * >::const_iterator I,SmallVectorImpl<AnnotatedLine * >::const_iterator E,unsigned Limit)263   limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
264                          SmallVectorImpl<AnnotatedLine *>::const_iterator E,
265                          unsigned Limit) {
266     if (I[0]->InPPDirective && I + 1 != E &&
267         !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) {
268       return Limit < 2 ? 0 : Limit - 2;
269     }
270     return Limit;
271   }
272 
nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine * >::const_iterator I,unsigned Limit)273   bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
274                            unsigned Limit) {
275     if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore)
276       return false;
277     return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
278   }
279 
containsMustBreak(const AnnotatedLine * Line)280   bool containsMustBreak(const AnnotatedLine *Line) {
281     for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
282       if (Tok->MustBreakBefore)
283         return true;
284     }
285     return false;
286   }
287 
288   const FormatStyle &Style;
289 };
290 
291 class NoColumnLimitFormatter {
292 public:
NoColumnLimitFormatter(ContinuationIndenter * Indenter)293   NoColumnLimitFormatter(ContinuationIndenter *Indenter) : Indenter(Indenter) {}
294 
295   /// \brief Formats the line starting at \p State, simply keeping all of the
296   /// input's line breaking decisions.
format(unsigned FirstIndent,const AnnotatedLine * Line)297   void format(unsigned FirstIndent, const AnnotatedLine *Line) {
298     LineState State =
299         Indenter->getInitialState(FirstIndent, Line, /*DryRun=*/false);
300     while (State.NextToken) {
301       bool Newline =
302           Indenter->mustBreak(State) ||
303           (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
304       Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
305     }
306   }
307 
308 private:
309   ContinuationIndenter *Indenter;
310 };
311 
312 } // namespace
313 
314 unsigned
format(const SmallVectorImpl<AnnotatedLine * > & Lines,bool DryRun,int AdditionalIndent,bool FixBadIndentation)315 UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
316                                bool DryRun, int AdditionalIndent,
317                                bool FixBadIndentation) {
318   LineJoiner Joiner(Style);
319 
320   // Try to look up already computed penalty in DryRun-mode.
321   std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey(
322       &Lines, AdditionalIndent);
323   auto CacheIt = PenaltyCache.find(CacheKey);
324   if (DryRun && CacheIt != PenaltyCache.end())
325     return CacheIt->second;
326 
327   assert(!Lines.empty());
328   unsigned Penalty = 0;
329   std::vector<int> IndentForLevel;
330   for (unsigned i = 0, e = Lines[0]->Level; i != e; ++i)
331     IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
332   const AnnotatedLine *PreviousLine = nullptr;
333   for (SmallVectorImpl<AnnotatedLine *>::const_iterator I = Lines.begin(),
334                                                         E = Lines.end();
335        I != E; ++I) {
336     const AnnotatedLine &TheLine = **I;
337     const FormatToken *FirstTok = TheLine.First;
338     int Offset = getIndentOffset(*FirstTok);
339 
340     // Determine indent and try to merge multiple unwrapped lines.
341     unsigned Indent;
342     if (TheLine.InPPDirective) {
343       Indent = TheLine.Level * Style.IndentWidth;
344     } else {
345       while (IndentForLevel.size() <= TheLine.Level)
346         IndentForLevel.push_back(-1);
347       IndentForLevel.resize(TheLine.Level + 1);
348       Indent = getIndent(IndentForLevel, TheLine.Level);
349     }
350     unsigned LevelIndent = Indent;
351     if (static_cast<int>(Indent) + Offset >= 0)
352       Indent += Offset;
353 
354     // Merge multiple lines if possible.
355     unsigned MergedLines = Joiner.tryFitMultipleLinesInOne(Indent, I, E);
356     if (MergedLines > 0 && Style.ColumnLimit == 0) {
357       // Disallow line merging if there is a break at the start of one of the
358       // input lines.
359       for (unsigned i = 0; i < MergedLines; ++i) {
360         if (I[i + 1]->First->NewlinesBefore > 0)
361           MergedLines = 0;
362       }
363     }
364     if (!DryRun) {
365       for (unsigned i = 0; i < MergedLines; ++i) {
366         join(*I[i], *I[i + 1]);
367       }
368     }
369     I += MergedLines;
370 
371     bool FixIndentation =
372         FixBadIndentation && (LevelIndent != FirstTok->OriginalColumn);
373     if (TheLine.First->is(tok::eof)) {
374       if (PreviousLine && PreviousLine->Affected && !DryRun) {
375         // Remove the file's trailing whitespace.
376         unsigned Newlines = std::min(FirstTok->NewlinesBefore, 1u);
377         Whitespaces->replaceWhitespace(*TheLine.First, Newlines,
378                                        /*IndentLevel=*/0, /*Spaces=*/0,
379                                        /*TargetColumn=*/0);
380       }
381     } else if (TheLine.Type != LT_Invalid &&
382                (TheLine.Affected || FixIndentation)) {
383       if (FirstTok->WhitespaceRange.isValid()) {
384         if (!DryRun)
385           formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level, Indent,
386                            TheLine.InPPDirective);
387       } else {
388         Indent = LevelIndent = FirstTok->OriginalColumn;
389       }
390 
391       // If everything fits on a single line, just put it there.
392       unsigned ColumnLimit = Style.ColumnLimit;
393       if (I + 1 != E) {
394         AnnotatedLine *NextLine = I[1];
395         if (NextLine->InPPDirective && !NextLine->First->HasUnescapedNewline)
396           ColumnLimit = getColumnLimit(TheLine.InPPDirective);
397       }
398 
399       if (TheLine.Last->TotalLength + Indent <= ColumnLimit ||
400           TheLine.Type == LT_ImportStatement) {
401         LineState State = Indenter->getInitialState(Indent, &TheLine, DryRun);
402         while (State.NextToken) {
403           formatChildren(State, /*Newline=*/false, /*DryRun=*/false, Penalty);
404           Indenter->addTokenToState(State, /*Newline=*/false, DryRun);
405         }
406       } else if (Style.ColumnLimit == 0) {
407         // FIXME: Implement nested blocks for ColumnLimit = 0.
408         NoColumnLimitFormatter Formatter(Indenter);
409         if (!DryRun)
410           Formatter.format(Indent, &TheLine);
411       } else {
412         Penalty += format(TheLine, Indent, DryRun);
413       }
414 
415       if (!TheLine.InPPDirective)
416         IndentForLevel[TheLine.Level] = LevelIndent;
417     } else if (TheLine.ChildrenAffected) {
418       format(TheLine.Children, DryRun);
419     } else {
420       // Format the first token if necessary, and notify the WhitespaceManager
421       // about the unchanged whitespace.
422       for (FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) {
423         if (Tok == TheLine.First && (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
424           unsigned LevelIndent = Tok->OriginalColumn;
425           if (!DryRun) {
426             // Remove trailing whitespace of the previous line.
427             if ((PreviousLine && PreviousLine->Affected) ||
428                 TheLine.LeadingEmptyLinesAffected) {
429               formatFirstToken(*Tok, PreviousLine, TheLine.Level, LevelIndent,
430                                TheLine.InPPDirective);
431             } else {
432               Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
433             }
434           }
435 
436           if (static_cast<int>(LevelIndent) - Offset >= 0)
437             LevelIndent -= Offset;
438           if (Tok->isNot(tok::comment) && !TheLine.InPPDirective)
439             IndentForLevel[TheLine.Level] = LevelIndent;
440         } else if (!DryRun) {
441           Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
442         }
443       }
444     }
445     if (!DryRun) {
446       for (FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) {
447         Tok->Finalized = true;
448       }
449     }
450     PreviousLine = *I;
451   }
452   PenaltyCache[CacheKey] = Penalty;
453   return Penalty;
454 }
455 
format(const AnnotatedLine & Line,unsigned FirstIndent,bool DryRun)456 unsigned UnwrappedLineFormatter::format(const AnnotatedLine &Line,
457                                         unsigned FirstIndent, bool DryRun) {
458   LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
459 
460   // If the ObjC method declaration does not fit on a line, we should format
461   // it with one arg per line.
462   if (State.Line->Type == LT_ObjCMethodDecl)
463     State.Stack.back().BreakBeforeParameter = true;
464 
465   // Find best solution in solution space.
466   return analyzeSolutionSpace(State, DryRun);
467 }
468 
formatFirstToken(FormatToken & RootToken,const AnnotatedLine * PreviousLine,unsigned IndentLevel,unsigned Indent,bool InPPDirective)469 void UnwrappedLineFormatter::formatFirstToken(FormatToken &RootToken,
470                                               const AnnotatedLine *PreviousLine,
471                                               unsigned IndentLevel,
472                                               unsigned Indent,
473                                               bool InPPDirective) {
474   unsigned Newlines =
475       std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
476   // Remove empty lines before "}" where applicable.
477   if (RootToken.is(tok::r_brace) &&
478       (!RootToken.Next ||
479        (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
480     Newlines = std::min(Newlines, 1u);
481   if (Newlines == 0 && !RootToken.IsFirst)
482     Newlines = 1;
483   if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
484     Newlines = 0;
485 
486   // Remove empty lines after "{".
487   if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine &&
488       PreviousLine->Last->is(tok::l_brace) &&
489       PreviousLine->First->isNot(tok::kw_namespace) &&
490       !startsExternCBlock(*PreviousLine))
491     Newlines = 1;
492 
493   // Insert extra new line before access specifiers.
494   if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
495       RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
496     ++Newlines;
497 
498   // Remove empty lines after access specifiers.
499   if (PreviousLine && PreviousLine->First->isAccessSpecifier())
500     Newlines = std::min(1u, Newlines);
501 
502   Whitespaces->replaceWhitespace(RootToken, Newlines, IndentLevel, Indent,
503                                  Indent, InPPDirective &&
504                                              !RootToken.HasUnescapedNewline);
505 }
506 
507 /// \brief Get the indent of \p Level from \p IndentForLevel.
508 ///
509 /// \p IndentForLevel must contain the indent for the level \c l
510 /// at \p IndentForLevel[l], or a value < 0 if the indent for
511 /// that level is unknown.
getIndent(ArrayRef<int> IndentForLevel,unsigned Level)512 unsigned UnwrappedLineFormatter::getIndent(ArrayRef<int> IndentForLevel,
513                                            unsigned Level) {
514   if (IndentForLevel[Level] != -1)
515     return IndentForLevel[Level];
516   if (Level == 0)
517     return 0;
518   return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
519 }
520 
join(AnnotatedLine & A,const AnnotatedLine & B)521 void UnwrappedLineFormatter::join(AnnotatedLine &A, const AnnotatedLine &B) {
522   assert(!A.Last->Next);
523   assert(!B.First->Previous);
524   if (B.Affected)
525     A.Affected = true;
526   A.Last->Next = B.First;
527   B.First->Previous = A.Last;
528   B.First->CanBreakBefore = true;
529   unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
530   for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
531     Tok->TotalLength += LengthA;
532     A.Last = Tok;
533   }
534 }
535 
analyzeSolutionSpace(LineState & InitialState,bool DryRun)536 unsigned UnwrappedLineFormatter::analyzeSolutionSpace(LineState &InitialState,
537                                                       bool DryRun) {
538   std::set<LineState *, CompareLineStatePointers> Seen;
539 
540   // Increasing count of \c StateNode items we have created. This is used to
541   // create a deterministic order independent of the container.
542   unsigned Count = 0;
543   QueueType Queue;
544 
545   // Insert start element into queue.
546   StateNode *Node =
547       new (Allocator.Allocate()) StateNode(InitialState, false, nullptr);
548   Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
549   ++Count;
550 
551   unsigned Penalty = 0;
552 
553   // While not empty, take first element and follow edges.
554   while (!Queue.empty()) {
555     Penalty = Queue.top().first.first;
556     StateNode *Node = Queue.top().second;
557     if (!Node->State.NextToken) {
558       DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
559       break;
560     }
561     Queue.pop();
562 
563     // Cut off the analysis of certain solutions if the analysis gets too
564     // complex. See description of IgnoreStackForComparison.
565     if (Count > 10000)
566       Node->State.IgnoreStackForComparison = true;
567 
568     if (!Seen.insert(&Node->State).second)
569       // State already examined with lower penalty.
570       continue;
571 
572     FormatDecision LastFormat = Node->State.NextToken->Decision;
573     if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
574       addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue);
575     if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
576       addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue);
577   }
578 
579   if (Queue.empty()) {
580     // We were unable to find a solution, do nothing.
581     // FIXME: Add diagnostic?
582     DEBUG(llvm::dbgs() << "Could not find a solution.\n");
583     return 0;
584   }
585 
586   // Reconstruct the solution.
587   if (!DryRun)
588     reconstructPath(InitialState, Queue.top().second);
589 
590   DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
591   DEBUG(llvm::dbgs() << "---\n");
592 
593   return Penalty;
594 }
595 
596 #ifndef NDEBUG
printLineState(const LineState & State)597 static void printLineState(const LineState &State) {
598   llvm::dbgs() << "State: ";
599   for (const ParenState &P : State.Stack) {
600     llvm::dbgs() << P.Indent << "|" << P.LastSpace << "|" << P.NestedBlockIndent
601                  << " ";
602   }
603   llvm::dbgs() << State.NextToken->TokenText << "\n";
604 }
605 #endif
606 
reconstructPath(LineState & State,StateNode * Current)607 void UnwrappedLineFormatter::reconstructPath(LineState &State,
608                                              StateNode *Current) {
609   std::deque<StateNode *> Path;
610   // We do not need a break before the initial token.
611   while (Current->Previous) {
612     Path.push_front(Current);
613     Current = Current->Previous;
614   }
615   for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
616        I != E; ++I) {
617     unsigned Penalty = 0;
618     formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
619     Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
620 
621     DEBUG({
622       printLineState((*I)->Previous->State);
623       if ((*I)->NewLine) {
624         llvm::dbgs() << "Penalty for placing "
625                      << (*I)->Previous->State.NextToken->Tok.getName() << ": "
626                      << Penalty << "\n";
627       }
628     });
629   }
630 }
631 
addNextStateToQueue(unsigned Penalty,StateNode * PreviousNode,bool NewLine,unsigned * Count,QueueType * Queue)632 void UnwrappedLineFormatter::addNextStateToQueue(unsigned Penalty,
633                                                  StateNode *PreviousNode,
634                                                  bool NewLine, unsigned *Count,
635                                                  QueueType *Queue) {
636   if (NewLine && !Indenter->canBreak(PreviousNode->State))
637     return;
638   if (!NewLine && Indenter->mustBreak(PreviousNode->State))
639     return;
640 
641   StateNode *Node = new (Allocator.Allocate())
642       StateNode(PreviousNode->State, NewLine, PreviousNode);
643   if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
644     return;
645 
646   Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
647 
648   Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node));
649   ++(*Count);
650 }
651 
formatChildren(LineState & State,bool NewLine,bool DryRun,unsigned & Penalty)652 bool UnwrappedLineFormatter::formatChildren(LineState &State, bool NewLine,
653                                             bool DryRun, unsigned &Penalty) {
654   FormatToken &Previous = *State.NextToken->Previous;
655   const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
656   if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->BlockKind != BK_Block ||
657       Previous.Children.size() == 0)
658     // The previous token does not open a block. Nothing to do. We don't
659     // assert so that we can simply call this function for all tokens.
660     return true;
661 
662   if (NewLine) {
663     int AdditionalIndent = State.Stack.back().Indent -
664                            Previous.Children[0]->Level * Style.IndentWidth;
665 
666     Penalty += format(Previous.Children, DryRun, AdditionalIndent,
667                       /*FixBadIndentation=*/true);
668     return true;
669   }
670 
671   if (Previous.Children[0]->First->MustBreakBefore)
672     return false;
673 
674   // Cannot merge multiple statements into a single line.
675   if (Previous.Children.size() > 1)
676     return false;
677 
678   // Cannot merge into one line if this line ends on a comment.
679   if (Previous.is(tok::comment))
680     return false;
681 
682   // We can't put the closing "}" on a line with a trailing comment.
683   if (Previous.Children[0]->Last->isTrailingComment())
684     return false;
685 
686   // If the child line exceeds the column limit, we wouldn't want to merge it.
687   // We add +2 for the trailing " }".
688   if (Style.ColumnLimit > 0 &&
689       Previous.Children[0]->Last->TotalLength + State.Column + 2 >
690           Style.ColumnLimit)
691     return false;
692 
693   if (!DryRun) {
694     Whitespaces->replaceWhitespace(
695         *Previous.Children[0]->First,
696         /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1,
697         /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
698   }
699   Penalty += format(*Previous.Children[0], State.Column + 1, DryRun);
700 
701   State.Column += 1 + Previous.Children[0]->Last->TotalLength;
702   return true;
703 }
704 
705 } // namespace format
706 } // namespace clang
707