blob: cc12fc9d81349fba497775b7e0cc510cf5acd1c8 [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 Jasper6b2afe42013-08-16 11:20:30 +000047ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
48 SourceManager &SourceMgr,
Daniel Jasper6b2afe42013-08-16 11:20:30 +000049 WhitespaceManager &Whitespaces,
50 encoding::Encoding Encoding,
51 bool BinPackInconclusiveFunctions)
Daniel Jasper567dcf92013-09-05 09:29:45 +000052 : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces),
53 Encoding(Encoding),
Daniel Jasper6b2afe42013-08-16 11:20:30 +000054 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {}
55
Daniel Jasper567dcf92013-09-05 09:29:45 +000056LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
Daniel Jasperb77d7412013-09-06 07:54:20 +000057 const AnnotatedLine *Line,
58 bool DryRun) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +000059 LineState State;
Daniel Jasper567dcf92013-09-05 09:29:45 +000060 State.FirstIndent = FirstIndent;
Daniel Jasper6b2afe42013-08-16 11:20:30 +000061 State.Column = FirstIndent;
Daniel Jasper567dcf92013-09-05 09:29:45 +000062 State.Line = Line;
63 State.NextToken = Line->First;
Daniel Jasper6b2afe42013-08-16 11:20:30 +000064 State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
65 /*AvoidBinPacking=*/false,
66 /*NoLineBreak=*/false));
67 State.LineContainsContinuedForLoopSection = false;
68 State.ParenLevel = 0;
69 State.StartOfStringLiteral = 0;
70 State.StartOfLineLevel = State.ParenLevel;
71 State.LowestLevelOnLine = State.ParenLevel;
72 State.IgnoreStackForComparison = false;
73
74 // The first token has already been indented and thus consumed.
Daniel Jasperb77d7412013-09-06 07:54:20 +000075 moveStateToNextToken(State, DryRun, /*Newline=*/false);
Daniel Jasper6b2afe42013-08-16 11:20:30 +000076 return State;
77}
78
79bool ContinuationIndenter::canBreak(const LineState &State) {
80 const FormatToken &Current = *State.NextToken;
81 const FormatToken &Previous = *Current.Previous;
82 assert(&Previous == Current.Previous);
83 if (!Current.CanBreakBefore &&
84 !(Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace))
85 return false;
86 // The opening "{" of a braced list has to be on the same line as the first
87 // element if it is nested in another braced init list or function call.
88 if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
Daniel Jasper567dcf92013-09-05 09:29:45 +000089 Previous.BlockKind == BK_BracedInit && Previous.Previous &&
Daniel Jasper6b2afe42013-08-16 11:20:30 +000090 Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
91 return false;
92 // This prevents breaks like:
93 // ...
94 // SomeParameter, OtherParameter).DoSomething(
95 // ...
96 // As they hide "DoSomething" and are generally bad for readability.
97 if (Previous.opensScope() && State.LowestLevelOnLine < State.StartOfLineLevel)
98 return false;
Daniel Jasperd3fef0f2013-08-27 14:24:43 +000099 if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
100 return false;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000101 return !State.Stack.back().NoLineBreak;
102}
103
104bool ContinuationIndenter::mustBreak(const LineState &State) {
105 const FormatToken &Current = *State.NextToken;
106 const FormatToken &Previous = *Current.Previous;
107 if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
108 return true;
Daniel Jasper57981202013-09-13 09:20:45 +0000109 if ((!Style.Cpp11BracedListStyle ||
110 (Current.MatchingParen &&
111 Current.MatchingParen->BlockKind == BK_Block)) &&
112 Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace)
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000113 return true;
114 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
115 return true;
116 if (Style.BreakConstructorInitializersBeforeComma) {
117 if (Previous.Type == TT_CtorInitializerComma)
118 return false;
119 if (Current.Type == TT_CtorInitializerComma)
120 return true;
121 }
122 if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
123 (Current.Type == TT_ConditionalExpr &&
124 !(Current.is(tok::colon) && Previous.is(tok::question)))) &&
125 State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
126 !Current.isOneOf(tok::r_paren, tok::r_brace))
127 return true;
128 if (Style.AlwaysBreakBeforeMultilineStrings &&
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000129 State.Column > State.Stack.back().Indent && // Breaking saves columns.
130 Previous.isNot(tok::lessless) && Previous.Type != TT_InlineASMColon &&
131 NextIsMultilineString(State))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000132 return true;
133
134 if (!Style.BreakBeforeBinaryOperators) {
135 // If we need to break somewhere inside the LHS of a binary expression, we
136 // should also break after the operator. Otherwise, the formatting would
137 // hide the operator precedence, e.g. in:
138 // if (aaaaaaaaaaaaaa ==
139 // bbbbbbbbbbbbbb && c) {..
140 // For comparisons, we only apply this rule, if the LHS is a binary
141 // expression itself as otherwise, the line breaks seem superfluous.
142 // We need special cases for ">>" which we have split into two ">" while
143 // lexing in order to make template parsing easier.
144 //
145 // FIXME: We'll need something similar for styles that break before binary
146 // operators.
147 bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
148 Previous.getPrecedence() == prec::Equality) &&
149 Previous.Previous &&
150 Previous.Previous->Type != TT_BinaryOperator; // For >>.
151 bool LHSIsBinaryExpr =
Daniel Jasperdb4813a2013-09-06 08:08:14 +0000152 Previous.Previous && Previous.Previous->EndsBinaryExpression;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000153 if (Previous.Type == TT_BinaryOperator &&
154 (!IsComparison || LHSIsBinaryExpr) &&
155 Current.Type != TT_BinaryOperator && // For >>.
156 !Current.isTrailingComment() &&
157 !Previous.isOneOf(tok::lessless, tok::question) &&
158 Previous.getPrecedence() != prec::Assignment &&
159 State.Stack.back().BreakBeforeParameter)
160 return true;
161 }
162
163 // Same as above, but for the first "<<" operator.
164 if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter &&
165 State.Stack.back().FirstLessLess == 0)
166 return true;
167
168 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
169 // out whether it is the first parameter. Clean this up.
170 if (Current.Type == TT_ObjCSelectorName &&
171 Current.LongestObjCSelectorName == 0 &&
172 State.Stack.back().BreakBeforeParameter)
173 return true;
174 if ((Current.Type == TT_CtorInitializerColon ||
175 (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0)))
176 return true;
177
178 if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
Daniel Jasper567dcf92013-09-05 09:29:45 +0000179 State.Line->MightBeFunctionDecl &&
180 State.Stack.back().BreakBeforeParameter && State.ParenLevel == 0)
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000181 return true;
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000182 if (startsSegmentOfBuilderTypeCall(Current) &&
Daniel Jaspereb331832013-08-30 07:12:40 +0000183 (State.Stack.back().CallContinuation != 0 ||
184 (State.Stack.back().BreakBeforeParameter &&
185 State.Stack.back().ContainsUnwrappedBuilder)))
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000186 return true;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000187 return false;
188}
189
190unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000191 bool DryRun,
192 unsigned ExtraSpaces) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000193 const FormatToken &Current = *State.NextToken;
194 const FormatToken &Previous = *State.NextToken->Previous;
Daniel Jasper48c099f2013-09-27 07:49:08 +0000195 const FormatToken *PreviousNonComment =
196 State.NextToken->getPreviousNonComment();
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000197
198 // Extra penalty that needs to be added because of the way certain line
199 // breaks are chosen.
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000200 unsigned Penalty = 0;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000201
202 if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
203 // FIXME: Is this correct?
204 int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
205 State.NextToken->WhitespaceRange.getEnd()) -
206 SourceMgr.getSpellingColumnNumber(
207 State.NextToken->WhitespaceRange.getBegin());
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000208 State.Column += WhitespaceLength + State.NextToken->ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000209 State.NextToken = State.NextToken->Next;
210 return 0;
211 }
212
213 // If we are continuing an expression, we want to indent an extra 4 spaces.
214 unsigned ContinuationIndent =
215 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
216 if (Newline) {
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000217 // The first line break on any ParenLevel causes an extra penalty in order
218 // prefer similar line breaks.
219 if (!State.Stack.back().ContainsLineBreak)
220 Penalty += 15;
221 State.Stack.back().ContainsLineBreak = true;
222
223 Penalty += State.NextToken->SplitPenalty;
224
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000225 // Breaking before the first "<<" is generally not desirable if the LHS is
226 // short.
227 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
228 State.Column <= Style.ColumnLimit / 2)
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000229 Penalty += Style.PenaltyBreakFirstLessLess;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000230
Daniel Jasper57981202013-09-13 09:20:45 +0000231 if (Current.is(tok::l_brace) && Current.BlockKind == BK_Block) {
232 State.Column = State.FirstIndent;
233 } else if (Current.is(tok::r_brace)) {
Daniel Jasper00e0f432013-09-06 21:46:41 +0000234 if (Current.MatchingParen &&
235 (Current.MatchingParen->BlockKind == BK_BracedInit ||
236 !Current.MatchingParen->Children.empty()))
237 State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
238 else
239 State.Column = State.FirstIndent;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000240 } else if (Current.is(tok::string_literal) &&
241 State.StartOfStringLiteral != 0) {
242 State.Column = State.StartOfStringLiteral;
243 State.Stack.back().BreakBeforeParameter = true;
244 } else if (Current.is(tok::lessless) &&
245 State.Stack.back().FirstLessLess != 0) {
246 State.Column = State.Stack.back().FirstLessLess;
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000247 } else if (Current.isMemberAccess()) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000248 if (State.Stack.back().CallContinuation == 0) {
249 State.Column = ContinuationIndent;
250 State.Stack.back().CallContinuation = State.Column;
251 } else {
252 State.Column = State.Stack.back().CallContinuation;
253 }
254 } else if (Current.Type == TT_ConditionalExpr) {
255 State.Column = State.Stack.back().QuestionColumn;
256 } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) {
257 State.Column = State.Stack.back().VariablePos;
Daniel Jasper48c099f2013-09-27 07:49:08 +0000258 } else if ((PreviousNonComment &&
259 PreviousNonComment->ClosesTemplateDeclaration) ||
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000260 ((Current.Type == TT_StartOfName ||
261 Current.is(tok::kw_operator)) &&
262 State.ParenLevel == 0 &&
263 (!Style.IndentFunctionDeclarationAfterType ||
Daniel Jasper567dcf92013-09-05 09:29:45 +0000264 State.Line->StartsDefinition))) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000265 State.Column = State.Stack.back().Indent;
266 } else if (Current.Type == TT_ObjCSelectorName) {
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000267 if (State.Stack.back().ColonPos > Current.ColumnWidth) {
268 State.Column = State.Stack.back().ColonPos - Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000269 } else {
270 State.Column = State.Stack.back().Indent;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000271 State.Stack.back().ColonPos = State.Column + Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000272 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000273 } else if (Current.is(tok::l_square) && Current.Type != TT_ObjCMethodExpr &&
274 Current.Type != TT_LambdaLSquare) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000275 if (State.Stack.back().StartOfArraySubscripts != 0)
276 State.Column = State.Stack.back().StartOfArraySubscripts;
277 else
278 State.Column = ContinuationIndent;
279 } else if (Current.Type == TT_StartOfName ||
280 Previous.isOneOf(tok::coloncolon, tok::equal) ||
281 Previous.Type == TT_ObjCMethodExpr) {
282 State.Column = ContinuationIndent;
283 } else if (Current.Type == TT_CtorInitializerColon) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000284 State.Column =
285 State.FirstIndent + Style.ConstructorInitializerIndentWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000286 } else if (Current.Type == TT_CtorInitializerComma) {
287 State.Column = State.Stack.back().Indent;
288 } else {
289 State.Column = State.Stack.back().Indent;
290 // Ensure that we fall back to indenting 4 spaces instead of just
291 // flushing continuations left.
Daniel Jasper567dcf92013-09-05 09:29:45 +0000292 if (State.Column == State.FirstIndent)
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000293 State.Column += 4;
294 }
295
296 if (Current.is(tok::question))
297 State.Stack.back().BreakBeforeParameter = true;
298 if ((Previous.isOneOf(tok::comma, tok::semi) &&
299 !State.Stack.back().AvoidBinPacking) ||
300 Previous.Type == TT_BinaryOperator)
301 State.Stack.back().BreakBeforeParameter = false;
302 if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
303 State.Stack.back().BreakBeforeParameter = false;
304
305 if (!DryRun) {
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000306 unsigned Newlines = 1;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000307 if (Current.is(tok::comment))
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000308 Newlines = std::max(Newlines, std::min(Current.NewlinesBefore,
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000309 Style.MaxEmptyLinesToKeep + 1));
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000310 Whitespaces.replaceWhitespace(Current, Newlines, State.Line->Level,
311 State.Column, State.Column,
312 State.Line->InPPDirective);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000313 }
314
315 if (!Current.isTrailingComment())
316 State.Stack.back().LastSpace = State.Column;
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000317 if (Current.isMemberAccess())
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000318 State.Stack.back().LastSpace += Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000319 State.StartOfLineLevel = State.ParenLevel;
320 State.LowestLevelOnLine = State.ParenLevel;
321
322 // Any break on this level means that the parent level has been broken
323 // and we need to avoid bin packing there.
324 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
325 State.Stack[i].BreakBeforeParameter = true;
326 }
327 const FormatToken *TokenBefore = Current.getPreviousNonComment();
328 if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
329 TokenBefore->Type != TT_TemplateCloser &&
330 TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
331 State.Stack.back().BreakBeforeParameter = true;
332
333 // If we break after {, we should also break before the corresponding }.
334 if (Previous.is(tok::l_brace))
335 State.Stack.back().BreakBeforeClosingBrace = true;
336
337 if (State.Stack.back().AvoidBinPacking) {
338 // If we are breaking after '(', '{', '<', this is not bin packing
339 // unless AllowAllParametersOfDeclarationOnNextLine is false.
340 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
341 Previous.Type == TT_BinaryOperator) ||
342 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
Daniel Jasper567dcf92013-09-05 09:29:45 +0000343 State.Line->MustBeDeclaration))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000344 State.Stack.back().BreakBeforeParameter = true;
345 }
346
347 } else {
348 if (Current.is(tok::equal) &&
Daniel Jasper567dcf92013-09-05 09:29:45 +0000349 (State.Line->First->is(tok::kw_for) || State.ParenLevel == 0) &&
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000350 State.Stack.back().VariablePos == 0) {
351 State.Stack.back().VariablePos = State.Column;
352 // Move over * and & if they are bound to the variable name.
353 const FormatToken *Tok = &Previous;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000354 while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
355 State.Stack.back().VariablePos -= Tok->ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000356 if (Tok->SpacesRequiredBefore != 0)
357 break;
358 Tok = Tok->Previous;
359 }
360 if (Previous.PartOfMultiVariableDeclStmt)
361 State.Stack.back().LastSpace = State.Stack.back().VariablePos;
362 }
363
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000364 unsigned Spaces = State.NextToken->SpacesRequiredBefore + ExtraSpaces;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000365
366 if (!DryRun)
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000367 Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0,
368 Spaces, State.Column + Spaces);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000369
370 if (Current.Type == TT_ObjCSelectorName &&
371 State.Stack.back().ColonPos == 0) {
372 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000373 State.Column + Spaces + Current.ColumnWidth)
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000374 State.Stack.back().ColonPos =
375 State.Stack.back().Indent + Current.LongestObjCSelectorName;
376 else
377 State.Stack.back().ColonPos =
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000378 State.Column + Spaces + Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000379 }
380
381 if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
382 Current.Type != TT_LineComment)
383 State.Stack.back().Indent = State.Column + Spaces;
384 if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
385 State.Stack.back().AvoidBinPacking)
386 State.Stack.back().NoLineBreak = true;
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000387 if (startsSegmentOfBuilderTypeCall(Current))
388 State.Stack.back().ContainsUnwrappedBuilder = true;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000389
390 State.Column += Spaces;
391 if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
392 // Treat the condition inside an if as if it was a second function
393 // parameter, i.e. let nested calls have an indent of 4.
394 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
395 else if (Previous.is(tok::comma))
396 State.Stack.back().LastSpace = State.Column;
397 else if ((Previous.Type == TT_BinaryOperator ||
398 Previous.Type == TT_ConditionalExpr ||
Daniel Jasper34f3d052013-08-21 08:39:01 +0000399 Previous.Type == TT_UnaryOperator ||
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000400 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000401 (Previous.getPrecedence() != prec::Assignment ||
Daniel Jasperdb4813a2013-09-06 08:08:14 +0000402 Current.StartsBinaryExpression))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000403 // Always indent relative to the RHS of the expression unless this is a
Daniel Jasper34f3d052013-08-21 08:39:01 +0000404 // simple assignment without binary expression on the RHS. Also indent
405 // relative to unary operators and the colons of constructor initializers.
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000406 State.Stack.back().LastSpace = State.Column;
407 else if (Previous.Type == TT_InheritanceColon)
408 State.Stack.back().Indent = State.Column;
409 else if (Previous.opensScope()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000410 // If a function has a trailing call, indent all parameters from the
411 // opening parenthesis. This avoids confusing indents like:
412 // OuterFunction(InnerFunctionCall( // break
413 // ParameterToInnerFunction)) // break
414 // .SecondInnerFunctionCall();
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000415 bool HasTrailingCall = false;
416 if (Previous.MatchingParen) {
417 const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000418 HasTrailingCall = Next && Next->isMemberAccess();
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000419 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000420 if (HasTrailingCall &&
421 State.Stack[State.Stack.size() - 2].CallContinuation == 0)
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000422 State.Stack.back().LastSpace = State.Column;
423 }
424 }
425
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000426 return moveStateToNextToken(State, DryRun, Newline) + Penalty;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000427}
428
429unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
430 bool DryRun, bool Newline) {
431 const FormatToken &Current = *State.NextToken;
432 assert(State.Stack.size());
433
434 if (Current.Type == TT_InheritanceColon)
435 State.Stack.back().AvoidBinPacking = true;
436 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
437 State.Stack.back().FirstLessLess = State.Column;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000438 if (Current.is(tok::l_square) && Current.Type != TT_LambdaLSquare &&
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000439 State.Stack.back().StartOfArraySubscripts == 0)
440 State.Stack.back().StartOfArraySubscripts = State.Column;
441 if (Current.is(tok::question))
442 State.Stack.back().QuestionColumn = State.Column;
443 if (!Current.opensScope() && !Current.closesScope())
444 State.LowestLevelOnLine =
445 std::min(State.LowestLevelOnLine, State.ParenLevel);
Daniel Jasperd3fef0f2013-08-27 14:24:43 +0000446 if (Current.isMemberAccess())
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000447 State.Stack.back().StartOfFunctionCall =
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000448 Current.LastInChainOfCalls ? 0 : State.Column + Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000449 if (Current.Type == TT_CtorInitializerColon) {
450 // Indent 2 from the column, so:
451 // SomeClass::SomeClass()
452 // : First(...), ...
453 // Next(...)
454 // ^ line up here.
455 State.Stack.back().Indent =
456 State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
457 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
458 State.Stack.back().AvoidBinPacking = true;
459 State.Stack.back().BreakBeforeParameter = false;
460 }
461
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000462 // In ObjC method declaration we align on the ":" of parameters, but we need
463 // to ensure that we indent parameters on subsequent lines by at least 4.
464 if (Current.Type == TT_ObjCMethodSpecifier)
465 State.Stack.back().Indent += 4;
466
467 // Insert scopes created by fake parenthesis.
468 const FormatToken *Previous = Current.getPreviousNonComment();
469 // Don't add extra indentation for the first fake parenthesis after
470 // 'return', assignements or opening <({[. The indentation for these cases
471 // is special cased.
472 bool SkipFirstExtraIndent =
Daniel Jasperf78bf4a2013-09-30 08:29:03 +0000473 (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) ||
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000474 Previous->getPrecedence() == prec::Assignment));
475 for (SmallVectorImpl<prec::Level>::const_reverse_iterator
476 I = Current.FakeLParens.rbegin(),
477 E = Current.FakeLParens.rend();
478 I != E; ++I) {
479 ParenState NewParenState = State.Stack.back();
480 NewParenState.ContainsLineBreak = false;
Daniel Jasperf78bf4a2013-09-30 08:29:03 +0000481
482 // Indent from 'LastSpace' unless this the fake parentheses encapsulating a
483 // builder type call after 'return'. If such a call is line-wrapped, we
484 // commonly just want to indent from the start of the line.
485 if (!Previous || Previous->isNot(tok::kw_return) || *I > 0)
486 NewParenState.Indent =
487 std::max(std::max(State.Column, NewParenState.Indent),
488 State.Stack.back().LastSpace);
489
Daniel Jasper567dcf92013-09-05 09:29:45 +0000490 // Do not indent relative to the fake parentheses inserted for "." or "->".
491 // This is a special case to make the following to statements consistent:
492 // OuterFunction(InnerFunctionCall( // break
493 // ParameterToInnerFunction));
494 // OuterFunction(SomeObject.InnerFunctionCall( // break
495 // ParameterToInnerFunction));
496 if (*I > prec::Unknown)
497 NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000498
499 // Always indent conditional expressions. Never indent expression where
500 // the 'operator' is ',', ';' or an assignment (i.e. *I <=
501 // prec::Assignment) as those have different indentation rules. Indent
502 // other expression, unless the indentation needs to be skipped.
503 if (*I == prec::Conditional ||
504 (!SkipFirstExtraIndent && *I > prec::Assignment &&
505 !Style.BreakBeforeBinaryOperators))
506 NewParenState.Indent += 4;
507 if (Previous && !Previous->opensScope())
508 NewParenState.BreakBeforeParameter = false;
509 State.Stack.push_back(NewParenState);
510 SkipFirstExtraIndent = false;
511 }
512
513 // If we encounter an opening (, [, { or <, we add a level to our stacks to
514 // prepare for the following tokens.
515 if (Current.opensScope()) {
516 unsigned NewIndent;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000517 bool AvoidBinPacking;
518 if (Current.is(tok::l_brace)) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000519 if (Current.MatchingParen && Current.BlockKind == BK_Block) {
Daniel Jasper1a925bc2013-09-05 10:48:50 +0000520 // If this is an l_brace starting a nested block, we pretend (wrt. to
521 // indentation) that we already consumed the corresponding r_brace.
522 // Thus, we remove all ParenStates caused bake fake parentheses that end
523 // at the r_brace. The net effect of this is that we don't indent
524 // relative to the l_brace, if the nested block is the last parameter of
525 // a function. For example, this formats:
526 //
527 // SomeFunction(a, [] {
528 // f(); // break
529 // });
530 //
531 // instead of:
532 // SomeFunction(a, [] {
533 // f(); // break
534 // });
Daniel Jasper567dcf92013-09-05 09:29:45 +0000535 for (unsigned i = 0; i != Current.MatchingParen->FakeRParens; ++i)
536 State.Stack.pop_back();
Daniel Jasper57981202013-09-13 09:20:45 +0000537 NewIndent = State.Stack.back().LastSpace + Style.IndentWidth;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000538 } else {
539 NewIndent = State.Stack.back().LastSpace +
540 (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth);
541 }
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000542 const FormatToken *NextNoComment = Current.getNextNonComment();
Daniel Jasper57981202013-09-13 09:20:45 +0000543 AvoidBinPacking = Current.BlockKind == BK_Block ||
544 (NextNoComment &&
545 NextNoComment->Type == TT_DesignatedInitializerPeriod);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000546 } else {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000547 NewIndent = 4 + std::max(State.Stack.back().LastSpace,
548 State.Stack.back().StartOfFunctionCall);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000549 AvoidBinPacking = !Style.BinPackParameters ||
550 (Style.ExperimentalAutoDetectBinPacking &&
551 (Current.PackingKind == PPK_OnePerLine ||
552 (!BinPackInconclusiveFunctions &&
553 Current.PackingKind == PPK_Inconclusive)));
554 }
555
Daniel Jasper567dcf92013-09-05 09:29:45 +0000556 State.Stack.push_back(ParenState(NewIndent, State.Stack.back().LastSpace,
557 AvoidBinPacking,
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000558 State.Stack.back().NoLineBreak));
Daniel Jasper57981202013-09-13 09:20:45 +0000559 State.Stack.back().BreakBeforeParameter = Current.BlockKind == BK_Block;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000560 ++State.ParenLevel;
561 }
562
563 // If this '[' opens an ObjC call, determine whether all parameters fit into
564 // one line and put one per line if they don't.
565 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
566 Current.MatchingParen != NULL) {
Daniel Jasper567dcf92013-09-05 09:29:45 +0000567 if (getLengthToMatchingParen(Current) + State.Column >
568 getColumnLimit(State))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000569 State.Stack.back().BreakBeforeParameter = true;
570 }
571
572 // If we encounter a closing ), ], } or >, we can remove a level from our
573 // stacks.
Daniel Jasper7143a212013-08-28 09:17:37 +0000574 if (State.Stack.size() > 1 &&
575 (Current.isOneOf(tok::r_paren, tok::r_square) ||
Daniel Jasper567dcf92013-09-05 09:29:45 +0000576 (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
Daniel Jasper7143a212013-08-28 09:17:37 +0000577 State.NextToken->Type == TT_TemplateCloser)) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000578 State.Stack.pop_back();
579 --State.ParenLevel;
580 }
581 if (Current.is(tok::r_square)) {
582 // If this ends the array subscript expr, reset the corresponding value.
583 const FormatToken *NextNonComment = Current.getNextNonComment();
584 if (NextNonComment && NextNonComment->isNot(tok::l_square))
585 State.Stack.back().StartOfArraySubscripts = 0;
586 }
587
588 // Remove scopes created by fake parenthesis.
Daniel Jasper567dcf92013-09-05 09:29:45 +0000589 if (Current.isNot(tok::r_brace) ||
590 (Current.MatchingParen && Current.MatchingParen->BlockKind != BK_Block)) {
Daniel Jasper1a925bc2013-09-05 10:48:50 +0000591 // Don't remove FakeRParens attached to r_braces that surround nested blocks
592 // as they will have been removed early (see above).
Daniel Jasper567dcf92013-09-05 09:29:45 +0000593 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
594 unsigned VariablePos = State.Stack.back().VariablePos;
595 State.Stack.pop_back();
596 State.Stack.back().VariablePos = VariablePos;
597 }
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000598 }
599
600 if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
601 State.StartOfStringLiteral = State.Column;
602 } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
603 tok::string_literal)) {
604 State.StartOfStringLiteral = 0;
605 }
606
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000607 State.Column += Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000608 State.NextToken = State.NextToken->Next;
Daniel Jasperd489f8c2013-08-27 11:09:05 +0000609 unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000610 if (State.Column > getColumnLimit(State)) {
611 unsigned ExcessCharacters = State.Column - getColumnLimit(State);
612 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
613 }
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000614
Daniel Jasperd4a03db2013-08-22 15:00:41 +0000615 // If the previous has a special role, let it consume tokens as appropriate.
616 // It is necessary to start at the previous token for the only implemented
617 // role (comma separated list). That way, the decision whether or not to break
618 // after the "{" is already done and both options are tried and evaluated.
619 // FIXME: This is ugly, find a better way.
620 if (Previous && Previous->Role)
621 Penalty += Previous->Role->format(State, this, DryRun);
622
623 return Penalty;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000624}
625
Alexander Kornienko6f6154c2013-09-10 12:29:48 +0000626unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
627 LineState &State) {
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000628 // Break before further function parameters on all levels.
629 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
630 State.Stack[i].BreakBeforeParameter = true;
631
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000632 unsigned ColumnsUsed = State.Column;
Alexander Kornienko4b762a92013-09-02 13:58:14 +0000633 // We can only affect layout of the first and the last line, so the penalty
634 // for all other lines is constant, and we ignore it.
Alexander Kornienko0b62cc32013-09-05 14:08:34 +0000635 State.Column = Current.LastLineColumnWidth;
Alexander Kornienko4b762a92013-09-02 13:58:14 +0000636
Daniel Jasper567dcf92013-09-05 09:29:45 +0000637 if (ColumnsUsed > getColumnLimit(State))
638 return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000639 return 0;
640}
641
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000642static bool getRawStringLiteralPrefixPostfix(StringRef Text,
643 StringRef &Prefix,
644 StringRef &Postfix) {
645 if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") ||
646 Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") ||
647 Text.startswith(Prefix = "LR\"")) {
648 size_t ParenPos = Text.find('(');
649 if (ParenPos != StringRef::npos) {
650 StringRef Delimiter =
651 Text.substr(Prefix.size(), ParenPos - Prefix.size());
652 Prefix = Text.substr(0, ParenPos + 1);
653 Postfix = Text.substr(Text.size() - 2 - Delimiter.size());
654 return Postfix.front() == ')' && Postfix.back() == '"' &&
655 Postfix.substr(1).startswith(Delimiter);
656 }
657 }
658 return false;
659}
660
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000661unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
662 LineState &State,
663 bool DryRun) {
Alexander Kornienko6f6154c2013-09-10 12:29:48 +0000664 // Don't break multi-line tokens other than block comments. Instead, just
665 // update the state.
666 if (Current.Type != TT_BlockComment && Current.IsMultiline)
667 return addMultilineToken(Current, State);
668
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000669 if (!Current.isOneOf(tok::string_literal, tok::wide_string_literal,
670 tok::utf8_string_literal, tok::utf16_string_literal,
671 tok::utf32_string_literal, tok::comment))
Daniel Jaspered51c022013-08-23 10:05:49 +0000672 return 0;
673
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000674 llvm::OwningPtr<BreakableToken> Token;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000675 unsigned StartColumn = State.Column - Current.ColumnWidth;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000676
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000677 if (Current.isOneOf(tok::string_literal, tok::wide_string_literal,
678 tok::utf8_string_literal, tok::utf16_string_literal,
679 tok::utf32_string_literal) &&
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000680 Current.Type != TT_ImplicitStringLiteral) {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000681 // Exempts unterminated string literals from line breaking. The user will
682 // likely want to terminate the string before any line breaking is done.
683 if (Current.IsUnterminatedLiteral)
684 return 0;
685
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000686 StringRef Text = Current.TokenText;
687 StringRef Prefix;
688 StringRef Postfix;
689 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
690 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
691 // reduce the overhead) for each FormatToken, which is a string, so that we
692 // don't run multiple checks here on the hot path.
693 if ((Text.endswith(Postfix = "\"") &&
694 (Text.startswith(Prefix = "\"") || Text.startswith(Prefix = "u\"") ||
695 Text.startswith(Prefix = "U\"") || Text.startswith(Prefix = "u8\"") ||
696 Text.startswith(Prefix = "L\""))) ||
697 (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) ||
698 getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) {
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000699 Token.reset(new BreakableStringLiteral(
700 Current, State.Line->Level, StartColumn, Prefix, Postfix,
701 State.Line->InPPDirective, Encoding, Style));
Alexander Kornienko2c2f7292013-09-16 20:20:49 +0000702 } else {
703 return 0;
704 }
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000705 } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
706 Token.reset(new BreakableBlockComment(
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000707 Current, State.Line->Level, StartColumn, Current.OriginalColumn,
708 !Current.Previous, State.Line->InPPDirective, Encoding, Style));
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000709 } else if (Current.Type == TT_LineComment &&
710 (Current.Previous == NULL ||
711 Current.Previous->Type != TT_ImplicitStringLiteral)) {
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000712 Token.reset(new BreakableLineComment(Current, State.Line->Level,
713 StartColumn, State.Line->InPPDirective,
714 Encoding, Style));
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000715 } else {
716 return 0;
717 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000718 if (Current.UnbreakableTailLength >= getColumnLimit(State))
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000719 return 0;
720
Daniel Jasper567dcf92013-09-05 09:29:45 +0000721 unsigned RemainingSpace =
722 getColumnLimit(State) - Current.UnbreakableTailLength;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000723 bool BreakInserted = false;
724 unsigned Penalty = 0;
725 unsigned RemainingTokenColumns = 0;
726 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
727 LineIndex != EndIndex; ++LineIndex) {
728 if (!DryRun)
729 Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
730 unsigned TailOffset = 0;
731 RemainingTokenColumns =
732 Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
733 while (RemainingTokenColumns > RemainingSpace) {
734 BreakableToken::Split Split =
Daniel Jasper567dcf92013-09-05 09:29:45 +0000735 Token->getSplit(LineIndex, TailOffset, getColumnLimit(State));
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000736 if (Split.first == StringRef::npos) {
737 // The last line's penalty is handled in addNextStateToQueue().
738 if (LineIndex < EndIndex - 1)
739 Penalty += Style.PenaltyExcessCharacter *
740 (RemainingTokenColumns - RemainingSpace);
741 break;
742 }
743 assert(Split.first != 0);
744 unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
745 LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
746 assert(NewRemainingTokenColumns < RemainingTokenColumns);
747 if (!DryRun)
748 Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
Daniel Jasperf5461782013-08-28 10:03:58 +0000749 Penalty += Current.SplitPenalty;
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000750 unsigned ColumnsUsed =
751 Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000752 if (ColumnsUsed > getColumnLimit(State)) {
753 Penalty += Style.PenaltyExcessCharacter *
754 (ColumnsUsed - getColumnLimit(State));
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000755 }
756 TailOffset += Split.first + Split.second;
757 RemainingTokenColumns = NewRemainingTokenColumns;
758 BreakInserted = true;
759 }
760 }
761
762 State.Column = RemainingTokenColumns;
763
764 if (BreakInserted) {
765 // If we break the token inside a parameter list, we need to break before
766 // the next parameter on all levels, so that the next parameter is clearly
767 // visible. Line comments already introduce a break.
768 if (Current.Type != TT_LineComment) {
769 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
770 State.Stack[i].BreakBeforeParameter = true;
771 }
772
Daniel Jasperf5461782013-08-28 10:03:58 +0000773 Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
774 : Style.PenaltyBreakComment;
775
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000776 State.Stack.back().LastSpace = StartColumn;
777 }
778 return Penalty;
779}
780
Daniel Jasper567dcf92013-09-05 09:29:45 +0000781unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000782 // In preprocessor directives reserve two chars for trailing " \"
Daniel Jasper567dcf92013-09-05 09:29:45 +0000783 return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000784}
785
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000786bool ContinuationIndenter::NextIsMultilineString(const LineState &State) {
787 const FormatToken &Current = *State.NextToken;
788 if (!Current.is(tok::string_literal))
789 return false;
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000790 // We never consider raw string literals "multiline" for the purpose of
791 // AlwaysBreakBeforeMultilineStrings implementation.
792 if (Current.TokenText.startswith("R\""))
793 return false;
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000794 if (Current.IsMultiline)
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000795 return true;
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000796 if (Current.getNextNonComment() &&
797 Current.getNextNonComment()->is(tok::string_literal))
798 return true; // Implicit concatenation.
Alexander Kornienko83a7dcd2013-09-10 09:38:25 +0000799 if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000800 Style.ColumnLimit)
801 return true; // String will be split.
Alexander Kornienkodcc0c5b2013-08-29 17:32:57 +0000802 return false;
Daniel Jasper4df1ff92013-08-23 11:57:34 +0000803}
804
Daniel Jasper6b2afe42013-08-16 11:20:30 +0000805} // namespace format
806} // namespace clang