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