1 //===--- UnwrappedLineFormatter.cpp - Format C++ code ---------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "UnwrappedLineFormatter.h" 10 #include "NamespaceEndCommentsFixer.h" 11 #include "WhitespaceManager.h" 12 #include "llvm/Support/Debug.h" 13 #include <queue> 14 15 #define DEBUG_TYPE "format-formatter" 16 17 namespace clang { 18 namespace format { 19 20 namespace { 21 22 bool startsExternCBlock(const AnnotatedLine &Line) { 23 const FormatToken *Next = Line.First->getNextNonComment(); 24 const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr; 25 return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() && 26 NextNext && NextNext->is(tok::l_brace); 27 } 28 29 /// Tracks the indent level of \c AnnotatedLines across levels. 30 /// 31 /// \c nextLine must be called for each \c AnnotatedLine, after which \c 32 /// getIndent() will return the indent for the last line \c nextLine was called 33 /// with. 34 /// If the line is not formatted (and thus the indent does not change), calling 35 /// \c adjustToUnmodifiedLine after the call to \c nextLine will cause 36 /// subsequent lines on the same level to be indented at the same level as the 37 /// given line. 38 class LevelIndentTracker { 39 public: 40 LevelIndentTracker(const FormatStyle &Style, 41 const AdditionalKeywords &Keywords, unsigned StartLevel, 42 int AdditionalIndent) 43 : Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) { 44 for (unsigned i = 0; i != StartLevel; ++i) 45 IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent); 46 } 47 48 /// Returns the indent for the current line. 49 unsigned getIndent() const { return Indent; } 50 51 /// Update the indent state given that \p Line is going to be formatted 52 /// next. 53 void nextLine(const AnnotatedLine &Line) { 54 Offset = getIndentOffset(*Line.First); 55 // Update the indent level cache size so that we can rely on it 56 // having the right size in adjustToUnmodifiedline. 57 while (IndentForLevel.size() <= Line.Level) 58 IndentForLevel.push_back(-1); 59 if (Line.InPPDirective) { 60 Indent = Line.Level * Style.IndentWidth + AdditionalIndent; 61 } else { 62 IndentForLevel.resize(Line.Level + 1); 63 Indent = getIndent(IndentForLevel, Line.Level); 64 } 65 if (static_cast<int>(Indent) + Offset >= 0) 66 Indent += Offset; 67 if (Line.First->is(TT_CSharpGenericTypeConstraint)) 68 Indent = Line.Level * Style.IndentWidth + Style.ContinuationIndentWidth; 69 } 70 71 /// Update the indent state given that \p Line indent should be 72 /// skipped. 73 void skipLine(const AnnotatedLine &Line) { 74 while (IndentForLevel.size() <= Line.Level) 75 IndentForLevel.push_back(Indent); 76 } 77 78 /// Update the level indent to adapt to the given \p Line. 79 /// 80 /// When a line is not formatted, we move the subsequent lines on the same 81 /// level to the same indent. 82 /// Note that \c nextLine must have been called before this method. 83 void adjustToUnmodifiedLine(const AnnotatedLine &Line) { 84 unsigned LevelIndent = Line.First->OriginalColumn; 85 if (static_cast<int>(LevelIndent) - Offset >= 0) 86 LevelIndent -= Offset; 87 if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) && 88 !Line.InPPDirective) 89 IndentForLevel[Line.Level] = LevelIndent; 90 } 91 92 private: 93 /// Get the offset of the line relatively to the level. 94 /// 95 /// For example, 'public:' labels in classes are offset by 1 or 2 96 /// characters to the left from their level. 97 int getIndentOffset(const FormatToken &RootToken) { 98 if (Style.Language == FormatStyle::LK_Java || 99 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) 100 return 0; 101 if (RootToken.isAccessSpecifier(false) || 102 RootToken.isObjCAccessSpecifier() || 103 (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) && 104 RootToken.Next && RootToken.Next->is(tok::colon))) { 105 // The AccessModifierOffset may be overriden by IndentAccessModifiers, 106 // in which case we take a negative value of the IndentWidth to simulate 107 // the upper indent level. 108 return Style.IndentAccessModifiers ? -Style.IndentWidth 109 : Style.AccessModifierOffset; 110 } 111 return 0; 112 } 113 114 /// Get the indent of \p Level from \p IndentForLevel. 115 /// 116 /// \p IndentForLevel must contain the indent for the level \c l 117 /// at \p IndentForLevel[l], or a value < 0 if the indent for 118 /// that level is unknown. 119 unsigned getIndent(ArrayRef<int> IndentForLevel, unsigned Level) { 120 if (IndentForLevel[Level] != -1) 121 return IndentForLevel[Level]; 122 if (Level == 0) 123 return 0; 124 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; 125 } 126 127 const FormatStyle &Style; 128 const AdditionalKeywords &Keywords; 129 const unsigned AdditionalIndent; 130 131 /// The indent in characters for each level. 132 std::vector<int> IndentForLevel; 133 134 /// Offset of the current line relative to the indent level. 135 /// 136 /// For example, the 'public' keywords is often indented with a negative 137 /// offset. 138 int Offset = 0; 139 140 /// The current line's indent. 141 unsigned Indent = 0; 142 }; 143 144 const FormatToken *getMatchingNamespaceToken( 145 const AnnotatedLine *Line, 146 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { 147 if (!Line->startsWith(tok::r_brace)) 148 return nullptr; 149 size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex; 150 if (StartLineIndex == UnwrappedLine::kInvalidIndex) 151 return nullptr; 152 assert(StartLineIndex < AnnotatedLines.size()); 153 return AnnotatedLines[StartLineIndex]->First->getNamespaceToken(); 154 } 155 156 StringRef getNamespaceTokenText(const AnnotatedLine *Line) { 157 const FormatToken *NamespaceToken = Line->First->getNamespaceToken(); 158 return NamespaceToken ? NamespaceToken->TokenText : StringRef(); 159 } 160 161 StringRef getMatchingNamespaceTokenText( 162 const AnnotatedLine *Line, 163 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { 164 const FormatToken *NamespaceToken = 165 getMatchingNamespaceToken(Line, AnnotatedLines); 166 return NamespaceToken ? NamespaceToken->TokenText : StringRef(); 167 } 168 169 class LineJoiner { 170 public: 171 LineJoiner(const FormatStyle &Style, const AdditionalKeywords &Keywords, 172 const SmallVectorImpl<AnnotatedLine *> &Lines) 173 : Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()), 174 AnnotatedLines(Lines) {} 175 176 /// Returns the next line, merging multiple lines into one if possible. 177 const AnnotatedLine *getNextMergedLine(bool DryRun, 178 LevelIndentTracker &IndentTracker) { 179 if (Next == End) 180 return nullptr; 181 const AnnotatedLine *Current = *Next; 182 IndentTracker.nextLine(*Current); 183 unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End); 184 if (MergedLines > 0 && Style.ColumnLimit == 0) 185 // Disallow line merging if there is a break at the start of one of the 186 // input lines. 187 for (unsigned i = 0; i < MergedLines; ++i) 188 if (Next[i + 1]->First->NewlinesBefore > 0) 189 MergedLines = 0; 190 if (!DryRun) 191 for (unsigned i = 0; i < MergedLines; ++i) 192 join(*Next[0], *Next[i + 1]); 193 Next = Next + MergedLines + 1; 194 return Current; 195 } 196 197 private: 198 /// Calculates how many lines can be merged into 1 starting at \p I. 199 unsigned 200 tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker, 201 SmallVectorImpl<AnnotatedLine *>::const_iterator I, 202 SmallVectorImpl<AnnotatedLine *>::const_iterator E) { 203 const unsigned Indent = IndentTracker.getIndent(); 204 205 // Can't join the last line with anything. 206 if (I + 1 == E) 207 return 0; 208 // We can never merge stuff if there are trailing line comments. 209 const AnnotatedLine *TheLine = *I; 210 if (TheLine->Last->is(TT_LineComment)) 211 return 0; 212 if (I[1]->Type == LT_Invalid || I[1]->First->MustBreakBefore) 213 return 0; 214 if (TheLine->InPPDirective && 215 (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline)) 216 return 0; 217 218 if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit) 219 return 0; 220 221 unsigned Limit = 222 Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent; 223 // If we already exceed the column limit, we set 'Limit' to 0. The different 224 // tryMerge..() functions can then decide whether to still do merging. 225 Limit = TheLine->Last->TotalLength > Limit 226 ? 0 227 : Limit - TheLine->Last->TotalLength; 228 229 if (TheLine->Last->is(TT_FunctionLBrace) && 230 TheLine->First == TheLine->Last && 231 !Style.BraceWrapping.SplitEmptyFunction && 232 I[1]->First->is(tok::r_brace)) 233 return tryMergeSimpleBlock(I, E, Limit); 234 235 // Handle empty record blocks where the brace has already been wrapped 236 if (TheLine->Last->is(tok::l_brace) && TheLine->First == TheLine->Last && 237 I != AnnotatedLines.begin()) { 238 bool EmptyBlock = I[1]->First->is(tok::r_brace); 239 240 const FormatToken *Tok = I[-1]->First; 241 if (Tok && Tok->is(tok::comment)) 242 Tok = Tok->getNextNonComment(); 243 244 if (Tok && Tok->getNamespaceToken()) 245 return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock 246 ? tryMergeSimpleBlock(I, E, Limit) 247 : 0; 248 249 if (Tok && Tok->is(tok::kw_typedef)) 250 Tok = Tok->getNextNonComment(); 251 if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union, 252 tok::kw_extern, Keywords.kw_interface)) 253 return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock 254 ? tryMergeSimpleBlock(I, E, Limit) 255 : 0; 256 257 if (Tok && Tok->is(tok::kw_template) && 258 Style.BraceWrapping.SplitEmptyRecord && EmptyBlock) { 259 return 0; 260 } 261 } 262 263 // FIXME: TheLine->Level != 0 might or might not be the right check to do. 264 // If necessary, change to something smarter. 265 bool MergeShortFunctions = 266 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All || 267 (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty && 268 I[1]->First->is(tok::r_brace)) || 269 (Style.AllowShortFunctionsOnASingleLine & FormatStyle::SFS_InlineOnly && 270 TheLine->Level != 0); 271 272 if (Style.CompactNamespaces) { 273 if (auto nsToken = TheLine->First->getNamespaceToken()) { 274 int i = 0; 275 unsigned closingLine = TheLine->MatchingClosingBlockLineIndex - 1; 276 for (; I + 1 + i != E && 277 nsToken->TokenText == getNamespaceTokenText(I[i + 1]) && 278 closingLine == I[i + 1]->MatchingClosingBlockLineIndex && 279 I[i + 1]->Last->TotalLength < Limit; 280 i++, closingLine--) { 281 // No extra indent for compacted namespaces 282 IndentTracker.skipLine(*I[i + 1]); 283 284 Limit -= I[i + 1]->Last->TotalLength; 285 } 286 return i; 287 } 288 289 if (auto nsToken = getMatchingNamespaceToken(TheLine, AnnotatedLines)) { 290 int i = 0; 291 unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1; 292 for (; I + 1 + i != E && 293 nsToken->TokenText == 294 getMatchingNamespaceTokenText(I[i + 1], AnnotatedLines) && 295 openingLine == I[i + 1]->MatchingOpeningBlockLineIndex; 296 i++, openingLine--) { 297 // No space between consecutive braces 298 I[i + 1]->First->SpacesRequiredBefore = !I[i]->Last->is(tok::r_brace); 299 300 // Indent like the outer-most namespace 301 IndentTracker.nextLine(*I[i + 1]); 302 } 303 return i; 304 } 305 } 306 307 // Try to merge a function block with left brace unwrapped 308 if (TheLine->Last->is(TT_FunctionLBrace) && 309 TheLine->First != TheLine->Last) { 310 return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0; 311 } 312 // Try to merge a control statement block with left brace unwrapped 313 if (TheLine->Last->is(tok::l_brace) && TheLine->First != TheLine->Last && 314 TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) { 315 return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never 316 ? tryMergeSimpleBlock(I, E, Limit) 317 : 0; 318 } 319 // Try to merge a control statement block with left brace wrapped 320 if (I[1]->First->is(tok::l_brace) && 321 (TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for, 322 tok::kw_switch, tok::kw_try, tok::kw_do, 323 TT_ForEachMacro) || 324 (TheLine->First->is(tok::r_brace) && TheLine->First->Next && 325 TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) && 326 Style.BraceWrapping.AfterControlStatement == 327 FormatStyle::BWACS_MultiLine) { 328 // If possible, merge the next line's wrapped left brace with the current 329 // line. Otherwise, leave it on the next line, as this is a multi-line 330 // control statement. 331 return (Style.ColumnLimit == 0 || 332 TheLine->Last->TotalLength <= Style.ColumnLimit) 333 ? 1 334 : 0; 335 } else if (I[1]->First->is(tok::l_brace) && 336 TheLine->First->isOneOf(tok::kw_if, tok::kw_while, 337 tok::kw_for)) { 338 return (Style.BraceWrapping.AfterControlStatement == 339 FormatStyle::BWACS_Always) 340 ? tryMergeSimpleBlock(I, E, Limit) 341 : 0; 342 } else if (I[1]->First->is(tok::l_brace) && 343 TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) && 344 Style.BraceWrapping.AfterControlStatement == 345 FormatStyle::BWACS_MultiLine) { 346 // This case if different from the upper BWACS_MultiLine processing 347 // in that a preceding r_brace is not on the same line as else/catch 348 // most likely because of BeforeElse/BeforeCatch set to true. 349 // If the line length doesn't fit ColumnLimit, leave l_brace on the 350 // next line to respect the BWACS_MultiLine. 351 return (Style.ColumnLimit == 0 || 352 TheLine->Last->TotalLength <= Style.ColumnLimit) 353 ? 1 354 : 0; 355 } 356 // Don't merge block with left brace wrapped after ObjC special blocks 357 if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() && 358 I[-1]->First->is(tok::at) && I[-1]->First->Next) { 359 tok::ObjCKeywordKind kwId = I[-1]->First->Next->Tok.getObjCKeywordID(); 360 if (kwId == clang::tok::objc_autoreleasepool || 361 kwId == clang::tok::objc_synchronized) 362 return 0; 363 } 364 // Don't merge block with left brace wrapped after case labels 365 if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() && 366 I[-1]->First->isOneOf(tok::kw_case, tok::kw_default)) 367 return 0; 368 369 // Don't merge an empty template class or struct if SplitEmptyRecords 370 // is defined. 371 if (Style.BraceWrapping.SplitEmptyRecord && 372 TheLine->Last->is(tok::l_brace) && I != AnnotatedLines.begin() && 373 I[-1]->Last) { 374 const FormatToken *Previous = I[-1]->Last; 375 if (Previous) { 376 if (Previous->is(tok::comment)) 377 Previous = Previous->getPreviousNonComment(); 378 if (Previous) { 379 if (Previous->is(tok::greater) && !I[-1]->InPPDirective) 380 return 0; 381 if (Previous->is(tok::identifier)) { 382 const FormatToken *PreviousPrevious = 383 Previous->getPreviousNonComment(); 384 if (PreviousPrevious && 385 PreviousPrevious->isOneOf(tok::kw_class, tok::kw_struct)) 386 return 0; 387 } 388 } 389 } 390 } 391 392 // Try to merge a block with left brace wrapped that wasn't yet covered 393 if (TheLine->Last->is(tok::l_brace)) { 394 return !Style.BraceWrapping.AfterFunction || 395 (I[1]->First->is(tok::r_brace) && 396 !Style.BraceWrapping.SplitEmptyRecord) 397 ? tryMergeSimpleBlock(I, E, Limit) 398 : 0; 399 } 400 // Try to merge a function block with left brace wrapped 401 if (I[1]->First->is(TT_FunctionLBrace) && 402 Style.BraceWrapping.AfterFunction) { 403 if (I[1]->Last->is(TT_LineComment)) 404 return 0; 405 406 // Check for Limit <= 2 to account for the " {". 407 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine))) 408 return 0; 409 Limit -= 2; 410 411 unsigned MergedLines = 0; 412 if (MergeShortFunctions || 413 (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty && 414 I[1]->First == I[1]->Last && I + 2 != E && 415 I[2]->First->is(tok::r_brace))) { 416 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit); 417 // If we managed to merge the block, count the function header, which is 418 // on a separate line. 419 if (MergedLines > 0) 420 ++MergedLines; 421 } 422 return MergedLines; 423 } 424 auto IsElseLine = [&TheLine]() -> bool { 425 const FormatToken *First = TheLine->First; 426 if (First->is(tok::kw_else)) 427 return true; 428 429 return First->is(tok::r_brace) && First->Next && 430 First->Next->is(tok::kw_else); 431 }; 432 if (TheLine->First->is(tok::kw_if) || 433 (IsElseLine() && (Style.AllowShortIfStatementsOnASingleLine == 434 FormatStyle::SIS_AllIfsAndElse))) { 435 return Style.AllowShortIfStatementsOnASingleLine 436 ? tryMergeSimpleControlStatement(I, E, Limit) 437 : 0; 438 } 439 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do)) { 440 return Style.AllowShortLoopsOnASingleLine 441 ? tryMergeSimpleControlStatement(I, E, Limit) 442 : 0; 443 } 444 if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) { 445 return Style.AllowShortCaseLabelsOnASingleLine 446 ? tryMergeShortCaseLabels(I, E, Limit) 447 : 0; 448 } 449 if (TheLine->InPPDirective && 450 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) { 451 return tryMergeSimplePPDirective(I, E, Limit); 452 } 453 return 0; 454 } 455 456 unsigned 457 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 458 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 459 unsigned Limit) { 460 if (Limit == 0) 461 return 0; 462 if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline) 463 return 0; 464 if (1 + I[1]->Last->TotalLength > Limit) 465 return 0; 466 return 1; 467 } 468 469 unsigned tryMergeSimpleControlStatement( 470 SmallVectorImpl<AnnotatedLine *>::const_iterator I, 471 SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) { 472 if (Limit == 0) 473 return 0; 474 if (Style.BraceWrapping.AfterControlStatement == 475 FormatStyle::BWACS_Always && 476 I[1]->First->is(tok::l_brace) && 477 Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) 478 return 0; 479 if (I[1]->InPPDirective != (*I)->InPPDirective || 480 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) 481 return 0; 482 Limit = limitConsideringMacros(I + 1, E, Limit); 483 AnnotatedLine &Line = **I; 484 if (!Line.First->is(tok::kw_do) && !Line.First->is(tok::kw_else) && 485 !Line.Last->is(tok::kw_else) && Line.Last->isNot(tok::r_paren)) 486 return 0; 487 // Only merge do while if do is the only statement on the line. 488 if (Line.First->is(tok::kw_do) && !Line.Last->is(tok::kw_do)) 489 return 0; 490 if (1 + I[1]->Last->TotalLength > Limit) 491 return 0; 492 if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while, 493 TT_LineComment)) 494 return 0; 495 // Only inline simple if's (no nested if or else), unless specified 496 if (Style.AllowShortIfStatementsOnASingleLine == 497 FormatStyle::SIS_WithoutElse) { 498 if (I + 2 != E && Line.startsWith(tok::kw_if) && 499 I[2]->First->is(tok::kw_else)) 500 return 0; 501 } 502 return 1; 503 } 504 505 unsigned 506 tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 507 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 508 unsigned Limit) { 509 if (Limit == 0 || I + 1 == E || 510 I[1]->First->isOneOf(tok::kw_case, tok::kw_default)) 511 return 0; 512 if (I[0]->Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace)) 513 return 0; 514 unsigned NumStmts = 0; 515 unsigned Length = 0; 516 bool EndsWithComment = false; 517 bool InPPDirective = I[0]->InPPDirective; 518 const unsigned Level = I[0]->Level; 519 for (; NumStmts < 3; ++NumStmts) { 520 if (I + 1 + NumStmts == E) 521 break; 522 const AnnotatedLine *Line = I[1 + NumStmts]; 523 if (Line->InPPDirective != InPPDirective) 524 break; 525 if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace)) 526 break; 527 if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch, 528 tok::kw_while) || 529 EndsWithComment) 530 return 0; 531 if (Line->First->is(tok::comment)) { 532 if (Level != Line->Level) 533 return 0; 534 SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts; 535 for (; J != E; ++J) { 536 Line = *J; 537 if (Line->InPPDirective != InPPDirective) 538 break; 539 if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace)) 540 break; 541 if (Line->First->isNot(tok::comment) || Level != Line->Level) 542 return 0; 543 } 544 break; 545 } 546 if (Line->Last->is(tok::comment)) 547 EndsWithComment = true; 548 Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space. 549 } 550 if (NumStmts == 0 || NumStmts == 3 || Length > Limit) 551 return 0; 552 return NumStmts; 553 } 554 555 unsigned 556 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 557 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 558 unsigned Limit) { 559 AnnotatedLine &Line = **I; 560 561 // Don't merge ObjC @ keywords and methods. 562 // FIXME: If an option to allow short exception handling clauses on a single 563 // line is added, change this to not return for @try and friends. 564 if (Style.Language != FormatStyle::LK_Java && 565 Line.First->isOneOf(tok::at, tok::minus, tok::plus)) 566 return 0; 567 568 // Check that the current line allows merging. This depends on whether we 569 // are in a control flow statements as well as several style flags. 570 if (Line.First->isOneOf(tok::kw_else, tok::kw_case) || 571 (Line.First->Next && Line.First->Next->is(tok::kw_else))) 572 return 0; 573 // default: in switch statement 574 if (Line.First->is(tok::kw_default)) { 575 const FormatToken *Tok = Line.First->getNextNonComment(); 576 if (Tok && Tok->is(tok::colon)) 577 return 0; 578 } 579 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try, 580 tok::kw___try, tok::kw_catch, tok::kw___finally, 581 tok::kw_for, tok::r_brace, Keywords.kw___except)) { 582 if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) 583 return 0; 584 // Don't merge when we can't except the case when 585 // the control statement block is empty 586 if (!Style.AllowShortIfStatementsOnASingleLine && 587 Line.startsWith(tok::kw_if) && 588 !Style.BraceWrapping.AfterControlStatement && 589 !I[1]->First->is(tok::r_brace)) 590 return 0; 591 if (!Style.AllowShortIfStatementsOnASingleLine && 592 Line.startsWith(tok::kw_if) && 593 Style.BraceWrapping.AfterControlStatement == 594 FormatStyle::BWACS_Always && 595 I + 2 != E && !I[2]->First->is(tok::r_brace)) 596 return 0; 597 if (!Style.AllowShortLoopsOnASingleLine && 598 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) && 599 !Style.BraceWrapping.AfterControlStatement && 600 !I[1]->First->is(tok::r_brace)) 601 return 0; 602 if (!Style.AllowShortLoopsOnASingleLine && 603 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) && 604 Style.BraceWrapping.AfterControlStatement == 605 FormatStyle::BWACS_Always && 606 I + 2 != E && !I[2]->First->is(tok::r_brace)) 607 return 0; 608 // FIXME: Consider an option to allow short exception handling clauses on 609 // a single line. 610 // FIXME: This isn't covered by tests. 611 // FIXME: For catch, __except, __finally the first token on the line 612 // is '}', so this isn't correct here. 613 if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch, 614 Keywords.kw___except, tok::kw___finally)) 615 return 0; 616 } 617 618 if (Line.Last->is(tok::l_brace)) { 619 FormatToken *Tok = I[1]->First; 620 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && 621 (Tok->getNextNonComment() == nullptr || 622 Tok->getNextNonComment()->is(tok::semi))) { 623 // We merge empty blocks even if the line exceeds the column limit. 624 Tok->SpacesRequiredBefore = Style.SpaceInEmptyBlock ? 1 : 0; 625 Tok->CanBreakBefore = true; 626 return 1; 627 } else if (Limit != 0 && !Line.startsWithNamespace() && 628 !startsExternCBlock(Line)) { 629 // We don't merge short records. 630 FormatToken *RecordTok = Line.First; 631 // Skip record modifiers. 632 while (RecordTok->Next && 633 RecordTok->isOneOf( 634 tok::kw_typedef, tok::kw_export, Keywords.kw_declare, 635 Keywords.kw_abstract, tok::kw_default, tok::kw_public, 636 tok::kw_private, tok::kw_protected, Keywords.kw_internal)) 637 RecordTok = RecordTok->Next; 638 if (RecordTok && 639 RecordTok->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct, 640 Keywords.kw_interface)) 641 return 0; 642 643 // Check that we still have three lines and they fit into the limit. 644 if (I + 2 == E || I[2]->Type == LT_Invalid) 645 return 0; 646 Limit = limitConsideringMacros(I + 2, E, Limit); 647 648 if (!nextTwoLinesFitInto(I, Limit)) 649 return 0; 650 651 // Second, check that the next line does not contain any braces - if it 652 // does, readability declines when putting it into a single line. 653 if (I[1]->Last->is(TT_LineComment)) 654 return 0; 655 do { 656 if (Tok->is(tok::l_brace) && Tok->isNot(BK_BracedInit)) 657 return 0; 658 Tok = Tok->Next; 659 } while (Tok); 660 661 // Last, check that the third line starts with a closing brace. 662 Tok = I[2]->First; 663 if (Tok->isNot(tok::r_brace)) 664 return 0; 665 666 // Don't merge "if (a) { .. } else {". 667 if (Tok->Next && Tok->Next->is(tok::kw_else)) 668 return 0; 669 670 // Don't merge a trailing multi-line control statement block like: 671 // } else if (foo && 672 // bar) 673 // { <-- current Line 674 // baz(); 675 // } 676 if (Line.First == Line.Last && 677 Style.BraceWrapping.AfterControlStatement == 678 FormatStyle::BWACS_MultiLine) 679 return 0; 680 681 return 2; 682 } 683 } else if (I[1]->First->is(tok::l_brace)) { 684 if (I[1]->Last->is(TT_LineComment)) 685 return 0; 686 687 // Check for Limit <= 2 to account for the " {". 688 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I))) 689 return 0; 690 Limit -= 2; 691 unsigned MergedLines = 0; 692 if (Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never || 693 (I[1]->First == I[1]->Last && I + 2 != E && 694 I[2]->First->is(tok::r_brace))) { 695 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit); 696 // If we managed to merge the block, count the statement header, which 697 // is on a separate line. 698 if (MergedLines > 0) 699 ++MergedLines; 700 } 701 return MergedLines; 702 } 703 return 0; 704 } 705 706 /// Returns the modified column limit for \p I if it is inside a macro and 707 /// needs a trailing '\'. 708 unsigned 709 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 710 SmallVectorImpl<AnnotatedLine *>::const_iterator E, 711 unsigned Limit) { 712 if (I[0]->InPPDirective && I + 1 != E && 713 !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) { 714 return Limit < 2 ? 0 : Limit - 2; 715 } 716 return Limit; 717 } 718 719 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 720 unsigned Limit) { 721 if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore) 722 return false; 723 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit; 724 } 725 726 bool containsMustBreak(const AnnotatedLine *Line) { 727 for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { 728 if (Tok->MustBreakBefore) 729 return true; 730 } 731 return false; 732 } 733 734 void join(AnnotatedLine &A, const AnnotatedLine &B) { 735 assert(!A.Last->Next); 736 assert(!B.First->Previous); 737 if (B.Affected) 738 A.Affected = true; 739 A.Last->Next = B.First; 740 B.First->Previous = A.Last; 741 B.First->CanBreakBefore = true; 742 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore; 743 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) { 744 Tok->TotalLength += LengthA; 745 A.Last = Tok; 746 } 747 } 748 749 const FormatStyle &Style; 750 const AdditionalKeywords &Keywords; 751 const SmallVectorImpl<AnnotatedLine *>::const_iterator End; 752 753 SmallVectorImpl<AnnotatedLine *>::const_iterator Next; 754 const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines; 755 }; 756 757 static void markFinalized(FormatToken *Tok) { 758 for (; Tok; Tok = Tok->Next) { 759 Tok->Finalized = true; 760 for (AnnotatedLine *Child : Tok->Children) 761 markFinalized(Child->First); 762 } 763 } 764 765 #ifndef NDEBUG 766 static void printLineState(const LineState &State) { 767 llvm::dbgs() << "State: "; 768 for (const ParenState &P : State.Stack) { 769 llvm::dbgs() << (P.Tok ? P.Tok->TokenText : "F") << "|" << P.Indent << "|" 770 << P.LastSpace << "|" << P.NestedBlockIndent << " "; 771 } 772 llvm::dbgs() << State.NextToken->TokenText << "\n"; 773 } 774 #endif 775 776 /// Base class for classes that format one \c AnnotatedLine. 777 class LineFormatter { 778 public: 779 LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces, 780 const FormatStyle &Style, 781 UnwrappedLineFormatter *BlockFormatter) 782 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), 783 BlockFormatter(BlockFormatter) {} 784 virtual ~LineFormatter() {} 785 786 /// Formats an \c AnnotatedLine and returns the penalty. 787 /// 788 /// If \p DryRun is \c false, directly applies the changes. 789 virtual unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent, 790 unsigned FirstStartColumn, bool DryRun) = 0; 791 792 protected: 793 /// If the \p State's next token is an r_brace closing a nested block, 794 /// format the nested block before it. 795 /// 796 /// Returns \c true if all children could be placed successfully and adapts 797 /// \p Penalty as well as \p State. If \p DryRun is false, also directly 798 /// creates changes using \c Whitespaces. 799 /// 800 /// The crucial idea here is that children always get formatted upon 801 /// encountering the closing brace right after the nested block. Now, if we 802 /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is 803 /// \c false), the entire block has to be kept on the same line (which is only 804 /// possible if it fits on the line, only contains a single statement, etc. 805 /// 806 /// If \p NewLine is true, we format the nested block on separate lines, i.e. 807 /// break after the "{", format all lines with correct indentation and the put 808 /// the closing "}" on yet another new line. 809 /// 810 /// This enables us to keep the simple structure of the 811 /// \c UnwrappedLineFormatter, where we only have two options for each token: 812 /// break or don't break. 813 bool formatChildren(LineState &State, bool NewLine, bool DryRun, 814 unsigned &Penalty) { 815 const FormatToken *LBrace = State.NextToken->getPreviousNonComment(); 816 FormatToken &Previous = *State.NextToken->Previous; 817 if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->isNot(BK_Block) || 818 Previous.Children.size() == 0) 819 // The previous token does not open a block. Nothing to do. We don't 820 // assert so that we can simply call this function for all tokens. 821 return true; 822 823 if (NewLine) { 824 int AdditionalIndent = State.Stack.back().Indent - 825 Previous.Children[0]->Level * Style.IndentWidth; 826 827 Penalty += 828 BlockFormatter->format(Previous.Children, DryRun, AdditionalIndent, 829 /*FixBadIndentation=*/true); 830 return true; 831 } 832 833 if (Previous.Children[0]->First->MustBreakBefore) 834 return false; 835 836 // Cannot merge into one line if this line ends on a comment. 837 if (Previous.is(tok::comment)) 838 return false; 839 840 // Cannot merge multiple statements into a single line. 841 if (Previous.Children.size() > 1) 842 return false; 843 844 const AnnotatedLine *Child = Previous.Children[0]; 845 // We can't put the closing "}" on a line with a trailing comment. 846 if (Child->Last->isTrailingComment()) 847 return false; 848 849 // If the child line exceeds the column limit, we wouldn't want to merge it. 850 // We add +2 for the trailing " }". 851 if (Style.ColumnLimit > 0 && 852 Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) 853 return false; 854 855 if (!DryRun) { 856 Whitespaces->replaceWhitespace( 857 *Child->First, /*Newlines=*/0, /*Spaces=*/1, 858 /*StartOfTokenColumn=*/State.Column, /*IsAligned=*/false, 859 State.Line->InPPDirective); 860 } 861 Penalty += 862 formatLine(*Child, State.Column + 1, /*FirstStartColumn=*/0, DryRun); 863 864 State.Column += 1 + Child->Last->TotalLength; 865 return true; 866 } 867 868 ContinuationIndenter *Indenter; 869 870 private: 871 WhitespaceManager *Whitespaces; 872 const FormatStyle &Style; 873 UnwrappedLineFormatter *BlockFormatter; 874 }; 875 876 /// Formatter that keeps the existing line breaks. 877 class NoColumnLimitLineFormatter : public LineFormatter { 878 public: 879 NoColumnLimitLineFormatter(ContinuationIndenter *Indenter, 880 WhitespaceManager *Whitespaces, 881 const FormatStyle &Style, 882 UnwrappedLineFormatter *BlockFormatter) 883 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {} 884 885 /// Formats the line, simply keeping all of the input's line breaking 886 /// decisions. 887 unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent, 888 unsigned FirstStartColumn, bool DryRun) override { 889 assert(!DryRun); 890 LineState State = Indenter->getInitialState(FirstIndent, FirstStartColumn, 891 &Line, /*DryRun=*/false); 892 while (State.NextToken) { 893 bool Newline = 894 Indenter->mustBreak(State) || 895 (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); 896 unsigned Penalty = 0; 897 formatChildren(State, Newline, /*DryRun=*/false, Penalty); 898 Indenter->addTokenToState(State, Newline, /*DryRun=*/false); 899 } 900 return 0; 901 } 902 }; 903 904 /// Formatter that puts all tokens into a single line without breaks. 905 class NoLineBreakFormatter : public LineFormatter { 906 public: 907 NoLineBreakFormatter(ContinuationIndenter *Indenter, 908 WhitespaceManager *Whitespaces, const FormatStyle &Style, 909 UnwrappedLineFormatter *BlockFormatter) 910 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {} 911 912 /// Puts all tokens into a single line. 913 unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent, 914 unsigned FirstStartColumn, bool DryRun) override { 915 unsigned Penalty = 0; 916 LineState State = 917 Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun); 918 while (State.NextToken) { 919 formatChildren(State, /*NewLine=*/false, DryRun, Penalty); 920 Indenter->addTokenToState( 921 State, /*Newline=*/State.NextToken->MustBreakBefore, DryRun); 922 } 923 return Penalty; 924 } 925 }; 926 927 /// Finds the best way to break lines. 928 class OptimizingLineFormatter : public LineFormatter { 929 public: 930 OptimizingLineFormatter(ContinuationIndenter *Indenter, 931 WhitespaceManager *Whitespaces, 932 const FormatStyle &Style, 933 UnwrappedLineFormatter *BlockFormatter) 934 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {} 935 936 /// Formats the line by finding the best line breaks with line lengths 937 /// below the column limit. 938 unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent, 939 unsigned FirstStartColumn, bool DryRun) override { 940 LineState State = 941 Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun); 942 943 // If the ObjC method declaration does not fit on a line, we should format 944 // it with one arg per line. 945 if (State.Line->Type == LT_ObjCMethodDecl) 946 State.Stack.back().BreakBeforeParameter = true; 947 948 // Find best solution in solution space. 949 return analyzeSolutionSpace(State, DryRun); 950 } 951 952 private: 953 struct CompareLineStatePointers { 954 bool operator()(LineState *obj1, LineState *obj2) const { 955 return *obj1 < *obj2; 956 } 957 }; 958 959 /// A pair of <penalty, count> that is used to prioritize the BFS on. 960 /// 961 /// In case of equal penalties, we want to prefer states that were inserted 962 /// first. During state generation we make sure that we insert states first 963 /// that break the line as late as possible. 964 typedef std::pair<unsigned, unsigned> OrderedPenalty; 965 966 /// An edge in the solution space from \c Previous->State to \c State, 967 /// inserting a newline dependent on the \c NewLine. 968 struct StateNode { 969 StateNode(const LineState &State, bool NewLine, StateNode *Previous) 970 : State(State), NewLine(NewLine), Previous(Previous) {} 971 LineState State; 972 bool NewLine; 973 StateNode *Previous; 974 }; 975 976 /// An item in the prioritized BFS search queue. The \c StateNode's 977 /// \c State has the given \c OrderedPenalty. 978 typedef std::pair<OrderedPenalty, StateNode *> QueueItem; 979 980 /// The BFS queue type. 981 typedef std::priority_queue<QueueItem, std::vector<QueueItem>, 982 std::greater<QueueItem>> 983 QueueType; 984 985 /// Analyze the entire solution space starting from \p InitialState. 986 /// 987 /// This implements a variant of Dijkstra's algorithm on the graph that spans 988 /// the solution space (\c LineStates are the nodes). The algorithm tries to 989 /// find the shortest path (the one with lowest penalty) from \p InitialState 990 /// to a state where all tokens are placed. Returns the penalty. 991 /// 992 /// If \p DryRun is \c false, directly applies the changes. 993 unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun) { 994 std::set<LineState *, CompareLineStatePointers> Seen; 995 996 // Increasing count of \c StateNode items we have created. This is used to 997 // create a deterministic order independent of the container. 998 unsigned Count = 0; 999 QueueType Queue; 1000 1001 // Insert start element into queue. 1002 StateNode *Node = 1003 new (Allocator.Allocate()) StateNode(InitialState, false, nullptr); 1004 Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); 1005 ++Count; 1006 1007 unsigned Penalty = 0; 1008 1009 // While not empty, take first element and follow edges. 1010 while (!Queue.empty()) { 1011 Penalty = Queue.top().first.first; 1012 StateNode *Node = Queue.top().second; 1013 if (!Node->State.NextToken) { 1014 LLVM_DEBUG(llvm::dbgs() 1015 << "\n---\nPenalty for line: " << Penalty << "\n"); 1016 break; 1017 } 1018 Queue.pop(); 1019 1020 // Cut off the analysis of certain solutions if the analysis gets too 1021 // complex. See description of IgnoreStackForComparison. 1022 if (Count > 50000) 1023 Node->State.IgnoreStackForComparison = true; 1024 1025 if (!Seen.insert(&Node->State).second) 1026 // State already examined with lower penalty. 1027 continue; 1028 1029 FormatDecision LastFormat = Node->State.NextToken->getDecision(); 1030 if (LastFormat == FD_Unformatted || LastFormat == FD_Continue) 1031 addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue); 1032 if (LastFormat == FD_Unformatted || LastFormat == FD_Break) 1033 addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue); 1034 } 1035 1036 if (Queue.empty()) { 1037 // We were unable to find a solution, do nothing. 1038 // FIXME: Add diagnostic? 1039 LLVM_DEBUG(llvm::dbgs() << "Could not find a solution.\n"); 1040 return 0; 1041 } 1042 1043 // Reconstruct the solution. 1044 if (!DryRun) 1045 reconstructPath(InitialState, Queue.top().second); 1046 1047 LLVM_DEBUG(llvm::dbgs() 1048 << "Total number of analyzed states: " << Count << "\n"); 1049 LLVM_DEBUG(llvm::dbgs() << "---\n"); 1050 1051 return Penalty; 1052 } 1053 1054 /// Add the following state to the analysis queue \c Queue. 1055 /// 1056 /// Assume the current state is \p PreviousNode and has been reached with a 1057 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. 1058 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, 1059 bool NewLine, unsigned *Count, QueueType *Queue) { 1060 if (NewLine && !Indenter->canBreak(PreviousNode->State)) 1061 return; 1062 if (!NewLine && Indenter->mustBreak(PreviousNode->State)) 1063 return; 1064 1065 StateNode *Node = new (Allocator.Allocate()) 1066 StateNode(PreviousNode->State, NewLine, PreviousNode); 1067 if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty)) 1068 return; 1069 1070 Penalty += Indenter->addTokenToState(Node->State, NewLine, true); 1071 1072 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node)); 1073 ++(*Count); 1074 } 1075 1076 /// Applies the best formatting by reconstructing the path in the 1077 /// solution space that leads to \c Best. 1078 void reconstructPath(LineState &State, StateNode *Best) { 1079 std::deque<StateNode *> Path; 1080 // We do not need a break before the initial token. 1081 while (Best->Previous) { 1082 Path.push_front(Best); 1083 Best = Best->Previous; 1084 } 1085 for (auto I = Path.begin(), E = Path.end(); I != E; ++I) { 1086 unsigned Penalty = 0; 1087 formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty); 1088 Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false); 1089 1090 LLVM_DEBUG({ 1091 printLineState((*I)->Previous->State); 1092 if ((*I)->NewLine) { 1093 llvm::dbgs() << "Penalty for placing " 1094 << (*I)->Previous->State.NextToken->Tok.getName() 1095 << " on a new line: " << Penalty << "\n"; 1096 } 1097 }); 1098 } 1099 } 1100 1101 llvm::SpecificBumpPtrAllocator<StateNode> Allocator; 1102 }; 1103 1104 } // anonymous namespace 1105 1106 unsigned UnwrappedLineFormatter::format( 1107 const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun, 1108 int AdditionalIndent, bool FixBadIndentation, unsigned FirstStartColumn, 1109 unsigned NextStartColumn, unsigned LastStartColumn) { 1110 LineJoiner Joiner(Style, Keywords, Lines); 1111 1112 // Try to look up already computed penalty in DryRun-mode. 1113 std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey( 1114 &Lines, AdditionalIndent); 1115 auto CacheIt = PenaltyCache.find(CacheKey); 1116 if (DryRun && CacheIt != PenaltyCache.end()) 1117 return CacheIt->second; 1118 1119 assert(!Lines.empty()); 1120 unsigned Penalty = 0; 1121 LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level, 1122 AdditionalIndent); 1123 const AnnotatedLine *PreviousLine = nullptr; 1124 const AnnotatedLine *NextLine = nullptr; 1125 1126 // The minimum level of consecutive lines that have been formatted. 1127 unsigned RangeMinLevel = UINT_MAX; 1128 1129 bool FirstLine = true; 1130 for (const AnnotatedLine *Line = 1131 Joiner.getNextMergedLine(DryRun, IndentTracker); 1132 Line; Line = NextLine, FirstLine = false) { 1133 const AnnotatedLine &TheLine = *Line; 1134 unsigned Indent = IndentTracker.getIndent(); 1135 1136 // We continue formatting unchanged lines to adjust their indent, e.g. if a 1137 // scope was added. However, we need to carefully stop doing this when we 1138 // exit the scope of affected lines to prevent indenting a the entire 1139 // remaining file if it currently missing a closing brace. 1140 bool PreviousRBrace = 1141 PreviousLine && PreviousLine->startsWith(tok::r_brace); 1142 bool ContinueFormatting = 1143 TheLine.Level > RangeMinLevel || 1144 (TheLine.Level == RangeMinLevel && !PreviousRBrace && 1145 !TheLine.startsWith(tok::r_brace)); 1146 1147 bool FixIndentation = (FixBadIndentation || ContinueFormatting) && 1148 Indent != TheLine.First->OriginalColumn; 1149 bool ShouldFormat = TheLine.Affected || FixIndentation; 1150 // We cannot format this line; if the reason is that the line had a 1151 // parsing error, remember that. 1152 if (ShouldFormat && TheLine.Type == LT_Invalid && Status) { 1153 Status->FormatComplete = false; 1154 Status->Line = 1155 SourceMgr.getSpellingLineNumber(TheLine.First->Tok.getLocation()); 1156 } 1157 1158 if (ShouldFormat && TheLine.Type != LT_Invalid) { 1159 if (!DryRun) { 1160 bool LastLine = Line->First->is(tok::eof); 1161 formatFirstToken(TheLine, PreviousLine, Lines, Indent, 1162 LastLine ? LastStartColumn : NextStartColumn + Indent); 1163 } 1164 1165 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker); 1166 unsigned ColumnLimit = getColumnLimit(TheLine.InPPDirective, NextLine); 1167 bool FitsIntoOneLine = 1168 TheLine.Last->TotalLength + Indent <= ColumnLimit || 1169 (TheLine.Type == LT_ImportStatement && 1170 (Style.Language != FormatStyle::LK_JavaScript || 1171 !Style.JavaScriptWrapImports)) || 1172 (Style.isCSharp() && 1173 TheLine.InPPDirective); // don't split #regions in C# 1174 if (Style.ColumnLimit == 0) 1175 NoColumnLimitLineFormatter(Indenter, Whitespaces, Style, this) 1176 .formatLine(TheLine, NextStartColumn + Indent, 1177 FirstLine ? FirstStartColumn : 0, DryRun); 1178 else if (FitsIntoOneLine) 1179 Penalty += NoLineBreakFormatter(Indenter, Whitespaces, Style, this) 1180 .formatLine(TheLine, NextStartColumn + Indent, 1181 FirstLine ? FirstStartColumn : 0, DryRun); 1182 else 1183 Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this) 1184 .formatLine(TheLine, NextStartColumn + Indent, 1185 FirstLine ? FirstStartColumn : 0, DryRun); 1186 RangeMinLevel = std::min(RangeMinLevel, TheLine.Level); 1187 } else { 1188 // If no token in the current line is affected, we still need to format 1189 // affected children. 1190 if (TheLine.ChildrenAffected) 1191 for (const FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) 1192 if (!Tok->Children.empty()) 1193 format(Tok->Children, DryRun); 1194 1195 // Adapt following lines on the current indent level to the same level 1196 // unless the current \c AnnotatedLine is not at the beginning of a line. 1197 bool StartsNewLine = 1198 TheLine.First->NewlinesBefore > 0 || TheLine.First->IsFirst; 1199 if (StartsNewLine) 1200 IndentTracker.adjustToUnmodifiedLine(TheLine); 1201 if (!DryRun) { 1202 bool ReformatLeadingWhitespace = 1203 StartsNewLine && ((PreviousLine && PreviousLine->Affected) || 1204 TheLine.LeadingEmptyLinesAffected); 1205 // Format the first token. 1206 if (ReformatLeadingWhitespace) 1207 formatFirstToken(TheLine, PreviousLine, Lines, 1208 TheLine.First->OriginalColumn, 1209 TheLine.First->OriginalColumn); 1210 else 1211 Whitespaces->addUntouchableToken(*TheLine.First, 1212 TheLine.InPPDirective); 1213 1214 // Notify the WhitespaceManager about the unchanged whitespace. 1215 for (FormatToken *Tok = TheLine.First->Next; Tok; Tok = Tok->Next) 1216 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective); 1217 } 1218 NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker); 1219 RangeMinLevel = UINT_MAX; 1220 } 1221 if (!DryRun) 1222 markFinalized(TheLine.First); 1223 PreviousLine = &TheLine; 1224 } 1225 PenaltyCache[CacheKey] = Penalty; 1226 return Penalty; 1227 } 1228 1229 void UnwrappedLineFormatter::formatFirstToken( 1230 const AnnotatedLine &Line, const AnnotatedLine *PreviousLine, 1231 const SmallVectorImpl<AnnotatedLine *> &Lines, unsigned Indent, 1232 unsigned NewlineIndent) { 1233 FormatToken &RootToken = *Line.First; 1234 if (RootToken.is(tok::eof)) { 1235 unsigned Newlines = std::min(RootToken.NewlinesBefore, 1u); 1236 unsigned TokenIndent = Newlines ? NewlineIndent : 0; 1237 Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent, 1238 TokenIndent); 1239 return; 1240 } 1241 unsigned Newlines = 1242 std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); 1243 // Remove empty lines before "}" where applicable. 1244 if (RootToken.is(tok::r_brace) && 1245 (!RootToken.Next || 1246 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) && 1247 // Do not remove empty lines before namespace closing "}". 1248 !getNamespaceToken(&Line, Lines)) 1249 Newlines = std::min(Newlines, 1u); 1250 // Remove empty lines at the start of nested blocks (lambdas/arrow functions) 1251 if (PreviousLine == nullptr && Line.Level > 0) 1252 Newlines = std::min(Newlines, 1u); 1253 if (Newlines == 0 && !RootToken.IsFirst) 1254 Newlines = 1; 1255 if (RootToken.IsFirst && !RootToken.HasUnescapedNewline) 1256 Newlines = 0; 1257 1258 // Remove empty lines after "{". 1259 if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine && 1260 PreviousLine->Last->is(tok::l_brace) && 1261 !PreviousLine->startsWithNamespace() && 1262 !startsExternCBlock(*PreviousLine)) 1263 Newlines = 1; 1264 1265 // Insert or remove empty line before access specifiers. 1266 if (PreviousLine && RootToken.isAccessSpecifier()) { 1267 switch (Style.EmptyLineBeforeAccessModifier) { 1268 case FormatStyle::ELBAMS_Never: 1269 if (Newlines > 1) 1270 Newlines = 1; 1271 break; 1272 case FormatStyle::ELBAMS_Leave: 1273 Newlines = std::max(RootToken.NewlinesBefore, 1u); 1274 break; 1275 case FormatStyle::ELBAMS_LogicalBlock: 1276 if (PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && Newlines <= 1) 1277 Newlines = 2; 1278 if (PreviousLine->First->isAccessSpecifier()) 1279 Newlines = 1; // Previous is an access modifier remove all new lines. 1280 break; 1281 case FormatStyle::ELBAMS_Always: { 1282 const FormatToken *previousToken; 1283 if (PreviousLine->Last->is(tok::comment)) 1284 previousToken = PreviousLine->Last->getPreviousNonComment(); 1285 else 1286 previousToken = PreviousLine->Last; 1287 if ((!previousToken || !previousToken->is(tok::l_brace)) && Newlines <= 1) 1288 Newlines = 2; 1289 } break; 1290 } 1291 } 1292 1293 // Insert or remove empty line after access specifiers. 1294 if (PreviousLine && PreviousLine->First->isAccessSpecifier() && 1295 (!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline)) { 1296 // EmptyLineBeforeAccessModifier is handling the case when two access 1297 // modifiers follow each other. 1298 if (!RootToken.isAccessSpecifier()) { 1299 switch (Style.EmptyLineAfterAccessModifier) { 1300 case FormatStyle::ELAAMS_Never: 1301 Newlines = 1; 1302 break; 1303 case FormatStyle::ELAAMS_Leave: 1304 Newlines = std::max(Newlines, 1u); 1305 break; 1306 case FormatStyle::ELAAMS_Always: 1307 if (RootToken.is(tok::r_brace)) // Do not add at end of class. 1308 Newlines = 1u; 1309 else 1310 Newlines = std::max(Newlines, 2u); 1311 break; 1312 } 1313 } 1314 } 1315 1316 if (Newlines) 1317 Indent = NewlineIndent; 1318 1319 // Preprocessor directives get indented before the hash only if specified 1320 if (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && 1321 (Line.Type == LT_PreprocessorDirective || 1322 Line.Type == LT_ImportStatement)) 1323 Indent = 0; 1324 1325 Whitespaces->replaceWhitespace(RootToken, Newlines, Indent, Indent, 1326 /*IsAligned=*/false, 1327 Line.InPPDirective && 1328 !RootToken.HasUnescapedNewline); 1329 } 1330 1331 unsigned 1332 UnwrappedLineFormatter::getColumnLimit(bool InPPDirective, 1333 const AnnotatedLine *NextLine) const { 1334 // In preprocessor directives reserve two chars for trailing " \" if the 1335 // next line continues the preprocessor directive. 1336 bool ContinuesPPDirective = 1337 InPPDirective && 1338 // If there is no next line, this is likely a child line and the parent 1339 // continues the preprocessor directive. 1340 (!NextLine || 1341 (NextLine->InPPDirective && 1342 // If there is an unescaped newline between this line and the next, the 1343 // next line starts a new preprocessor directive. 1344 !NextLine->First->HasUnescapedNewline)); 1345 return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0); 1346 } 1347 1348 } // namespace format 1349 } // namespace clang 1350