[PATCH] D129742: [clang-format] Fix invalid-code-generation by RemoveBracesLLVM
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 14 15:20:12 PDT 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG6ab7307177c3: [clang-format] Fix invalid-code-generation by RemoveBracesLLVM (authored by owenpan).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D129742/new/
https://reviews.llvm.org/D129742
Files:
clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -25741,6 +25741,17 @@
"}",
Style);
+ verifyFormat("if (a) {\n"
+ " if (b)\n"
+ " c = 1; // comment\n"
+ "}",
+ "if (a) {\n"
+ " if (b) {\n"
+ " c = 1; // comment\n"
+ " }\n"
+ "}",
+ Style);
+
verifyFormat("if (a) {\n"
"Label:\n"
"}",
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1895,26 +1895,31 @@
void removeBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
tooling::Replacements &Result) {
const auto &SourceMgr = Env.getSourceManager();
+ bool EndsWithComment = false;
for (AnnotatedLine *Line : Lines) {
removeBraces(Line->Children, Result);
- if (!Line->Affected)
- continue;
- 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->Next || Token == Line->Last);
- const auto Start =
- Token == Line->Last ||
- (Token->Next->isOneOf(tok::kw_else, tok::comment) &&
- Token->Next->NewlinesBefore > 0)
- ? Token->WhitespaceRange.getBegin()
- : Token->Tok.getLocation();
- const auto Range =
- CharSourceRange::getCharRange(Start, Token->Tok.getEndLoc());
- cantFail(Result.add(tooling::Replacement(SourceMgr, Range, "")));
+ 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, "")));
+ }
}
+ assert(Line->Last);
+ EndsWithComment = Line->Last->is(tok::comment);
}
}
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129742.444812.patch
Type: text/x-patch
Size: 2961 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220714/43a1bf18/attachment.bin>
More information about the cfe-commits
mailing list