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