blob: 1e10ce34924731414ba681624c358a9edc9833e9 [file] [log] [blame]
Daniel Jasper6b2afe42013-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
15#define DEBUG_TYPE "format-formatter"
16
17#include "BreakableToken.h"
18#include "ContinuationIndenter.h"
19#include "WhitespaceManager.h"
20#include "clang/Basic/OperatorPrecedence.h"
21#include "clang/Basic/SourceManager.h"
22#include "clang/Format/Format.h"
23#include "llvm/Support/Debug.h"
24#include <string>
25
26namespace 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) {
32 if (Tok.MatchingParen == NULL)
33 return 0;
34 FormatToken *End = Tok.MatchingParen;
35 while (End->Next && !End->Next->CanBreakBefore) {
36 End = End->Next;
37 }
38 return End->TotalLength - Tok.TotalLength + 1;
39}
40
Daniel Jasperd3fef0f2013-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 Jasper19ccb122013-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 Jasper6b2afe42013-08-16 11:20:30 +000059ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
60 SourceManager &SourceMgr,
Daniel Jasper6b2afe42013-08-16 11:20:30 +000061 WhitespaceManager &Whitespaces,
62 encoding::Encoding Encoding,
63 bool BinPackInconclusiveFunctions)
Daniel Jasper567dcf92013-09-05 09:29:45 +000064 : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces),
65 Encoding(Encoding),
Daniel Jasper6b2afe42013-08-16 11:20:30 +000066 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {}
67
Daniel Jasper567dcf92013-09-05 09:29:45 +000068LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
Daniel Jasperb77d7412013-09-06 07:54:20 +000069 const AnnotatedLine *Line,
70 bool DryRun) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +000071 LineState State;
Daniel Jasper567dcf92013-09-05 09:29:45 +000072 State.FirstIndent = FirstIndent;
Daniel Jasper6b2afe42013-08-16 11:20:30 +000073 State.Column = FirstIndent;
Daniel Jasper567dcf92013-09-05 09:29:45 +000074 State.Line = Line;
75 State.NextToken = Line->First;
Alexander Kornienkof0fc89c2013-10-14 00:46:35 +000076 State.Stack.push_back(ParenState(FirstIndent, Line->Level, FirstIndent,
Daniel Jasper6b2afe42013-08-16 11:20:30 +000077 /*AvoidBinPacking=*/false,
78 /*NoLineBreak=*/false));
79 State.LineContainsContinuedForLoopSection = false;
80 State.ParenLevel = 0;
81 State.StartOfStringLiteral = 0;
82 State.StartOfLineLevel = State.ParenLevel;
83 State.LowestLevelOnLine = State.ParenLevel;
84 State.IgnoreStackForComparison = false;
85
86 // The first token has already been indented and thus consumed.
Daniel Jasperb77d7412013-09-06 07:54:20 +000087 moveStateToNextToken(State, DryRun, /*Newline=*/false);
Daniel Jasper6b2afe42013-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 Jaspera07aa662013-10-22 15:30:28 +000095 if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace &&
96 Current.closesBlockTypeList(Style)))
Daniel Jasper6b2afe42013-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 Jasper3c6aea72013-10-24 10:31:50 +0000101 Previous.Type != TT_DictLiteral &&
Daniel Jasper567dcf92013-09-05 09:29:45 +0000102 Previous.BlockKind == BK_BracedInit && Previous.Previous &&
Daniel Jasper6b2afe42013-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.
110 if (Previous.opensScope() && State.LowestLevelOnLine < State.StartOfLineLevel)
111 return false;
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000112 if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
113 return false;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000114 return !State.Stack.back().NoLineBreak;
115}
116
117bool ContinuationIndenter::mustBreak(const LineState &State) {
118 const FormatToken &Current = *State.NextToken;
119 const FormatToken &Previous = *Current.Previous;
120 if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
121 return true;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000122 if (State.Stack.back().BreakBeforeClosingBrace &&
123 Current.closesBlockTypeList(Style))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000124 return true;
125 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
126 return true;
Daniel Jasper19ccb122013-10-08 05:11:18 +0000127 if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
Daniel Jasper1a896a52013-11-08 00:57:11 +0000128 (Style.BreakBeforeTernaryOperators &&
129 (Current.is(tok::question) || (Current.Type == TT_ConditionalExpr &&
130 Previous.isNot(tok::question)))) ||
131 (!Style.BreakBeforeTernaryOperators &&
132 (Previous.is(tok::question) || Previous.Type == TT_ConditionalExpr))) &&
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000133 State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
134 !Current.isOneOf(tok::r_paren, tok::r_brace))
135 return true;
136 if (Style.AlwaysBreakBeforeMultilineStrings &&
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000137 State.Column > State.Stack.back().Indent && // Breaking saves columns.
Daniel Jaspera7856d02013-11-09 03:08:25 +0000138 !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) &&
Daniel Jasper49c77b22013-10-18 16:47:55 +0000139 Previous.Type != TT_InlineASMColon && NextIsMultilineString(State))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000140 return true;
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000141 if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) ||
Daniel Jaspera07aa662013-10-22 15:30:28 +0000142 Previous.Type == TT_ArrayInitializerLSquare) &&
Daniel Jaspera53bbae2013-10-20 16:45:46 +0000143 getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State))
144 return true;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000145
146 if (!Style.BreakBeforeBinaryOperators) {
147 // If we need to break somewhere inside the LHS of a binary expression, we
148 // should also break after the operator. Otherwise, the formatting would
149 // hide the operator precedence, e.g. in:
150 // if (aaaaaaaaaaaaaa ==
151 // bbbbbbbbbbbbbb && c) {..
152 // For comparisons, we only apply this rule, if the LHS is a binary
153 // expression itself as otherwise, the line breaks seem superfluous.
154 // We need special cases for ">>" which we have split into two ">" while
155 // lexing in order to make template parsing easier.
156 //
157 // FIXME: We'll need something similar for styles that break before binary
158 // operators.
159 bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
160 Previous.getPrecedence() == prec::Equality) &&
161 Previous.Previous &&
162 Previous.Previous->Type != TT_BinaryOperator; // For >>.
163 bool LHSIsBinaryExpr =
Daniel Jasperdb4813a2013-09-06 08:08:14 +0000164 Previous.Previous && Previous.Previous->EndsBinaryExpression;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000165 if (Previous.Type == TT_BinaryOperator &&
166 (!IsComparison || LHSIsBinaryExpr) &&
167 Current.Type != TT_BinaryOperator && // For >>.
168 !Current.isTrailingComment() &&
169 !Previous.isOneOf(tok::lessless, tok::question) &&
170 Previous.getPrecedence() != prec::Assignment &&
171 State.Stack.back().BreakBeforeParameter)
172 return true;
173 }
174
175 // Same as above, but for the first "<<" operator.
176 if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter &&
177 State.Stack.back().FirstLessLess == 0)
178 return true;
179
180 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
181 // out whether it is the first parameter. Clean this up.
182 if (Current.Type == TT_ObjCSelectorName &&
183 Current.LongestObjCSelectorName == 0 &&
184 State.Stack.back().BreakBeforeParameter)
185 return true;
186 if ((Current.Type == TT_CtorInitializerColon ||
Daniel Jasper6e7f1932013-10-09 15:06:17 +0000187 (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0 &&
188 !Current.isTrailingComment())))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000189 return true;
190
191 if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
Daniel Jasper567dcf92013-09-05 09:29:45 +0000192 State.Line->MightBeFunctionDecl &&
193 State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0)
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000194 return true;
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000195 if (startsSegmentOfBuilderTypeCall(Current) &&
Daniel Jaspereb331832013-08-30 07:12:40 +0000196 (State.Stack.back().CallContinuation != 0 ||
197 (State.Stack.back().BreakBeforeParameter &&
198 State.Stack.back().ContainsUnwrappedBuilder)))
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000199 return true;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000200 return false;
201}
202
203unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000204 bool DryRun,
205 unsigned ExtraSpaces) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000206 const FormatToken &Current = *State.NextToken;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000207
Daniel Jasper84379572013-10-30 13:54:53 +0000208 if (State.Stack.size() == 0 ||
209 (Current.Type == TT_ImplicitStringLiteral &&
210 (Current.Previous->Tok.getIdentifierInfo() == NULL ||
211 Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
212 tok::pp_not_keyword))) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000213 // FIXME: Is this correct?
214 int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
215 State.NextToken->WhitespaceRange.getEnd()) -
216 SourceMgr.getSpellingColumnNumber(
217 State.NextToken->WhitespaceRange.getBegin());
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000218 State.Column += WhitespaceLength + State.NextToken->ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000219 State.NextToken = State.NextToken->Next;
220 return 0;
221 }
222
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000223 unsigned Penalty = 0;
224 if (Newline)
225 Penalty = addTokenOnNewLine(State, DryRun);
226 else
227 addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
228
229 return moveStateToNextToken(State, DryRun, Newline) + Penalty;
230}
231
232void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
233 unsigned ExtraSpaces) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000234 FormatToken &Current = *State.NextToken;
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000235 const FormatToken &Previous = *State.NextToken->Previous;
236 if (Current.is(tok::equal) &&
237 (State.Line->First->is(tok::kw_for) || State.ParenLevel == 0) &&
238 State.Stack.back().VariablePos == 0) {
239 State.Stack.back().VariablePos = State.Column;
240 // Move over * and & if they are bound to the variable name.
241 const FormatToken *Tok = &Previous;
242 while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
243 State.Stack.back().VariablePos -= Tok->ColumnWidth;
244 if (Tok->SpacesRequiredBefore != 0)
245 break;
246 Tok = Tok->Previous;
247 }
248 if (Previous.PartOfMultiVariableDeclStmt)
249 State.Stack.back().LastSpace = State.Stack.back().VariablePos;
250 }
251
252 unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
253
254 if (!DryRun)
255 Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0,
256 Spaces, State.Column + Spaces);
257
258 if (Current.Type == TT_ObjCSelectorName && State.Stack.back().ColonPos == 0) {
259 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
260 State.Column + Spaces + Current.ColumnWidth)
261 State.Stack.back().ColonPos =
262 State.Stack.back().Indent + Current.LongestObjCSelectorName;
263 else
264 State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth;
265 }
266
267 if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
268 Current.Type != TT_LineComment)
269 State.Stack.back().Indent = State.Column + Spaces;
Daniel Jasper19ccb122013-10-08 05:11:18 +0000270 if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000271 State.Stack.back().NoLineBreak = true;
272 if (startsSegmentOfBuilderTypeCall(Current))
273 State.Stack.back().ContainsUnwrappedBuilder = true;
274
275 State.Column += Spaces;
276 if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
277 // Treat the condition inside an if as if it was a second function
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000278 // parameter, i.e. let nested calls have a continuation indent.
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000279 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
Daniel Jasperdeb61c52013-11-08 17:33:24 +0000280 else if (Previous.is(tok::comma) || Previous.Type == TT_ObjCMethodExpr)
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000281 State.Stack.back().LastSpace = State.Column;
282 else if ((Previous.Type == TT_BinaryOperator ||
283 Previous.Type == TT_ConditionalExpr ||
284 Previous.Type == TT_UnaryOperator ||
285 Previous.Type == TT_CtorInitializerColon) &&
286 (Previous.getPrecedence() != prec::Assignment ||
287 Current.StartsBinaryExpression))
288 // Always indent relative to the RHS of the expression unless this is a
289 // simple assignment without binary expression on the RHS. Also indent
290 // relative to unary operators and the colons of constructor initializers.
291 State.Stack.back().LastSpace = State.Column;
Daniel Jaspercea014b2013-10-08 16:24:07 +0000292 else if (Previous.Type == TT_InheritanceColon) {
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000293 State.Stack.back().Indent = State.Column;
Daniel Jaspercea014b2013-10-08 16:24:07 +0000294 State.Stack.back().LastSpace = State.Column;
295 } else if (Previous.opensScope()) {
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000296 // If a function has a trailing call, indent all parameters from the
297 // opening parenthesis. This avoids confusing indents like:
298 // OuterFunction(InnerFunctionCall( // break
299 // ParameterToInnerFunction)) // break
300 // .SecondInnerFunctionCall();
301 bool HasTrailingCall = false;
302 if (Previous.MatchingParen) {
303 const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
304 HasTrailingCall = Next && Next->isMemberAccess();
305 }
306 if (HasTrailingCall &&
307 State.Stack[State.Stack.size() - 2].CallContinuation == 0)
308 State.Stack.back().LastSpace = State.Column;
309 }
310}
311
312unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
313 bool DryRun) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000314 FormatToken &Current = *State.NextToken;
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000315 const FormatToken &Previous = *State.NextToken->Previous;
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000316 // If we are continuing an expression, we want to use the continuation indent.
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000317 unsigned ContinuationIndent =
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000318 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
319 Style.ContinuationIndentWidth;
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000320 // Extra penalty that needs to be added because of the way certain line
321 // breaks are chosen.
322 unsigned Penalty = 0;
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000323
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000324 const FormatToken *PreviousNonComment =
325 State.NextToken->getPreviousNonComment();
326 // The first line break on any ParenLevel causes an extra penalty in order
327 // prefer similar line breaks.
328 if (!State.Stack.back().ContainsLineBreak)
329 Penalty += 15;
330 State.Stack.back().ContainsLineBreak = true;
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000331
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000332 Penalty += State.NextToken->SplitPenalty;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000333
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000334 // Breaking before the first "<<" is generally not desirable if the LHS is
335 // short.
336 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
337 State.Column <= Style.ColumnLimit / 2)
338 Penalty += Style.PenaltyBreakFirstLessLess;
339
340 if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {
341 State.Column = State.FirstIndent;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000342 } else if (Current.isOneOf(tok::r_brace, tok::r_square)) {
Daniel Jasper16a8b0e2013-11-07 14:02:28 +0000343 if (Current.closesBlockTypeList(Style) ||
344 (Current.MatchingParen &&
345 Current.MatchingParen->BlockKind == BK_BracedInit))
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000346 State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
347 else
Daniel Jasper57981202013-09-13 09:20:45 +0000348 State.Column = State.FirstIndent;
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000349 } else if (Current.is(tok::string_literal) &&
350 State.StartOfStringLiteral != 0) {
351 State.Column = State.StartOfStringLiteral;
352 State.Stack.back().BreakBeforeParameter = true;
353 } else if (Current.is(tok::lessless) &&
354 State.Stack.back().FirstLessLess != 0) {
355 State.Column = State.Stack.back().FirstLessLess;
356 } else if (Current.isMemberAccess()) {
357 if (State.Stack.back().CallContinuation == 0) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000358 State.Column = ContinuationIndent;
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000359 State.Stack.back().CallContinuation = State.Column;
360 } else {
361 State.Column = State.Stack.back().CallContinuation;
362 }
Daniel Jasper1a896a52013-11-08 00:57:11 +0000363 } else if (State.Stack.back().QuestionColumn != 0 &&
364 (Current.Type == TT_ConditionalExpr ||
365 Previous.Type == TT_ConditionalExpr)) {
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000366 State.Column = State.Stack.back().QuestionColumn;
367 } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) {
368 State.Column = State.Stack.back().VariablePos;
369 } else if ((PreviousNonComment &&
370 PreviousNonComment->ClosesTemplateDeclaration) ||
371 ((Current.Type == TT_StartOfName ||
372 Current.is(tok::kw_operator)) &&
373 State.ParenLevel == 0 &&
374 (!Style.IndentFunctionDeclarationAfterType ||
375 State.Line->StartsDefinition))) {
376 State.Column = State.Stack.back().Indent;
377 } else if (Current.Type == TT_ObjCSelectorName) {
Daniel Jasper072ac6c2013-11-08 02:08:01 +0000378 if (State.Stack.back().ColonPos == 0) {
379 State.Stack.back().ColonPos =
380 State.Stack.back().Indent + Current.LongestObjCSelectorName;
381 State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
382 } else if (State.Stack.back().ColonPos > Current.ColumnWidth) {
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000383 State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000384 } else {
385 State.Column = State.Stack.back().Indent;
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000386 State.Stack.back().ColonPos = State.Column + Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000387 }
Daniel Jaspera07aa662013-10-22 15:30:28 +0000388 } else if (Current.Type == TT_ArraySubscriptLSquare) {
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000389 if (State.Stack.back().StartOfArraySubscripts != 0)
390 State.Column = State.Stack.back().StartOfArraySubscripts;
391 else
392 State.Column = ContinuationIndent;
393 } else if (Current.Type == TT_StartOfName ||
394 Previous.isOneOf(tok::coloncolon, tok::equal) ||
395 Previous.Type == TT_ObjCMethodExpr) {
396 State.Column = ContinuationIndent;
397 } else if (Current.Type == TT_CtorInitializerColon) {
398 State.Column = State.FirstIndent + Style.ConstructorInitializerIndentWidth;
399 } else if (Current.Type == TT_CtorInitializerComma) {
400 State.Column = State.Stack.back().Indent;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000401 } else {
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000402 State.Column = State.Stack.back().Indent;
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000403 // Ensure that we fall back to the continuation indent width instead of just
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000404 // flushing continuations left.
Daniel Jasperdc837b12013-10-30 14:04:10 +0000405 if (State.Column == State.FirstIndent &&
406 PreviousNonComment->isNot(tok::r_brace))
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000407 State.Column += Style.ContinuationIndentWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000408 }
409
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000410 if ((Previous.isOneOf(tok::comma, tok::semi) &&
411 !State.Stack.back().AvoidBinPacking) ||
412 Previous.Type == TT_BinaryOperator)
413 State.Stack.back().BreakBeforeParameter = false;
414 if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
415 State.Stack.back().BreakBeforeParameter = false;
Daniel Jasper1a896a52013-11-08 00:57:11 +0000416 if (Current.is(tok::question) ||
417 (PreviousNonComment && PreviousNonComment->is(tok::question)))
418 State.Stack.back().BreakBeforeParameter = true;
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000419
420 if (!DryRun) {
421 unsigned Newlines = 1;
422 if (Current.is(tok::comment))
423 Newlines = std::max(Newlines, std::min(Current.NewlinesBefore,
424 Style.MaxEmptyLinesToKeep + 1));
Alexander Kornienkof0fc89c2013-10-14 00:46:35 +0000425 Whitespaces.replaceWhitespace(Current, Newlines,
426 State.Stack.back().IndentLevel, State.Column,
427 State.Column, State.Line->InPPDirective);
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000428 }
429
430 if (!Current.isTrailingComment())
431 State.Stack.back().LastSpace = State.Column;
432 if (Current.isMemberAccess())
433 State.Stack.back().LastSpace += Current.ColumnWidth;
434 State.StartOfLineLevel = State.ParenLevel;
435 State.LowestLevelOnLine = State.ParenLevel;
436
437 // Any break on this level means that the parent level has been broken
438 // and we need to avoid bin packing there.
439 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
440 State.Stack[i].BreakBeforeParameter = true;
441 }
Daniel Jasper8b156e22013-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 &&
446 Current.Type != TT_BinaryOperator &&
447 !PreviousNonComment->opensScope())
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000448 State.Stack.back().BreakBeforeParameter = true;
449
Daniel Jaspera07aa662013-10-22 15:30:28 +0000450 // If we break after { or the [ of an array initializer, we should also break
451 // before the corresponding } or ].
452 if (Previous.is(tok::l_brace) || Previous.Type == TT_ArrayInitializerLSquare)
Alexander Kornienkoe5321c42013-10-01 14:41:18 +0000453 State.Stack.back().BreakBeforeClosingBrace = true;
454
455 if (State.Stack.back().AvoidBinPacking) {
456 // If we are breaking after '(', '{', '<', this is not bin packing
457 // unless AllowAllParametersOfDeclarationOnNextLine is false.
458 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
459 Previous.Type == TT_BinaryOperator) ||
460 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
461 State.Line->MustBeDeclaration))
462 State.Stack.back().BreakBeforeParameter = true;
463 }
464
465 return Penalty;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000466}
467
468unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
469 bool DryRun, bool Newline) {
470 const FormatToken &Current = *State.NextToken;
471 assert(State.Stack.size());
472
473 if (Current.Type == TT_InheritanceColon)
474 State.Stack.back().AvoidBinPacking = true;
475 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
476 State.Stack.back().FirstLessLess = State.Column;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000477 if (Current.Type == TT_ArraySubscriptLSquare &&
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000478 State.Stack.back().StartOfArraySubscripts == 0)
479 State.Stack.back().StartOfArraySubscripts = State.Column;
Daniel Jasper1a896a52013-11-08 00:57:11 +0000480 if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) ||
481 (Current.getPreviousNonComment() && Current.isNot(tok::colon) &&
482 Current.getPreviousNonComment()->is(tok::question) &&
483 !Style.BreakBeforeTernaryOperators))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000484 State.Stack.back().QuestionColumn = State.Column;
485 if (!Current.opensScope() && !Current.closesScope())
486 State.LowestLevelOnLine =
487 std::min(State.LowestLevelOnLine, State.ParenLevel);
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000488 if (Current.isMemberAccess())
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000489 State.Stack.back().StartOfFunctionCall =
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000490 Current.LastInChainOfCalls ? 0 : State.Column + Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000491 if (Current.Type == TT_CtorInitializerColon) {
492 // Indent 2 from the column, so:
493 // SomeClass::SomeClass()
494 // : First(...), ...
495 // Next(...)
496 // ^ line up here.
497 State.Stack.back().Indent =
498 State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
499 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
500 State.Stack.back().AvoidBinPacking = true;
501 State.Stack.back().BreakBeforeParameter = false;
502 }
503
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000504 // In ObjC method declaration we align on the ":" of parameters, but we need
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000505 // to ensure that we indent parameters on subsequent lines by at least our
506 // continuation indent width.
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000507 if (Current.Type == TT_ObjCMethodSpecifier)
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000508 State.Stack.back().Indent += Style.ContinuationIndentWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000509
510 // Insert scopes created by fake parenthesis.
511 const FormatToken *Previous = Current.getPreviousNonComment();
512 // Don't add extra indentation for the first fake parenthesis after
513 // 'return', assignements or opening <({[. The indentation for these cases
514 // is special cased.
515 bool SkipFirstExtraIndent =
Daniel Jasperf78bf4a2013-09-30 08:29:03 +0000516 (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) ||
Daniel Jasper966e6d32013-11-07 19:23:49 +0000517 Previous->getPrecedence() == prec::Assignment ||
518 Previous->Type == TT_ObjCMethodExpr));
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000519 for (SmallVectorImpl<prec::Level>::const_reverse_iterator
520 I = Current.FakeLParens.rbegin(),
521 E = Current.FakeLParens.rend();
522 I != E; ++I) {
523 ParenState NewParenState = State.Stack.back();
524 NewParenState.ContainsLineBreak = false;
Daniel Jasperf78bf4a2013-09-30 08:29:03 +0000525
526 // Indent from 'LastSpace' unless this the fake parentheses encapsulating a
527 // builder type call after 'return'. If such a call is line-wrapped, we
528 // commonly just want to indent from the start of the line.
529 if (!Previous || Previous->isNot(tok::kw_return) || *I > 0)
530 NewParenState.Indent =
531 std::max(std::max(State.Column, NewParenState.Indent),
532 State.Stack.back().LastSpace);
533
Daniel Jasper567dcf92013-09-05 09:29:45 +0000534 // Do not indent relative to the fake parentheses inserted for "." or "->".
535 // This is a special case to make the following to statements consistent:
536 // OuterFunction(InnerFunctionCall( // break
537 // ParameterToInnerFunction));
538 // OuterFunction(SomeObject.InnerFunctionCall( // break
539 // ParameterToInnerFunction));
540 if (*I > prec::Unknown)
541 NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000542
543 // Always indent conditional expressions. Never indent expression where
544 // the 'operator' is ',', ';' or an assignment (i.e. *I <=
545 // prec::Assignment) as those have different indentation rules. Indent
546 // other expression, unless the indentation needs to be skipped.
547 if (*I == prec::Conditional ||
548 (!SkipFirstExtraIndent && *I > prec::Assignment &&
549 !Style.BreakBeforeBinaryOperators))
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000550 NewParenState.Indent += Style.ContinuationIndentWidth;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000551 if ((Previous && !Previous->opensScope()) || *I > prec::Comma)
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000552 NewParenState.BreakBeforeParameter = false;
553 State.Stack.push_back(NewParenState);
554 SkipFirstExtraIndent = false;
555 }
556
557 // If we encounter an opening (, [, { or <, we add a level to our stacks to
558 // prepare for the following tokens.
559 if (Current.opensScope()) {
560 unsigned NewIndent;
Alexander Kornienkof0fc89c2013-10-14 00:46:35 +0000561 unsigned NewIndentLevel = State.Stack.back().IndentLevel;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000562 bool AvoidBinPacking;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000563 bool BreakBeforeParameter = false;
564 if (Current.is(tok::l_brace) ||
565 Current.Type == TT_ArrayInitializerLSquare) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000566 if (Current.MatchingParen && Current.BlockKind == BK_Block) {
Daniel Jasper1a925bc2013-09-05 10:48:50 +0000567 // If this is an l_brace starting a nested block, we pretend (wrt. to
568 // indentation) that we already consumed the corresponding r_brace.
569 // Thus, we remove all ParenStates caused bake fake parentheses that end
570 // at the r_brace. The net effect of this is that we don't indent
571 // relative to the l_brace, if the nested block is the last parameter of
572 // a function. For example, this formats:
573 //
574 // SomeFunction(a, [] {
575 // f(); // break
576 // });
577 //
578 // instead of:
579 // SomeFunction(a, [] {
580 // f(); // break
581 // });
Daniel Jasper567dcf92013-09-05 09:29:45 +0000582 for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i)
583 State.Stack.pop_back();
Daniel Jasper57981202013-09-13 09:20:45 +0000584 NewIndent = State.Stack.back().LastSpace + Style.IndentWidth;
Alexander Kornienkof0fc89c2013-10-14 00:46:35 +0000585 ++NewIndentLevel;
Daniel Jaspera07aa662013-10-22 15:30:28 +0000586 BreakBeforeParameter = true;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000587 } else {
Alexander Kornienkof0fc89c2013-10-14 00:46:35 +0000588 NewIndent = State.Stack.back().LastSpace;
Daniel Jasperc9689432013-10-22 15:45:58 +0000589 if (Current.opensBlockTypeList(Style)) {
Alexander Kornienkof0fc89c2013-10-14 00:46:35 +0000590 NewIndent += Style.IndentWidth;
591 ++NewIndentLevel;
Daniel Jasperc9689432013-10-22 15:45:58 +0000592 } else {
593 NewIndent += Style.ContinuationIndentWidth;
Alexander Kornienkof0fc89c2013-10-14 00:46:35 +0000594 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000595 }
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000596 const FormatToken *NextNoComment = Current.getNextNonComment();
Daniel Jasper57981202013-09-13 09:20:45 +0000597 AvoidBinPacking = Current.BlockKind == BK_Block ||
Daniel Jaspera07aa662013-10-22 15:30:28 +0000598 Current.Type == TT_ArrayInitializerLSquare ||
Daniel Jasper3c6aea72013-10-24 10:31:50 +0000599 Current.Type == TT_DictLiteral ||
Daniel Jasper57981202013-09-13 09:20:45 +0000600 (NextNoComment &&
601 NextNoComment->Type == TT_DesignatedInitializerPeriod);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000602 } else {
Daniel Jasperc2827ec2013-10-18 10:38:14 +0000603 NewIndent = Style.ContinuationIndentWidth +
604 std::max(State.Stack.back().LastSpace,
605 State.Stack.back().StartOfFunctionCall);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000606 AvoidBinPacking = !Style.BinPackParameters ||
607 (Style.ExperimentalAutoDetectBinPacking &&
608 (Current.PackingKind == PPK_OnePerLine ||
609 (!BinPackInconclusiveFunctions &&
610 Current.PackingKind == PPK_Inconclusive)));
Daniel Jaspera07aa662013-10-22 15:30:28 +0000611 // If this '[' opens an ObjC call, determine whether all parameters fit
612 // into one line and put one per line if they don't.
613 if (Current.Type == TT_ObjCMethodExpr &&
614 getLengthToMatchingParen(Current) + State.Column >
615 getColumnLimit(State))
616 BreakBeforeParameter = true;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000617 }
618
Daniel Jasper9b3cb442013-10-18 15:23:06 +0000619 bool NoLineBreak = State.Stack.back().NoLineBreak ||
620 (Current.Type == TT_TemplateOpener &&
621 State.Stack.back().ContainsUnwrappedBuilder);
622 State.Stack.push_back(ParenState(NewIndent, NewIndentLevel,
623 State.Stack.back().LastSpace,
624 AvoidBinPacking, NoLineBreak));
Daniel Jaspera07aa662013-10-22 15:30:28 +0000625 State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000626 ++State.ParenLevel;
627 }
628
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000629 // If we encounter a closing ), ], } or >, we can remove a level from our
630 // stacks.
Daniel Jasper7143a212013-08-28 09:17:37 +0000631 if (State.Stack.size() > 1 &&
632 (Current.isOneOf(tok::r_paren, tok::r_square) ||
Daniel Jasper567dcf92013-09-05 09:29:45 +0000633 (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
Daniel Jasper7143a212013-08-28 09:17:37 +0000634 State.NextToken->Type == TT_TemplateCloser)) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000635 State.Stack.pop_back();
636 --State.ParenLevel;
637 }
638 if (Current.is(tok::r_square)) {
639 // If this ends the array subscript expr, reset the corresponding value.
640 const FormatToken *NextNonComment = Current.getNextNonComment();
641 if (NextNonComment && NextNonComment->isNot(tok::l_square))
642 State.Stack.back().StartOfArraySubscripts = 0;
643 }
644
645 // Remove scopes created by fake parenthesis.
Daniel Jasper567dcf92013-09-05 09:29:45 +0000646 if (Current.isNot(tok::r_brace) ||
647 (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) {
Daniel Jasper1a925bc2013-09-05 10:48:50 +0000648 // Don't remove FakeRParens attached to r_braces that surround nested blocks
649 // as they will have been removed early (see above).
Daniel Jasper567dcf92013-09-05 09:29:45 +0000650 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
651 unsigned VariablePos = State.Stack.back().VariablePos;
652 State.Stack.pop_back();
653 State.Stack.back().VariablePos = VariablePos;
654 }
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000655 }
656
657 if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
658 State.StartOfStringLiteral = State.Column;
659 } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
660 tok::string_literal)) {
661 State.StartOfStringLiteral = 0;
662 }
663
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000664 State.Column += Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000665 State.NextToken = State.NextToken->Next;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000666 unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000667 if (State.Column > getColumnLimit(State)) {
668 unsigned ExcessCharacters = State.Column - getColumnLimit(State);
669 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
670 }
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000671
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000672 // If the previous has a special role, let it consume tokens as appropriate.
673 // It is necessary to start at the previous token for the only implemented
674 // role (comma separated list). That way, the decision whether or not to break
675 // after the "{" is already done and both options are tried and evaluated.
676 // FIXME: This is ugly, find a better way.
677 if (Previous && Previous->Role)
678 Penalty += Previous->Role->format(State, this, DryRun);
679
680 return Penalty;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000681}
682
Alexander Kornienko6f6154c2013-09-10 12:29:48 +0000683unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
684 LineState &State) {
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000685 // Break before further function parameters on all levels.
686 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
687 State.Stack[i].BreakBeforeParameter = true;
688
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000689 unsigned ColumnsUsed = State.Column;
Alexander Kornienko4b762a92013-09-02 13:58:14 +0000690 // We can only affect layout of the first and the last line, so the penalty
691 // for all other lines is constant, and we ignore it.
Alexander Kornienko0b62cc32013-09-05 14:08:34 +0000692 State.Column = Current.LastLineColumnWidth;
Alexander Kornienko4b762a92013-09-02 13:58:14 +0000693
Daniel Jasper567dcf92013-09-05 09:29:45 +0000694 if (ColumnsUsed > getColumnLimit(State))
695 return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000696 return 0;
697}
698
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000699static bool getRawStringLiteralPrefixPostfix(StringRef Text,
700 StringRef &Prefix,
701 StringRef &Postfix) {
702 if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") ||
703 Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") ||
704 Text.startswith(Prefix = "LR\"")) {
705 size_t ParenPos = Text.find('(');
706 if (ParenPos != StringRef::npos) {
707 StringRef Delimiter =
708 Text.substr(Prefix.size(), ParenPos - Prefix.size());
709 Prefix = Text.substr(0, ParenPos + 1);
710 Postfix = Text.substr(Text.size() - 2 - Delimiter.size());
711 return Postfix.front() == ')' && Postfix.back() == '"' &&
712 Postfix.substr(1).startswith(Delimiter);
713 }
714 }
715 return false;
716}
717
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000718unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
719 LineState &State,
720 bool DryRun) {
Alexander Kornienko6f6154c2013-09-10 12:29:48 +0000721 // Don't break multi-line tokens other than block comments. Instead, just
722 // update the state.
723 if (Current.Type != TT_BlockComment && Current.IsMultiline)
724 return addMultilineToken(Current, State);
725
Daniel Jasper84379572013-10-30 13:54:53 +0000726 // Don't break implicit string literals.
727 if (Current.Type == TT_ImplicitStringLiteral)
728 return 0;
729
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000730 if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal,
731 tok::utf8_string_literal, tok::utf16_string_literal,
732 tok::utf32_string_literal, tok::comment))
Daniel Jaspered51c022013-08-23 10:05:49 +0000733 return 0;
734
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000735 llvm::OwningPtr<BreakableToken> Token;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000736 unsigned StartColumn = State.Column - Current.ColumnWidth;
Alexander Kornienko5486a422013-11-12 17:30:49 +0000737 unsigned ColumnLimit = getColumnLimit(State);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000738
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000739 if (Current.isOneOf(tok::string_literal, tok::wide_string_literal,
740 tok::utf8_string_literal, tok::utf16_string_literal,
741 tok::utf32_string_literal) &&
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000742 Current.Type != TT_ImplicitStringLiteral) {
Alexander Kornienkob18c2582013-10-11 21:43:05 +0000743 // Don't break string literals inside preprocessor directives (except for
744 // #define directives, as their contents are stored in separate lines and
745 // are not affected by this check).
746 // This way we avoid breaking code with line directives and unknown
747 // preprocessor directives that contain long string literals.
748 if (State.Line->Type == LT_PreprocessorDirective)
749 return 0;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000750 // Exempts unterminated string literals from line breaking. The user will
751 // likely want to terminate the string before any line breaking is done.
752 if (Current.IsUnterminatedLiteral)
753 return 0;
754
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000755 StringRef Text = Current.TokenText;
756 StringRef Prefix;
757 StringRef Postfix;
758 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
759 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
760 // reduce the overhead) for each FormatToken, which is a string, so that we
761 // don't run multiple checks here on the hot path.
762 if ((Text.endswith(Postfix = "\"") &&
763 (Text.startswith(Prefix = "\"") || Text.startswith(Prefix = "u\"") ||
764 Text.startswith(Prefix = "U\"") || Text.startswith(Prefix = "u8\"") ||
765 Text.startswith(Prefix = "L\""))) ||
766 (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) ||
767 getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) {
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000768 Token.reset(new BreakableStringLiteral(
769 Current, State.Line->Level, StartColumn, Prefix, Postfix,
770 State.Line->InPPDirective, Encoding, Style));
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000771 } else {
772 return 0;
773 }
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000774 } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
775 Token.reset(new BreakableBlockComment(
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000776 Current, State.Line->Level, StartColumn, Current.OriginalColumn,
777 !Current.Previous, State.Line->InPPDirective, Encoding, Style));
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000778 } else if (Current.Type == TT_LineComment &&
779 (Current.Previous == NULL ||
780 Current.Previous->Type != TT_ImplicitStringLiteral)) {
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000781 Token.reset(new BreakableLineComment(Current, State.Line->Level,
Alexander Kornienko5486a422013-11-12 17:30:49 +0000782 StartColumn, /*InPPDirective=*/false,
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000783 Encoding, Style));
Alexander Kornienko5486a422013-11-12 17:30:49 +0000784 // We don't insert backslashes when breaking line comments.
785 ColumnLimit = Style.ColumnLimit;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000786 } else {
787 return 0;
788 }
Alexander Kornienko5486a422013-11-12 17:30:49 +0000789 if (Current.UnbreakableTailLength >= ColumnLimit)
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000790 return 0;
791
Alexander Kornienko5486a422013-11-12 17:30:49 +0000792 unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000793 bool BreakInserted = false;
794 unsigned Penalty = 0;
795 unsigned RemainingTokenColumns = 0;
796 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
797 LineIndex != EndIndex; ++LineIndex) {
798 if (!DryRun)
799 Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
800 unsigned TailOffset = 0;
801 RemainingTokenColumns =
802 Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
803 while (RemainingTokenColumns > RemainingSpace) {
804 BreakableToken::Split Split =
Alexander Kornienko5486a422013-11-12 17:30:49 +0000805 Token->getSplit(LineIndex, TailOffset, ColumnLimit);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000806 if (Split.first == StringRef::npos) {
807 // The last line's penalty is handled in addNextStateToQueue().
808 if (LineIndex < EndIndex - 1)
809 Penalty += Style.PenaltyExcessCharacter *
810 (RemainingTokenColumns - RemainingSpace);
811 break;
812 }
813 assert(Split.first != 0);
814 unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
815 LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
816 assert(NewRemainingTokenColumns < RemainingTokenColumns);
817 if (!DryRun)
818 Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
Daniel Jasperf5461782013-08-28 10:03:58 +0000819 Penalty += Current.SplitPenalty;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000820 unsigned ColumnsUsed =
821 Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
Alexander Kornienko5486a422013-11-12 17:30:49 +0000822 if (ColumnsUsed > ColumnLimit) {
823 Penalty += Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000824 }
825 TailOffset += Split.first + Split.second;
826 RemainingTokenColumns = NewRemainingTokenColumns;
827 BreakInserted = true;
828 }
829 }
830
831 State.Column = RemainingTokenColumns;
832
833 if (BreakInserted) {
834 // If we break the token inside a parameter list, we need to break before
835 // the next parameter on all levels, so that the next parameter is clearly
836 // visible. Line comments already introduce a break.
837 if (Current.Type != TT_LineComment) {
838 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
839 State.Stack[i].BreakBeforeParameter = true;
840 }
841
Daniel Jasperf5461782013-08-28 10:03:58 +0000842 Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
843 : Style.PenaltyBreakComment;
844
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000845 State.Stack.back().LastSpace = StartColumn;
846 }
847 return Penalty;
848}
849
Daniel Jasper567dcf92013-09-05 09:29:45 +0000850unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000851 // In preprocessor directives reserve two chars for trailing " \"
Daniel Jasper567dcf92013-09-05 09:29:45 +0000852 return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000853}
854
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000855bool ContinuationIndenter::NextIsMultilineString(const LineState &State) {
856 const FormatToken &Current = *State.NextToken;
857 if (!Current.is(tok::string_literal))
858 return false;
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000859 // We never consider raw string literals "multiline" for the purpose of
860 // AlwaysBreakBeforeMultilineStrings implementation.
861 if (Current.TokenText.startswith("R\""))
862 return false;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000863 if (Current.IsMultiline)
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000864 return true;
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000865 if (Current.getNextNonComment() &&
866 Current.getNextNonComment()->is(tok::string_literal))
867 return true; // Implicit concatenation.
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000868 if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000869 Style.ColumnLimit)
870 return true; // String will be split.
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000871 return false;
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000872}
873
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000874} // namespace format
875} // namespace clang