forked from OSchip/llvm-project
[clang-format] Don't remove braces if a 1-statement body would wrap
Reimplement the RemoveBracesLLVM feature which handles a single-statement block that would get wrapped. Fixes #53543. Differential Revision: https://reviews.llvm.org/D125137
This commit is contained in:
parent
50f846d634
commit
b6d8c84f28
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
#include "UnwrappedLineParser.h"
|
#include "UnwrappedLineParser.h"
|
||||||
#include "FormatToken.h"
|
#include "FormatToken.h"
|
||||||
|
#include "TokenAnnotator.h"
|
||||||
#include "llvm/ADT/STLExtras.h"
|
#include "llvm/ADT/STLExtras.h"
|
||||||
#include "llvm/Support/Debug.h"
|
#include "llvm/Support/Debug.h"
|
||||||
#include "llvm/Support/raw_ostream.h"
|
#include "llvm/Support/raw_ostream.h"
|
||||||
|
@ -460,6 +461,7 @@ bool UnwrappedLineParser::precededByCommentOrPPDirective() const {
|
||||||
return Previous && Previous->is(tok::comment) &&
|
return Previous && Previous->is(tok::comment) &&
|
||||||
(Previous->IsMultiline || Previous->NewlinesBefore > 0);
|
(Previous->IsMultiline || Previous->NewlinesBefore > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Parses a level, that is ???.
|
/// \brief Parses a level, that is ???.
|
||||||
/// \param HasOpeningBrace If that level is started by an opening brace.
|
/// \param HasOpeningBrace If that level is started by an opening brace.
|
||||||
/// \param CanContainBracedList If the content can contain (at any level) a
|
/// \param CanContainBracedList If the content can contain (at any level) a
|
||||||
|
@ -751,6 +753,50 @@ size_t UnwrappedLineParser::computePPHash() const {
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Checks whether \p ParsedLine might fit on a single line. We must clone the
|
||||||
|
// tokens of \p ParsedLine before running the token annotator on it so that we
|
||||||
|
// can restore them afterward.
|
||||||
|
bool UnwrappedLineParser::mightFitOnOneLine(UnwrappedLine &ParsedLine) const {
|
||||||
|
const auto ColumnLimit = Style.ColumnLimit;
|
||||||
|
if (ColumnLimit == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
auto &Tokens = ParsedLine.Tokens;
|
||||||
|
assert(!Tokens.empty());
|
||||||
|
const auto *LastToken = Tokens.back().Tok;
|
||||||
|
assert(LastToken);
|
||||||
|
|
||||||
|
SmallVector<UnwrappedLineNode> SavedTokens(Tokens.size());
|
||||||
|
|
||||||
|
int Index = 0;
|
||||||
|
for (const auto &Token : Tokens) {
|
||||||
|
assert(Token.Tok);
|
||||||
|
auto &SavedToken = SavedTokens[Index++];
|
||||||
|
SavedToken.Tok = new FormatToken;
|
||||||
|
SavedToken.Tok->copyFrom(*Token.Tok);
|
||||||
|
SavedToken.Children = std::move(Token.Children);
|
||||||
|
}
|
||||||
|
|
||||||
|
AnnotatedLine Line(ParsedLine);
|
||||||
|
assert(Line.Last == LastToken);
|
||||||
|
|
||||||
|
TokenAnnotator Annotator(Style, Keywords);
|
||||||
|
Annotator.annotate(Line);
|
||||||
|
Annotator.calculateFormattingInformation(Line);
|
||||||
|
|
||||||
|
const int Length = LastToken->TotalLength;
|
||||||
|
|
||||||
|
Index = 0;
|
||||||
|
for (auto &Token : Tokens) {
|
||||||
|
const auto &SavedToken = SavedTokens[Index++];
|
||||||
|
Token.Tok->copyFrom(*SavedToken.Tok);
|
||||||
|
Token.Children = std::move(SavedToken.Children);
|
||||||
|
delete SavedToken.Tok;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Line.Level * Style.IndentWidth + Length <= ColumnLimit;
|
||||||
|
}
|
||||||
|
|
||||||
UnwrappedLineParser::IfStmtKind
|
UnwrappedLineParser::IfStmtKind
|
||||||
UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels,
|
UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels,
|
||||||
bool MunchSemi, bool UnindentWhitesmithsBraces,
|
bool MunchSemi, bool UnindentWhitesmithsBraces,
|
||||||
|
@ -813,8 +859,11 @@ UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels,
|
||||||
const FormatToken *Previous = Tokens->getPreviousToken();
|
const FormatToken *Previous = Tokens->getPreviousToken();
|
||||||
assert(Previous);
|
assert(Previous);
|
||||||
if (Previous->isNot(tok::r_brace) || Previous->Optional) {
|
if (Previous->isNot(tok::r_brace) || Previous->Optional) {
|
||||||
Tok->MatchingParen = FormatTok;
|
assert(!CurrentLines->empty());
|
||||||
FormatTok->MatchingParen = Tok;
|
if (mightFitOnOneLine(CurrentLines->back())) {
|
||||||
|
Tok->MatchingParen = FormatTok;
|
||||||
|
FormatTok->MatchingParen = Tok;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,6 +95,7 @@ private:
|
||||||
bool parseLevel(bool HasOpeningBrace, bool CanContainBracedList,
|
bool parseLevel(bool HasOpeningBrace, bool CanContainBracedList,
|
||||||
IfStmtKind *IfKind = nullptr,
|
IfStmtKind *IfKind = nullptr,
|
||||||
TokenType NextLBracesType = TT_Unknown);
|
TokenType NextLBracesType = TT_Unknown);
|
||||||
|
bool mightFitOnOneLine(UnwrappedLine &Line) const;
|
||||||
IfStmtKind parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u,
|
IfStmtKind parseBlock(bool MustBeDeclaration = false, unsigned AddLevels = 1u,
|
||||||
bool MunchSemi = true,
|
bool MunchSemi = true,
|
||||||
bool UnindentWhitesmithsBraces = false,
|
bool UnindentWhitesmithsBraces = false,
|
||||||
|
|
|
@ -25365,8 +25365,6 @@ TEST_F(FormatTest, RemoveBraces) {
|
||||||
"}",
|
"}",
|
||||||
Style);
|
Style);
|
||||||
|
|
||||||
// FIXME: See https://github.com/llvm/llvm-project/issues/53543.
|
|
||||||
#if 0
|
|
||||||
Style.ColumnLimit = 65;
|
Style.ColumnLimit = 65;
|
||||||
|
|
||||||
verifyFormat("if (condition) {\n"
|
verifyFormat("if (condition) {\n"
|
||||||
|
@ -25380,6 +25378,15 @@ TEST_F(FormatTest, RemoveBraces) {
|
||||||
|
|
||||||
Style.ColumnLimit = 20;
|
Style.ColumnLimit = 20;
|
||||||
|
|
||||||
|
verifyFormat("int ab = [](int i) {\n"
|
||||||
|
" if (i > 0) {\n"
|
||||||
|
" i = 12345678 -\n"
|
||||||
|
" i;\n"
|
||||||
|
" }\n"
|
||||||
|
" return i;\n"
|
||||||
|
"};",
|
||||||
|
Style);
|
||||||
|
|
||||||
verifyFormat("if (a) {\n"
|
verifyFormat("if (a) {\n"
|
||||||
" b = c + // 1 -\n"
|
" b = c + // 1 -\n"
|
||||||
" d;\n"
|
" d;\n"
|
||||||
|
@ -25394,9 +25401,6 @@ TEST_F(FormatTest, RemoveBraces) {
|
||||||
" b = c >= 0 ? d : e;\n"
|
" b = c >= 0 ? d : e;\n"
|
||||||
"}",
|
"}",
|
||||||
Style);
|
Style);
|
||||||
#endif
|
|
||||||
|
|
||||||
Style.ColumnLimit = 20;
|
|
||||||
|
|
||||||
verifyFormat("if (a)\n"
|
verifyFormat("if (a)\n"
|
||||||
" b = c > 0 ? d : e;",
|
" b = c > 0 ? d : e;",
|
||||||
|
|
Loading…
Reference in New Issue