UnwrappedLineFormatter.cpp revision 327952
1277325Sdim//===--- UnwrappedLineFormatter.cpp - Format C++ code ---------------------===// 2277325Sdim// 3277325Sdim// The LLVM Compiler Infrastructure 4277325Sdim// 5277325Sdim// This file is distributed under the University of Illinois Open Source 6277325Sdim// License. See LICENSE.TXT for details. 7277325Sdim// 8277325Sdim//===----------------------------------------------------------------------===// 9277325Sdim 10277325Sdim#include "UnwrappedLineFormatter.h" 11277325Sdim#include "WhitespaceManager.h" 12277325Sdim#include "llvm/Support/Debug.h" 13314564Sdim#include <queue> 14277325Sdim 15277325Sdim#define DEBUG_TYPE "format-formatter" 16277325Sdim 17277325Sdimnamespace clang { 18277325Sdimnamespace format { 19277325Sdim 20277325Sdimnamespace { 21277325Sdim 22277325Sdimbool startsExternCBlock(const AnnotatedLine &Line) { 23277325Sdim const FormatToken *Next = Line.First->getNextNonComment(); 24277325Sdim const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr; 25288943Sdim return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() && 26277325Sdim NextNext && NextNext->is(tok::l_brace); 27277325Sdim} 28277325Sdim 29288943Sdim/// \brief Tracks the indent level of \c AnnotatedLines across levels. 30288943Sdim/// 31288943Sdim/// \c nextLine must be called for each \c AnnotatedLine, after which \c 32288943Sdim/// getIndent() will return the indent for the last line \c nextLine was called 33288943Sdim/// with. 34288943Sdim/// If the line is not formatted (and thus the indent does not change), calling 35288943Sdim/// \c adjustToUnmodifiedLine after the call to \c nextLine will cause 36288943Sdim/// subsequent lines on the same level to be indented at the same level as the 37288943Sdim/// given line. 38288943Sdimclass LevelIndentTracker { 39288943Sdimpublic: 40288943Sdim LevelIndentTracker(const FormatStyle &Style, 41288943Sdim const AdditionalKeywords &Keywords, unsigned StartLevel, 42288943Sdim int AdditionalIndent) 43288943Sdim : Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) { 44288943Sdim for (unsigned i = 0; i != StartLevel; ++i) 45288943Sdim IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent); 46288943Sdim } 47288943Sdim 48288943Sdim /// \brief Returns the indent for the current line. 49288943Sdim unsigned getIndent() const { return Indent; } 50288943Sdim 51288943Sdim /// \brief Update the indent state given that \p Line is going to be formatted 52288943Sdim /// next. 53288943Sdim void nextLine(const AnnotatedLine &Line) { 54288943Sdim Offset = getIndentOffset(*Line.First); 55288943Sdim // Update the indent level cache size so that we can rely on it 56288943Sdim // having the right size in adjustToUnmodifiedline. 57288943Sdim while (IndentForLevel.size() <= Line.Level) 58288943Sdim IndentForLevel.push_back(-1); 59288943Sdim if (Line.InPPDirective) { 60288943Sdim Indent = Line.Level * Style.IndentWidth + AdditionalIndent; 61288943Sdim } else { 62288943Sdim IndentForLevel.resize(Line.Level + 1); 63288943Sdim Indent = getIndent(IndentForLevel, Line.Level); 64288943Sdim } 65288943Sdim if (static_cast<int>(Indent) + Offset >= 0) 66288943Sdim Indent += Offset; 67288943Sdim } 68288943Sdim 69321369Sdim /// \brief Update the indent state given that \p Line indent should be 70321369Sdim /// skipped. 71321369Sdim void skipLine(const AnnotatedLine &Line) { 72321369Sdim while (IndentForLevel.size() <= Line.Level) 73321369Sdim IndentForLevel.push_back(Indent); 74321369Sdim } 75321369Sdim 76288943Sdim /// \brief Update the level indent to adapt to the given \p Line. 77288943Sdim /// 78288943Sdim /// When a line is not formatted, we move the subsequent lines on the same 79288943Sdim /// level to the same indent. 80288943Sdim /// Note that \c nextLine must have been called before this method. 81288943Sdim void adjustToUnmodifiedLine(const AnnotatedLine &Line) { 82288943Sdim unsigned LevelIndent = Line.First->OriginalColumn; 83288943Sdim if (static_cast<int>(LevelIndent) - Offset >= 0) 84288943Sdim LevelIndent -= Offset; 85288943Sdim if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) && 86288943Sdim !Line.InPPDirective) 87288943Sdim IndentForLevel[Line.Level] = LevelIndent; 88288943Sdim } 89288943Sdim 90288943Sdimprivate: 91288943Sdim /// \brief Get the offset of the line relatively to the level. 92288943Sdim /// 93288943Sdim /// For example, 'public:' labels in classes are offset by 1 or 2 94288943Sdim /// characters to the left from their level. 95288943Sdim int getIndentOffset(const FormatToken &RootToken) { 96288943Sdim if (Style.Language == FormatStyle::LK_Java || 97288943Sdim Style.Language == FormatStyle::LK_JavaScript) 98288943Sdim return 0; 99288943Sdim if (RootToken.isAccessSpecifier(false) || 100288943Sdim RootToken.isObjCAccessSpecifier() || 101296417Sdim (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) && 102296417Sdim RootToken.Next && RootToken.Next->is(tok::colon))) 103288943Sdim return Style.AccessModifierOffset; 104288943Sdim return 0; 105288943Sdim } 106288943Sdim 107288943Sdim /// \brief Get the indent of \p Level from \p IndentForLevel. 108288943Sdim /// 109288943Sdim /// \p IndentForLevel must contain the indent for the level \c l 110288943Sdim /// at \p IndentForLevel[l], or a value < 0 if the indent for 111288943Sdim /// that level is unknown. 112288943Sdim unsigned getIndent(ArrayRef<int> IndentForLevel, unsigned Level) { 113288943Sdim if (IndentForLevel[Level] != -1) 114288943Sdim return IndentForLevel[Level]; 115288943Sdim if (Level == 0) 116288943Sdim return 0; 117288943Sdim return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; 118288943Sdim } 119288943Sdim 120288943Sdim const FormatStyle &Style; 121288943Sdim const AdditionalKeywords &Keywords; 122288943Sdim const unsigned AdditionalIndent; 123288943Sdim 124288943Sdim /// \brief The indent in characters for each level. 125288943Sdim std::vector<int> IndentForLevel; 126288943Sdim 127288943Sdim /// \brief Offset of the current line relative to the indent level. 128288943Sdim /// 129288943Sdim /// For example, the 'public' keywords is often indented with a negative 130288943Sdim /// offset. 131288943Sdim int Offset = 0; 132288943Sdim 133288943Sdim /// \brief The current line's indent. 134288943Sdim unsigned Indent = 0; 135288943Sdim}; 136288943Sdim 137321369Sdimbool isNamespaceDeclaration(const AnnotatedLine *Line) { 138321369Sdim const FormatToken *NamespaceTok = Line->First; 139321369Sdim return NamespaceTok && NamespaceTok->getNamespaceToken(); 140321369Sdim} 141321369Sdim 142321369Sdimbool isEndOfNamespace(const AnnotatedLine *Line, 143321369Sdim const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) { 144321369Sdim if (!Line->startsWith(tok::r_brace)) 145321369Sdim return false; 146321369Sdim size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex; 147321369Sdim if (StartLineIndex == UnwrappedLine::kInvalidIndex) 148321369Sdim return false; 149321369Sdim assert(StartLineIndex < AnnotatedLines.size()); 150321369Sdim return isNamespaceDeclaration(AnnotatedLines[StartLineIndex]); 151321369Sdim} 152321369Sdim 153277325Sdimclass LineJoiner { 154277325Sdimpublic: 155288943Sdim LineJoiner(const FormatStyle &Style, const AdditionalKeywords &Keywords, 156288943Sdim const SmallVectorImpl<AnnotatedLine *> &Lines) 157321369Sdim : Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()), 158321369Sdim AnnotatedLines(Lines) {} 159277325Sdim 160288943Sdim /// \brief Returns the next line, merging multiple lines into one if possible. 161288943Sdim const AnnotatedLine *getNextMergedLine(bool DryRun, 162288943Sdim LevelIndentTracker &IndentTracker) { 163288943Sdim if (Next == End) 164288943Sdim return nullptr; 165288943Sdim const AnnotatedLine *Current = *Next; 166288943Sdim IndentTracker.nextLine(*Current); 167327952Sdim unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End); 168288943Sdim if (MergedLines > 0 && Style.ColumnLimit == 0) 169288943Sdim // Disallow line merging if there is a break at the start of one of the 170288943Sdim // input lines. 171288943Sdim for (unsigned i = 0; i < MergedLines; ++i) 172288943Sdim if (Next[i + 1]->First->NewlinesBefore > 0) 173288943Sdim MergedLines = 0; 174288943Sdim if (!DryRun) 175288943Sdim for (unsigned i = 0; i < MergedLines; ++i) 176314564Sdim join(*Next[0], *Next[i + 1]); 177288943Sdim Next = Next + MergedLines + 1; 178288943Sdim return Current; 179288943Sdim } 180288943Sdim 181288943Sdimprivate: 182277325Sdim /// \brief Calculates how many lines can be merged into 1 starting at \p I. 183277325Sdim unsigned 184321369Sdim tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker, 185277325Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator I, 186277325Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator E) { 187321369Sdim const unsigned Indent = IndentTracker.getIndent(); 188321369Sdim 189288943Sdim // Can't join the last line with anything. 190288943Sdim if (I + 1 == E) 191288943Sdim return 0; 192277325Sdim // We can never merge stuff if there are trailing line comments. 193277325Sdim const AnnotatedLine *TheLine = *I; 194277325Sdim if (TheLine->Last->is(TT_LineComment)) 195277325Sdim return 0; 196288943Sdim if (I[1]->Type == LT_Invalid || I[1]->First->MustBreakBefore) 197288943Sdim return 0; 198288943Sdim if (TheLine->InPPDirective && 199288943Sdim (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline)) 200288943Sdim return 0; 201277325Sdim 202277325Sdim if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit) 203277325Sdim return 0; 204277325Sdim 205277325Sdim unsigned Limit = 206277325Sdim Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent; 207277325Sdim // If we already exceed the column limit, we set 'Limit' to 0. The different 208277325Sdim // tryMerge..() functions can then decide whether to still do merging. 209277325Sdim Limit = TheLine->Last->TotalLength > Limit 210277325Sdim ? 0 211277325Sdim : Limit - TheLine->Last->TotalLength; 212277325Sdim 213321369Sdim if (TheLine->Last->is(TT_FunctionLBrace) && 214321369Sdim TheLine->First == TheLine->Last && 215321369Sdim !Style.BraceWrapping.SplitEmptyFunction && 216321369Sdim I[1]->First->is(tok::r_brace)) 217321369Sdim return tryMergeSimpleBlock(I, E, Limit); 218321369Sdim 219321369Sdim // Handle empty record blocks where the brace has already been wrapped 220321369Sdim if (TheLine->Last->is(tok::l_brace) && TheLine->First == TheLine->Last && 221321369Sdim I != AnnotatedLines.begin()) { 222321369Sdim bool EmptyBlock = I[1]->First->is(tok::r_brace); 223321369Sdim 224321369Sdim const FormatToken *Tok = I[-1]->First; 225321369Sdim if (Tok && Tok->is(tok::comment)) 226321369Sdim Tok = Tok->getNextNonComment(); 227321369Sdim 228321369Sdim if (Tok && Tok->getNamespaceToken()) 229321369Sdim return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock 230327952Sdim ? tryMergeSimpleBlock(I, E, Limit) 231327952Sdim : 0; 232321369Sdim 233321369Sdim if (Tok && Tok->is(tok::kw_typedef)) 234321369Sdim Tok = Tok->getNextNonComment(); 235321369Sdim if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union, 236327952Sdim tok::kw_extern, Keywords.kw_interface)) 237321369Sdim return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock 238327952Sdim ? tryMergeSimpleBlock(I, E, Limit) 239327952Sdim : 0; 240321369Sdim } 241321369Sdim 242277325Sdim // FIXME: TheLine->Level != 0 might or might not be the right check to do. 243277325Sdim // If necessary, change to something smarter. 244277325Sdim bool MergeShortFunctions = 245277325Sdim Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All || 246288943Sdim (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty && 247277325Sdim I[1]->First->is(tok::r_brace)) || 248321369Sdim (Style.AllowShortFunctionsOnASingleLine & FormatStyle::SFS_InlineOnly && 249277325Sdim TheLine->Level != 0); 250277325Sdim 251321369Sdim if (Style.CompactNamespaces) { 252321369Sdim if (isNamespaceDeclaration(TheLine)) { 253321369Sdim int i = 0; 254321369Sdim unsigned closingLine = TheLine->MatchingOpeningBlockLineIndex - 1; 255321369Sdim for (; I + 1 + i != E && isNamespaceDeclaration(I[i + 1]) && 256321369Sdim closingLine == I[i + 1]->MatchingOpeningBlockLineIndex && 257321369Sdim I[i + 1]->Last->TotalLength < Limit; 258321369Sdim i++, closingLine--) { 259321369Sdim // No extra indent for compacted namespaces 260321369Sdim IndentTracker.skipLine(*I[i + 1]); 261321369Sdim 262321369Sdim Limit -= I[i + 1]->Last->TotalLength; 263321369Sdim } 264321369Sdim return i; 265321369Sdim } 266321369Sdim 267321369Sdim if (isEndOfNamespace(TheLine, AnnotatedLines)) { 268321369Sdim int i = 0; 269321369Sdim unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1; 270321369Sdim for (; I + 1 + i != E && isEndOfNamespace(I[i + 1], AnnotatedLines) && 271321369Sdim openingLine == I[i + 1]->MatchingOpeningBlockLineIndex; 272321369Sdim i++, openingLine--) { 273321369Sdim // No space between consecutive braces 274321369Sdim I[i + 1]->First->SpacesRequiredBefore = !I[i]->Last->is(tok::r_brace); 275321369Sdim 276321369Sdim // Indent like the outer-most namespace 277321369Sdim IndentTracker.nextLine(*I[i + 1]); 278321369Sdim } 279321369Sdim return i; 280321369Sdim } 281321369Sdim } 282321369Sdim 283327952Sdim // Try to merge a function block with left brace unwrapped 284277325Sdim if (TheLine->Last->is(TT_FunctionLBrace) && 285277325Sdim TheLine->First != TheLine->Last) { 286277325Sdim return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0; 287277325Sdim } 288327952Sdim // Try to merge a control statement block with left brace unwrapped 289327952Sdim if (TheLine->Last->is(tok::l_brace) && TheLine->First != TheLine->Last && 290327952Sdim TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) { 291327952Sdim return Style.AllowShortBlocksOnASingleLine 292327952Sdim ? tryMergeSimpleBlock(I, E, Limit) 293327952Sdim : 0; 294327952Sdim } 295327952Sdim // Try to merge a control statement block with left brace wrapped 296327952Sdim if (I[1]->First->is(tok::l_brace) && 297327952Sdim TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) { 298327952Sdim return Style.BraceWrapping.AfterControlStatement 299327952Sdim ? tryMergeSimpleBlock(I, E, Limit) 300327952Sdim : 0; 301327952Sdim } 302327952Sdim // Try to merge either empty or one-line block if is precedeed by control 303327952Sdim // statement token 304327952Sdim if (TheLine->First->is(tok::l_brace) && TheLine->First == TheLine->Last && 305327952Sdim I != AnnotatedLines.begin() && 306327952Sdim I[-1]->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) { 307327952Sdim return Style.AllowShortBlocksOnASingleLine 308327952Sdim ? tryMergeSimpleBlock(I - 1, E, Limit) 309327952Sdim : 0; 310327952Sdim } 311327952Sdim // Try to merge a block with left brace wrapped that wasn't yet covered 312277325Sdim if (TheLine->Last->is(tok::l_brace)) { 313327952Sdim return !Style.BraceWrapping.AfterFunction || 314327952Sdim (I[1]->First->is(tok::r_brace) && 315327952Sdim !Style.BraceWrapping.SplitEmptyRecord) 316277325Sdim ? tryMergeSimpleBlock(I, E, Limit) 317277325Sdim : 0; 318277325Sdim } 319327952Sdim // Try to merge a function block with left brace wrapped 320277325Sdim if (I[1]->First->is(TT_FunctionLBrace) && 321296417Sdim Style.BraceWrapping.AfterFunction) { 322277325Sdim if (I[1]->Last->is(TT_LineComment)) 323277325Sdim return 0; 324277325Sdim 325277325Sdim // Check for Limit <= 2 to account for the " {". 326277325Sdim if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine))) 327277325Sdim return 0; 328277325Sdim Limit -= 2; 329277325Sdim 330277325Sdim unsigned MergedLines = 0; 331321369Sdim if (MergeShortFunctions || 332321369Sdim (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty && 333321369Sdim I[1]->First == I[1]->Last && I + 2 != E && 334321369Sdim I[2]->First->is(tok::r_brace))) { 335277325Sdim MergedLines = tryMergeSimpleBlock(I + 1, E, Limit); 336277325Sdim // If we managed to merge the block, count the function header, which is 337277325Sdim // on a separate line. 338277325Sdim if (MergedLines > 0) 339277325Sdim ++MergedLines; 340277325Sdim } 341277325Sdim return MergedLines; 342277325Sdim } 343277325Sdim if (TheLine->First->is(tok::kw_if)) { 344277325Sdim return Style.AllowShortIfStatementsOnASingleLine 345277325Sdim ? tryMergeSimpleControlStatement(I, E, Limit) 346277325Sdim : 0; 347277325Sdim } 348277325Sdim if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) { 349277325Sdim return Style.AllowShortLoopsOnASingleLine 350277325Sdim ? tryMergeSimpleControlStatement(I, E, Limit) 351277325Sdim : 0; 352277325Sdim } 353277325Sdim if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) { 354277325Sdim return Style.AllowShortCaseLabelsOnASingleLine 355277325Sdim ? tryMergeShortCaseLabels(I, E, Limit) 356277325Sdim : 0; 357277325Sdim } 358277325Sdim if (TheLine->InPPDirective && 359277325Sdim (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) { 360277325Sdim return tryMergeSimplePPDirective(I, E, Limit); 361277325Sdim } 362277325Sdim return 0; 363277325Sdim } 364277325Sdim 365277325Sdim unsigned 366277325Sdim tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 367277325Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator E, 368277325Sdim unsigned Limit) { 369277325Sdim if (Limit == 0) 370277325Sdim return 0; 371277325Sdim if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline) 372277325Sdim return 0; 373277325Sdim if (1 + I[1]->Last->TotalLength > Limit) 374277325Sdim return 0; 375277325Sdim return 1; 376277325Sdim } 377277325Sdim 378277325Sdim unsigned tryMergeSimpleControlStatement( 379277325Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator I, 380277325Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) { 381277325Sdim if (Limit == 0) 382277325Sdim return 0; 383296417Sdim if (Style.BraceWrapping.AfterControlStatement && 384277325Sdim (I[1]->First->is(tok::l_brace) && !Style.AllowShortBlocksOnASingleLine)) 385277325Sdim return 0; 386277325Sdim if (I[1]->InPPDirective != (*I)->InPPDirective || 387277325Sdim (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) 388277325Sdim return 0; 389277325Sdim Limit = limitConsideringMacros(I + 1, E, Limit); 390277325Sdim AnnotatedLine &Line = **I; 391277325Sdim if (Line.Last->isNot(tok::r_paren)) 392277325Sdim return 0; 393277325Sdim if (1 + I[1]->Last->TotalLength > Limit) 394277325Sdim return 0; 395288943Sdim if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while, 396288943Sdim TT_LineComment)) 397277325Sdim return 0; 398277325Sdim // Only inline simple if's (no nested if or else). 399288943Sdim if (I + 2 != E && Line.startsWith(tok::kw_if) && 400277325Sdim I[2]->First->is(tok::kw_else)) 401277325Sdim return 0; 402277325Sdim return 1; 403277325Sdim } 404277325Sdim 405288943Sdim unsigned 406288943Sdim tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 407288943Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator E, 408288943Sdim unsigned Limit) { 409277325Sdim if (Limit == 0 || I + 1 == E || 410277325Sdim I[1]->First->isOneOf(tok::kw_case, tok::kw_default)) 411277325Sdim return 0; 412277325Sdim unsigned NumStmts = 0; 413277325Sdim unsigned Length = 0; 414327952Sdim bool EndsWithComment = false; 415277325Sdim bool InPPDirective = I[0]->InPPDirective; 416327952Sdim const unsigned Level = I[0]->Level; 417277325Sdim for (; NumStmts < 3; ++NumStmts) { 418277325Sdim if (I + 1 + NumStmts == E) 419277325Sdim break; 420277325Sdim const AnnotatedLine *Line = I[1 + NumStmts]; 421277325Sdim if (Line->InPPDirective != InPPDirective) 422277325Sdim break; 423277325Sdim if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace)) 424277325Sdim break; 425277325Sdim if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch, 426327952Sdim tok::kw_while) || 427327952Sdim EndsWithComment) 428277325Sdim return 0; 429327952Sdim if (Line->First->is(tok::comment)) { 430327952Sdim if (Level != Line->Level) 431327952Sdim return 0; 432327952Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts; 433327952Sdim for (; J != E; ++J) { 434327952Sdim Line = *J; 435327952Sdim if (Line->InPPDirective != InPPDirective) 436327952Sdim break; 437327952Sdim if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace)) 438327952Sdim break; 439327952Sdim if (Line->First->isNot(tok::comment) || Level != Line->Level) 440327952Sdim return 0; 441327952Sdim } 442327952Sdim break; 443327952Sdim } 444327952Sdim if (Line->Last->is(tok::comment)) 445327952Sdim EndsWithComment = true; 446277325Sdim Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space. 447277325Sdim } 448277325Sdim if (NumStmts == 0 || NumStmts == 3 || Length > Limit) 449277325Sdim return 0; 450277325Sdim return NumStmts; 451277325Sdim } 452277325Sdim 453277325Sdim unsigned 454277325Sdim tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 455277325Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator E, 456277325Sdim unsigned Limit) { 457277325Sdim AnnotatedLine &Line = **I; 458277325Sdim 459277325Sdim // Don't merge ObjC @ keywords and methods. 460288943Sdim // FIXME: If an option to allow short exception handling clauses on a single 461288943Sdim // line is added, change this to not return for @try and friends. 462277325Sdim if (Style.Language != FormatStyle::LK_Java && 463277325Sdim Line.First->isOneOf(tok::at, tok::minus, tok::plus)) 464277325Sdim return 0; 465277325Sdim 466277325Sdim // Check that the current line allows merging. This depends on whether we 467277325Sdim // are in a control flow statements as well as several style flags. 468288943Sdim if (Line.First->isOneOf(tok::kw_else, tok::kw_case) || 469288943Sdim (Line.First->Next && Line.First->Next->is(tok::kw_else))) 470277325Sdim return 0; 471277325Sdim if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try, 472288943Sdim tok::kw___try, tok::kw_catch, tok::kw___finally, 473288943Sdim tok::kw_for, tok::r_brace, Keywords.kw___except)) { 474277325Sdim if (!Style.AllowShortBlocksOnASingleLine) 475277325Sdim return 0; 476327952Sdim // Don't merge when we can't except the case when 477327952Sdim // the control statement block is empty 478277325Sdim if (!Style.AllowShortIfStatementsOnASingleLine && 479327952Sdim Line.startsWith(tok::kw_if) && 480327952Sdim !Style.BraceWrapping.AfterControlStatement && 481327952Sdim !I[1]->First->is(tok::r_brace)) 482277325Sdim return 0; 483327952Sdim if (!Style.AllowShortIfStatementsOnASingleLine && 484327952Sdim Line.startsWith(tok::kw_if) && 485327952Sdim Style.BraceWrapping.AfterControlStatement && I + 2 != E && 486327952Sdim !I[2]->First->is(tok::r_brace)) 487327952Sdim return 0; 488277325Sdim if (!Style.AllowShortLoopsOnASingleLine && 489327952Sdim Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) && 490327952Sdim !Style.BraceWrapping.AfterControlStatement && 491327952Sdim !I[1]->First->is(tok::r_brace)) 492277325Sdim return 0; 493327952Sdim if (!Style.AllowShortLoopsOnASingleLine && 494327952Sdim Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) && 495327952Sdim Style.BraceWrapping.AfterControlStatement && I + 2 != E && 496327952Sdim !I[2]->First->is(tok::r_brace)) 497327952Sdim return 0; 498277325Sdim // FIXME: Consider an option to allow short exception handling clauses on 499277325Sdim // a single line. 500288943Sdim // FIXME: This isn't covered by tests. 501288943Sdim // FIXME: For catch, __except, __finally the first token on the line 502288943Sdim // is '}', so this isn't correct here. 503288943Sdim if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch, 504288943Sdim Keywords.kw___except, tok::kw___finally)) 505277325Sdim return 0; 506277325Sdim } 507277325Sdim 508327952Sdim if (Line.Last->is(tok::l_brace)) { 509327952Sdim FormatToken *Tok = I[1]->First; 510327952Sdim if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore && 511327952Sdim (Tok->getNextNonComment() == nullptr || 512327952Sdim Tok->getNextNonComment()->is(tok::semi))) { 513327952Sdim // We merge empty blocks even if the line exceeds the column limit. 514327952Sdim Tok->SpacesRequiredBefore = 0; 515327952Sdim Tok->CanBreakBefore = true; 516327952Sdim return 1; 517327952Sdim } else if (Limit != 0 && !Line.startsWith(tok::kw_namespace) && 518327952Sdim !startsExternCBlock(Line)) { 519327952Sdim // We don't merge short records. 520327952Sdim FormatToken *RecordTok = Line.First; 521327952Sdim // Skip record modifiers. 522327952Sdim while (RecordTok->Next && 523327952Sdim RecordTok->isOneOf(tok::kw_typedef, tok::kw_export, 524327952Sdim Keywords.kw_declare, Keywords.kw_abstract, 525327952Sdim tok::kw_default)) 526327952Sdim RecordTok = RecordTok->Next; 527327952Sdim if (RecordTok && 528327952Sdim RecordTok->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct, 529327952Sdim Keywords.kw_interface)) 530327952Sdim return 0; 531277325Sdim 532327952Sdim // Check that we still have three lines and they fit into the limit. 533327952Sdim if (I + 2 == E || I[2]->Type == LT_Invalid) 534327952Sdim return 0; 535327952Sdim Limit = limitConsideringMacros(I + 2, E, Limit); 536277325Sdim 537327952Sdim if (!nextTwoLinesFitInto(I, Limit)) 538327952Sdim return 0; 539277325Sdim 540327952Sdim // Second, check that the next line does not contain any braces - if it 541327952Sdim // does, readability declines when putting it into a single line. 542327952Sdim if (I[1]->Last->is(TT_LineComment)) 543277325Sdim return 0; 544327952Sdim do { 545327952Sdim if (Tok->is(tok::l_brace) && Tok->BlockKind != BK_BracedInit) 546327952Sdim return 0; 547327952Sdim Tok = Tok->Next; 548327952Sdim } while (Tok); 549277325Sdim 550327952Sdim // Last, check that the third line starts with a closing brace. 551327952Sdim Tok = I[2]->First; 552327952Sdim if (Tok->isNot(tok::r_brace)) 553327952Sdim return 0; 554327952Sdim 555327952Sdim // Don't merge "if (a) { .. } else {". 556327952Sdim if (Tok->Next && Tok->Next->is(tok::kw_else)) 557327952Sdim return 0; 558327952Sdim 559327952Sdim return 2; 560327952Sdim } 561327952Sdim } else if (I[1]->First->is(tok::l_brace)) { 562327952Sdim if (I[1]->Last->is(TT_LineComment)) 563277325Sdim return 0; 564277325Sdim 565327952Sdim // Check for Limit <= 2 to account for the " {". 566327952Sdim if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I))) 567288943Sdim return 0; 568327952Sdim Limit -= 2; 569327952Sdim unsigned MergedLines = 0; 570327952Sdim if (Style.AllowShortBlocksOnASingleLine || 571327952Sdim (I[1]->First == I[1]->Last && I + 2 != E && 572327952Sdim I[2]->First->is(tok::r_brace))) { 573327952Sdim MergedLines = tryMergeSimpleBlock(I + 1, E, Limit); 574327952Sdim // If we managed to merge the block, count the statement header, which 575327952Sdim // is on a separate line. 576327952Sdim if (MergedLines > 0) 577327952Sdim ++MergedLines; 578327952Sdim } 579327952Sdim return MergedLines; 580277325Sdim } 581277325Sdim return 0; 582277325Sdim } 583277325Sdim 584277325Sdim /// Returns the modified column limit for \p I if it is inside a macro and 585277325Sdim /// needs a trailing '\'. 586277325Sdim unsigned 587277325Sdim limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 588277325Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator E, 589277325Sdim unsigned Limit) { 590277325Sdim if (I[0]->InPPDirective && I + 1 != E && 591277325Sdim !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) { 592277325Sdim return Limit < 2 ? 0 : Limit - 2; 593277325Sdim } 594277325Sdim return Limit; 595277325Sdim } 596277325Sdim 597277325Sdim bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I, 598277325Sdim unsigned Limit) { 599277325Sdim if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore) 600277325Sdim return false; 601277325Sdim return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit; 602277325Sdim } 603277325Sdim 604277325Sdim bool containsMustBreak(const AnnotatedLine *Line) { 605277325Sdim for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) { 606277325Sdim if (Tok->MustBreakBefore) 607277325Sdim return true; 608277325Sdim } 609277325Sdim return false; 610277325Sdim } 611277325Sdim 612288943Sdim void join(AnnotatedLine &A, const AnnotatedLine &B) { 613288943Sdim assert(!A.Last->Next); 614288943Sdim assert(!B.First->Previous); 615288943Sdim if (B.Affected) 616288943Sdim A.Affected = true; 617288943Sdim A.Last->Next = B.First; 618288943Sdim B.First->Previous = A.Last; 619288943Sdim B.First->CanBreakBefore = true; 620288943Sdim unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore; 621288943Sdim for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) { 622288943Sdim Tok->TotalLength += LengthA; 623288943Sdim A.Last = Tok; 624288943Sdim } 625288943Sdim } 626288943Sdim 627277325Sdim const FormatStyle &Style; 628288943Sdim const AdditionalKeywords &Keywords; 629288943Sdim const SmallVectorImpl<AnnotatedLine *>::const_iterator End; 630288943Sdim 631288943Sdim SmallVectorImpl<AnnotatedLine *>::const_iterator Next; 632321369Sdim const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines; 633277325Sdim}; 634277325Sdim 635288943Sdimstatic void markFinalized(FormatToken *Tok) { 636288943Sdim for (; Tok; Tok = Tok->Next) { 637288943Sdim Tok->Finalized = true; 638288943Sdim for (AnnotatedLine *Child : Tok->Children) 639288943Sdim markFinalized(Child->First); 640288943Sdim } 641288943Sdim} 642288943Sdim 643288943Sdim#ifndef NDEBUG 644288943Sdimstatic void printLineState(const LineState &State) { 645288943Sdim llvm::dbgs() << "State: "; 646288943Sdim for (const ParenState &P : State.Stack) { 647288943Sdim llvm::dbgs() << P.Indent << "|" << P.LastSpace << "|" << P.NestedBlockIndent 648288943Sdim << " "; 649288943Sdim } 650288943Sdim llvm::dbgs() << State.NextToken->TokenText << "\n"; 651288943Sdim} 652288943Sdim#endif 653288943Sdim 654288943Sdim/// \brief Base class for classes that format one \c AnnotatedLine. 655288943Sdimclass LineFormatter { 656277325Sdimpublic: 657288943Sdim LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces, 658288943Sdim const FormatStyle &Style, 659288943Sdim UnwrappedLineFormatter *BlockFormatter) 660288943Sdim : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), 661288943Sdim BlockFormatter(BlockFormatter) {} 662288943Sdim virtual ~LineFormatter() {} 663277325Sdim 664288943Sdim /// \brief Formats an \c AnnotatedLine and returns the penalty. 665288943Sdim /// 666288943Sdim /// If \p DryRun is \c false, directly applies the changes. 667327952Sdim virtual unsigned formatLine(const AnnotatedLine &Line, 668327952Sdim unsigned FirstIndent, 669327952Sdim unsigned FirstStartColumn, 670288943Sdim bool DryRun) = 0; 671288943Sdim 672288943Sdimprotected: 673288943Sdim /// \brief If the \p State's next token is an r_brace closing a nested block, 674288943Sdim /// format the nested block before it. 675288943Sdim /// 676288943Sdim /// Returns \c true if all children could be placed successfully and adapts 677288943Sdim /// \p Penalty as well as \p State. If \p DryRun is false, also directly 678288943Sdim /// creates changes using \c Whitespaces. 679288943Sdim /// 680288943Sdim /// The crucial idea here is that children always get formatted upon 681288943Sdim /// encountering the closing brace right after the nested block. Now, if we 682288943Sdim /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is 683288943Sdim /// \c false), the entire block has to be kept on the same line (which is only 684288943Sdim /// possible if it fits on the line, only contains a single statement, etc. 685288943Sdim /// 686288943Sdim /// If \p NewLine is true, we format the nested block on separate lines, i.e. 687288943Sdim /// break after the "{", format all lines with correct indentation and the put 688288943Sdim /// the closing "}" on yet another new line. 689288943Sdim /// 690288943Sdim /// This enables us to keep the simple structure of the 691288943Sdim /// \c UnwrappedLineFormatter, where we only have two options for each token: 692288943Sdim /// break or don't break. 693288943Sdim bool formatChildren(LineState &State, bool NewLine, bool DryRun, 694288943Sdim unsigned &Penalty) { 695288943Sdim const FormatToken *LBrace = State.NextToken->getPreviousNonComment(); 696288943Sdim FormatToken &Previous = *State.NextToken->Previous; 697288943Sdim if (!LBrace || LBrace->isNot(tok::l_brace) || 698288943Sdim LBrace->BlockKind != BK_Block || Previous.Children.size() == 0) 699288943Sdim // The previous token does not open a block. Nothing to do. We don't 700288943Sdim // assert so that we can simply call this function for all tokens. 701288943Sdim return true; 702288943Sdim 703288943Sdim if (NewLine) { 704288943Sdim int AdditionalIndent = State.Stack.back().Indent - 705288943Sdim Previous.Children[0]->Level * Style.IndentWidth; 706288943Sdim 707288943Sdim Penalty += 708288943Sdim BlockFormatter->format(Previous.Children, DryRun, AdditionalIndent, 709288943Sdim /*FixBadIndentation=*/true); 710288943Sdim return true; 711288943Sdim } 712288943Sdim 713288943Sdim if (Previous.Children[0]->First->MustBreakBefore) 714288943Sdim return false; 715288943Sdim 716321369Sdim // Cannot merge into one line if this line ends on a comment. 717321369Sdim if (Previous.is(tok::comment)) 718321369Sdim return false; 719321369Sdim 720288943Sdim // Cannot merge multiple statements into a single line. 721288943Sdim if (Previous.Children.size() > 1) 722288943Sdim return false; 723288943Sdim 724321369Sdim const AnnotatedLine *Child = Previous.Children[0]; 725288943Sdim // We can't put the closing "}" on a line with a trailing comment. 726321369Sdim if (Child->Last->isTrailingComment()) 727288943Sdim return false; 728288943Sdim 729288943Sdim // If the child line exceeds the column limit, we wouldn't want to merge it. 730288943Sdim // We add +2 for the trailing " }". 731288943Sdim if (Style.ColumnLimit > 0 && 732321369Sdim Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) 733288943Sdim return false; 734288943Sdim 735288943Sdim if (!DryRun) { 736288943Sdim Whitespaces->replaceWhitespace( 737321369Sdim *Child->First, /*Newlines=*/0, /*Spaces=*/1, 738288943Sdim /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective); 739288943Sdim } 740327952Sdim Penalty += 741327952Sdim formatLine(*Child, State.Column + 1, /*FirstStartColumn=*/0, DryRun); 742288943Sdim 743321369Sdim State.Column += 1 + Child->Last->TotalLength; 744288943Sdim return true; 745288943Sdim } 746288943Sdim 747288943Sdim ContinuationIndenter *Indenter; 748288943Sdim 749288943Sdimprivate: 750288943Sdim WhitespaceManager *Whitespaces; 751288943Sdim const FormatStyle &Style; 752288943Sdim UnwrappedLineFormatter *BlockFormatter; 753288943Sdim}; 754288943Sdim 755288943Sdim/// \brief Formatter that keeps the existing line breaks. 756288943Sdimclass NoColumnLimitLineFormatter : public LineFormatter { 757288943Sdimpublic: 758288943Sdim NoColumnLimitLineFormatter(ContinuationIndenter *Indenter, 759288943Sdim WhitespaceManager *Whitespaces, 760288943Sdim const FormatStyle &Style, 761288943Sdim UnwrappedLineFormatter *BlockFormatter) 762288943Sdim : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {} 763288943Sdim 764288943Sdim /// \brief Formats the line, simply keeping all of the input's line breaking 765288943Sdim /// decisions. 766288943Sdim unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent, 767327952Sdim unsigned FirstStartColumn, bool DryRun) override { 768288943Sdim assert(!DryRun); 769327952Sdim LineState State = Indenter->getInitialState(FirstIndent, FirstStartColumn, 770327952Sdim &Line, /*DryRun=*/false); 771277325Sdim while (State.NextToken) { 772277325Sdim bool Newline = 773277325Sdim Indenter->mustBreak(State) || 774277325Sdim (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); 775288943Sdim unsigned Penalty = 0; 776288943Sdim formatChildren(State, Newline, /*DryRun=*/false, Penalty); 777277325Sdim Indenter->addTokenToState(State, Newline, /*DryRun=*/false); 778277325Sdim } 779288943Sdim return 0; 780277325Sdim } 781288943Sdim}; 782277325Sdim 783288943Sdim/// \brief Formatter that puts all tokens into a single line without breaks. 784288943Sdimclass NoLineBreakFormatter : public LineFormatter { 785288943Sdimpublic: 786288943Sdim NoLineBreakFormatter(ContinuationIndenter *Indenter, 787288943Sdim WhitespaceManager *Whitespaces, const FormatStyle &Style, 788288943Sdim UnwrappedLineFormatter *BlockFormatter) 789288943Sdim : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {} 790288943Sdim 791288943Sdim /// \brief Puts all tokens into a single line. 792288943Sdim unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent, 793327952Sdim unsigned FirstStartColumn, bool DryRun) override { 794288943Sdim unsigned Penalty = 0; 795327952Sdim LineState State = 796327952Sdim Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun); 797288943Sdim while (State.NextToken) { 798288943Sdim formatChildren(State, /*Newline=*/false, DryRun, Penalty); 799321369Sdim Indenter->addTokenToState( 800321369Sdim State, /*Newline=*/State.NextToken->MustBreakBefore, DryRun); 801288943Sdim } 802288943Sdim return Penalty; 803288943Sdim } 804288943Sdim}; 805288943Sdim 806288943Sdim/// \brief Finds the best way to break lines. 807288943Sdimclass OptimizingLineFormatter : public LineFormatter { 808288943Sdimpublic: 809288943Sdim OptimizingLineFormatter(ContinuationIndenter *Indenter, 810288943Sdim WhitespaceManager *Whitespaces, 811288943Sdim const FormatStyle &Style, 812288943Sdim UnwrappedLineFormatter *BlockFormatter) 813288943Sdim : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {} 814288943Sdim 815288943Sdim /// \brief Formats the line by finding the best line breaks with line lengths 816288943Sdim /// below the column limit. 817288943Sdim unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent, 818327952Sdim unsigned FirstStartColumn, bool DryRun) override { 819327952Sdim LineState State = 820327952Sdim Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun); 821288943Sdim 822288943Sdim // If the ObjC method declaration does not fit on a line, we should format 823288943Sdim // it with one arg per line. 824288943Sdim if (State.Line->Type == LT_ObjCMethodDecl) 825288943Sdim State.Stack.back().BreakBeforeParameter = true; 826288943Sdim 827288943Sdim // Find best solution in solution space. 828288943Sdim return analyzeSolutionSpace(State, DryRun); 829288943Sdim } 830288943Sdim 831277325Sdimprivate: 832288943Sdim struct CompareLineStatePointers { 833288943Sdim bool operator()(LineState *obj1, LineState *obj2) const { 834288943Sdim return *obj1 < *obj2; 835288943Sdim } 836288943Sdim }; 837288943Sdim 838288943Sdim /// \brief A pair of <penalty, count> that is used to prioritize the BFS on. 839288943Sdim /// 840288943Sdim /// In case of equal penalties, we want to prefer states that were inserted 841288943Sdim /// first. During state generation we make sure that we insert states first 842288943Sdim /// that break the line as late as possible. 843288943Sdim typedef std::pair<unsigned, unsigned> OrderedPenalty; 844288943Sdim 845288943Sdim /// \brief An edge in the solution space from \c Previous->State to \c State, 846288943Sdim /// inserting a newline dependent on the \c NewLine. 847288943Sdim struct StateNode { 848288943Sdim StateNode(const LineState &State, bool NewLine, StateNode *Previous) 849288943Sdim : State(State), NewLine(NewLine), Previous(Previous) {} 850288943Sdim LineState State; 851288943Sdim bool NewLine; 852288943Sdim StateNode *Previous; 853288943Sdim }; 854288943Sdim 855288943Sdim /// \brief An item in the prioritized BFS search queue. The \c StateNode's 856288943Sdim /// \c State has the given \c OrderedPenalty. 857288943Sdim typedef std::pair<OrderedPenalty, StateNode *> QueueItem; 858288943Sdim 859288943Sdim /// \brief The BFS queue type. 860288943Sdim typedef std::priority_queue<QueueItem, std::vector<QueueItem>, 861327952Sdim std::greater<QueueItem>> 862327952Sdim QueueType; 863288943Sdim 864288943Sdim /// \brief Analyze the entire solution space starting from \p InitialState. 865288943Sdim /// 866288943Sdim /// This implements a variant of Dijkstra's algorithm on the graph that spans 867288943Sdim /// the solution space (\c LineStates are the nodes). The algorithm tries to 868288943Sdim /// find the shortest path (the one with lowest penalty) from \p InitialState 869288943Sdim /// to a state where all tokens are placed. Returns the penalty. 870288943Sdim /// 871288943Sdim /// If \p DryRun is \c false, directly applies the changes. 872288943Sdim unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun) { 873288943Sdim std::set<LineState *, CompareLineStatePointers> Seen; 874288943Sdim 875288943Sdim // Increasing count of \c StateNode items we have created. This is used to 876288943Sdim // create a deterministic order independent of the container. 877288943Sdim unsigned Count = 0; 878288943Sdim QueueType Queue; 879288943Sdim 880288943Sdim // Insert start element into queue. 881288943Sdim StateNode *Node = 882288943Sdim new (Allocator.Allocate()) StateNode(InitialState, false, nullptr); 883288943Sdim Queue.push(QueueItem(OrderedPenalty(0, Count), Node)); 884288943Sdim ++Count; 885288943Sdim 886288943Sdim unsigned Penalty = 0; 887288943Sdim 888288943Sdim // While not empty, take first element and follow edges. 889288943Sdim while (!Queue.empty()) { 890288943Sdim Penalty = Queue.top().first.first; 891288943Sdim StateNode *Node = Queue.top().second; 892288943Sdim if (!Node->State.NextToken) { 893288943Sdim DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n"); 894288943Sdim break; 895288943Sdim } 896288943Sdim Queue.pop(); 897288943Sdim 898288943Sdim // Cut off the analysis of certain solutions if the analysis gets too 899288943Sdim // complex. See description of IgnoreStackForComparison. 900296417Sdim if (Count > 50000) 901288943Sdim Node->State.IgnoreStackForComparison = true; 902288943Sdim 903288943Sdim if (!Seen.insert(&Node->State).second) 904288943Sdim // State already examined with lower penalty. 905288943Sdim continue; 906288943Sdim 907288943Sdim FormatDecision LastFormat = Node->State.NextToken->Decision; 908288943Sdim if (LastFormat == FD_Unformatted || LastFormat == FD_Continue) 909288943Sdim addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue); 910288943Sdim if (LastFormat == FD_Unformatted || LastFormat == FD_Break) 911288943Sdim addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue); 912288943Sdim } 913288943Sdim 914288943Sdim if (Queue.empty()) { 915288943Sdim // We were unable to find a solution, do nothing. 916288943Sdim // FIXME: Add diagnostic? 917288943Sdim DEBUG(llvm::dbgs() << "Could not find a solution.\n"); 918288943Sdim return 0; 919288943Sdim } 920288943Sdim 921288943Sdim // Reconstruct the solution. 922288943Sdim if (!DryRun) 923288943Sdim reconstructPath(InitialState, Queue.top().second); 924288943Sdim 925288943Sdim DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n"); 926288943Sdim DEBUG(llvm::dbgs() << "---\n"); 927288943Sdim 928288943Sdim return Penalty; 929288943Sdim } 930288943Sdim 931288943Sdim /// \brief Add the following state to the analysis queue \c Queue. 932288943Sdim /// 933288943Sdim /// Assume the current state is \p PreviousNode and has been reached with a 934288943Sdim /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true. 935288943Sdim void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode, 936288943Sdim bool NewLine, unsigned *Count, QueueType *Queue) { 937288943Sdim if (NewLine && !Indenter->canBreak(PreviousNode->State)) 938288943Sdim return; 939288943Sdim if (!NewLine && Indenter->mustBreak(PreviousNode->State)) 940288943Sdim return; 941288943Sdim 942288943Sdim StateNode *Node = new (Allocator.Allocate()) 943288943Sdim StateNode(PreviousNode->State, NewLine, PreviousNode); 944288943Sdim if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty)) 945288943Sdim return; 946288943Sdim 947288943Sdim Penalty += Indenter->addTokenToState(Node->State, NewLine, true); 948288943Sdim 949288943Sdim Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node)); 950288943Sdim ++(*Count); 951288943Sdim } 952288943Sdim 953288943Sdim /// \brief Applies the best formatting by reconstructing the path in the 954288943Sdim /// solution space that leads to \c Best. 955288943Sdim void reconstructPath(LineState &State, StateNode *Best) { 956288943Sdim std::deque<StateNode *> Path; 957288943Sdim // We do not need a break before the initial token. 958288943Sdim while (Best->Previous) { 959288943Sdim Path.push_front(Best); 960288943Sdim Best = Best->Previous; 961288943Sdim } 962288943Sdim for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end(); 963288943Sdim I != E; ++I) { 964288943Sdim unsigned Penalty = 0; 965288943Sdim formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty); 966288943Sdim Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false); 967288943Sdim 968288943Sdim DEBUG({ 969288943Sdim printLineState((*I)->Previous->State); 970288943Sdim if ((*I)->NewLine) { 971288943Sdim llvm::dbgs() << "Penalty for placing " 972288943Sdim << (*I)->Previous->State.NextToken->Tok.getName() << ": " 973288943Sdim << Penalty << "\n"; 974288943Sdim } 975288943Sdim }); 976288943Sdim } 977288943Sdim } 978288943Sdim 979288943Sdim llvm::SpecificBumpPtrAllocator<StateNode> Allocator; 980277325Sdim}; 981277325Sdim 982296417Sdim} // anonymous namespace 983277325Sdim 984277325Sdimunsigned 985277325SdimUnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines, 986277325Sdim bool DryRun, int AdditionalIndent, 987327952Sdim bool FixBadIndentation, 988327952Sdim unsigned FirstStartColumn, 989327952Sdim unsigned NextStartColumn, 990327952Sdim unsigned LastStartColumn) { 991288943Sdim LineJoiner Joiner(Style, Keywords, Lines); 992277325Sdim 993277325Sdim // Try to look up already computed penalty in DryRun-mode. 994277325Sdim std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey( 995277325Sdim &Lines, AdditionalIndent); 996277325Sdim auto CacheIt = PenaltyCache.find(CacheKey); 997277325Sdim if (DryRun && CacheIt != PenaltyCache.end()) 998277325Sdim return CacheIt->second; 999277325Sdim 1000277325Sdim assert(!Lines.empty()); 1001277325Sdim unsigned Penalty = 0; 1002288943Sdim LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level, 1003288943Sdim AdditionalIndent); 1004277325Sdim const AnnotatedLine *PreviousLine = nullptr; 1005288943Sdim const AnnotatedLine *NextLine = nullptr; 1006296417Sdim 1007296417Sdim // The minimum level of consecutive lines that have been formatted. 1008296417Sdim unsigned RangeMinLevel = UINT_MAX; 1009296417Sdim 1010327952Sdim bool FirstLine = true; 1011288943Sdim for (const AnnotatedLine *Line = 1012288943Sdim Joiner.getNextMergedLine(DryRun, IndentTracker); 1013327952Sdim Line; Line = NextLine, FirstLine = false) { 1014288943Sdim const AnnotatedLine &TheLine = *Line; 1015288943Sdim unsigned Indent = IndentTracker.getIndent(); 1016296417Sdim 1017296417Sdim // We continue formatting unchanged lines to adjust their indent, e.g. if a 1018296417Sdim // scope was added. However, we need to carefully stop doing this when we 1019296417Sdim // exit the scope of affected lines to prevent indenting a the entire 1020296417Sdim // remaining file if it currently missing a closing brace. 1021296417Sdim bool ContinueFormatting = 1022296417Sdim TheLine.Level > RangeMinLevel || 1023296417Sdim (TheLine.Level == RangeMinLevel && !TheLine.startsWith(tok::r_brace)); 1024296417Sdim 1025296417Sdim bool FixIndentation = (FixBadIndentation || ContinueFormatting) && 1026296417Sdim Indent != TheLine.First->OriginalColumn; 1027288943Sdim bool ShouldFormat = TheLine.Affected || FixIndentation; 1028288943Sdim // We cannot format this line; if the reason is that the line had a 1029288943Sdim // parsing error, remember that. 1030321369Sdim if (ShouldFormat && TheLine.Type == LT_Invalid && Status) { 1031321369Sdim Status->FormatComplete = false; 1032321369Sdim Status->Line = 1033321369Sdim SourceMgr.getSpellingLineNumber(TheLine.First->Tok.getLocation()); 1034321369Sdim } 1035277325Sdim 1036288943Sdim if (ShouldFormat && TheLine.Type != LT_Invalid) { 1037327952Sdim if (!DryRun) { 1038327952Sdim bool LastLine = Line->First->is(tok::eof); 1039327952Sdim formatFirstToken(TheLine, PreviousLine, 1040327952Sdim Indent, 1041327952Sdim LastLine ? LastStartColumn : NextStartColumn + Indent); 1042327952Sdim } 1043277325Sdim 1044288943Sdim NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker); 1045288943Sdim unsigned ColumnLimit = getColumnLimit(TheLine.InPPDirective, NextLine); 1046288943Sdim bool FitsIntoOneLine = 1047288943Sdim TheLine.Last->TotalLength + Indent <= ColumnLimit || 1048309124Sdim (TheLine.Type == LT_ImportStatement && 1049309124Sdim (Style.Language != FormatStyle::LK_JavaScript || 1050309124Sdim !Style.JavaScriptWrapImports)); 1051288943Sdim if (Style.ColumnLimit == 0) 1052288943Sdim NoColumnLimitLineFormatter(Indenter, Whitespaces, Style, this) 1053327952Sdim .formatLine(TheLine, NextStartColumn + Indent, 1054327952Sdim FirstLine ? FirstStartColumn : 0, DryRun); 1055288943Sdim else if (FitsIntoOneLine) 1056288943Sdim Penalty += NoLineBreakFormatter(Indenter, Whitespaces, Style, this) 1057327952Sdim .formatLine(TheLine, NextStartColumn + Indent, 1058327952Sdim FirstLine ? FirstStartColumn : 0, DryRun); 1059288943Sdim else 1060288943Sdim Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this) 1061327952Sdim .formatLine(TheLine, NextStartColumn + Indent, 1062327952Sdim FirstLine ? FirstStartColumn : 0, DryRun); 1063296417Sdim RangeMinLevel = std::min(RangeMinLevel, TheLine.Level); 1064277325Sdim } else { 1065288943Sdim // If no token in the current line is affected, we still need to format 1066288943Sdim // affected children. 1067288943Sdim if (TheLine.ChildrenAffected) 1068309124Sdim for (const FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) 1069309124Sdim if (!Tok->Children.empty()) 1070309124Sdim format(Tok->Children, DryRun); 1071277325Sdim 1072288943Sdim // Adapt following lines on the current indent level to the same level 1073288943Sdim // unless the current \c AnnotatedLine is not at the beginning of a line. 1074288943Sdim bool StartsNewLine = 1075288943Sdim TheLine.First->NewlinesBefore > 0 || TheLine.First->IsFirst; 1076288943Sdim if (StartsNewLine) 1077288943Sdim IndentTracker.adjustToUnmodifiedLine(TheLine); 1078288943Sdim if (!DryRun) { 1079288943Sdim bool ReformatLeadingWhitespace = 1080288943Sdim StartsNewLine && ((PreviousLine && PreviousLine->Affected) || 1081288943Sdim TheLine.LeadingEmptyLinesAffected); 1082288943Sdim // Format the first token. 1083288943Sdim if (ReformatLeadingWhitespace) 1084321369Sdim formatFirstToken(TheLine, PreviousLine, 1085327952Sdim TheLine.First->OriginalColumn, 1086321369Sdim TheLine.First->OriginalColumn); 1087288943Sdim else 1088288943Sdim Whitespaces->addUntouchableToken(*TheLine.First, 1089288943Sdim TheLine.InPPDirective); 1090288943Sdim 1091288943Sdim // Notify the WhitespaceManager about the unchanged whitespace. 1092288943Sdim for (FormatToken *Tok = TheLine.First->Next; Tok; Tok = Tok->Next) 1093277325Sdim Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective); 1094277325Sdim } 1095288943Sdim NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker); 1096296417Sdim RangeMinLevel = UINT_MAX; 1097277325Sdim } 1098288943Sdim if (!DryRun) 1099288943Sdim markFinalized(TheLine.First); 1100288943Sdim PreviousLine = &TheLine; 1101277325Sdim } 1102277325Sdim PenaltyCache[CacheKey] = Penalty; 1103277325Sdim return Penalty; 1104277325Sdim} 1105277325Sdim 1106321369Sdimvoid UnwrappedLineFormatter::formatFirstToken(const AnnotatedLine &Line, 1107277325Sdim const AnnotatedLine *PreviousLine, 1108327952Sdim unsigned Indent, 1109327952Sdim unsigned NewlineIndent) { 1110327952Sdim FormatToken &RootToken = *Line.First; 1111288943Sdim if (RootToken.is(tok::eof)) { 1112288943Sdim unsigned Newlines = std::min(RootToken.NewlinesBefore, 1u); 1113327952Sdim unsigned TokenIndent = Newlines ? NewlineIndent : 0; 1114327952Sdim Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent, 1115327952Sdim TokenIndent); 1116288943Sdim return; 1117288943Sdim } 1118277325Sdim unsigned Newlines = 1119277325Sdim std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1); 1120277325Sdim // Remove empty lines before "}" where applicable. 1121277325Sdim if (RootToken.is(tok::r_brace) && 1122277325Sdim (!RootToken.Next || 1123277325Sdim (RootToken.Next->is(tok::semi) && !RootToken.Next->Next))) 1124277325Sdim Newlines = std::min(Newlines, 1u); 1125327952Sdim // Remove empty lines at the start of nested blocks (lambdas/arrow functions) 1126327952Sdim if (PreviousLine == nullptr && Line.Level > 0) 1127327952Sdim Newlines = std::min(Newlines, 1u); 1128277325Sdim if (Newlines == 0 && !RootToken.IsFirst) 1129277325Sdim Newlines = 1; 1130277325Sdim if (RootToken.IsFirst && !RootToken.HasUnescapedNewline) 1131277325Sdim Newlines = 0; 1132277325Sdim 1133277325Sdim // Remove empty lines after "{". 1134277325Sdim if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine && 1135277325Sdim PreviousLine->Last->is(tok::l_brace) && 1136277325Sdim PreviousLine->First->isNot(tok::kw_namespace) && 1137277325Sdim !startsExternCBlock(*PreviousLine)) 1138277325Sdim Newlines = 1; 1139277325Sdim 1140277325Sdim // Insert extra new line before access specifiers. 1141277325Sdim if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && 1142277325Sdim RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1) 1143277325Sdim ++Newlines; 1144277325Sdim 1145277325Sdim // Remove empty lines after access specifiers. 1146288943Sdim if (PreviousLine && PreviousLine->First->isAccessSpecifier() && 1147288943Sdim (!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline)) 1148277325Sdim Newlines = std::min(1u, Newlines); 1149277325Sdim 1150327952Sdim if (Newlines) 1151327952Sdim Indent = NewlineIndent; 1152327952Sdim 1153327952Sdim // Preprocessor directives get indented after the hash, if indented. 1154327952Sdim if (Line.Type == LT_PreprocessorDirective || Line.Type == LT_ImportStatement) 1155327952Sdim Indent = 0; 1156327952Sdim 1157321369Sdim Whitespaces->replaceWhitespace(RootToken, Newlines, Indent, Indent, 1158321369Sdim Line.InPPDirective && 1159321369Sdim !RootToken.HasUnescapedNewline); 1160277325Sdim} 1161277325Sdim 1162288943Sdimunsigned 1163288943SdimUnwrappedLineFormatter::getColumnLimit(bool InPPDirective, 1164288943Sdim const AnnotatedLine *NextLine) const { 1165288943Sdim // In preprocessor directives reserve two chars for trailing " \" if the 1166288943Sdim // next line continues the preprocessor directive. 1167288943Sdim bool ContinuesPPDirective = 1168288943Sdim InPPDirective && 1169288943Sdim // If there is no next line, this is likely a child line and the parent 1170288943Sdim // continues the preprocessor directive. 1171288943Sdim (!NextLine || 1172288943Sdim (NextLine->InPPDirective && 1173288943Sdim // If there is an unescaped newline between this line and the next, the 1174288943Sdim // next line starts a new preprocessor directive. 1175288943Sdim !NextLine->First->HasUnescapedNewline)); 1176288943Sdim return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0); 1177277325Sdim} 1178277325Sdim 1179277325Sdim} // namespace format 1180277325Sdim} // namespace clang 1181