[clang-format] Rework removeBraces() in Format.cpp
Fixes #57373. Differential Revision: https://reviews.llvm.org/D132719
This commit is contained in:
parent
7743badafa
commit
44a06b51b2
|
@ -1902,31 +1902,30 @@ private:
|
||||||
void removeBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
|
void removeBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
|
||||||
tooling::Replacements &Result) {
|
tooling::Replacements &Result) {
|
||||||
const auto &SourceMgr = Env.getSourceManager();
|
const auto &SourceMgr = Env.getSourceManager();
|
||||||
bool EndsWithComment = false;
|
const auto End = Lines.end();
|
||||||
for (AnnotatedLine *Line : Lines) {
|
for (auto I = Lines.begin(); I != End; ++I) {
|
||||||
|
const auto Line = *I;
|
||||||
removeBraces(Line->Children, Result);
|
removeBraces(Line->Children, Result);
|
||||||
if (Line->Affected) {
|
if (!Line->Affected)
|
||||||
for (FormatToken *Token = Line->First; Token && !Token->Finalized;
|
continue;
|
||||||
Token = Token->Next) {
|
const auto NextLine = I + 1 == End ? nullptr : I[1];
|
||||||
if (!Token->Optional)
|
for (auto Token = Line->First; Token && !Token->Finalized;
|
||||||
continue;
|
Token = Token->Next) {
|
||||||
assert(Token->isOneOf(tok::l_brace, tok::r_brace));
|
if (!Token->Optional)
|
||||||
assert(Token->Previous || Token == Line->First);
|
continue;
|
||||||
const FormatToken *Next = Token->Next;
|
assert(Token->isOneOf(tok::l_brace, tok::r_brace));
|
||||||
assert(Next || Token == Line->Last);
|
auto Next = Token->Next;
|
||||||
const auto Start =
|
assert(Next || Token == Line->Last);
|
||||||
(!Token->Previous && EndsWithComment) ||
|
if (!Next && NextLine)
|
||||||
(Next && !(Next->isOneOf(tok::kw_else, tok::comment) &&
|
Next = NextLine->First;
|
||||||
Next->NewlinesBefore > 0))
|
const auto Start =
|
||||||
? Token->Tok.getLocation()
|
Next && Next->NewlinesBefore == 0 && Next->isNot(tok::eof)
|
||||||
: Token->WhitespaceRange.getBegin();
|
? Token->Tok.getLocation()
|
||||||
const auto Range =
|
: Token->WhitespaceRange.getBegin();
|
||||||
CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
|
const auto Range =
|
||||||
cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
|
CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
|
||||||
}
|
cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
|
||||||
}
|
}
|
||||||
assert(Line->Last);
|
|
||||||
EndsWithComment = Line->Last->is(tok::comment);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -26005,6 +26005,14 @@ TEST_F(FormatTest, RemoveBraces) {
|
||||||
"}",
|
"}",
|
||||||
Style);
|
Style);
|
||||||
|
|
||||||
|
verifyFormat("if (a) // comment\n"
|
||||||
|
" b = 1;",
|
||||||
|
"if (a) // comment\n"
|
||||||
|
"{\n"
|
||||||
|
" b = 1;\n"
|
||||||
|
"}",
|
||||||
|
Style);
|
||||||
|
|
||||||
Style.ColumnLimit = 20;
|
Style.ColumnLimit = 20;
|
||||||
|
|
||||||
verifyFormat("int ab = [](int i) {\n"
|
verifyFormat("int ab = [](int i) {\n"
|
||||||
|
|
Loading…
Reference in New Issue