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