1 //===--- 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 #include "BreakableToken.h" 16 #include "ContinuationIndenter.h" 17 #include "WhitespaceManager.h" 18 #include "clang/Basic/OperatorPrecedence.h" 19 #include "clang/Basic/SourceManager.h" 20 #include "clang/Format/Format.h" 21 #include "llvm/Support/Debug.h" 22 #include <string> 23 24 #define DEBUG_TYPE "format-formatter" 25 26 namespace clang { 27 namespace format { 28 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) 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 41 // Returns \c true if \c Tok is the "." or "->" of a call and starts the next 42 // segment of a builder type call. 43 static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) { 44 return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope(); 45 } 46 47 // Returns \c true if \c Current starts a new parameter. 48 static bool startsNextParameter(const FormatToken &Current, 49 const FormatStyle &Style) { 50 const FormatToken &Previous = *Current.Previous; 51 if (Current.Type == TT_CtorInitializerComma && 52 Style.BreakConstructorInitializersBeforeComma) 53 return true; 54 return Previous.is(tok::comma) && !Current.isTrailingComment() && 55 (Previous.Type != TT_CtorInitializerComma || 56 !Style.BreakConstructorInitializersBeforeComma); 57 } 58 59 ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, 60 SourceManager &SourceMgr, 61 WhitespaceManager &Whitespaces, 62 encoding::Encoding Encoding, 63 bool BinPackInconclusiveFunctions) 64 : Style(Style), SourceMgr(SourceMgr), Whitespaces(Whitespaces), 65 Encoding(Encoding), 66 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), 67 CommentPragmasRegex(Style.CommentPragmas) {} 68 69 LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, 70 const AnnotatedLine *Line, 71 bool DryRun) { 72 LineState State; 73 State.FirstIndent = FirstIndent; 74 State.Column = FirstIndent; 75 State.Line = Line; 76 State.NextToken = Line->First; 77 State.Stack.push_back(ParenState(FirstIndent, Line->Level, FirstIndent, 78 /*AvoidBinPacking=*/false, 79 /*NoLineBreak=*/false)); 80 State.LineContainsContinuedForLoopSection = false; 81 State.StartOfStringLiteral = 0; 82 State.StartOfLineLevel = 0; 83 State.LowestLevelOnLine = 0; 84 State.IgnoreStackForComparison = false; 85 86 // The first token has already been indented and thus consumed. 87 moveStateToNextToken(State, DryRun, /*Newline=*/false); 88 return State; 89 } 90 91 bool ContinuationIndenter::canBreak(const LineState &State) { 92 const FormatToken &Current = *State.NextToken; 93 const FormatToken &Previous = *Current.Previous; 94 assert(&Previous == Current.Previous); 95 if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace && 96 Current.closesBlockTypeList(Style))) 97 return false; 98 // The opening "{" of a braced list has to be on the same line as the first 99 // element if it is nested in another braced init list or function call. 100 if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && 101 Previous.Type != TT_DictLiteral && Previous.BlockKind == BK_BracedInit && 102 Previous.Previous && 103 Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) 104 return false; 105 // This prevents breaks like: 106 // ... 107 // SomeParameter, OtherParameter).DoSomething( 108 // ... 109 // As they hide "DoSomething" and are generally bad for readability. 110 if (Previous.opensScope() && Previous.isNot(tok::l_brace) && 111 State.LowestLevelOnLine < State.StartOfLineLevel && 112 State.LowestLevelOnLine < Current.NestingLevel) 113 return false; 114 if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder) 115 return false; 116 117 // Don't create a 'hanging' indent if there are multiple blocks in a single 118 // statement. 119 if (Style.Language == FormatStyle::LK_JavaScript && 120 Previous.is(tok::l_brace) && State.Stack.size() > 1 && 121 State.Stack[State.Stack.size() - 2].JSFunctionInlined && 122 State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks) 123 return false; 124 125 return !State.Stack.back().NoLineBreak; 126 } 127 128 bool ContinuationIndenter::mustBreak(const LineState &State) { 129 const FormatToken &Current = *State.NextToken; 130 const FormatToken &Previous = *Current.Previous; 131 if (Current.MustBreakBefore || Current.Type == TT_InlineASMColon) 132 return true; 133 if (State.Stack.back().BreakBeforeClosingBrace && 134 Current.closesBlockTypeList(Style)) 135 return true; 136 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) 137 return true; 138 if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) || 139 (Style.BreakBeforeTernaryOperators && 140 (Current.is(tok::question) || (Current.Type == TT_ConditionalExpr && 141 Previous.isNot(tok::question)))) || 142 (!Style.BreakBeforeTernaryOperators && 143 (Previous.is(tok::question) || Previous.Type == TT_ConditionalExpr))) && 144 State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() && 145 !Current.isOneOf(tok::r_paren, tok::r_brace)) 146 return true; 147 if (Style.AlwaysBreakBeforeMultilineStrings && 148 State.Column > State.Stack.back().Indent && // Breaking saves columns. 149 !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at) && 150 Previous.Type != TT_InlineASMColon && 151 Previous.Type != TT_ConditionalExpr && nextIsMultilineString(State)) 152 return true; 153 if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) || 154 Previous.Type == TT_ArrayInitializerLSquare) && 155 Style.ColumnLimit > 0 && 156 getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State)) 157 return true; 158 if (Current.Type == TT_CtorInitializerColon && 159 ((Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All) || 160 Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0)) 161 return true; 162 163 if (State.Column < getNewLineColumn(State)) 164 return false; 165 if (!Style.BreakBeforeBinaryOperators) { 166 // If we need to break somewhere inside the LHS of a binary expression, we 167 // should also break after the operator. Otherwise, the formatting would 168 // hide the operator precedence, e.g. in: 169 // if (aaaaaaaaaaaaaa == 170 // bbbbbbbbbbbbbb && c) {.. 171 // For comparisons, we only apply this rule, if the LHS is a binary 172 // expression itself as otherwise, the line breaks seem superfluous. 173 // We need special cases for ">>" which we have split into two ">" while 174 // lexing in order to make template parsing easier. 175 // 176 // FIXME: We'll need something similar for styles that break before binary 177 // operators. 178 bool IsComparison = (Previous.getPrecedence() == prec::Relational || 179 Previous.getPrecedence() == prec::Equality) && 180 Previous.Previous && 181 Previous.Previous->Type != TT_BinaryOperator; // For >>. 182 bool LHSIsBinaryExpr = 183 Previous.Previous && Previous.Previous->EndsBinaryExpression; 184 if (Previous.Type == TT_BinaryOperator && 185 (!IsComparison || LHSIsBinaryExpr) && 186 Current.Type != TT_BinaryOperator && // For >>. 187 !Current.isTrailingComment() && !Previous.is(tok::lessless) && 188 Previous.getPrecedence() != prec::Assignment && 189 State.Stack.back().BreakBeforeParameter) 190 return true; 191 } 192 193 // Same as above, but for the first "<<" operator. 194 if (Current.is(tok::lessless) && Current.Type != TT_OverloadedOperator && 195 State.Stack.back().BreakBeforeParameter && 196 State.Stack.back().FirstLessLess == 0) 197 return true; 198 199 if (Current.Type == TT_SelectorName && 200 State.Stack.back().ObjCSelectorNameFound && 201 State.Stack.back().BreakBeforeParameter) 202 return true; 203 if (Previous.ClosesTemplateDeclaration && Current.NestingLevel == 0 && 204 !Current.isTrailingComment()) 205 return true; 206 207 // If the return type spans multiple lines, wrap before the function name. 208 if ((Current.Type == TT_FunctionDeclarationName || 209 Current.is(tok::kw_operator)) && 210 State.Stack.back().BreakBeforeParameter) 211 return true; 212 213 if (startsSegmentOfBuilderTypeCall(Current) && 214 (State.Stack.back().CallContinuation != 0 || 215 (State.Stack.back().BreakBeforeParameter && 216 State.Stack.back().ContainsUnwrappedBuilder))) 217 return true; 218 219 // The following could be precomputed as they do not depend on the state. 220 // However, as they should take effect only if the UnwrappedLine does not fit 221 // into the ColumnLimit, they are checked here in the ContinuationIndenter. 222 if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block && 223 Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment)) 224 return true; 225 226 return false; 227 } 228 229 unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline, 230 bool DryRun, 231 unsigned ExtraSpaces) { 232 const FormatToken &Current = *State.NextToken; 233 234 assert(!State.Stack.empty()); 235 if ((Current.Type == TT_ImplicitStringLiteral && 236 (Current.Previous->Tok.getIdentifierInfo() == nullptr || 237 Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() == 238 tok::pp_not_keyword))) { 239 // FIXME: Is this correct? 240 int WhitespaceLength = SourceMgr.getSpellingColumnNumber( 241 State.NextToken->WhitespaceRange.getEnd()) - 242 SourceMgr.getSpellingColumnNumber( 243 State.NextToken->WhitespaceRange.getBegin()); 244 State.Column += WhitespaceLength; 245 moveStateToNextToken(State, DryRun, /*Newline=*/false); 246 return 0; 247 } 248 249 unsigned Penalty = 0; 250 if (Newline) 251 Penalty = addTokenOnNewLine(State, DryRun); 252 else 253 addTokenOnCurrentLine(State, DryRun, ExtraSpaces); 254 255 return moveStateToNextToken(State, DryRun, Newline) + Penalty; 256 } 257 258 void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, 259 unsigned ExtraSpaces) { 260 FormatToken &Current = *State.NextToken; 261 const FormatToken &Previous = *State.NextToken->Previous; 262 if (Current.is(tok::equal) && 263 (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) && 264 State.Stack.back().VariablePos == 0) { 265 State.Stack.back().VariablePos = State.Column; 266 // Move over * and & if they are bound to the variable name. 267 const FormatToken *Tok = &Previous; 268 while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) { 269 State.Stack.back().VariablePos -= Tok->ColumnWidth; 270 if (Tok->SpacesRequiredBefore != 0) 271 break; 272 Tok = Tok->Previous; 273 } 274 if (Previous.PartOfMultiVariableDeclStmt) 275 State.Stack.back().LastSpace = State.Stack.back().VariablePos; 276 } 277 278 unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces; 279 280 if (!DryRun) 281 Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0, 282 Spaces, State.Column + Spaces); 283 284 if (Current.Type == TT_SelectorName && 285 !State.Stack.back().ObjCSelectorNameFound) { 286 if (Current.LongestObjCSelectorName == 0) 287 State.Stack.back().AlignColons = false; 288 else if (State.Stack.back().Indent + Current.LongestObjCSelectorName > 289 State.Column + Spaces + Current.ColumnWidth) 290 State.Stack.back().ColonPos = 291 State.Stack.back().Indent + Current.LongestObjCSelectorName; 292 else 293 State.Stack.back().ColonPos = State.Column + Spaces + Current.ColumnWidth; 294 } 295 296 if (Previous.opensScope() && Previous.Type != TT_ObjCMethodExpr && 297 (Current.Type != TT_LineComment || Previous.BlockKind == BK_BracedInit)) 298 State.Stack.back().Indent = State.Column + Spaces; 299 if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style)) 300 State.Stack.back().NoLineBreak = true; 301 if (startsSegmentOfBuilderTypeCall(Current)) 302 State.Stack.back().ContainsUnwrappedBuilder = true; 303 304 State.Column += Spaces; 305 if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) && 306 Previous.Previous && Previous.Previous->isOneOf(tok::kw_if, tok::kw_for)) 307 // Treat the condition inside an if as if it was a second function 308 // parameter, i.e. let nested calls have a continuation indent. 309 State.Stack.back().LastSpace = State.Column; 310 else if (!Current.isOneOf(tok::comment, tok::caret) && 311 (Previous.is(tok::comma) || 312 (Previous.is(tok::colon) && Previous.Type == TT_ObjCMethodExpr))) 313 State.Stack.back().LastSpace = State.Column; 314 else if ((Previous.Type == TT_BinaryOperator || 315 Previous.Type == TT_ConditionalExpr || 316 Previous.Type == TT_CtorInitializerColon) && 317 ((Previous.getPrecedence() != prec::Assignment && 318 (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 || 319 !Previous.LastOperator)) || 320 Current.StartsBinaryExpression)) 321 // Always indent relative to the RHS of the expression unless this is a 322 // simple assignment without binary expression on the RHS. Also indent 323 // relative to unary operators and the colons of constructor initializers. 324 State.Stack.back().LastSpace = State.Column; 325 else if (Previous.Type == TT_InheritanceColon) { 326 State.Stack.back().Indent = State.Column; 327 State.Stack.back().LastSpace = State.Column; 328 } else if (Previous.opensScope()) { 329 // If a function has a trailing call, indent all parameters from the 330 // opening parenthesis. This avoids confusing indents like: 331 // OuterFunction(InnerFunctionCall( // break 332 // ParameterToInnerFunction)) // break 333 // .SecondInnerFunctionCall(); 334 bool HasTrailingCall = false; 335 if (Previous.MatchingParen) { 336 const FormatToken *Next = Previous.MatchingParen->getNextNonComment(); 337 HasTrailingCall = Next && Next->isMemberAccess(); 338 } 339 if (HasTrailingCall && 340 State.Stack[State.Stack.size() - 2].CallContinuation == 0) 341 State.Stack.back().LastSpace = State.Column; 342 } 343 } 344 345 unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, 346 bool DryRun) { 347 FormatToken &Current = *State.NextToken; 348 const FormatToken &Previous = *State.NextToken->Previous; 349 350 // Extra penalty that needs to be added because of the way certain line 351 // breaks are chosen. 352 unsigned Penalty = 0; 353 354 const FormatToken *PreviousNonComment = Current.getPreviousNonComment(); 355 const FormatToken *NextNonComment = Previous.getNextNonComment(); 356 if (!NextNonComment) 357 NextNonComment = &Current; 358 // The first line break on any NestingLevel causes an extra penalty in order 359 // prefer similar line breaks. 360 if (!State.Stack.back().ContainsLineBreak) 361 Penalty += 15; 362 State.Stack.back().ContainsLineBreak = true; 363 364 Penalty += State.NextToken->SplitPenalty; 365 366 // Breaking before the first "<<" is generally not desirable if the LHS is 367 // short. Also always add the penalty if the LHS is split over mutliple lines 368 // to avoid unnecessary line breaks that just work around this penalty. 369 if (NextNonComment->is(tok::lessless) && 370 State.Stack.back().FirstLessLess == 0 && 371 (State.Column <= Style.ColumnLimit / 3 || 372 State.Stack.back().BreakBeforeParameter)) 373 Penalty += Style.PenaltyBreakFirstLessLess; 374 375 State.Column = getNewLineColumn(State); 376 if (NextNonComment->isMemberAccess()) { 377 if (State.Stack.back().CallContinuation == 0) 378 State.Stack.back().CallContinuation = State.Column; 379 } else if (NextNonComment->Type == TT_SelectorName) { 380 if (!State.Stack.back().ObjCSelectorNameFound) { 381 if (NextNonComment->LongestObjCSelectorName == 0) { 382 State.Stack.back().AlignColons = false; 383 } else { 384 State.Stack.back().ColonPos = 385 State.Stack.back().Indent + NextNonComment->LongestObjCSelectorName; 386 } 387 } else if (State.Stack.back().AlignColons && 388 State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) { 389 State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth; 390 } 391 } else if (PreviousNonComment && PreviousNonComment->is(tok::colon) && 392 (PreviousNonComment->Type == TT_ObjCMethodExpr || 393 PreviousNonComment->Type == TT_DictLiteral)) { 394 // FIXME: This is hacky, find a better way. The problem is that in an ObjC 395 // method expression, the block should be aligned to the line starting it, 396 // e.g.: 397 // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason 398 // ^(int *i) { 399 // // ... 400 // }]; 401 // Thus, we set LastSpace of the next higher NestingLevel, to which we move 402 // when we consume all of the "}"'s FakeRParens at the "{". 403 if (State.Stack.size() > 1) 404 State.Stack[State.Stack.size() - 2].LastSpace = 405 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 406 Style.ContinuationIndentWidth; 407 } 408 409 if ((Previous.isOneOf(tok::comma, tok::semi) && 410 !State.Stack.back().AvoidBinPacking) || 411 Previous.Type == TT_BinaryOperator) 412 State.Stack.back().BreakBeforeParameter = false; 413 if (Previous.Type == TT_TemplateCloser && Current.NestingLevel == 0) 414 State.Stack.back().BreakBeforeParameter = false; 415 if (NextNonComment->is(tok::question) || 416 (PreviousNonComment && PreviousNonComment->is(tok::question))) 417 State.Stack.back().BreakBeforeParameter = true; 418 419 if (!DryRun) { 420 unsigned Newlines = std::max( 421 1u, std::min(Current.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1)); 422 Whitespaces.replaceWhitespace(Current, Newlines, 423 State.Stack.back().IndentLevel, State.Column, 424 State.Column, State.Line->InPPDirective); 425 } 426 427 if (!Current.isTrailingComment()) 428 State.Stack.back().LastSpace = State.Column; 429 State.StartOfLineLevel = Current.NestingLevel; 430 State.LowestLevelOnLine = Current.NestingLevel; 431 432 // Any break on this level means that the parent level has been broken 433 // and we need to avoid bin packing there. 434 bool JavaScriptFormat = Style.Language == FormatStyle::LK_JavaScript && 435 Current.is(tok::r_brace) && 436 State.Stack.size() > 1 && 437 State.Stack[State.Stack.size() - 2].JSFunctionInlined; 438 if (!JavaScriptFormat) { 439 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { 440 State.Stack[i].BreakBeforeParameter = true; 441 } 442 } 443 444 if (PreviousNonComment && 445 !PreviousNonComment->isOneOf(tok::comma, tok::semi) && 446 PreviousNonComment->Type != TT_TemplateCloser && 447 PreviousNonComment->Type != TT_BinaryOperator && 448 Current.Type != TT_BinaryOperator && !PreviousNonComment->opensScope()) 449 State.Stack.back().BreakBeforeParameter = true; 450 451 // If we break after { or the [ of an array initializer, we should also break 452 // before the corresponding } or ]. 453 if (PreviousNonComment && 454 (PreviousNonComment->is(tok::l_brace) || 455 PreviousNonComment->Type == TT_ArrayInitializerLSquare)) 456 State.Stack.back().BreakBeforeClosingBrace = true; 457 458 if (State.Stack.back().AvoidBinPacking) { 459 // If we are breaking after '(', '{', '<', this is not bin packing 460 // unless AllowAllParametersOfDeclarationOnNextLine is false or this is a 461 // dict/object literal. 462 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) || 463 Previous.Type == TT_BinaryOperator) || 464 (!Style.AllowAllParametersOfDeclarationOnNextLine && 465 State.Line->MustBeDeclaration) || 466 Previous.Type == TT_DictLiteral) 467 State.Stack.back().BreakBeforeParameter = true; 468 } 469 470 return Penalty; 471 } 472 473 unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { 474 if (!State.NextToken || !State.NextToken->Previous) 475 return 0; 476 FormatToken &Current = *State.NextToken; 477 const FormatToken &Previous = *State.NextToken->Previous; 478 // If we are continuing an expression, we want to use the continuation indent. 479 unsigned ContinuationIndent = 480 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 481 Style.ContinuationIndentWidth; 482 const FormatToken *PreviousNonComment = Current.getPreviousNonComment(); 483 const FormatToken *NextNonComment = Previous.getNextNonComment(); 484 if (!NextNonComment) 485 NextNonComment = &Current; 486 if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block) 487 return Current.NestingLevel == 0 ? State.FirstIndent 488 : State.Stack.back().Indent; 489 if (Current.isOneOf(tok::r_brace, tok::r_square)) { 490 if (State.Stack.size() > 1 && 491 State.Stack[State.Stack.size() - 2].JSFunctionInlined) 492 return State.FirstIndent; 493 if (Current.closesBlockTypeList(Style) || 494 (Current.MatchingParen && 495 Current.MatchingParen->BlockKind == BK_BracedInit)) 496 return State.Stack[State.Stack.size() - 2].LastSpace; 497 else 498 return State.FirstIndent; 499 } 500 if (Current.is(tok::identifier) && Current.Next && 501 Current.Next->Type == TT_DictLiteral) 502 return State.Stack.back().Indent; 503 if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0) 504 return State.StartOfStringLiteral; 505 if (NextNonComment->is(tok::lessless) && 506 State.Stack.back().FirstLessLess != 0) 507 return State.Stack.back().FirstLessLess; 508 if (NextNonComment->isMemberAccess()) { 509 if (State.Stack.back().CallContinuation == 0) { 510 return ContinuationIndent; 511 } else { 512 return State.Stack.back().CallContinuation; 513 } 514 } 515 if (State.Stack.back().QuestionColumn != 0 && 516 ((NextNonComment->is(tok::colon) && 517 NextNonComment->Type == TT_ConditionalExpr) || 518 Previous.Type == TT_ConditionalExpr)) 519 return State.Stack.back().QuestionColumn; 520 if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) 521 return State.Stack.back().VariablePos; 522 if ((PreviousNonComment && (PreviousNonComment->ClosesTemplateDeclaration || 523 PreviousNonComment->Type == TT_AttributeParen)) || 524 (!Style.IndentWrappedFunctionNames && 525 (NextNonComment->is(tok::kw_operator) || 526 NextNonComment->Type == TT_FunctionDeclarationName))) 527 return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent); 528 if (NextNonComment->Type == TT_SelectorName) { 529 if (!State.Stack.back().ObjCSelectorNameFound) { 530 if (NextNonComment->LongestObjCSelectorName == 0) { 531 return State.Stack.back().Indent; 532 } else { 533 return State.Stack.back().Indent + 534 NextNonComment->LongestObjCSelectorName - 535 NextNonComment->ColumnWidth; 536 } 537 } else if (!State.Stack.back().AlignColons) { 538 return State.Stack.back().Indent; 539 } else if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth) { 540 return State.Stack.back().ColonPos - NextNonComment->ColumnWidth; 541 } else { 542 return State.Stack.back().Indent; 543 } 544 } 545 if (NextNonComment->Type == TT_ArraySubscriptLSquare) { 546 if (State.Stack.back().StartOfArraySubscripts != 0) 547 return State.Stack.back().StartOfArraySubscripts; 548 else 549 return ContinuationIndent; 550 } 551 if (NextNonComment->Type == TT_StartOfName || 552 Previous.isOneOf(tok::coloncolon, tok::equal)) { 553 return ContinuationIndent; 554 } 555 if (PreviousNonComment && PreviousNonComment->is(tok::colon) && 556 (PreviousNonComment->Type == TT_ObjCMethodExpr || 557 PreviousNonComment->Type == TT_DictLiteral)) 558 return ContinuationIndent; 559 if (NextNonComment->Type == TT_CtorInitializerColon) 560 return State.FirstIndent + Style.ConstructorInitializerIndentWidth; 561 if (NextNonComment->Type == TT_CtorInitializerComma) 562 return State.Stack.back().Indent; 563 if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment && 564 PreviousNonComment->isNot(tok::r_brace)) 565 // Ensure that we fall back to the continuation indent width instead of 566 // just flushing continuations left. 567 return State.Stack.back().Indent + Style.ContinuationIndentWidth; 568 return State.Stack.back().Indent; 569 } 570 571 unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, 572 bool DryRun, bool Newline) { 573 assert(State.Stack.size()); 574 const FormatToken &Current = *State.NextToken; 575 576 if (Current.Type == TT_InheritanceColon) 577 State.Stack.back().AvoidBinPacking = true; 578 if (Current.is(tok::lessless) && Current.Type != TT_OverloadedOperator) { 579 if (State.Stack.back().FirstLessLess == 0) 580 State.Stack.back().FirstLessLess = State.Column; 581 else 582 State.Stack.back().LastOperatorWrapped = Newline; 583 } 584 if ((Current.Type == TT_BinaryOperator && Current.isNot(tok::lessless)) || 585 Current.Type == TT_ConditionalExpr) 586 State.Stack.back().LastOperatorWrapped = Newline; 587 if (Current.Type == TT_ArraySubscriptLSquare && 588 State.Stack.back().StartOfArraySubscripts == 0) 589 State.Stack.back().StartOfArraySubscripts = State.Column; 590 if ((Current.is(tok::question) && Style.BreakBeforeTernaryOperators) || 591 (Current.getPreviousNonComment() && Current.isNot(tok::colon) && 592 Current.getPreviousNonComment()->is(tok::question) && 593 !Style.BreakBeforeTernaryOperators)) 594 State.Stack.back().QuestionColumn = State.Column; 595 if (!Current.opensScope() && !Current.closesScope()) 596 State.LowestLevelOnLine = 597 std::min(State.LowestLevelOnLine, Current.NestingLevel); 598 if (Current.isMemberAccess()) 599 State.Stack.back().StartOfFunctionCall = 600 Current.LastOperator ? 0 : State.Column + Current.ColumnWidth; 601 if (Current.Type == TT_SelectorName) 602 State.Stack.back().ObjCSelectorNameFound = true; 603 if (Current.Type == TT_CtorInitializerColon) { 604 // Indent 2 from the column, so: 605 // SomeClass::SomeClass() 606 // : First(...), ... 607 // Next(...) 608 // ^ line up here. 609 State.Stack.back().Indent = 610 State.Column + (Style.BreakConstructorInitializersBeforeComma ? 0 : 2); 611 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) 612 State.Stack.back().AvoidBinPacking = true; 613 State.Stack.back().BreakBeforeParameter = false; 614 } 615 616 // In ObjC method declaration we align on the ":" of parameters, but we need 617 // to ensure that we indent parameters on subsequent lines by at least our 618 // continuation indent width. 619 if (Current.Type == TT_ObjCMethodSpecifier) 620 State.Stack.back().Indent += Style.ContinuationIndentWidth; 621 622 // Insert scopes created by fake parenthesis. 623 const FormatToken *Previous = Current.getPreviousNonComment(); 624 625 // Add special behavior to support a format commonly used for JavaScript 626 // closures: 627 // SomeFunction(function() { 628 // foo(); 629 // bar(); 630 // }, a, b, c); 631 if (Style.Language == FormatStyle::LK_JavaScript) { 632 if (Current.isNot(tok::comment) && Previous && Previous->is(tok::l_brace) && 633 State.Stack.size() > 1) { 634 if (State.Stack[State.Stack.size() - 2].JSFunctionInlined && Newline) { 635 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) { 636 State.Stack[i].NoLineBreak = true; 637 } 638 } 639 State.Stack[State.Stack.size() - 2].JSFunctionInlined = false; 640 } 641 if (Current.TokenText == "function") 642 State.Stack.back().JSFunctionInlined = !Newline; 643 } 644 645 moveStatePastFakeLParens(State, Newline); 646 moveStatePastScopeOpener(State, Newline); 647 moveStatePastScopeCloser(State); 648 moveStatePastFakeRParens(State); 649 650 if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) { 651 State.StartOfStringLiteral = State.Column; 652 } else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) && 653 !Current.isStringLiteral()) { 654 State.StartOfStringLiteral = 0; 655 } 656 657 State.Column += Current.ColumnWidth; 658 State.NextToken = State.NextToken->Next; 659 unsigned Penalty = breakProtrudingToken(Current, State, DryRun); 660 if (State.Column > getColumnLimit(State)) { 661 unsigned ExcessCharacters = State.Column - getColumnLimit(State); 662 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; 663 } 664 665 if (Current.Role) 666 Current.Role->formatFromToken(State, this, DryRun); 667 // If the previous has a special role, let it consume tokens as appropriate. 668 // It is necessary to start at the previous token for the only implemented 669 // role (comma separated list). That way, the decision whether or not to break 670 // after the "{" is already done and both options are tried and evaluated. 671 // FIXME: This is ugly, find a better way. 672 if (Previous && Previous->Role) 673 Penalty += Previous->Role->formatAfterToken(State, this, DryRun); 674 675 return Penalty; 676 } 677 678 void ContinuationIndenter::moveStatePastFakeLParens(LineState &State, 679 bool Newline) { 680 const FormatToken &Current = *State.NextToken; 681 const FormatToken *Previous = Current.getPreviousNonComment(); 682 683 // Don't add extra indentation for the first fake parenthesis after 684 // 'return', assignments or opening <({[. The indentation for these cases 685 // is special cased. 686 bool SkipFirstExtraIndent = 687 (Previous && (Previous->opensScope() || Previous->is(tok::kw_return) || 688 Previous->getPrecedence() == prec::Assignment || 689 Previous->Type == TT_ObjCMethodExpr)); 690 for (SmallVectorImpl<prec::Level>::const_reverse_iterator 691 I = Current.FakeLParens.rbegin(), 692 E = Current.FakeLParens.rend(); 693 I != E; ++I) { 694 ParenState NewParenState = State.Stack.back(); 695 NewParenState.ContainsLineBreak = false; 696 697 // Indent from 'LastSpace' unless this the fake parentheses encapsulating a 698 // builder type call after 'return'. If such a call is line-wrapped, we 699 // commonly just want to indent from the start of the line. 700 if (!Previous || Previous->isNot(tok::kw_return) || *I > 0) 701 NewParenState.Indent = 702 std::max(std::max(State.Column, NewParenState.Indent), 703 State.Stack.back().LastSpace); 704 705 // Don't allow the RHS of an operator to be split over multiple lines unless 706 // there is a line-break right after the operator. 707 // Exclude relational operators, as there, it is always more desirable to 708 // have the LHS 'left' of the RHS. 709 if (Previous && Previous->getPrecedence() > prec::Assignment && 710 (Previous->Type == TT_BinaryOperator || 711 Previous->Type == TT_ConditionalExpr) && 712 Previous->getPrecedence() != prec::Relational) { 713 bool BreakBeforeOperator = Previous->is(tok::lessless) || 714 (Previous->Type == TT_BinaryOperator && 715 Style.BreakBeforeBinaryOperators) || 716 (Previous->Type == TT_ConditionalExpr && 717 Style.BreakBeforeTernaryOperators); 718 if ((!Newline && !BreakBeforeOperator) || 719 (!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator)) 720 NewParenState.NoLineBreak = true; 721 } 722 723 // Do not indent relative to the fake parentheses inserted for "." or "->". 724 // This is a special case to make the following to statements consistent: 725 // OuterFunction(InnerFunctionCall( // break 726 // ParameterToInnerFunction)); 727 // OuterFunction(SomeObject.InnerFunctionCall( // break 728 // ParameterToInnerFunction)); 729 if (*I > prec::Unknown) 730 NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column); 731 NewParenState.StartOfFunctionCall = State.Column; 732 733 // Always indent conditional expressions. Never indent expression where 734 // the 'operator' is ',', ';' or an assignment (i.e. *I <= 735 // prec::Assignment) as those have different indentation rules. Indent 736 // other expression, unless the indentation needs to be skipped. 737 if (*I == prec::Conditional || 738 (!SkipFirstExtraIndent && *I > prec::Assignment && 739 !Style.BreakBeforeBinaryOperators)) 740 NewParenState.Indent += Style.ContinuationIndentWidth; 741 if ((Previous && !Previous->opensScope()) || *I > prec::Comma) 742 NewParenState.BreakBeforeParameter = false; 743 State.Stack.push_back(NewParenState); 744 SkipFirstExtraIndent = false; 745 } 746 } 747 748 // Remove the fake r_parens after 'Tok'. 749 static void consumeRParens(LineState& State, const FormatToken &Tok) { 750 for (unsigned i = 0, e = Tok.FakeRParens; i != e; ++i) { 751 unsigned VariablePos = State.Stack.back().VariablePos; 752 assert(State.Stack.size() > 1); 753 if (State.Stack.size() == 1) { 754 // Do not pop the last element. 755 break; 756 } 757 State.Stack.pop_back(); 758 State.Stack.back().VariablePos = VariablePos; 759 } 760 } 761 762 // Returns whether 'Tok' opens or closes a scope requiring special handling 763 // of the subsequent fake r_parens. 764 // 765 // For example, if this is an l_brace starting a nested block, we pretend (wrt. 766 // to indentation) that we already consumed the corresponding r_brace. Thus, we 767 // remove all ParenStates caused by fake parentheses that end at the r_brace. 768 // The net effect of this is that we don't indent relative to the l_brace, if 769 // the nested block is the last parameter of a function. This formats: 770 // 771 // SomeFunction(a, [] { 772 // f(); // break 773 // }); 774 // 775 // instead of: 776 // SomeFunction(a, [] { 777 // f(); // break 778 // }); 779 static bool fakeRParenSpecialCase(const LineState &State) { 780 const FormatToken &Tok = *State.NextToken; 781 if (!Tok.MatchingParen) 782 return false; 783 const FormatToken *Left = &Tok; 784 if (Tok.isOneOf(tok::r_brace, tok::r_square)) 785 Left = Tok.MatchingParen; 786 return !State.Stack.back().HasMultipleNestedBlocks && 787 Left->isOneOf(tok::l_brace, tok::l_square) && 788 (Left->BlockKind == BK_Block || 789 Left->Type == TT_ArrayInitializerLSquare || 790 Left->Type == TT_DictLiteral); 791 } 792 793 void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) { 794 // Don't remove FakeRParens attached to r_braces that surround nested blocks 795 // as they will have been removed early (see above). 796 if (fakeRParenSpecialCase(State)) 797 return; 798 799 consumeRParens(State, *State.NextToken); 800 } 801 802 void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, 803 bool Newline) { 804 const FormatToken &Current = *State.NextToken; 805 if (!Current.opensScope()) 806 return; 807 808 if (Current.MatchingParen && Current.BlockKind == BK_Block) { 809 moveStateToNewBlock(State); 810 return; 811 } 812 813 unsigned NewIndent; 814 unsigned NewIndentLevel = State.Stack.back().IndentLevel; 815 bool AvoidBinPacking; 816 bool BreakBeforeParameter = false; 817 if (Current.is(tok::l_brace) || Current.Type == TT_ArrayInitializerLSquare) { 818 if (fakeRParenSpecialCase(State)) 819 consumeRParens(State, *Current.MatchingParen); 820 821 NewIndent = State.Stack.back().LastSpace; 822 if (Current.opensBlockTypeList(Style)) { 823 NewIndent += Style.IndentWidth; 824 NewIndent = std::min(State.Column + 2, NewIndent); 825 ++NewIndentLevel; 826 } else { 827 NewIndent += Style.ContinuationIndentWidth; 828 NewIndent = std::min(State.Column + 1, NewIndent); 829 } 830 const FormatToken *NextNoComment = Current.getNextNonComment(); 831 AvoidBinPacking = Current.Type == TT_ArrayInitializerLSquare || 832 Current.Type == TT_DictLiteral || 833 Style.Language == FormatStyle::LK_Proto || 834 !Style.BinPackParameters || 835 (NextNoComment && 836 NextNoComment->Type == TT_DesignatedInitializerPeriod); 837 } else { 838 NewIndent = Style.ContinuationIndentWidth + 839 std::max(State.Stack.back().LastSpace, 840 State.Stack.back().StartOfFunctionCall); 841 AvoidBinPacking = !Style.BinPackParameters || 842 (Style.ExperimentalAutoDetectBinPacking && 843 (Current.PackingKind == PPK_OnePerLine || 844 (!BinPackInconclusiveFunctions && 845 Current.PackingKind == PPK_Inconclusive))); 846 // If this '[' opens an ObjC call, determine whether all parameters fit 847 // into one line and put one per line if they don't. 848 if (Current.Type == TT_ObjCMethodExpr && Style.ColumnLimit != 0 && 849 getLengthToMatchingParen(Current) + State.Column > 850 getColumnLimit(State)) 851 BreakBeforeParameter = true; 852 } 853 bool NoLineBreak = State.Stack.back().NoLineBreak || 854 (Current.Type == TT_TemplateOpener && 855 State.Stack.back().ContainsUnwrappedBuilder); 856 State.Stack.push_back(ParenState(NewIndent, NewIndentLevel, 857 State.Stack.back().LastSpace, 858 AvoidBinPacking, NoLineBreak)); 859 State.Stack.back().BreakBeforeParameter = BreakBeforeParameter; 860 State.Stack.back().HasMultipleNestedBlocks = Current.BlockParameterCount > 1; 861 } 862 863 void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) { 864 const FormatToken &Current = *State.NextToken; 865 if (!Current.closesScope()) 866 return; 867 868 // If we encounter a closing ), ], } or >, we can remove a level from our 869 // stacks. 870 if (State.Stack.size() > 1 && 871 (Current.isOneOf(tok::r_paren, tok::r_square) || 872 (Current.is(tok::r_brace) && State.NextToken != State.Line->First) || 873 State.NextToken->Type == TT_TemplateCloser)) 874 State.Stack.pop_back(); 875 876 if (Current.is(tok::r_square)) { 877 // If this ends the array subscript expr, reset the corresponding value. 878 const FormatToken *NextNonComment = Current.getNextNonComment(); 879 if (NextNonComment && NextNonComment->isNot(tok::l_square)) 880 State.Stack.back().StartOfArraySubscripts = 0; 881 } 882 } 883 884 void ContinuationIndenter::moveStateToNewBlock(LineState &State) { 885 // If we have already found more than one lambda introducers on this level, we 886 // opt out of this because similarity between the lambdas is more important. 887 if (fakeRParenSpecialCase(State)) 888 consumeRParens(State, *State.NextToken->MatchingParen); 889 890 // For some reason, ObjC blocks are indented like continuations. 891 unsigned NewIndent = State.Stack.back().LastSpace + 892 (State.NextToken->Type == TT_ObjCBlockLBrace 893 ? Style.ContinuationIndentWidth 894 : Style.IndentWidth); 895 State.Stack.push_back(ParenState( 896 NewIndent, /*NewIndentLevel=*/State.Stack.back().IndentLevel + 1, 897 State.Stack.back().LastSpace, /*AvoidBinPacking=*/true, 898 State.Stack.back().NoLineBreak)); 899 State.Stack.back().BreakBeforeParameter = true; 900 } 901 902 unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current, 903 LineState &State) { 904 // Break before further function parameters on all levels. 905 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) 906 State.Stack[i].BreakBeforeParameter = true; 907 908 unsigned ColumnsUsed = State.Column; 909 // We can only affect layout of the first and the last line, so the penalty 910 // for all other lines is constant, and we ignore it. 911 State.Column = Current.LastLineColumnWidth; 912 913 if (ColumnsUsed > getColumnLimit(State)) 914 return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State)); 915 return 0; 916 } 917 918 static bool getRawStringLiteralPrefixPostfix(StringRef Text, StringRef &Prefix, 919 StringRef &Postfix) { 920 if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") || 921 Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") || 922 Text.startswith(Prefix = "LR\"")) { 923 size_t ParenPos = Text.find('('); 924 if (ParenPos != StringRef::npos) { 925 StringRef Delimiter = 926 Text.substr(Prefix.size(), ParenPos - Prefix.size()); 927 Prefix = Text.substr(0, ParenPos + 1); 928 Postfix = Text.substr(Text.size() - 2 - Delimiter.size()); 929 return Postfix.front() == ')' && Postfix.back() == '"' && 930 Postfix.substr(1).startswith(Delimiter); 931 } 932 } 933 return false; 934 } 935 936 unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, 937 LineState &State, 938 bool DryRun) { 939 // Don't break multi-line tokens other than block comments. Instead, just 940 // update the state. 941 if (Current.Type != TT_BlockComment && Current.IsMultiline) 942 return addMultilineToken(Current, State); 943 944 // Don't break implicit string literals. 945 if (Current.Type == TT_ImplicitStringLiteral) 946 return 0; 947 948 if (!Current.isStringLiteral() && !Current.is(tok::comment)) 949 return 0; 950 951 std::unique_ptr<BreakableToken> Token; 952 unsigned StartColumn = State.Column - Current.ColumnWidth; 953 unsigned ColumnLimit = getColumnLimit(State); 954 955 if (Current.isStringLiteral()) { 956 // Don't break string literals inside preprocessor directives (except for 957 // #define directives, as their contents are stored in separate lines and 958 // are not affected by this check). 959 // This way we avoid breaking code with line directives and unknown 960 // preprocessor directives that contain long string literals. 961 if (State.Line->Type == LT_PreprocessorDirective) 962 return 0; 963 // Exempts unterminated string literals from line breaking. The user will 964 // likely want to terminate the string before any line breaking is done. 965 if (Current.IsUnterminatedLiteral) 966 return 0; 967 968 StringRef Text = Current.TokenText; 969 StringRef Prefix; 970 StringRef Postfix; 971 bool IsNSStringLiteral = false; 972 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'. 973 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to 974 // reduce the overhead) for each FormatToken, which is a string, so that we 975 // don't run multiple checks here on the hot path. 976 if (Text.startswith("\"") && Current.Previous && 977 Current.Previous->is(tok::at)) { 978 IsNSStringLiteral = true; 979 Prefix = "@\""; 980 } 981 if ((Text.endswith(Postfix = "\"") && 982 (IsNSStringLiteral || Text.startswith(Prefix = "\"") || 983 Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") || 984 Text.startswith(Prefix = "u8\"") || 985 Text.startswith(Prefix = "L\""))) || 986 (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) || 987 getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) { 988 Token.reset(new BreakableStringLiteral( 989 Current, State.Line->Level, StartColumn, Prefix, Postfix, 990 State.Line->InPPDirective, Encoding, Style)); 991 } else { 992 return 0; 993 } 994 } else if (Current.Type == TT_BlockComment && Current.isTrailingComment()) { 995 if (CommentPragmasRegex.match(Current.TokenText.substr(2))) 996 return 0; 997 Token.reset(new BreakableBlockComment( 998 Current, State.Line->Level, StartColumn, Current.OriginalColumn, 999 !Current.Previous, State.Line->InPPDirective, Encoding, Style)); 1000 } else if (Current.Type == TT_LineComment && 1001 (Current.Previous == nullptr || 1002 Current.Previous->Type != TT_ImplicitStringLiteral)) { 1003 if (CommentPragmasRegex.match(Current.TokenText.substr(2))) 1004 return 0; 1005 Token.reset(new BreakableLineComment(Current, State.Line->Level, 1006 StartColumn, /*InPPDirective=*/false, 1007 Encoding, Style)); 1008 // We don't insert backslashes when breaking line comments. 1009 ColumnLimit = Style.ColumnLimit; 1010 } else { 1011 return 0; 1012 } 1013 if (Current.UnbreakableTailLength >= ColumnLimit) 1014 return 0; 1015 1016 unsigned RemainingSpace = ColumnLimit - Current.UnbreakableTailLength; 1017 bool BreakInserted = false; 1018 unsigned Penalty = 0; 1019 unsigned RemainingTokenColumns = 0; 1020 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount(); 1021 LineIndex != EndIndex; ++LineIndex) { 1022 if (!DryRun) 1023 Token->replaceWhitespaceBefore(LineIndex, Whitespaces); 1024 unsigned TailOffset = 0; 1025 RemainingTokenColumns = 1026 Token->getLineLengthAfterSplit(LineIndex, TailOffset, StringRef::npos); 1027 while (RemainingTokenColumns > RemainingSpace) { 1028 BreakableToken::Split Split = 1029 Token->getSplit(LineIndex, TailOffset, ColumnLimit); 1030 if (Split.first == StringRef::npos) { 1031 // The last line's penalty is handled in addNextStateToQueue(). 1032 if (LineIndex < EndIndex - 1) 1033 Penalty += Style.PenaltyExcessCharacter * 1034 (RemainingTokenColumns - RemainingSpace); 1035 break; 1036 } 1037 assert(Split.first != 0); 1038 unsigned NewRemainingTokenColumns = Token->getLineLengthAfterSplit( 1039 LineIndex, TailOffset + Split.first + Split.second, StringRef::npos); 1040 1041 // We can remove extra whitespace instead of breaking the line. 1042 if (RemainingTokenColumns + 1 - Split.second <= RemainingSpace) { 1043 RemainingTokenColumns = 0; 1044 if (!DryRun) 1045 Token->replaceWhitespace(LineIndex, TailOffset, Split, Whitespaces); 1046 break; 1047 } 1048 1049 // When breaking before a tab character, it may be moved by a few columns, 1050 // but will still be expanded to the next tab stop, so we don't save any 1051 // columns. 1052 if (NewRemainingTokenColumns == RemainingTokenColumns) 1053 break; 1054 1055 assert(NewRemainingTokenColumns < RemainingTokenColumns); 1056 if (!DryRun) 1057 Token->insertBreak(LineIndex, TailOffset, Split, Whitespaces); 1058 Penalty += Current.SplitPenalty; 1059 unsigned ColumnsUsed = 1060 Token->getLineLengthAfterSplit(LineIndex, TailOffset, Split.first); 1061 if (ColumnsUsed > ColumnLimit) { 1062 Penalty += Style.PenaltyExcessCharacter * (ColumnsUsed - ColumnLimit); 1063 } 1064 TailOffset += Split.first + Split.second; 1065 RemainingTokenColumns = NewRemainingTokenColumns; 1066 BreakInserted = true; 1067 } 1068 } 1069 1070 State.Column = RemainingTokenColumns; 1071 1072 if (BreakInserted) { 1073 // If we break the token inside a parameter list, we need to break before 1074 // the next parameter on all levels, so that the next parameter is clearly 1075 // visible. Line comments already introduce a break. 1076 if (Current.Type != TT_LineComment) { 1077 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) 1078 State.Stack[i].BreakBeforeParameter = true; 1079 } 1080 1081 Penalty += Current.isStringLiteral() ? Style.PenaltyBreakString 1082 : Style.PenaltyBreakComment; 1083 1084 State.Stack.back().LastSpace = StartColumn; 1085 } 1086 return Penalty; 1087 } 1088 1089 unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const { 1090 // In preprocessor directives reserve two chars for trailing " \" 1091 return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0); 1092 } 1093 1094 bool ContinuationIndenter::nextIsMultilineString(const LineState &State) { 1095 const FormatToken &Current = *State.NextToken; 1096 if (!Current.isStringLiteral() || Current.Type == TT_ImplicitStringLiteral) 1097 return false; 1098 // We never consider raw string literals "multiline" for the purpose of 1099 // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased 1100 // (see TokenAnnotator::mustBreakBefore(). 1101 if (Current.TokenText.startswith("R\"")) 1102 return false; 1103 if (Current.IsMultiline) 1104 return true; 1105 if (Current.getNextNonComment() && 1106 Current.getNextNonComment()->isStringLiteral()) 1107 return true; // Implicit concatenation. 1108 if (State.Column + Current.ColumnWidth + Current.UnbreakableTailLength > 1109 Style.ColumnLimit) 1110 return true; // String will be split. 1111 return false; 1112 } 1113 1114 } // namespace format 1115 } // namespace clang 1116