Path: blob/main/contrib/llvm-project/clang/lib/Format/UnwrappedLineFormatter.cpp
35234 views
//===--- UnwrappedLineFormatter.cpp - Format C++ code ---------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "UnwrappedLineFormatter.h"9#include "FormatToken.h"10#include "NamespaceEndCommentsFixer.h"11#include "WhitespaceManager.h"12#include "llvm/Support/Debug.h"13#include <queue>1415#define DEBUG_TYPE "format-formatter"1617namespace clang {18namespace format {1920namespace {2122bool startsExternCBlock(const AnnotatedLine &Line) {23const FormatToken *Next = Line.First->getNextNonComment();24const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr;25return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() &&26NextNext && NextNext->is(tok::l_brace);27}2829bool isRecordLBrace(const FormatToken &Tok) {30return Tok.isOneOf(TT_ClassLBrace, TT_EnumLBrace, TT_RecordLBrace,31TT_StructLBrace, TT_UnionLBrace);32}3334/// Tracks the indent level of \c AnnotatedLines across levels.35///36/// \c nextLine must be called for each \c AnnotatedLine, after which \c37/// getIndent() will return the indent for the last line \c nextLine was called38/// with.39/// If the line is not formatted (and thus the indent does not change), calling40/// \c adjustToUnmodifiedLine after the call to \c nextLine will cause41/// subsequent lines on the same level to be indented at the same level as the42/// given line.43class LevelIndentTracker {44public:45LevelIndentTracker(const FormatStyle &Style,46const AdditionalKeywords &Keywords, unsigned StartLevel,47int AdditionalIndent)48: Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) {49for (unsigned i = 0; i != StartLevel; ++i)50IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);51}5253/// Returns the indent for the current line.54unsigned getIndent() const { return Indent; }5556/// Update the indent state given that \p Line is going to be formatted57/// next.58void nextLine(const AnnotatedLine &Line) {59Offset = getIndentOffset(Line);60// Update the indent level cache size so that we can rely on it61// having the right size in adjustToUnmodifiedline.62if (Line.Level >= IndentForLevel.size())63IndentForLevel.resize(Line.Level + 1, -1);64if (Style.IndentPPDirectives != FormatStyle::PPDIS_None &&65(Line.InPPDirective ||66(Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&67Line.Type == LT_CommentAbovePPDirective))) {68unsigned PPIndentWidth =69(Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth;70Indent = Line.InMacroBody71? Line.PPLevel * PPIndentWidth +72(Line.Level - Line.PPLevel) * Style.IndentWidth73: Line.Level * PPIndentWidth;74Indent += AdditionalIndent;75} else {76// When going to lower levels, forget previous higher levels so that we77// recompute future higher levels. But don't forget them if we enter a PP78// directive, since these do not terminate a C++ code block.79if (!Line.InPPDirective) {80assert(Line.Level <= IndentForLevel.size());81IndentForLevel.resize(Line.Level + 1);82}83Indent = getIndent(Line.Level);84}85if (static_cast<int>(Indent) + Offset >= 0)86Indent += Offset;87if (Line.IsContinuation)88Indent = Line.Level * Style.IndentWidth + Style.ContinuationIndentWidth;89}9091/// Update the level indent to adapt to the given \p Line.92///93/// When a line is not formatted, we move the subsequent lines on the same94/// level to the same indent.95/// Note that \c nextLine must have been called before this method.96void adjustToUnmodifiedLine(const AnnotatedLine &Line) {97if (Line.InPPDirective || Line.IsContinuation)98return;99assert(Line.Level < IndentForLevel.size());100if (Line.First->is(tok::comment) && IndentForLevel[Line.Level] != -1)101return;102unsigned LevelIndent = Line.First->OriginalColumn;103if (static_cast<int>(LevelIndent) - Offset >= 0)104LevelIndent -= Offset;105IndentForLevel[Line.Level] = LevelIndent;106}107108private:109/// Get the offset of the line relatively to the level.110///111/// For example, 'public:' labels in classes are offset by 1 or 2112/// characters to the left from their level.113int getIndentOffset(const AnnotatedLine &Line) {114if (Style.Language == FormatStyle::LK_Java || Style.isJavaScript() ||115Style.isCSharp()) {116return 0;117}118119auto IsAccessModifier = [&](const FormatToken &RootToken) {120if (Line.Type == LT_AccessModifier || RootToken.isObjCAccessSpecifier())121return true;122123const auto *Next = RootToken.Next;124125// Handle Qt signals.126if (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&127Next && Next->is(tok::colon)) {128return true;129}130131if (Next && Next->isOneOf(Keywords.kw_slots, Keywords.kw_qslots) &&132Next->Next && Next->Next->is(tok::colon)) {133return true;134}135136// Handle malformed access specifier e.g. 'private' without trailing ':'.137return !Next && RootToken.isAccessSpecifier(false);138};139140if (IsAccessModifier(*Line.First)) {141// The AccessModifierOffset may be overridden by IndentAccessModifiers,142// in which case we take a negative value of the IndentWidth to simulate143// the upper indent level.144return Style.IndentAccessModifiers ? -Style.IndentWidth145: Style.AccessModifierOffset;146}147148return 0;149}150151/// Get the indent of \p Level from \p IndentForLevel.152///153/// \p IndentForLevel must contain the indent for the level \c l154/// at \p IndentForLevel[l], or a value < 0 if the indent for155/// that level is unknown.156unsigned getIndent(unsigned Level) const {157assert(Level < IndentForLevel.size());158if (IndentForLevel[Level] != -1)159return IndentForLevel[Level];160if (Level == 0)161return 0;162return getIndent(Level - 1) + Style.IndentWidth;163}164165const FormatStyle &Style;166const AdditionalKeywords &Keywords;167const unsigned AdditionalIndent;168169/// The indent in characters for each level. It remembers the indent of170/// previous lines (that are not PP directives) of equal or lower levels. This171/// is used to align formatted lines to the indent of previous non-formatted172/// lines. Think about the --lines parameter of clang-format.173SmallVector<int> IndentForLevel;174175/// Offset of the current line relative to the indent level.176///177/// For example, the 'public' keywords is often indented with a negative178/// offset.179int Offset = 0;180181/// The current line's indent.182unsigned Indent = 0;183};184185const FormatToken *getMatchingNamespaceToken(186const AnnotatedLine *Line,187const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {188if (!Line->startsWith(tok::r_brace))189return nullptr;190size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;191if (StartLineIndex == UnwrappedLine::kInvalidIndex)192return nullptr;193assert(StartLineIndex < AnnotatedLines.size());194return AnnotatedLines[StartLineIndex]->First->getNamespaceToken();195}196197StringRef getNamespaceTokenText(const AnnotatedLine *Line) {198const FormatToken *NamespaceToken = Line->First->getNamespaceToken();199return NamespaceToken ? NamespaceToken->TokenText : StringRef();200}201202StringRef getMatchingNamespaceTokenText(203const AnnotatedLine *Line,204const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {205const FormatToken *NamespaceToken =206getMatchingNamespaceToken(Line, AnnotatedLines);207return NamespaceToken ? NamespaceToken->TokenText : StringRef();208}209210class LineJoiner {211public:212LineJoiner(const FormatStyle &Style, const AdditionalKeywords &Keywords,213const SmallVectorImpl<AnnotatedLine *> &Lines)214: Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()),215AnnotatedLines(Lines) {}216217/// Returns the next line, merging multiple lines into one if possible.218const AnnotatedLine *getNextMergedLine(bool DryRun,219LevelIndentTracker &IndentTracker) {220if (Next == End)221return nullptr;222const AnnotatedLine *Current = *Next;223IndentTracker.nextLine(*Current);224unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End);225if (MergedLines > 0 && Style.ColumnLimit == 0) {226// Disallow line merging if there is a break at the start of one of the227// input lines.228for (unsigned i = 0; i < MergedLines; ++i)229if (Next[i + 1]->First->NewlinesBefore > 0)230MergedLines = 0;231}232if (!DryRun)233for (unsigned i = 0; i < MergedLines; ++i)234join(*Next[0], *Next[i + 1]);235Next = Next + MergedLines + 1;236return Current;237}238239private:240/// Calculates how many lines can be merged into 1 starting at \p I.241unsigned242tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,243SmallVectorImpl<AnnotatedLine *>::const_iterator I,244SmallVectorImpl<AnnotatedLine *>::const_iterator E) {245const unsigned Indent = IndentTracker.getIndent();246247// Can't join the last line with anything.248if (I + 1 == E)249return 0;250// We can never merge stuff if there are trailing line comments.251const AnnotatedLine *TheLine = *I;252if (TheLine->Last->is(TT_LineComment))253return 0;254const auto &NextLine = *I[1];255if (NextLine.Type == LT_Invalid || NextLine.First->MustBreakBefore)256return 0;257if (TheLine->InPPDirective &&258(!NextLine.InPPDirective || NextLine.First->HasUnescapedNewline)) {259return 0;260}261262if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)263return 0;264265unsigned Limit =266Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent;267// If we already exceed the column limit, we set 'Limit' to 0. The different268// tryMerge..() functions can then decide whether to still do merging.269Limit = TheLine->Last->TotalLength > Limit270? 0271: Limit - TheLine->Last->TotalLength;272273if (TheLine->Last->is(TT_FunctionLBrace) &&274TheLine->First == TheLine->Last &&275!Style.BraceWrapping.SplitEmptyFunction &&276NextLine.First->is(tok::r_brace)) {277return tryMergeSimpleBlock(I, E, Limit);278}279280const auto *PreviousLine = I != AnnotatedLines.begin() ? I[-1] : nullptr;281// Handle empty record blocks where the brace has already been wrapped.282if (PreviousLine && TheLine->Last->is(tok::l_brace) &&283TheLine->First == TheLine->Last) {284bool EmptyBlock = NextLine.First->is(tok::r_brace);285286const FormatToken *Tok = PreviousLine->First;287if (Tok && Tok->is(tok::comment))288Tok = Tok->getNextNonComment();289290if (Tok && Tok->getNamespaceToken()) {291return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock292? tryMergeSimpleBlock(I, E, Limit)293: 0;294}295296if (Tok && Tok->is(tok::kw_typedef))297Tok = Tok->getNextNonComment();298if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,299tok::kw_extern, Keywords.kw_interface)) {300return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock301? tryMergeSimpleBlock(I, E, Limit)302: 0;303}304305if (Tok && Tok->is(tok::kw_template) &&306Style.BraceWrapping.SplitEmptyRecord && EmptyBlock) {307return 0;308}309}310311auto ShouldMergeShortFunctions = [this, &I, &NextLine, PreviousLine,312TheLine]() {313if (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All)314return true;315if (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&316NextLine.First->is(tok::r_brace)) {317return true;318}319320if (Style.AllowShortFunctionsOnASingleLine &321FormatStyle::SFS_InlineOnly) {322// Just checking TheLine->Level != 0 is not enough, because it323// provokes treating functions inside indented namespaces as short.324if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace))325return true;326327if (TheLine->Level != 0) {328if (!PreviousLine)329return false;330331// TODO: Use IndentTracker to avoid loop?332// Find the last line with lower level.333const AnnotatedLine *Line = nullptr;334for (auto J = I - 1; J >= AnnotatedLines.begin(); --J) {335assert(*J);336if (!(*J)->InPPDirective && !(*J)->isComment() &&337(*J)->Level < TheLine->Level) {338Line = *J;339break;340}341}342343if (!Line)344return false;345346// Check if the found line starts a record.347const auto *LastNonComment = Line->getLastNonComment();348// There must be another token (usually `{`), because we chose a349// non-PPDirective and non-comment line that has a smaller level.350assert(LastNonComment);351return isRecordLBrace(*LastNonComment);352}353}354355return false;356};357358bool MergeShortFunctions = ShouldMergeShortFunctions();359360const auto *FirstNonComment = TheLine->getFirstNonComment();361if (!FirstNonComment)362return 0;363// FIXME: There are probably cases where we should use FirstNonComment364// instead of TheLine->First.365366if (Style.CompactNamespaces) {367if (const auto *NSToken = TheLine->First->getNamespaceToken()) {368int J = 1;369assert(TheLine->MatchingClosingBlockLineIndex > 0);370for (auto ClosingLineIndex = TheLine->MatchingClosingBlockLineIndex - 1;371I + J != E && NSToken->TokenText == getNamespaceTokenText(I[J]) &&372ClosingLineIndex == I[J]->MatchingClosingBlockLineIndex &&373I[J]->Last->TotalLength < Limit;374++J, --ClosingLineIndex) {375Limit -= I[J]->Last->TotalLength;376377// Reduce indent level for bodies of namespaces which were compacted,378// but only if their content was indented in the first place.379auto *ClosingLine = AnnotatedLines.begin() + ClosingLineIndex + 1;380const int OutdentBy = I[J]->Level - TheLine->Level;381assert(OutdentBy >= 0);382for (auto *CompactedLine = I + J; CompactedLine <= ClosingLine;383++CompactedLine) {384if (!(*CompactedLine)->InPPDirective) {385const int Level = (*CompactedLine)->Level;386(*CompactedLine)->Level = std::max(Level - OutdentBy, 0);387}388}389}390return J - 1;391}392393if (auto nsToken = getMatchingNamespaceToken(TheLine, AnnotatedLines)) {394int i = 0;395unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;396for (; I + 1 + i != E &&397nsToken->TokenText ==398getMatchingNamespaceTokenText(I[i + 1], AnnotatedLines) &&399openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;400i++, --openingLine) {401// No space between consecutive braces.402I[i + 1]->First->SpacesRequiredBefore =403I[i]->Last->isNot(tok::r_brace);404405// Indent like the outer-most namespace.406IndentTracker.nextLine(*I[i + 1]);407}408return i;409}410}411412const auto *LastNonComment = TheLine->getLastNonComment();413assert(LastNonComment);414// FIXME: There are probably cases where we should use LastNonComment415// instead of TheLine->Last.416417// Try to merge a function block with left brace unwrapped.418if (LastNonComment->is(TT_FunctionLBrace) &&419TheLine->First != LastNonComment) {420return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;421}422// Try to merge a control statement block with left brace unwrapped.423if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&424FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,425TT_ForEachMacro)) {426return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never427? tryMergeSimpleBlock(I, E, Limit)428: 0;429}430// Try to merge a control statement block with left brace wrapped.431if (NextLine.First->is(tok::l_brace)) {432if ((TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,433tok::kw_for, tok::kw_switch, tok::kw_try,434tok::kw_do, TT_ForEachMacro) ||435(TheLine->First->is(tok::r_brace) && TheLine->First->Next &&436TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&437Style.BraceWrapping.AfterControlStatement ==438FormatStyle::BWACS_MultiLine) {439// If possible, merge the next line's wrapped left brace with the440// current line. Otherwise, leave it on the next line, as this is a441// multi-line control statement.442return (Style.ColumnLimit == 0 || TheLine->Level * Style.IndentWidth +443TheLine->Last->TotalLength <=444Style.ColumnLimit)445? 1446: 0;447}448if (TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,449tok::kw_for, TT_ForEachMacro)) {450return (Style.BraceWrapping.AfterControlStatement ==451FormatStyle::BWACS_Always)452? tryMergeSimpleBlock(I, E, Limit)453: 0;454}455if (TheLine->First->isOneOf(tok::kw_else, tok::kw_catch) &&456Style.BraceWrapping.AfterControlStatement ==457FormatStyle::BWACS_MultiLine) {458// This case if different from the upper BWACS_MultiLine processing459// in that a preceding r_brace is not on the same line as else/catch460// most likely because of BeforeElse/BeforeCatch set to true.461// If the line length doesn't fit ColumnLimit, leave l_brace on the462// next line to respect the BWACS_MultiLine.463return (Style.ColumnLimit == 0 ||464TheLine->Last->TotalLength <= Style.ColumnLimit)465? 1466: 0;467}468}469if (PreviousLine && TheLine->First->is(tok::l_brace)) {470switch (PreviousLine->First->Tok.getKind()) {471case tok::at:472// Don't merge block with left brace wrapped after ObjC special blocks.473if (PreviousLine->First->Next) {474tok::ObjCKeywordKind kwId =475PreviousLine->First->Next->Tok.getObjCKeywordID();476if (kwId == tok::objc_autoreleasepool ||477kwId == tok::objc_synchronized) {478return 0;479}480}481break;482483case tok::kw_case:484case tok::kw_default:485// Don't merge block with left brace wrapped after case labels.486return 0;487488default:489break;490}491}492493// Don't merge an empty template class or struct if SplitEmptyRecords494// is defined.495if (PreviousLine && Style.BraceWrapping.SplitEmptyRecord &&496TheLine->Last->is(tok::l_brace) && PreviousLine->Last) {497const FormatToken *Previous = PreviousLine->Last;498if (Previous) {499if (Previous->is(tok::comment))500Previous = Previous->getPreviousNonComment();501if (Previous) {502if (Previous->is(tok::greater) && !PreviousLine->InPPDirective)503return 0;504if (Previous->is(tok::identifier)) {505const FormatToken *PreviousPrevious =506Previous->getPreviousNonComment();507if (PreviousPrevious &&508PreviousPrevious->isOneOf(tok::kw_class, tok::kw_struct)) {509return 0;510}511}512}513}514}515516if (TheLine->First->is(TT_SwitchExpressionLabel)) {517return Style.AllowShortCaseExpressionOnASingleLine518? tryMergeShortCaseLabels(I, E, Limit)519: 0;520}521522if (TheLine->Last->is(tok::l_brace)) {523bool ShouldMerge = false;524// Try to merge records.525if (TheLine->Last->is(TT_EnumLBrace)) {526ShouldMerge = Style.AllowShortEnumsOnASingleLine;527} else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) {528ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;529} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {530// NOTE: We use AfterClass (whereas AfterStruct exists) for both classes531// and structs, but it seems that wrapping is still handled correctly532// elsewhere.533ShouldMerge = !Style.BraceWrapping.AfterClass ||534(NextLine.First->is(tok::r_brace) &&535!Style.BraceWrapping.SplitEmptyRecord);536} else if (TheLine->InPPDirective ||537!TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,538tok::kw_struct)) {539// Try to merge a block with left brace unwrapped that wasn't yet540// covered.541ShouldMerge = !Style.BraceWrapping.AfterFunction ||542(NextLine.First->is(tok::r_brace) &&543!Style.BraceWrapping.SplitEmptyFunction);544}545return ShouldMerge ? tryMergeSimpleBlock(I, E, Limit) : 0;546}547548// Try to merge a function block with left brace wrapped.549if (NextLine.First->is(TT_FunctionLBrace) &&550Style.BraceWrapping.AfterFunction) {551if (NextLine.Last->is(TT_LineComment))552return 0;553554// Check for Limit <= 2 to account for the " {".555if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))556return 0;557Limit -= 2;558559unsigned MergedLines = 0;560if (MergeShortFunctions ||561(Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&562NextLine.First == NextLine.Last && I + 2 != E &&563I[2]->First->is(tok::r_brace))) {564MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);565// If we managed to merge the block, count the function header, which is566// on a separate line.567if (MergedLines > 0)568++MergedLines;569}570return MergedLines;571}572auto IsElseLine = [&TheLine]() -> bool {573const FormatToken *First = TheLine->First;574if (First->is(tok::kw_else))575return true;576577return First->is(tok::r_brace) && First->Next &&578First->Next->is(tok::kw_else);579};580if (TheLine->First->is(tok::kw_if) ||581(IsElseLine() && (Style.AllowShortIfStatementsOnASingleLine ==582FormatStyle::SIS_AllIfsAndElse))) {583return Style.AllowShortIfStatementsOnASingleLine584? tryMergeSimpleControlStatement(I, E, Limit)585: 0;586}587if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do,588TT_ForEachMacro)) {589return Style.AllowShortLoopsOnASingleLine590? tryMergeSimpleControlStatement(I, E, Limit)591: 0;592}593if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {594return Style.AllowShortCaseLabelsOnASingleLine595? tryMergeShortCaseLabels(I, E, Limit)596: 0;597}598if (TheLine->InPPDirective &&599(TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {600return tryMergeSimplePPDirective(I, E, Limit);601}602return 0;603}604605unsigned606tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,607SmallVectorImpl<AnnotatedLine *>::const_iterator E,608unsigned Limit) {609if (Limit == 0)610return 0;611if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline)612return 0;613if (1 + I[1]->Last->TotalLength > Limit)614return 0;615return 1;616}617618unsigned tryMergeSimpleControlStatement(619SmallVectorImpl<AnnotatedLine *>::const_iterator I,620SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {621if (Limit == 0)622return 0;623if (Style.BraceWrapping.AfterControlStatement ==624FormatStyle::BWACS_Always &&625I[1]->First->is(tok::l_brace) &&626Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never) {627return 0;628}629if (I[1]->InPPDirective != (*I)->InPPDirective ||630(I[1]->InPPDirective && I[1]->First->HasUnescapedNewline)) {631return 0;632}633Limit = limitConsideringMacros(I + 1, E, Limit);634AnnotatedLine &Line = **I;635if (Line.First->isNot(tok::kw_do) && Line.First->isNot(tok::kw_else) &&636Line.Last->isNot(tok::kw_else) && Line.Last->isNot(tok::r_paren)) {637return 0;638}639// Only merge `do while` if `do` is the only statement on the line.640if (Line.First->is(tok::kw_do) && Line.Last->isNot(tok::kw_do))641return 0;642if (1 + I[1]->Last->TotalLength > Limit)643return 0;644// Don't merge with loops, ifs, a single semicolon or a line comment.645if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,646TT_ForEachMacro, TT_LineComment)) {647return 0;648}649// Only inline simple if's (no nested if or else), unless specified650if (Style.AllowShortIfStatementsOnASingleLine ==651FormatStyle::SIS_WithoutElse) {652if (I + 2 != E && Line.startsWith(tok::kw_if) &&653I[2]->First->is(tok::kw_else)) {654return 0;655}656}657return 1;658}659660unsigned661tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,662SmallVectorImpl<AnnotatedLine *>::const_iterator E,663unsigned Limit) {664if (Limit == 0 || I + 1 == E ||665I[1]->First->isOneOf(tok::kw_case, tok::kw_default)) {666return 0;667}668if (I[0]->Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))669return 0;670unsigned NumStmts = 0;671unsigned Length = 0;672bool EndsWithComment = false;673bool InPPDirective = I[0]->InPPDirective;674bool InMacroBody = I[0]->InMacroBody;675const unsigned Level = I[0]->Level;676for (; NumStmts < 3; ++NumStmts) {677if (I + 1 + NumStmts == E)678break;679const AnnotatedLine *Line = I[1 + NumStmts];680if (Line->InPPDirective != InPPDirective)681break;682if (Line->InMacroBody != InMacroBody)683break;684if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))685break;686if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,687tok::kw_while) ||688EndsWithComment) {689return 0;690}691if (Line->First->is(tok::comment)) {692if (Level != Line->Level)693return 0;694SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts;695for (; J != E; ++J) {696Line = *J;697if (Line->InPPDirective != InPPDirective)698break;699if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))700break;701if (Line->First->isNot(tok::comment) || Level != Line->Level)702return 0;703}704break;705}706if (Line->Last->is(tok::comment))707EndsWithComment = true;708Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space.709}710if (NumStmts == 0 || NumStmts == 3 || Length > Limit)711return 0;712return NumStmts;713}714715unsigned716tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,717SmallVectorImpl<AnnotatedLine *>::const_iterator E,718unsigned Limit) {719// Don't merge with a preprocessor directive.720if (I[1]->Type == LT_PreprocessorDirective)721return 0;722723AnnotatedLine &Line = **I;724725// Don't merge ObjC @ keywords and methods.726// FIXME: If an option to allow short exception handling clauses on a single727// line is added, change this to not return for @try and friends.728if (Style.Language != FormatStyle::LK_Java &&729Line.First->isOneOf(tok::at, tok::minus, tok::plus)) {730return 0;731}732733// Check that the current line allows merging. This depends on whether we734// are in a control flow statements as well as several style flags.735if (Line.First->is(tok::kw_case) ||736(Line.First->Next && Line.First->Next->is(tok::kw_else))) {737return 0;738}739// default: in switch statement740if (Line.First->is(tok::kw_default)) {741const FormatToken *Tok = Line.First->getNextNonComment();742if (Tok && Tok->is(tok::colon))743return 0;744}745746auto IsCtrlStmt = [](const auto &Line) {747return Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,748tok::kw_do, tok::kw_for, TT_ForEachMacro);749};750751const bool IsSplitBlock =752Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never ||753(Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Empty &&754I[1]->First->isNot(tok::r_brace));755756if (IsCtrlStmt(Line) ||757Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,758tok::kw___finally, tok::r_brace,759Keywords.kw___except)) {760if (IsSplitBlock)761return 0;762// Don't merge when we can't except the case when763// the control statement block is empty764if (!Style.AllowShortIfStatementsOnASingleLine &&765Line.First->isOneOf(tok::kw_if, tok::kw_else) &&766!Style.BraceWrapping.AfterControlStatement &&767I[1]->First->isNot(tok::r_brace)) {768return 0;769}770if (!Style.AllowShortIfStatementsOnASingleLine &&771Line.First->isOneOf(tok::kw_if, tok::kw_else) &&772Style.BraceWrapping.AfterControlStatement ==773FormatStyle::BWACS_Always &&774I + 2 != E && I[2]->First->isNot(tok::r_brace)) {775return 0;776}777if (!Style.AllowShortLoopsOnASingleLine &&778Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,779TT_ForEachMacro) &&780!Style.BraceWrapping.AfterControlStatement &&781I[1]->First->isNot(tok::r_brace)) {782return 0;783}784if (!Style.AllowShortLoopsOnASingleLine &&785Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for,786TT_ForEachMacro) &&787Style.BraceWrapping.AfterControlStatement ==788FormatStyle::BWACS_Always &&789I + 2 != E && I[2]->First->isNot(tok::r_brace)) {790return 0;791}792// FIXME: Consider an option to allow short exception handling clauses on793// a single line.794// FIXME: This isn't covered by tests.795// FIXME: For catch, __except, __finally the first token on the line796// is '}', so this isn't correct here.797if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,798Keywords.kw___except, tok::kw___finally)) {799return 0;800}801}802803if (Line.endsWith(tok::l_brace)) {804if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never &&805Line.First->is(TT_BlockLBrace)) {806return 0;807}808809if (IsSplitBlock && Line.First == Line.Last &&810I > AnnotatedLines.begin() &&811(I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {812return 0;813}814FormatToken *Tok = I[1]->First;815auto ShouldMerge = [Tok]() {816if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore)817return false;818const FormatToken *Next = Tok->getNextNonComment();819return !Next || Next->is(tok::semi);820};821822if (ShouldMerge()) {823// We merge empty blocks even if the line exceeds the column limit.824Tok->SpacesRequiredBefore =825(Style.SpaceInEmptyBlock || Line.Last->is(tok::comment)) ? 1 : 0;826Tok->CanBreakBefore = true;827return 1;828} else if (Limit != 0 && !Line.startsWithNamespace() &&829!startsExternCBlock(Line)) {830// We don't merge short records.831if (isRecordLBrace(*Line.Last))832return 0;833834// Check that we still have three lines and they fit into the limit.835if (I + 2 == E || I[2]->Type == LT_Invalid)836return 0;837Limit = limitConsideringMacros(I + 2, E, Limit);838839if (!nextTwoLinesFitInto(I, Limit))840return 0;841842// Second, check that the next line does not contain any braces - if it843// does, readability declines when putting it into a single line.844if (I[1]->Last->is(TT_LineComment))845return 0;846do {847if (Tok->is(tok::l_brace) && Tok->isNot(BK_BracedInit))848return 0;849Tok = Tok->Next;850} while (Tok);851852// Last, check that the third line starts with a closing brace.853Tok = I[2]->First;854if (Tok->isNot(tok::r_brace))855return 0;856857// Don't merge "if (a) { .. } else {".858if (Tok->Next && Tok->Next->is(tok::kw_else))859return 0;860861// Don't merge a trailing multi-line control statement block like:862// } else if (foo &&863// bar)864// { <-- current Line865// baz();866// }867if (Line.First == Line.Last && Line.First->isNot(TT_FunctionLBrace) &&868Style.BraceWrapping.AfterControlStatement ==869FormatStyle::BWACS_MultiLine) {870return 0;871}872873return 2;874}875} else if (I[1]->First->is(tok::l_brace)) {876if (I[1]->Last->is(TT_LineComment))877return 0;878879// Check for Limit <= 2 to account for the " {".880if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I)))881return 0;882Limit -= 2;883unsigned MergedLines = 0;884if (Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never ||885(I[1]->First == I[1]->Last && I + 2 != E &&886I[2]->First->is(tok::r_brace))) {887MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);888// If we managed to merge the block, count the statement header, which889// is on a separate line.890if (MergedLines > 0)891++MergedLines;892}893return MergedLines;894}895return 0;896}897898/// Returns the modified column limit for \p I if it is inside a macro and899/// needs a trailing '\'.900unsigned901limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,902SmallVectorImpl<AnnotatedLine *>::const_iterator E,903unsigned Limit) {904if (I[0]->InPPDirective && I + 1 != E &&905!I[1]->First->HasUnescapedNewline && I[1]->First->isNot(tok::eof)) {906return Limit < 2 ? 0 : Limit - 2;907}908return Limit;909}910911bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,912unsigned Limit) {913if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore)914return false;915return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;916}917918bool containsMustBreak(const AnnotatedLine *Line) {919assert(Line->First);920// Ignore the first token, because in this situation, it applies more to the921// last token of the previous line.922for (const FormatToken *Tok = Line->First->Next; Tok; Tok = Tok->Next)923if (Tok->MustBreakBefore)924return true;925return false;926}927928void join(AnnotatedLine &A, const AnnotatedLine &B) {929assert(!A.Last->Next);930assert(!B.First->Previous);931if (B.Affected)932A.Affected = true;933A.Last->Next = B.First;934B.First->Previous = A.Last;935B.First->CanBreakBefore = true;936unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;937for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {938Tok->TotalLength += LengthA;939A.Last = Tok;940}941}942943const FormatStyle &Style;944const AdditionalKeywords &Keywords;945const SmallVectorImpl<AnnotatedLine *>::const_iterator End;946947SmallVectorImpl<AnnotatedLine *>::const_iterator Next;948const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines;949};950951static void markFinalized(FormatToken *Tok) {952if (Tok->is(tok::hash) && !Tok->Previous && Tok->Next &&953Tok->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_ifndef,954tok::pp_elif, tok::pp_elifdef, tok::pp_elifndef,955tok::pp_else, tok::pp_endif)) {956Tok = Tok->Next;957}958for (; Tok; Tok = Tok->Next) {959if (Tok->MacroCtx && Tok->MacroCtx->Role == MR_ExpandedArg) {960// In the first pass we format all macro arguments in the expanded token961// stream. Instead of finalizing the macro arguments, we mark that they962// will be modified as unexpanded arguments (as part of the macro call963// formatting) in the next pass.964Tok->MacroCtx->Role = MR_UnexpandedArg;965// Reset whether spaces or a line break are required before this token, as966// that is context dependent, and that context may change when formatting967// the macro call. For example, given M(x) -> 2 * x, and the macro call968// M(var), the token 'var' will have SpacesRequiredBefore = 1 after being969// formatted as part of the expanded macro, but SpacesRequiredBefore = 0970// for its position within the macro call.971Tok->SpacesRequiredBefore = 0;972if (!Tok->MustBreakBeforeFinalized)973Tok->MustBreakBefore = 0;974} else {975Tok->Finalized = true;976}977}978}979980#ifndef NDEBUG981static void printLineState(const LineState &State) {982llvm::dbgs() << "State: ";983for (const ParenState &P : State.Stack) {984llvm::dbgs() << (P.Tok ? P.Tok->TokenText : "F") << "|" << P.Indent << "|"985<< P.LastSpace << "|" << P.NestedBlockIndent << " ";986}987llvm::dbgs() << State.NextToken->TokenText << "\n";988}989#endif990991/// Base class for classes that format one \c AnnotatedLine.992class LineFormatter {993public:994LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces,995const FormatStyle &Style,996UnwrappedLineFormatter *BlockFormatter)997: Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),998BlockFormatter(BlockFormatter) {}999virtual ~LineFormatter() {}10001001/// Formats an \c AnnotatedLine and returns the penalty.1002///1003/// If \p DryRun is \c false, directly applies the changes.1004virtual unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,1005unsigned FirstStartColumn, bool DryRun) = 0;10061007protected:1008/// If the \p State's next token is an r_brace closing a nested block,1009/// format the nested block before it.1010///1011/// Returns \c true if all children could be placed successfully and adapts1012/// \p Penalty as well as \p State. If \p DryRun is false, also directly1013/// creates changes using \c Whitespaces.1014///1015/// The crucial idea here is that children always get formatted upon1016/// encountering the closing brace right after the nested block. Now, if we1017/// are currently trying to keep the "}" on the same line (i.e. \p NewLine is1018/// \c false), the entire block has to be kept on the same line (which is only1019/// possible if it fits on the line, only contains a single statement, etc.1020///1021/// If \p NewLine is true, we format the nested block on separate lines, i.e.1022/// break after the "{", format all lines with correct indentation and the put1023/// the closing "}" on yet another new line.1024///1025/// This enables us to keep the simple structure of the1026/// \c UnwrappedLineFormatter, where we only have two options for each token:1027/// break or don't break.1028bool formatChildren(LineState &State, bool NewLine, bool DryRun,1029unsigned &Penalty) {1030const FormatToken *LBrace = State.NextToken->getPreviousNonComment();1031bool HasLBrace = LBrace && LBrace->is(tok::l_brace) && LBrace->is(BK_Block);1032FormatToken &Previous = *State.NextToken->Previous;1033if (Previous.Children.size() == 0 || (!HasLBrace && !LBrace->MacroParent)) {1034// The previous token does not open a block. Nothing to do. We don't1035// assert so that we can simply call this function for all tokens.1036return true;1037}10381039if (NewLine || Previous.MacroParent) {1040const ParenState &P = State.Stack.back();10411042int AdditionalIndent =1043P.Indent - Previous.Children[0]->Level * Style.IndentWidth;1044Penalty +=1045BlockFormatter->format(Previous.Children, DryRun, AdditionalIndent,1046/*FixBadIndentation=*/true);1047return true;1048}10491050if (Previous.Children[0]->First->MustBreakBefore)1051return false;10521053// Cannot merge into one line if this line ends on a comment.1054if (Previous.is(tok::comment))1055return false;10561057// Cannot merge multiple statements into a single line.1058if (Previous.Children.size() > 1)1059return false;10601061const AnnotatedLine *Child = Previous.Children[0];1062// We can't put the closing "}" on a line with a trailing comment.1063if (Child->Last->isTrailingComment())1064return false;10651066// If the child line exceeds the column limit, we wouldn't want to merge it.1067// We add +2 for the trailing " }".1068if (Style.ColumnLimit > 0 &&1069Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit) {1070return false;1071}10721073if (!DryRun) {1074Whitespaces->replaceWhitespace(1075*Child->First, /*Newlines=*/0, /*Spaces=*/1,1076/*StartOfTokenColumn=*/State.Column, /*IsAligned=*/false,1077State.Line->InPPDirective);1078}1079Penalty +=1080formatLine(*Child, State.Column + 1, /*FirstStartColumn=*/0, DryRun);1081if (!DryRun)1082markFinalized(Child->First);10831084State.Column += 1 + Child->Last->TotalLength;1085return true;1086}10871088ContinuationIndenter *Indenter;10891090private:1091WhitespaceManager *Whitespaces;1092const FormatStyle &Style;1093UnwrappedLineFormatter *BlockFormatter;1094};10951096/// Formatter that keeps the existing line breaks.1097class NoColumnLimitLineFormatter : public LineFormatter {1098public:1099NoColumnLimitLineFormatter(ContinuationIndenter *Indenter,1100WhitespaceManager *Whitespaces,1101const FormatStyle &Style,1102UnwrappedLineFormatter *BlockFormatter)1103: LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}11041105/// Formats the line, simply keeping all of the input's line breaking1106/// decisions.1107unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,1108unsigned FirstStartColumn, bool DryRun) override {1109assert(!DryRun);1110LineState State = Indenter->getInitialState(FirstIndent, FirstStartColumn,1111&Line, /*DryRun=*/false);1112while (State.NextToken) {1113bool Newline =1114Indenter->mustBreak(State) ||1115(Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);1116unsigned Penalty = 0;1117formatChildren(State, Newline, /*DryRun=*/false, Penalty);1118Indenter->addTokenToState(State, Newline, /*DryRun=*/false);1119}1120return 0;1121}1122};11231124/// Formatter that puts all tokens into a single line without breaks.1125class NoLineBreakFormatter : public LineFormatter {1126public:1127NoLineBreakFormatter(ContinuationIndenter *Indenter,1128WhitespaceManager *Whitespaces, const FormatStyle &Style,1129UnwrappedLineFormatter *BlockFormatter)1130: LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}11311132/// Puts all tokens into a single line.1133unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,1134unsigned FirstStartColumn, bool DryRun) override {1135unsigned Penalty = 0;1136LineState State =1137Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);1138while (State.NextToken) {1139formatChildren(State, /*NewLine=*/false, DryRun, Penalty);1140Indenter->addTokenToState(1141State, /*Newline=*/State.NextToken->MustBreakBefore, DryRun);1142}1143return Penalty;1144}1145};11461147/// Finds the best way to break lines.1148class OptimizingLineFormatter : public LineFormatter {1149public:1150OptimizingLineFormatter(ContinuationIndenter *Indenter,1151WhitespaceManager *Whitespaces,1152const FormatStyle &Style,1153UnwrappedLineFormatter *BlockFormatter)1154: LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}11551156/// Formats the line by finding the best line breaks with line lengths1157/// below the column limit.1158unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,1159unsigned FirstStartColumn, bool DryRun) override {1160LineState State =1161Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);11621163// If the ObjC method declaration does not fit on a line, we should format1164// it with one arg per line.1165if (State.Line->Type == LT_ObjCMethodDecl)1166State.Stack.back().BreakBeforeParameter = true;11671168// Find best solution in solution space.1169return analyzeSolutionSpace(State, DryRun);1170}11711172private:1173struct CompareLineStatePointers {1174bool operator()(LineState *obj1, LineState *obj2) const {1175return *obj1 < *obj2;1176}1177};11781179/// A pair of <penalty, count> that is used to prioritize the BFS on.1180///1181/// In case of equal penalties, we want to prefer states that were inserted1182/// first. During state generation we make sure that we insert states first1183/// that break the line as late as possible.1184typedef std::pair<unsigned, unsigned> OrderedPenalty;11851186/// An edge in the solution space from \c Previous->State to \c State,1187/// inserting a newline dependent on the \c NewLine.1188struct StateNode {1189StateNode(const LineState &State, bool NewLine, StateNode *Previous)1190: State(State), NewLine(NewLine), Previous(Previous) {}1191LineState State;1192bool NewLine;1193StateNode *Previous;1194};11951196/// An item in the prioritized BFS search queue. The \c StateNode's1197/// \c State has the given \c OrderedPenalty.1198typedef std::pair<OrderedPenalty, StateNode *> QueueItem;11991200/// The BFS queue type.1201typedef std::priority_queue<QueueItem, SmallVector<QueueItem>,1202std::greater<QueueItem>>1203QueueType;12041205/// Analyze the entire solution space starting from \p InitialState.1206///1207/// This implements a variant of Dijkstra's algorithm on the graph that spans1208/// the solution space (\c LineStates are the nodes). The algorithm tries to1209/// find the shortest path (the one with lowest penalty) from \p InitialState1210/// to a state where all tokens are placed. Returns the penalty.1211///1212/// If \p DryRun is \c false, directly applies the changes.1213unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun) {1214std::set<LineState *, CompareLineStatePointers> Seen;12151216// Increasing count of \c StateNode items we have created. This is used to1217// create a deterministic order independent of the container.1218unsigned Count = 0;1219QueueType Queue;12201221// Insert start element into queue.1222StateNode *RootNode =1223new (Allocator.Allocate()) StateNode(InitialState, false, nullptr);1224Queue.push(QueueItem(OrderedPenalty(0, Count), RootNode));1225++Count;12261227unsigned Penalty = 0;12281229// While not empty, take first element and follow edges.1230while (!Queue.empty()) {1231// Quit if we still haven't found a solution by now.1232if (Count > 25'000'000)1233return 0;12341235Penalty = Queue.top().first.first;1236StateNode *Node = Queue.top().second;1237if (!Node->State.NextToken) {1238LLVM_DEBUG(llvm::dbgs()1239<< "\n---\nPenalty for line: " << Penalty << "\n");1240break;1241}1242Queue.pop();12431244// Cut off the analysis of certain solutions if the analysis gets too1245// complex. See description of IgnoreStackForComparison.1246if (Count > 50'000)1247Node->State.IgnoreStackForComparison = true;12481249if (!Seen.insert(&Node->State).second) {1250// State already examined with lower penalty.1251continue;1252}12531254FormatDecision LastFormat = Node->State.NextToken->getDecision();1255if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)1256addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue);1257if (LastFormat == FD_Unformatted || LastFormat == FD_Break)1258addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue);1259}12601261if (Queue.empty()) {1262// We were unable to find a solution, do nothing.1263// FIXME: Add diagnostic?1264LLVM_DEBUG(llvm::dbgs() << "Could not find a solution.\n");1265return 0;1266}12671268// Reconstruct the solution.1269if (!DryRun)1270reconstructPath(InitialState, Queue.top().second);12711272LLVM_DEBUG(llvm::dbgs()1273<< "Total number of analyzed states: " << Count << "\n");1274LLVM_DEBUG(llvm::dbgs() << "---\n");12751276return Penalty;1277}12781279/// Add the following state to the analysis queue \c Queue.1280///1281/// Assume the current state is \p PreviousNode and has been reached with a1282/// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.1283void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,1284bool NewLine, unsigned *Count, QueueType *Queue) {1285if (NewLine && !Indenter->canBreak(PreviousNode->State))1286return;1287if (!NewLine && Indenter->mustBreak(PreviousNode->State))1288return;12891290StateNode *Node = new (Allocator.Allocate())1291StateNode(PreviousNode->State, NewLine, PreviousNode);1292if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))1293return;12941295Penalty += Indenter->addTokenToState(Node->State, NewLine, true);12961297Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node));1298++(*Count);1299}13001301/// Applies the best formatting by reconstructing the path in the1302/// solution space that leads to \c Best.1303void reconstructPath(LineState &State, StateNode *Best) {1304llvm::SmallVector<StateNode *> Path;1305// We do not need a break before the initial token.1306while (Best->Previous) {1307Path.push_back(Best);1308Best = Best->Previous;1309}1310for (const auto &Node : llvm::reverse(Path)) {1311unsigned Penalty = 0;1312formatChildren(State, Node->NewLine, /*DryRun=*/false, Penalty);1313Penalty += Indenter->addTokenToState(State, Node->NewLine, false);13141315LLVM_DEBUG({1316printLineState(Node->Previous->State);1317if (Node->NewLine) {1318llvm::dbgs() << "Penalty for placing "1319<< Node->Previous->State.NextToken->Tok.getName()1320<< " on a new line: " << Penalty << "\n";1321}1322});1323}1324}13251326llvm::SpecificBumpPtrAllocator<StateNode> Allocator;1327};13281329} // anonymous namespace13301331unsigned UnwrappedLineFormatter::format(1332const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,1333int AdditionalIndent, bool FixBadIndentation, unsigned FirstStartColumn,1334unsigned NextStartColumn, unsigned LastStartColumn) {1335LineJoiner Joiner(Style, Keywords, Lines);13361337// Try to look up already computed penalty in DryRun-mode.1338std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey(1339&Lines, AdditionalIndent);1340auto CacheIt = PenaltyCache.find(CacheKey);1341if (DryRun && CacheIt != PenaltyCache.end())1342return CacheIt->second;13431344assert(!Lines.empty());1345unsigned Penalty = 0;1346LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,1347AdditionalIndent);1348const AnnotatedLine *PrevPrevLine = nullptr;1349const AnnotatedLine *PreviousLine = nullptr;1350const AnnotatedLine *NextLine = nullptr;13511352// The minimum level of consecutive lines that have been formatted.1353unsigned RangeMinLevel = UINT_MAX;13541355bool FirstLine = true;1356for (const AnnotatedLine *Line =1357Joiner.getNextMergedLine(DryRun, IndentTracker);1358Line; PrevPrevLine = PreviousLine, PreviousLine = Line, Line = NextLine,1359FirstLine = false) {1360assert(Line->First);1361const AnnotatedLine &TheLine = *Line;1362unsigned Indent = IndentTracker.getIndent();13631364// We continue formatting unchanged lines to adjust their indent, e.g. if a1365// scope was added. However, we need to carefully stop doing this when we1366// exit the scope of affected lines to prevent indenting the entire1367// remaining file if it currently missing a closing brace.1368bool PreviousRBrace =1369PreviousLine && PreviousLine->startsWith(tok::r_brace);1370bool ContinueFormatting =1371TheLine.Level > RangeMinLevel ||1372(TheLine.Level == RangeMinLevel && !PreviousRBrace &&1373!TheLine.startsWith(tok::r_brace));13741375bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&1376Indent != TheLine.First->OriginalColumn;1377bool ShouldFormat = TheLine.Affected || FixIndentation;1378// We cannot format this line; if the reason is that the line had a1379// parsing error, remember that.1380if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {1381Status->FormatComplete = false;1382Status->Line =1383SourceMgr.getSpellingLineNumber(TheLine.First->Tok.getLocation());1384}13851386if (ShouldFormat && TheLine.Type != LT_Invalid) {1387if (!DryRun) {1388bool LastLine = TheLine.First->is(tok::eof);1389formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines, Indent,1390LastLine ? LastStartColumn : NextStartColumn + Indent);1391}13921393NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);1394unsigned ColumnLimit = getColumnLimit(TheLine.InPPDirective, NextLine);1395bool FitsIntoOneLine =1396!TheLine.ContainsMacroCall &&1397(TheLine.Last->TotalLength + Indent <= ColumnLimit ||1398(TheLine.Type == LT_ImportStatement &&1399(!Style.isJavaScript() || !Style.JavaScriptWrapImports)) ||1400(Style.isCSharp() &&1401TheLine.InPPDirective)); // don't split #regions in C#1402if (Style.ColumnLimit == 0) {1403NoColumnLimitLineFormatter(Indenter, Whitespaces, Style, this)1404.formatLine(TheLine, NextStartColumn + Indent,1405FirstLine ? FirstStartColumn : 0, DryRun);1406} else if (FitsIntoOneLine) {1407Penalty += NoLineBreakFormatter(Indenter, Whitespaces, Style, this)1408.formatLine(TheLine, NextStartColumn + Indent,1409FirstLine ? FirstStartColumn : 0, DryRun);1410} else {1411Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)1412.formatLine(TheLine, NextStartColumn + Indent,1413FirstLine ? FirstStartColumn : 0, DryRun);1414}1415RangeMinLevel = std::min(RangeMinLevel, TheLine.Level);1416} else {1417// If no token in the current line is affected, we still need to format1418// affected children.1419if (TheLine.ChildrenAffected) {1420for (const FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next)1421if (!Tok->Children.empty())1422format(Tok->Children, DryRun);1423}14241425// Adapt following lines on the current indent level to the same level1426// unless the current \c AnnotatedLine is not at the beginning of a line.1427bool StartsNewLine =1428TheLine.First->NewlinesBefore > 0 || TheLine.First->IsFirst;1429if (StartsNewLine)1430IndentTracker.adjustToUnmodifiedLine(TheLine);1431if (!DryRun) {1432bool ReformatLeadingWhitespace =1433StartsNewLine && ((PreviousLine && PreviousLine->Affected) ||1434TheLine.LeadingEmptyLinesAffected);1435// Format the first token.1436if (ReformatLeadingWhitespace) {1437formatFirstToken(TheLine, PreviousLine, PrevPrevLine, Lines,1438TheLine.First->OriginalColumn,1439TheLine.First->OriginalColumn);1440} else {1441Whitespaces->addUntouchableToken(*TheLine.First,1442TheLine.InPPDirective);1443}14441445// Notify the WhitespaceManager about the unchanged whitespace.1446for (FormatToken *Tok = TheLine.First->Next; Tok; Tok = Tok->Next)1447Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);1448}1449NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);1450RangeMinLevel = UINT_MAX;1451}1452if (!DryRun)1453markFinalized(TheLine.First);1454}1455PenaltyCache[CacheKey] = Penalty;1456return Penalty;1457}14581459static auto computeNewlines(const AnnotatedLine &Line,1460const AnnotatedLine *PreviousLine,1461const AnnotatedLine *PrevPrevLine,1462const SmallVectorImpl<AnnotatedLine *> &Lines,1463const FormatStyle &Style) {1464const auto &RootToken = *Line.First;1465auto Newlines =1466std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);1467// Remove empty lines before "}" where applicable.1468if (RootToken.is(tok::r_brace) &&1469(!RootToken.Next ||1470(RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&1471// Do not remove empty lines before namespace closing "}".1472!getNamespaceToken(&Line, Lines)) {1473Newlines = std::min(Newlines, 1u);1474}1475// Remove empty lines at the start of nested blocks (lambdas/arrow functions)1476if (!PreviousLine && Line.Level > 0)1477Newlines = std::min(Newlines, 1u);1478if (Newlines == 0 && !RootToken.IsFirst)1479Newlines = 1;1480if (RootToken.IsFirst &&1481(!Style.KeepEmptyLines.AtStartOfFile || !RootToken.HasUnescapedNewline)) {1482Newlines = 0;1483}14841485// Remove empty lines after "{".1486if (!Style.KeepEmptyLines.AtStartOfBlock && PreviousLine &&1487PreviousLine->Last->is(tok::l_brace) &&1488!PreviousLine->startsWithNamespace() &&1489!(PrevPrevLine && PrevPrevLine->startsWithNamespace() &&1490PreviousLine->startsWith(tok::l_brace)) &&1491!startsExternCBlock(*PreviousLine)) {1492Newlines = 1;1493}14941495// Insert or remove empty line before access specifiers.1496if (PreviousLine && RootToken.isAccessSpecifier()) {1497switch (Style.EmptyLineBeforeAccessModifier) {1498case FormatStyle::ELBAMS_Never:1499if (Newlines > 1)1500Newlines = 1;1501break;1502case FormatStyle::ELBAMS_Leave:1503Newlines = std::max(RootToken.NewlinesBefore, 1u);1504break;1505case FormatStyle::ELBAMS_LogicalBlock:1506if (PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) && Newlines <= 1)1507Newlines = 2;1508if (PreviousLine->First->isAccessSpecifier())1509Newlines = 1; // Previous is an access modifier remove all new lines.1510break;1511case FormatStyle::ELBAMS_Always: {1512const FormatToken *previousToken;1513if (PreviousLine->Last->is(tok::comment))1514previousToken = PreviousLine->Last->getPreviousNonComment();1515else1516previousToken = PreviousLine->Last;1517if ((!previousToken || previousToken->isNot(tok::l_brace)) &&1518Newlines <= 1) {1519Newlines = 2;1520}1521} break;1522}1523}15241525// Insert or remove empty line after access specifiers.1526if (PreviousLine && PreviousLine->First->isAccessSpecifier() &&1527(!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline)) {1528// EmptyLineBeforeAccessModifier is handling the case when two access1529// modifiers follow each other.1530if (!RootToken.isAccessSpecifier()) {1531switch (Style.EmptyLineAfterAccessModifier) {1532case FormatStyle::ELAAMS_Never:1533Newlines = 1;1534break;1535case FormatStyle::ELAAMS_Leave:1536Newlines = std::max(Newlines, 1u);1537break;1538case FormatStyle::ELAAMS_Always:1539if (RootToken.is(tok::r_brace)) // Do not add at end of class.1540Newlines = 1u;1541else1542Newlines = std::max(Newlines, 2u);1543break;1544}1545}1546}15471548return Newlines;1549}15501551void UnwrappedLineFormatter::formatFirstToken(1552const AnnotatedLine &Line, const AnnotatedLine *PreviousLine,1553const AnnotatedLine *PrevPrevLine,1554const SmallVectorImpl<AnnotatedLine *> &Lines, unsigned Indent,1555unsigned NewlineIndent) {1556FormatToken &RootToken = *Line.First;1557if (RootToken.is(tok::eof)) {1558unsigned Newlines = std::min(1559RootToken.NewlinesBefore,1560Style.KeepEmptyLines.AtEndOfFile ? Style.MaxEmptyLinesToKeep + 1 : 1);1561unsigned TokenIndent = Newlines ? NewlineIndent : 0;1562Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,1563TokenIndent);1564return;1565}15661567if (RootToken.Newlines < 0) {1568RootToken.Newlines =1569computeNewlines(Line, PreviousLine, PrevPrevLine, Lines, Style);1570assert(RootToken.Newlines >= 0);1571}15721573if (RootToken.Newlines > 0)1574Indent = NewlineIndent;15751576// Preprocessor directives get indented before the hash only if specified. In1577// Javascript import statements are indented like normal statements.1578if (!Style.isJavaScript() &&1579Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&1580(Line.Type == LT_PreprocessorDirective ||1581Line.Type == LT_ImportStatement)) {1582Indent = 0;1583}15841585Whitespaces->replaceWhitespace(RootToken, RootToken.Newlines, Indent, Indent,1586/*IsAligned=*/false,1587Line.InPPDirective &&1588!RootToken.HasUnescapedNewline);1589}15901591unsigned1592UnwrappedLineFormatter::getColumnLimit(bool InPPDirective,1593const AnnotatedLine *NextLine) const {1594// In preprocessor directives reserve two chars for trailing " \" if the1595// next line continues the preprocessor directive.1596bool ContinuesPPDirective =1597InPPDirective &&1598// If there is no next line, this is likely a child line and the parent1599// continues the preprocessor directive.1600(!NextLine ||1601(NextLine->InPPDirective &&1602// If there is an unescaped newline between this line and the next, the1603// next line starts a new preprocessor directive.1604!NextLine->First->HasUnescapedNewline));1605return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0);1606}16071608} // namespace format1609} // namespace clang161016111612