[PATCH] D118475: [clang-format] Fix misaligned trailing comments in the presence of an empty block comment.
Marek Kurdej via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 28 07:50:00 PST 2022
curdeius created this revision.
curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan.
curdeius requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Fixes https://github.com/llvm/llvm-project/issues/53441.
Expected code:
/**/ //
int a; //
was before misformatted to:
/**/ //
int a; //
Because the "remaining length" (after the starting `/*`) of an empty block comment `/**/` was computed to be 0 instead of 2.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D118475
Files:
clang/lib/Format/BreakableToken.cpp
clang/unittests/Format/FormatTestComments.cpp
Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -2842,6 +2842,12 @@
"#define FOO_NODELOCAL 4 // Loopback\n\n"
"} // namespace m\n",
getLLVMStyleWithColumns(80)));
+
+ // https://llvm.org/PR53441
+ verifyFormat("/* */ //\n"
+ "int a; //\n");
+ verifyFormat("/**/ //\n"
+ "int a; //\n");
}
TEST_F(FormatTestComments, AlignsBlockCommentDecorations) {
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -555,7 +555,9 @@
// We never need a decoration when breaking just the trailing "*/" postfix.
bool HasRemainingText = Offset < Content[LineIndex].size();
if (!HasRemainingText) {
- LineLength -= Decoration.size();
+ bool HasDecoration = Lines[LineIndex].ltrim().startswith(Decoration);
+ if (HasDecoration)
+ LineLength -= Decoration.size();
}
}
return LineLength;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118475.404024.patch
Type: text/x-patch
Size: 1211 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220128/d63729a4/attachment.bin>
More information about the cfe-commits
mailing list