1 //===--- ContinuationIndenter.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 /// \file
11 /// \brief This file implements the continuation indenter.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #define DEBUG_TYPE "format-formatter"
16 
17 #include "BreakableToken.h"
18 #include "ContinuationIndenter.h"
19 #include "WhitespaceManager.h"
20 #include "clang/Basic/OperatorPrecedence.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Format/Format.h"
23 #include "llvm/Support/Debug.h"
24 #include <string>
25 
26 namespace clang {
27 namespace format {
28 
29 // Returns the length of everything up to the first possible line break after
30 // the ), ], } or > matching \c Tok.
31 static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
32   if (Tok.MatchingParen == NULL)
33     return 0;
34   FormatToken *End = Tok.MatchingParen;
35   while (End->Next && !End->Next->CanBreakBefore) {
36     End = End->Next;
37   }
38   return End->TotalLength - Tok.TotalLength + 1;
39 }
40 
41 // Returns \c true if \c Tok is the "." or "->" of a call and starts the next
42 // segment of a builder type call.
43 static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
44   return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
45 }
46 
47 // Returns \c true if \c Current starts a new parameter.
48 static bool startsNextParameter(const FormatToken &Current,
49                                 const FormatStyle &Style) {
50   const FormatToken &Previous = *Current.Previous;
51   if (Current.Type == TT_CtorInitializerComma &&
52       Style.BreakConstructorInitializersBeforeComma)
53     return true;
54   return Previous.is(tok::comma) && !Current.isTrailingComment() &&
55          (Previous.Type != TT_CtorInitializerComma ||
56           !Style.BreakConstructorInitializersBeforeComma);
57 }
58 
59 ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
60                                            SourceManager &SourceMgr,
61                                            WhitespaceManager &Whitespaces,
62                                            encoding::Encoding Encoding,
63                                            bool BinPackInconclusiveFunctions)
64     : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces),
65       Encoding(Encoding),
66       BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {}
67 
68 LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
69                                                 const AnnotatedLine *Line,
70                                                 bool DryRun) {
71   LineState State;
72   State.FirstIndent = FirstIndent;
73   State.Column = FirstIndent;
74   State.Line = Line;
75   State.NextToken = Line->First;
76   State.Stack.push_back(ParenState(FirstIndent, Line->Level, FirstIndent,
77                                    /*AvoidBinPacking=*/false,
78                                    /*NoLineBreak=*/false));
79   State.LineContainsContinuedForLoopSection = false;
80   State.ParenLevel = 0;
81   State.StartOfStringLiteral = 0;
82   State.StartOfLineLevel = State.ParenLevel;
83   State.LowestLevelOnLine = State.ParenLevel;
84   State.IgnoreStackForComparison = false;
85 
86   // The first token has already been indented and thus consumed.
87   moveStateToNextToken(State, DryRun, /*Newline=*/false);
88   return State;
89 }
90 
91 bool ContinuationIndenter::canBreak(const LineState &State) {
92   const FormatToken &Current = *State.NextToken;
93   const FormatToken &Previous = *Current.Previous;
94   assert(&Previous == Current.Previous);
95   if (!Current.CanBreakBefore &&
96       !(Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace))
97     return false;
98   // The opening "{" of a braced list has to be on the same line as the first
99   // element if it is nested in another braced init list or function call.
100   if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
101       Previous.BlockKind == BK_BracedInit && Previous.Previous &&
102       Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
103     return false;
104   // This prevents breaks like:
105   //   ...
106   //   SomeParameter, OtherParameter).DoSomething(
107   //   ...
108   // As they hide "DoSomething" and are generally bad for readability.
109   if (Previous.opensScope() && State.LowestLevelOnLine < State.StartOfLineLevel)
110     return false;
111   if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
112     return false;
113   return !State.Stack.back().NoLineBreak;
114 }
115 
116 bool ContinuationIndenter::mustBreak(const LineState &State) {
117   const FormatToken &Current = *State.NextToken;
118   const FormatToken &Previous = *Current.Previous;
119   if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
120     return true;
121   if ((!Style.Cpp11BracedListStyle ||
122        (Current.MatchingParen &&
123         Current.MatchingParen->BlockKind == BK_Block)) &&
124       Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
125     return true;
126   if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
127     return true;
128   if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
129        Current.is(tok::question) ||
130        (Current.Type == TT_ConditionalExpr && Previous.isNot(tok::question))) &&
131       State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
132       !Current.isOneOf(tok::r_paren, tok::r_brace))
133     return true;
134   if (Style.AlwaysBreakBeforeMultilineStrings &&
135       State.Column > State.Stack.back().Indent && // Breaking saves columns.
136       !Previous.isOneOf(tok::kw_return, tok::lessless) &&
137       Previous.Type != TT_InlineASMColon && NextIsMultilineString(State))
138     return true;
139   if (Previous.Type == TT_ObjCDictLiteral && Previous.is(tok::l_brace) &&
140       getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State))
141     return true;
142 
143   if (!Style.BreakBeforeBinaryOperators) {
144     // If we need to break somewhere inside the LHS of a binary expression, we
145     // should also break after the operator. Otherwise, the formatting would
146     // hide the operator precedence, e.g. in:
147     //   if (aaaaaaaaaaaaaa ==
148     //           bbbbbbbbbbbbbb && c) {..
149     // For comparisons, we only apply this rule, if the LHS is a binary
150     // expression itself as otherwise, the line breaks seem superfluous.
151     // We need special cases for ">>" which we have split into two ">" while
152     // lexing in order to make template parsing easier.
153     //
154     // FIXME: We'll need something similar for styles that break before binary
155     // operators.
156     bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
157                          Previous.getPrecedence() == prec::Equality) &&
158                         Previous.Previous &&
159                         Previous.Previous->Type != TT_BinaryOperator; // For >>.
160     bool LHSIsBinaryExpr =
161         Previous.Previous && Previous.Previous->EndsBinaryExpression;
162     if (Previous.Type == TT_BinaryOperator &&
163         (!IsComparison || LHSIsBinaryExpr) &&
164         Current.Type != TT_BinaryOperator && // For >>.
165         !Current.isTrailingComment() &&
166         !Previous.isOneOf(tok::lessless, tok::question) &&
167         Previous.getPrecedence() != prec::Assignment &&
168         State.Stack.back().BreakBeforeParameter)
169       return true;
170   }
171 
172   // Same as above, but for the first "<<" operator.
173   if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter &&
174       State.Stack.back().FirstLessLess == 0)
175     return true;
176 
177   // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
178   // out whether it is the first parameter. Clean this up.
179   if (Current.Type == TT_ObjCSelectorName &&
180       Current.LongestObjCSelectorName == 0 &&
181       State.Stack.back().BreakBeforeParameter)
182     return true;
183   if ((Current.Type == TT_CtorInitializerColon ||
184        (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0 &&
185         !Current.isTrailingComment())))
186     return true;
187 
188   if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
189       State.Line->MightBeFunctionDecl &&
190       State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0)
191     return true;
192   if (startsSegmentOfBuilderTypeCall(Current) &&
193       (State.Stack.back().CallContinuation != 0 ||
194        (State.Stack.back().BreakBeforeParameter &&
195         State.Stack.back().ContainsUnwrappedBuilder)))
196     return true;
197   return false;
198 }
199 
200 unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
201                                                bool DryRun,
202                                                unsigned ExtraSpaces) {
203   const FormatToken &Current = *State.NextToken;
204 
205   if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
206     // FIXME: Is this correct?
207     int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
208                                State.NextToken->WhitespaceRange.getEnd()) -
209                            SourceMgr.getSpellingColumnNumber(
210                                State.NextToken->WhitespaceRange.getBegin());
211     State.Column += WhitespaceLength + State.NextToken->ColumnWidth;
212     State.NextToken = State.NextToken->Next;
213     return 0;
214   }
215 
216   unsigned Penalty = 0;
217   if (Newline)
218     Penalty = addTokenOnNewLine(State, DryRun);
219   else
220     addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
221 
222   return moveStateToNextToken(State, DryRun, Newline) + Penalty;
223 }
224 
225 void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
226                                                  unsigned ExtraSpaces) {
227   FormatToken &Current = *State.NextToken;
228   const FormatToken &Previous = *State.NextToken->Previous;
229   if (Current.is(tok::equal) &&
230       (State.Line->First->is(tok::kw_for) || State.ParenLevel == 0) &&
231       State.Stack.back().VariablePos == 0) {
232     State.Stack.back().VariablePos = State.Column;
233     // Move over * and & if they are bound to the variable name.
234     const FormatToken *Tok = &Previous;
235     while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
236       State.Stack.back().VariablePos -= Tok->ColumnWidth;
237       if (Tok->SpacesRequiredBefore != 0)
238         break;
239       Tok = Tok->Previous;
240     }
241     if (Previous.PartOfMultiVariableDeclStmt)
242       State.Stack.back().LastSpace = State.Stack.back().VariablePos;
243   }
244 
245   unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
246 
247   if (!DryRun)
248     Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0,
249                                   Spaces, State.Column + Spaces);
250 
251   if (Current.Type == TT_ObjCSelectorName && State.Stack.back().ColonPos == 0) {
252     if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
253         State.Column + Spaces + Current.ColumnWidth)
254       State.Stack.back().ColonPos =
255           State.Stack.back().Indent + Current.LongestObjCSelectorName;
256     else
257       State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth;
258   }
259 
260   if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
261       Current.Type != TT_LineComment)
262     State.Stack.back().Indent = State.Column + Spaces;
263   if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
264     State.Stack.back().NoLineBreak = true;
265   if (startsSegmentOfBuilderTypeCall(Current))
266     State.Stack.back().ContainsUnwrappedBuilder = true;
267 
268   State.Column += Spaces;
269   if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
270     // Treat the condition inside an if as if it was a second function
271     // parameter, i.e. let nested calls have a continuation indent.
272     State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
273   else if (Previous.is(tok::comma))
274     State.Stack.back().LastSpace = State.Column;
275   else if ((Previous.Type == TT_BinaryOperator ||
276             Previous.Type == TT_ConditionalExpr ||
277             Previous.Type == TT_UnaryOperator ||
278             Previous.Type == TT_CtorInitializerColon) &&
279            (Previous.getPrecedence() != prec::Assignment ||
280             Current.StartsBinaryExpression))
281     // Always indent relative to the RHS of the expression unless this is a
282     // simple assignment without binary expression on the RHS. Also indent
283     // relative to unary operators and the colons of constructor initializers.
284     State.Stack.back().LastSpace = State.Column;
285   else if (Previous.Type == TT_InheritanceColon) {
286     State.Stack.back().Indent = State.Column;
287     State.Stack.back().LastSpace = State.Column;
288   } else if (Previous.opensScope()) {
289     // If a function has a trailing call, indent all parameters from the
290     // opening parenthesis. This avoids confusing indents like:
291     //   OuterFunction(InnerFunctionCall( // break
292     //       ParameterToInnerFunction))   // break
293     //       .SecondInnerFunctionCall();
294     bool HasTrailingCall = false;
295     if (Previous.MatchingParen) {
296       const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
297       HasTrailingCall = Next && Next->isMemberAccess();
298     }
299     if (HasTrailingCall &&
300         State.Stack[State.Stack.size() - 2].CallContinuation == 0)
301       State.Stack.back().LastSpace = State.Column;
302   }
303 }
304 
305 unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
306                                                  bool DryRun) {
307   FormatToken &Current = *State.NextToken;
308   const FormatToken &Previous = *State.NextToken->Previous;
309   // If we are continuing an expression, we want to use the continuation indent.
310   unsigned ContinuationIndent =
311       std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
312       Style.ContinuationIndentWidth;
313   // Extra penalty that needs to be added because of the way certain line
314   // breaks are chosen.
315   unsigned Penalty = 0;
316 
317   const FormatToken *PreviousNonComment =
318       State.NextToken->getPreviousNonComment();
319   // The first line break on any ParenLevel causes an extra penalty in order
320   // prefer similar line breaks.
321   if (!State.Stack.back().ContainsLineBreak)
322     Penalty += 15;
323   State.Stack.back().ContainsLineBreak = true;
324 
325   Penalty += State.NextToken->SplitPenalty;
326 
327   // Breaking before the first "<<" is generally not desirable if the LHS is
328   // short.
329   if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
330       State.Column <= Style.ColumnLimit / 2)
331     Penalty += Style.PenaltyBreakFirstLessLess;
332 
333   if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {
334     State.Column = State.FirstIndent;
335   } else if (Current.is(tok::r_brace)) {
336     if (Current.MatchingParen &&
337         (Current.MatchingParen->BlockKind == BK_BracedInit ||
338          !Current.MatchingParen->Children.empty()))
339       State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
340     else
341       State.Column = State.FirstIndent;
342   } else if (Current.is(tok::string_literal) &&
343              State.StartOfStringLiteral != 0) {
344     State.Column = State.StartOfStringLiteral;
345     State.Stack.back().BreakBeforeParameter = true;
346   } else if (Current.is(tok::lessless) &&
347              State.Stack.back().FirstLessLess != 0) {
348     State.Column = State.Stack.back().FirstLessLess;
349   } else if (Current.isMemberAccess()) {
350     if (State.Stack.back().CallContinuation == 0) {
351       State.Column = ContinuationIndent;
352       State.Stack.back().CallContinuation = State.Column;
353     } else {
354       State.Column = State.Stack.back().CallContinuation;
355     }
356   } else if (Current.Type == TT_ConditionalExpr) {
357     State.Column = State.Stack.back().QuestionColumn;
358   } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) {
359     State.Column = State.Stack.back().VariablePos;
360   } else if ((PreviousNonComment &&
361               PreviousNonComment->ClosesTemplateDeclaration) ||
362              ((Current.Type == TT_StartOfName ||
363                Current.is(tok::kw_operator)) &&
364               State.ParenLevel == 0 &&
365               (!Style.IndentFunctionDeclarationAfterType ||
366                State.Line->StartsDefinition))) {
367     State.Column = State.Stack.back().Indent;
368   } else if (Current.Type == TT_ObjCSelectorName) {
369     if (State.Stack.back().ColonPos > Current.ColumnWidth) {
370       State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
371     } else {
372       State.Column = State.Stack.back().Indent;
373       State.Stack.back().ColonPos = State.Column + Current.ColumnWidth;
374     }
375   } else if (Current.is(tok::l_square) && Current.Type != TT_ObjCMethodExpr &&
376              Current.Type != TT_LambdaLSquare) {
377     if (State.Stack.back().StartOfArraySubscripts != 0)
378       State.Column = State.Stack.back().StartOfArraySubscripts;
379     else
380       State.Column = ContinuationIndent;
381   } else if (Current.Type == TT_StartOfName ||
382              Previous.isOneOf(tok::coloncolon, tok::equal) ||
383              Previous.Type == TT_ObjCMethodExpr) {
384     State.Column = ContinuationIndent;
385   } else if (Current.Type == TT_CtorInitializerColon) {
386     State.Column = State.FirstIndent + Style.ConstructorInitializerIndentWidth;
387   } else if (Current.Type == TT_CtorInitializerComma) {
388     State.Column = State.Stack.back().Indent;
389   } else {
390     State.Column = State.Stack.back().Indent;
391     // Ensure that we fall back to the continuation indent width instead of just
392     // flushing continuations left.
393     if (State.Column == State.FirstIndent)
394       State.Column += Style.ContinuationIndentWidth;
395   }
396 
397   if (Current.is(tok::question))
398     State.Stack.back().BreakBeforeParameter = true;
399   if ((Previous.isOneOf(tok::comma, tok::semi) &&
400        !State.Stack.back().AvoidBinPacking) ||
401       Previous.Type == TT_BinaryOperator)
402     State.Stack.back().BreakBeforeParameter = false;
403   if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
404     State.Stack.back().BreakBeforeParameter = false;
405 
406   if (!DryRun) {
407     unsigned Newlines = 1;
408     if (Current.is(tok::comment))
409       Newlines = std::max(Newlines, std::min(Current.NewlinesBefore,
410                                              Style.MaxEmptyLinesToKeep + 1));
411     Whitespaces.replaceWhitespace(Current, Newlines,
412                                   State.Stack.back().IndentLevel, State.Column,
413                                   State.Column, State.Line->InPPDirective);
414   }
415 
416   if (!Current.isTrailingComment())
417     State.Stack.back().LastSpace = State.Column;
418   if (Current.isMemberAccess())
419     State.Stack.back().LastSpace += Current.ColumnWidth;
420   State.StartOfLineLevel = State.ParenLevel;
421   State.LowestLevelOnLine = State.ParenLevel;
422 
423   // Any break on this level means that the parent level has been broken
424   // and we need to avoid bin packing there.
425   for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
426     State.Stack[i].BreakBeforeParameter = true;
427   }
428   const FormatToken *TokenBefore = Current.getPreviousNonComment();
429   if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
430       TokenBefore->Type != TT_TemplateCloser &&
431       TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
432     State.Stack.back().BreakBeforeParameter = true;
433 
434   // If we break after {, we should also break before the corresponding }.
435   if (Previous.is(tok::l_brace))
436     State.Stack.back().BreakBeforeClosingBrace = true;
437 
438   if (State.Stack.back().AvoidBinPacking) {
439     // If we are breaking after '(', '{', '<', this is not bin packing
440     // unless AllowAllParametersOfDeclarationOnNextLine is false.
441     if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
442           Previous.Type == TT_BinaryOperator) ||
443         (!Style.AllowAllParametersOfDeclarationOnNextLine &&
444          State.Line->MustBeDeclaration))
445       State.Stack.back().BreakBeforeParameter = true;
446   }
447 
448   return Penalty;
449 }
450 
451 unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
452                                                     bool DryRun, bool Newline) {
453   const FormatToken &Current = *State.NextToken;
454   assert(State.Stack.size());
455 
456   if (Current.Type == TT_InheritanceColon)
457     State.Stack.back().AvoidBinPacking = true;
458   if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
459     State.Stack.back().FirstLessLess = State.Column;
460   if (Current.is(tok::l_square) && Current.Type != TT_LambdaLSquare &&
461       State.Stack.back().StartOfArraySubscripts == 0)
462     State.Stack.back().StartOfArraySubscripts = State.Column;
463   if (Current.is(tok::question))
464     State.Stack.back().QuestionColumn = State.Column;
465   if (!Current.opensScope() && !Current.closesScope())
466     State.LowestLevelOnLine =
467         std::min(State.LowestLevelOnLine, State.ParenLevel);
468   if (Current.isMemberAccess())
469     State.Stack.back().StartOfFunctionCall =
470         Current.LastInChainOfCalls ? 0 : State.Column + Current.ColumnWidth;
471   if (Current.Type == TT_CtorInitializerColon) {
472     // Indent 2 from the column, so:
473     // SomeClass::SomeClass()
474     //     : First(...), ...
475     //       Next(...)
476     //       ^ line up here.
477     State.Stack.back().Indent =
478         State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
479     if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
480       State.Stack.back().AvoidBinPacking = true;
481     State.Stack.back().BreakBeforeParameter = false;
482   }
483 
484   // In ObjC method declaration we align on the ":" of parameters, but we need
485   // to ensure that we indent parameters on subsequent lines by at least our
486   // continuation indent width.
487   if (Current.Type == TT_ObjCMethodSpecifier)
488     State.Stack.back().Indent += Style.ContinuationIndentWidth;
489 
490   // Insert scopes created by fake parenthesis.
491   const FormatToken *Previous = Current.getPreviousNonComment();
492   // Don't add extra indentation for the first fake parenthesis after
493   // 'return', assignements or opening <({[. The indentation for these cases
494   // is special cased.
495   bool SkipFirstExtraIndent =
496       (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) ||
497                     Previous->getPrecedence() == prec::Assignment));
498   for (SmallVectorImpl<prec::Level>::const_reverse_iterator
499            I = Current.FakeLParens.rbegin(),
500            E = Current.FakeLParens.rend();
501        I != E; ++I) {
502     ParenState NewParenState = State.Stack.back();
503     NewParenState.ContainsLineBreak = false;
504 
505     // Indent from 'LastSpace' unless this the fake parentheses encapsulating a
506     // builder type call after 'return'. If such a call is line-wrapped, we
507     // commonly just want to indent from the start of the line.
508     if (!Previous || Previous->isNot(tok::kw_return) || *I > 0)
509       NewParenState.Indent =
510           std::max(std::max(State.Column, NewParenState.Indent),
511                    State.Stack.back().LastSpace);
512 
513     // Do not indent relative to the fake parentheses inserted for "." or "->".
514     // This is a special case to make the following to statements consistent:
515     //   OuterFunction(InnerFunctionCall( // break
516     //       ParameterToInnerFunction));
517     //   OuterFunction(SomeObject.InnerFunctionCall( // break
518     //       ParameterToInnerFunction));
519     if (*I > prec::Unknown)
520       NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
521 
522     // Always indent conditional expressions. Never indent expression where
523     // the 'operator' is ',', ';' or an assignment (i.e. *I <=
524     // prec::Assignment) as those have different indentation rules. Indent
525     // other expression, unless the indentation needs to be skipped.
526     if (*I == prec::Conditional ||
527         (!SkipFirstExtraIndent && *I > prec::Assignment &&
528          !Style.BreakBeforeBinaryOperators))
529       NewParenState.Indent += Style.ContinuationIndentWidth;
530     if (Previous && !Previous->opensScope())
531       NewParenState.BreakBeforeParameter = false;
532     State.Stack.push_back(NewParenState);
533     SkipFirstExtraIndent = false;
534   }
535 
536   // If we encounter an opening (, [, { or <, we add a level to our stacks to
537   // prepare for the following tokens.
538   if (Current.opensScope()) {
539     unsigned NewIndent;
540     unsigned NewIndentLevel = State.Stack.back().IndentLevel;
541     bool AvoidBinPacking;
542     if (Current.is(tok::l_brace)) {
543       if (Current.MatchingParen && Current.BlockKind == BK_Block) {
544         // If this is an l_brace starting a nested block, we pretend (wrt. to
545         // indentation) that we already consumed the corresponding r_brace.
546         // Thus, we remove all ParenStates caused bake fake parentheses that end
547         // at the r_brace. The net effect of this is that we don't indent
548         // relative to the l_brace, if the nested block is the last parameter of
549         // a function. For example, this formats:
550         //
551         //   SomeFunction(a, [] {
552         //     f();  // break
553         //   });
554         //
555         // instead of:
556         //   SomeFunction(a, [] {
557         //                        f();  // break
558         //                      });
559         for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i)
560           State.Stack.pop_back();
561         NewIndent = State.Stack.back().LastSpace + Style.IndentWidth;
562         ++NewIndentLevel;
563       } else {
564         NewIndent = State.Stack.back().LastSpace;
565         if (Style.Cpp11BracedListStyle)
566           NewIndent += Style.ContinuationIndentWidth;
567         else {
568           NewIndent += Style.IndentWidth;
569           ++NewIndentLevel;
570         }
571       }
572       const FormatToken *NextNoComment = Current.getNextNonComment();
573       AvoidBinPacking = Current.BlockKind == BK_Block ||
574                         (NextNoComment &&
575                          NextNoComment->Type == TT_DesignatedInitializerPeriod);
576     } else {
577       NewIndent = Style.ContinuationIndentWidth +
578                   std::max(State.Stack.back().LastSpace,
579                            State.Stack.back().StartOfFunctionCall);
580       AvoidBinPacking = !Style.BinPackParameters ||
581                         (Style.ExperimentalAutoDetectBinPacking &&
582                          (Current.PackingKind == PPK_OnePerLine ||
583                           (!BinPackInconclusiveFunctions &&
584                            Current.PackingKind == PPK_Inconclusive)));
585     }
586 
587     bool NoLineBreak = State.Stack.back().NoLineBreak ||
588                        (Current.Type == TT_TemplateOpener &&
589                         State.Stack.back().ContainsUnwrappedBuilder);
590     State.Stack.push_back(ParenState(NewIndent, NewIndentLevel,
591                                      State.Stack.back().LastSpace,
592                                      AvoidBinPacking, NoLineBreak));
593     State.Stack.back().BreakBeforeParameter = Current.BlockKind == BK_Block;
594     ++State.ParenLevel;
595   }
596 
597   // If this '[' opens an ObjC call, determine whether all parameters fit into
598   // one line and put one per line if they don't.
599   if (Current.isOneOf(tok::l_brace, tok::l_square) &&
600       (Current.Type == TT_ObjCDictLiteral ||
601        Current.Type == TT_ObjCMethodExpr) &&
602       Current.MatchingParen != NULL) {
603     if (getLengthToMatchingParen(Current) + State.Column >
604         getColumnLimit(State)) {
605       State.Stack.back().BreakBeforeParameter = true;
606       State.Stack.back().AvoidBinPacking = true;
607     }
608   }
609 
610   // If we encounter a closing ), ], } or >, we can remove a level from our
611   // stacks.
612   if (State.Stack.size() > 1 &&
613       (Current.isOneOf(tok::r_paren, tok::r_square) ||
614        (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
615        State.NextToken->Type == TT_TemplateCloser)) {
616     State.Stack.pop_back();
617     --State.ParenLevel;
618   }
619   if (Current.is(tok::r_square)) {
620     // If this ends the array subscript expr, reset the corresponding value.
621     const FormatToken *NextNonComment = Current.getNextNonComment();
622     if (NextNonComment && NextNonComment->isNot(tok::l_square))
623       State.Stack.back().StartOfArraySubscripts = 0;
624   }
625 
626   // Remove scopes created by fake parenthesis.
627   if (Current.isNot(tok::r_brace) ||
628       (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) {
629     // Don't remove FakeRParens attached to r_braces that surround nested blocks
630     // as they will have been removed early (see above).
631     for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
632       unsigned VariablePos = State.Stack.back().VariablePos;
633       State.Stack.pop_back();
634       State.Stack.back().VariablePos = VariablePos;
635     }
636   }
637 
638   if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
639     State.StartOfStringLiteral = State.Column;
640   } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
641                               tok::string_literal)) {
642     State.StartOfStringLiteral = 0;
643   }
644 
645   State.Column += Current.ColumnWidth;
646   State.NextToken = State.NextToken->Next;
647   unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
648   if (State.Column > getColumnLimit(State)) {
649     unsigned ExcessCharacters = State.Column - getColumnLimit(State);
650     Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
651   }
652 
653   // If the previous has a special role, let it consume tokens as appropriate.
654   // It is necessary to start at the previous token for the only implemented
655   // role (comma separated list). That way, the decision whether or not to break
656   // after the "{" is already done and both options are tried and evaluated.
657   // FIXME: This is ugly, find a better way.
658   if (Previous && Previous->Role)
659     Penalty += Previous->Role->format(State, this, DryRun);
660 
661   return Penalty;
662 }
663 
664 unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
665                                                  LineState &State) {
666   // Break before further function parameters on all levels.
667   for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
668     State.Stack[i].BreakBeforeParameter = true;
669 
670   unsigned ColumnsUsed = State.Column;
671   // We can only affect layout of the first and the last line, so the penalty
672   // for all other lines is constant, and we ignore it.
673   State.Column = Current.LastLineColumnWidth;
674 
675   if (ColumnsUsed > getColumnLimit(State))
676     return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
677   return 0;
678 }
679 
680 static bool getRawStringLiteralPrefixPostfix(StringRef Text,
681                                              StringRef &Prefix,
682                                              StringRef &Postfix) {
683   if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") ||
684       Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") ||
685       Text.startswith(Prefix = "LR\"")) {
686     size_t ParenPos = Text.find('(');
687     if (ParenPos != StringRef::npos) {
688       StringRef Delimiter =
689           Text.substr(Prefix.size(), ParenPos - Prefix.size());
690       Prefix = Text.substr(0, ParenPos + 1);
691       Postfix = Text.substr(Text.size() - 2 - Delimiter.size());
692       return Postfix.front() == ')' && Postfix.back() == '"' &&
693              Postfix.substr(1).startswith(Delimiter);
694     }
695   }
696   return false;
697 }
698 
699 unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
700                                                     LineState &State,
701                                                     bool DryRun) {
702   // Don't break multi-line tokens other than block comments. Instead, just
703   // update the state.
704   if (Current.Type != TT_BlockComment && Current.IsMultiline)
705     return addMultilineToken(Current, State);
706 
707   if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal,
708                        tok::utf8_string_literal, tok::utf16_string_literal,
709                        tok::utf32_string_literal, tok::comment))
710     return 0;
711 
712   llvm::OwningPtr<BreakableToken> Token;
713   unsigned StartColumn = State.Column - Current.ColumnWidth;
714 
715   if (Current.isOneOf(tok::string_literal, tok::wide_string_literal,
716                       tok::utf8_string_literal, tok::utf16_string_literal,
717                       tok::utf32_string_literal) &&
718       Current.Type != TT_ImplicitStringLiteral) {
719     // Don't break string literals inside preprocessor directives (except for
720     // #define directives, as their contents are stored in separate lines and
721     // are not affected by this check).
722     // This way we avoid breaking code with line directives and unknown
723     // preprocessor directives that contain long string literals.
724     if (State.Line->Type == LT_PreprocessorDirective)
725       return 0;
726     // Exempts unterminated string literals from line breaking. The user will
727     // likely want to terminate the string before any line breaking is done.
728     if (Current.IsUnterminatedLiteral)
729       return 0;
730 
731     StringRef Text = Current.TokenText;
732     StringRef Prefix;
733     StringRef Postfix;
734     // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
735     // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
736     // reduce the overhead) for each FormatToken, which is a string, so that we
737     // don't run multiple checks here on the hot path.
738     if ((Text.endswith(Postfix = "\"") &&
739          (Text.startswith(Prefix = "\"") || Text.startswith(Prefix = "u\"") ||
740           Text.startswith(Prefix = "U\"") || Text.startswith(Prefix = "u8\"") ||
741           Text.startswith(Prefix = "L\""))) ||
742         (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) ||
743         getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) {
744       Token.reset(new BreakableStringLiteral(
745           Current, State.Line->Level, StartColumn, Prefix, Postfix,
746           State.Line->InPPDirective, Encoding, Style));
747     } else {
748       return 0;
749     }
750   } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
751     Token.reset(new BreakableBlockComment(
752         Current, State.Line->Level, StartColumn, Current.OriginalColumn,
753         !Current.Previous, State.Line->InPPDirective, Encoding, Style));
754   } else if (Current.Type == TT_LineComment &&
755              (Current.Previous == NULL ||
756               Current.Previous->Type != TT_ImplicitStringLiteral)) {
757     Token.reset(new BreakableLineComment(Current, State.Line->Level,
758                                          StartColumn, State.Line->InPPDirective,
759                                          Encoding, Style));
760   } else {
761     return 0;
762   }
763   if (Current.UnbreakableTailLength >= getColumnLimit(State))
764     return 0;
765 
766   unsigned RemainingSpace =
767       getColumnLimit(State) - Current.UnbreakableTailLength;
768   bool BreakInserted = false;
769   unsigned Penalty = 0;
770   unsigned RemainingTokenColumns = 0;
771   for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
772        LineIndex != EndIndex; ++LineIndex) {
773     if (!DryRun)
774       Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
775     unsigned TailOffset = 0;
776     RemainingTokenColumns =
777         Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
778     while (RemainingTokenColumns > RemainingSpace) {
779       BreakableToken::Split Split =
780           Token->getSplit(LineIndex, TailOffset, getColumnLimit(State));
781       if (Split.first == StringRef::npos) {
782         // The last line's penalty is handled in addNextStateToQueue().
783         if (LineIndex < EndIndex - 1)
784           Penalty += Style.PenaltyExcessCharacter *
785                      (RemainingTokenColumns - RemainingSpace);
786         break;
787       }
788       assert(Split.first != 0);
789       unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
790           LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
791       assert(NewRemainingTokenColumns < RemainingTokenColumns);
792       if (!DryRun)
793         Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
794       Penalty += Current.SplitPenalty;
795       unsigned ColumnsUsed =
796           Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
797       if (ColumnsUsed > getColumnLimit(State)) {
798         Penalty += Style.PenaltyExcessCharacter *
799                    (ColumnsUsed - getColumnLimit(State));
800       }
801       TailOffset += Split.first + Split.second;
802       RemainingTokenColumns = NewRemainingTokenColumns;
803       BreakInserted = true;
804     }
805   }
806 
807   State.Column = RemainingTokenColumns;
808 
809   if (BreakInserted) {
810     // If we break the token inside a parameter list, we need to break before
811     // the next parameter on all levels, so that the next parameter is clearly
812     // visible. Line comments already introduce a break.
813     if (Current.Type != TT_LineComment) {
814       for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
815         State.Stack[i].BreakBeforeParameter = true;
816     }
817 
818     Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
819                                                : Style.PenaltyBreakComment;
820 
821     State.Stack.back().LastSpace = StartColumn;
822   }
823   return Penalty;
824 }
825 
826 unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
827   // In preprocessor directives reserve two chars for trailing " \"
828   return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
829 }
830 
831 bool ContinuationIndenter::NextIsMultilineString(const LineState &State) {
832   const FormatToken &Current = *State.NextToken;
833   if (!Current.is(tok::string_literal))
834     return false;
835   // We never consider raw string literals "multiline" for the purpose of
836   // AlwaysBreakBeforeMultilineStrings implementation.
837   if (Current.TokenText.startswith("R\""))
838     return false;
839   if (Current.IsMultiline)
840     return true;
841   if (Current.getNextNonComment() &&
842       Current.getNextNonComment()->is(tok::string_literal))
843     return true; // Implicit concatenation.
844   if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
845       Style.ColumnLimit)
846     return true; // String will be split.
847   return false;
848 }
849 
850 } // namespace format
851 } // namespace clang
852