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