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