[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 01:30:28 PDT 2022
owenpan created this revision.
owenpan added reviewers: HazardyKnusperkeks, curdeius, MyDeveloperDay.
owenpan added a project: clang-format.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
When removing an r_brace that is the first token of an annotated line, if the
line above ends with a line comment, clang-format generates invalid code by
merging the tokens after the r_brace into the line comment.
Fixes https://github.com/llvm/llvm-project/issues/56488.
Repository:
rG LLVM Github Monorepo
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.444558.patch
Type: text/x-patch
Size: 2961 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220714/c8672fd9/attachment.bin>
More information about the cfe-commits
mailing list