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