[clang] 44a06b5 - [clang-format] Rework removeBraces() in Format.cpp
via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 27 14:10:33 PDT 2022
Author: owenca
Date: 2022-08-27T14:10:24-07:00
New Revision: 44a06b51b2ce2c75ec327902b7ddd1172e54bc8f
URL: https://github.com/llvm/llvm-project/commit/44a06b51b2ce2c75ec327902b7ddd1172e54bc8f
DIFF: https://github.com/llvm/llvm-project/commit/44a06b51b2ce2c75ec327902b7ddd1172e54bc8f.diff
LOG: [clang-format] Rework removeBraces() in Format.cpp
Fixes #57373.
Differential Revision: https://reviews.llvm.org/D132719
Added:
Modified:
clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index fd6a1799578d8..c2c020ea7ca85 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1902,31 +1902,30 @@ class BracesRemover : public TokenAnalyzer {
void removeBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
tooling::Replacements &Result) {
const auto &SourceMgr = Env.getSourceManager();
- bool EndsWithComment = false;
- for (AnnotatedLine *Line : Lines) {
+ const auto End = Lines.end();
+ for (auto I = Lines.begin(); I != End; ++I) {
+ const auto Line = *I;
removeBraces(Line->Children, Result);
- if (Line->Affected) {
- for (FormatToken *Token = Line->First; Token && !Token->Finalized;
- Token = Token->Next) {
- if (!Token->Optional)
- continue;
- assert(Token->isOneOf(tok::l_brace, tok::r_brace));
- assert(Token->Previous || Token == Line->First);
- const FormatToken *Next = Token->Next;
- assert(Next || Token == Line->Last);
- const auto Start =
- (!Token->Previous && EndsWithComment) ||
- (Next && !(Next->isOneOf(tok::kw_else, tok::comment) &&
- Next->NewlinesBefore > 0))
- ? Token->Tok.getLocation()
- : Token->WhitespaceRange.getBegin();
- const auto Range =
- CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
- cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
- }
+ if (!Line->Affected)
+ continue;
+ const auto NextLine = I + 1 == End ? nullptr : I[1];
+ for (auto Token = Line->First; Token && !Token->Finalized;
+ Token = Token->Next) {
+ if (!Token->Optional)
+ continue;
+ assert(Token->isOneOf(tok::l_brace, tok::r_brace));
+ auto Next = Token->Next;
+ assert(Next || Token == Line->Last);
+ if (!Next && NextLine)
+ Next = NextLine->First;
+ const auto Start =
+ Next && Next->NewlinesBefore == 0 && Next->isNot(tok::eof)
+ ? Token->Tok.getLocation()
+ : Token->WhitespaceRange.getBegin();
+ const auto Range =
+ CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
+ cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
}
- assert(Line->Last);
- EndsWithComment = Line->Last->is(tok::comment);
}
}
};
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4879b0e06bd38..6abe155c388da 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -26005,6 +26005,14 @@ TEST_F(FormatTest, RemoveBraces) {
"}",
Style);
+ verifyFormat("if (a) // comment\n"
+ " b = 1;",
+ "if (a) // comment\n"
+ "{\n"
+ " b = 1;\n"
+ "}",
+ Style);
+
Style.ColumnLimit = 20;
verifyFormat("int ab = [](int i) {\n"
More information about the cfe-commits
mailing list