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