blob: f4ae5cce01b78cc96899b4219bd55650e8422d9d [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
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 Jasperb27c4b72013-08-27 11:09:05 +000041// Returns \c true if \c Tok starts a binary expression.
42static bool startsBinaryExpression(const FormatToken &Tok) {
43 for (unsigned i = 0, e = Tok.FakeLParens.size(); i != e; ++i) {
44 if (Tok.FakeLParens[i] > prec::Unknown)
45 return true;
46 }
47 return false;
48}
49
Daniel Jasper4c6e0052013-08-27 14:24:43 +000050// Returns \c true if \c Tok is the "." or "->" of a call and starts the next
51// segment of a builder type call.
52static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
53 return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
54}
55
Daniel Jasperde0328a2013-08-16 11:20:30 +000056ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
57 SourceManager &SourceMgr,
58 const AnnotatedLine &Line,
59 unsigned FirstIndent,
60 WhitespaceManager &Whitespaces,
61 encoding::Encoding Encoding,
62 bool BinPackInconclusiveFunctions)
63 : Style(Style), SourceMgr(SourceMgr), Line(Line), FirstIndent(FirstIndent),
64 Whitespaces(Whitespaces), Encoding(Encoding),
65 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions) {}
66
67LineState ContinuationIndenter::getInitialState() {
68 // Initialize state dependent on indent.
69 LineState State;
70 State.Column = FirstIndent;
71 State.NextToken = Line.First;
72 State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
73 /*AvoidBinPacking=*/false,
74 /*NoLineBreak=*/false));
75 State.LineContainsContinuedForLoopSection = false;
76 State.ParenLevel = 0;
77 State.StartOfStringLiteral = 0;
78 State.StartOfLineLevel = State.ParenLevel;
79 State.LowestLevelOnLine = State.ParenLevel;
80 State.IgnoreStackForComparison = false;
81
82 // The first token has already been indented and thus consumed.
83 moveStateToNextToken(State, /*DryRun=*/false,
84 /*Newline=*/false);
85 return State;
86}
87
88bool ContinuationIndenter::canBreak(const LineState &State) {
89 const FormatToken &Current = *State.NextToken;
90 const FormatToken &Previous = *Current.Previous;
91 assert(&Previous == Current.Previous);
92 if (!Current.CanBreakBefore &&
93 !(Current.is(tok::r_brace) && State.Stack.back().BreakBeforeClosingBrace))
94 return false;
95 // The opening "{" of a braced list has to be on the same line as the first
96 // element if it is nested in another braced init list or function call.
97 if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
98 Previous.Previous &&
99 Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
100 return false;
101 // This prevents breaks like:
102 // ...
103 // SomeParameter, OtherParameter).DoSomething(
104 // ...
105 // As they hide "DoSomething" and are generally bad for readability.
106 if (Previous.opensScope() && State.LowestLevelOnLine < State.StartOfLineLevel)
107 return false;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000108 if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
109 return false;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000110 return !State.Stack.back().NoLineBreak;
111}
112
113bool ContinuationIndenter::mustBreak(const LineState &State) {
114 const FormatToken &Current = *State.NextToken;
115 const FormatToken &Previous = *Current.Previous;
116 if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon)
117 return true;
118 if (!Style.Cpp11BracedListStyle && Current.is(tok::r_brace) &&
119 State.Stack.back().BreakBeforeClosingBrace)
120 return true;
121 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
122 return true;
123 if (Style.BreakConstructorInitializersBeforeComma) {
124 if (Previous.Type == TT_CtorInitializerComma)
125 return false;
126 if (Current.Type == TT_CtorInitializerComma)
127 return true;
128 }
129 if ((Previous.isOneOf(tok::comma, tok::semi) || Current.is(tok::question) ||
130 (Current.Type == TT_ConditionalExpr &&
131 !(Current.is(tok::colon) && Previous.is(tok::question)))) &&
132 State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
133 !Current.isOneOf(tok::r_paren, tok::r_brace))
134 return true;
135 if (Style.AlwaysBreakBeforeMultilineStrings &&
Daniel Jasperf438cb72013-08-23 11:57:34 +0000136 State.Column > State.Stack.back().Indent && // Breaking saves columns.
137 Previous.isNot(tok::lessless) && Previous.Type != TT_InlineASMColon &&
138 NextIsMultilineString(State))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000139 return true;
140
141 if (!Style.BreakBeforeBinaryOperators) {
142 // If we need to break somewhere inside the LHS of a binary expression, we
143 // should also break after the operator. Otherwise, the formatting would
144 // hide the operator precedence, e.g. in:
145 // if (aaaaaaaaaaaaaa ==
146 // bbbbbbbbbbbbbb && c) {..
147 // For comparisons, we only apply this rule, if the LHS is a binary
148 // expression itself as otherwise, the line breaks seem superfluous.
149 // We need special cases for ">>" which we have split into two ">" while
150 // lexing in order to make template parsing easier.
151 //
152 // FIXME: We'll need something similar for styles that break before binary
153 // operators.
154 bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
155 Previous.getPrecedence() == prec::Equality) &&
156 Previous.Previous &&
157 Previous.Previous->Type != TT_BinaryOperator; // For >>.
158 bool LHSIsBinaryExpr =
159 Previous.Previous && Previous.Previous->FakeRParens > 0;
160 if (Previous.Type == TT_BinaryOperator &&
161 (!IsComparison || LHSIsBinaryExpr) &&
162 Current.Type != TT_BinaryOperator && // For >>.
163 !Current.isTrailingComment() &&
164 !Previous.isOneOf(tok::lessless, tok::question) &&
165 Previous.getPrecedence() != prec::Assignment &&
166 State.Stack.back().BreakBeforeParameter)
167 return true;
168 }
169
170 // Same as above, but for the first "<<" operator.
171 if (Current.is(tok::lessless) && State.Stack.back().BreakBeforeParameter &&
172 State.Stack.back().FirstLessLess == 0)
173 return true;
174
175 // FIXME: Comparing LongestObjCSelectorName to 0 is a hacky way of finding
176 // out whether it is the first parameter. Clean this up.
177 if (Current.Type == TT_ObjCSelectorName &&
178 Current.LongestObjCSelectorName == 0 &&
179 State.Stack.back().BreakBeforeParameter)
180 return true;
181 if ((Current.Type == TT_CtorInitializerColon ||
182 (Previous.ClosesTemplateDeclaration && State.ParenLevel == 0)))
183 return true;
184
185 if ((Current.Type == TT_StartOfName || Current.is(tok::kw_operator)) &&
186 Line.MightBeFunctionDecl && State.Stack.back().BreakBeforeParameter &&
187 State.ParenLevel == 0)
188 return true;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000189 if (startsSegmentOfBuilderTypeCall(Current) &&
190 State.Stack.back().CallContinuation != 0)
191 return true;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000192 return false;
193}
194
195unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000196 bool DryRun,
197 unsigned ExtraSpaces) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000198 const FormatToken &Current = *State.NextToken;
199 const FormatToken &Previous = *State.NextToken->Previous;
200
201 // Extra penalty that needs to be added because of the way certain line
202 // breaks are chosen.
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000203 unsigned Penalty = 0;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000204
205 if (State.Stack.size() == 0 || Current.Type == TT_ImplicitStringLiteral) {
206 // FIXME: Is this correct?
207 int WhitespaceLength = SourceMgr.getSpellingColumnNumber(
208 State.NextToken->WhitespaceRange.getEnd()) -
209 SourceMgr.getSpellingColumnNumber(
210 State.NextToken->WhitespaceRange.getBegin());
211 State.Column += WhitespaceLength + State.NextToken->CodePointCount;
212 State.NextToken = State.NextToken->Next;
213 return 0;
214 }
215
216 // If we are continuing an expression, we want to indent an extra 4 spaces.
217 unsigned ContinuationIndent =
218 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
219 if (Newline) {
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000220 // The first line break on any ParenLevel causes an extra penalty in order
221 // prefer similar line breaks.
222 if (!State.Stack.back().ContainsLineBreak)
223 Penalty += 15;
224 State.Stack.back().ContainsLineBreak = true;
225
226 Penalty += State.NextToken->SplitPenalty;
227
Daniel Jasperde0328a2013-08-16 11:20:30 +0000228 // Breaking before the first "<<" is generally not desirable if the LHS is
229 // short.
230 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0 &&
231 State.Column <= Style.ColumnLimit / 2)
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000232 Penalty += Style.PenaltyBreakFirstLessLess;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000233
Daniel Jasperde0328a2013-08-16 11:20:30 +0000234 if (Current.is(tok::r_brace)) {
235 if (Current.BlockKind == BK_BracedInit)
236 State.Column = State.Stack[State.Stack.size() - 2].LastSpace;
237 else
238 State.Column = FirstIndent;
239 } else if (Current.is(tok::string_literal) &&
240 State.StartOfStringLiteral != 0) {
241 State.Column = State.StartOfStringLiteral;
242 State.Stack.back().BreakBeforeParameter = true;
243 } else if (Current.is(tok::lessless) &&
244 State.Stack.back().FirstLessLess != 0) {
245 State.Column = State.Stack.back().FirstLessLess;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000246 } else if (Current.isMemberAccess()) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000247 if (State.Stack.back().CallContinuation == 0) {
248 State.Column = ContinuationIndent;
249 State.Stack.back().CallContinuation = State.Column;
250 } else {
251 State.Column = State.Stack.back().CallContinuation;
252 }
253 } else if (Current.Type == TT_ConditionalExpr) {
254 State.Column = State.Stack.back().QuestionColumn;
255 } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) {
256 State.Column = State.Stack.back().VariablePos;
257 } else if (Previous.ClosesTemplateDeclaration ||
258 ((Current.Type == TT_StartOfName ||
259 Current.is(tok::kw_operator)) &&
260 State.ParenLevel == 0 &&
261 (!Style.IndentFunctionDeclarationAfterType ||
262 Line.StartsDefinition))) {
263 State.Column = State.Stack.back().Indent;
264 } else if (Current.Type == TT_ObjCSelectorName) {
265 if (State.Stack.back().ColonPos > Current.CodePointCount) {
266 State.Column = State.Stack.back().ColonPos - Current.CodePointCount;
267 } else {
268 State.Column = State.Stack.back().Indent;
269 State.Stack.back().ColonPos = State.Column + Current.CodePointCount;
270 }
271 } else if (Current.is(tok::l_square) && Current.Type != TT_ObjCMethodExpr) {
272 if (State.Stack.back().StartOfArraySubscripts != 0)
273 State.Column = State.Stack.back().StartOfArraySubscripts;
274 else
275 State.Column = ContinuationIndent;
276 } else if (Current.Type == TT_StartOfName ||
277 Previous.isOneOf(tok::coloncolon, tok::equal) ||
278 Previous.Type == TT_ObjCMethodExpr) {
279 State.Column = ContinuationIndent;
280 } else if (Current.Type == TT_CtorInitializerColon) {
281 State.Column = FirstIndent + Style.ConstructorInitializerIndentWidth;
282 } else if (Current.Type == TT_CtorInitializerComma) {
283 State.Column = State.Stack.back().Indent;
284 } else {
285 State.Column = State.Stack.back().Indent;
286 // Ensure that we fall back to indenting 4 spaces instead of just
287 // flushing continuations left.
288 if (State.Column == FirstIndent)
289 State.Column += 4;
290 }
291
292 if (Current.is(tok::question))
293 State.Stack.back().BreakBeforeParameter = true;
294 if ((Previous.isOneOf(tok::comma, tok::semi) &&
295 !State.Stack.back().AvoidBinPacking) ||
296 Previous.Type == TT_BinaryOperator)
297 State.Stack.back().BreakBeforeParameter = false;
298 if (Previous.Type == TT_TemplateCloser && State.ParenLevel == 0)
299 State.Stack.back().BreakBeforeParameter = false;
300
301 if (!DryRun) {
302 unsigned NewLines = 1;
303 if (Current.is(tok::comment))
304 NewLines = std::max(NewLines, std::min(Current.NewlinesBefore,
305 Style.MaxEmptyLinesToKeep + 1));
306 Whitespaces.replaceWhitespace(Current, NewLines, State.Column,
307 State.Column, Line.InPPDirective);
308 }
309
310 if (!Current.isTrailingComment())
311 State.Stack.back().LastSpace = State.Column;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000312 if (Current.isMemberAccess())
Daniel Jasperde0328a2013-08-16 11:20:30 +0000313 State.Stack.back().LastSpace += Current.CodePointCount;
314 State.StartOfLineLevel = State.ParenLevel;
315 State.LowestLevelOnLine = State.ParenLevel;
316
317 // Any break on this level means that the parent level has been broken
318 // and we need to avoid bin packing there.
319 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) {
320 State.Stack[i].BreakBeforeParameter = true;
321 }
322 const FormatToken *TokenBefore = Current.getPreviousNonComment();
323 if (TokenBefore && !TokenBefore->isOneOf(tok::comma, tok::semi) &&
324 TokenBefore->Type != TT_TemplateCloser &&
325 TokenBefore->Type != TT_BinaryOperator && !TokenBefore->opensScope())
326 State.Stack.back().BreakBeforeParameter = true;
327
328 // If we break after {, we should also break before the corresponding }.
329 if (Previous.is(tok::l_brace))
330 State.Stack.back().BreakBeforeClosingBrace = true;
331
332 if (State.Stack.back().AvoidBinPacking) {
333 // If we are breaking after '(', '{', '<', this is not bin packing
334 // unless AllowAllParametersOfDeclarationOnNextLine is false.
335 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) ||
336 Previous.Type == TT_BinaryOperator) ||
337 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
338 Line.MustBeDeclaration))
339 State.Stack.back().BreakBeforeParameter = true;
340 }
341
342 } else {
343 if (Current.is(tok::equal) &&
344 (Line.First->is(tok::kw_for) || State.ParenLevel == 0) &&
345 State.Stack.back().VariablePos == 0) {
346 State.Stack.back().VariablePos = State.Column;
347 // Move over * and & if they are bound to the variable name.
348 const FormatToken *Tok = &Previous;
349 while (Tok && State.Stack.back().VariablePos >= Tok->CodePointCount) {
350 State.Stack.back().VariablePos -= Tok->CodePointCount;
351 if (Tok->SpacesRequiredBefore != 0)
352 break;
353 Tok = Tok->Previous;
354 }
355 if (Previous.PartOfMultiVariableDeclStmt)
356 State.Stack.back().LastSpace = State.Stack.back().VariablePos;
357 }
358
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000359 unsigned Spaces = State.NextToken->SpacesRequiredBefore + ExtraSpaces;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000360
361 if (!DryRun)
362 Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column + Spaces);
363
364 if (Current.Type == TT_ObjCSelectorName &&
365 State.Stack.back().ColonPos == 0) {
366 if (State.Stack.back().Indent + Current.LongestObjCSelectorName >
367 State.Column + Spaces + Current.CodePointCount)
368 State.Stack.back().ColonPos =
369 State.Stack.back().Indent + Current.LongestObjCSelectorName;
370 else
371 State.Stack.back().ColonPos =
372 State.Column + Spaces + Current.CodePointCount;
373 }
374
375 if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr &&
376 Current.Type != TT_LineComment)
377 State.Stack.back().Indent = State.Column + Spaces;
378 if (Previous.is(tok::comma) && !Current.isTrailingComment() &&
379 State.Stack.back().AvoidBinPacking)
380 State.Stack.back().NoLineBreak = true;
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000381 if (startsSegmentOfBuilderTypeCall(Current))
382 State.Stack.back().ContainsUnwrappedBuilder = true;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000383
384 State.Column += Spaces;
385 if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
386 // Treat the condition inside an if as if it was a second function
387 // parameter, i.e. let nested calls have an indent of 4.
388 State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
389 else if (Previous.is(tok::comma))
390 State.Stack.back().LastSpace = State.Column;
391 else if ((Previous.Type == TT_BinaryOperator ||
392 Previous.Type == TT_ConditionalExpr ||
Daniel Jasperf110e202013-08-21 08:39:01 +0000393 Previous.Type == TT_UnaryOperator ||
Daniel Jasperde0328a2013-08-16 11:20:30 +0000394 Previous.Type == TT_CtorInitializerColon) &&
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000395 (Previous.getPrecedence() != prec::Assignment ||
396 startsBinaryExpression(Current)))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000397 // Always indent relative to the RHS of the expression unless this is a
Daniel Jasperf110e202013-08-21 08:39:01 +0000398 // simple assignment without binary expression on the RHS. Also indent
399 // relative to unary operators and the colons of constructor initializers.
Daniel Jasperde0328a2013-08-16 11:20:30 +0000400 State.Stack.back().LastSpace = State.Column;
401 else if (Previous.Type == TT_InheritanceColon)
402 State.Stack.back().Indent = State.Column;
403 else if (Previous.opensScope()) {
404 // If a function has multiple parameters (including a single parameter
405 // that is a binary expression) or a trailing call, indent all
406 // parameters from the opening parenthesis. This avoids confusing
407 // indents like:
408 // OuterFunction(InnerFunctionCall(
409 // ParameterToInnerFunction),
410 // SecondParameterToOuterFunction);
411 bool HasMultipleParameters = !Current.FakeLParens.empty();
412 bool HasTrailingCall = false;
413 if (Previous.MatchingParen) {
414 const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000415 HasTrailingCall = Next && Next->isMemberAccess();
Daniel Jasperde0328a2013-08-16 11:20:30 +0000416 }
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000417 if (HasMultipleParameters ||
418 (HasTrailingCall &&
419 State.Stack[State.Stack.size() - 2].CallContinuation == 0))
Daniel Jasperde0328a2013-08-16 11:20:30 +0000420 State.Stack.back().LastSpace = State.Column;
421 }
422 }
423
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000424 return moveStateToNextToken(State, DryRun, Newline) + Penalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000425}
426
427unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
428 bool DryRun, bool Newline) {
429 const FormatToken &Current = *State.NextToken;
430 assert(State.Stack.size());
431
432 if (Current.Type == TT_InheritanceColon)
433 State.Stack.back().AvoidBinPacking = true;
434 if (Current.is(tok::lessless) && State.Stack.back().FirstLessLess == 0)
435 State.Stack.back().FirstLessLess = State.Column;
436 if (Current.is(tok::l_square) &&
437 State.Stack.back().StartOfArraySubscripts == 0)
438 State.Stack.back().StartOfArraySubscripts = State.Column;
439 if (Current.is(tok::question))
440 State.Stack.back().QuestionColumn = State.Column;
441 if (!Current.opensScope() && !Current.closesScope())
442 State.LowestLevelOnLine =
443 std::min(State.LowestLevelOnLine, State.ParenLevel);
Daniel Jasper4c6e0052013-08-27 14:24:43 +0000444 if (Current.isMemberAccess())
Daniel Jasperde0328a2013-08-16 11:20:30 +0000445 State.Stack.back().StartOfFunctionCall =
446 Current.LastInChainOfCalls ? 0 : State.Column + Current.CodePointCount;
447 if (Current.Type == TT_CtorInitializerColon) {
448 // Indent 2 from the column, so:
449 // SomeClass::SomeClass()
450 // : First(...), ...
451 // Next(...)
452 // ^ line up here.
453 State.Stack.back().Indent =
454 State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
455 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
456 State.Stack.back().AvoidBinPacking = true;
457 State.Stack.back().BreakBeforeParameter = false;
458 }
459
460 // If return returns a binary expression, align after it.
461 if (Current.is(tok::kw_return) && !Current.FakeLParens.empty())
462 State.Stack.back().LastSpace = State.Column + 7;
463
464 // In ObjC method declaration we align on the ":" of parameters, but we need
465 // to ensure that we indent parameters on subsequent lines by at least 4.
466 if (Current.Type == TT_ObjCMethodSpecifier)
467 State.Stack.back().Indent += 4;
468
469 // Insert scopes created by fake parenthesis.
470 const FormatToken *Previous = Current.getPreviousNonComment();
471 // Don't add extra indentation for the first fake parenthesis after
472 // 'return', assignements or opening <({[. The indentation for these cases
473 // is special cased.
474 bool SkipFirstExtraIndent =
475 Current.is(tok::kw_return) ||
476 (Previous && (Previous->opensScope() ||
477 Previous->getPrecedence() == prec::Assignment));
478 for (SmallVectorImpl<prec::Level>::const_reverse_iterator
479 I = Current.FakeLParens.rbegin(),
480 E = Current.FakeLParens.rend();
481 I != E; ++I) {
482 ParenState NewParenState = State.Stack.back();
483 NewParenState.ContainsLineBreak = false;
484 NewParenState.Indent =
485 std::max(std::max(State.Column, NewParenState.Indent),
486 State.Stack.back().LastSpace);
487
488 // Always indent conditional expressions. Never indent expression where
489 // the 'operator' is ',', ';' or an assignment (i.e. *I <=
490 // prec::Assignment) as those have different indentation rules. Indent
491 // other expression, unless the indentation needs to be skipped.
492 if (*I == prec::Conditional ||
493 (!SkipFirstExtraIndent && *I > prec::Assignment &&
494 !Style.BreakBeforeBinaryOperators))
495 NewParenState.Indent += 4;
496 if (Previous && !Previous->opensScope())
497 NewParenState.BreakBeforeParameter = false;
498 State.Stack.push_back(NewParenState);
499 SkipFirstExtraIndent = false;
500 }
501
502 // If we encounter an opening (, [, { or <, we add a level to our stacks to
503 // prepare for the following tokens.
504 if (Current.opensScope()) {
505 unsigned NewIndent;
506 unsigned LastSpace = State.Stack.back().LastSpace;
507 bool AvoidBinPacking;
508 if (Current.is(tok::l_brace)) {
509 NewIndent =
510 LastSpace + (Style.Cpp11BracedListStyle ? 4 : Style.IndentWidth);
511 const FormatToken *NextNoComment = Current.getNextNonComment();
512 AvoidBinPacking = NextNoComment &&
513 NextNoComment->Type == TT_DesignatedInitializerPeriod;
514 } else {
515 NewIndent =
516 4 + std::max(LastSpace, State.Stack.back().StartOfFunctionCall);
517 AvoidBinPacking = !Style.BinPackParameters ||
518 (Style.ExperimentalAutoDetectBinPacking &&
519 (Current.PackingKind == PPK_OnePerLine ||
520 (!BinPackInconclusiveFunctions &&
521 Current.PackingKind == PPK_Inconclusive)));
522 }
523
524 State.Stack.push_back(ParenState(NewIndent, LastSpace, AvoidBinPacking,
525 State.Stack.back().NoLineBreak));
526 ++State.ParenLevel;
527 }
528
529 // If this '[' opens an ObjC call, determine whether all parameters fit into
530 // one line and put one per line if they don't.
531 if (Current.is(tok::l_square) && Current.Type == TT_ObjCMethodExpr &&
532 Current.MatchingParen != NULL) {
533 if (getLengthToMatchingParen(Current) + State.Column > getColumnLimit())
534 State.Stack.back().BreakBeforeParameter = true;
535 }
536
537 // If we encounter a closing ), ], } or >, we can remove a level from our
538 // stacks.
Daniel Jasper96df37a2013-08-28 09:17:37 +0000539 if (State.Stack.size() > 1 &&
540 (Current.isOneOf(tok::r_paren, tok::r_square) ||
541 (Current.is(tok::r_brace) && State.NextToken != Line.First) ||
542 State.NextToken->Type == TT_TemplateCloser)) {
Daniel Jasperde0328a2013-08-16 11:20:30 +0000543 State.Stack.pop_back();
544 --State.ParenLevel;
545 }
546 if (Current.is(tok::r_square)) {
547 // If this ends the array subscript expr, reset the corresponding value.
548 const FormatToken *NextNonComment = Current.getNextNonComment();
549 if (NextNonComment && NextNonComment->isNot(tok::l_square))
550 State.Stack.back().StartOfArraySubscripts = 0;
551 }
552
553 // Remove scopes created by fake parenthesis.
554 for (unsigned i = 0, e = Current.FakeRParens; i != e; ++i) {
555 unsigned VariablePos = State.Stack.back().VariablePos;
556 State.Stack.pop_back();
557 State.Stack.back().VariablePos = VariablePos;
558 }
559
560 if (Current.is(tok::string_literal) && State.StartOfStringLiteral == 0) {
561 State.StartOfStringLiteral = State.Column;
562 } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash,
563 tok::string_literal)) {
564 State.StartOfStringLiteral = 0;
565 }
566
567 State.Column += Current.CodePointCount;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000568 State.NextToken = State.NextToken->Next;
Daniel Jasperb27c4b72013-08-27 11:09:05 +0000569 unsigned Penalty = breakProtrudingToken(Current, State, DryRun);
Daniel Jasperde0328a2013-08-16 11:20:30 +0000570
Daniel Jasper8de9ed02013-08-22 15:00:41 +0000571 // If the previous has a special role, let it consume tokens as appropriate.
572 // It is necessary to start at the previous token for the only implemented
573 // role (comma separated list). That way, the decision whether or not to break
574 // after the "{" is already done and both options are tried and evaluated.
575 // FIXME: This is ugly, find a better way.
576 if (Previous && Previous->Role)
577 Penalty += Previous->Role->format(State, this, DryRun);
578
579 return Penalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000580}
581
582unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
583 LineState &State,
584 bool DryRun) {
Daniel Jasperf93551c2013-08-23 10:05:49 +0000585 if (!Current.isOneOf(tok::string_literal, tok::comment))
586 return 0;
587
Daniel Jasperde0328a2013-08-16 11:20:30 +0000588 llvm::OwningPtr<BreakableToken> Token;
589 unsigned StartColumn = State.Column - Current.CodePointCount;
590 unsigned OriginalStartColumn =
591 SourceMgr.getSpellingColumnNumber(Current.getStartOfNonWhitespace()) - 1;
592
593 if (Current.is(tok::string_literal) &&
594 Current.Type != TT_ImplicitStringLiteral) {
595 // Only break up default narrow strings.
596 if (!Current.TokenText.startswith("\""))
597 return 0;
598 // Don't break string literals with escaped newlines. As clang-format must
599 // not change the string's content, it is unlikely that we'll end up with
600 // a better format.
601 if (Current.TokenText.find("\\\n") != StringRef::npos)
602 return 0;
603 // Exempts unterminated string literals from line breaking. The user will
604 // likely want to terminate the string before any line breaking is done.
605 if (Current.IsUnterminatedLiteral)
606 return 0;
607
608 Token.reset(new BreakableStringLiteral(Current, StartColumn,
609 Line.InPPDirective, Encoding));
610 } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) {
611 Token.reset(new BreakableBlockComment(
612 Style, Current, StartColumn, OriginalStartColumn, !Current.Previous,
613 Line.InPPDirective, Encoding));
614 } else if (Current.Type == TT_LineComment &&
615 (Current.Previous == NULL ||
616 Current.Previous->Type != TT_ImplicitStringLiteral)) {
617 // Don't break line comments with escaped newlines. These look like
618 // separate line comments, but in fact contain a single line comment with
619 // multiple lines including leading whitespace and the '//' markers.
620 //
621 // FIXME: If we want to handle them correctly, we'll need to adjust
622 // leading whitespace in consecutive lines when changing indentation of
623 // the first line similar to what we do with block comments.
624 StringRef::size_type EscapedNewlinePos = Current.TokenText.find("\\\n");
625 if (EscapedNewlinePos != StringRef::npos) {
626 State.Column =
627 StartColumn +
628 encoding::getCodePointCount(
629 Current.TokenText.substr(0, EscapedNewlinePos), Encoding) +
630 1;
631 return 0;
632 }
633
634 Token.reset(new BreakableLineComment(Current, StartColumn,
635 Line.InPPDirective, Encoding));
636 } else {
637 return 0;
638 }
639 if (Current.UnbreakableTailLength >= getColumnLimit())
640 return 0;
641
642 unsigned RemainingSpace = getColumnLimit() - Current.UnbreakableTailLength;
643 bool BreakInserted = false;
644 unsigned Penalty = 0;
645 unsigned RemainingTokenColumns = 0;
646 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
647 LineIndex != EndIndex; ++LineIndex) {
648 if (!DryRun)
649 Token->replaceWhitespaceBefore(LineIndex, Whitespaces);
650 unsigned TailOffset = 0;
651 RemainingTokenColumns =
652 Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos);
653 while (RemainingTokenColumns > RemainingSpace) {
654 BreakableToken::Split Split =
655 Token->getSplit(LineIndex, TailOffset, getColumnLimit());
656 if (Split.first == StringRef::npos) {
657 // The last line's penalty is handled in addNextStateToQueue().
658 if (LineIndex < EndIndex - 1)
659 Penalty += Style.PenaltyExcessCharacter *
660 (RemainingTokenColumns - RemainingSpace);
661 break;
662 }
663 assert(Split.first != 0);
664 unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit(
665 LineIndex, TailOffset + Split.first + Split.second, StringRef::npos);
666 assert(NewRemainingTokenColumns < RemainingTokenColumns);
667 if (!DryRun)
668 Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
Daniel Jasper2739af32013-08-28 10:03:58 +0000669 Penalty += Current.SplitPenalty;
Daniel Jasperde0328a2013-08-16 11:20:30 +0000670 unsigned ColumnsUsed =
671 Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first);
672 if (ColumnsUsed > getColumnLimit()) {
673 Penalty +=
674 Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit());
675 }
676 TailOffset += Split.first + Split.second;
677 RemainingTokenColumns = NewRemainingTokenColumns;
678 BreakInserted = true;
679 }
680 }
681
682 State.Column = RemainingTokenColumns;
683
684 if (BreakInserted) {
685 // If we break the token inside a parameter list, we need to break before
686 // the next parameter on all levels, so that the next parameter is clearly
687 // visible. Line comments already introduce a break.
688 if (Current.Type != TT_LineComment) {
689 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
690 State.Stack[i].BreakBeforeParameter = true;
691 }
692
Daniel Jasper2739af32013-08-28 10:03:58 +0000693 Penalty += Current.is(tok::string_literal) ? Style.PenaltyBreakString
694 : Style.PenaltyBreakComment;
695
Daniel Jasperde0328a2013-08-16 11:20:30 +0000696 State.Stack.back().LastSpace = StartColumn;
697 }
698 return Penalty;
699}
700
701unsigned ContinuationIndenter::getColumnLimit() const {
702 // In preprocessor directives reserve two chars for trailing " \"
703 return Style.ColumnLimit - (Line.InPPDirective ? 2 : 0);
704}
705
Daniel Jasperf438cb72013-08-23 11:57:34 +0000706bool ContinuationIndenter::NextIsMultilineString(const LineState &State) {
707 const FormatToken &Current = *State.NextToken;
708 if (!Current.is(tok::string_literal))
709 return false;
710 if (Current.getNextNonComment() &&
711 Current.getNextNonComment()->is(tok::string_literal))
712 return true; // Implicit concatenation.
713 if (State.Column + Current.CodePointCount + Current.UnbreakableTailLength >
714 Style.ColumnLimit)
715 return true; // String will be split.
716 // String literal might have escaped newlines.
717 return Current.TokenText.find("\\\n") != StringRef::npos;
718}
719
Daniel Jasperde0328a2013-08-16 11:20:30 +0000720} // namespace format
721} // namespace clang