1 //===--- ContinuationIndenter.cpp - Format C++ code -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements the continuation indenter.
13 //===----------------------------------------------------------------------===//
15 #include "ContinuationIndenter.h"
16 #include "BreakableToken.h"
17 #include "FormatInternal.h"
18 #include "WhitespaceManager.h"
19 #include "clang/Basic/OperatorPrecedence.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Format/Format.h"
22 #include "llvm/Support/Debug.h"
24 #define DEBUG_TYPE "format-indenter"
29 // Returns the length of everything up to the first possible line break after
30 // the ), ], } or > matching \c Tok.
31 static unsigned getLengthToMatchingParen(const FormatToken &Tok) {
32 if (!Tok.MatchingParen)
34 FormatToken *End = Tok.MatchingParen;
35 while (End->Next && !End->Next->CanBreakBefore) {
38 return End->TotalLength - Tok.TotalLength + 1;
41 static unsigned getLengthToNextOperator(const FormatToken &Tok) {
42 if (!Tok.NextOperator)
44 return Tok.NextOperator->TotalLength - Tok.TotalLength;
47 // Returns \c true if \c Tok is the "." or "->" of a call and starts the next
48 // segment of a builder type call.
49 static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
50 return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
53 // Returns \c true if \c Current starts a new parameter.
54 static bool startsNextParameter(const FormatToken &Current,
55 const FormatStyle &Style) {
56 const FormatToken &Previous = *Current.Previous;
57 if (Current.is(TT_CtorInitializerComma) &&
58 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma)
60 if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
62 return Previous.is(tok::comma) && !Current.isTrailingComment() &&
63 ((Previous.isNot(TT_CtorInitializerComma) ||
64 Style.BreakConstructorInitializers !=
65 FormatStyle::BCIS_BeforeComma) &&
66 (Previous.isNot(TT_InheritanceComma) ||
67 !Style.BreakBeforeInheritanceComma));
70 static bool opensProtoMessageField(const FormatToken &LessTok,
71 const FormatStyle &Style) {
72 if (LessTok.isNot(tok::less))
74 return Style.Language == FormatStyle::LK_TextProto ||
75 (Style.Language == FormatStyle::LK_Proto &&
76 (LessTok.NestingLevel > 0 ||
77 (LessTok.Previous && LessTok.Previous->is(tok::equal))));
80 // Returns the delimiter of a raw string literal, or None if TokenText is not
81 // the text of a raw string literal. The delimiter could be the empty string.
82 // For example, the delimiter of R"deli(cont)deli" is deli.
83 static llvm::Optional<StringRef> getRawStringDelimiter(StringRef TokenText) {
84 if (TokenText.size() < 5 // The smallest raw string possible is 'R"()"'.
85 || !TokenText.startswith("R\"") || !TokenText.endswith("\""))
88 // A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has
89 // size at most 16 by the standard, so the first '(' must be among the first
91 size_t LParenPos = TokenText.substr(0, 19).find_first_of('(');
92 if (LParenPos == StringRef::npos)
94 StringRef Delimiter = TokenText.substr(2, LParenPos - 2);
96 // Check that the string ends in ')Delimiter"'.
97 size_t RParenPos = TokenText.size() - Delimiter.size() - 2;
98 if (TokenText[RParenPos] != ')')
100 if (!TokenText.substr(RParenPos + 1).startswith(Delimiter))
105 RawStringFormatStyleManager::RawStringFormatStyleManager(
106 const FormatStyle &CodeStyle) {
107 for (const auto &RawStringFormat : CodeStyle.RawStringFormats) {
109 if (!getPredefinedStyle(RawStringFormat.BasedOnStyle,
110 RawStringFormat.Language, &Style)) {
111 Style = getLLVMStyle();
112 Style.Language = RawStringFormat.Language;
114 Style.ColumnLimit = CodeStyle.ColumnLimit;
115 DelimiterStyle.insert({RawStringFormat.Delimiter, Style});
119 llvm::Optional<FormatStyle>
120 RawStringFormatStyleManager::get(StringRef Delimiter) const {
121 auto It = DelimiterStyle.find(Delimiter);
122 if (It == DelimiterStyle.end())
127 ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style,
128 const AdditionalKeywords &Keywords,
129 const SourceManager &SourceMgr,
130 WhitespaceManager &Whitespaces,
131 encoding::Encoding Encoding,
132 bool BinPackInconclusiveFunctions)
133 : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr),
134 Whitespaces(Whitespaces), Encoding(Encoding),
135 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions),
136 CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {}
138 LineState ContinuationIndenter::getInitialState(unsigned FirstIndent,
139 unsigned FirstStartColumn,
140 const AnnotatedLine *Line,
143 State.FirstIndent = FirstIndent;
144 if (FirstStartColumn && Line->First->NewlinesBefore == 0)
145 State.Column = FirstStartColumn;
147 State.Column = FirstIndent;
148 // With preprocessor directive indentation, the line starts on column 0
149 // since it's indented after the hash, but FirstIndent is set to the
150 // preprocessor indent.
151 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
152 (Line->Type == LT_PreprocessorDirective ||
153 Line->Type == LT_ImportStatement))
156 State.NextToken = Line->First;
157 State.Stack.push_back(ParenState(FirstIndent, FirstIndent,
158 /*AvoidBinPacking=*/false,
159 /*NoLineBreak=*/false));
160 State.LineContainsContinuedForLoopSection = false;
161 State.NoContinuation = false;
162 State.StartOfStringLiteral = 0;
163 State.StartOfLineLevel = 0;
164 State.LowestLevelOnLine = 0;
165 State.IgnoreStackForComparison = false;
167 if (Style.Language == FormatStyle::LK_TextProto) {
168 // We need this in order to deal with the bin packing of text fields at
170 State.Stack.back().AvoidBinPacking = true;
171 State.Stack.back().BreakBeforeParameter = true;
174 // The first token has already been indented and thus consumed.
175 moveStateToNextToken(State, DryRun, /*Newline=*/false);
179 bool ContinuationIndenter::canBreak(const LineState &State) {
180 const FormatToken &Current = *State.NextToken;
181 const FormatToken &Previous = *Current.Previous;
182 assert(&Previous == Current.Previous);
183 if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace &&
184 Current.closesBlockOrBlockTypeList(Style)))
186 // The opening "{" of a braced list has to be on the same line as the first
187 // element if it is nested in another braced init list or function call.
188 if (!Current.MustBreakBefore && Previous.is(tok::l_brace) &&
189 Previous.isNot(TT_DictLiteral) && Previous.BlockKind == BK_BracedInit &&
191 Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma))
193 // This prevents breaks like:
195 // SomeParameter, OtherParameter).DoSomething(
197 // As they hide "DoSomething" and are generally bad for readability.
198 if (Previous.opensScope() && Previous.isNot(tok::l_brace) &&
199 State.LowestLevelOnLine < State.StartOfLineLevel &&
200 State.LowestLevelOnLine < Current.NestingLevel)
202 if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder)
205 // Don't create a 'hanging' indent if there are multiple blocks in a single
207 if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
208 State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
209 State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks)
212 // Don't break after very short return types (e.g. "void") as that is often
214 if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) {
215 if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None)
219 // If binary operators are moved to the next line (including commas for some
220 // styles of constructor initializers), that's always ok.
221 if (!Current.isOneOf(TT_BinaryOperator, tok::comma) &&
222 State.Stack.back().NoLineBreakInOperand)
225 return !State.Stack.back().NoLineBreak;
228 bool ContinuationIndenter::mustBreak(const LineState &State) {
229 const FormatToken &Current = *State.NextToken;
230 const FormatToken &Previous = *Current.Previous;
231 if (Current.MustBreakBefore || Current.is(TT_InlineASMColon))
233 if (State.Stack.back().BreakBeforeClosingBrace &&
234 Current.closesBlockOrBlockTypeList(Style))
236 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection)
238 if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
239 (Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
241 // FIXME: This is a temporary workaround for the case where clang-format
242 // sets BreakBeforeParameter to avoid bin packing and this creates a
243 // completely unnecessary line break after a template type that isn't
245 (Previous.NestingLevel == 1 || Style.BinPackParameters)) ||
246 (Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
247 Previous.isNot(tok::question)) ||
248 (!Style.BreakBeforeTernaryOperators &&
249 Previous.is(TT_ConditionalExpr))) &&
250 State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() &&
251 !Current.isOneOf(tok::r_paren, tok::r_brace))
253 if (((Previous.is(TT_DictLiteral) && Previous.is(tok::l_brace)) ||
254 (Previous.is(TT_ArrayInitializerLSquare) &&
255 Previous.ParameterCount > 1) ||
256 opensProtoMessageField(Previous, Style)) &&
257 Style.ColumnLimit > 0 &&
258 getLengthToMatchingParen(Previous) + State.Column - 1 >
259 getColumnLimit(State))
262 const FormatToken &BreakConstructorInitializersToken =
263 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon
266 if (BreakConstructorInitializersToken.is(TT_CtorInitializerColon) &&
267 (State.Column + State.Line->Last->TotalLength - Previous.TotalLength >
268 getColumnLimit(State) ||
269 State.Stack.back().BreakBeforeParameter) &&
270 (Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All ||
271 Style.BreakConstructorInitializers != FormatStyle::BCIS_BeforeColon ||
272 Style.ColumnLimit != 0))
275 if (Current.is(TT_ObjCMethodExpr) && !Previous.is(TT_SelectorName) &&
276 State.Line->startsWith(TT_ObjCMethodSpecifier))
278 if (Current.is(TT_SelectorName) && State.Stack.back().ObjCSelectorNameFound &&
279 State.Stack.back().BreakBeforeParameter)
282 unsigned NewLineColumn = getNewLineColumn(State);
283 if (Current.isMemberAccess() && Style.ColumnLimit != 0 &&
284 State.Column + getLengthToNextOperator(Current) > Style.ColumnLimit &&
285 (State.Column > NewLineColumn ||
286 Current.NestingLevel < State.StartOfLineLevel))
289 if (startsSegmentOfBuilderTypeCall(Current) &&
290 (State.Stack.back().CallContinuation != 0 ||
291 State.Stack.back().BreakBeforeParameter) &&
292 // JavaScript is treated different here as there is a frequent pattern:
293 // SomeFunction(function() {
296 // FIXME: We should find a more generic solution to this problem.
297 !(State.Column <= NewLineColumn &&
298 Style.Language == FormatStyle::LK_JavaScript))
301 if (State.Column <= NewLineColumn)
304 if (Style.AlwaysBreakBeforeMultilineStrings &&
305 (NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth ||
306 Previous.is(tok::comma) || Current.NestingLevel < 2) &&
307 !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) &&
308 !Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) &&
309 nextIsMultilineString(State))
312 // Using CanBreakBefore here and below takes care of the decision whether the
313 // current style uses wrapping before or after operators for the given
315 if (Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) {
316 // If we need to break somewhere inside the LHS of a binary expression, we
317 // should also break after the operator. Otherwise, the formatting would
318 // hide the operator precedence, e.g. in:
319 // if (aaaaaaaaaaaaaa ==
320 // bbbbbbbbbbbbbb && c) {..
321 // For comparisons, we only apply this rule, if the LHS is a binary
322 // expression itself as otherwise, the line breaks seem superfluous.
323 // We need special cases for ">>" which we have split into two ">" while
324 // lexing in order to make template parsing easier.
325 bool IsComparison = (Previous.getPrecedence() == prec::Relational ||
326 Previous.getPrecedence() == prec::Equality ||
327 Previous.getPrecedence() == prec::Spaceship) &&
329 Previous.Previous->isNot(TT_BinaryOperator); // For >>.
330 bool LHSIsBinaryExpr =
331 Previous.Previous && Previous.Previous->EndsBinaryExpression;
332 if ((!IsComparison || LHSIsBinaryExpr) && !Current.isTrailingComment() &&
333 Previous.getPrecedence() != prec::Assignment &&
334 State.Stack.back().BreakBeforeParameter)
336 } else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore &&
337 State.Stack.back().BreakBeforeParameter) {
341 // Same as above, but for the first "<<" operator.
342 if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator) &&
343 State.Stack.back().BreakBeforeParameter &&
344 State.Stack.back().FirstLessLess == 0)
347 if (Current.NestingLevel == 0 && !Current.isTrailingComment()) {
348 // Always break after "template <...>" and leading annotations. This is only
349 // for cases where the entire line does not fit on a single line as a
350 // different LineFormatter would be used otherwise.
351 if (Previous.ClosesTemplateDeclaration)
353 if (Previous.is(TT_FunctionAnnotationRParen))
355 if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) &&
356 Current.isNot(TT_LeadingJavaAnnotation))
360 // If the return type spans multiple lines, wrap before the function name.
361 if ((Current.is(TT_FunctionDeclarationName) ||
362 (Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) &&
363 !Previous.is(tok::kw_template) && State.Stack.back().BreakBeforeParameter)
366 // The following could be precomputed as they do not depend on the state.
367 // However, as they should take effect only if the UnwrappedLine does not fit
368 // into the ColumnLimit, they are checked here in the ContinuationIndenter.
369 if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block &&
370 Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment))
373 if (Current.is(tok::lessless) &&
374 ((Previous.is(tok::identifier) && Previous.TokenText == "endl") ||
375 (Previous.Tok.isLiteral() && (Previous.TokenText.endswith("\\n\"") ||
376 Previous.TokenText == "\'\\n\'"))))
379 if (Previous.is(TT_BlockComment) && Previous.IsMultiline)
382 if (State.NoContinuation)
388 unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
390 unsigned ExtraSpaces) {
391 const FormatToken &Current = *State.NextToken;
393 assert(!State.Stack.empty());
394 State.NoContinuation = false;
396 if ((Current.is(TT_ImplicitStringLiteral) &&
397 (Current.Previous->Tok.getIdentifierInfo() == nullptr ||
398 Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() ==
399 tok::pp_not_keyword))) {
401 SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd());
402 if (Current.LastNewlineOffset != 0) {
403 // If there is a newline within this token, the final column will solely
404 // determined by the current end column.
405 State.Column = EndColumn;
407 unsigned StartColumn =
408 SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getBegin());
409 assert(EndColumn >= StartColumn);
410 State.Column += EndColumn - StartColumn;
412 moveStateToNextToken(State, DryRun, /*Newline=*/false);
416 unsigned Penalty = 0;
418 Penalty = addTokenOnNewLine(State, DryRun);
420 addTokenOnCurrentLine(State, DryRun, ExtraSpaces);
422 return moveStateToNextToken(State, DryRun, Newline) + Penalty;
425 void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
426 unsigned ExtraSpaces) {
427 FormatToken &Current = *State.NextToken;
428 const FormatToken &Previous = *State.NextToken->Previous;
429 if (Current.is(tok::equal) &&
430 (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) &&
431 State.Stack.back().VariablePos == 0) {
432 State.Stack.back().VariablePos = State.Column;
433 // Move over * and & if they are bound to the variable name.
434 const FormatToken *Tok = &Previous;
435 while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) {
436 State.Stack.back().VariablePos -= Tok->ColumnWidth;
437 if (Tok->SpacesRequiredBefore != 0)
441 if (Previous.PartOfMultiVariableDeclStmt)
442 State.Stack.back().LastSpace = State.Stack.back().VariablePos;
445 unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces;
447 // Indent preprocessor directives after the hash if required.
448 int PPColumnCorrection = 0;
449 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash &&
450 Previous.is(tok::hash) && State.FirstIndent > 0 &&
451 (State.Line->Type == LT_PreprocessorDirective ||
452 State.Line->Type == LT_ImportStatement)) {
453 Spaces += State.FirstIndent;
455 // For preprocessor indent with tabs, State.Column will be 1 because of the
456 // hash. This causes second-level indents onward to have an extra space
457 // after the tabs. We avoid this misalignment by subtracting 1 from the
458 // column value passed to replaceWhitespace().
459 if (Style.UseTab != FormatStyle::UT_Never)
460 PPColumnCorrection = -1;
464 Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces,
465 State.Column + Spaces + PPColumnCorrection);
467 // If "BreakBeforeInheritanceComma" mode, don't break within the inheritance
468 // declaration unless there is multiple inheritance.
469 if (Style.BreakBeforeInheritanceComma && Current.is(TT_InheritanceColon))
470 State.Stack.back().NoLineBreak = true;
472 if (Current.is(TT_SelectorName) &&
473 !State.Stack.back().ObjCSelectorNameFound) {
475 std::max(State.FirstIndent + Style.ContinuationIndentWidth,
476 State.Stack.back().Indent);
477 unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth;
478 if (Current.LongestObjCSelectorName == 0)
479 State.Stack.back().AlignColons = false;
480 else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos)
481 State.Stack.back().ColonPos = MinIndent + Current.LongestObjCSelectorName;
483 State.Stack.back().ColonPos = FirstColonPos;
486 // In "AlwaysBreak" mode, enforce wrapping directly after the parenthesis by
487 // disallowing any further line breaks if there is no line break after the
488 // opening parenthesis. Don't break if it doesn't conserve columns.
489 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak &&
490 Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
491 State.Column > getNewLineColumn(State) &&
492 (!Previous.Previous || !Previous.Previous->isOneOf(
493 tok::kw_for, tok::kw_while, tok::kw_switch)) &&
494 // Don't do this for simple (no expressions) one-argument function calls
495 // as that feels like needlessly wasting whitespace, e.g.:
500 // caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa))));
501 Current.FakeLParens.size() > 0 &&
502 Current.FakeLParens.back() > prec::Unknown)
503 State.Stack.back().NoLineBreak = true;
504 if (Previous.is(TT_TemplateString) && Previous.opensScope())
505 State.Stack.back().NoLineBreak = true;
507 if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign &&
508 Previous.opensScope() && Previous.isNot(TT_ObjCMethodExpr) &&
509 (Current.isNot(TT_LineComment) || Previous.BlockKind == BK_BracedInit))
510 State.Stack.back().Indent = State.Column + Spaces;
511 if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style))
512 State.Stack.back().NoLineBreak = true;
513 if (startsSegmentOfBuilderTypeCall(Current) &&
514 State.Column > getNewLineColumn(State))
515 State.Stack.back().ContainsUnwrappedBuilder = true;
517 if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java)
518 State.Stack.back().NoLineBreak = true;
519 if (Current.isMemberAccess() && Previous.is(tok::r_paren) &&
520 (Previous.MatchingParen &&
521 (Previous.TotalLength - Previous.MatchingParen->TotalLength > 10)))
522 // If there is a function call with long parameters, break before trailing
523 // calls. This prevents things like:
524 // EXPECT_CALL(SomeLongParameter).Times(
526 // We don't want to do this for short parameters as they can just be
528 State.Stack.back().NoLineBreak = true;
530 // Don't allow the RHS of an operator to be split over multiple lines unless
531 // there is a line-break right after the operator.
532 // Exclude relational operators, as there, it is always more desirable to
533 // have the LHS 'left' of the RHS.
534 const FormatToken *P = Current.getPreviousNonComment();
535 if (!Current.is(tok::comment) && P &&
536 (P->isOneOf(TT_BinaryOperator, tok::comma) ||
537 (P->is(TT_ConditionalExpr) && P->is(tok::colon))) &&
538 !P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) &&
539 P->getPrecedence() != prec::Assignment &&
540 P->getPrecedence() != prec::Relational &&
541 P->getPrecedence() != prec::Spaceship) {
542 bool BreakBeforeOperator =
543 P->MustBreakBefore || P->is(tok::lessless) ||
544 (P->is(TT_BinaryOperator) &&
545 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None) ||
546 (P->is(TT_ConditionalExpr) && Style.BreakBeforeTernaryOperators);
547 // Don't do this if there are only two operands. In these cases, there is
548 // always a nice vertical separation between them and the extra line break
550 bool HasTwoOperands =
551 P->OperatorIndex == 0 && !P->NextOperator && !P->is(TT_ConditionalExpr);
552 if ((!BreakBeforeOperator && !(HasTwoOperands && Style.AlignOperands)) ||
553 (!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator))
554 State.Stack.back().NoLineBreakInOperand = true;
557 State.Column += Spaces;
558 if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) &&
560 (Previous.Previous->isOneOf(tok::kw_if, tok::kw_for) ||
561 Previous.Previous->endsSequence(tok::kw_constexpr, tok::kw_if))) {
562 // Treat the condition inside an if as if it was a second function
563 // parameter, i.e. let nested calls have a continuation indent.
564 State.Stack.back().LastSpace = State.Column;
565 State.Stack.back().NestedBlockIndent = State.Column;
566 } else if (!Current.isOneOf(tok::comment, tok::caret) &&
567 ((Previous.is(tok::comma) &&
568 !Previous.is(TT_OverloadedOperator)) ||
569 (Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) {
570 State.Stack.back().LastSpace = State.Column;
571 } else if (Previous.is(TT_CtorInitializerColon) &&
572 Style.BreakConstructorInitializers ==
573 FormatStyle::BCIS_AfterColon) {
574 State.Stack.back().Indent = State.Column;
575 State.Stack.back().LastSpace = State.Column;
576 } else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr,
577 TT_CtorInitializerColon)) &&
578 ((Previous.getPrecedence() != prec::Assignment &&
579 (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 ||
580 Previous.NextOperator)) ||
581 Current.StartsBinaryExpression)) {
582 // Indent relative to the RHS of the expression unless this is a simple
583 // assignment without binary expression on the RHS. Also indent relative to
584 // unary operators and the colons of constructor initializers.
585 State.Stack.back().LastSpace = State.Column;
586 } else if (Previous.is(TT_InheritanceColon)) {
587 State.Stack.back().Indent = State.Column;
588 State.Stack.back().LastSpace = State.Column;
589 } else if (Previous.opensScope()) {
590 // If a function has a trailing call, indent all parameters from the
591 // opening parenthesis. This avoids confusing indents like:
592 // OuterFunction(InnerFunctionCall( // break
593 // ParameterToInnerFunction)) // break
594 // .SecondInnerFunctionCall();
595 bool HasTrailingCall = false;
596 if (Previous.MatchingParen) {
597 const FormatToken *Next = Previous.MatchingParen->getNextNonComment();
598 HasTrailingCall = Next && Next->isMemberAccess();
600 if (HasTrailingCall && State.Stack.size() > 1 &&
601 State.Stack[State.Stack.size() - 2].CallContinuation == 0)
602 State.Stack.back().LastSpace = State.Column;
606 unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
608 FormatToken &Current = *State.NextToken;
609 const FormatToken &Previous = *State.NextToken->Previous;
611 // Extra penalty that needs to be added because of the way certain line
612 // breaks are chosen.
613 unsigned Penalty = 0;
615 const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
616 const FormatToken *NextNonComment = Previous.getNextNonComment();
618 NextNonComment = &Current;
619 // The first line break on any NestingLevel causes an extra penalty in order
620 // prefer similar line breaks.
621 if (!State.Stack.back().ContainsLineBreak)
623 State.Stack.back().ContainsLineBreak = true;
625 Penalty += State.NextToken->SplitPenalty;
627 // Breaking before the first "<<" is generally not desirable if the LHS is
628 // short. Also always add the penalty if the LHS is split over multiple lines
629 // to avoid unnecessary line breaks that just work around this penalty.
630 if (NextNonComment->is(tok::lessless) &&
631 State.Stack.back().FirstLessLess == 0 &&
632 (State.Column <= Style.ColumnLimit / 3 ||
633 State.Stack.back().BreakBeforeParameter))
634 Penalty += Style.PenaltyBreakFirstLessLess;
636 State.Column = getNewLineColumn(State);
638 // Indent nested blocks relative to this column, unless in a very specific
639 // JavaScript special case where:
641 // var loooooong_name =
646 // is common and should be formatted like a free-standing function. The same
647 // goes for wrapping before the lambda return type arrow.
648 if (!Current.is(TT_LambdaArrow) &&
649 (Style.Language != FormatStyle::LK_JavaScript ||
650 Current.NestingLevel != 0 || !PreviousNonComment ||
651 !PreviousNonComment->is(tok::equal) ||
652 !Current.isOneOf(Keywords.kw_async, Keywords.kw_function)))
653 State.Stack.back().NestedBlockIndent = State.Column;
655 if (NextNonComment->isMemberAccess()) {
656 if (State.Stack.back().CallContinuation == 0)
657 State.Stack.back().CallContinuation = State.Column;
658 } else if (NextNonComment->is(TT_SelectorName)) {
659 if (!State.Stack.back().ObjCSelectorNameFound) {
660 if (NextNonComment->LongestObjCSelectorName == 0) {
661 State.Stack.back().AlignColons = false;
663 State.Stack.back().ColonPos =
664 (Style.IndentWrappedFunctionNames
665 ? std::max(State.Stack.back().Indent,
666 State.FirstIndent + Style.ContinuationIndentWidth)
667 : State.Stack.back().Indent) +
668 NextNonComment->LongestObjCSelectorName;
670 } else if (State.Stack.back().AlignColons &&
671 State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) {
672 State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth;
674 } else if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
675 PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) {
676 // FIXME: This is hacky, find a better way. The problem is that in an ObjC
677 // method expression, the block should be aligned to the line starting it,
679 // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason
683 // Thus, we set LastSpace of the next higher NestingLevel, to which we move
684 // when we consume all of the "}"'s FakeRParens at the "{".
685 if (State.Stack.size() > 1)
686 State.Stack[State.Stack.size() - 2].LastSpace =
687 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
688 Style.ContinuationIndentWidth;
691 if ((PreviousNonComment &&
692 PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
693 !State.Stack.back().AvoidBinPacking) ||
694 Previous.is(TT_BinaryOperator))
695 State.Stack.back().BreakBeforeParameter = false;
696 if (Previous.isOneOf(TT_TemplateCloser, TT_JavaAnnotation) &&
697 Current.NestingLevel == 0)
698 State.Stack.back().BreakBeforeParameter = false;
699 if (NextNonComment->is(tok::question) ||
700 (PreviousNonComment && PreviousNonComment->is(tok::question)))
701 State.Stack.back().BreakBeforeParameter = true;
702 if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore)
703 State.Stack.back().BreakBeforeParameter = false;
706 unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1;
707 if (Current.is(tok::r_brace) && Current.MatchingParen &&
708 // Only strip trailing empty lines for l_braces that have children, i.e.
709 // for function expressions (lambdas, arrows, etc).
710 !Current.MatchingParen->Children.empty()) {
711 // lambdas and arrow functions are expressions, thus their r_brace is not
712 // on its own line, and thus not covered by UnwrappedLineFormatter's logic
713 // about removing empty lines on closing blocks. Special case them here.
714 MaxEmptyLinesToKeep = 1;
716 unsigned Newlines = std::max(
717 1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep));
718 bool ContinuePPDirective =
719 State.Line->InPPDirective && State.Line->Type != LT_ImportStatement;
720 Whitespaces.replaceWhitespace(Current, Newlines, State.Column, State.Column,
721 ContinuePPDirective);
724 if (!Current.isTrailingComment())
725 State.Stack.back().LastSpace = State.Column;
726 if (Current.is(tok::lessless))
727 // If we are breaking before a "<<", we always want to indent relative to
728 // RHS. This is necessary only for "<<", as we special-case it and don't
729 // always indent relative to the RHS.
730 State.Stack.back().LastSpace += 3; // 3 -> width of "<< ".
732 State.StartOfLineLevel = Current.NestingLevel;
733 State.LowestLevelOnLine = Current.NestingLevel;
735 // Any break on this level means that the parent level has been broken
736 // and we need to avoid bin packing there.
737 bool NestedBlockSpecialCase =
738 !Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
739 State.Stack[State.Stack.size() - 2].NestedBlockInlined;
740 if (!NestedBlockSpecialCase)
741 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
742 State.Stack[i].BreakBeforeParameter = true;
744 if (PreviousNonComment &&
745 !PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) &&
746 (PreviousNonComment->isNot(TT_TemplateCloser) ||
747 Current.NestingLevel != 0) &&
748 !PreviousNonComment->isOneOf(
749 TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
750 TT_LeadingJavaAnnotation) &&
751 Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope())
752 State.Stack.back().BreakBeforeParameter = true;
754 // If we break after { or the [ of an array initializer, we should also break
755 // before the corresponding } or ].
756 if (PreviousNonComment &&
757 (PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
758 opensProtoMessageField(*PreviousNonComment, Style)))
759 State.Stack.back().BreakBeforeClosingBrace = true;
761 if (State.Stack.back().AvoidBinPacking) {
762 // If we are breaking after '(', '{', '<', this is not bin packing
763 // unless AllowAllParametersOfDeclarationOnNextLine is false or this is a
764 // dict/object literal.
765 if (!Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
766 (!Style.AllowAllParametersOfDeclarationOnNextLine &&
767 State.Line->MustBeDeclaration) ||
768 Previous.is(TT_DictLiteral))
769 State.Stack.back().BreakBeforeParameter = true;
775 unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
776 if (!State.NextToken || !State.NextToken->Previous)
778 FormatToken &Current = *State.NextToken;
779 const FormatToken &Previous = *Current.Previous;
780 // If we are continuing an expression, we want to use the continuation indent.
781 unsigned ContinuationIndent =
782 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
783 Style.ContinuationIndentWidth;
784 const FormatToken *PreviousNonComment = Current.getPreviousNonComment();
785 const FormatToken *NextNonComment = Previous.getNextNonComment();
787 NextNonComment = &Current;
789 // Java specific bits.
790 if (Style.Language == FormatStyle::LK_Java &&
791 Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends))
792 return std::max(State.Stack.back().LastSpace,
793 State.Stack.back().Indent + Style.ContinuationIndentWidth);
795 if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block)
796 return Current.NestingLevel == 0 ? State.FirstIndent
797 : State.Stack.back().Indent;
798 if ((Current.isOneOf(tok::r_brace, tok::r_square) ||
799 (Current.is(tok::greater) &&
800 (Style.Language == FormatStyle::LK_Proto ||
801 Style.Language == FormatStyle::LK_TextProto))) &&
802 State.Stack.size() > 1) {
803 if (Current.closesBlockOrBlockTypeList(Style))
804 return State.Stack[State.Stack.size() - 2].NestedBlockIndent;
805 if (Current.MatchingParen &&
806 Current.MatchingParen->BlockKind == BK_BracedInit)
807 return State.Stack[State.Stack.size() - 2].LastSpace;
808 return State.FirstIndent;
810 // Indent a closing parenthesis at the previous level if followed by a semi or
811 // opening brace. This allows indentations such as:
820 if (Current.is(tok::r_paren) && State.Stack.size() > 1 &&
821 (!Current.Next || Current.Next->isOneOf(tok::semi, tok::l_brace)))
822 return State.Stack[State.Stack.size() - 2].LastSpace;
823 if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope())
824 return State.Stack[State.Stack.size() - 2].LastSpace;
825 if (Current.is(tok::identifier) && Current.Next &&
826 (Current.Next->is(TT_DictLiteral) ||
827 ((Style.Language == FormatStyle::LK_Proto ||
828 Style.Language == FormatStyle::LK_TextProto) &&
829 Current.Next->isOneOf(TT_TemplateOpener, tok::l_brace))))
830 return State.Stack.back().Indent;
831 if (NextNonComment->is(TT_ObjCStringLiteral) &&
832 State.StartOfStringLiteral != 0)
833 return State.StartOfStringLiteral - 1;
834 if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0)
835 return State.StartOfStringLiteral;
836 if (NextNonComment->is(tok::lessless) &&
837 State.Stack.back().FirstLessLess != 0)
838 return State.Stack.back().FirstLessLess;
839 if (NextNonComment->isMemberAccess()) {
840 if (State.Stack.back().CallContinuation == 0)
841 return ContinuationIndent;
842 return State.Stack.back().CallContinuation;
844 if (State.Stack.back().QuestionColumn != 0 &&
845 ((NextNonComment->is(tok::colon) &&
846 NextNonComment->is(TT_ConditionalExpr)) ||
847 Previous.is(TT_ConditionalExpr)))
848 return State.Stack.back().QuestionColumn;
849 if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0)
850 return State.Stack.back().VariablePos;
851 if ((PreviousNonComment &&
852 (PreviousNonComment->ClosesTemplateDeclaration ||
853 PreviousNonComment->isOneOf(
854 TT_AttributeParen, TT_FunctionAnnotationRParen, TT_JavaAnnotation,
855 TT_LeadingJavaAnnotation))) ||
856 (!Style.IndentWrappedFunctionNames &&
857 NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName)))
858 return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent);
859 if (NextNonComment->is(TT_SelectorName)) {
860 if (!State.Stack.back().ObjCSelectorNameFound) {
861 if (NextNonComment->LongestObjCSelectorName == 0)
862 return State.Stack.back().Indent;
863 return (Style.IndentWrappedFunctionNames
864 ? std::max(State.Stack.back().Indent,
865 State.FirstIndent + Style.ContinuationIndentWidth)
866 : State.Stack.back().Indent) +
867 NextNonComment->LongestObjCSelectorName -
868 NextNonComment->ColumnWidth;
870 if (!State.Stack.back().AlignColons)
871 return State.Stack.back().Indent;
872 if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth)
873 return State.Stack.back().ColonPos - NextNonComment->ColumnWidth;
874 return State.Stack.back().Indent;
876 if (NextNonComment->is(tok::colon) && NextNonComment->is(TT_ObjCMethodExpr))
877 return State.Stack.back().ColonPos;
878 if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
879 if (State.Stack.back().StartOfArraySubscripts != 0)
880 return State.Stack.back().StartOfArraySubscripts;
881 return ContinuationIndent;
884 // This ensure that we correctly format ObjC methods calls without inputs,
885 // i.e. where the last element isn't selector like: [callee method];
886 if (NextNonComment->is(tok::identifier) && NextNonComment->FakeRParens == 0 &&
887 NextNonComment->Next && NextNonComment->Next->is(TT_ObjCMethodExpr))
888 return State.Stack.back().Indent;
890 if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) ||
891 Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon))
892 return ContinuationIndent;
893 if (PreviousNonComment && PreviousNonComment->is(tok::colon) &&
894 PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral))
895 return ContinuationIndent;
896 if (NextNonComment->is(TT_CtorInitializerComma))
897 return State.Stack.back().Indent;
898 if (PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) &&
899 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon)
900 return State.Stack.back().Indent;
901 if (NextNonComment->isOneOf(TT_CtorInitializerColon, TT_InheritanceColon,
902 TT_InheritanceComma))
903 return State.FirstIndent + Style.ConstructorInitializerIndentWidth;
904 if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() &&
905 !Current.isOneOf(tok::colon, tok::comment))
906 return ContinuationIndent;
907 if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment &&
908 PreviousNonComment->isNot(tok::r_brace))
909 // Ensure that we fall back to the continuation indent width instead of
910 // just flushing continuations left.
911 return State.Stack.back().Indent + Style.ContinuationIndentWidth;
912 return State.Stack.back().Indent;
915 unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
916 bool DryRun, bool Newline) {
917 assert(State.Stack.size());
918 const FormatToken &Current = *State.NextToken;
920 if (Current.isOneOf(tok::comma, TT_BinaryOperator))
921 State.Stack.back().NoLineBreakInOperand = false;
922 if (Current.is(TT_InheritanceColon))
923 State.Stack.back().AvoidBinPacking = true;
924 if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator)) {
925 if (State.Stack.back().FirstLessLess == 0)
926 State.Stack.back().FirstLessLess = State.Column;
928 State.Stack.back().LastOperatorWrapped = Newline;
930 if (Current.is(TT_BinaryOperator) && Current.isNot(tok::lessless))
931 State.Stack.back().LastOperatorWrapped = Newline;
932 if (Current.is(TT_ConditionalExpr) && Current.Previous &&
933 !Current.Previous->is(TT_ConditionalExpr))
934 State.Stack.back().LastOperatorWrapped = Newline;
935 if (Current.is(TT_ArraySubscriptLSquare) &&
936 State.Stack.back().StartOfArraySubscripts == 0)
937 State.Stack.back().StartOfArraySubscripts = State.Column;
938 if (Style.BreakBeforeTernaryOperators && Current.is(tok::question))
939 State.Stack.back().QuestionColumn = State.Column;
940 if (!Style.BreakBeforeTernaryOperators && Current.isNot(tok::colon)) {
941 const FormatToken *Previous = Current.Previous;
942 while (Previous && Previous->isTrailingComment())
943 Previous = Previous->Previous;
944 if (Previous && Previous->is(tok::question))
945 State.Stack.back().QuestionColumn = State.Column;
947 if (!Current.opensScope() && !Current.closesScope() &&
948 !Current.is(TT_PointerOrReference))
949 State.LowestLevelOnLine =
950 std::min(State.LowestLevelOnLine, Current.NestingLevel);
951 if (Current.isMemberAccess())
952 State.Stack.back().StartOfFunctionCall =
953 !Current.NextOperator ? 0 : State.Column;
954 if (Current.is(TT_SelectorName)) {
955 State.Stack.back().ObjCSelectorNameFound = true;
956 if (Style.IndentWrappedFunctionNames) {
957 State.Stack.back().Indent =
958 State.FirstIndent + Style.ContinuationIndentWidth;
961 if (Current.is(TT_CtorInitializerColon) &&
962 Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon) {
963 // Indent 2 from the column, so:
964 // SomeClass::SomeClass()
968 State.Stack.back().Indent =
970 (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma
973 State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
974 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
975 State.Stack.back().AvoidBinPacking = true;
976 State.Stack.back().BreakBeforeParameter = false;
978 if (Current.is(TT_CtorInitializerColon) &&
979 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) {
980 State.Stack.back().Indent =
981 State.FirstIndent + Style.ConstructorInitializerIndentWidth;
982 State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
983 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
984 State.Stack.back().AvoidBinPacking = true;
986 if (Current.is(TT_InheritanceColon))
987 State.Stack.back().Indent =
988 State.FirstIndent + Style.ContinuationIndentWidth;
989 if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
990 State.Stack.back().NestedBlockIndent =
991 State.Column + Current.ColumnWidth + 1;
992 if (Current.isOneOf(TT_LambdaLSquare, TT_LambdaArrow))
993 State.Stack.back().LastSpace = State.Column;
995 // Insert scopes created by fake parenthesis.
996 const FormatToken *Previous = Current.getPreviousNonComment();
998 // Add special behavior to support a format commonly used for JavaScript
1000 // SomeFunction(function() {
1004 if (Current.isNot(tok::comment) && Previous &&
1005 Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
1006 !Previous->is(TT_DictLiteral) && State.Stack.size() > 1) {
1007 if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
1008 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
1009 State.Stack[i].NoLineBreak = true;
1010 State.Stack[State.Stack.size() - 2].NestedBlockInlined = false;
1013 (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) ||
1014 Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr)) &&
1015 !Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
1016 State.Stack.back().NestedBlockInlined =
1018 (Previous->isNot(tok::l_paren) || Previous->ParameterCount > 1);
1021 moveStatePastFakeLParens(State, Newline);
1022 moveStatePastScopeCloser(State);
1023 bool AllowBreak = !State.Stack.back().NoLineBreak &&
1024 !State.Stack.back().NoLineBreakInOperand;
1025 moveStatePastScopeOpener(State, Newline);
1026 moveStatePastFakeRParens(State);
1028 if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0)
1029 State.StartOfStringLiteral = State.Column + 1;
1030 else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0)
1031 State.StartOfStringLiteral = State.Column;
1032 else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) &&
1033 !Current.isStringLiteral())
1034 State.StartOfStringLiteral = 0;
1036 State.Column += Current.ColumnWidth;
1037 State.NextToken = State.NextToken->Next;
1040 handleEndOfLine(Current, State, DryRun, AllowBreak);
1043 Current.Role->formatFromToken(State, this, DryRun);
1044 // If the previous has a special role, let it consume tokens as appropriate.
1045 // It is necessary to start at the previous token for the only implemented
1046 // role (comma separated list). That way, the decision whether or not to break
1047 // after the "{" is already done and both options are tried and evaluated.
1048 // FIXME: This is ugly, find a better way.
1049 if (Previous && Previous->Role)
1050 Penalty += Previous->Role->formatAfterToken(State, this, DryRun);
1055 void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
1057 const FormatToken &Current = *State.NextToken;
1058 const FormatToken *Previous = Current.getPreviousNonComment();
1060 // Don't add extra indentation for the first fake parenthesis after
1061 // 'return', assignments or opening <({[. The indentation for these cases
1062 // is special cased.
1063 bool SkipFirstExtraIndent =
1064 (Previous && (Previous->opensScope() ||
1065 Previous->isOneOf(tok::semi, tok::kw_return) ||
1066 (Previous->getPrecedence() == prec::Assignment &&
1067 Style.AlignOperands) ||
1068 Previous->is(TT_ObjCMethodExpr)));
1069 for (SmallVectorImpl<prec::Level>::const_reverse_iterator
1070 I = Current.FakeLParens.rbegin(),
1071 E = Current.FakeLParens.rend();
1073 ParenState NewParenState = State.Stack.back();
1074 NewParenState.ContainsLineBreak = false;
1075 NewParenState.LastOperatorWrapped = true;
1076 NewParenState.NoLineBreak =
1077 NewParenState.NoLineBreak || State.Stack.back().NoLineBreakInOperand;
1079 // Don't propagate AvoidBinPacking into subexpressions of arg/param lists.
1080 if (*I > prec::Comma)
1081 NewParenState.AvoidBinPacking = false;
1083 // Indent from 'LastSpace' unless these are fake parentheses encapsulating
1084 // a builder type call after 'return' or, if the alignment after opening
1085 // brackets is disabled.
1086 if (!Current.isTrailingComment() &&
1087 (Style.AlignOperands || *I < prec::Assignment) &&
1088 (!Previous || Previous->isNot(tok::kw_return) ||
1089 (Style.Language != FormatStyle::LK_Java && *I > 0)) &&
1090 (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
1091 *I != prec::Comma || Current.NestingLevel == 0))
1092 NewParenState.Indent =
1093 std::max(std::max(State.Column, NewParenState.Indent),
1094 State.Stack.back().LastSpace);
1096 // Do not indent relative to the fake parentheses inserted for "." or "->".
1097 // This is a special case to make the following to statements consistent:
1098 // OuterFunction(InnerFunctionCall( // break
1099 // ParameterToInnerFunction));
1100 // OuterFunction(SomeObject.InnerFunctionCall( // break
1101 // ParameterToInnerFunction));
1102 if (*I > prec::Unknown)
1103 NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column);
1104 if (*I != prec::Conditional && !Current.is(TT_UnaryOperator) &&
1105 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign)
1106 NewParenState.StartOfFunctionCall = State.Column;
1108 // Always indent conditional expressions. Never indent expression where
1109 // the 'operator' is ',', ';' or an assignment (i.e. *I <=
1110 // prec::Assignment) as those have different indentation rules. Indent
1111 // other expression, unless the indentation needs to be skipped.
1112 if (*I == prec::Conditional ||
1113 (!SkipFirstExtraIndent && *I > prec::Assignment &&
1114 !Current.isTrailingComment()))
1115 NewParenState.Indent += Style.ContinuationIndentWidth;
1116 if ((Previous && !Previous->opensScope()) || *I != prec::Comma)
1117 NewParenState.BreakBeforeParameter = false;
1118 State.Stack.push_back(NewParenState);
1119 SkipFirstExtraIndent = false;
1123 void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) {
1124 for (unsigned i = 0, e = State.NextToken->FakeRParens; i != e; ++i) {
1125 unsigned VariablePos = State.Stack.back().VariablePos;
1126 if (State.Stack.size() == 1) {
1127 // Do not pop the last element.
1130 State.Stack.pop_back();
1131 State.Stack.back().VariablePos = VariablePos;
1135 void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
1137 const FormatToken &Current = *State.NextToken;
1138 if (!Current.opensScope())
1141 if (Current.MatchingParen && Current.BlockKind == BK_Block) {
1142 moveStateToNewBlock(State);
1147 unsigned LastSpace = State.Stack.back().LastSpace;
1148 bool AvoidBinPacking;
1149 bool BreakBeforeParameter = false;
1150 unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall,
1151 State.Stack.back().NestedBlockIndent);
1152 if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
1153 opensProtoMessageField(Current, Style)) {
1154 if (Current.opensBlockOrBlockTypeList(Style)) {
1155 NewIndent = Style.IndentWidth +
1156 std::min(State.Column, State.Stack.back().NestedBlockIndent);
1158 NewIndent = State.Stack.back().LastSpace + Style.ContinuationIndentWidth;
1160 const FormatToken *NextNoComment = Current.getNextNonComment();
1161 bool EndsInComma = Current.MatchingParen &&
1162 Current.MatchingParen->Previous &&
1163 Current.MatchingParen->Previous->is(tok::comma);
1164 AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) ||
1165 Style.Language == FormatStyle::LK_Proto ||
1166 Style.Language == FormatStyle::LK_TextProto ||
1167 !Style.BinPackArguments ||
1169 NextNoComment->isOneOf(TT_DesignatedInitializerPeriod,
1170 TT_DesignatedInitializerLSquare));
1171 BreakBeforeParameter = EndsInComma;
1172 if (Current.ParameterCount > 1)
1173 NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
1175 NewIndent = Style.ContinuationIndentWidth +
1176 std::max(State.Stack.back().LastSpace,
1177 State.Stack.back().StartOfFunctionCall);
1179 // Ensure that different different brackets force relative alignment, e.g.:
1180 // void SomeFunction(vector< // break
1182 // FIXME: We likely want to do this for more combinations of brackets.
1183 // Verify that it is wanted for ObjC, too.
1184 if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) {
1185 NewIndent = std::max(NewIndent, State.Stack.back().Indent);
1186 LastSpace = std::max(LastSpace, State.Stack.back().Indent);
1190 Current.MatchingParen &&
1191 Current.MatchingParen->getPreviousNonComment() &&
1192 Current.MatchingParen->getPreviousNonComment()->is(tok::comma);
1195 (Style.Language == FormatStyle::LK_JavaScript && EndsInComma) ||
1196 (State.Line->MustBeDeclaration && !Style.BinPackParameters) ||
1197 (!State.Line->MustBeDeclaration && !Style.BinPackArguments) ||
1198 (Style.ExperimentalAutoDetectBinPacking &&
1199 (Current.PackingKind == PPK_OnePerLine ||
1200 (!BinPackInconclusiveFunctions &&
1201 Current.PackingKind == PPK_Inconclusive)));
1203 if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen) {
1204 if (Style.ColumnLimit) {
1205 // If this '[' opens an ObjC call, determine whether all parameters fit
1206 // into one line and put one per line if they don't.
1207 if (getLengthToMatchingParen(Current) + State.Column >
1208 getColumnLimit(State))
1209 BreakBeforeParameter = true;
1211 // For ColumnLimit = 0, we have to figure out whether there is or has to
1212 // be a line break within this call.
1213 for (const FormatToken *Tok = &Current;
1214 Tok && Tok != Current.MatchingParen; Tok = Tok->Next) {
1215 if (Tok->MustBreakBefore ||
1216 (Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) {
1217 BreakBeforeParameter = true;
1224 if (Style.Language == FormatStyle::LK_JavaScript && EndsInComma)
1225 BreakBeforeParameter = true;
1227 // Generally inherit NoLineBreak from the current scope to nested scope.
1228 // However, don't do this for non-empty nested blocks, dict literals and
1229 // array literals as these follow different indentation rules.
1231 Current.Children.empty() &&
1232 !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) &&
1233 (State.Stack.back().NoLineBreak ||
1234 State.Stack.back().NoLineBreakInOperand ||
1235 (Current.is(TT_TemplateOpener) &&
1236 State.Stack.back().ContainsUnwrappedBuilder));
1237 State.Stack.push_back(
1238 ParenState(NewIndent, LastSpace, AvoidBinPacking, NoLineBreak));
1239 State.Stack.back().NestedBlockIndent = NestedBlockIndent;
1240 State.Stack.back().BreakBeforeParameter = BreakBeforeParameter;
1241 State.Stack.back().HasMultipleNestedBlocks = Current.BlockParameterCount > 1;
1244 void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {
1245 const FormatToken &Current = *State.NextToken;
1246 if (!Current.closesScope())
1249 // If we encounter a closing ), ], } or >, we can remove a level from our
1251 if (State.Stack.size() > 1 &&
1252 (Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) ||
1253 (Current.is(tok::r_brace) && State.NextToken != State.Line->First) ||
1254 State.NextToken->is(TT_TemplateCloser)))
1255 State.Stack.pop_back();
1257 if (Current.is(tok::r_square)) {
1258 // If this ends the array subscript expr, reset the corresponding value.
1259 const FormatToken *NextNonComment = Current.getNextNonComment();
1260 if (NextNonComment && NextNonComment->isNot(tok::l_square))
1261 State.Stack.back().StartOfArraySubscripts = 0;
1265 void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
1266 unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
1267 // ObjC block sometimes follow special indentation rules.
1268 unsigned NewIndent =
1269 NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace)
1270 ? Style.ObjCBlockIndentWidth
1271 : Style.IndentWidth);
1272 State.Stack.push_back(ParenState(NewIndent, State.Stack.back().LastSpace,
1273 /*AvoidBinPacking=*/true,
1274 /*NoLineBreak=*/false));
1275 State.Stack.back().NestedBlockIndent = NestedBlockIndent;
1276 State.Stack.back().BreakBeforeParameter = true;
1279 static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn,
1281 encoding::Encoding Encoding) {
1282 size_t LastNewlinePos = Text.find_last_of("\n");
1283 if (LastNewlinePos == StringRef::npos) {
1284 return StartColumn +
1285 encoding::columnWidthWithTabs(Text, StartColumn, TabWidth, Encoding);
1287 return encoding::columnWidthWithTabs(Text.substr(LastNewlinePos),
1288 /*StartColumn=*/0, TabWidth, Encoding);
1292 unsigned ContinuationIndenter::reformatRawStringLiteral(
1293 const FormatToken &Current, LineState &State,
1294 const FormatStyle &RawStringStyle, bool DryRun) {
1295 unsigned StartColumn = State.Column - Current.ColumnWidth;
1296 auto Delimiter = *getRawStringDelimiter(Current.TokenText);
1297 // The text of a raw string is between the leading 'R"delimiter(' and the
1298 // trailing 'delimiter)"'.
1299 unsigned PrefixSize = 3 + Delimiter.size();
1300 unsigned SuffixSize = 2 + Delimiter.size();
1302 // The first start column is the column the raw text starts.
1303 unsigned FirstStartColumn = StartColumn + PrefixSize;
1305 // The next start column is the intended indentation a line break inside
1306 // the raw string at level 0. It is determined by the following rules:
1307 // - if the content starts on newline, it is one level more than the current
1309 // - if the content does not start on a newline, it is the first start
1311 // These rules have the advantage that the formatted content both does not
1312 // violate the rectangle rule and visually flows within the surrounding
1314 bool ContentStartsOnNewline = Current.TokenText[PrefixSize] == '\n';
1315 unsigned NextStartColumn = ContentStartsOnNewline
1316 ? State.Stack.back().Indent + Style.IndentWidth
1319 // The last start column is the column the raw string suffix starts if it is
1320 // put on a newline.
1321 // The last start column is the intended indentation of the raw string postfix
1322 // if it is put on a newline. It is determined by the following rules:
1323 // - if the raw string prefix starts on a newline, it is the column where
1324 // that raw string prefix starts, and
1325 // - if the raw string prefix does not start on a newline, it is the current
1327 unsigned LastStartColumn = Current.NewlinesBefore
1328 ? FirstStartColumn - PrefixSize
1329 : State.Stack.back().Indent;
1331 std::string RawText =
1332 Current.TokenText.substr(PrefixSize).drop_back(SuffixSize);
1334 std::pair<tooling::Replacements, unsigned> Fixes = internal::reformat(
1335 RawStringStyle, RawText, {tooling::Range(0, RawText.size())},
1336 FirstStartColumn, NextStartColumn, LastStartColumn, "<stdin>",
1337 /*Status=*/nullptr);
1339 auto NewCode = applyAllReplacements(RawText, Fixes.first);
1340 tooling::Replacements NoFixes;
1342 State.Column += Current.ColumnWidth;
1346 SourceLocation OriginLoc =
1347 Current.Tok.getLocation().getLocWithOffset(PrefixSize);
1348 for (const tooling::Replacement &Fix : Fixes.first) {
1349 auto Err = Whitespaces.addReplacement(tooling::Replacement(
1350 SourceMgr, OriginLoc.getLocWithOffset(Fix.getOffset()),
1351 Fix.getLength(), Fix.getReplacementText()));
1353 llvm::errs() << "Failed to reformat raw string: "
1354 << llvm::toString(std::move(Err)) << "\n";
1358 unsigned RawLastLineEndColumn = getLastLineEndColumn(
1359 *NewCode, FirstStartColumn, Style.TabWidth, Encoding);
1360 State.Column = RawLastLineEndColumn + SuffixSize;
1361 return Fixes.second;
1364 unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
1366 // Break before further function parameters on all levels.
1367 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
1368 State.Stack[i].BreakBeforeParameter = true;
1370 unsigned ColumnsUsed = State.Column;
1371 // We can only affect layout of the first and the last line, so the penalty
1372 // for all other lines is constant, and we ignore it.
1373 State.Column = Current.LastLineColumnWidth;
1375 if (ColumnsUsed > getColumnLimit(State))
1376 return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State));
1380 unsigned ContinuationIndenter::handleEndOfLine(const FormatToken &Current,
1381 LineState &State, bool DryRun,
1383 unsigned Penalty = 0;
1384 // Compute the raw string style to use in case this is a raw string literal
1385 // that can be reformatted.
1386 auto RawStringStyle = getRawStringStyle(Current, State);
1387 if (RawStringStyle) {
1388 Penalty = reformatRawStringLiteral(Current, State, *RawStringStyle, DryRun);
1389 } else if (Current.IsMultiline && Current.isNot(TT_BlockComment)) {
1390 // Don't break multi-line tokens other than block comments and raw string
1391 // literals. Instead, just update the state.
1392 Penalty = addMultilineToken(Current, State);
1393 } else if (State.Line->Type != LT_ImportStatement) {
1394 // We generally don't break import statements.
1395 LineState OriginalState = State;
1397 // Whether we force the reflowing algorithm to stay strictly within the
1399 bool Strict = false;
1400 // Whether the first non-strict attempt at reflowing did intentionally
1401 // exceed the column limit.
1402 bool Exceeded = false;
1403 std::tie(Penalty, Exceeded) = breakProtrudingToken(
1404 Current, State, AllowBreak, /*DryRun=*/true, Strict);
1406 // If non-strict reflowing exceeds the column limit, try whether strict
1407 // reflowing leads to an overall lower penalty.
1408 LineState StrictState = OriginalState;
1409 unsigned StrictPenalty =
1410 breakProtrudingToken(Current, StrictState, AllowBreak,
1411 /*DryRun=*/true, /*Strict=*/true)
1413 Strict = StrictPenalty <= Penalty;
1415 Penalty = StrictPenalty;
1416 State = StrictState;
1420 // If we're not in dry-run mode, apply the changes with the decision on
1421 // strictness made above.
1422 breakProtrudingToken(Current, OriginalState, AllowBreak, /*DryRun=*/false,
1426 if (State.Column > getColumnLimit(State)) {
1427 unsigned ExcessCharacters = State.Column - getColumnLimit(State);
1428 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters;
1433 llvm::Optional<FormatStyle>
1434 ContinuationIndenter::getRawStringStyle(const FormatToken &Current,
1435 const LineState &State) {
1436 if (!Current.isStringLiteral())
1438 auto Delimiter = getRawStringDelimiter(Current.TokenText);
1441 auto RawStringStyle = RawStringFormats.get(*Delimiter);
1442 if (!RawStringStyle)
1444 RawStringStyle->ColumnLimit = getColumnLimit(State);
1445 return RawStringStyle;
1448 std::unique_ptr<BreakableToken> ContinuationIndenter::createBreakableToken(
1449 const FormatToken &Current, LineState &State, bool AllowBreak) {
1450 unsigned StartColumn = State.Column - Current.ColumnWidth;
1451 if (Current.isStringLiteral()) {
1452 // FIXME: String literal breaking is currently disabled for Java and JS, as
1453 // it requires strings to be merged using "+" which we don't support.
1454 if (Style.Language == FormatStyle::LK_Java ||
1455 Style.Language == FormatStyle::LK_JavaScript ||
1456 !Style.BreakStringLiterals ||
1460 // Don't break string literals inside preprocessor directives (except for
1461 // #define directives, as their contents are stored in separate lines and
1462 // are not affected by this check).
1463 // This way we avoid breaking code with line directives and unknown
1464 // preprocessor directives that contain long string literals.
1465 if (State.Line->Type == LT_PreprocessorDirective)
1467 // Exempts unterminated string literals from line breaking. The user will
1468 // likely want to terminate the string before any line breaking is done.
1469 if (Current.IsUnterminatedLiteral)
1472 StringRef Text = Current.TokenText;
1475 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'.
1476 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to
1477 // reduce the overhead) for each FormatToken, which is a string, so that we
1478 // don't run multiple checks here on the hot path.
1479 if ((Text.endswith(Postfix = "\"") &&
1480 (Text.startswith(Prefix = "@\"") || Text.startswith(Prefix = "\"") ||
1481 Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") ||
1482 Text.startswith(Prefix = "u8\"") ||
1483 Text.startswith(Prefix = "L\""))) ||
1484 (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) {
1485 return llvm::make_unique<BreakableStringLiteral>(
1486 Current, StartColumn, Prefix, Postfix, State.Line->InPPDirective,
1489 } else if (Current.is(TT_BlockComment)) {
1490 if (!Style.ReflowComments ||
1491 // If a comment token switches formatting, like
1492 // /* clang-format on */, we don't want to break it further,
1493 // but we may still want to adjust its indentation.
1494 switchesFormatting(Current)) {
1497 return llvm::make_unique<BreakableBlockComment>(
1498 Current, StartColumn, Current.OriginalColumn, !Current.Previous,
1499 State.Line->InPPDirective, Encoding, Style);
1500 } else if (Current.is(TT_LineComment) &&
1501 (Current.Previous == nullptr ||
1502 Current.Previous->isNot(TT_ImplicitStringLiteral))) {
1503 if (!Style.ReflowComments ||
1504 CommentPragmasRegex.match(Current.TokenText.substr(2)) ||
1505 switchesFormatting(Current))
1507 return llvm::make_unique<BreakableLineCommentSection>(
1508 Current, StartColumn, Current.OriginalColumn, !Current.Previous,
1509 /*InPPDirective=*/false, Encoding, Style);
1514 std::pair<unsigned, bool>
1515 ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
1516 LineState &State, bool AllowBreak,
1517 bool DryRun, bool Strict) {
1518 std::unique_ptr<const BreakableToken> Token =
1519 createBreakableToken(Current, State, AllowBreak);
1522 assert(Token->getLineCount() > 0);
1523 unsigned ColumnLimit = getColumnLimit(State);
1524 if (Current.is(TT_LineComment)) {
1525 // We don't insert backslashes when breaking line comments.
1526 ColumnLimit = Style.ColumnLimit;
1528 if (Current.UnbreakableTailLength >= ColumnLimit)
1530 // ColumnWidth was already accounted into State.Column before calling
1531 // breakProtrudingToken.
1532 unsigned StartColumn = State.Column - Current.ColumnWidth;
1533 unsigned NewBreakPenalty = Current.isStringLiteral()
1534 ? Style.PenaltyBreakString
1535 : Style.PenaltyBreakComment;
1536 // Stores whether we intentionally decide to let a line exceed the column
1538 bool Exceeded = false;
1539 // Stores whether we introduce a break anywhere in the token.
1540 bool BreakInserted = Token->introducesBreakBeforeToken();
1541 // Store whether we inserted a new line break at the end of the previous
1543 bool NewBreakBefore = false;
1544 // We use a conservative reflowing strategy. Reflow starts after a line is
1545 // broken or the corresponding whitespace compressed. Reflow ends as soon as a
1546 // line that doesn't get reflown with the previous line is reached.
1547 bool Reflow = false;
1548 // Keep track of where we are in the token:
1549 // Where we are in the content of the current logical line.
1550 unsigned TailOffset = 0;
1551 // The column number we're currently at.
1552 unsigned ContentStartColumn =
1553 Token->getContentStartColumn(0, /*Break=*/false);
1554 // The number of columns left in the current logical line after TailOffset.
1555 unsigned RemainingTokenColumns =
1556 Token->getRemainingLength(0, TailOffset, ContentStartColumn);
1557 // Adapt the start of the token, for example indent.
1559 Token->adaptStartOfLine(0, Whitespaces);
1561 unsigned Penalty = 0;
1562 DEBUG(llvm::dbgs() << "Breaking protruding token at column " << StartColumn
1564 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount();
1565 LineIndex != EndIndex; ++LineIndex) {
1566 DEBUG(llvm::dbgs() << " Line: " << LineIndex << " (Reflow: " << Reflow
1568 NewBreakBefore = false;
1569 // If we did reflow the previous line, we'll try reflowing again. Otherwise
1570 // we'll start reflowing if the current line is broken or whitespace is
1572 bool TryReflow = Reflow;
1573 // Break the current token until we can fit the rest of the line.
1574 while (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
1575 DEBUG(llvm::dbgs() << " Over limit, need: "
1576 << (ContentStartColumn + RemainingTokenColumns)
1577 << ", space: " << ColumnLimit
1578 << ", reflown prefix: " << ContentStartColumn
1579 << ", offset in line: " << TailOffset << "\n");
1580 // If the current token doesn't fit, find the latest possible split in the
1581 // current line so that breaking at it will be under the column limit.
1582 // FIXME: Use the earliest possible split while reflowing to correctly
1583 // compress whitespace within a line.
1584 BreakableToken::Split Split =
1585 Token->getSplit(LineIndex, TailOffset, ColumnLimit,
1586 ContentStartColumn, CommentPragmasRegex);
1587 if (Split.first == StringRef::npos) {
1588 // No break opportunity - update the penalty and continue with the next
1590 if (LineIndex < EndIndex - 1)
1591 // The last line's penalty is handled in addNextStateToQueue().
1592 Penalty += Style.PenaltyExcessCharacter *
1593 (ContentStartColumn + RemainingTokenColumns - ColumnLimit);
1594 DEBUG(llvm::dbgs() << " No break opportunity.\n");
1597 assert(Split.first != 0);
1599 if (Token->supportsReflow()) {
1600 // Check whether the next natural split point after the current one can
1601 // still fit the line, either because we can compress away whitespace,
1602 // or because the penalty the excess characters introduce is lower than
1603 // the break penalty.
1604 // We only do this for tokens that support reflowing, and thus allow us
1605 // to change the whitespace arbitrarily (e.g. comments).
1606 // Other tokens, like string literals, can be broken on arbitrary
1609 // First, compute the columns from TailOffset to the next possible split
1613 // // Some text that breaks
1616 // ^-------- to split columns
1618 // ^--------------- to next split columns
1619 unsigned ToSplitColumns = Token->getRangeLength(
1620 LineIndex, TailOffset, Split.first, ContentStartColumn);
1621 DEBUG(llvm::dbgs() << " ToSplit: " << ToSplitColumns << "\n");
1623 BreakableToken::Split NextSplit = Token->getSplit(
1624 LineIndex, TailOffset + Split.first + Split.second, ColumnLimit,
1625 ContentStartColumn + ToSplitColumns + 1, CommentPragmasRegex);
1626 // Compute the columns necessary to fit the next non-breakable sequence
1627 // into the current line.
1628 unsigned ToNextSplitColumns = 0;
1629 if (NextSplit.first == StringRef::npos) {
1630 ToNextSplitColumns = Token->getRemainingLength(LineIndex, TailOffset,
1631 ContentStartColumn);
1633 ToNextSplitColumns = Token->getRangeLength(
1634 LineIndex, TailOffset,
1635 Split.first + Split.second + NextSplit.first, ContentStartColumn);
1637 // Compress the whitespace between the break and the start of the next
1638 // unbreakable sequence.
1639 ToNextSplitColumns =
1640 Token->getLengthAfterCompression(ToNextSplitColumns, Split);
1641 DEBUG(llvm::dbgs() << " ContentStartColumn: " << ContentStartColumn
1643 DEBUG(llvm::dbgs() << " ToNextSplit: " << ToNextSplitColumns << "\n");
1644 // If the whitespace compression makes us fit, continue on the current
1646 bool ContinueOnLine =
1647 ContentStartColumn + ToNextSplitColumns <= ColumnLimit;
1648 unsigned ExcessCharactersPenalty = 0;
1649 if (!ContinueOnLine && !Strict) {
1650 // Similarly, if the excess characters' penalty is lower than the
1651 // penalty of introducing a new break, continue on the current line.
1652 ExcessCharactersPenalty =
1653 (ContentStartColumn + ToNextSplitColumns - ColumnLimit) *
1654 Style.PenaltyExcessCharacter;
1656 << " Penalty excess: " << ExcessCharactersPenalty
1657 << "\n break : " << NewBreakPenalty << "\n");
1658 if (ExcessCharactersPenalty < NewBreakPenalty) {
1660 ContinueOnLine = true;
1663 if (ContinueOnLine) {
1664 DEBUG(llvm::dbgs() << " Continuing on line...\n");
1665 // The current line fits after compressing the whitespace - reflow
1666 // the next line into it if possible.
1669 Token->compressWhitespace(LineIndex, TailOffset, Split,
1671 // When we continue on the same line, leave one space between content.
1672 ContentStartColumn += ToSplitColumns + 1;
1673 Penalty += ExcessCharactersPenalty;
1674 TailOffset += Split.first + Split.second;
1675 RemainingTokenColumns = Token->getRemainingLength(
1676 LineIndex, TailOffset, ContentStartColumn);
1680 DEBUG(llvm::dbgs() << " Breaking...\n");
1681 ContentStartColumn =
1682 Token->getContentStartColumn(LineIndex, /*Break=*/true);
1683 unsigned NewRemainingTokenColumns = Token->getRemainingLength(
1684 LineIndex, TailOffset + Split.first + Split.second,
1685 ContentStartColumn);
1687 // When breaking before a tab character, it may be moved by a few columns,
1688 // but will still be expanded to the next tab stop, so we don't save any
1690 if (NewRemainingTokenColumns == RemainingTokenColumns) {
1691 // FIXME: Do we need to adjust the penalty?
1694 assert(NewRemainingTokenColumns < RemainingTokenColumns);
1696 DEBUG(llvm::dbgs() << " Breaking at: " << TailOffset + Split.first
1697 << ", " << Split.second << "\n");
1699 Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces);
1701 Penalty += NewBreakPenalty;
1702 TailOffset += Split.first + Split.second;
1703 RemainingTokenColumns = NewRemainingTokenColumns;
1704 BreakInserted = true;
1705 NewBreakBefore = true;
1707 // In case there's another line, prepare the state for the start of the next
1709 if (LineIndex + 1 != EndIndex) {
1710 unsigned NextLineIndex = LineIndex + 1;
1712 // After breaking a line, try to reflow the next line into the current
1713 // one once RemainingTokenColumns fits.
1716 // We decided that we want to try reflowing the next line into the
1718 // We will now adjust the state as if the reflow is successful (in
1719 // preparation for the next line), and see whether that works. If we
1720 // decide that we cannot reflow, we will later reset the state to the
1721 // start of the next line.
1723 // As we did not continue breaking the line, RemainingTokenColumns is
1724 // known to fit after ContentStartColumn. Adapt ContentStartColumn to
1725 // the position at which we want to format the next line if we do
1727 // When we reflow, we need to add a space between the end of the current
1728 // line and the next line's start column.
1729 ContentStartColumn += RemainingTokenColumns + 1;
1730 // Get the split that we need to reflow next logical line into the end
1731 // of the current one; the split will include any leading whitespace of
1732 // the next logical line.
1733 BreakableToken::Split SplitBeforeNext =
1734 Token->getReflowSplit(NextLineIndex, CommentPragmasRegex);
1735 DEBUG(llvm::dbgs() << " Size of reflown text: " << ContentStartColumn
1736 << "\n Potential reflow split: ");
1737 if (SplitBeforeNext.first != StringRef::npos) {
1738 DEBUG(llvm::dbgs() << SplitBeforeNext.first << ", "
1739 << SplitBeforeNext.second << "\n");
1740 TailOffset = SplitBeforeNext.first + SplitBeforeNext.second;
1741 // If the rest of the next line fits into the current line below the
1742 // column limit, we can safely reflow.
1743 RemainingTokenColumns = Token->getRemainingLength(
1744 NextLineIndex, TailOffset, ContentStartColumn);
1746 if (ContentStartColumn + RemainingTokenColumns > ColumnLimit) {
1747 DEBUG(llvm::dbgs() << " Over limit after reflow, need: "
1748 << (ContentStartColumn + RemainingTokenColumns)
1749 << ", space: " << ColumnLimit
1750 << ", reflown prefix: " << ContentStartColumn
1751 << ", offset in line: " << TailOffset << "\n");
1752 // If the whole next line does not fit, try to find a point in
1753 // the next line at which we can break so that attaching the part
1754 // of the next line to that break point onto the current line is
1755 // below the column limit.
1756 BreakableToken::Split Split =
1757 Token->getSplit(NextLineIndex, TailOffset, ColumnLimit,
1758 ContentStartColumn, CommentPragmasRegex);
1759 if (Split.first == StringRef::npos) {
1760 DEBUG(llvm::dbgs() << " Did not find later break\n");
1763 // Check whether the first split point gets us below the column
1764 // limit. Note that we will execute this split below as part of
1765 // the normal token breaking and reflow logic within the line.
1766 unsigned ToSplitColumns = Token->getRangeLength(
1767 NextLineIndex, TailOffset, Split.first, ContentStartColumn);
1768 if (ContentStartColumn + ToSplitColumns > ColumnLimit) {
1769 DEBUG(llvm::dbgs() << " Next split protrudes, need: "
1770 << (ContentStartColumn + ToSplitColumns)
1771 << ", space: " << ColumnLimit);
1772 unsigned ExcessCharactersPenalty =
1773 (ContentStartColumn + ToSplitColumns - ColumnLimit) *
1774 Style.PenaltyExcessCharacter;
1775 if (NewBreakPenalty < ExcessCharactersPenalty) {
1782 DEBUG(llvm::dbgs() << "not found.\n");
1786 // If we didn't reflow into the next line, the only space to consider is
1787 // the next logical line. Reset our state to match the start of the next
1790 ContentStartColumn =
1791 Token->getContentStartColumn(NextLineIndex, /*Break=*/false);
1792 RemainingTokenColumns = Token->getRemainingLength(
1793 NextLineIndex, TailOffset, ContentStartColumn);
1794 // Adapt the start of the token, for example indent.
1796 Token->adaptStartOfLine(NextLineIndex, Whitespaces);
1798 // If we found a reflow split and have added a new break before the next
1799 // line, we are going to remove the line break at the start of the next
1800 // logical line. For example, here we'll add a new line break after
1801 // 'text', and subsequently delete the line break between 'that' and
1803 // // some text that
1808 // When adding the line break, we also added the penalty for it, so we
1809 // need to subtract that penalty again when we remove the line break due
1811 if (NewBreakBefore) {
1812 assert(Penalty >= NewBreakPenalty);
1813 Penalty -= NewBreakPenalty;
1816 Token->reflow(NextLineIndex, Whitespaces);
1821 BreakableToken::Split SplitAfterLastLine =
1822 Token->getSplitAfterLastLine(TailOffset);
1823 if (SplitAfterLastLine.first != StringRef::npos) {
1824 DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n");
1826 Token->replaceWhitespaceAfterLastLine(TailOffset, SplitAfterLastLine,
1828 ContentStartColumn =
1829 Token->getContentStartColumn(Token->getLineCount() - 1, /*Break=*/true);
1830 RemainingTokenColumns = Token->getRemainingLength(
1831 Token->getLineCount() - 1,
1832 TailOffset + SplitAfterLastLine.first + SplitAfterLastLine.second,
1833 ContentStartColumn);
1836 State.Column = ContentStartColumn + RemainingTokenColumns -
1837 Current.UnbreakableTailLength;
1839 if (BreakInserted) {
1840 // If we break the token inside a parameter list, we need to break before
1841 // the next parameter on all levels, so that the next parameter is clearly
1842 // visible. Line comments already introduce a break.
1843 if (Current.isNot(TT_LineComment)) {
1844 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i)
1845 State.Stack[i].BreakBeforeParameter = true;
1848 if (Current.is(TT_BlockComment))
1849 State.NoContinuation = true;
1851 State.Stack.back().LastSpace = StartColumn;
1854 Token->updateNextToken(State);
1856 return {Penalty, Exceeded};
1859 unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const {
1860 // In preprocessor directives reserve two chars for trailing " \"
1861 return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0);
1864 bool ContinuationIndenter::nextIsMultilineString(const LineState &State) {
1865 const FormatToken &Current = *State.NextToken;
1866 if (!Current.isStringLiteral() || Current.is(TT_ImplicitStringLiteral))
1868 // We never consider raw string literals "multiline" for the purpose of
1869 // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased
1870 // (see TokenAnnotator::mustBreakBefore().
1871 if (Current.TokenText.startswith("R\""))
1873 if (Current.IsMultiline)
1875 if (Current.getNextNonComment() &&
1876 Current.getNextNonComment()->isStringLiteral())
1877 return true; // Implicit concatenation.
1878 if (Style.ColumnLimit != 0 &&
1879 State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
1881 return true; // String will be split.
1885 } // namespace format
1886 } // namespace clang