blob: e1a559d0df12604b7aa59a6bad6c575b333b65c2 [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 Jasperde0328a2013-08-16 11:20:30 +0000115 return !State.Stack.back().NoLineBreak;
116}
117
118bool 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;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000123 if (State.Stack.back().BreakBeforeClosingBrace &&
124 Current.closesBlockTypeList(Style))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000125 return true;
126 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
127 return true;
Daniel Jasperec01cd62013-10-08 05:11:18 +0000128 if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
Daniel Jasper165b29e2013-11-08 00:57:11 +0000129 (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))) &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000134 State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
135 !Current.isOneOf(tok::r_paren, tok::r_brace))
136 return true;
137 if (Style.AlwaysBreakBeforeMultilineStrings &&
Daniel Jasperf438cb72013-08-23 11:57:34 +0000138 State.Column > State.Stack.back().Indent && // Breaking saves columns.
Daniel Jasper27943052013-11-09 03:08:25 +0000139 !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) &&
Daniel Jasperc39b56f2013-12-16 07:23:08 +0000140 Previous.Type != TT_InlineASMColon && nextIsMultilineString(State))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000141 return true;
Daniel Jasperb596fb22013-10-24 10:31:50 +0000142 if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) ||
Daniel Jasper1db6c382013-10-22 15:30:28 +0000143 Previous.Type == TT_ArrayInitializerLSquare) &&
Daniel Jasperdb8804b2014-04-14 12:11:07 +0000144 Style.ColumnLimit > 0 &&
Daniel Jasperd489dd32013-10-20 16:45:46 +0000145 getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State))
146 return true;
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000147 if (Current.Type == TT_CtorInitializerColon &&
Daniel Jasperd74cf402014-04-08 12:46:38 +0000148 ((Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All) ||
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000149 Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0))
150 return true;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000151
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000152 if (State.Column < getNewLineColumn(State))
153 return false;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000154 if (!Style.BreakBeforeBinaryOperators) {
155 // If we need to break somewhere inside the LHS of a binary expression, we
156 // should also break after the operator. Otherwise, the formatting would
157 // hide the operator precedence, e.g. in:
158 // if (aaaaaaaaaaaaaa ==
159 // bbbbbbbbbbbbbb && c) {..
160 // For comparisons, we only apply this rule, if the LHS is a binary
161 // expression itself as otherwise, the line breaks seem superfluous.
162 // We need special cases for ">>" which we have split into two ">" while
163 // lexing in order to make template parsing easier.
164 //
165 // FIXME: We'll need something similar for styles that break before binary
166 // operators.
167 bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
168 Previous.getPrecedence() == prec::Equality) &&
169 Previous.Previous &&
170 Previous.Previous->Type != TT_BinaryOperator; // For >>.
171 bool LHSIsBinaryExpr =
Daniel Jasper562ecd42013-09-06 08:08:14 +0000172 Previous.Previous && Previous.Previous->EndsBinaryExpression;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000173 if (Previous.Type == TT_BinaryOperator &&
174 (!IsComparison || LHSIsBinaryExpr) &&
175 Current.Type != TT_BinaryOperator && // For >>.
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000176 !Current.isTrailingComment() && !Previous.is(tok::lessless) &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000177 Previous.getPrecedence() != prec::Assignment &&
178 State.Stack.back().BreakBeforeParameter)
179 return true;
180 }
181
182 // Same as above, but for the first "<<" operator.
Alexander Kornienko86b2dfd2014-03-06 15:13:08 +0000183 if (Current.is(tok::lessless) && Current.Type != TT_OverloadedOperator &&
184 State.Stack.back().BreakBeforeParameter &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000185 State.Stack.back().FirstLessLess == 0)
186 return true;
187
Daniel Jasperde0328a2013-08-16 11:20:30 +0000188 if (Current.Type == TT_ObjCSelectorName &&
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000189 State.Stack.back().ObjCSelectorNameFound &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000190 State.Stack.back().BreakBeforeParameter)
191 return true;
Daniel Jasper05cd5862014-05-08 12:21:30 +0000192 if (Previous.ClosesTemplateDeclaration && Current.NestingLevel == 0 &&
Alexander Kornienkoa594ba82013-12-16 14:35:51 +0000193 !Current.isTrailingComment())
Daniel Jasperde0328a2013-08-16 11:20:30 +0000194 return true;
195
196 if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000197 State.Line->MightBeFunctionDecl &&
Daniel Jasper05cd5862014-05-08 12:21:30 +0000198 State.Stack.back().BreakBeforeParameter && Current.NestingLevel == 0)
Daniel Jasperde0328a2013-08-16 11:20:30 +0000199 return true;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000200 if (startsSegmentOfBuilderTypeCall(Current) &&
Daniel Jasperf8151e92013-08-30 07:12:40 +0000201 (State.Stack.back().CallContinuation != 0 ||
202 (State.Stack.back().BreakBeforeParameter &&
203 State.Stack.back().ContainsUnwrappedBuilder)))
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000204 return true;
Daniel Jasper96972812014-01-05 12:38:10 +0000205
206 // The following could be precomputed as they do not depend on the state.
207 // However, as they should take effect only if the UnwrappedLine does not fit
208 // into the ColumnLimit, they are checked here in the ContinuationIndenter.
Daniel Jasper35995672014-04-29 14:05:20 +0000209 if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block &&
210 Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment))
Daniel Jasper96972812014-01-05 12:38:10 +0000211 return true;
Daniel Jasper96972812014-01-05 12:38:10 +0000212
Daniel Jasperde0328a2013-08-16 11:20:30 +0000213 return false;
214}
215
216unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000217 bool DryRun,
218 unsigned ExtraSpaces) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000219 const FormatToken &Current = *State.NextToken;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000220
Manuel Klimek819788d2014-03-18 11:22:45 +0000221 assert(!State.Stack.empty());
222 if ((Current.Type == TT_ImplicitStringLiteral &&
Craig Topper2145bc02014-05-09 08:15:10 +0000223 (Current.Previous->Tok.getIdentifierInfo() == nullptr ||
Daniel Jasper98857842013-10-30 13:54:53 +0000224 Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
225 tok::pp_not_keyword))) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000226 // FIXME: Is this correct?
227 int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
228 State.NextToken->WhitespaceRange.getEnd()) -
229 SourceMgr.getSpellingColumnNumber(
230 State.NextToken->WhitespaceRange.getBegin());
Daniel Jasperda353cd2014-03-12 08:24:47 +0000231 State.Column += WhitespaceLength;
Daniel Jasper240dfda2014-03-31 14:23:49 +0000232 moveStateToNextToken(State, DryRun, /*Newline=*/false);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000233 return 0;
234 }
235
Alexander Kornienko1f803962013-10-01 14:41:18 +0000236 unsigned Penalty = 0;
237 if (Newline)
238 Penalty = addTokenOnNewLine(State, DryRun);
239 else
Daniel Jasper48437ce2013-11-20 14:54:39 +0000240 addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
Alexander Kornienko1f803962013-10-01 14:41:18 +0000241
242 return moveStateToNextToken(State, DryRun, Newline) + Penalty;
243}
244
Daniel Jasper48437ce2013-11-20 14:54:39 +0000245void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
246 unsigned ExtraSpaces) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000247 FormatToken &Current = *State.NextToken;
Alexander Kornienko1f803962013-10-01 14:41:18 +0000248 const FormatToken &Previous = *State.NextToken->Previous;
249 if (Current.is(tok::equal) &&
Daniel Jasper05cd5862014-05-08 12:21:30 +0000250 (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
Alexander Kornienko1f803962013-10-01 14:41:18 +0000251 State.Stack.back().VariablePos == 0) {
252 State.Stack.back().VariablePos = State.Column;
253 // Move over * and & if they are bound to the variable name.
254 const FormatToken *Tok = &Previous;
255 while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
256 State.Stack.back().VariablePos -= Tok->ColumnWidth;
257 if (Tok->SpacesRequiredBefore != 0)
258 break;
259 Tok = Tok->Previous;
260 }
261 if (Previous.PartOfMultiVariableDeclStmt)
262 State.Stack.back().LastSpace = State.Stack.back().VariablePos;
263 }
264
265 unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
266
267 if (!DryRun)
268 Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0,
269 Spaces, State.Column + Spaces);
270
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000271 if (Current.Type == TT_ObjCSelectorName &&
272 !State.Stack.back().ObjCSelectorNameFound) {
273 if (Current.LongestObjCSelectorName == 0)
274 State.Stack.back().AlignColons = false;
275 else if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
276 State.Column + Spaces + Current.ColumnWidth)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000277 State.Stack.back().ColonPos =
278 State.Stack.back().Indent + Current.LongestObjCSelectorName;
279 else
280 State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth;
281 }
282
283 if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
Daniel Jasper5a611392013-12-19 21:41:37 +0000284 (Current.Type != TT_LineComment || Previous.BlockKind == BK_BracedInit))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000285 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jasperec01cd62013-10-08 05:11:18 +0000286 if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000287 State.Stack.back().NoLineBreak = true;
288 if (startsSegmentOfBuilderTypeCall(Current))
289 State.Stack.back().ContainsUnwrappedBuilder = true;
290
291 State.Column += Spaces;
Daniel Jasper8acf8222014-05-07 09:23:05 +0000292 if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
293 Previous.Previous && Previous.Previous->isOneOf(tok::kw_if, tok::kw_for))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000294 // Treat the condition inside an if as if it was a second function
Daniel Jasper6633ab82013-10-18 10:38:14 +0000295 // parameter, i.e. let nested calls have a continuation indent.
Daniel Jasper8acf8222014-05-07 09:23:05 +0000296 State.Stack.back().LastSpace = State.Column;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000297 else if (Current.isNot(tok::comment) &&
298 (Previous.is(tok::comma) ||
299 (Previous.is(tok::colon) && Previous.Type == TT_ObjCMethodExpr)))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000300 State.Stack.back().LastSpace = State.Column;
301 else if ((Previous.Type == TT_BinaryOperator ||
302 Previous.Type == TT_ConditionalExpr ||
Alexander Kornienko1f803962013-10-01 14:41:18 +0000303 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasper0e617842014-04-16 12:26:54 +0000304 ((Previous.getPrecedence() != prec::Assignment &&
305 (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
306 !Previous.LastOperator)) ||
Alexander Kornienko1f803962013-10-01 14:41:18 +0000307 Current.StartsBinaryExpression))
308 // Always indent relative to the RHS of the expression unless this is a
309 // simple assignment without binary expression on the RHS. Also indent
310 // relative to unary operators and the colons of constructor initializers.
311 State.Stack.back().LastSpace = State.Column;
Daniel Jasperf9a5e402013-10-08 16:24:07 +0000312 else if (Previous.Type == TT_InheritanceColon) {
Alexander Kornienko1f803962013-10-01 14:41:18 +0000313 State.Stack.back().Indent = State.Column;
Daniel Jasperf9a5e402013-10-08 16:24:07 +0000314 State.Stack.back().LastSpace = State.Column;
315 } else if (Previous.opensScope()) {
Alexander Kornienko1f803962013-10-01 14:41:18 +0000316 // If a function has a trailing call, indent all parameters from the
317 // opening parenthesis. This avoids confusing indents like:
318 // OuterFunction(InnerFunctionCall( // break
319 // ParameterToInnerFunction)) // break
320 // .SecondInnerFunctionCall();
321 bool HasTrailingCall = false;
322 if (Previous.MatchingParen) {
323 const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
324 HasTrailingCall = Next && Next->isMemberAccess();
325 }
326 if (HasTrailingCall &&
327 State.Stack[State.Stack.size() - 2].CallContinuation == 0)
328 State.Stack.back().LastSpace = State.Column;
329 }
330}
331
332unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
333 bool DryRun) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000334 FormatToken &Current = *State.NextToken;
Alexander Kornienko1f803962013-10-01 14:41:18 +0000335 const FormatToken &Previous = *State.NextToken->Previous;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000336
Alexander Kornienko1f803962013-10-01 14:41:18 +0000337 // Extra penalty that needs to be added because of the way certain line
338 // breaks are chosen.
339 unsigned Penalty = 0;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000340
Daniel Jaspera0407742014-02-11 10:08:11 +0000341 const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
342 const FormatToken *NextNonComment = Previous.getNextNonComment();
343 if (!NextNonComment)
344 NextNonComment = &Current;
Daniel Jasper05cd5862014-05-08 12:21:30 +0000345 // The first line break on any NestingLevel causes an extra penalty in order
Alexander Kornienko1f803962013-10-01 14:41:18 +0000346 // prefer similar line breaks.
347 if (!State.Stack.back().ContainsLineBreak)
348 Penalty += 15;
349 State.Stack.back().ContainsLineBreak = true;
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000350
Alexander Kornienko1f803962013-10-01 14:41:18 +0000351 Penalty += State.NextToken->SplitPenalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000352
Alexander Kornienko1f803962013-10-01 14:41:18 +0000353 // Breaking before the first "<<" is generally not desirable if the LHS is
Daniel Jasper48437ce2013-11-20 14:54:39 +0000354 // short. Also always add the penalty if the LHS is split over mutliple lines
Daniel Jasper2b7556e2014-04-03 12:00:27 +0000355 // to avoid unnecessary line breaks that just work around this penalty.
Daniel Jaspera0407742014-02-11 10:08:11 +0000356 if (NextNonComment->is(tok::lessless) &&
357 State.Stack.back().FirstLessLess == 0 &&
Daniel Jasper004177e2013-12-19 16:06:40 +0000358 (State.Column <= Style.ColumnLimit / 3 ||
Daniel Jasper48437ce2013-11-20 14:54:39 +0000359 State.Stack.back().BreakBeforeParameter))
Alexander Kornienko1f803962013-10-01 14:41:18 +0000360 Penalty += Style.PenaltyBreakFirstLessLess;
361
Daniel Jasper9f388d02014-03-27 14:33:30 +0000362 State.Column = getNewLineColumn(State);
363 if (NextNonComment->isMemberAccess()) {
364 if (State.Stack.back().CallContinuation == 0)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000365 State.Stack.back().CallContinuation = State.Column;
Daniel Jaspera0407742014-02-11 10:08:11 +0000366 } else if (NextNonComment->Type == TT_ObjCSelectorName) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000367 if (!State.Stack.back().ObjCSelectorNameFound) {
Daniel Jaspera0407742014-02-11 10:08:11 +0000368 if (NextNonComment->LongestObjCSelectorName == 0) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000369 State.Stack.back().AlignColons = false;
370 } else {
371 State.Stack.back().ColonPos =
Daniel Jaspera0407742014-02-11 10:08:11 +0000372 State.Stack.back().Indent + NextNonComment->LongestObjCSelectorName;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000373 }
Daniel Jasper9f388d02014-03-27 14:33:30 +0000374 } else if (State.Stack.back().AlignColons &&
375 State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) {
Daniel Jaspera0407742014-02-11 10:08:11 +0000376 State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000377 }
Daniel Jasper1fd6f1f2014-03-17 14:32:47 +0000378 } else if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
379 (PreviousNonComment->Type == TT_ObjCMethodExpr ||
380 PreviousNonComment->Type == TT_DictLiteral)) {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000381 // FIXME: This is hacky, find a better way. The problem is that in an ObjC
382 // method expression, the block should be aligned to the line starting it,
383 // e.g.:
384 // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
385 // ^(int *i) {
386 // // ...
387 // }];
Daniel Jasper05cd5862014-05-08 12:21:30 +0000388 // Thus, we set LastSpace of the next higher NestingLevel, to which we move
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000389 // when we consume all of the "}"'s FakeRParens at the "{".
Daniel Jasper9a26e772013-12-23 11:25:40 +0000390 if (State.Stack.size() > 1)
Daniel Jasper9f388d02014-03-27 14:33:30 +0000391 State.Stack[State.Stack.size() - 2].LastSpace =
392 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
393 Style.ContinuationIndentWidth;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000394 }
395
Alexander Kornienko1f803962013-10-01 14:41:18 +0000396 if ((Previous.isOneOf(tok::comma, tok::semi) &&
397 !State.Stack.back().AvoidBinPacking) ||
398 Previous.Type == TT_BinaryOperator)
399 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper05cd5862014-05-08 12:21:30 +0000400 if (Previous.Type == TT_TemplateCloser && Current.NestingLevel == 0)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000401 State.Stack.back().BreakBeforeParameter = false;
Daniel Jaspera0407742014-02-11 10:08:11 +0000402 if (NextNonComment->is(tok::question) ||
Daniel Jasper165b29e2013-11-08 00:57:11 +0000403 (PreviousNonComment && PreviousNonComment->is(tok::question)))
404 State.Stack.back().BreakBeforeParameter = true;
Alexander Kornienko1f803962013-10-01 14:41:18 +0000405
406 if (!DryRun) {
407 unsigned Newlines = 1;
408 if (Current.is(tok::comment))
409 Newlines = std::max(Newlines, std::min(Current.NewlinesBefore,
410 Style.MaxEmptyLinesToKeep + 1));
Alexander Kornienkoe2e03872013-10-14 00:46:35 +0000411 Whitespaces.replaceWhitespace(Current, Newlines,
412 State.Stack.back().IndentLevel, State.Column,
413 State.Column, State.Line->InPPDirective);
Alexander Kornienko1f803962013-10-01 14:41:18 +0000414 }
415
416 if (!Current.isTrailingComment())
417 State.Stack.back().LastSpace = State.Column;
Daniel Jasper05cd5862014-05-08 12:21:30 +0000418 State.StartOfLineLevel = Current.NestingLevel;
419 State.LowestLevelOnLine = Current.NestingLevel;
Alexander Kornienko1f803962013-10-01 14:41:18 +0000420
421 // Any break on this level means that the parent level has been broken
422 // and we need to avoid bin packing there.
Daniel Jasperb16b9692014-05-21 12:51:23 +0000423 bool JavaScriptFormat = Style.Language == FormatStyle::LK_JavaScript &&
424 Current.is(tok::r_brace) &&
425 State.Stack.size() > 1 &&
426 State.Stack[State.Stack.size() - 2].JSFunctionInlined;
427 if (!JavaScriptFormat) {
428 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
429 State.Stack[i].BreakBeforeParameter = true;
430 }
Alexander Kornienko1f803962013-10-01 14:41:18 +0000431 }
Daniel Jasperb16b9692014-05-21 12:51:23 +0000432
Daniel Jasper9e5ede02013-11-08 19:56:28 +0000433 if (PreviousNonComment &&
434 !PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
435 PreviousNonComment->Type != TT_TemplateCloser &&
436 PreviousNonComment->Type != TT_BinaryOperator &&
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000437 Current.Type != TT_BinaryOperator && !PreviousNonComment->opensScope())
Alexander Kornienko1f803962013-10-01 14:41:18 +0000438 State.Stack.back().BreakBeforeParameter = true;
439
Daniel Jasper1db6c382013-10-22 15:30:28 +0000440 // If we break after { or the [ of an array initializer, we should also break
441 // before the corresponding } or ].
442 if (Previous.is(tok::l_brace) || Previous.Type == TT_ArrayInitializerLSquare)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000443 State.Stack.back().BreakBeforeClosingBrace = true;
444
445 if (State.Stack.back().AvoidBinPacking) {
446 // If we are breaking after '(', '{', '<', this is not bin packing
Daniel Jasper2a958322014-05-21 13:26:58 +0000447 // unless AllowAllParametersOfDeclarationOnNextLine is false or this is a
448 // dict/object literal.
Alexander Kornienko1f803962013-10-01 14:41:18 +0000449 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
450 Previous.Type == TT_BinaryOperator) ||
451 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
Daniel Jasper2a958322014-05-21 13:26:58 +0000452 State.Line->MustBeDeclaration) ||
453 Previous.Type == TT_DictLiteral)
Alexander Kornienko1f803962013-10-01 14:41:18 +0000454 State.Stack.back().BreakBeforeParameter = true;
455 }
456
457 return Penalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000458}
459
Daniel Jasper9f388d02014-03-27 14:33:30 +0000460unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000461 if (!State.NextToken || !State.NextToken->Previous)
462 return 0;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000463 FormatToken &Current = *State.NextToken;
464 const FormatToken &Previous = *State.NextToken->Previous;
465 // If we are continuing an expression, we want to use the continuation indent.
466 unsigned ContinuationIndent =
467 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
468 Style.ContinuationIndentWidth;
469 const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
470 const FormatToken *NextNonComment = Previous.getNextNonComment();
471 if (!NextNonComment)
472 NextNonComment = &Current;
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000473 if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
Daniel Jasper05cd5862014-05-08 12:21:30 +0000474 return Current.NestingLevel == 0 ? State.FirstIndent
475 : State.Stack.back().Indent;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000476 if (Current.isOneOf(tok::r_brace, tok::r_square)) {
Daniel Jasperb16b9692014-05-21 12:51:23 +0000477 if (State.Stack.size() > 1 &&
478 State.Stack[State.Stack.size() - 2].JSFunctionInlined)
479 return State.FirstIndent;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000480 if (Current.closesBlockTypeList(Style) ||
481 (Current.MatchingParen &&
482 Current.MatchingParen->BlockKind == BK_BracedInit))
483 return State.Stack[State.Stack.size() - 2].LastSpace;
484 else
485 return State.FirstIndent;
486 }
Daniel Jasper783bac62014-04-15 09:54:30 +0000487 if (Current.is(tok::identifier) && Current.Next &&
488 Current.Next->Type == TT_DictLiteral)
489 return State.Stack.back().Indent;
Daniel Jasper9f388d02014-03-27 14:33:30 +0000490 if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0)
491 return State.StartOfStringLiteral;
492 if (NextNonComment->is(tok::lessless) &&
493 State.Stack.back().FirstLessLess != 0)
494 return State.Stack.back().FirstLessLess;
495 if (NextNonComment->isMemberAccess()) {
496 if (State.Stack.back().CallContinuation == 0) {
497 return ContinuationIndent;
498 } else {
499 return State.Stack.back().CallContinuation;
500 }
501 }
502 if (State.Stack.back().QuestionColumn != 0 &&
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000503 ((NextNonComment->is(tok::colon) &&
504 NextNonComment->Type == TT_ConditionalExpr) ||
Daniel Jasper9f388d02014-03-27 14:33:30 +0000505 Previous.Type == TT_ConditionalExpr))
506 return State.Stack.back().QuestionColumn;
507 if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0)
508 return State.Stack.back().VariablePos;
509 if ((PreviousNonComment && (PreviousNonComment->ClosesTemplateDeclaration ||
510 PreviousNonComment->Type == TT_AttributeParen)) ||
511 ((NextNonComment->Type == TT_StartOfName ||
512 NextNonComment->is(tok::kw_operator)) &&
Daniel Jasper05cd5862014-05-08 12:21:30 +0000513 Current.NestingLevel == 0 &&
514 (!Style.IndentFunctionDeclarationAfterType ||
515 State.Line->StartsDefinition)))
Daniel Jasper9f388d02014-03-27 14:33:30 +0000516 return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
517 if (NextNonComment->Type == TT_ObjCSelectorName) {
518 if (!State.Stack.back().ObjCSelectorNameFound) {
519 if (NextNonComment->LongestObjCSelectorName == 0) {
520 return State.Stack.back().Indent;
521 } else {
522 return State.Stack.back().Indent +
523 NextNonComment->LongestObjCSelectorName -
524 NextNonComment->ColumnWidth;
525 }
526 } else if (!State.Stack.back().AlignColons) {
527 return State.Stack.back().Indent;
528 } else if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth) {
529 return State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
530 } else {
531 return State.Stack.back().Indent;
532 }
533 }
534 if (NextNonComment->Type == TT_ArraySubscriptLSquare) {
535 if (State.Stack.back().StartOfArraySubscripts != 0)
536 return State.Stack.back().StartOfArraySubscripts;
537 else
538 return ContinuationIndent;
539 }
540 if (NextNonComment->Type == TT_StartOfName ||
541 Previous.isOneOf(tok::coloncolon, tok::equal)) {
542 return ContinuationIndent;
543 }
544 if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
545 (PreviousNonComment->Type == TT_ObjCMethodExpr ||
546 PreviousNonComment->Type == TT_DictLiteral))
547 return ContinuationIndent;
548 if (NextNonComment->Type == TT_CtorInitializerColon)
549 return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
550 if (NextNonComment->Type == TT_CtorInitializerComma)
551 return State.Stack.back().Indent;
Daniel Jasper5d2587d2014-03-27 16:14:13 +0000552 if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
Daniel Jasper9f388d02014-03-27 14:33:30 +0000553 PreviousNonComment->isNot(tok::r_brace))
554 // Ensure that we fall back to the continuation indent width instead of
555 // just flushing continuations left.
556 return State.Stack.back().Indent + Style.ContinuationIndentWidth;
557 return State.Stack.back().Indent;
558}
559
Daniel Jasperde0328a2013-08-16 11:20:30 +0000560unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
561 bool DryRun, bool Newline) {
562 const FormatToken &Current = *State.NextToken;
563 assert(State.Stack.size());
564
565 if (Current.Type == TT_InheritanceColon)
566 State.Stack.back().AvoidBinPacking = true;
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000567 if (Current.is(tok::lessless) && Current.Type != TT_OverloadedOperator) {
568 if (State.Stack.back().FirstLessLess == 0)
569 State.Stack.back().FirstLessLess = State.Column;
570 else
571 State.Stack.back().LastOperatorWrapped = Newline;
572 }
573 if ((Current.Type == TT_BinaryOperator && Current.isNot(tok::lessless)) ||
574 Current.Type == TT_ConditionalExpr)
575 State.Stack.back().LastOperatorWrapped = Newline;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000576 if (Current.Type == TT_ArraySubscriptLSquare &&
Daniel Jasperde0328a2013-08-16 11:20:30 +0000577 State.Stack.back().StartOfArraySubscripts == 0)
578 State.Stack.back().StartOfArraySubscripts = State.Column;
Daniel Jasper165b29e2013-11-08 00:57:11 +0000579 if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) ||
580 (Current.getPreviousNonComment() && Current.isNot(tok::colon) &&
581 Current.getPreviousNonComment()->is(tok::question) &&
582 !Style.BreakBeforeTernaryOperators))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000583 State.Stack.back().QuestionColumn = State.Column;
584 if (!Current.opensScope() && !Current.closesScope())
585 State.LowestLevelOnLine =
Daniel Jasper05cd5862014-05-08 12:21:30 +0000586 std::min(State.LowestLevelOnLine, Current.NestingLevel);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000587 if (Current.isMemberAccess())
Daniel Jasperde0328a2013-08-16 11:20:30 +0000588 State.Stack.back().StartOfFunctionCall =
Daniel Jasper0e617842014-04-16 12:26:54 +0000589 Current.LastOperator ? 0 : State.Column + Current.ColumnWidth;
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000590 if (Current.Type == TT_ObjCSelectorName)
591 State.Stack.back().ObjCSelectorNameFound = true;
Daniel Jasper3ae6f5a2014-04-09 12:08:39 +0000592 if (Current.Type == TT_LambdaLSquare)
593 ++State.Stack.back().LambdasFound;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000594 if (Current.Type == TT_CtorInitializerColon) {
595 // Indent 2 from the column, so:
596 // SomeClass::SomeClass()
597 // : First(...), ...
598 // Next(...)
599 // ^ line up here.
600 State.Stack.back().Indent =
601 State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
602 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
603 State.Stack.back().AvoidBinPacking = true;
604 State.Stack.back().BreakBeforeParameter = false;
605 }
606
Daniel Jasperde0328a2013-08-16 11:20:30 +0000607 // In ObjC method declaration we align on the ":" of parameters, but we need
Daniel Jasper6633ab82013-10-18 10:38:14 +0000608 // to ensure that we indent parameters on subsequent lines by at least our
609 // continuation indent width.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000610 if (Current.Type == TT_ObjCMethodSpecifier)
Daniel Jasper6633ab82013-10-18 10:38:14 +0000611 State.Stack.back().Indent += Style.ContinuationIndentWidth;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000612
613 // Insert scopes created by fake parenthesis.
614 const FormatToken *Previous = Current.getPreviousNonComment();
Daniel Jasperb16b9692014-05-21 12:51:23 +0000615
616 // Add special behavior to support a format commonly used for JavaScript
617 // closures:
618 // SomeFunction(function() {
619 // foo();
620 // bar();
621 // }, a, b, c);
622 if (Style.Language == FormatStyle::LK_JavaScript) {
623 if (Current.isNot(tok::comment) && Previous && Previous->is(tok::l_brace) &&
624 State.Stack.size() > 1) {
625 if (State.Stack[State.Stack.size() - 2].JSFunctionInlined && Newline) {
626 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
627 State.Stack[i].NoLineBreak = true;
628 }
629 }
630 State.Stack[State.Stack.size() - 2].JSFunctionInlined = false;
631 }
632 if (Current.TokenText == "function")
633 State.Stack.back().JSFunctionInlined = !Newline;
634 }
635
Daniel Jasperde0328a2013-08-16 11:20:30 +0000636 // Don't add extra indentation for the first fake parenthesis after
Dinesh Dwivedi0db806b2014-05-01 17:19:34 +0000637 // 'return', assignments or opening <({[. The indentation for these cases
Daniel Jasperde0328a2013-08-16 11:20:30 +0000638 // is special cased.
639 bool SkipFirstExtraIndent =
Daniel Jaspereabede62013-09-30 08:29:03 +0000640 (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) ||
Daniel Jasperf48b5ab2013-11-07 19:23:49 +0000641 Previous->getPrecedence() == prec::Assignment ||
642 Previous->Type == TT_ObjCMethodExpr));
Daniel Jasperde0328a2013-08-16 11:20:30 +0000643 for (SmallVectorImpl<prec::Level>::const_reverse_iterator
644 I = Current.FakeLParens.rbegin(),
645 E = Current.FakeLParens.rend();
646 I != E; ++I) {
647 ParenState NewParenState = State.Stack.back();
648 NewParenState.ContainsLineBreak = false;
Daniel Jaspereabede62013-09-30 08:29:03 +0000649
650 // Indent from 'LastSpace' unless this the fake parentheses encapsulating a
651 // builder type call after 'return'. If such a call is line-wrapped, we
652 // commonly just want to indent from the start of the line.
653 if (!Previous || Previous->isNot(tok::kw_return) || *I > 0)
654 NewParenState.Indent =
655 std::max(std::max(State.Column, NewParenState.Indent),
656 State.Stack.back().LastSpace);
657
Daniel Jasper5c332652014-04-03 12:00:33 +0000658 // Don't allow the RHS of an operator to be split over multiple lines unless
659 // there is a line-break right after the operator.
660 // Exclude relational operators, as there, it is always more desirable to
661 // have the LHS 'left' of the RHS.
Daniel Jasperc0d606a2014-04-14 11:08:45 +0000662 if (Previous && Previous->getPrecedence() > prec::Assignment &&
663 (Previous->Type == TT_BinaryOperator ||
664 Previous->Type == TT_ConditionalExpr) &&
665 Previous->getPrecedence() != prec::Relational) {
666 bool BreakBeforeOperator = Previous->is(tok::lessless) ||
667 (Previous->Type == TT_BinaryOperator &&
668 Style.BreakBeforeBinaryOperators) ||
669 (Previous->Type == TT_ConditionalExpr &&
670 Style.BreakBeforeTernaryOperators);
671 if ((!Newline && !BreakBeforeOperator) ||
672 (!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator))
673 NewParenState.NoLineBreak = true;
674 }
Daniel Jasper5c332652014-04-03 12:00:33 +0000675
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000676 // Do not indent relative to the fake parentheses inserted for "." or "->".
677 // This is a special case to make the following to statements consistent:
678 // OuterFunction(InnerFunctionCall( // break
679 // ParameterToInnerFunction));
680 // OuterFunction(SomeObject.InnerFunctionCall( // break
681 // ParameterToInnerFunction));
682 if (*I > prec::Unknown)
683 NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
Daniel Jasper96964352013-12-18 10:44:36 +0000684 NewParenState.StartOfFunctionCall = State.Column;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000685
686 // Always indent conditional expressions. Never indent expression where
687 // the 'operator' is ',', ';' or an assignment (i.e. *I <=
688 // prec::Assignment) as those have different indentation rules. Indent
689 // other expression, unless the indentation needs to be skipped.
690 if (*I == prec::Conditional ||
691 (!SkipFirstExtraIndent && *I > prec::Assignment &&
692 !Style.BreakBeforeBinaryOperators))
Daniel Jasper6633ab82013-10-18 10:38:14 +0000693 NewParenState.Indent += Style.ContinuationIndentWidth;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000694 if ((Previous && !Previous->opensScope()) || *I > prec::Comma)
Daniel Jasperde0328a2013-08-16 11:20:30 +0000695 NewParenState.BreakBeforeParameter = false;
696 State.Stack.push_back(NewParenState);
697 SkipFirstExtraIndent = false;
698 }
699
700 // If we encounter an opening (, [, { or <, we add a level to our stacks to
701 // prepare for the following tokens.
702 if (Current.opensScope()) {
703 unsigned NewIndent;
Alexander Kornienkoe2e03872013-10-14 00:46:35 +0000704 unsigned NewIndentLevel = State.Stack.back().IndentLevel;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000705 bool AvoidBinPacking;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000706 bool BreakBeforeParameter = false;
707 if (Current.is(tok::l_brace) ||
708 Current.Type == TT_ArrayInitializerLSquare) {
Daniel Jasper3ae6f5a2014-04-09 12:08:39 +0000709 if (Current.MatchingParen && Current.BlockKind == BK_Block &&
710 State.Stack.back().LambdasFound <= 1) {
Daniel Jasperf3a5d002013-09-05 10:48:50 +0000711 // If this is an l_brace starting a nested block, we pretend (wrt. to
712 // indentation) that we already consumed the corresponding r_brace.
Daniel Jasper96964352013-12-18 10:44:36 +0000713 // Thus, we remove all ParenStates caused by fake parentheses that end
Daniel Jasperf3a5d002013-09-05 10:48:50 +0000714 // at the r_brace. The net effect of this is that we don't indent
715 // relative to the l_brace, if the nested block is the last parameter of
716 // a function. For example, this formats:
717 //
718 // SomeFunction(a, [] {
719 // f(); // break
720 // });
721 //
722 // instead of:
723 // SomeFunction(a, [] {
Daniel Jasper5500f612013-11-25 11:08:59 +0000724 // f(); // break
725 // });
Daniel Jasper3ae6f5a2014-04-09 12:08:39 +0000726 //
727 // If we have already found more than one lambda introducers on this
728 // level, we opt out of this because similarity between the lambdas is
729 // more important.
Manuel Klimek819788d2014-03-18 11:22:45 +0000730 for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i) {
731 assert(State.Stack.size() > 1);
732 if (State.Stack.size() == 1) {
733 // Do not pop the last element.
734 break;
735 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000736 State.Stack.pop_back();
Manuel Klimek819788d2014-03-18 11:22:45 +0000737 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000738 // For some reason, ObjC blocks are indented like continuations.
739 NewIndent =
Daniel Jasperc13ee342014-03-27 09:43:54 +0000740 State.Stack.back().LastSpace + (Current.Type == TT_ObjCBlockLBrace
741 ? Style.ContinuationIndentWidth
742 : Style.IndentWidth);
Alexander Kornienkoe2e03872013-10-14 00:46:35 +0000743 ++NewIndentLevel;
Daniel Jasper1db6c382013-10-22 15:30:28 +0000744 BreakBeforeParameter = true;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000745 } else {
Alexander Kornienkoe2e03872013-10-14 00:46:35 +0000746 NewIndent = State.Stack.back().LastSpace;
Daniel Jasperb8f61682013-10-22 15:45:58 +0000747 if (Current.opensBlockTypeList(Style)) {
Alexander Kornienkoe2e03872013-10-14 00:46:35 +0000748 NewIndent += Style.IndentWidth;
Daniel Jasper5a611392013-12-19 21:41:37 +0000749 NewIndent = std::min(State.Column + 2, NewIndent);
Alexander Kornienkoe2e03872013-10-14 00:46:35 +0000750 ++NewIndentLevel;
Daniel Jasperb8f61682013-10-22 15:45:58 +0000751 } else {
752 NewIndent += Style.ContinuationIndentWidth;
Daniel Jasper5a611392013-12-19 21:41:37 +0000753 NewIndent = std::min(State.Column + 1, NewIndent);
Alexander Kornienkoe2e03872013-10-14 00:46:35 +0000754 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000755 }
Daniel Jasperde0328a2013-08-16 11:20:30 +0000756 const FormatToken *NextNoComment = Current.getNextNonComment();
Daniel Jasper015ed022013-09-13 09:20:45 +0000757 AvoidBinPacking = Current.BlockKind == BK_Block ||
Daniel Jasper1db6c382013-10-22 15:30:28 +0000758 Current.Type == TT_ArrayInitializerLSquare ||
Daniel Jasperb596fb22013-10-24 10:31:50 +0000759 Current.Type == TT_DictLiteral ||
Daniel Jasper883ae9d2014-04-29 15:54:14 +0000760 Style.Language == FormatStyle::LK_Proto ||
Daniel Jasperae8e0d82014-04-17 11:32:02 +0000761 !Style.BinPackParameters ||
Daniel Jasper015ed022013-09-13 09:20:45 +0000762 (NextNoComment &&
763 NextNoComment->Type == TT_DesignatedInitializerPeriod);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000764 } else {
Daniel Jasper6633ab82013-10-18 10:38:14 +0000765 NewIndent = Style.ContinuationIndentWidth +
766 std::max(State.Stack.back().LastSpace,
767 State.Stack.back().StartOfFunctionCall);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000768 AvoidBinPacking = !Style.BinPackParameters ||
769 (Style.ExperimentalAutoDetectBinPacking &&
770 (Current.PackingKind == PPK_OnePerLine ||
771 (!BinPackInconclusiveFunctions &&
772 Current.PackingKind == PPK_Inconclusive)));
Daniel Jasper1db6c382013-10-22 15:30:28 +0000773 // If this '[' opens an ObjC call, determine whether all parameters fit
774 // into one line and put one per line if they don't.
Daniel Jasper7f0c5172014-05-19 08:06:34 +0000775 if (Current.Type == TT_ObjCMethodExpr && Style.ColumnLimit != 0 &&
Daniel Jasper1db6c382013-10-22 15:30:28 +0000776 getLengthToMatchingParen(Current) + State.Column >
777 getColumnLimit(State))
778 BreakBeforeParameter = true;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000779 }
780
Daniel Jaspercc3114d2013-10-18 15:23:06 +0000781 bool NoLineBreak = State.Stack.back().NoLineBreak ||
782 (Current.Type == TT_TemplateOpener &&
783 State.Stack.back().ContainsUnwrappedBuilder);
784 State.Stack.push_back(ParenState(NewIndent, NewIndentLevel,
785 State.Stack.back().LastSpace,
786 AvoidBinPacking, NoLineBreak));
Daniel Jasper1db6c382013-10-22 15:30:28 +0000787 State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000788 }
789
Daniel Jasperde0328a2013-08-16 11:20:30 +0000790 // If we encounter a closing ), ], } or >, we can remove a level from our
791 // stacks.
Daniel Jasper96df37a2013-08-28 09:17:37 +0000792 if (State.Stack.size() > 1 &&
793 (Current.isOneOf(tok::r_paren, tok::r_square) ||
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000794 (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
Daniel Jasper96df37a2013-08-28 09:17:37 +0000795 State.NextToken->Type == TT_TemplateCloser)) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000796 State.Stack.pop_back();
Daniel Jasperde0328a2013-08-16 11:20:30 +0000797 }
798 if (Current.is(tok::r_square)) {
799 // If this ends the array subscript expr, reset the corresponding value.
800 const FormatToken *NextNonComment = Current.getNextNonComment();
801 if (NextNonComment && NextNonComment->isNot(tok::l_square))
802 State.Stack.back().StartOfArraySubscripts = 0;
803 }
804
805 // Remove scopes created by fake parenthesis.
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000806 if (Current.isNot(tok::r_brace) ||
807 (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) {
Daniel Jasperf3a5d002013-09-05 10:48:50 +0000808 // Don't remove FakeRParens attached to r_braces that surround nested blocks
809 // as they will have been removed early (see above).
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000810 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
811 unsigned VariablePos = State.Stack.back().VariablePos;
Manuel Klimek819788d2014-03-18 11:22:45 +0000812 assert(State.Stack.size() > 1);
813 if (State.Stack.size() == 1) {
814 // Do not pop the last element.
815 break;
816 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000817 State.Stack.pop_back();
818 State.Stack.back().VariablePos = VariablePos;
819 }
Daniel Jasperde0328a2013-08-16 11:20:30 +0000820 }
821
Daniel Jasper04b6a082013-12-20 06:22:01 +0000822 if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000823 State.StartOfStringLiteral = State.Column;
Daniel Jasper04b6a082013-12-20 06:22:01 +0000824 } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
825 !Current.isStringLiteral()) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000826 State.StartOfStringLiteral = 0;
827 }
828
Alexander Kornienko39856b72013-09-10 09:38:25 +0000829 State.Column += Current.ColumnWidth;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000830 State.NextToken = State.NextToken->Next;
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000831 unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000832 if (State.Column > getColumnLimit(State)) {
833 unsigned ExcessCharacters = State.Column - getColumnLimit(State);
834 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
835 }
Daniel Jasperde0328a2013-08-16 11:20:30 +0000836
Daniel Jasper01603472014-01-09 13:42:56 +0000837 if (Current.Role)
838 Current.Role->formatFromToken(State, this, DryRun);
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000839 // If the previous has a special role, let it consume tokens as appropriate.
840 // It is necessary to start at the previous token for the only implemented
841 // role (comma separated list). That way, the decision whether or not to break
842 // after the "{" is already done and both options are tried and evaluated.
843 // FIXME: This is ugly, find a better way.
844 if (Previous && Previous->Role)
Daniel Jasper01603472014-01-09 13:42:56 +0000845 Penalty += Previous->Role->formatAfterToken(State, this, DryRun);
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000846
847 return Penalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000848}
849
Alexander Kornienko917f9e02013-09-10 12:29:48 +0000850unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
851 LineState &State) {
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000852 // Break before further function parameters on all levels.
853 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
854 State.Stack[i].BreakBeforeParameter = true;
855
Alexander Kornienko39856b72013-09-10 09:38:25 +0000856 unsigned ColumnsUsed = State.Column;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000857 // We can only affect layout of the first and the last line, so the penalty
858 // for all other lines is constant, and we ignore it.
Alexander Kornienkoebb43ca2013-09-05 14:08:34 +0000859 State.Column = Current.LastLineColumnWidth;
Alexander Kornienko632abb92013-09-02 13:58:14 +0000860
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000861 if (ColumnsUsed > getColumnLimit(State))
862 return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
Alexander Kornienkod7b837e2013-08-29 17:32:57 +0000863 return 0;
864}
865
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000866static bool getRawStringLiteralPrefixPostfix(StringRef Text, StringRef &Prefix,
Alexander Kornienko81e32942013-09-16 20:20:49 +0000867 StringRef &Postfix) {
868 if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") ||
869 Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") ||
870 Text.startswith(Prefix = "LR\"")) {
871 size_t ParenPos = Text.find('(');
872 if (ParenPos != StringRef::npos) {
873 StringRef Delimiter =
874 Text.substr(Prefix.size(), ParenPos - Prefix.size());
875 Prefix = Text.substr(0, ParenPos + 1);
876 Postfix = Text.substr(Text.size() - 2 - Delimiter.size());
877 return Postfix.front() == ')' && Postfix.back() == '"' &&
878 Postfix.substr(1).startswith(Delimiter);
879 }
880 }
881 return false;
882}
883
Daniel Jasperde0328a2013-08-16 11:20:30 +0000884unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
885 LineState &State,
886 bool DryRun) {
Alexander Kornienko917f9e02013-09-10 12:29:48 +0000887 // Don't break multi-line tokens other than block comments. Instead, just
888 // update the state.
889 if (Current.Type != TT_BlockComment && Current.IsMultiline)
890 return addMultilineToken(Current, State);
891
Daniel Jasper98857842013-10-30 13:54:53 +0000892 // Don't break implicit string literals.
893 if (Current.Type == TT_ImplicitStringLiteral)
894 return 0;
895
Daniel Jasper04b6a082013-12-20 06:22:01 +0000896 if (!Current.isStringLiteral() && !Current.is(tok::comment))
Daniel Jasperf93551c2013-08-23 10:05:49 +0000897 return 0;
898
Ahmed Charlesb8984322014-03-07 20:03:18 +0000899 std::unique_ptr<BreakableToken> Token;
Alexander Kornienko39856b72013-09-10 09:38:25 +0000900 unsigned StartColumn = State.Column - Current.ColumnWidth;
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +0000901 unsigned ColumnLimit = getColumnLimit(State);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000902
Daniel Jasper04b6a082013-12-20 06:22:01 +0000903 if (Current.isStringLiteral()) {
Alexander Kornienko384b40b2013-10-11 21:43:05 +0000904 // Don't break string literals inside preprocessor directives (except for
905 // #define directives, as their contents are stored in separate lines and
906 // are not affected by this check).
907 // This way we avoid breaking code with line directives and unknown
908 // preprocessor directives that contain long string literals.
909 if (State.Line->Type == LT_PreprocessorDirective)
910 return 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000911 // Exempts unterminated string literals from line breaking. The user will
912 // likely want to terminate the string before any line breaking is done.
913 if (Current.IsUnterminatedLiteral)
914 return 0;
915
Alexander Kornienko81e32942013-09-16 20:20:49 +0000916 StringRef Text = Current.TokenText;
917 StringRef Prefix;
918 StringRef Postfix;
Daniel Jasper174b0122014-01-09 14:18:12 +0000919 bool IsNSStringLiteral = false;
Alexander Kornienko81e32942013-09-16 20:20:49 +0000920 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
921 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
922 // reduce the overhead) for each FormatToken, which is a string, so that we
923 // don't run multiple checks here on the hot path.
Daniel Jasper174b0122014-01-09 14:18:12 +0000924 if (Text.startswith("\"") && Current.Previous &&
925 Current.Previous->is(tok::at)) {
926 IsNSStringLiteral = true;
927 Prefix = "@\"";
Daniel Jasper174b0122014-01-09 14:18:12 +0000928 }
Alexander Kornienko81e32942013-09-16 20:20:49 +0000929 if ((Text.endswith(Postfix = "\"") &&
Daniel Jasper174b0122014-01-09 14:18:12 +0000930 (IsNSStringLiteral || Text.startswith(Prefix = "\"") ||
931 Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") ||
932 Text.startswith(Prefix = "u8\"") ||
Alexander Kornienko81e32942013-09-16 20:20:49 +0000933 Text.startswith(Prefix = "L\""))) ||
934 (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) ||
935 getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) {
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000936 Token.reset(new BreakableStringLiteral(
937 Current, State.Line->Level, StartColumn, Prefix, Postfix,
938 State.Line->InPPDirective, Encoding, Style));
Alexander Kornienko81e32942013-09-16 20:20:49 +0000939 } else {
940 return 0;
941 }
Daniel Jasperde0328a2013-08-16 11:20:30 +0000942 } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
Alexander Kornienkoce9161a2014-01-02 15:13:14 +0000943 if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
944 return 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000945 Token.reset(new BreakableBlockComment(
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000946 Current, State.Line->Level, StartColumn, Current.OriginalColumn,
947 !Current.Previous, State.Line->InPPDirective, Encoding, Style));
Daniel Jasperde0328a2013-08-16 11:20:30 +0000948 } else if (Current.Type == TT_LineComment &&
Craig Topper2145bc02014-05-09 08:15:10 +0000949 (Current.Previous == nullptr ||
Daniel Jasperde0328a2013-08-16 11:20:30 +0000950 Current.Previous->Type != TT_ImplicitStringLiteral)) {
Alexander Kornienkoce9161a2014-01-02 15:13:14 +0000951 if (CommentPragmasRegex.match(Current.TokenText.substr(2)))
952 return 0;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000953 Token.reset(new BreakableLineComment(Current, State.Line->Level,
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +0000954 StartColumn, /*InPPDirective=*/false,
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000955 Encoding, Style));
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +0000956 // We don't insert backslashes when breaking line comments.
957 ColumnLimit = Style.ColumnLimit;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000958 } else {
959 return 0;
960 }
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +0000961 if (Current.UnbreakableTailLength >= ColumnLimit)
Daniel Jasperde0328a2013-08-16 11:20:30 +0000962 return 0;
963
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +0000964 unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000965 bool BreakInserted = false;
966 unsigned Penalty = 0;
967 unsigned RemainingTokenColumns = 0;
968 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
969 LineIndex != EndIndex; ++LineIndex) {
970 if (!DryRun)
971 Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
972 unsigned TailOffset = 0;
973 RemainingTokenColumns =
974 Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
975 while (RemainingTokenColumns > RemainingSpace) {
976 BreakableToken::Split Split =
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +0000977 Token->getSplit(LineIndex, TailOffset, ColumnLimit);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000978 if (Split.first == StringRef::npos) {
979 // The last line's penalty is handled in addNextStateToQueue().
980 if (LineIndex < EndIndex - 1)
981 Penalty += Style.PenaltyExcessCharacter *
982 (RemainingTokenColumns - RemainingSpace);
983 break;
984 }
985 assert(Split.first != 0);
986 unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
987 LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
Alexander Kornienko875395f2013-11-12 17:50:13 +0000988
989 // We can remove extra whitespace instead of breaking the line.
990 if (RemainingTokenColumns + 1 - Split.second <= RemainingSpace) {
991 RemainingTokenColumns = 0;
992 if (!DryRun)
993 Token->replaceWhitespace(LineIndex, TailOffset, Split, Whitespaces);
994 break;
995 }
996
Alexander Kornienko64a42b82014-04-15 14:52:43 +0000997 // When breaking before a tab character, it may be moved by a few columns,
998 // but will still be expanded to the next tab stop, so we don't save any
999 // columns.
1000 if (NewRemainingTokenColumns == RemainingTokenColumns)
1001 break;
1002
Daniel Jasperde0328a2013-08-16 11:20:30 +00001003 assert(NewRemainingTokenColumns < RemainingTokenColumns);
1004 if (!DryRun)
1005 Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
Daniel Jasper2739af32013-08-28 10:03:58 +00001006 Penalty += Current.SplitPenalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +00001007 unsigned ColumnsUsed =
1008 Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
Alexander Kornienko3abbb8a2013-11-12 17:30:49 +00001009 if (ColumnsUsed > ColumnLimit) {
1010 Penalty += Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit);
Daniel Jasperde0328a2013-08-16 11:20:30 +00001011 }
1012 TailOffset += Split.first + Split.second;
1013 RemainingTokenColumns = NewRemainingTokenColumns;
1014 BreakInserted = true;
1015 }
1016 }
1017
1018 State.Column = RemainingTokenColumns;
1019
1020 if (BreakInserted) {
1021 // If we break the token inside a parameter list, we need to break before
1022 // the next parameter on all levels, so that the next parameter is clearly
1023 // visible. Line comments already introduce a break.
1024 if (Current.Type != TT_LineComment) {
1025 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
1026 State.Stack[i].BreakBeforeParameter = true;
1027 }
1028
Daniel Jasper04b6a082013-12-20 06:22:01 +00001029 Penalty += Current.isStringLiteral() ? Style.PenaltyBreakString
1030 : Style.PenaltyBreakComment;
Daniel Jasper2739af32013-08-28 10:03:58 +00001031
Daniel Jasperde0328a2013-08-16 11:20:30 +00001032 State.Stack.back().LastSpace = StartColumn;
1033 }
1034 return Penalty;
1035}
1036
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001037unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
Daniel Jasperde0328a2013-08-16 11:20:30 +00001038 // In preprocessor directives reserve two chars for trailing " \"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001039 return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
Daniel Jasperde0328a2013-08-16 11:20:30 +00001040}
1041
Daniel Jasperc39b56f2013-12-16 07:23:08 +00001042bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
Daniel Jasperf438cb72013-08-23 11:57:34 +00001043 const FormatToken &Current = *State.NextToken;
Daniel Jasper9509f182014-05-05 08:08:07 +00001044 if (!Current.isStringLiteral() || Current.Type == TT_ImplicitStringLiteral)
Daniel Jasperf438cb72013-08-23 11:57:34 +00001045 return false;
Alexander Kornienkod7b837e2013-08-29 17:32:57 +00001046 // We never consider raw string literals "multiline" for the purpose of
Daniel Jasperc39b56f2013-12-16 07:23:08 +00001047 // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
1048 // (see TokenAnnotator::mustBreakBefore().
Alexander Kornienkod7b837e2013-08-29 17:32:57 +00001049 if (Current.TokenText.startswith("R\""))
1050 return false;
Alexander Kornienko39856b72013-09-10 09:38:25 +00001051 if (Current.IsMultiline)
Alexander Kornienkod7b837e2013-08-29 17:32:57 +00001052 return true;
Daniel Jasperf438cb72013-08-23 11:57:34 +00001053 if (Current.getNextNonComment() &&
Daniel Jasper04b6a082013-12-20 06:22:01 +00001054 Current.getNextNonComment()->isStringLiteral())
Daniel Jasperf438cb72013-08-23 11:57:34 +00001055 return true; // Implicit concatenation.
Alexander Kornienko39856b72013-09-10 09:38:25 +00001056 if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
Daniel Jasperf438cb72013-08-23 11:57:34 +00001057 Style.ColumnLimit)
1058 return true; // String will be split.
Alexander Kornienkod7b837e2013-08-29 17:32:57 +00001059 return false;
Daniel Jasperf438cb72013-08-23 11:57:34 +00001060}
1061
Daniel Jasperde0328a2013-08-16 11:20:30 +00001062} // namespace format
1063} // namespace clang