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