1 //===--- ContinuationIndenter.cpp - Format C++ code -----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 /// 9 /// \file 10 /// This file implements the continuation indenter. 11 /// 12 //===----------------------------------------------------------------------===// 13 14 #include "ContinuationIndenter.h" 15 #include "BreakableToken.h" 16 #include "FormatInternal.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 23 #define DEBUG_TYPE "format-indenter" 24 25 namespace clang { 26 namespace format { 27 28 // Returns true if a TT_SelectorName should be indented when wrapped, 29 // false otherwise. 30 static bool shouldIndentWrappedSelectorName(const FormatStyle &Style, 31 LineType LineType) { 32 return Style.IndentWrappedFunctionNames || LineType == LT_ObjCMethodDecl; 33 } 34 35 // Returns the length of everything up to the first possible line break after 36 // the ), ], } or > matching \c Tok. 37 static unsigned getLengthToMatchingParen(const FormatToken &Tok, 38 const std::vector<ParenState> &Stack) { 39 // Normally whether or not a break before T is possible is calculated and 40 // stored in T.CanBreakBefore. Braces, array initializers and text proto 41 // messages like `key: < ... >` are an exception: a break is possible 42 // before a closing brace R if a break was inserted after the corresponding 43 // opening brace. The information about whether or not a break is needed 44 // before a closing brace R is stored in the ParenState field 45 // S.BreakBeforeClosingBrace where S is the state that R closes. 46 // 47 // In order to decide whether there can be a break before encountered right 48 // braces, this implementation iterates over the sequence of tokens and over 49 // the paren stack in lockstep, keeping track of the stack level which visited 50 // right braces correspond to in MatchingStackIndex. 51 // 52 // For example, consider: 53 // L. <- line number 54 // 1. { 55 // 2. {1}, 56 // 3. {2}, 57 // 4. {{3}}} 58 // ^ where we call this method with this token. 59 // The paren stack at this point contains 3 brace levels: 60 // 0. { at line 1, BreakBeforeClosingBrace: true 61 // 1. first { at line 4, BreakBeforeClosingBrace: false 62 // 2. second { at line 4, BreakBeforeClosingBrace: false, 63 // where there might be fake parens levels in-between these levels. 64 // The algorithm will start at the first } on line 4, which is the matching 65 // brace of the initial left brace and at level 2 of the stack. Then, 66 // examining BreakBeforeClosingBrace: false at level 2, it will continue to 67 // the second } on line 4, and will traverse the stack downwards until it 68 // finds the matching { on level 1. Then, examining BreakBeforeClosingBrace: 69 // false at level 1, it will continue to the third } on line 4 and will 70 // traverse the stack downwards until it finds the matching { on level 0. 71 // Then, examining BreakBeforeClosingBrace: true at level 0, the algorithm 72 // will stop and will use the second } on line 4 to determine the length to 73 // return, as in this example the range will include the tokens: {3}} 74 // 75 // The algorithm will only traverse the stack if it encounters braces, array 76 // initializer squares or text proto angle brackets. 77 if (!Tok.MatchingParen) 78 return 0; 79 FormatToken *End = Tok.MatchingParen; 80 // Maintains a stack level corresponding to the current End token. 81 int MatchingStackIndex = Stack.size() - 1; 82 // Traverses the stack downwards, looking for the level to which LBrace 83 // corresponds. Returns either a pointer to the matching level or nullptr if 84 // LParen is not found in the initial portion of the stack up to 85 // MatchingStackIndex. 86 auto FindParenState = [&](const FormatToken *LBrace) -> const ParenState * { 87 while (MatchingStackIndex >= 0 && Stack[MatchingStackIndex].Tok != LBrace) 88 --MatchingStackIndex; 89 return MatchingStackIndex >= 0 ? &Stack[MatchingStackIndex] : nullptr; 90 }; 91 for (; End->Next; End = End->Next) { 92 if (End->Next->CanBreakBefore) 93 break; 94 if (!End->Next->closesScope()) 95 continue; 96 if (End->Next->MatchingParen && 97 End->Next->MatchingParen->isOneOf( 98 tok::l_brace, TT_ArrayInitializerLSquare, tok::less)) { 99 const ParenState *State = FindParenState(End->Next->MatchingParen); 100 if (State && State->BreakBeforeClosingBrace) 101 break; 102 } 103 } 104 return End->TotalLength - Tok.TotalLength + 1; 105 } 106 107 static unsigned getLengthToNextOperator(const FormatToken &Tok) { 108 if (!Tok.NextOperator) 109 return 0; 110 return Tok.NextOperator->TotalLength - Tok.TotalLength; 111 } 112 113 // Returns \c true if \c Tok is the "." or "->" of a call and starts the next 114 // segment of a builder type call. 115 static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) { 116 return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope(); 117 } 118 119 // Returns \c true if \c Current starts a new parameter. 120 static bool startsNextParameter(const FormatToken &Current, 121 const FormatStyle &Style) { 122 const FormatToken &Previous = *Current.Previous; 123 if (Current.is(TT_CtorInitializerComma) && 124 Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) 125 return true; 126 if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName)) 127 return true; 128 return Previous.is(tok::comma) && !Current.isTrailingComment() && 129 ((Previous.isNot(TT_CtorInitializerComma) || 130 Style.BreakConstructorInitializers != 131 FormatStyle::BCIS_BeforeComma) && 132 (Previous.isNot(TT_InheritanceComma) || 133 Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma)); 134 } 135 136 static bool opensProtoMessageField(const FormatToken &LessTok, 137 const FormatStyle &Style) { 138 if (LessTok.isNot(tok::less)) 139 return false; 140 return Style.Language == FormatStyle::LK_TextProto || 141 (Style.Language == FormatStyle::LK_Proto && 142 (LessTok.NestingLevel > 0 || 143 (LessTok.Previous && LessTok.Previous->is(tok::equal)))); 144 } 145 146 // Returns the delimiter of a raw string literal, or None if TokenText is not 147 // the text of a raw string literal. The delimiter could be the empty string. 148 // For example, the delimiter of R"deli(cont)deli" is deli. 149 static llvm::Optional<StringRef> getRawStringDelimiter(StringRef TokenText) { 150 if (TokenText.size() < 5 // The smallest raw string possible is 'R"()"'. 151 || !TokenText.startswith("R\"") || !TokenText.endswith("\"")) 152 return None; 153 154 // A raw string starts with 'R"<delimiter>(' and delimiter is ascii and has 155 // size at most 16 by the standard, so the first '(' must be among the first 156 // 19 bytes. 157 size_t LParenPos = TokenText.substr(0, 19).find_first_of('('); 158 if (LParenPos == StringRef::npos) 159 return None; 160 StringRef Delimiter = TokenText.substr(2, LParenPos - 2); 161 162 // Check that the string ends in ')Delimiter"'. 163 size_t RParenPos = TokenText.size() - Delimiter.size() - 2; 164 if (TokenText[RParenPos] != ')') 165 return None; 166 if (!TokenText.substr(RParenPos + 1).startswith(Delimiter)) 167 return None; 168 return Delimiter; 169 } 170 171 // Returns the canonical delimiter for \p Language, or the empty string if no 172 // canonical delimiter is specified. 173 static StringRef 174 getCanonicalRawStringDelimiter(const FormatStyle &Style, 175 FormatStyle::LanguageKind Language) { 176 for (const auto &Format : Style.RawStringFormats) { 177 if (Format.Language == Language) 178 return StringRef(Format.CanonicalDelimiter); 179 } 180 return ""; 181 } 182 183 RawStringFormatStyleManager::RawStringFormatStyleManager( 184 const FormatStyle &CodeStyle) { 185 for (const auto &RawStringFormat : CodeStyle.RawStringFormats) { 186 llvm::Optional<FormatStyle> LanguageStyle = 187 CodeStyle.GetLanguageStyle(RawStringFormat.Language); 188 if (!LanguageStyle) { 189 FormatStyle PredefinedStyle; 190 if (!getPredefinedStyle(RawStringFormat.BasedOnStyle, 191 RawStringFormat.Language, &PredefinedStyle)) { 192 PredefinedStyle = getLLVMStyle(); 193 PredefinedStyle.Language = RawStringFormat.Language; 194 } 195 LanguageStyle = PredefinedStyle; 196 } 197 LanguageStyle->ColumnLimit = CodeStyle.ColumnLimit; 198 for (StringRef Delimiter : RawStringFormat.Delimiters) { 199 DelimiterStyle.insert({Delimiter, *LanguageStyle}); 200 } 201 for (StringRef EnclosingFunction : RawStringFormat.EnclosingFunctions) { 202 EnclosingFunctionStyle.insert({EnclosingFunction, *LanguageStyle}); 203 } 204 } 205 } 206 207 llvm::Optional<FormatStyle> 208 RawStringFormatStyleManager::getDelimiterStyle(StringRef Delimiter) const { 209 auto It = DelimiterStyle.find(Delimiter); 210 if (It == DelimiterStyle.end()) 211 return None; 212 return It->second; 213 } 214 215 llvm::Optional<FormatStyle> 216 RawStringFormatStyleManager::getEnclosingFunctionStyle( 217 StringRef EnclosingFunction) const { 218 auto It = EnclosingFunctionStyle.find(EnclosingFunction); 219 if (It == EnclosingFunctionStyle.end()) 220 return None; 221 return It->second; 222 } 223 224 ContinuationIndenter::ContinuationIndenter(const FormatStyle &Style, 225 const AdditionalKeywords &Keywords, 226 const SourceManager &SourceMgr, 227 WhitespaceManager &Whitespaces, 228 encoding::Encoding Encoding, 229 bool BinPackInconclusiveFunctions) 230 : Style(Style), Keywords(Keywords), SourceMgr(SourceMgr), 231 Whitespaces(Whitespaces), Encoding(Encoding), 232 BinPackInconclusiveFunctions(BinPackInconclusiveFunctions), 233 CommentPragmasRegex(Style.CommentPragmas), RawStringFormats(Style) {} 234 235 LineState ContinuationIndenter::getInitialState(unsigned FirstIndent, 236 unsigned FirstStartColumn, 237 const AnnotatedLine *Line, 238 bool DryRun) { 239 LineState State; 240 State.FirstIndent = FirstIndent; 241 if (FirstStartColumn && Line->First->NewlinesBefore == 0) 242 State.Column = FirstStartColumn; 243 else 244 State.Column = FirstIndent; 245 // With preprocessor directive indentation, the line starts on column 0 246 // since it's indented after the hash, but FirstIndent is set to the 247 // preprocessor indent. 248 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && 249 (Line->Type == LT_PreprocessorDirective || 250 Line->Type == LT_ImportStatement)) 251 State.Column = 0; 252 State.Line = Line; 253 State.NextToken = Line->First; 254 State.Stack.push_back(ParenState(/*Tok=*/nullptr, FirstIndent, FirstIndent, 255 /*AvoidBinPacking=*/false, 256 /*NoLineBreak=*/false)); 257 State.LineContainsContinuedForLoopSection = false; 258 State.NoContinuation = false; 259 State.StartOfStringLiteral = 0; 260 State.StartOfLineLevel = 0; 261 State.LowestLevelOnLine = 0; 262 State.IgnoreStackForComparison = false; 263 264 if (Style.Language == FormatStyle::LK_TextProto) { 265 // We need this in order to deal with the bin packing of text fields at 266 // global scope. 267 State.Stack.back().AvoidBinPacking = true; 268 State.Stack.back().BreakBeforeParameter = true; 269 State.Stack.back().AlignColons = false; 270 } 271 272 // The first token has already been indented and thus consumed. 273 moveStateToNextToken(State, DryRun, /*Newline=*/false); 274 return State; 275 } 276 277 bool ContinuationIndenter::canBreak(const LineState &State) { 278 const FormatToken &Current = *State.NextToken; 279 const FormatToken &Previous = *Current.Previous; 280 assert(&Previous == Current.Previous); 281 if (!Current.CanBreakBefore && !(State.Stack.back().BreakBeforeClosingBrace && 282 Current.closesBlockOrBlockTypeList(Style))) 283 return false; 284 // The opening "{" of a braced list has to be on the same line as the first 285 // element if it is nested in another braced init list or function call. 286 if (!Current.MustBreakBefore && Previous.is(tok::l_brace) && 287 Previous.isNot(TT_DictLiteral) && Previous.BlockKind == BK_BracedInit && 288 Previous.Previous && 289 Previous.Previous->isOneOf(tok::l_brace, tok::l_paren, tok::comma)) 290 return false; 291 // This prevents breaks like: 292 // ... 293 // SomeParameter, OtherParameter).DoSomething( 294 // ... 295 // As they hide "DoSomething" and are generally bad for readability. 296 if (Previous.opensScope() && Previous.isNot(tok::l_brace) && 297 State.LowestLevelOnLine < State.StartOfLineLevel && 298 State.LowestLevelOnLine < Current.NestingLevel) 299 return false; 300 if (Current.isMemberAccess() && State.Stack.back().ContainsUnwrappedBuilder) 301 return false; 302 303 // Don't create a 'hanging' indent if there are multiple blocks in a single 304 // statement. 305 if (Previous.is(tok::l_brace) && State.Stack.size() > 1 && 306 State.Stack[State.Stack.size() - 2].NestedBlockInlined && 307 State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks) 308 return false; 309 310 // Don't break after very short return types (e.g. "void") as that is often 311 // unexpected. 312 if (Current.is(TT_FunctionDeclarationName) && State.Column < 6) { 313 if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) 314 return false; 315 } 316 317 // If binary operators are moved to the next line (including commas for some 318 // styles of constructor initializers), that's always ok. 319 if (!Current.isOneOf(TT_BinaryOperator, tok::comma) && 320 State.Stack.back().NoLineBreakInOperand) 321 return false; 322 323 if (Previous.is(tok::l_square) && Previous.is(TT_ObjCMethodExpr)) 324 return false; 325 326 return !State.Stack.back().NoLineBreak; 327 } 328 329 bool ContinuationIndenter::mustBreak(const LineState &State) { 330 const FormatToken &Current = *State.NextToken; 331 const FormatToken &Previous = *Current.Previous; 332 if (Style.BraceWrapping.BeforeLambdaBody && Current.CanBreakBefore && 333 Current.is(TT_LambdaLBrace) && Previous.isNot(TT_LineComment)) { 334 auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack); 335 return (LambdaBodyLength > getColumnLimit(State)); 336 } 337 if (Current.MustBreakBefore || Current.is(TT_InlineASMColon)) 338 return true; 339 if (State.Stack.back().BreakBeforeClosingBrace && 340 Current.closesBlockOrBlockTypeList(Style)) 341 return true; 342 if (Previous.is(tok::semi) && State.LineContainsContinuedForLoopSection) 343 return true; 344 if (Style.Language == FormatStyle::LK_ObjC && 345 Style.ObjCBreakBeforeNestedBlockParam && 346 Current.ObjCSelectorNameParts > 1 && 347 Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)) { 348 return true; 349 } 350 // Avoid producing inconsistent states by requiring breaks where they are not 351 // permitted for C# generic type constraints. 352 if (State.Stack.back().IsCSharpGenericTypeConstraint && 353 Previous.isNot(TT_CSharpGenericTypeConstraintComma)) 354 return false; 355 if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) || 356 (Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) && 357 Style.isCpp() && 358 // FIXME: This is a temporary workaround for the case where clang-format 359 // sets BreakBeforeParameter to avoid bin packing and this creates a 360 // completely unnecessary line break after a template type that isn't 361 // line-wrapped. 362 (Previous.NestingLevel == 1 || Style.BinPackParameters)) || 363 (Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) && 364 Previous.isNot(tok::question)) || 365 (!Style.BreakBeforeTernaryOperators && 366 Previous.is(TT_ConditionalExpr))) && 367 State.Stack.back().BreakBeforeParameter && !Current.isTrailingComment() && 368 !Current.isOneOf(tok::r_paren, tok::r_brace)) 369 return true; 370 if (State.Stack.back().IsChainedConditional && 371 ((Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) && 372 Current.is(tok::colon)) || 373 (!Style.BreakBeforeTernaryOperators && Previous.is(TT_ConditionalExpr) && 374 Previous.is(tok::colon)))) 375 return true; 376 if (((Previous.is(TT_DictLiteral) && Previous.is(tok::l_brace)) || 377 (Previous.is(TT_ArrayInitializerLSquare) && 378 Previous.ParameterCount > 1) || 379 opensProtoMessageField(Previous, Style)) && 380 Style.ColumnLimit > 0 && 381 getLengthToMatchingParen(Previous, State.Stack) + State.Column - 1 > 382 getColumnLimit(State)) 383 return true; 384 385 const FormatToken &BreakConstructorInitializersToken = 386 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon 387 ? Previous 388 : Current; 389 if (BreakConstructorInitializersToken.is(TT_CtorInitializerColon) && 390 (State.Column + State.Line->Last->TotalLength - Previous.TotalLength > 391 getColumnLimit(State) || 392 State.Stack.back().BreakBeforeParameter) && 393 (Style.AllowShortFunctionsOnASingleLine != FormatStyle::SFS_All || 394 Style.BreakConstructorInitializers != FormatStyle::BCIS_BeforeColon || 395 Style.ColumnLimit != 0)) 396 return true; 397 398 if (Current.is(TT_ObjCMethodExpr) && !Previous.is(TT_SelectorName) && 399 State.Line->startsWith(TT_ObjCMethodSpecifier)) 400 return true; 401 if (Current.is(TT_SelectorName) && !Previous.is(tok::at) && 402 State.Stack.back().ObjCSelectorNameFound && 403 State.Stack.back().BreakBeforeParameter) 404 return true; 405 406 unsigned NewLineColumn = getNewLineColumn(State); 407 if (Current.isMemberAccess() && Style.ColumnLimit != 0 && 408 State.Column + getLengthToNextOperator(Current) > Style.ColumnLimit && 409 (State.Column > NewLineColumn || 410 Current.NestingLevel < State.StartOfLineLevel)) 411 return true; 412 413 if (startsSegmentOfBuilderTypeCall(Current) && 414 (State.Stack.back().CallContinuation != 0 || 415 State.Stack.back().BreakBeforeParameter) && 416 // JavaScript is treated different here as there is a frequent pattern: 417 // SomeFunction(function() { 418 // ... 419 // }.bind(...)); 420 // FIXME: We should find a more generic solution to this problem. 421 !(State.Column <= NewLineColumn && 422 Style.Language == FormatStyle::LK_JavaScript) && 423 !(Previous.closesScopeAfterBlock() && State.Column <= NewLineColumn)) 424 return true; 425 426 // If the template declaration spans multiple lines, force wrap before the 427 // function/class declaration 428 if (Previous.ClosesTemplateDeclaration && 429 State.Stack.back().BreakBeforeParameter && Current.CanBreakBefore) 430 return true; 431 432 if (!State.Line->First->is(tok::kw_enum) && State.Column <= NewLineColumn) 433 return false; 434 435 if (Style.AlwaysBreakBeforeMultilineStrings && 436 (NewLineColumn == State.FirstIndent + Style.ContinuationIndentWidth || 437 Previous.is(tok::comma) || Current.NestingLevel < 2) && 438 !Previous.isOneOf(tok::kw_return, tok::lessless, tok::at, 439 Keywords.kw_dollar) && 440 !Previous.isOneOf(TT_InlineASMColon, TT_ConditionalExpr) && 441 nextIsMultilineString(State)) 442 return true; 443 444 // Using CanBreakBefore here and below takes care of the decision whether the 445 // current style uses wrapping before or after operators for the given 446 // operator. 447 if (Previous.is(TT_BinaryOperator) && Current.CanBreakBefore) { 448 // If we need to break somewhere inside the LHS of a binary expression, we 449 // should also break after the operator. Otherwise, the formatting would 450 // hide the operator precedence, e.g. in: 451 // if (aaaaaaaaaaaaaa == 452 // bbbbbbbbbbbbbb && c) {.. 453 // For comparisons, we only apply this rule, if the LHS is a binary 454 // expression itself as otherwise, the line breaks seem superfluous. 455 // We need special cases for ">>" which we have split into two ">" while 456 // lexing in order to make template parsing easier. 457 bool IsComparison = (Previous.getPrecedence() == prec::Relational || 458 Previous.getPrecedence() == prec::Equality || 459 Previous.getPrecedence() == prec::Spaceship) && 460 Previous.Previous && 461 Previous.Previous->isNot(TT_BinaryOperator); // For >>. 462 bool LHSIsBinaryExpr = 463 Previous.Previous && Previous.Previous->EndsBinaryExpression; 464 if ((!IsComparison || LHSIsBinaryExpr) && !Current.isTrailingComment() && 465 Previous.getPrecedence() != prec::Assignment && 466 State.Stack.back().BreakBeforeParameter) 467 return true; 468 } else if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore && 469 State.Stack.back().BreakBeforeParameter) { 470 return true; 471 } 472 473 // Same as above, but for the first "<<" operator. 474 if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator) && 475 State.Stack.back().BreakBeforeParameter && 476 State.Stack.back().FirstLessLess == 0) 477 return true; 478 479 if (Current.NestingLevel == 0 && !Current.isTrailingComment()) { 480 // Always break after "template <...>" and leading annotations. This is only 481 // for cases where the entire line does not fit on a single line as a 482 // different LineFormatter would be used otherwise. 483 if (Previous.ClosesTemplateDeclaration) 484 return Style.AlwaysBreakTemplateDeclarations != FormatStyle::BTDS_No; 485 if (Previous.is(TT_FunctionAnnotationRParen)) 486 return true; 487 if (Previous.is(TT_LeadingJavaAnnotation) && Current.isNot(tok::l_paren) && 488 Current.isNot(TT_LeadingJavaAnnotation)) 489 return true; 490 } 491 492 // If the return type spans multiple lines, wrap before the function name. 493 if (((Current.is(TT_FunctionDeclarationName) && 494 // Don't break before a C# function when no break after return type 495 (!Style.isCSharp() || 496 Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None)) || 497 (Current.is(tok::kw_operator) && !Previous.is(tok::coloncolon))) && 498 !Previous.is(tok::kw_template) && State.Stack.back().BreakBeforeParameter) 499 return true; 500 501 // The following could be precomputed as they do not depend on the state. 502 // However, as they should take effect only if the UnwrappedLine does not fit 503 // into the ColumnLimit, they are checked here in the ContinuationIndenter. 504 if (Style.ColumnLimit != 0 && Previous.BlockKind == BK_Block && 505 Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment)) 506 return true; 507 508 if (Current.is(tok::lessless) && 509 ((Previous.is(tok::identifier) && Previous.TokenText == "endl") || 510 (Previous.Tok.isLiteral() && (Previous.TokenText.endswith("\\n\"") || 511 Previous.TokenText == "\'\\n\'")))) 512 return true; 513 514 if (Previous.is(TT_BlockComment) && Previous.IsMultiline) 515 return true; 516 517 if (State.NoContinuation) 518 return true; 519 520 return false; 521 } 522 523 unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline, 524 bool DryRun, 525 unsigned ExtraSpaces) { 526 const FormatToken &Current = *State.NextToken; 527 528 assert(!State.Stack.empty()); 529 State.NoContinuation = false; 530 531 if ((Current.is(TT_ImplicitStringLiteral) && 532 (Current.Previous->Tok.getIdentifierInfo() == nullptr || 533 Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() == 534 tok::pp_not_keyword))) { 535 unsigned EndColumn = 536 SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getEnd()); 537 if (Current.LastNewlineOffset != 0) { 538 // If there is a newline within this token, the final column will solely 539 // determined by the current end column. 540 State.Column = EndColumn; 541 } else { 542 unsigned StartColumn = 543 SourceMgr.getSpellingColumnNumber(Current.WhitespaceRange.getBegin()); 544 assert(EndColumn >= StartColumn); 545 State.Column += EndColumn - StartColumn; 546 } 547 moveStateToNextToken(State, DryRun, /*Newline=*/false); 548 return 0; 549 } 550 551 unsigned Penalty = 0; 552 if (Newline) 553 Penalty = addTokenOnNewLine(State, DryRun); 554 else 555 addTokenOnCurrentLine(State, DryRun, ExtraSpaces); 556 557 return moveStateToNextToken(State, DryRun, Newline) + Penalty; 558 } 559 560 void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, 561 unsigned ExtraSpaces) { 562 FormatToken &Current = *State.NextToken; 563 const FormatToken &Previous = *State.NextToken->Previous; 564 if (Current.is(tok::equal) && 565 (State.Line->First->is(tok::kw_for) || Current.NestingLevel == 0) && 566 State.Stack.back().VariablePos == 0) { 567 State.Stack.back().VariablePos = State.Column; 568 // Move over * and & if they are bound to the variable name. 569 const FormatToken *Tok = &Previous; 570 while (Tok && State.Stack.back().VariablePos >= Tok->ColumnWidth) { 571 State.Stack.back().VariablePos -= Tok->ColumnWidth; 572 if (Tok->SpacesRequiredBefore != 0) 573 break; 574 Tok = Tok->Previous; 575 } 576 if (Previous.PartOfMultiVariableDeclStmt) 577 State.Stack.back().LastSpace = State.Stack.back().VariablePos; 578 } 579 580 unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces; 581 582 // Indent preprocessor directives after the hash if required. 583 int PPColumnCorrection = 0; 584 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && 585 Previous.is(tok::hash) && State.FirstIndent > 0 && 586 (State.Line->Type == LT_PreprocessorDirective || 587 State.Line->Type == LT_ImportStatement)) { 588 Spaces += State.FirstIndent; 589 590 // For preprocessor indent with tabs, State.Column will be 1 because of the 591 // hash. This causes second-level indents onward to have an extra space 592 // after the tabs. We avoid this misalignment by subtracting 1 from the 593 // column value passed to replaceWhitespace(). 594 if (Style.UseTab != FormatStyle::UT_Never) 595 PPColumnCorrection = -1; 596 } 597 598 if (!DryRun) 599 Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, Spaces, 600 State.Column + Spaces + PPColumnCorrection); 601 602 // If "BreakBeforeInheritanceComma" mode, don't break within the inheritance 603 // declaration unless there is multiple inheritance. 604 if (Style.BreakInheritanceList == FormatStyle::BILS_BeforeComma && 605 Current.is(TT_InheritanceColon)) 606 State.Stack.back().NoLineBreak = true; 607 if (Style.BreakInheritanceList == FormatStyle::BILS_AfterColon && 608 Previous.is(TT_InheritanceColon)) 609 State.Stack.back().NoLineBreak = true; 610 611 if (Current.is(TT_SelectorName) && 612 !State.Stack.back().ObjCSelectorNameFound) { 613 unsigned MinIndent = 614 std::max(State.FirstIndent + Style.ContinuationIndentWidth, 615 State.Stack.back().Indent); 616 unsigned FirstColonPos = State.Column + Spaces + Current.ColumnWidth; 617 if (Current.LongestObjCSelectorName == 0) 618 State.Stack.back().AlignColons = false; 619 else if (MinIndent + Current.LongestObjCSelectorName > FirstColonPos) 620 State.Stack.back().ColonPos = MinIndent + Current.LongestObjCSelectorName; 621 else 622 State.Stack.back().ColonPos = FirstColonPos; 623 } 624 625 // In "AlwaysBreak" mode, enforce wrapping directly after the parenthesis by 626 // disallowing any further line breaks if there is no line break after the 627 // opening parenthesis. Don't break if it doesn't conserve columns. 628 if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak && 629 (Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) || 630 (Previous.is(tok::l_brace) && Previous.BlockKind != BK_Block && 631 Style.Cpp11BracedListStyle)) && 632 State.Column > getNewLineColumn(State) && 633 (!Previous.Previous || !Previous.Previous->isOneOf( 634 tok::kw_for, tok::kw_while, tok::kw_switch)) && 635 // Don't do this for simple (no expressions) one-argument function calls 636 // as that feels like needlessly wasting whitespace, e.g.: 637 // 638 // caaaaaaaaaaaall( 639 // caaaaaaaaaaaall( 640 // caaaaaaaaaaaall( 641 // caaaaaaaaaaaaaaaaaaaaaaall(aaaaaaaaaaaaaa, aaaaaaaaa)))); 642 Current.FakeLParens.size() > 0 && 643 Current.FakeLParens.back() > prec::Unknown) 644 State.Stack.back().NoLineBreak = true; 645 if (Previous.is(TT_TemplateString) && Previous.opensScope()) 646 State.Stack.back().NoLineBreak = true; 647 648 if (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign && 649 !State.Stack.back().IsCSharpGenericTypeConstraint && 650 Previous.opensScope() && Previous.isNot(TT_ObjCMethodExpr) && 651 (Current.isNot(TT_LineComment) || Previous.BlockKind == BK_BracedInit)) { 652 State.Stack.back().Indent = State.Column + Spaces; 653 State.Stack.back().IsAligned = true; 654 } 655 if (State.Stack.back().AvoidBinPacking && startsNextParameter(Current, Style)) 656 State.Stack.back().NoLineBreak = true; 657 if (startsSegmentOfBuilderTypeCall(Current) && 658 State.Column > getNewLineColumn(State)) 659 State.Stack.back().ContainsUnwrappedBuilder = true; 660 661 if (Current.is(TT_LambdaArrow) && Style.Language == FormatStyle::LK_Java) 662 State.Stack.back().NoLineBreak = true; 663 if (Current.isMemberAccess() && Previous.is(tok::r_paren) && 664 (Previous.MatchingParen && 665 (Previous.TotalLength - Previous.MatchingParen->TotalLength > 10))) 666 // If there is a function call with long parameters, break before trailing 667 // calls. This prevents things like: 668 // EXPECT_CALL(SomeLongParameter).Times( 669 // 2); 670 // We don't want to do this for short parameters as they can just be 671 // indexes. 672 State.Stack.back().NoLineBreak = true; 673 674 // Don't allow the RHS of an operator to be split over multiple lines unless 675 // there is a line-break right after the operator. 676 // Exclude relational operators, as there, it is always more desirable to 677 // have the LHS 'left' of the RHS. 678 const FormatToken *P = Current.getPreviousNonComment(); 679 if (!Current.is(tok::comment) && P && 680 (P->isOneOf(TT_BinaryOperator, tok::comma) || 681 (P->is(TT_ConditionalExpr) && P->is(tok::colon))) && 682 !P->isOneOf(TT_OverloadedOperator, TT_CtorInitializerComma) && 683 P->getPrecedence() != prec::Assignment && 684 P->getPrecedence() != prec::Relational && 685 P->getPrecedence() != prec::Spaceship) { 686 bool BreakBeforeOperator = 687 P->MustBreakBefore || P->is(tok::lessless) || 688 (P->is(TT_BinaryOperator) && 689 Style.BreakBeforeBinaryOperators != FormatStyle::BOS_None) || 690 (P->is(TT_ConditionalExpr) && Style.BreakBeforeTernaryOperators); 691 // Don't do this if there are only two operands. In these cases, there is 692 // always a nice vertical separation between them and the extra line break 693 // does not help. 694 bool HasTwoOperands = 695 P->OperatorIndex == 0 && !P->NextOperator && !P->is(TT_ConditionalExpr); 696 if ((!BreakBeforeOperator && 697 !(HasTwoOperands && 698 Style.AlignOperands != FormatStyle::OAS_DontAlign)) || 699 (!State.Stack.back().LastOperatorWrapped && BreakBeforeOperator)) 700 State.Stack.back().NoLineBreakInOperand = true; 701 } 702 703 State.Column += Spaces; 704 if (Current.isNot(tok::comment) && Previous.is(tok::l_paren) && 705 Previous.Previous && 706 (Previous.Previous->is(tok::kw_for) || Previous.Previous->isIf())) { 707 // Treat the condition inside an if as if it was a second function 708 // parameter, i.e. let nested calls have a continuation indent. 709 State.Stack.back().LastSpace = State.Column; 710 State.Stack.back().NestedBlockIndent = State.Column; 711 } else if (!Current.isOneOf(tok::comment, tok::caret) && 712 ((Previous.is(tok::comma) && 713 !Previous.is(TT_OverloadedOperator)) || 714 (Previous.is(tok::colon) && Previous.is(TT_ObjCMethodExpr)))) { 715 State.Stack.back().LastSpace = State.Column; 716 } else if (Previous.is(TT_CtorInitializerColon) && 717 Style.BreakConstructorInitializers == 718 FormatStyle::BCIS_AfterColon) { 719 State.Stack.back().Indent = State.Column; 720 State.Stack.back().LastSpace = State.Column; 721 } else if ((Previous.isOneOf(TT_BinaryOperator, TT_ConditionalExpr, 722 TT_CtorInitializerColon)) && 723 ((Previous.getPrecedence() != prec::Assignment && 724 (Previous.isNot(tok::lessless) || Previous.OperatorIndex != 0 || 725 Previous.NextOperator)) || 726 Current.StartsBinaryExpression)) { 727 // Indent relative to the RHS of the expression unless this is a simple 728 // assignment without binary expression on the RHS. Also indent relative to 729 // unary operators and the colons of constructor initializers. 730 if (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None) 731 State.Stack.back().LastSpace = State.Column; 732 } else if (Previous.is(TT_InheritanceColon)) { 733 State.Stack.back().Indent = State.Column; 734 State.Stack.back().LastSpace = State.Column; 735 } else if (Current.is(TT_CSharpGenericTypeConstraintColon)) { 736 State.Stack.back().ColonPos = State.Column; 737 } else if (Previous.opensScope()) { 738 // If a function has a trailing call, indent all parameters from the 739 // opening parenthesis. This avoids confusing indents like: 740 // OuterFunction(InnerFunctionCall( // break 741 // ParameterToInnerFunction)) // break 742 // .SecondInnerFunctionCall(); 743 bool HasTrailingCall = false; 744 if (Previous.MatchingParen) { 745 const FormatToken *Next = Previous.MatchingParen->getNextNonComment(); 746 HasTrailingCall = Next && Next->isMemberAccess(); 747 } 748 if (HasTrailingCall && State.Stack.size() > 1 && 749 State.Stack[State.Stack.size() - 2].CallContinuation == 0) 750 State.Stack.back().LastSpace = State.Column; 751 } 752 } 753 754 unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, 755 bool DryRun) { 756 FormatToken &Current = *State.NextToken; 757 const FormatToken &Previous = *State.NextToken->Previous; 758 759 // Extra penalty that needs to be added because of the way certain line 760 // breaks are chosen. 761 unsigned Penalty = 0; 762 763 const FormatToken *PreviousNonComment = Current.getPreviousNonComment(); 764 const FormatToken *NextNonComment = Previous.getNextNonComment(); 765 if (!NextNonComment) 766 NextNonComment = &Current; 767 // The first line break on any NestingLevel causes an extra penalty in order 768 // prefer similar line breaks. 769 if (!State.Stack.back().ContainsLineBreak) 770 Penalty += 15; 771 State.Stack.back().ContainsLineBreak = true; 772 773 Penalty += State.NextToken->SplitPenalty; 774 775 // Breaking before the first "<<" is generally not desirable if the LHS is 776 // short. Also always add the penalty if the LHS is split over multiple lines 777 // to avoid unnecessary line breaks that just work around this penalty. 778 if (NextNonComment->is(tok::lessless) && 779 State.Stack.back().FirstLessLess == 0 && 780 (State.Column <= Style.ColumnLimit / 3 || 781 State.Stack.back().BreakBeforeParameter)) 782 Penalty += Style.PenaltyBreakFirstLessLess; 783 784 State.Column = getNewLineColumn(State); 785 786 // Indent nested blocks relative to this column, unless in a very specific 787 // JavaScript special case where: 788 // 789 // var loooooong_name = 790 // function() { 791 // // code 792 // } 793 // 794 // is common and should be formatted like a free-standing function. The same 795 // goes for wrapping before the lambda return type arrow. 796 if (!Current.is(TT_LambdaArrow) && 797 (Style.Language != FormatStyle::LK_JavaScript || 798 Current.NestingLevel != 0 || !PreviousNonComment || 799 !PreviousNonComment->is(tok::equal) || 800 !Current.isOneOf(Keywords.kw_async, Keywords.kw_function))) 801 State.Stack.back().NestedBlockIndent = State.Column; 802 803 if (NextNonComment->isMemberAccess()) { 804 if (State.Stack.back().CallContinuation == 0) 805 State.Stack.back().CallContinuation = State.Column; 806 } else if (NextNonComment->is(TT_SelectorName)) { 807 if (!State.Stack.back().ObjCSelectorNameFound) { 808 if (NextNonComment->LongestObjCSelectorName == 0) { 809 State.Stack.back().AlignColons = false; 810 } else { 811 State.Stack.back().ColonPos = 812 (shouldIndentWrappedSelectorName(Style, State.Line->Type) 813 ? std::max(State.Stack.back().Indent, 814 State.FirstIndent + Style.ContinuationIndentWidth) 815 : State.Stack.back().Indent) + 816 std::max(NextNonComment->LongestObjCSelectorName, 817 NextNonComment->ColumnWidth); 818 } 819 } else if (State.Stack.back().AlignColons && 820 State.Stack.back().ColonPos <= NextNonComment->ColumnWidth) { 821 State.Stack.back().ColonPos = State.Column + NextNonComment->ColumnWidth; 822 } 823 } else if (PreviousNonComment && PreviousNonComment->is(tok::colon) && 824 PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) { 825 // FIXME: This is hacky, find a better way. The problem is that in an ObjC 826 // method expression, the block should be aligned to the line starting it, 827 // e.g.: 828 // [aaaaaaaaaaaaaaa aaaaaaaaa: \\ break for some reason 829 // ^(int *i) { 830 // // ... 831 // }]; 832 // Thus, we set LastSpace of the next higher NestingLevel, to which we move 833 // when we consume all of the "}"'s FakeRParens at the "{". 834 if (State.Stack.size() > 1) 835 State.Stack[State.Stack.size() - 2].LastSpace = 836 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 837 Style.ContinuationIndentWidth; 838 } 839 840 if ((PreviousNonComment && 841 PreviousNonComment->isOneOf(tok::comma, tok::semi) && 842 !State.Stack.back().AvoidBinPacking) || 843 Previous.is(TT_BinaryOperator)) 844 State.Stack.back().BreakBeforeParameter = false; 845 if (PreviousNonComment && 846 PreviousNonComment->isOneOf(TT_TemplateCloser, TT_JavaAnnotation) && 847 Current.NestingLevel == 0) 848 State.Stack.back().BreakBeforeParameter = false; 849 if (NextNonComment->is(tok::question) || 850 (PreviousNonComment && PreviousNonComment->is(tok::question))) 851 State.Stack.back().BreakBeforeParameter = true; 852 if (Current.is(TT_BinaryOperator) && Current.CanBreakBefore) 853 State.Stack.back().BreakBeforeParameter = false; 854 855 if (!DryRun) { 856 unsigned MaxEmptyLinesToKeep = Style.MaxEmptyLinesToKeep + 1; 857 if (Current.is(tok::r_brace) && Current.MatchingParen && 858 // Only strip trailing empty lines for l_braces that have children, i.e. 859 // for function expressions (lambdas, arrows, etc). 860 !Current.MatchingParen->Children.empty()) { 861 // lambdas and arrow functions are expressions, thus their r_brace is not 862 // on its own line, and thus not covered by UnwrappedLineFormatter's logic 863 // about removing empty lines on closing blocks. Special case them here. 864 MaxEmptyLinesToKeep = 1; 865 } 866 unsigned Newlines = 867 std::max(1u, std::min(Current.NewlinesBefore, MaxEmptyLinesToKeep)); 868 bool ContinuePPDirective = 869 State.Line->InPPDirective && State.Line->Type != LT_ImportStatement; 870 Whitespaces.replaceWhitespace(Current, Newlines, State.Column, State.Column, 871 State.Stack.back().IsAligned, 872 ContinuePPDirective); 873 } 874 875 if (!Current.isTrailingComment()) 876 State.Stack.back().LastSpace = State.Column; 877 if (Current.is(tok::lessless)) 878 // If we are breaking before a "<<", we always want to indent relative to 879 // RHS. This is necessary only for "<<", as we special-case it and don't 880 // always indent relative to the RHS. 881 State.Stack.back().LastSpace += 3; // 3 -> width of "<< ". 882 883 State.StartOfLineLevel = Current.NestingLevel; 884 State.LowestLevelOnLine = Current.NestingLevel; 885 886 // Any break on this level means that the parent level has been broken 887 // and we need to avoid bin packing there. 888 bool NestedBlockSpecialCase = 889 (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 && 890 State.Stack[State.Stack.size() - 2].NestedBlockInlined) || 891 (Style.Language == FormatStyle::LK_ObjC && Current.is(tok::r_brace) && 892 State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam); 893 if (!NestedBlockSpecialCase) 894 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) 895 State.Stack[i].BreakBeforeParameter = true; 896 897 if (PreviousNonComment && 898 !PreviousNonComment->isOneOf(tok::comma, tok::colon, tok::semi) && 899 (PreviousNonComment->isNot(TT_TemplateCloser) || 900 Current.NestingLevel != 0) && 901 !PreviousNonComment->isOneOf( 902 TT_BinaryOperator, TT_FunctionAnnotationRParen, TT_JavaAnnotation, 903 TT_LeadingJavaAnnotation) && 904 Current.isNot(TT_BinaryOperator) && !PreviousNonComment->opensScope()) 905 State.Stack.back().BreakBeforeParameter = true; 906 907 // If we break after { or the [ of an array initializer, we should also break 908 // before the corresponding } or ]. 909 if (PreviousNonComment && 910 (PreviousNonComment->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || 911 opensProtoMessageField(*PreviousNonComment, Style))) 912 State.Stack.back().BreakBeforeClosingBrace = true; 913 914 if (State.Stack.back().AvoidBinPacking) { 915 // If we are breaking after '(', '{', '<', or this is the break after a ':' 916 // to start a member initializater list in a constructor, this should not 917 // be considered bin packing unless the relevant AllowAll option is false or 918 // this is a dict/object literal. 919 bool PreviousIsBreakingCtorInitializerColon = 920 Previous.is(TT_CtorInitializerColon) && 921 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon; 922 if (!(Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) || 923 PreviousIsBreakingCtorInitializerColon) || 924 (!Style.AllowAllParametersOfDeclarationOnNextLine && 925 State.Line->MustBeDeclaration) || 926 (!Style.AllowAllArgumentsOnNextLine && 927 !State.Line->MustBeDeclaration) || 928 (!Style.AllowAllConstructorInitializersOnNextLine && 929 PreviousIsBreakingCtorInitializerColon) || 930 Previous.is(TT_DictLiteral)) 931 State.Stack.back().BreakBeforeParameter = true; 932 933 // If we are breaking after a ':' to start a member initializer list, 934 // and we allow all arguments on the next line, we should not break 935 // before the next parameter. 936 if (PreviousIsBreakingCtorInitializerColon && 937 Style.AllowAllConstructorInitializersOnNextLine) 938 State.Stack.back().BreakBeforeParameter = false; 939 } 940 941 return Penalty; 942 } 943 944 unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { 945 if (!State.NextToken || !State.NextToken->Previous) 946 return 0; 947 948 FormatToken &Current = *State.NextToken; 949 950 if (State.Stack.back().IsCSharpGenericTypeConstraint && 951 Current.isNot(TT_CSharpGenericTypeConstraint)) 952 return State.Stack.back().ColonPos + 2; 953 954 const FormatToken &Previous = *Current.Previous; 955 // If we are continuing an expression, we want to use the continuation indent. 956 unsigned ContinuationIndent = 957 std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 958 Style.ContinuationIndentWidth; 959 const FormatToken *PreviousNonComment = Current.getPreviousNonComment(); 960 const FormatToken *NextNonComment = Previous.getNextNonComment(); 961 if (!NextNonComment) 962 NextNonComment = &Current; 963 964 // Java specific bits. 965 if (Style.Language == FormatStyle::LK_Java && 966 Current.isOneOf(Keywords.kw_implements, Keywords.kw_extends)) 967 return std::max(State.Stack.back().LastSpace, 968 State.Stack.back().Indent + Style.ContinuationIndentWidth); 969 970 if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths && 971 State.Line->First->is(tok::kw_enum)) 972 return (Style.IndentWidth * State.Line->First->IndentLevel) + 973 Style.IndentWidth; 974 975 if (NextNonComment->is(tok::l_brace) && NextNonComment->BlockKind == BK_Block) 976 return Current.NestingLevel == 0 ? State.FirstIndent 977 : State.Stack.back().Indent; 978 if ((Current.isOneOf(tok::r_brace, tok::r_square) || 979 (Current.is(tok::greater) && 980 (Style.Language == FormatStyle::LK_Proto || 981 Style.Language == FormatStyle::LK_TextProto))) && 982 State.Stack.size() > 1) { 983 if (Current.closesBlockOrBlockTypeList(Style)) 984 return State.Stack[State.Stack.size() - 2].NestedBlockIndent; 985 if (Current.MatchingParen && 986 Current.MatchingParen->BlockKind == BK_BracedInit) 987 return State.Stack[State.Stack.size() - 2].LastSpace; 988 return State.FirstIndent; 989 } 990 // Indent a closing parenthesis at the previous level if followed by a semi, 991 // const, or opening brace. This allows indentations such as: 992 // foo( 993 // a, 994 // ); 995 // int Foo::getter( 996 // // 997 // ) const { 998 // return foo; 999 // } 1000 // function foo( 1001 // a, 1002 // ) { 1003 // code(); // 1004 // } 1005 if (Current.is(tok::r_paren) && State.Stack.size() > 1 && 1006 (!Current.Next || 1007 Current.Next->isOneOf(tok::semi, tok::kw_const, tok::l_brace))) 1008 return State.Stack[State.Stack.size() - 2].LastSpace; 1009 if (NextNonComment->is(TT_TemplateString) && NextNonComment->closesScope()) 1010 return State.Stack[State.Stack.size() - 2].LastSpace; 1011 if (Current.is(tok::identifier) && Current.Next && 1012 (Current.Next->is(TT_DictLiteral) || 1013 ((Style.Language == FormatStyle::LK_Proto || 1014 Style.Language == FormatStyle::LK_TextProto) && 1015 Current.Next->isOneOf(tok::less, tok::l_brace)))) 1016 return State.Stack.back().Indent; 1017 if (NextNonComment->is(TT_ObjCStringLiteral) && 1018 State.StartOfStringLiteral != 0) 1019 return State.StartOfStringLiteral - 1; 1020 if (NextNonComment->isStringLiteral() && State.StartOfStringLiteral != 0) 1021 return State.StartOfStringLiteral; 1022 if (NextNonComment->is(tok::lessless) && 1023 State.Stack.back().FirstLessLess != 0) 1024 return State.Stack.back().FirstLessLess; 1025 if (NextNonComment->isMemberAccess()) { 1026 if (State.Stack.back().CallContinuation == 0) 1027 return ContinuationIndent; 1028 return State.Stack.back().CallContinuation; 1029 } 1030 if (State.Stack.back().QuestionColumn != 0 && 1031 ((NextNonComment->is(tok::colon) && 1032 NextNonComment->is(TT_ConditionalExpr)) || 1033 Previous.is(TT_ConditionalExpr))) { 1034 if (((NextNonComment->is(tok::colon) && NextNonComment->Next && 1035 !NextNonComment->Next->FakeLParens.empty() && 1036 NextNonComment->Next->FakeLParens.back() == prec::Conditional) || 1037 (Previous.is(tok::colon) && !Current.FakeLParens.empty() && 1038 Current.FakeLParens.back() == prec::Conditional)) && 1039 !State.Stack.back().IsWrappedConditional) { 1040 // NOTE: we may tweak this slightly: 1041 // * not remove the 'lead' ContinuationIndentWidth 1042 // * always un-indent by the operator when 1043 // BreakBeforeTernaryOperators=true 1044 unsigned Indent = State.Stack.back().Indent; 1045 if (Style.AlignOperands != FormatStyle::OAS_DontAlign) { 1046 Indent -= Style.ContinuationIndentWidth; 1047 } 1048 if (Style.BreakBeforeTernaryOperators && 1049 State.Stack.back().UnindentOperator) 1050 Indent -= 2; 1051 return Indent; 1052 } 1053 return State.Stack.back().QuestionColumn; 1054 } 1055 if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) 1056 return State.Stack.back().VariablePos; 1057 if ((PreviousNonComment && 1058 (PreviousNonComment->ClosesTemplateDeclaration || 1059 PreviousNonComment->isOneOf( 1060 TT_AttributeParen, TT_AttributeSquare, TT_FunctionAnnotationRParen, 1061 TT_JavaAnnotation, TT_LeadingJavaAnnotation))) || 1062 (!Style.IndentWrappedFunctionNames && 1063 NextNonComment->isOneOf(tok::kw_operator, TT_FunctionDeclarationName))) 1064 return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent); 1065 if (NextNonComment->is(TT_SelectorName)) { 1066 if (!State.Stack.back().ObjCSelectorNameFound) { 1067 unsigned MinIndent = State.Stack.back().Indent; 1068 if (shouldIndentWrappedSelectorName(Style, State.Line->Type)) 1069 MinIndent = std::max(MinIndent, 1070 State.FirstIndent + Style.ContinuationIndentWidth); 1071 // If LongestObjCSelectorName is 0, we are indenting the first 1072 // part of an ObjC selector (or a selector component which is 1073 // not colon-aligned due to block formatting). 1074 // 1075 // Otherwise, we are indenting a subsequent part of an ObjC 1076 // selector which should be colon-aligned to the longest 1077 // component of the ObjC selector. 1078 // 1079 // In either case, we want to respect Style.IndentWrappedFunctionNames. 1080 return MinIndent + 1081 std::max(NextNonComment->LongestObjCSelectorName, 1082 NextNonComment->ColumnWidth) - 1083 NextNonComment->ColumnWidth; 1084 } 1085 if (!State.Stack.back().AlignColons) 1086 return State.Stack.back().Indent; 1087 if (State.Stack.back().ColonPos > NextNonComment->ColumnWidth) 1088 return State.Stack.back().ColonPos - NextNonComment->ColumnWidth; 1089 return State.Stack.back().Indent; 1090 } 1091 if (NextNonComment->is(tok::colon) && NextNonComment->is(TT_ObjCMethodExpr)) 1092 return State.Stack.back().ColonPos; 1093 if (NextNonComment->is(TT_ArraySubscriptLSquare)) { 1094 if (State.Stack.back().StartOfArraySubscripts != 0) 1095 return State.Stack.back().StartOfArraySubscripts; 1096 else if (Style.isCSharp()) // C# allows `["key"] = value` inside object 1097 // initializers. 1098 return State.Stack.back().Indent; 1099 return ContinuationIndent; 1100 } 1101 1102 // This ensure that we correctly format ObjC methods calls without inputs, 1103 // i.e. where the last element isn't selector like: [callee method]; 1104 if (NextNonComment->is(tok::identifier) && NextNonComment->FakeRParens == 0 && 1105 NextNonComment->Next && NextNonComment->Next->is(TT_ObjCMethodExpr)) 1106 return State.Stack.back().Indent; 1107 1108 if (NextNonComment->isOneOf(TT_StartOfName, TT_PointerOrReference) || 1109 Previous.isOneOf(tok::coloncolon, tok::equal, TT_JsTypeColon)) 1110 return ContinuationIndent; 1111 if (PreviousNonComment && PreviousNonComment->is(tok::colon) && 1112 PreviousNonComment->isOneOf(TT_ObjCMethodExpr, TT_DictLiteral)) 1113 return ContinuationIndent; 1114 if (NextNonComment->is(TT_CtorInitializerComma)) 1115 return State.Stack.back().Indent; 1116 if (PreviousNonComment && PreviousNonComment->is(TT_CtorInitializerColon) && 1117 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) 1118 return State.Stack.back().Indent; 1119 if (PreviousNonComment && PreviousNonComment->is(TT_InheritanceColon) && 1120 Style.BreakInheritanceList == FormatStyle::BILS_AfterColon) 1121 return State.Stack.back().Indent; 1122 if (NextNonComment->isOneOf(TT_CtorInitializerColon, TT_InheritanceColon, 1123 TT_InheritanceComma)) 1124 return State.FirstIndent + Style.ConstructorInitializerIndentWidth; 1125 if (Previous.is(tok::r_paren) && !Current.isBinaryOperator() && 1126 !Current.isOneOf(tok::colon, tok::comment)) 1127 return ContinuationIndent; 1128 if (Current.is(TT_ProtoExtensionLSquare)) 1129 return State.Stack.back().Indent; 1130 if (Current.isBinaryOperator() && State.Stack.back().UnindentOperator) 1131 return State.Stack.back().Indent - Current.Tok.getLength() - 1132 Current.SpacesRequiredBefore; 1133 if (Current.isOneOf(tok::comment, TT_BlockComment, TT_LineComment) && 1134 NextNonComment->isBinaryOperator() && State.Stack.back().UnindentOperator) 1135 return State.Stack.back().Indent - NextNonComment->Tok.getLength() - 1136 NextNonComment->SpacesRequiredBefore; 1137 if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment && 1138 !PreviousNonComment->isOneOf(tok::r_brace, TT_CtorInitializerComma)) 1139 // Ensure that we fall back to the continuation indent width instead of 1140 // just flushing continuations left. 1141 return State.Stack.back().Indent + Style.ContinuationIndentWidth; 1142 return State.Stack.back().Indent; 1143 } 1144 1145 static bool hasNestedBlockInlined(const FormatToken *Previous, 1146 const FormatToken &Current, 1147 const FormatStyle &Style) { 1148 if (Previous->isNot(tok::l_paren)) 1149 return true; 1150 if (Previous->ParameterCount > 1) 1151 return true; 1152 1153 // Also a nested block if contains a lambda inside function with 1 parameter 1154 return (Style.BraceWrapping.BeforeLambdaBody && Current.is(TT_LambdaLSquare)); 1155 } 1156 1157 unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, 1158 bool DryRun, bool Newline) { 1159 assert(State.Stack.size()); 1160 const FormatToken &Current = *State.NextToken; 1161 1162 if (Current.is(TT_CSharpGenericTypeConstraint)) 1163 State.Stack.back().IsCSharpGenericTypeConstraint = true; 1164 if (Current.isOneOf(tok::comma, TT_BinaryOperator)) 1165 State.Stack.back().NoLineBreakInOperand = false; 1166 if (Current.isOneOf(TT_InheritanceColon, TT_CSharpGenericTypeConstraintColon)) 1167 State.Stack.back().AvoidBinPacking = true; 1168 if (Current.is(tok::lessless) && Current.isNot(TT_OverloadedOperator)) { 1169 if (State.Stack.back().FirstLessLess == 0) 1170 State.Stack.back().FirstLessLess = State.Column; 1171 else 1172 State.Stack.back().LastOperatorWrapped = Newline; 1173 } 1174 if (Current.is(TT_BinaryOperator) && Current.isNot(tok::lessless)) 1175 State.Stack.back().LastOperatorWrapped = Newline; 1176 if (Current.is(TT_ConditionalExpr) && Current.Previous && 1177 !Current.Previous->is(TT_ConditionalExpr)) 1178 State.Stack.back().LastOperatorWrapped = Newline; 1179 if (Current.is(TT_ArraySubscriptLSquare) && 1180 State.Stack.back().StartOfArraySubscripts == 0) 1181 State.Stack.back().StartOfArraySubscripts = State.Column; 1182 if (Current.is(TT_ConditionalExpr) && Current.is(tok::question) && 1183 ((Current.MustBreakBefore) || 1184 (Current.getNextNonComment() && 1185 Current.getNextNonComment()->MustBreakBefore))) 1186 State.Stack.back().IsWrappedConditional = true; 1187 if (Style.BreakBeforeTernaryOperators && Current.is(tok::question)) 1188 State.Stack.back().QuestionColumn = State.Column; 1189 if (!Style.BreakBeforeTernaryOperators && Current.isNot(tok::colon)) { 1190 const FormatToken *Previous = Current.Previous; 1191 while (Previous && Previous->isTrailingComment()) 1192 Previous = Previous->Previous; 1193 if (Previous && Previous->is(tok::question)) 1194 State.Stack.back().QuestionColumn = State.Column; 1195 } 1196 if (!Current.opensScope() && !Current.closesScope() && 1197 !Current.is(TT_PointerOrReference)) 1198 State.LowestLevelOnLine = 1199 std::min(State.LowestLevelOnLine, Current.NestingLevel); 1200 if (Current.isMemberAccess()) 1201 State.Stack.back().StartOfFunctionCall = 1202 !Current.NextOperator ? 0 : State.Column; 1203 if (Current.is(TT_SelectorName)) 1204 State.Stack.back().ObjCSelectorNameFound = true; 1205 if (Current.is(TT_CtorInitializerColon) && 1206 Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon) { 1207 // Indent 2 from the column, so: 1208 // SomeClass::SomeClass() 1209 // : First(...), ... 1210 // Next(...) 1211 // ^ line up here. 1212 State.Stack.back().Indent = 1213 State.Column + 1214 (Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma 1215 ? 0 1216 : 2); 1217 State.Stack.back().NestedBlockIndent = State.Stack.back().Indent; 1218 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) { 1219 State.Stack.back().AvoidBinPacking = true; 1220 State.Stack.back().BreakBeforeParameter = 1221 !Style.AllowAllConstructorInitializersOnNextLine; 1222 } else { 1223 State.Stack.back().BreakBeforeParameter = false; 1224 } 1225 } 1226 if (Current.is(TT_CtorInitializerColon) && 1227 Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) { 1228 State.Stack.back().Indent = 1229 State.FirstIndent + Style.ConstructorInitializerIndentWidth; 1230 State.Stack.back().NestedBlockIndent = State.Stack.back().Indent; 1231 if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) 1232 State.Stack.back().AvoidBinPacking = true; 1233 } 1234 if (Current.is(TT_InheritanceColon)) 1235 State.Stack.back().Indent = 1236 State.FirstIndent + Style.ConstructorInitializerIndentWidth; 1237 if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline) 1238 State.Stack.back().NestedBlockIndent = 1239 State.Column + Current.ColumnWidth + 1; 1240 if (Current.isOneOf(TT_LambdaLSquare, TT_LambdaArrow)) 1241 State.Stack.back().LastSpace = State.Column; 1242 1243 // Insert scopes created by fake parenthesis. 1244 const FormatToken *Previous = Current.getPreviousNonComment(); 1245 1246 // Add special behavior to support a format commonly used for JavaScript 1247 // closures: 1248 // SomeFunction(function() { 1249 // foo(); 1250 // bar(); 1251 // }, a, b, c); 1252 if (Current.isNot(tok::comment) && Previous && 1253 Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) && 1254 !Previous->is(TT_DictLiteral) && State.Stack.size() > 1 && 1255 !State.Stack.back().HasMultipleNestedBlocks) { 1256 if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline) 1257 for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i) 1258 State.Stack[i].NoLineBreak = true; 1259 State.Stack[State.Stack.size() - 2].NestedBlockInlined = false; 1260 } 1261 if (Previous && 1262 (Previous->isOneOf(tok::l_paren, tok::comma, tok::colon) || 1263 Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr)) && 1264 !Previous->isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) { 1265 State.Stack.back().NestedBlockInlined = 1266 !Newline && hasNestedBlockInlined(Previous, Current, Style); 1267 } 1268 1269 moveStatePastFakeLParens(State, Newline); 1270 moveStatePastScopeCloser(State); 1271 bool AllowBreak = !State.Stack.back().NoLineBreak && 1272 !State.Stack.back().NoLineBreakInOperand; 1273 moveStatePastScopeOpener(State, Newline); 1274 moveStatePastFakeRParens(State); 1275 1276 if (Current.is(TT_ObjCStringLiteral) && State.StartOfStringLiteral == 0) 1277 State.StartOfStringLiteral = State.Column + 1; 1278 if (Current.is(TT_CSharpStringLiteral) && State.StartOfStringLiteral == 0) 1279 State.StartOfStringLiteral = State.Column + 1; 1280 else if (Current.isStringLiteral() && State.StartOfStringLiteral == 0) 1281 State.StartOfStringLiteral = State.Column; 1282 else if (!Current.isOneOf(tok::comment, tok::identifier, tok::hash) && 1283 !Current.isStringLiteral()) 1284 State.StartOfStringLiteral = 0; 1285 1286 State.Column += Current.ColumnWidth; 1287 State.NextToken = State.NextToken->Next; 1288 1289 unsigned Penalty = 1290 handleEndOfLine(Current, State, DryRun, AllowBreak, Newline); 1291 1292 if (Current.Role) 1293 Current.Role->formatFromToken(State, this, DryRun); 1294 // If the previous has a special role, let it consume tokens as appropriate. 1295 // It is necessary to start at the previous token for the only implemented 1296 // role (comma separated list). That way, the decision whether or not to break 1297 // after the "{" is already done and both options are tried and evaluated. 1298 // FIXME: This is ugly, find a better way. 1299 if (Previous && Previous->Role) 1300 Penalty += Previous->Role->formatAfterToken(State, this, DryRun); 1301 1302 return Penalty; 1303 } 1304 1305 void ContinuationIndenter::moveStatePastFakeLParens(LineState &State, 1306 bool Newline) { 1307 const FormatToken &Current = *State.NextToken; 1308 const FormatToken *Previous = Current.getPreviousNonComment(); 1309 1310 // Don't add extra indentation for the first fake parenthesis after 1311 // 'return', assignments or opening <({[. The indentation for these cases 1312 // is special cased. 1313 bool SkipFirstExtraIndent = 1314 (Previous && (Previous->opensScope() || 1315 Previous->isOneOf(tok::semi, tok::kw_return) || 1316 (Previous->getPrecedence() == prec::Assignment && 1317 Style.AlignOperands != FormatStyle::OAS_DontAlign) || 1318 Previous->is(TT_ObjCMethodExpr))); 1319 for (SmallVectorImpl<prec::Level>::const_reverse_iterator 1320 I = Current.FakeLParens.rbegin(), 1321 E = Current.FakeLParens.rend(); 1322 I != E; ++I) { 1323 ParenState NewParenState = State.Stack.back(); 1324 NewParenState.Tok = nullptr; 1325 NewParenState.ContainsLineBreak = false; 1326 NewParenState.LastOperatorWrapped = true; 1327 NewParenState.IsChainedConditional = false; 1328 NewParenState.IsWrappedConditional = false; 1329 NewParenState.UnindentOperator = false; 1330 NewParenState.NoLineBreak = 1331 NewParenState.NoLineBreak || State.Stack.back().NoLineBreakInOperand; 1332 1333 // Don't propagate AvoidBinPacking into subexpressions of arg/param lists. 1334 if (*I > prec::Comma) 1335 NewParenState.AvoidBinPacking = false; 1336 1337 // Indent from 'LastSpace' unless these are fake parentheses encapsulating 1338 // a builder type call after 'return' or, if the alignment after opening 1339 // brackets is disabled. 1340 if (!Current.isTrailingComment() && 1341 (Style.AlignOperands != FormatStyle::OAS_DontAlign || 1342 *I < prec::Assignment) && 1343 (!Previous || Previous->isNot(tok::kw_return) || 1344 (Style.Language != FormatStyle::LK_Java && *I > 0)) && 1345 (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign || 1346 *I != prec::Comma || Current.NestingLevel == 0)) { 1347 NewParenState.Indent = 1348 std::max(std::max(State.Column, NewParenState.Indent), 1349 State.Stack.back().LastSpace); 1350 } 1351 1352 // If BreakBeforeBinaryOperators is set, un-indent a bit to account for 1353 // the operator and keep the operands aligned 1354 if (Style.AlignOperands == FormatStyle::OAS_AlignAfterOperator && 1355 Previous && 1356 (Previous->getPrecedence() == prec::Assignment || 1357 Previous->is(tok::kw_return) || 1358 (*I == prec::Conditional && Previous->is(tok::question) && 1359 Previous->is(TT_ConditionalExpr))) && 1360 !Newline) 1361 NewParenState.UnindentOperator = true; 1362 1363 // Do not indent relative to the fake parentheses inserted for "." or "->". 1364 // This is a special case to make the following to statements consistent: 1365 // OuterFunction(InnerFunctionCall( // break 1366 // ParameterToInnerFunction)); 1367 // OuterFunction(SomeObject.InnerFunctionCall( // break 1368 // ParameterToInnerFunction)); 1369 if (*I > prec::Unknown) 1370 NewParenState.LastSpace = std::max(NewParenState.LastSpace, State.Column); 1371 if (*I != prec::Conditional && !Current.is(TT_UnaryOperator) && 1372 Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) 1373 NewParenState.StartOfFunctionCall = State.Column; 1374 1375 // Indent conditional expressions, unless they are chained "else-if" 1376 // conditionals. Never indent expression where the 'operator' is ',', ';' or 1377 // an assignment (i.e. *I <= prec::Assignment) as those have different 1378 // indentation rules. Indent other expression, unless the indentation needs 1379 // to be skipped. 1380 if (*I == prec::Conditional && Previous && Previous->is(tok::colon) && 1381 Previous->is(TT_ConditionalExpr) && I == Current.FakeLParens.rbegin() && 1382 !State.Stack.back().IsWrappedConditional) { 1383 NewParenState.IsChainedConditional = true; 1384 NewParenState.UnindentOperator = State.Stack.back().UnindentOperator; 1385 } else if (*I == prec::Conditional || 1386 (!SkipFirstExtraIndent && *I > prec::Assignment && 1387 !Current.isTrailingComment())) { 1388 NewParenState.Indent += Style.ContinuationIndentWidth; 1389 } 1390 if ((Previous && !Previous->opensScope()) || *I != prec::Comma) 1391 NewParenState.BreakBeforeParameter = false; 1392 State.Stack.push_back(NewParenState); 1393 SkipFirstExtraIndent = false; 1394 } 1395 } 1396 1397 void ContinuationIndenter::moveStatePastFakeRParens(LineState &State) { 1398 for (unsigned i = 0, e = State.NextToken->FakeRParens; i != e; ++i) { 1399 unsigned VariablePos = State.Stack.back().VariablePos; 1400 if (State.Stack.size() == 1) { 1401 // Do not pop the last element. 1402 break; 1403 } 1404 State.Stack.pop_back(); 1405 State.Stack.back().VariablePos = VariablePos; 1406 } 1407 } 1408 1409 void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, 1410 bool Newline) { 1411 const FormatToken &Current = *State.NextToken; 1412 if (!Current.opensScope()) 1413 return; 1414 1415 // Don't allow '<' or '(' in C# generic type constraints to start new scopes. 1416 if (Current.isOneOf(tok::less, tok::l_paren) && 1417 State.Stack.back().IsCSharpGenericTypeConstraint) 1418 return; 1419 1420 if (Current.MatchingParen && Current.BlockKind == BK_Block) { 1421 moveStateToNewBlock(State); 1422 return; 1423 } 1424 1425 unsigned NewIndent; 1426 unsigned LastSpace = State.Stack.back().LastSpace; 1427 bool AvoidBinPacking; 1428 bool BreakBeforeParameter = false; 1429 unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall, 1430 State.Stack.back().NestedBlockIndent); 1431 if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) || 1432 opensProtoMessageField(Current, Style)) { 1433 if (Current.opensBlockOrBlockTypeList(Style)) { 1434 NewIndent = Style.IndentWidth + 1435 std::min(State.Column, State.Stack.back().NestedBlockIndent); 1436 } else { 1437 NewIndent = State.Stack.back().LastSpace + Style.ContinuationIndentWidth; 1438 } 1439 const FormatToken *NextNoComment = Current.getNextNonComment(); 1440 bool EndsInComma = Current.MatchingParen && 1441 Current.MatchingParen->Previous && 1442 Current.MatchingParen->Previous->is(tok::comma); 1443 AvoidBinPacking = EndsInComma || Current.is(TT_DictLiteral) || 1444 Style.Language == FormatStyle::LK_Proto || 1445 Style.Language == FormatStyle::LK_TextProto || 1446 !Style.BinPackArguments || 1447 (NextNoComment && 1448 NextNoComment->isOneOf(TT_DesignatedInitializerPeriod, 1449 TT_DesignatedInitializerLSquare)); 1450 BreakBeforeParameter = EndsInComma; 1451 if (Current.ParameterCount > 1) 1452 NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1); 1453 } else { 1454 NewIndent = Style.ContinuationIndentWidth + 1455 std::max(State.Stack.back().LastSpace, 1456 State.Stack.back().StartOfFunctionCall); 1457 1458 // Ensure that different different brackets force relative alignment, e.g.: 1459 // void SomeFunction(vector< // break 1460 // int> v); 1461 // FIXME: We likely want to do this for more combinations of brackets. 1462 if (Current.is(tok::less) && Current.ParentBracket == tok::l_paren) { 1463 NewIndent = std::max(NewIndent, State.Stack.back().Indent); 1464 LastSpace = std::max(LastSpace, State.Stack.back().Indent); 1465 } 1466 1467 bool EndsInComma = 1468 Current.MatchingParen && 1469 Current.MatchingParen->getPreviousNonComment() && 1470 Current.MatchingParen->getPreviousNonComment()->is(tok::comma); 1471 1472 // If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters 1473 // for backwards compatibility. 1474 bool ObjCBinPackProtocolList = 1475 (Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto && 1476 Style.BinPackParameters) || 1477 Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always; 1478 1479 bool BinPackDeclaration = 1480 (State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) || 1481 (State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList); 1482 1483 AvoidBinPacking = 1484 (State.Stack.back().IsCSharpGenericTypeConstraint) || 1485 (Style.Language == FormatStyle::LK_JavaScript && EndsInComma) || 1486 (State.Line->MustBeDeclaration && !BinPackDeclaration) || 1487 (!State.Line->MustBeDeclaration && !Style.BinPackArguments) || 1488 (Style.ExperimentalAutoDetectBinPacking && 1489 (Current.PackingKind == PPK_OnePerLine || 1490 (!BinPackInconclusiveFunctions && 1491 Current.PackingKind == PPK_Inconclusive))); 1492 1493 if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen && 1494 Style.ObjCBreakBeforeNestedBlockParam) { 1495 if (Style.ColumnLimit) { 1496 // If this '[' opens an ObjC call, determine whether all parameters fit 1497 // into one line and put one per line if they don't. 1498 if (getLengthToMatchingParen(Current, State.Stack) + State.Column > 1499 getColumnLimit(State)) 1500 BreakBeforeParameter = true; 1501 } else { 1502 // For ColumnLimit = 0, we have to figure out whether there is or has to 1503 // be a line break within this call. 1504 for (const FormatToken *Tok = &Current; 1505 Tok && Tok != Current.MatchingParen; Tok = Tok->Next) { 1506 if (Tok->MustBreakBefore || 1507 (Tok->CanBreakBefore && Tok->NewlinesBefore > 0)) { 1508 BreakBeforeParameter = true; 1509 break; 1510 } 1511 } 1512 } 1513 } 1514 1515 if (Style.Language == FormatStyle::LK_JavaScript && EndsInComma) 1516 BreakBeforeParameter = true; 1517 } 1518 // Generally inherit NoLineBreak from the current scope to nested scope. 1519 // However, don't do this for non-empty nested blocks, dict literals and 1520 // array literals as these follow different indentation rules. 1521 bool NoLineBreak = 1522 Current.Children.empty() && 1523 !Current.isOneOf(TT_DictLiteral, TT_ArrayInitializerLSquare) && 1524 (State.Stack.back().NoLineBreak || 1525 State.Stack.back().NoLineBreakInOperand || 1526 (Current.is(TT_TemplateOpener) && 1527 State.Stack.back().ContainsUnwrappedBuilder)); 1528 State.Stack.push_back( 1529 ParenState(&Current, NewIndent, LastSpace, AvoidBinPacking, NoLineBreak)); 1530 State.Stack.back().NestedBlockIndent = NestedBlockIndent; 1531 State.Stack.back().BreakBeforeParameter = BreakBeforeParameter; 1532 State.Stack.back().HasMultipleNestedBlocks = 1533 (Current.BlockParameterCount > 1); 1534 1535 if (Style.BraceWrapping.BeforeLambdaBody && Current.Next != nullptr && 1536 Current.Tok.is(tok::l_paren)) { 1537 // Search for any parameter that is a lambda 1538 FormatToken const *next = Current.Next; 1539 while (next != nullptr) { 1540 if (next->is(TT_LambdaLSquare)) { 1541 State.Stack.back().HasMultipleNestedBlocks = true; 1542 break; 1543 } 1544 next = next->Next; 1545 } 1546 } 1547 1548 State.Stack.back().IsInsideObjCArrayLiteral = 1549 Current.is(TT_ArrayInitializerLSquare) && Current.Previous && 1550 Current.Previous->is(tok::at); 1551 } 1552 1553 void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) { 1554 const FormatToken &Current = *State.NextToken; 1555 if (!Current.closesScope()) 1556 return; 1557 1558 // If we encounter a closing ), ], } or >, we can remove a level from our 1559 // stacks. 1560 if (State.Stack.size() > 1 && 1561 (Current.isOneOf(tok::r_paren, tok::r_square, TT_TemplateString) || 1562 (Current.is(tok::r_brace) && State.NextToken != State.Line->First) || 1563 State.NextToken->is(TT_TemplateCloser) || 1564 (Current.is(tok::greater) && Current.is(TT_DictLiteral)))) 1565 State.Stack.pop_back(); 1566 1567 // Reevaluate whether ObjC message arguments fit into one line. 1568 // If a receiver spans multiple lines, e.g.: 1569 // [[object block:^{ 1570 // return 42; 1571 // }] a:42 b:42]; 1572 // BreakBeforeParameter is calculated based on an incorrect assumption 1573 // (it is checked whether the whole expression fits into one line without 1574 // considering a line break inside a message receiver). 1575 // We check whether arguements fit after receiver scope closer (into the same 1576 // line). 1577 if (State.Stack.back().BreakBeforeParameter && Current.MatchingParen && 1578 Current.MatchingParen->Previous) { 1579 const FormatToken &CurrentScopeOpener = *Current.MatchingParen->Previous; 1580 if (CurrentScopeOpener.is(TT_ObjCMethodExpr) && 1581 CurrentScopeOpener.MatchingParen) { 1582 int NecessarySpaceInLine = 1583 getLengthToMatchingParen(CurrentScopeOpener, State.Stack) + 1584 CurrentScopeOpener.TotalLength - Current.TotalLength - 1; 1585 if (State.Column + Current.ColumnWidth + NecessarySpaceInLine <= 1586 Style.ColumnLimit) 1587 State.Stack.back().BreakBeforeParameter = false; 1588 } 1589 } 1590 1591 if (Current.is(tok::r_square)) { 1592 // If this ends the array subscript expr, reset the corresponding value. 1593 const FormatToken *NextNonComment = Current.getNextNonComment(); 1594 if (NextNonComment && NextNonComment->isNot(tok::l_square)) 1595 State.Stack.back().StartOfArraySubscripts = 0; 1596 } 1597 } 1598 1599 void ContinuationIndenter::moveStateToNewBlock(LineState &State) { 1600 unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent; 1601 // ObjC block sometimes follow special indentation rules. 1602 unsigned NewIndent = 1603 NestedBlockIndent + (State.NextToken->is(TT_ObjCBlockLBrace) 1604 ? Style.ObjCBlockIndentWidth 1605 : Style.IndentWidth); 1606 State.Stack.push_back(ParenState(State.NextToken, NewIndent, 1607 State.Stack.back().LastSpace, 1608 /*AvoidBinPacking=*/true, 1609 /*NoLineBreak=*/false)); 1610 State.Stack.back().NestedBlockIndent = NestedBlockIndent; 1611 State.Stack.back().BreakBeforeParameter = true; 1612 } 1613 1614 static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn, 1615 unsigned TabWidth, 1616 encoding::Encoding Encoding) { 1617 size_t LastNewlinePos = Text.find_last_of("\n"); 1618 if (LastNewlinePos == StringRef::npos) { 1619 return StartColumn + 1620 encoding::columnWidthWithTabs(Text, StartColumn, TabWidth, Encoding); 1621 } else { 1622 return encoding::columnWidthWithTabs(Text.substr(LastNewlinePos), 1623 /*StartColumn=*/0, TabWidth, Encoding); 1624 } 1625 } 1626 1627 unsigned ContinuationIndenter::reformatRawStringLiteral( 1628 const FormatToken &Current, LineState &State, 1629 const FormatStyle &RawStringStyle, bool DryRun, bool Newline) { 1630 unsigned StartColumn = State.Column - Current.ColumnWidth; 1631 StringRef OldDelimiter = *getRawStringDelimiter(Current.TokenText); 1632 StringRef NewDelimiter = 1633 getCanonicalRawStringDelimiter(Style, RawStringStyle.Language); 1634 if (NewDelimiter.empty() || OldDelimiter.empty()) 1635 NewDelimiter = OldDelimiter; 1636 // The text of a raw string is between the leading 'R"delimiter(' and the 1637 // trailing 'delimiter)"'. 1638 unsigned OldPrefixSize = 3 + OldDelimiter.size(); 1639 unsigned OldSuffixSize = 2 + OldDelimiter.size(); 1640 // We create a virtual text environment which expects a null-terminated 1641 // string, so we cannot use StringRef. 1642 std::string RawText = std::string( 1643 Current.TokenText.substr(OldPrefixSize).drop_back(OldSuffixSize)); 1644 if (NewDelimiter != OldDelimiter) { 1645 // Don't update to the canonical delimiter 'deli' if ')deli"' occurs in the 1646 // raw string. 1647 std::string CanonicalDelimiterSuffix = (")" + NewDelimiter + "\"").str(); 1648 if (StringRef(RawText).contains(CanonicalDelimiterSuffix)) 1649 NewDelimiter = OldDelimiter; 1650 } 1651 1652 unsigned NewPrefixSize = 3 + NewDelimiter.size(); 1653 unsigned NewSuffixSize = 2 + NewDelimiter.size(); 1654 1655 // The first start column is the column the raw text starts after formatting. 1656 unsigned FirstStartColumn = StartColumn + NewPrefixSize; 1657 1658 // The next start column is the intended indentation a line break inside 1659 // the raw string at level 0. It is determined by the following rules: 1660 // - if the content starts on newline, it is one level more than the current 1661 // indent, and 1662 // - if the content does not start on a newline, it is the first start 1663 // column. 1664 // These rules have the advantage that the formatted content both does not 1665 // violate the rectangle rule and visually flows within the surrounding 1666 // source. 1667 bool ContentStartsOnNewline = Current.TokenText[OldPrefixSize] == '\n'; 1668 // If this token is the last parameter (checked by looking if it's followed by 1669 // `)` and is not on a newline, the base the indent off the line's nested 1670 // block indent. Otherwise, base the indent off the arguments indent, so we 1671 // can achieve: 1672 // 1673 // fffffffffff(1, 2, 3, R"pb( 1674 // key1: 1 # 1675 // key2: 2)pb"); 1676 // 1677 // fffffffffff(1, 2, 3, 1678 // R"pb( 1679 // key1: 1 # 1680 // key2: 2 1681 // )pb"); 1682 // 1683 // fffffffffff(1, 2, 3, 1684 // R"pb( 1685 // key1: 1 # 1686 // key2: 2 1687 // )pb", 1688 // 5); 1689 unsigned CurrentIndent = 1690 (!Newline && Current.Next && Current.Next->is(tok::r_paren)) 1691 ? State.Stack.back().NestedBlockIndent 1692 : State.Stack.back().Indent; 1693 unsigned NextStartColumn = ContentStartsOnNewline 1694 ? CurrentIndent + Style.IndentWidth 1695 : FirstStartColumn; 1696 1697 // The last start column is the column the raw string suffix starts if it is 1698 // put on a newline. 1699 // The last start column is the intended indentation of the raw string postfix 1700 // if it is put on a newline. It is determined by the following rules: 1701 // - if the raw string prefix starts on a newline, it is the column where 1702 // that raw string prefix starts, and 1703 // - if the raw string prefix does not start on a newline, it is the current 1704 // indent. 1705 unsigned LastStartColumn = 1706 Current.NewlinesBefore ? FirstStartColumn - NewPrefixSize : CurrentIndent; 1707 1708 std::pair<tooling::Replacements, unsigned> Fixes = internal::reformat( 1709 RawStringStyle, RawText, {tooling::Range(0, RawText.size())}, 1710 FirstStartColumn, NextStartColumn, LastStartColumn, "<stdin>", 1711 /*Status=*/nullptr); 1712 1713 auto NewCode = applyAllReplacements(RawText, Fixes.first); 1714 tooling::Replacements NoFixes; 1715 if (!NewCode) { 1716 return addMultilineToken(Current, State); 1717 } 1718 if (!DryRun) { 1719 if (NewDelimiter != OldDelimiter) { 1720 // In 'R"delimiter(...', the delimiter starts 2 characters after the start 1721 // of the token. 1722 SourceLocation PrefixDelimiterStart = 1723 Current.Tok.getLocation().getLocWithOffset(2); 1724 auto PrefixErr = Whitespaces.addReplacement(tooling::Replacement( 1725 SourceMgr, PrefixDelimiterStart, OldDelimiter.size(), NewDelimiter)); 1726 if (PrefixErr) { 1727 llvm::errs() 1728 << "Failed to update the prefix delimiter of a raw string: " 1729 << llvm::toString(std::move(PrefixErr)) << "\n"; 1730 } 1731 // In 'R"delimiter(...)delimiter"', the suffix delimiter starts at 1732 // position length - 1 - |delimiter|. 1733 SourceLocation SuffixDelimiterStart = 1734 Current.Tok.getLocation().getLocWithOffset(Current.TokenText.size() - 1735 1 - OldDelimiter.size()); 1736 auto SuffixErr = Whitespaces.addReplacement(tooling::Replacement( 1737 SourceMgr, SuffixDelimiterStart, OldDelimiter.size(), NewDelimiter)); 1738 if (SuffixErr) { 1739 llvm::errs() 1740 << "Failed to update the suffix delimiter of a raw string: " 1741 << llvm::toString(std::move(SuffixErr)) << "\n"; 1742 } 1743 } 1744 SourceLocation OriginLoc = 1745 Current.Tok.getLocation().getLocWithOffset(OldPrefixSize); 1746 for (const tooling::Replacement &Fix : Fixes.first) { 1747 auto Err = Whitespaces.addReplacement(tooling::Replacement( 1748 SourceMgr, OriginLoc.getLocWithOffset(Fix.getOffset()), 1749 Fix.getLength(), Fix.getReplacementText())); 1750 if (Err) { 1751 llvm::errs() << "Failed to reformat raw string: " 1752 << llvm::toString(std::move(Err)) << "\n"; 1753 } 1754 } 1755 } 1756 unsigned RawLastLineEndColumn = getLastLineEndColumn( 1757 *NewCode, FirstStartColumn, Style.TabWidth, Encoding); 1758 State.Column = RawLastLineEndColumn + NewSuffixSize; 1759 // Since we're updating the column to after the raw string literal here, we 1760 // have to manually add the penalty for the prefix R"delim( over the column 1761 // limit. 1762 unsigned PrefixExcessCharacters = 1763 StartColumn + NewPrefixSize > Style.ColumnLimit 1764 ? StartColumn + NewPrefixSize - Style.ColumnLimit 1765 : 0; 1766 bool IsMultiline = 1767 ContentStartsOnNewline || (NewCode->find('\n') != std::string::npos); 1768 if (IsMultiline) { 1769 // Break before further function parameters on all levels. 1770 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) 1771 State.Stack[i].BreakBeforeParameter = true; 1772 } 1773 return Fixes.second + PrefixExcessCharacters * Style.PenaltyExcessCharacter; 1774 } 1775 1776 unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current, 1777 LineState &State) { 1778 // Break before further function parameters on all levels. 1779 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) 1780 State.Stack[i].BreakBeforeParameter = true; 1781 1782 unsigned ColumnsUsed = State.Column; 1783 // We can only affect layout of the first and the last line, so the penalty 1784 // for all other lines is constant, and we ignore it. 1785 State.Column = Current.LastLineColumnWidth; 1786 1787 if (ColumnsUsed > getColumnLimit(State)) 1788 return Style.PenaltyExcessCharacter * (ColumnsUsed - getColumnLimit(State)); 1789 return 0; 1790 } 1791 1792 unsigned ContinuationIndenter::handleEndOfLine(const FormatToken &Current, 1793 LineState &State, bool DryRun, 1794 bool AllowBreak, bool Newline) { 1795 unsigned Penalty = 0; 1796 // Compute the raw string style to use in case this is a raw string literal 1797 // that can be reformatted. 1798 auto RawStringStyle = getRawStringStyle(Current, State); 1799 if (RawStringStyle && !Current.Finalized) { 1800 Penalty = reformatRawStringLiteral(Current, State, *RawStringStyle, DryRun, 1801 Newline); 1802 } else if (Current.IsMultiline && Current.isNot(TT_BlockComment)) { 1803 // Don't break multi-line tokens other than block comments and raw string 1804 // literals. Instead, just update the state. 1805 Penalty = addMultilineToken(Current, State); 1806 } else if (State.Line->Type != LT_ImportStatement) { 1807 // We generally don't break import statements. 1808 LineState OriginalState = State; 1809 1810 // Whether we force the reflowing algorithm to stay strictly within the 1811 // column limit. 1812 bool Strict = false; 1813 // Whether the first non-strict attempt at reflowing did intentionally 1814 // exceed the column limit. 1815 bool Exceeded = false; 1816 std::tie(Penalty, Exceeded) = breakProtrudingToken( 1817 Current, State, AllowBreak, /*DryRun=*/true, Strict); 1818 if (Exceeded) { 1819 // If non-strict reflowing exceeds the column limit, try whether strict 1820 // reflowing leads to an overall lower penalty. 1821 LineState StrictState = OriginalState; 1822 unsigned StrictPenalty = 1823 breakProtrudingToken(Current, StrictState, AllowBreak, 1824 /*DryRun=*/true, /*Strict=*/true) 1825 .first; 1826 Strict = StrictPenalty <= Penalty; 1827 if (Strict) { 1828 Penalty = StrictPenalty; 1829 State = StrictState; 1830 } 1831 } 1832 if (!DryRun) { 1833 // If we're not in dry-run mode, apply the changes with the decision on 1834 // strictness made above. 1835 breakProtrudingToken(Current, OriginalState, AllowBreak, /*DryRun=*/false, 1836 Strict); 1837 } 1838 } 1839 if (State.Column > getColumnLimit(State)) { 1840 unsigned ExcessCharacters = State.Column - getColumnLimit(State); 1841 Penalty += Style.PenaltyExcessCharacter * ExcessCharacters; 1842 } 1843 return Penalty; 1844 } 1845 1846 // Returns the enclosing function name of a token, or the empty string if not 1847 // found. 1848 static StringRef getEnclosingFunctionName(const FormatToken &Current) { 1849 // Look for: 'function(' or 'function<templates>(' before Current. 1850 auto Tok = Current.getPreviousNonComment(); 1851 if (!Tok || !Tok->is(tok::l_paren)) 1852 return ""; 1853 Tok = Tok->getPreviousNonComment(); 1854 if (!Tok) 1855 return ""; 1856 if (Tok->is(TT_TemplateCloser)) { 1857 Tok = Tok->MatchingParen; 1858 if (Tok) 1859 Tok = Tok->getPreviousNonComment(); 1860 } 1861 if (!Tok || !Tok->is(tok::identifier)) 1862 return ""; 1863 return Tok->TokenText; 1864 } 1865 1866 llvm::Optional<FormatStyle> 1867 ContinuationIndenter::getRawStringStyle(const FormatToken &Current, 1868 const LineState &State) { 1869 if (!Current.isStringLiteral()) 1870 return None; 1871 auto Delimiter = getRawStringDelimiter(Current.TokenText); 1872 if (!Delimiter) 1873 return None; 1874 auto RawStringStyle = RawStringFormats.getDelimiterStyle(*Delimiter); 1875 if (!RawStringStyle && Delimiter->empty()) 1876 RawStringStyle = RawStringFormats.getEnclosingFunctionStyle( 1877 getEnclosingFunctionName(Current)); 1878 if (!RawStringStyle) 1879 return None; 1880 RawStringStyle->ColumnLimit = getColumnLimit(State); 1881 return RawStringStyle; 1882 } 1883 1884 std::unique_ptr<BreakableToken> 1885 ContinuationIndenter::createBreakableToken(const FormatToken &Current, 1886 LineState &State, bool AllowBreak) { 1887 unsigned StartColumn = State.Column - Current.ColumnWidth; 1888 if (Current.isStringLiteral()) { 1889 // FIXME: String literal breaking is currently disabled for C#, Java and 1890 // JavaScript, as it requires strings to be merged using "+" which we 1891 // don't support. 1892 if (Style.Language == FormatStyle::LK_Java || 1893 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp() || 1894 !Style.BreakStringLiterals || !AllowBreak) 1895 return nullptr; 1896 1897 // Don't break string literals inside preprocessor directives (except for 1898 // #define directives, as their contents are stored in separate lines and 1899 // are not affected by this check). 1900 // This way we avoid breaking code with line directives and unknown 1901 // preprocessor directives that contain long string literals. 1902 if (State.Line->Type == LT_PreprocessorDirective) 1903 return nullptr; 1904 // Exempts unterminated string literals from line breaking. The user will 1905 // likely want to terminate the string before any line breaking is done. 1906 if (Current.IsUnterminatedLiteral) 1907 return nullptr; 1908 // Don't break string literals inside Objective-C array literals (doing so 1909 // raises the warning -Wobjc-string-concatenation). 1910 if (State.Stack.back().IsInsideObjCArrayLiteral) { 1911 return nullptr; 1912 } 1913 1914 StringRef Text = Current.TokenText; 1915 StringRef Prefix; 1916 StringRef Postfix; 1917 // FIXME: Handle whitespace between '_T', '(', '"..."', and ')'. 1918 // FIXME: Store Prefix and Suffix (or PrefixLength and SuffixLength to 1919 // reduce the overhead) for each FormatToken, which is a string, so that we 1920 // don't run multiple checks here on the hot path. 1921 if ((Text.endswith(Postfix = "\"") && 1922 (Text.startswith(Prefix = "@\"") || Text.startswith(Prefix = "\"") || 1923 Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") || 1924 Text.startswith(Prefix = "u8\"") || 1925 Text.startswith(Prefix = "L\""))) || 1926 (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) { 1927 // We need this to address the case where there is an unbreakable tail 1928 // only if certain other formatting decisions have been taken. The 1929 // UnbreakableTailLength of Current is an overapproximation is that case 1930 // and we need to be correct here. 1931 unsigned UnbreakableTailLength = (State.NextToken && canBreak(State)) 1932 ? 0 1933 : Current.UnbreakableTailLength; 1934 return std::make_unique<BreakableStringLiteral>( 1935 Current, StartColumn, Prefix, Postfix, UnbreakableTailLength, 1936 State.Line->InPPDirective, Encoding, Style); 1937 } 1938 } else if (Current.is(TT_BlockComment)) { 1939 if (!Style.ReflowComments || 1940 // If a comment token switches formatting, like 1941 // /* clang-format on */, we don't want to break it further, 1942 // but we may still want to adjust its indentation. 1943 switchesFormatting(Current)) { 1944 return nullptr; 1945 } 1946 return std::make_unique<BreakableBlockComment>( 1947 Current, StartColumn, Current.OriginalColumn, !Current.Previous, 1948 State.Line->InPPDirective, Encoding, Style, Whitespaces.useCRLF()); 1949 } else if (Current.is(TT_LineComment) && 1950 (Current.Previous == nullptr || 1951 Current.Previous->isNot(TT_ImplicitStringLiteral))) { 1952 if (!Style.ReflowComments || 1953 CommentPragmasRegex.match(Current.TokenText.substr(2)) || 1954 switchesFormatting(Current)) 1955 return nullptr; 1956 return std::make_unique<BreakableLineCommentSection>( 1957 Current, StartColumn, Current.OriginalColumn, !Current.Previous, 1958 /*InPPDirective=*/false, Encoding, Style); 1959 } 1960 return nullptr; 1961 } 1962 1963 std::pair<unsigned, bool> 1964 ContinuationIndenter::breakProtrudingToken(const FormatToken &Current, 1965 LineState &State, bool AllowBreak, 1966 bool DryRun, bool Strict) { 1967 std::unique_ptr<const BreakableToken> Token = 1968 createBreakableToken(Current, State, AllowBreak); 1969 if (!Token) 1970 return {0, false}; 1971 assert(Token->getLineCount() > 0); 1972 unsigned ColumnLimit = getColumnLimit(State); 1973 if (Current.is(TT_LineComment)) { 1974 // We don't insert backslashes when breaking line comments. 1975 ColumnLimit = Style.ColumnLimit; 1976 } 1977 if (Current.UnbreakableTailLength >= ColumnLimit) 1978 return {0, false}; 1979 // ColumnWidth was already accounted into State.Column before calling 1980 // breakProtrudingToken. 1981 unsigned StartColumn = State.Column - Current.ColumnWidth; 1982 unsigned NewBreakPenalty = Current.isStringLiteral() 1983 ? Style.PenaltyBreakString 1984 : Style.PenaltyBreakComment; 1985 // Stores whether we intentionally decide to let a line exceed the column 1986 // limit. 1987 bool Exceeded = false; 1988 // Stores whether we introduce a break anywhere in the token. 1989 bool BreakInserted = Token->introducesBreakBeforeToken(); 1990 // Store whether we inserted a new line break at the end of the previous 1991 // logical line. 1992 bool NewBreakBefore = false; 1993 // We use a conservative reflowing strategy. Reflow starts after a line is 1994 // broken or the corresponding whitespace compressed. Reflow ends as soon as a 1995 // line that doesn't get reflown with the previous line is reached. 1996 bool Reflow = false; 1997 // Keep track of where we are in the token: 1998 // Where we are in the content of the current logical line. 1999 unsigned TailOffset = 0; 2000 // The column number we're currently at. 2001 unsigned ContentStartColumn = 2002 Token->getContentStartColumn(0, /*Break=*/false); 2003 // The number of columns left in the current logical line after TailOffset. 2004 unsigned RemainingTokenColumns = 2005 Token->getRemainingLength(0, TailOffset, ContentStartColumn); 2006 // Adapt the start of the token, for example indent. 2007 if (!DryRun) 2008 Token->adaptStartOfLine(0, Whitespaces); 2009 2010 unsigned ContentIndent = 0; 2011 unsigned Penalty = 0; 2012 LLVM_DEBUG(llvm::dbgs() << "Breaking protruding token at column " 2013 << StartColumn << ".\n"); 2014 for (unsigned LineIndex = 0, EndIndex = Token->getLineCount(); 2015 LineIndex != EndIndex; ++LineIndex) { 2016 LLVM_DEBUG(llvm::dbgs() 2017 << " Line: " << LineIndex << " (Reflow: " << Reflow << ")\n"); 2018 NewBreakBefore = false; 2019 // If we did reflow the previous line, we'll try reflowing again. Otherwise 2020 // we'll start reflowing if the current line is broken or whitespace is 2021 // compressed. 2022 bool TryReflow = Reflow; 2023 // Break the current token until we can fit the rest of the line. 2024 while (ContentStartColumn + RemainingTokenColumns > ColumnLimit) { 2025 LLVM_DEBUG(llvm::dbgs() << " Over limit, need: " 2026 << (ContentStartColumn + RemainingTokenColumns) 2027 << ", space: " << ColumnLimit 2028 << ", reflown prefix: " << ContentStartColumn 2029 << ", offset in line: " << TailOffset << "\n"); 2030 // If the current token doesn't fit, find the latest possible split in the 2031 // current line so that breaking at it will be under the column limit. 2032 // FIXME: Use the earliest possible split while reflowing to correctly 2033 // compress whitespace within a line. 2034 BreakableToken::Split Split = 2035 Token->getSplit(LineIndex, TailOffset, ColumnLimit, 2036 ContentStartColumn, CommentPragmasRegex); 2037 if (Split.first == StringRef::npos) { 2038 // No break opportunity - update the penalty and continue with the next 2039 // logical line. 2040 if (LineIndex < EndIndex - 1) 2041 // The last line's penalty is handled in addNextStateToQueue() or when 2042 // calling replaceWhitespaceAfterLastLine below. 2043 Penalty += Style.PenaltyExcessCharacter * 2044 (ContentStartColumn + RemainingTokenColumns - ColumnLimit); 2045 LLVM_DEBUG(llvm::dbgs() << " No break opportunity.\n"); 2046 break; 2047 } 2048 assert(Split.first != 0); 2049 2050 if (Token->supportsReflow()) { 2051 // Check whether the next natural split point after the current one can 2052 // still fit the line, either because we can compress away whitespace, 2053 // or because the penalty the excess characters introduce is lower than 2054 // the break penalty. 2055 // We only do this for tokens that support reflowing, and thus allow us 2056 // to change the whitespace arbitrarily (e.g. comments). 2057 // Other tokens, like string literals, can be broken on arbitrary 2058 // positions. 2059 2060 // First, compute the columns from TailOffset to the next possible split 2061 // position. 2062 // For example: 2063 // ColumnLimit: | 2064 // // Some text that breaks 2065 // ^ tail offset 2066 // ^-- split 2067 // ^-------- to split columns 2068 // ^--- next split 2069 // ^--------------- to next split columns 2070 unsigned ToSplitColumns = Token->getRangeLength( 2071 LineIndex, TailOffset, Split.first, ContentStartColumn); 2072 LLVM_DEBUG(llvm::dbgs() << " ToSplit: " << ToSplitColumns << "\n"); 2073 2074 BreakableToken::Split NextSplit = Token->getSplit( 2075 LineIndex, TailOffset + Split.first + Split.second, ColumnLimit, 2076 ContentStartColumn + ToSplitColumns + 1, CommentPragmasRegex); 2077 // Compute the columns necessary to fit the next non-breakable sequence 2078 // into the current line. 2079 unsigned ToNextSplitColumns = 0; 2080 if (NextSplit.first == StringRef::npos) { 2081 ToNextSplitColumns = Token->getRemainingLength(LineIndex, TailOffset, 2082 ContentStartColumn); 2083 } else { 2084 ToNextSplitColumns = Token->getRangeLength( 2085 LineIndex, TailOffset, 2086 Split.first + Split.second + NextSplit.first, ContentStartColumn); 2087 } 2088 // Compress the whitespace between the break and the start of the next 2089 // unbreakable sequence. 2090 ToNextSplitColumns = 2091 Token->getLengthAfterCompression(ToNextSplitColumns, Split); 2092 LLVM_DEBUG(llvm::dbgs() 2093 << " ContentStartColumn: " << ContentStartColumn << "\n"); 2094 LLVM_DEBUG(llvm::dbgs() 2095 << " ToNextSplit: " << ToNextSplitColumns << "\n"); 2096 // If the whitespace compression makes us fit, continue on the current 2097 // line. 2098 bool ContinueOnLine = 2099 ContentStartColumn + ToNextSplitColumns <= ColumnLimit; 2100 unsigned ExcessCharactersPenalty = 0; 2101 if (!ContinueOnLine && !Strict) { 2102 // Similarly, if the excess characters' penalty is lower than the 2103 // penalty of introducing a new break, continue on the current line. 2104 ExcessCharactersPenalty = 2105 (ContentStartColumn + ToNextSplitColumns - ColumnLimit) * 2106 Style.PenaltyExcessCharacter; 2107 LLVM_DEBUG(llvm::dbgs() 2108 << " Penalty excess: " << ExcessCharactersPenalty 2109 << "\n break : " << NewBreakPenalty << "\n"); 2110 if (ExcessCharactersPenalty < NewBreakPenalty) { 2111 Exceeded = true; 2112 ContinueOnLine = true; 2113 } 2114 } 2115 if (ContinueOnLine) { 2116 LLVM_DEBUG(llvm::dbgs() << " Continuing on line...\n"); 2117 // The current line fits after compressing the whitespace - reflow 2118 // the next line into it if possible. 2119 TryReflow = true; 2120 if (!DryRun) 2121 Token->compressWhitespace(LineIndex, TailOffset, Split, 2122 Whitespaces); 2123 // When we continue on the same line, leave one space between content. 2124 ContentStartColumn += ToSplitColumns + 1; 2125 Penalty += ExcessCharactersPenalty; 2126 TailOffset += Split.first + Split.second; 2127 RemainingTokenColumns = Token->getRemainingLength( 2128 LineIndex, TailOffset, ContentStartColumn); 2129 continue; 2130 } 2131 } 2132 LLVM_DEBUG(llvm::dbgs() << " Breaking...\n"); 2133 // Update the ContentIndent only if the current line was not reflown with 2134 // the previous line, since in that case the previous line should still 2135 // determine the ContentIndent. Also never intent the last line. 2136 if (!Reflow) 2137 ContentIndent = Token->getContentIndent(LineIndex); 2138 LLVM_DEBUG(llvm::dbgs() 2139 << " ContentIndent: " << ContentIndent << "\n"); 2140 ContentStartColumn = ContentIndent + Token->getContentStartColumn( 2141 LineIndex, /*Break=*/true); 2142 2143 unsigned NewRemainingTokenColumns = Token->getRemainingLength( 2144 LineIndex, TailOffset + Split.first + Split.second, 2145 ContentStartColumn); 2146 if (NewRemainingTokenColumns == 0) { 2147 // No content to indent. 2148 ContentIndent = 0; 2149 ContentStartColumn = 2150 Token->getContentStartColumn(LineIndex, /*Break=*/true); 2151 NewRemainingTokenColumns = Token->getRemainingLength( 2152 LineIndex, TailOffset + Split.first + Split.second, 2153 ContentStartColumn); 2154 } 2155 2156 // When breaking before a tab character, it may be moved by a few columns, 2157 // but will still be expanded to the next tab stop, so we don't save any 2158 // columns. 2159 if (NewRemainingTokenColumns == RemainingTokenColumns) { 2160 // FIXME: Do we need to adjust the penalty? 2161 break; 2162 } 2163 assert(NewRemainingTokenColumns < RemainingTokenColumns); 2164 2165 LLVM_DEBUG(llvm::dbgs() << " Breaking at: " << TailOffset + Split.first 2166 << ", " << Split.second << "\n"); 2167 if (!DryRun) 2168 Token->insertBreak(LineIndex, TailOffset, Split, ContentIndent, 2169 Whitespaces); 2170 2171 Penalty += NewBreakPenalty; 2172 TailOffset += Split.first + Split.second; 2173 RemainingTokenColumns = NewRemainingTokenColumns; 2174 BreakInserted = true; 2175 NewBreakBefore = true; 2176 } 2177 // In case there's another line, prepare the state for the start of the next 2178 // line. 2179 if (LineIndex + 1 != EndIndex) { 2180 unsigned NextLineIndex = LineIndex + 1; 2181 if (NewBreakBefore) 2182 // After breaking a line, try to reflow the next line into the current 2183 // one once RemainingTokenColumns fits. 2184 TryReflow = true; 2185 if (TryReflow) { 2186 // We decided that we want to try reflowing the next line into the 2187 // current one. 2188 // We will now adjust the state as if the reflow is successful (in 2189 // preparation for the next line), and see whether that works. If we 2190 // decide that we cannot reflow, we will later reset the state to the 2191 // start of the next line. 2192 Reflow = false; 2193 // As we did not continue breaking the line, RemainingTokenColumns is 2194 // known to fit after ContentStartColumn. Adapt ContentStartColumn to 2195 // the position at which we want to format the next line if we do 2196 // actually reflow. 2197 // When we reflow, we need to add a space between the end of the current 2198 // line and the next line's start column. 2199 ContentStartColumn += RemainingTokenColumns + 1; 2200 // Get the split that we need to reflow next logical line into the end 2201 // of the current one; the split will include any leading whitespace of 2202 // the next logical line. 2203 BreakableToken::Split SplitBeforeNext = 2204 Token->getReflowSplit(NextLineIndex, CommentPragmasRegex); 2205 LLVM_DEBUG(llvm::dbgs() 2206 << " Size of reflown text: " << ContentStartColumn 2207 << "\n Potential reflow split: "); 2208 if (SplitBeforeNext.first != StringRef::npos) { 2209 LLVM_DEBUG(llvm::dbgs() << SplitBeforeNext.first << ", " 2210 << SplitBeforeNext.second << "\n"); 2211 TailOffset = SplitBeforeNext.first + SplitBeforeNext.second; 2212 // If the rest of the next line fits into the current line below the 2213 // column limit, we can safely reflow. 2214 RemainingTokenColumns = Token->getRemainingLength( 2215 NextLineIndex, TailOffset, ContentStartColumn); 2216 Reflow = true; 2217 if (ContentStartColumn + RemainingTokenColumns > ColumnLimit) { 2218 LLVM_DEBUG(llvm::dbgs() 2219 << " Over limit after reflow, need: " 2220 << (ContentStartColumn + RemainingTokenColumns) 2221 << ", space: " << ColumnLimit 2222 << ", reflown prefix: " << ContentStartColumn 2223 << ", offset in line: " << TailOffset << "\n"); 2224 // If the whole next line does not fit, try to find a point in 2225 // the next line at which we can break so that attaching the part 2226 // of the next line to that break point onto the current line is 2227 // below the column limit. 2228 BreakableToken::Split Split = 2229 Token->getSplit(NextLineIndex, TailOffset, ColumnLimit, 2230 ContentStartColumn, CommentPragmasRegex); 2231 if (Split.first == StringRef::npos) { 2232 LLVM_DEBUG(llvm::dbgs() << " Did not find later break\n"); 2233 Reflow = false; 2234 } else { 2235 // Check whether the first split point gets us below the column 2236 // limit. Note that we will execute this split below as part of 2237 // the normal token breaking and reflow logic within the line. 2238 unsigned ToSplitColumns = Token->getRangeLength( 2239 NextLineIndex, TailOffset, Split.first, ContentStartColumn); 2240 if (ContentStartColumn + ToSplitColumns > ColumnLimit) { 2241 LLVM_DEBUG(llvm::dbgs() << " Next split protrudes, need: " 2242 << (ContentStartColumn + ToSplitColumns) 2243 << ", space: " << ColumnLimit); 2244 unsigned ExcessCharactersPenalty = 2245 (ContentStartColumn + ToSplitColumns - ColumnLimit) * 2246 Style.PenaltyExcessCharacter; 2247 if (NewBreakPenalty < ExcessCharactersPenalty) { 2248 Reflow = false; 2249 } 2250 } 2251 } 2252 } 2253 } else { 2254 LLVM_DEBUG(llvm::dbgs() << "not found.\n"); 2255 } 2256 } 2257 if (!Reflow) { 2258 // If we didn't reflow into the next line, the only space to consider is 2259 // the next logical line. Reset our state to match the start of the next 2260 // line. 2261 TailOffset = 0; 2262 ContentStartColumn = 2263 Token->getContentStartColumn(NextLineIndex, /*Break=*/false); 2264 RemainingTokenColumns = Token->getRemainingLength( 2265 NextLineIndex, TailOffset, ContentStartColumn); 2266 // Adapt the start of the token, for example indent. 2267 if (!DryRun) 2268 Token->adaptStartOfLine(NextLineIndex, Whitespaces); 2269 } else { 2270 // If we found a reflow split and have added a new break before the next 2271 // line, we are going to remove the line break at the start of the next 2272 // logical line. For example, here we'll add a new line break after 2273 // 'text', and subsequently delete the line break between 'that' and 2274 // 'reflows'. 2275 // // some text that 2276 // // reflows 2277 // -> 2278 // // some text 2279 // // that reflows 2280 // When adding the line break, we also added the penalty for it, so we 2281 // need to subtract that penalty again when we remove the line break due 2282 // to reflowing. 2283 if (NewBreakBefore) { 2284 assert(Penalty >= NewBreakPenalty); 2285 Penalty -= NewBreakPenalty; 2286 } 2287 if (!DryRun) 2288 Token->reflow(NextLineIndex, Whitespaces); 2289 } 2290 } 2291 } 2292 2293 BreakableToken::Split SplitAfterLastLine = 2294 Token->getSplitAfterLastLine(TailOffset); 2295 if (SplitAfterLastLine.first != StringRef::npos) { 2296 LLVM_DEBUG(llvm::dbgs() << "Replacing whitespace after last line.\n"); 2297 2298 // We add the last line's penalty here, since that line is going to be split 2299 // now. 2300 Penalty += Style.PenaltyExcessCharacter * 2301 (ContentStartColumn + RemainingTokenColumns - ColumnLimit); 2302 2303 if (!DryRun) 2304 Token->replaceWhitespaceAfterLastLine(TailOffset, SplitAfterLastLine, 2305 Whitespaces); 2306 ContentStartColumn = 2307 Token->getContentStartColumn(Token->getLineCount() - 1, /*Break=*/true); 2308 RemainingTokenColumns = Token->getRemainingLength( 2309 Token->getLineCount() - 1, 2310 TailOffset + SplitAfterLastLine.first + SplitAfterLastLine.second, 2311 ContentStartColumn); 2312 } 2313 2314 State.Column = ContentStartColumn + RemainingTokenColumns - 2315 Current.UnbreakableTailLength; 2316 2317 if (BreakInserted) { 2318 // If we break the token inside a parameter list, we need to break before 2319 // the next parameter on all levels, so that the next parameter is clearly 2320 // visible. Line comments already introduce a break. 2321 if (Current.isNot(TT_LineComment)) { 2322 for (unsigned i = 0, e = State.Stack.size(); i != e; ++i) 2323 State.Stack[i].BreakBeforeParameter = true; 2324 } 2325 2326 if (Current.is(TT_BlockComment)) 2327 State.NoContinuation = true; 2328 2329 State.Stack.back().LastSpace = StartColumn; 2330 } 2331 2332 Token->updateNextToken(State); 2333 2334 return {Penalty, Exceeded}; 2335 } 2336 2337 unsigned ContinuationIndenter::getColumnLimit(const LineState &State) const { 2338 // In preprocessor directives reserve two chars for trailing " \" 2339 return Style.ColumnLimit - (State.Line->InPPDirective ? 2 : 0); 2340 } 2341 2342 bool ContinuationIndenter::nextIsMultilineString(const LineState &State) { 2343 const FormatToken &Current = *State.NextToken; 2344 if (!Current.isStringLiteral() || Current.is(TT_ImplicitStringLiteral)) 2345 return false; 2346 // We never consider raw string literals "multiline" for the purpose of 2347 // AlwaysBreakBeforeMultilineStrings implementation as they are special-cased 2348 // (see TokenAnnotator::mustBreakBefore(). 2349 if (Current.TokenText.startswith("R\"")) 2350 return false; 2351 if (Current.IsMultiline) 2352 return true; 2353 if (Current.getNextNonComment() && 2354 Current.getNextNonComment()->isStringLiteral()) 2355 return true; // Implicit concatenation. 2356 if (Style.ColumnLimit != 0 && Style.BreakStringLiterals && 2357 State.Column + Current.ColumnWidth + Current.UnbreakableTailLength > 2358 Style.ColumnLimit) 2359 return true; // String will be split. 2360 return false; 2361 } 2362 2363 } // namespace format 2364 } // namespace clang 2365