diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -92,7 +92,6 @@ void reset(); void parseFile(); bool precededByCommentOrPPDirective() const; - bool mightFitOnOneLine() const; bool parseLevel(bool HasOpeningBrace, IfStmtKind *IfKind = nullptr); IfStmtKind parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u, bool MunchSemi = true, diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -14,7 +14,6 @@ #include "UnwrappedLineParser.h" #include "FormatToken.h" -#include "TokenAnnotator.h" #include "llvm/ADT/STLExtras.h" #include "llvm/Support/Debug.h" #include "llvm/Support/raw_ostream.h" @@ -438,34 +437,8 @@ (Previous->IsMultiline || Previous->NewlinesBefore > 0); } -bool UnwrappedLineParser::mightFitOnOneLine() const { - const auto ColumnLimit = Style.ColumnLimit; - if (ColumnLimit == 0) - return true; - - if (Lines.empty()) - return true; - - const auto &PreviousLine = Lines.back(); - const auto &Tokens = PreviousLine.Tokens; - assert(!Tokens.empty()); - const auto *LastToken = Tokens.back().Tok; - assert(LastToken); - if (!LastToken->isOneOf(tok::semi, tok::comment)) - return true; - - AnnotatedLine Line(PreviousLine); - assert(Line.Last == LastToken); - - TokenAnnotator Annotator(Style, Keywords); - Annotator.annotate(Line); - Annotator.calculateFormattingInformation(Line); - - return Line.Level * Style.IndentWidth + LastToken->TotalLength <= ColumnLimit; -} - // Returns true if a simple block, or false otherwise. (A simple block has a -// single statement that fits on a single line.) +// single statement.) bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace, IfStmtKind *IfKind) { const bool IsPrecededByCommentOrPPDirective = !Style.RemoveBracesLLVM || precededByCommentOrPPDirective(); @@ -502,9 +475,7 @@ precededByCommentOrPPDirective()) return false; const FormatToken *Next = Tokens->peekNextToken(); - if (Next->is(tok::comment) && Next->NewlinesBefore == 0) - return false; - return mightFitOnOneLine(); + return Next->isNot(tok::comment) || Next->NewlinesBefore > 0; } nextToken(); addUnwrappedLine(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -23965,6 +23965,19 @@ "};", Style); + // FIXME: See https://github.com/llvm/llvm-project/issues/53543. +#if 0 + Style.ColumnLimit = 65; + + verifyFormat("if (condition) {\n" + " ff(Indices,\n" + " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" + "} else {\n" + " ff(Indices,\n" + " [&](unsigned LHSI, unsigned RHSI) { return true; });\n" + "}", + Style); + Style.ColumnLimit = 20; verifyFormat("if (a) {\n" @@ -23981,6 +23994,9 @@ " b = c >= 0 ? d : e;\n" "}", Style); +#endif + + Style.ColumnLimit = 20; verifyFormat("if (a)\n" " b = c > 0 ? d : e;",