blob: 5e63d0cb9beb7731d0ddb4c7bf25aa15c70ce73d [file] [log] [blame]
Daniel Jasperde0328a2013-08-16 11:20:30 +00001//===--- 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
Daniel Jasperde0328a2013-08-16 11:20:30 +000015#include "BreakableToken.h"
16#include "ContinuationIndenter.h"
17#include "WhitespaceManager.h"
18#include "clang/Basic/OperatorPrecedence.h"
19#include "clang/Basic/SourceManager.h"
20#include "clang/Format/Format.h"
21#include "llvm/Support/Debug.h"
22#include <string>
23
Chandler Carruth10346662014-04-22 03:17:02 +000024#define DEBUG_TYPE "format-formatter"
25
Daniel Jasperde0328a2013-08-16 11:20:30 +000026namespace clang {
27namespace format {
28
29// Returns the length of everything up to the first possible line break after
30// the ), ], } or > matching \c Tok.
31static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
Craig Topper2145bc02014-05-09 08:15:10 +000032 if (!Tok.MatchingParen)
Daniel Jasperde0328a2013-08-16 11:20:30 +000033 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
Daniel Jasper4c6e0052013-08-27 14:24:43 +000041// Returns \c true if \c Tok is the "." or "->" of a call and starts the next
42// segment of a builder type call.
43static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
44 return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
45}
46
Daniel Jasperec01cd62013-10-08 05:11:18 +000047// Returns \c true if \c Current starts a new parameter.
48static 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
Daniel Jasperde0328a2013-08-16 11:20:30 +000059ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
60 SourceManager &SourceMgr,
Daniel Jasperde0328a2013-08-16 11:20:30 +000061 WhitespaceManager &Whitespaces,
62 encoding::Encoding Encoding,
63 bool BinPackInconclusiveFunctions)
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000064 : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces),
65 Encoding(Encoding),
Alexander Kornienkoce9161a2014-01-02 15:13:14 +000066 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
67 CommentPragmasRegex(Style.CommentPragmas) {}
Daniel Jasperde0328a2013-08-16 11:20:30 +000068
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000069LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
Daniel Jasper1c5d9df2013-09-06 07:54:20 +000070 const AnnotatedLine *Line,
71 bool DryRun) {
Daniel Jasperde0328a2013-08-16 11:20:30 +000072 LineState State;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000073 State.FirstIndent = FirstIndent;
Daniel Jasperde0328a2013-08-16 11:20:30 +000074 State.Column = FirstIndent;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000075 State.Line = Line;
76 State.NextToken = Line->First;
Alexander Kornienkoe2e03872013-10-14 00:46:35 +000077 State.Stack.push_back(ParenState(FirstIndent, Line->Level, FirstIndent,
Daniel Jasperde0328a2013-08-16 11:20:30 +000078 /*AvoidBinPacking=*/false,
79 /*NoLineBreak=*/false));
80 State.LineContainsContinuedForLoopSection = false;
Daniel Jasperde0328a2013-08-16 11:20:30 +000081 State.StartOfStringLiteral = 0;
Daniel Jasper05cd5862014-05-08 12:21:30 +000082 State.StartOfLineLevel = 0;
83 State.LowestLevelOnLine = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +000084 State.IgnoreStackForComparison = false;
85
86 // The first token has already been indented and thus consumed.
Daniel Jasper1c5d9df2013-09-06 07:54:20 +000087 moveStateToNextToken(State, DryRun, /*Newline=*/false);
Daniel Jasperde0328a2013-08-16 11:20:30 +000088 return State;
89}
90
91bool ContinuationIndenter::canBreak(const LineState &State) {
92 const FormatToken &Current = *State.NextToken;
93 const FormatToken &Previous = *Current.Previous;
94 assert(&Previous == Current.Previous);
Daniel Jasper1db6c382013-10-22 15:30:28 +000095 if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace &&
96 Current.closesBlockTypeList(Style)))
Daniel Jasperde0328a2013-08-16 11:20:30 +000097 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) &&
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000101 Previous.Type != TT_DictLiteral && Previous.BlockKind == BK_BracedInit &&
102 Previous.Previous &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000103 Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
104 return false;
105 // This prevents breaks like:
106 // ...
107 // SomeParameter, OtherParameter).DoSomething(
108 // ...
109 // As they hide "DoSomething" and are generally bad for readability.
Daniel Jasper8f59ae52014-03-11 11:03:26 +0000110 if (Previous.opensScope() && Previous.isNot(tok::l_brace) &&
111 State.LowestLevelOnLine < State.StartOfLineLevel)
Daniel Jasperde0328a2013-08-16 11:20:30 +0000112 return false;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000113 if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
114 return false;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000115
116 // Don't create a 'hanging' indent if there are multiple blocks in a single
117 // statement.
118 if (Style.Language == FormatStyle::LK_JavaScript &&
119 Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
120 State.Stack[State.Stack.size() - 2].JSFunctionInlined &&
121 State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks)
122 return false;
123
Daniel Jasperde0328a2013-08-16 11:20:30 +0000124 return !State.Stack.back().NoLineBreak;
125}
126
127bool ContinuationIndenter::mustBreak(const LineState &State) {
128 const FormatToken &Current = *State.NextToken;
129 const FormatToken &Previous = *Current.Previous;
130 if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
131 return true;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000132 if (State.Stack.back().BreakBeforeClosingBrace &&
133 Current.closesBlockTypeList(Style))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000134 return true;
135 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
136 return true;
Daniel Jasperec01cd62013-10-08 05:11:18 +0000137 if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
Daniel Jasper165b29e2013-11-08 00:57:11 +0000138 (Style.BreakBeforeTernaryOperators &&
139 (Current.is(tok::question) || (Current.Type == TT_ConditionalExpr &&
140 Previous.isNot(tok::question)))) ||
141 (!Style.BreakBeforeTernaryOperators &&
142 (Previous.is(tok::question) || Previous.Type == TT_ConditionalExpr))) &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000143 State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
144 !Current.isOneOf(tok::r_paren, tok::r_brace))
145 return true;
146 if (Style.AlwaysBreakBeforeMultilineStrings &&
Daniel Jasperf438cb72013-08-23 11:57:34 +0000147 State.Column > State.Stack.back().Indent && // Breaking saves columns.
Daniel Jasper27943052013-11-09 03:08:25 +0000148 !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) &&
Daniel Jasperc39b56f2013-12-16 07:23:08 +0000149 Previous.Type != TT_InlineASMColon && nextIsMultilineString(State))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000150 return true;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000151 if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) ||
Daniel Jasper1db6c382013-10-22 15:30:28 +0000152 Previous.Type == TT_ArrayInitializerLSquare) &&
Daniel Jasperdb8804b2014-04-14 12:11:07 +0000153 Style.ColumnLimit > 0 &&
Daniel Jasperd489dd32013-10-20 16:45:46 +0000154 getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State))
155 return true;
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000156 if (Current.Type == TT_CtorInitializerColon &&
Daniel Jasperd74cf402014-04-08 12:46:38 +0000157 ((Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All) ||
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000158 Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0))
159 return true;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000160
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000161 if (State.Column < getNewLineColumn(State))
162 return false;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000163 if (!Style.BreakBeforeBinaryOperators) {
164 // If we need to break somewhere inside the LHS of a binary expression, we
165 // should also break after the operator. Otherwise, the formatting would
166 // hide the operator precedence, e.g. in:
167 // if (aaaaaaaaaaaaaa ==
168 // bbbbbbbbbbbbbb && c) {..
169 // For comparisons, we only apply this rule, if the LHS is a binary
170 // expression itself as otherwise, the line breaks seem superfluous.
171 // We need special cases for ">>" which we have split into two ">" while
172 // lexing in order to make template parsing easier.
173 //
174 // FIXME: We'll need something similar for styles that break before binary
175 // operators.
176 bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
177 Previous.getPrecedence() == prec::Equality) &&
178 Previous.Previous &&
179 Previous.Previous->Type != TT_BinaryOperator; // For >>.
180 bool LHSIsBinaryExpr =
Daniel Jasper562ecd42013-09-06 08:08:14 +0000181 Previous.Previous && Previous.Previous->EndsBinaryExpression;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000182 if (Previous.Type == TT_BinaryOperator &&
183 (!IsComparison || LHSIsBinaryExpr) &&
184 Current.Type != TT_BinaryOperator && // For >>.
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000185 !Current.isTrailingComment() && !Previous.is(tok::lessless) &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000186 Previous.getPrecedence() != prec::Assignment &&
187 State.Stack.back().BreakBeforeParameter)
188 return true;
189 }
190
191 // Same as above, but for the first "<<" operator.
Alexander Kornienko86b2dfd2014-03-06 15:13:08 +0000192 if (Current.is(tok::lessless) && Current.Type != TT_OverloadedOperator &&
193 State.Stack.back().BreakBeforeParameter &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000194 State.Stack.back().FirstLessLess == 0)
195 return true;
196
Daniel Jasperde0328a2013-08-16 11:20:30 +0000197 if (Current.Type == TT_ObjCSelectorName &&
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000198 State.Stack.back().ObjCSelectorNameFound &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000199 State.Stack.back().BreakBeforeParameter)
200 return true;
Daniel Jasper05cd5862014-05-08 12:21:30 +0000201 if (Previous.ClosesTemplateDeclaration && Current.NestingLevel == 0 &&
Alexander Kornienkoa594ba82013-12-16 14:35:51 +0000202 !Current.isTrailingComment())
Daniel Jasperde0328a2013-08-16 11:20:30 +0000203 return true;
204
205 if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000206 State.Line->MightBeFunctionDecl &&
Daniel Jasper05cd5862014-05-08 12:21:30 +0000207 State.Stack.back().BreakBeforeParameter && Current.NestingLevel == 0)
Daniel Jasperde0328a2013-08-16 11:20:30 +0000208 return true;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000209 if (startsSegmentOfBuilderTypeCall(Current) &&
Daniel Jasperf8151e92013-08-30 07:12:40 +0000210 (State.Stack.back().CallContinuation != 0 ||
211 (State.Stack.back().BreakBeforeParameter &&
212 State.Stack.back().ContainsUnwrappedBuilder)))
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000213 return true;
Daniel Jasper96972812014-01-05 12:38:10 +0000214
215 // The following could be precomputed as they do not depend on the state.
216 // However, as they should take effect only if the UnwrappedLine does not fit
217 // into the ColumnLimit, they are checked here in the ContinuationIndenter.
Daniel Jasper35995672014-04-29 14:05:20 +0000218 if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block &&
219 Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment))
Daniel Jasper96972812014-01-05 12:38:10 +0000220 return true;
Daniel Jasper96972812014-01-05 12:38:10 +0000221
Daniel Jasperde0328a2013-08-16 11:20:30 +0000222 return false;
223}
224
225unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000226 bool DryRun,
227 unsigned ExtraSpaces) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000228 const FormatToken &Current = *State.NextToken;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000229
Manuel Klimek819788d2014-03-18 11:22:45 +0000230 assert(!State.Stack.empty());
231 if ((Current.Type == TT_ImplicitStringLiteral &&
Craig Topper2145bc02014-05-09 08:15:10 +0000232 (Current.Previous->Tok.getIdentifierInfo() == nullptr ||
Daniel Jasper98857842013-10-30 13:54:53 +0000233 Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
234 tok::pp_not_keyword))) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000235 // FIXME: Is this correct?
236 int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
237 State.NextToken->WhitespaceRange.getEnd()) -
238 SourceMgr.getSpellingColumnNumber(
239 State.NextToken->WhitespaceRange.getBegin());
Daniel Jasperda353cd2014-03-12 08:24:47 +0000240 State.Column += WhitespaceLength;
Daniel Jasper240dfda2014-03-31 14:23:49 +0000241 moveStateToNextToken(State, DryRun, /*Newline=*/false);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000242 return 0;
243 }
244
Alexander Kornienko1f803962013-10-01 14:41:18 +0000245 unsigned Penalty = 0;
246 if (Newline)
247 Penalty = addTokenOnNewLine(State, DryRun);
248 else
Daniel Jasper48437ce2013-11-20 14:54:39 +0000249 addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
Alexander Kornienko1f803962013-10-01 14:41:18 +0000250
251 return moveStateToNextToken(State, DryRun, Newline) + Penalty;
252}
253
Daniel Jasper48437ce2013-11-20 14:54:39 +0000254void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
255 unsigned ExtraSpaces) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000256 FormatToken &Current = *State.NextToken;
Alexander Kornienko1f803962013-10-01 14:41:18 +0000257 const FormatToken &Previous = *State.NextToken->Previous;
258 if (Current.is(tok::equal) &&
Daniel Jasper05cd5862014-05-08 12:21:30 +0000259 (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
Alexander Kornienko1f803962013-10-01 14:41:18 +0000260 State.Stack.back().VariablePos == 0) {
261 State.Stack.back().VariablePos = State.Column;
262 // Move over * and & if they are bound to the variable name.
263 const FormatToken *Tok = &Previous;
264 while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
265 State.Stack.back().VariablePos -= Tok->ColumnWidth;
266 if (Tok->SpacesRequiredBefore != 0)
267 break;
268 Tok = Tok->Previous;
269 }
270 if (Previous.PartOfMultiVariableDeclStmt)
271 State.Stack.back().LastSpace = State.Stack.back().VariablePos;
272 }
273
274 unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
275
276 if (!DryRun)
277 Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0,
278 Spaces, State.Column + Spaces);
279
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000280 if (Current.Type == TT_ObjCSelectorName &&
281 !State.Stack.back().ObjCSelectorNameFound) {
282 if (Current.LongestObjCSelectorName == 0)
283 State.Stack.back().AlignColons = false;
284 else if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
285 State.Column + Spaces + Current.ColumnWidth)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000286 State.Stack.back().ColonPos =
287 State.Stack.back().Indent + Current.LongestObjCSelectorName;
288 else
289 State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth;
290 }
291
292 if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
Daniel Jasper5a611392013-12-19 21:41:37 +0000293 (Current.Type != TT_LineComment || Previous.BlockKind == BK_BracedInit))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000294 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jasperec01cd62013-10-08 05:11:18 +0000295 if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000296 State.Stack.back().NoLineBreak = true;
297 if (startsSegmentOfBuilderTypeCall(Current))
298 State.Stack.back().ContainsUnwrappedBuilder = true;
299
300 State.Column += Spaces;
Daniel Jasper8acf8222014-05-07 09:23:05 +0000301 if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
302 Previous.Previous && Previous.Previous->isOneOf(tok::kw_if, tok::kw_for))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000303 // Treat the condition inside an if as if it was a second function
Daniel Jasper6633ab82013-10-18 10:38:14 +0000304 // parameter, i.e. let nested calls have a continuation indent.
Daniel Jasper8acf8222014-05-07 09:23:05 +0000305 State.Stack.back().LastSpace = State.Column;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000306 else if (!Current.isOneOf(tok::comment, tok::caret) &&
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000307 (Previous.is(tok::comma) ||
308 (Previous.is(tok::colon) && Previous.Type == TT_ObjCMethodExpr)))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000309 State.Stack.back().LastSpace = State.Column;
310 else if ((Previous.Type == TT_BinaryOperator ||
311 Previous.Type == TT_ConditionalExpr ||
Alexander Kornienko1f803962013-10-01 14:41:18 +0000312 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasper0e617842014-04-16 12:26:54 +0000313 ((Previous.getPrecedence() != prec::Assignment &&
314 (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
315 !Previous.LastOperator)) ||
Alexander Kornienko1f803962013-10-01 14:41:18 +0000316 Current.StartsBinaryExpression))
317 // Always indent relative to the RHS of the expression unless this is a
318 // simple assignment without binary expression on the RHS. Also indent
319 // relative to unary operators and the colons of constructor initializers.
320 State.Stack.back().LastSpace = State.Column;
Daniel Jasperf9a5e402013-10-08 16:24:07 +0000321 else if (Previous.Type == TT_InheritanceColon) {
Alexander Kornienko1f803962013-10-01 14:41:18 +0000322 State.Stack.back().Indent = State.Column;
Daniel Jasperf9a5e402013-10-08 16:24:07 +0000323 State.Stack.back().LastSpace = State.Column;
324 } else if (Previous.opensScope()) {
Alexander Kornienko1f803962013-10-01 14:41:18 +0000325 // If a function has a trailing call, indent all parameters from the
326 // opening parenthesis. This avoids confusing indents like:
327 // OuterFunction(InnerFunctionCall( // break
328 // ParameterToInnerFunction)) // break
329 // .SecondInnerFunctionCall();
330 bool HasTrailingCall = false;
331 if (Previous.MatchingParen) {
332 const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
333 HasTrailingCall = Next && Next->isMemberAccess();
334 }
335 if (HasTrailingCall &&
336 State.Stack[State.Stack.size() - 2].CallContinuation == 0)
337 State.Stack.back().LastSpace = State.Column;
338 }
339}
340
341unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
342 bool DryRun) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000343 FormatToken &Current = *State.NextToken;
Alexander Kornienko1f803962013-10-01 14:41:18 +0000344 const FormatToken &Previous = *State.NextToken->Previous;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000345
Alexander Kornienko1f803962013-10-01 14:41:18 +0000346 // Extra penalty that needs to be added because of the way certain line
347 // breaks are chosen.
348 unsigned Penalty = 0;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000349
Daniel Jaspera0407742014-02-11 10:08:11 +0000350 const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
351 const FormatToken *NextNonComment = Previous.getNextNonComment();
352 if (!NextNonComment)
353 NextNonComment = &Current;
Daniel Jasper05cd5862014-05-08 12:21:30 +0000354 // The first line break on any NestingLevel causes an extra penalty in order
Alexander Kornienko1f803962013-10-01 14:41:18 +0000355 // prefer similar line breaks.
356 if (!State.Stack.back().ContainsLineBreak)
357 Penalty += 15;
358 State.Stack.back().ContainsLineBreak = true;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000359
Alexander Kornienko1f803962013-10-01 14:41:18 +0000360 Penalty += State.NextToken->SplitPenalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000361
Alexander Kornienko1f803962013-10-01 14:41:18 +0000362 // Breaking before the first "<<" is generally not desirable if the LHS is
Daniel Jasper48437ce2013-11-20 14:54:39 +0000363 // short. Also always add the penalty if the LHS is split over mutliple lines
Daniel Jasper2b7556e2014-04-03 12:00:27 +0000364 // to avoid unnecessary line breaks that just work around this penalty.
Daniel Jaspera0407742014-02-11 10:08:11 +0000365 if (NextNonComment->is(tok::lessless) &&
366 State.Stack.back().FirstLessLess == 0 &&
Daniel Jasper004177e2013-12-19 16:06:40 +0000367 (State.Column <= Style.ColumnLimit / 3 ||
Daniel Jasper48437ce2013-11-20 14:54:39 +0000368 State.Stack.back().BreakBeforeParameter))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000369 Penalty += Style.PenaltyBreakFirstLessLess;
370
Daniel Jasper9f388d02014-03-27 14:33:30 +0000371 State.Column = getNewLineColumn(State);
372 if (NextNonComment->isMemberAccess()) {
373 if (State.Stack.back().CallContinuation == 0)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000374 State.Stack.back().CallContinuation = State.Column;
Daniel Jaspera0407742014-02-11 10:08:11 +0000375 } else if (NextNonComment->Type == TT_ObjCSelectorName) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000376 if (!State.Stack.back().ObjCSelectorNameFound) {
Daniel Jaspera0407742014-02-11 10:08:11 +0000377 if (NextNonComment->LongestObjCSelectorName == 0) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000378 State.Stack.back().AlignColons = false;
379 } else {
380 State.Stack.back().ColonPos =
Daniel Jaspera0407742014-02-11 10:08:11 +0000381 State.Stack.back().Indent + NextNonComment->LongestObjCSelectorName;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000382 }
Daniel Jasper9f388d02014-03-27 14:33:30 +0000383 } else if (State.Stack.back().AlignColons &&
384 State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) {
Daniel Jaspera0407742014-02-11 10:08:11 +0000385 State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000386 }
Daniel Jasper1fd6f1f2014-03-17 14:32:47 +0000387 } else if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
388 (PreviousNonComment->Type == TT_ObjCMethodExpr ||
389 PreviousNonComment->Type == TT_DictLiteral)) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000390 // FIXME: This is hacky, find a better way. The problem is that in an ObjC
391 // method expression, the block should be aligned to the line starting it,
392 // e.g.:
393 // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
394 // ^(int *i) {
395 // // ...
396 // }];
Daniel Jasper05cd5862014-05-08 12:21:30 +0000397 // Thus, we set LastSpace of the next higher NestingLevel, to which we move
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000398 // when we consume all of the "}"'s FakeRParens at the "{".
Daniel Jasper9a26e772013-12-23 11:25:40 +0000399 if (State.Stack.size() > 1)
Daniel Jasper9f388d02014-03-27 14:33:30 +0000400 State.Stack[State.Stack.size() - 2].LastSpace =
401 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
402 Style.ContinuationIndentWidth;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000403 }
404
Alexander Kornienko1f803962013-10-01 14:41:18 +0000405 if ((Previous.isOneOf(tok::comma, tok::semi) &&
406 !State.Stack.back().AvoidBinPacking) ||
407 Previous.Type == TT_BinaryOperator)
408 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper05cd5862014-05-08 12:21:30 +0000409 if (Previous.Type == TT_TemplateCloser && Current.NestingLevel == 0)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000410 State.Stack.back().BreakBeforeParameter = false;
Daniel Jaspera0407742014-02-11 10:08:11 +0000411 if (NextNonComment->is(tok::question) ||
Daniel Jasper165b29e2013-11-08 00:57:11 +0000412 (PreviousNonComment && PreviousNonComment->is(tok::question)))
413 State.Stack.back().BreakBeforeParameter = true;
Alexander Kornienko1f803962013-10-01 14:41:18 +0000414
415 if (!DryRun) {
416 unsigned Newlines = 1;
417 if (Current.is(tok::comment))
418 Newlines = std::max(Newlines, std::min(Current.NewlinesBefore,
419 Style.MaxEmptyLinesToKeep + 1));
Alexander Kornienkoe2e03872013-10-14 00:46:35 +0000420 Whitespaces.replaceWhitespace(Current, Newlines,
421 State.Stack.back().IndentLevel, State.Column,
422 State.Column, State.Line->InPPDirective);
Alexander Kornienko1f803962013-10-01 14:41:18 +0000423 }
424
425 if (!Current.isTrailingComment())
426 State.Stack.back().LastSpace = State.Column;
Daniel Jasper05cd5862014-05-08 12:21:30 +0000427 State.StartOfLineLevel = Current.NestingLevel;
428 State.LowestLevelOnLine = Current.NestingLevel;
Alexander Kornienko1f803962013-10-01 14:41:18 +0000429
430 // Any break on this level means that the parent level has been broken
431 // and we need to avoid bin packing there.
Daniel Jasperb16b9692014-05-21 12:51:23 +0000432 bool JavaScriptFormat = Style.Language == FormatStyle::LK_JavaScript &&
433 Current.is(tok::r_brace) &&
434 State.Stack.size() > 1 &&
435 State.Stack[State.Stack.size() - 2].JSFunctionInlined;
436 if (!JavaScriptFormat) {
437 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
438 State.Stack[i].BreakBeforeParameter = true;
439 }
Alexander Kornienko1f803962013-10-01 14:41:18 +0000440 }
Daniel Jasperb16b9692014-05-21 12:51:23 +0000441
Daniel Jasper9e5ede02013-11-08 19:56:28 +0000442 if (PreviousNonComment &&
443 !PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
444 PreviousNonComment->Type != TT_TemplateCloser &&
445 PreviousNonComment->Type != TT_BinaryOperator &&
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000446 Current.Type != TT_BinaryOperator && !PreviousNonComment->opensScope())
Alexander Kornienko1f803962013-10-01 14:41:18 +0000447 State.Stack.back().BreakBeforeParameter = true;
448
Daniel Jasper1db6c382013-10-22 15:30:28 +0000449 // If we break after { or the [ of an array initializer, we should also break
450 // before the corresponding } or ].
451 if (Previous.is(tok::l_brace) || Previous.Type == TT_ArrayInitializerLSquare)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000452 State.Stack.back().BreakBeforeClosingBrace = true;
453
454 if (State.Stack.back().AvoidBinPacking) {
455 // If we are breaking after '(', '{', '<', this is not bin packing
Daniel Jasper2a958322014-05-21 13:26:58 +0000456 // unless AllowAllParametersOfDeclarationOnNextLine is false or this is a
457 // dict/object literal.
Alexander Kornienko1f803962013-10-01 14:41:18 +0000458 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
459 Previous.Type == TT_BinaryOperator) ||
460 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
Daniel Jasper2a958322014-05-21 13:26:58 +0000461 State.Line->MustBeDeclaration) ||
462 Previous.Type == TT_DictLiteral)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000463 State.Stack.back().BreakBeforeParameter = true;
464 }
465
466 return Penalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000467}
468
Daniel Jasper9f388d02014-03-27 14:33:30 +0000469unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000470 if (!State.NextToken || !State.NextToken->Previous)
471 return 0;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000472 FormatToken &Current = *State.NextToken;
473 const FormatToken &Previous = *State.NextToken->Previous;
474 // If we are continuing an expression, we want to use the continuation indent.
475 unsigned ContinuationIndent =
476 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
477 Style.ContinuationIndentWidth;
478 const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
479 const FormatToken *NextNonComment = Previous.getNextNonComment();
480 if (!NextNonComment)
481 NextNonComment = &Current;
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000482 if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
Daniel Jasper05cd5862014-05-08 12:21:30 +0000483 return Current.NestingLevel == 0 ? State.FirstIndent
484 : State.Stack.back().Indent;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000485 if (Current.isOneOf(tok::r_brace, tok::r_square)) {
Daniel Jasperb16b9692014-05-21 12:51:23 +0000486 if (State.Stack.size() > 1 &&
487 State.Stack[State.Stack.size() - 2].JSFunctionInlined)
488 return State.FirstIndent;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000489 if (Current.closesBlockTypeList(Style) ||
490 (Current.MatchingParen &&
491 Current.MatchingParen->BlockKind == BK_BracedInit))
492 return State.Stack[State.Stack.size() - 2].LastSpace;
493 else
494 return State.FirstIndent;
495 }
Daniel Jasper783bac62014-04-15 09:54:30 +0000496 if (Current.is(tok::identifier) && Current.Next &&
497 Current.Next->Type == TT_DictLiteral)
498 return State.Stack.back().Indent;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000499 if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0)
500 return State.StartOfStringLiteral;
501 if (NextNonComment->is(tok::lessless) &&
502 State.Stack.back().FirstLessLess != 0)
503 return State.Stack.back().FirstLessLess;
504 if (NextNonComment->isMemberAccess()) {
505 if (State.Stack.back().CallContinuation == 0) {
506 return ContinuationIndent;
507 } else {
508 return State.Stack.back().CallContinuation;
509 }
510 }
511 if (State.Stack.back().QuestionColumn != 0 &&
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000512 ((NextNonComment->is(tok::colon) &&
513 NextNonComment->Type == TT_ConditionalExpr) ||
Daniel Jasper9f388d02014-03-27 14:33:30 +0000514 Previous.Type == TT_ConditionalExpr))
515 return State.Stack.back().QuestionColumn;
516 if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0)
517 return State.Stack.back().VariablePos;
518 if ((PreviousNonComment && (PreviousNonComment->ClosesTemplateDeclaration ||
519 PreviousNonComment->Type == TT_AttributeParen)) ||
520 ((NextNonComment->Type == TT_StartOfName ||
521 NextNonComment->is(tok::kw_operator)) &&
Daniel Jasper05cd5862014-05-08 12:21:30 +0000522 Current.NestingLevel == 0 &&
523 (!Style.IndentFunctionDeclarationAfterType ||
524 State.Line->StartsDefinition)))
Daniel Jasper9f388d02014-03-27 14:33:30 +0000525 return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
526 if (NextNonComment->Type == TT_ObjCSelectorName) {
527 if (!State.Stack.back().ObjCSelectorNameFound) {
528 if (NextNonComment->LongestObjCSelectorName == 0) {
529 return State.Stack.back().Indent;
530 } else {
531 return State.Stack.back().Indent +
532 NextNonComment->LongestObjCSelectorName -
533 NextNonComment->ColumnWidth;
534 }
535 } else if (!State.Stack.back().AlignColons) {
536 return State.Stack.back().Indent;
537 } else if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth) {
538 return State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
539 } else {
540 return State.Stack.back().Indent;
541 }
542 }
543 if (NextNonComment->Type == TT_ArraySubscriptLSquare) {
544 if (State.Stack.back().StartOfArraySubscripts != 0)
545 return State.Stack.back().StartOfArraySubscripts;
546 else
547 return ContinuationIndent;
548 }
549 if (NextNonComment->Type == TT_StartOfName ||
550 Previous.isOneOf(tok::coloncolon, tok::equal)) {
551 return ContinuationIndent;
552 }
553 if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
554 (PreviousNonComment->Type == TT_ObjCMethodExpr ||
555 PreviousNonComment->Type == TT_DictLiteral))
556 return ContinuationIndent;
557 if (NextNonComment->Type == TT_CtorInitializerColon)
558 return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
559 if (NextNonComment->Type == TT_CtorInitializerComma)
560 return State.Stack.back().Indent;
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000561 if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
Daniel Jasper9f388d02014-03-27 14:33:30 +0000562 PreviousNonComment->isNot(tok::r_brace))
563 // Ensure that we fall back to the continuation indent width instead of
564 // just flushing continuations left.
565 return State.Stack.back().Indent + Style.ContinuationIndentWidth;
566 return State.Stack.back().Indent;
567}
568
Daniel Jasperde0328a2013-08-16 11:20:30 +0000569unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
570 bool DryRun, bool Newline) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000571 assert(State.Stack.size());
Daniel Jasper60553be2014-05-26 13:10:39 +0000572 const FormatToken &Current = *State.NextToken;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000573
574 if (Current.Type == TT_InheritanceColon)
575 State.Stack.back().AvoidBinPacking = true;
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000576 if (Current.is(tok::lessless) && Current.Type != TT_OverloadedOperator) {
577 if (State.Stack.back().FirstLessLess == 0)
578 State.Stack.back().FirstLessLess = State.Column;
579 else
580 State.Stack.back().LastOperatorWrapped = Newline;
581 }
582 if ((Current.Type == TT_BinaryOperator && Current.isNot(tok::lessless)) ||
583 Current.Type == TT_ConditionalExpr)
584 State.Stack.back().LastOperatorWrapped = Newline;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000585 if (Current.Type == TT_ArraySubscriptLSquare &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000586 State.Stack.back().StartOfArraySubscripts == 0)
587 State.Stack.back().StartOfArraySubscripts = State.Column;
Daniel Jasper165b29e2013-11-08 00:57:11 +0000588 if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) ||
589 (Current.getPreviousNonComment() && Current.isNot(tok::colon) &&
590 Current.getPreviousNonComment()->is(tok::question) &&
591 !Style.BreakBeforeTernaryOperators))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000592 State.Stack.back().QuestionColumn = State.Column;
593 if (!Current.opensScope() && !Current.closesScope())
594 State.LowestLevelOnLine =
Daniel Jasper05cd5862014-05-08 12:21:30 +0000595 std::min(State.LowestLevelOnLine, Current.NestingLevel);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000596 if (Current.isMemberAccess())
Daniel Jasperde0328a2013-08-16 11:20:30 +0000597 State.Stack.back().StartOfFunctionCall =
Daniel Jasper0e617842014-04-16 12:26:54 +0000598 Current.LastOperator ? 0 : State.Column + Current.ColumnWidth;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000599 if (Current.Type == TT_ObjCSelectorName)
600 State.Stack.back().ObjCSelectorNameFound = true;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000601 if (Current.Type == TT_CtorInitializerColon) {
602 // Indent 2 from the column, so:
603 // SomeClass::SomeClass()
604 // : First(...), ...
605 // Next(...)
606 // ^ line up here.
607 State.Stack.back().Indent =
608 State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
609 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
610 State.Stack.back().AvoidBinPacking = true;
611 State.Stack.back().BreakBeforeParameter = false;
612 }
613
Daniel Jasperde0328a2013-08-16 11:20:30 +0000614 // In ObjC method declaration we align on the ":" of parameters, but we need
Daniel Jasper6633ab82013-10-18 10:38:14 +0000615 // to ensure that we indent parameters on subsequent lines by at least our
616 // continuation indent width.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000617 if (Current.Type == TT_ObjCMethodSpecifier)
Daniel Jasper6633ab82013-10-18 10:38:14 +0000618 State.Stack.back().Indent += Style.ContinuationIndentWidth;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000619
620 // Insert scopes created by fake parenthesis.
621 const FormatToken *Previous = Current.getPreviousNonComment();
Daniel Jasperb16b9692014-05-21 12:51:23 +0000622
623 // Add special behavior to support a format commonly used for JavaScript
624 // closures:
625 // SomeFunction(function() {
626 // foo();
627 // bar();
628 // }, a, b, c);
629 if (Style.Language == FormatStyle::LK_JavaScript) {
630 if (Current.isNot(tok::comment) && Previous && Previous->is(tok::l_brace) &&
631 State.Stack.size() > 1) {
632 if (State.Stack[State.Stack.size() - 2].JSFunctionInlined && Newline) {
633 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
634 State.Stack[i].NoLineBreak = true;
635 }
636 }
637 State.Stack[State.Stack.size() - 2].JSFunctionInlined = false;
638 }
639 if (Current.TokenText == "function")
640 State.Stack.back().JSFunctionInlined = !Newline;
641 }
642
Daniel Jasper60553be2014-05-26 13:10:39 +0000643 moveStatePastFakeLParens(State, Newline);
644 moveStatePastScopeOpener(State, Newline);
645 moveStatePastScopeCloser(State);
646 moveStatePastFakeRParens(State);
647
648 if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) {
649 State.StartOfStringLiteral = State.Column;
650 } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
651 !Current.isStringLiteral()) {
652 State.StartOfStringLiteral = 0;
653 }
654
655 State.Column += Current.ColumnWidth;
656 State.NextToken = State.NextToken->Next;
657 unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
658 if (State.Column > getColumnLimit(State)) {
659 unsigned ExcessCharacters = State.Column - getColumnLimit(State);
660 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
661 }
662
663 if (Current.Role)
664 Current.Role->formatFromToken(State, this, DryRun);
665 // If the previous has a special role, let it consume tokens as appropriate.
666 // It is necessary to start at the previous token for the only implemented
667 // role (comma separated list). That way, the decision whether or not to break
668 // after the "{" is already done and both options are tried and evaluated.
669 // FIXME: This is ugly, find a better way.
670 if (Previous && Previous->Role)
671 Penalty += Previous->Role->formatAfterToken(State, this, DryRun);
672
673 return Penalty;
674}
675
676void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
677 bool Newline) {
678 const FormatToken &Current = *State.NextToken;
679 const FormatToken *Previous = Current.getPreviousNonComment();
680
Daniel Jasperde0328a2013-08-16 11:20:30 +0000681 // Don't add extra indentation for the first fake parenthesis after
Dinesh Dwivedi0db806b2014-05-01 17:19:34 +0000682 // 'return', assignments or opening <({[. The indentation for these cases
Daniel Jasperde0328a2013-08-16 11:20:30 +0000683 // is special cased.
684 bool SkipFirstExtraIndent =
Daniel Jaspereabede62013-09-30 08:29:03 +0000685 (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) ||
Daniel Jasperf48b5ab2013-11-07 19:23:49 +0000686 Previous->getPrecedence() == prec::Assignment ||
687 Previous->Type == TT_ObjCMethodExpr));
Daniel Jasperde0328a2013-08-16 11:20:30 +0000688 for (SmallVectorImpl<prec::Level>::const_reverse_iterator
689 I = Current.FakeLParens.rbegin(),
690 E = Current.FakeLParens.rend();
691 I != E; ++I) {
692 ParenState NewParenState = State.Stack.back();
693 NewParenState.ContainsLineBreak = false;
Daniel Jaspereabede62013-09-30 08:29:03 +0000694
695 // Indent from 'LastSpace' unless this the fake parentheses encapsulating a
696 // builder type call after 'return'. If such a call is line-wrapped, we
697 // commonly just want to indent from the start of the line.
698 if (!Previous || Previous->isNot(tok::kw_return) || *I > 0)
699 NewParenState.Indent =
700 std::max(std::max(State.Column, NewParenState.Indent),
701 State.Stack.back().LastSpace);
702
Daniel Jasper5c332652014-04-03 12:00:33 +0000703 // Don't allow the RHS of an operator to be split over multiple lines unless
704 // there is a line-break right after the operator.
705 // Exclude relational operators, as there, it is always more desirable to
706 // have the LHS 'left' of the RHS.
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000707 if (Previous && Previous->getPrecedence() > prec::Assignment &&
708 (Previous->Type == TT_BinaryOperator ||
709 Previous->Type == TT_ConditionalExpr) &&
710 Previous->getPrecedence() != prec::Relational) {
711 bool BreakBeforeOperator = Previous->is(tok::lessless) ||
712 (Previous->Type == TT_BinaryOperator &&
713 Style.BreakBeforeBinaryOperators) ||
714 (Previous->Type == TT_ConditionalExpr &&
715 Style.BreakBeforeTernaryOperators);
716 if ((!Newline && !BreakBeforeOperator) ||
717 (!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator))
718 NewParenState.NoLineBreak = true;
719 }
Daniel Jasper5c332652014-04-03 12:00:33 +0000720
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000721 // Do not indent relative to the fake parentheses inserted for "." or "->".
722 // This is a special case to make the following to statements consistent:
723 // OuterFunction(InnerFunctionCall( // break
724 // ParameterToInnerFunction));
725 // OuterFunction(SomeObject.InnerFunctionCall( // break
726 // ParameterToInnerFunction));
727 if (*I > prec::Unknown)
728 NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
Daniel Jasper96964352013-12-18 10:44:36 +0000729 NewParenState.StartOfFunctionCall = State.Column;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000730
731 // Always indent conditional expressions. Never indent expression where
732 // the 'operator' is ',', ';' or an assignment (i.e. *I <=
733 // prec::Assignment) as those have different indentation rules. Indent
734 // other expression, unless the indentation needs to be skipped.
735 if (*I == prec::Conditional ||
736 (!SkipFirstExtraIndent && *I > prec::Assignment &&
737 !Style.BreakBeforeBinaryOperators))
Daniel Jasper6633ab82013-10-18 10:38:14 +0000738 NewParenState.Indent += Style.ContinuationIndentWidth;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000739 if ((Previous && !Previous->opensScope()) || *I > prec::Comma)
Daniel Jasperde0328a2013-08-16 11:20:30 +0000740 NewParenState.BreakBeforeParameter = false;
741 State.Stack.push_back(NewParenState);
742 SkipFirstExtraIndent = false;
743 }
Daniel Jasper60553be2014-05-26 13:10:39 +0000744}
Daniel Jasperde0328a2013-08-16 11:20:30 +0000745
Daniel Jasper335ff262014-05-28 09:11:53 +0000746// Remove the fake r_parens after 'Tok'.
747static void consumeRParens(LineState& State, const FormatToken &Tok) {
748 for (unsigned i = 0, e = Tok.FakeRParens; i != e; ++i) {
749 unsigned VariablePos = State.Stack.back().VariablePos;
750 assert(State.Stack.size() > 1);
751 if (State.Stack.size() == 1) {
752 // Do not pop the last element.
753 break;
754 }
755 State.Stack.pop_back();
756 State.Stack.back().VariablePos = VariablePos;
757 }
758}
759
760// Returns whether 'Tok' opens or closes a scope requiring special handling
761// of the subsequent fake r_parens.
762//
763// For example, if this is an l_brace starting a nested block, we pretend (wrt.
764// to indentation) that we already consumed the corresponding r_brace. Thus, we
765// remove all ParenStates caused by fake parentheses that end at the r_brace.
766// The net effect of this is that we don't indent relative to the l_brace, if
767// the nested block is the last parameter of a function. This formats:
768//
769// SomeFunction(a, [] {
770// f(); // break
771// });
772//
773// instead of:
774// SomeFunction(a, [] {
775// f(); // break
776// });
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000777static bool fakeRParenSpecialCase(const LineState &State) {
778 const FormatToken &Tok = *State.NextToken;
Daniel Jasper335ff262014-05-28 09:11:53 +0000779 if (!Tok.MatchingParen)
780 return false;
781 const FormatToken *Left = &Tok;
782 if (Tok.isOneOf(tok::r_brace, tok::r_square))
783 Left = Tok.MatchingParen;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000784 return !State.Stack.back().HasMultipleNestedBlocks &&
785 Left->isOneOf(tok::l_brace, tok::l_square) &&
Daniel Jasper335ff262014-05-28 09:11:53 +0000786 (Left->BlockKind == BK_Block ||
787 Left->Type == TT_ArrayInitializerLSquare ||
788 Left->Type == TT_DictLiteral);
789}
790
Daniel Jasper60553be2014-05-26 13:10:39 +0000791void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) {
Daniel Jasper335ff262014-05-28 09:11:53 +0000792 // Don't remove FakeRParens attached to r_braces that surround nested blocks
793 // as they will have been removed early (see above).
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000794 if (fakeRParenSpecialCase(State))
Daniel Jasper335ff262014-05-28 09:11:53 +0000795 return;
796
797 consumeRParens(State, *State.NextToken);
Daniel Jasper60553be2014-05-26 13:10:39 +0000798}
Daniel Jasperde0328a2013-08-16 11:20:30 +0000799
Daniel Jasper60553be2014-05-26 13:10:39 +0000800void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
801 bool Newline) {
802 const FormatToken &Current = *State.NextToken;
803 if (!Current.opensScope())
804 return;
805
806 if (Current.MatchingParen && Current.BlockKind == BK_Block) {
807 moveStateToNewBlock(State);
808 return;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000809 }
810
Daniel Jasper60553be2014-05-26 13:10:39 +0000811 unsigned NewIndent;
812 unsigned NewIndentLevel = State.Stack.back().IndentLevel;
813 bool AvoidBinPacking;
814 bool BreakBeforeParameter = false;
815 if (Current.is(tok::l_brace) || Current.Type == TT_ArrayInitializerLSquare) {
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000816 if (fakeRParenSpecialCase(State))
Daniel Jasper335ff262014-05-28 09:11:53 +0000817 consumeRParens(State, *Current.MatchingParen);
818
Daniel Jasper60553be2014-05-26 13:10:39 +0000819 NewIndent = State.Stack.back().LastSpace;
820 if (Current.opensBlockTypeList(Style)) {
821 NewIndent += Style.IndentWidth;
822 NewIndent = std::min(State.Column + 2, NewIndent);
823 ++NewIndentLevel;
824 } else {
825 NewIndent += Style.ContinuationIndentWidth;
826 NewIndent = std::min(State.Column + 1, NewIndent);
827 }
828 const FormatToken *NextNoComment = Current.getNextNonComment();
829 AvoidBinPacking = Current.Type == TT_ArrayInitializerLSquare ||
830 Current.Type == TT_DictLiteral ||
831 Style.Language == FormatStyle::LK_Proto ||
832 !Style.BinPackParameters ||
833 (NextNoComment &&
834 NextNoComment->Type == TT_DesignatedInitializerPeriod);
835 } else {
836 NewIndent = Style.ContinuationIndentWidth +
837 std::max(State.Stack.back().LastSpace,
838 State.Stack.back().StartOfFunctionCall);
839 AvoidBinPacking = !Style.BinPackParameters ||
840 (Style.ExperimentalAutoDetectBinPacking &&
841 (Current.PackingKind == PPK_OnePerLine ||
842 (!BinPackInconclusiveFunctions &&
843 Current.PackingKind == PPK_Inconclusive)));
844 // If this '[' opens an ObjC call, determine whether all parameters fit
845 // into one line and put one per line if they don't.
846 if (Current.Type == TT_ObjCMethodExpr && Style.ColumnLimit != 0 &&
847 getLengthToMatchingParen(Current) + State.Column >
848 getColumnLimit(State))
849 BreakBeforeParameter = true;
850 }
851 bool NoLineBreak = State.Stack.back().NoLineBreak ||
852 (Current.Type == TT_TemplateOpener &&
853 State.Stack.back().ContainsUnwrappedBuilder);
854 State.Stack.push_back(ParenState(NewIndent, NewIndentLevel,
855 State.Stack.back().LastSpace,
856 AvoidBinPacking, NoLineBreak));
857 State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000858 State.Stack.back().HasMultipleNestedBlocks = Current.BlockParameterCount > 1;
Daniel Jasper60553be2014-05-26 13:10:39 +0000859}
860
861void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
862 const FormatToken &Current = *State.NextToken;
863 if (!Current.closesScope())
864 return;
865
866 // If we encounter a closing ), ], } or >, we can remove a level from our
867 // stacks.
868 if (State.Stack.size() > 1 &&
869 (Current.isOneOf(tok::r_paren, tok::r_square) ||
870 (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
Daniel Jasper335ff262014-05-28 09:11:53 +0000871 State.NextToken->Type == TT_TemplateCloser))
Daniel Jasper60553be2014-05-26 13:10:39 +0000872 State.Stack.pop_back();
Daniel Jasper335ff262014-05-28 09:11:53 +0000873
Daniel Jasper60553be2014-05-26 13:10:39 +0000874 if (Current.is(tok::r_square)) {
875 // If this ends the array subscript expr, reset the corresponding value.
876 const FormatToken *NextNonComment = Current.getNextNonComment();
877 if (NextNonComment && NextNonComment->isNot(tok::l_square))
878 State.Stack.back().StartOfArraySubscripts = 0;
879 }
880}
881
882void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
Daniel Jasper60553be2014-05-26 13:10:39 +0000883 // If we have already found more than one lambda introducers on this level, we
884 // opt out of this because similarity between the lambdas is more important.
Daniel Jasper114a2bc2014-06-03 12:02:45 +0000885 if (fakeRParenSpecialCase(State))
Daniel Jasper335ff262014-05-28 09:11:53 +0000886 consumeRParens(State, *State.NextToken->MatchingParen);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000887
Daniel Jasper60553be2014-05-26 13:10:39 +0000888 // For some reason, ObjC blocks are indented like continuations.
889 unsigned NewIndent = State.Stack.back().LastSpace +
890 (State.NextToken->Type == TT_ObjCBlockLBrace
891 ? Style.ContinuationIndentWidth
892 : Style.IndentWidth);
893 State.Stack.push_back(ParenState(
894 NewIndent, /*NewIndentLevel=*/State.Stack.back().IndentLevel + 1,
895 State.Stack.back().LastSpace, /*AvoidBinPacking=*/true,
896 State.Stack.back().NoLineBreak));
897 State.Stack.back().BreakBeforeParameter = true;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000898}
899
Alexander Kornienko917f9e02013-09-10 12:29:48 +0000900unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
901 LineState &State) {
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000902 // Break before further function parameters on all levels.
903 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
904 State.Stack[i].BreakBeforeParameter = true;
905
Alexander Kornienko39856b72013-09-10 09:38:25 +0000906 unsigned ColumnsUsed = State.Column;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000907 // We can only affect layout of the first and the last line, so the penalty
908 // for all other lines is constant, and we ignore it.
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000909 State.Column = Current.LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000910
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000911 if (ColumnsUsed > getColumnLimit(State))
912 return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000913 return 0;
914}
915
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000916static bool getRawStringLiteralPrefixPostfix(StringRef Text, StringRef &Prefix,
Alexander Kornienko81e32942013-09-16 20:20:49 +0000917 StringRef &Postfix) {
918 if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") ||
919 Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") ||
920 Text.startswith(Prefix = "LR\"")) {
921 size_t ParenPos = Text.find('(');
922 if (ParenPos != StringRef::npos) {
923 StringRef Delimiter =
924 Text.substr(Prefix.size(), ParenPos - Prefix.size());
925 Prefix = Text.substr(0, ParenPos + 1);
926 Postfix = Text.substr(Text.size() - 2 - Delimiter.size());
927 return Postfix.front() == ')' && Postfix.back() == '"' &&
928 Postfix.substr(1).startswith(Delimiter);
929 }
930 }
931 return false;
932}
933
Daniel Jasperde0328a2013-08-16 11:20:30 +0000934unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
935 LineState &State,
936 bool DryRun) {
Alexander Kornienko917f9e02013-09-10 12:29:48 +0000937 // Don't break multi-line tokens other than block comments. Instead, just
938 // update the state.
939 if (Current.Type != TT_BlockComment && Current.IsMultiline)
940 return addMultilineToken(Current, State);
941
Daniel Jasper98857842013-10-30 13:54:53 +0000942 // Don't break implicit string literals.
943 if (Current.Type == TT_ImplicitStringLiteral)
944 return 0;
945
Daniel Jasper04b6a082013-12-20 06:22:01 +0000946 if (!Current.isStringLiteral() && !Current.is(tok::comment))
Daniel Jasperf93551c2013-08-23 10:05:49 +0000947 return 0;
948
Ahmed Charlesb8984322014-03-07 20:03:18 +0000949 std::unique_ptr<BreakableToken> Token;
Alexander Kornienko39856b72013-09-10 09:38:25 +0000950 unsigned StartColumn = State.Column - Current.ColumnWidth;
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +0000951 unsigned ColumnLimit = getColumnLimit(State);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000952
Daniel Jasper04b6a082013-12-20 06:22:01 +0000953 if (Current.isStringLiteral()) {
Alexander Kornienko384b40b2013-10-11 21:43:05 +0000954 // Don't break string literals inside preprocessor directives (except for
955 // #define directives, as their contents are stored in separate lines and
956 // are not affected by this check).
957 // This way we avoid breaking code with line directives and unknown
958 // preprocessor directives that contain long string literals.
959 if (State.Line->Type == LT_PreprocessorDirective)
960 return 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000961 // Exempts unterminated string literals from line breaking. The user will
962 // likely want to terminate the string before any line breaking is done.
963 if (Current.IsUnterminatedLiteral)
964 return 0;
965
Alexander Kornienko81e32942013-09-16 20:20:49 +0000966 StringRef Text = Current.TokenText;
967 StringRef Prefix;
968 StringRef Postfix;
Daniel Jasper174b0122014-01-09 14:18:12 +0000969 bool IsNSStringLiteral = false;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000970 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
971 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
972 // reduce the overhead) for each FormatToken, which is a string, so that we
973 // don't run multiple checks here on the hot path.
Daniel Jasper174b0122014-01-09 14:18:12 +0000974 if (Text.startswith("\"") && Current.Previous &&
975 Current.Previous->is(tok::at)) {
976 IsNSStringLiteral = true;
977 Prefix = "@\"";
Daniel Jasper174b0122014-01-09 14:18:12 +0000978 }
Alexander Kornienko81e32942013-09-16 20:20:49 +0000979 if ((Text.endswith(Postfix = "\"") &&
Daniel Jasper174b0122014-01-09 14:18:12 +0000980 (IsNSStringLiteral || Text.startswith(Prefix = "\"") ||
981 Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") ||
982 Text.startswith(Prefix = "u8\"") ||
Alexander Kornienko81e32942013-09-16 20:20:49 +0000983 Text.startswith(Prefix = "L\""))) ||
984 (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) ||
985 getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) {
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000986 Token.reset(new BreakableStringLiteral(
987 Current, State.Line->Level, StartColumn, Prefix, Postfix,
988 State.Line->InPPDirective, Encoding, Style));
Alexander Kornienko81e32942013-09-16 20:20:49 +0000989 } else {
990 return 0;
991 }
Daniel Jasperde0328a2013-08-16 11:20:30 +0000992 } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
Alexander Kornienkoce9161a2014-01-02 15:13:14 +0000993 if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
994 return 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000995 Token.reset(new BreakableBlockComment(
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000996 Current, State.Line->Level, StartColumn, Current.OriginalColumn,
997 !Current.Previous, State.Line->InPPDirective, Encoding, Style));
Daniel Jasperde0328a2013-08-16 11:20:30 +0000998 } else if (Current.Type == TT_LineComment &&
Craig Topper2145bc02014-05-09 08:15:10 +0000999 (Current.Previous == nullptr ||
Daniel Jasperde0328a2013-08-16 11:20:30 +00001000 Current.Previous->Type != TT_ImplicitStringLiteral)) {
Alexander Kornienkoce9161a2014-01-02 15:13:14 +00001001 if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
1002 return 0;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +00001003 Token.reset(new BreakableLineComment(Current, State.Line->Level,
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +00001004 StartColumn, /*InPPDirective=*/false,
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +00001005 Encoding, Style));
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +00001006 // We don't insert backslashes when breaking line comments.
1007 ColumnLimit = Style.ColumnLimit;
Daniel Jasperde0328a2013-08-16 11:20:30 +00001008 } else {
1009 return 0;
1010 }
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +00001011 if (Current.UnbreakableTailLength >= ColumnLimit)
Daniel Jasperde0328a2013-08-16 11:20:30 +00001012 return 0;
1013
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +00001014 unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength;
Daniel Jasperde0328a2013-08-16 11:20:30 +00001015 bool BreakInserted = false;
1016 unsigned Penalty = 0;
1017 unsigned RemainingTokenColumns = 0;
1018 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
1019 LineIndex != EndIndex; ++LineIndex) {
1020 if (!DryRun)
1021 Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
1022 unsigned TailOffset = 0;
1023 RemainingTokenColumns =
1024 Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
1025 while (RemainingTokenColumns > RemainingSpace) {
1026 BreakableToken::Split Split =
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +00001027 Token->getSplit(LineIndex, TailOffset, ColumnLimit);
Daniel Jasperde0328a2013-08-16 11:20:30 +00001028 if (Split.first == StringRef::npos) {
1029 // The last line's penalty is handled in addNextStateToQueue().
1030 if (LineIndex < EndIndex - 1)
1031 Penalty += Style.PenaltyExcessCharacter *
1032 (RemainingTokenColumns - RemainingSpace);
1033 break;
1034 }
1035 assert(Split.first != 0);
1036 unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
1037 LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
Alexander Kornienko875395f2013-11-12 17:50:13 +00001038
1039 // We can remove extra whitespace instead of breaking the line.
1040 if (RemainingTokenColumns + 1 - Split.second <= RemainingSpace) {
1041 RemainingTokenColumns = 0;
1042 if (!DryRun)
1043 Token->replaceWhitespace(LineIndex, TailOffset, Split, Whitespaces);
1044 break;
1045 }
1046
Alexander Kornienko64a42b82014-04-15 14:52:43 +00001047 // When breaking before a tab character, it may be moved by a few columns,
1048 // but will still be expanded to the next tab stop, so we don't save any
1049 // columns.
1050 if (NewRemainingTokenColumns == RemainingTokenColumns)
1051 break;
1052
Daniel Jasperde0328a2013-08-16 11:20:30 +00001053 assert(NewRemainingTokenColumns < RemainingTokenColumns);
1054 if (!DryRun)
1055 Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
Daniel Jasper2739af32013-08-28 10:03:58 +00001056 Penalty += Current.SplitPenalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +00001057 unsigned ColumnsUsed =
1058 Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +00001059 if (ColumnsUsed > ColumnLimit) {
1060 Penalty += Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit);
Daniel Jasperde0328a2013-08-16 11:20:30 +00001061 }
1062 TailOffset += Split.first + Split.second;
1063 RemainingTokenColumns = NewRemainingTokenColumns;
1064 BreakInserted = true;
1065 }
1066 }
1067
1068 State.Column = RemainingTokenColumns;
1069
1070 if (BreakInserted) {
1071 // If we break the token inside a parameter list, we need to break before
1072 // the next parameter on all levels, so that the next parameter is clearly
1073 // visible. Line comments already introduce a break.
1074 if (Current.Type != TT_LineComment) {
1075 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
1076 State.Stack[i].BreakBeforeParameter = true;
1077 }
1078
Daniel Jasper04b6a082013-12-20 06:22:01 +00001079 Penalty += Current.isStringLiteral() ? Style.PenaltyBreakString
1080 : Style.PenaltyBreakComment;
Daniel Jasper2739af32013-08-28 10:03:58 +00001081
Daniel Jasperde0328a2013-08-16 11:20:30 +00001082 State.Stack.back().LastSpace = StartColumn;
1083 }
1084 return Penalty;
1085}
1086
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001087unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
Daniel Jasperde0328a2013-08-16 11:20:30 +00001088 // In preprocessor directives reserve two chars for trailing " \"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001089 return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
Daniel Jasperde0328a2013-08-16 11:20:30 +00001090}
1091
Daniel Jasperc39b56f2013-12-16 07:23:08 +00001092bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
Daniel Jasperf438cb72013-08-23 11:57:34 +00001093 const FormatToken &Current = *State.NextToken;
Daniel Jasper9509f182014-05-05 08:08:07 +00001094 if (!Current.isStringLiteral() || Current.Type == TT_ImplicitStringLiteral)
Daniel Jasperf438cb72013-08-23 11:57:34 +00001095 return false;
Alexander Kornienkod7b837e2013-08-29 17:32:57 +00001096 // We never consider raw string literals "multiline" for the purpose of
Daniel Jasperc39b56f2013-12-16 07:23:08 +00001097 // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
1098 // (see TokenAnnotator::mustBreakBefore().
Alexander Kornienkod7b837e2013-08-29 17:32:57 +00001099 if (Current.TokenText.startswith("R\""))
1100 return false;
Alexander Kornienko39856b72013-09-10 09:38:25 +00001101 if (Current.IsMultiline)
Alexander Kornienkod7b837e2013-08-29 17:32:57 +00001102 return true;
Daniel Jasperf438cb72013-08-23 11:57:34 +00001103 if (Current.getNextNonComment() &&
Daniel Jasper04b6a082013-12-20 06:22:01 +00001104 Current.getNextNonComment()->isStringLiteral())
Daniel Jasperf438cb72013-08-23 11:57:34 +00001105 return true; // Implicit concatenation.
Alexander Kornienko39856b72013-09-10 09:38:25 +00001106 if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
Daniel Jasperf438cb72013-08-23 11:57:34 +00001107 Style.ColumnLimit)
1108 return true; // String will be split.
Alexander Kornienkod7b837e2013-08-29 17:32:57 +00001109 return false;
Daniel Jasperf438cb72013-08-23 11:57:34 +00001110}
1111
Daniel Jasperde0328a2013-08-16 11:20:30 +00001112} // namespace format
1113} // namespace clang