summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Kurdej <marek.kurdej+llvm.org@gmail.com>2022-02-13 21:56:37 +0100
committerTom Stellard <tstellar@redhat.com>2022-06-09 13:32:06 -0700
commitd0cd5a872f8d0d3720d2428dd420ae9177e6a8f1 (patch)
treee9485e4e9db3a6fd59bc06dcd9431c436f0af323
parent3cd9df8443f8d5bd308e2417a77bebeef5264327 (diff)
downloadllvm-d0cd5a872f8d0d3720d2428dd420ae9177e6a8f1.tar.gz
[clang-format] Fix SpacesInLineCommentPrefix deleting tokens.
Fixes https://github.com/llvm/llvm-project/issues/53799. Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D119680
-rw-r--r--clang/lib/Format/BreakableToken.cpp19
-rw-r--r--clang/unittests/Format/FormatTestComments.cpp5
2 files changed, 14 insertions, 10 deletions
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index f68d802c1f95..1ffaa7f6f45b 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -753,6 +753,7 @@ BreakableLineCommentSection::BreakableLineCommentSection(
assert(Tok.is(TT_LineComment) &&
"line comment section must start with a line comment");
FormatToken *LineTok = nullptr;
+ const int Minimum = Style.SpacesInLineCommentPrefix.Minimum;
// How many spaces we changed in the first line of the section, this will be
// applied in all following lines
int FirstLineSpaceChange = 0;
@@ -775,7 +776,7 @@ BreakableLineCommentSection::BreakableLineCommentSection(
Lines[i] = Lines[i].ltrim(Blanks);
StringRef IndentPrefix = getLineCommentIndentPrefix(Lines[i], Style);
OriginalPrefix[i] = IndentPrefix;
- const unsigned SpacesInPrefix = llvm::count(IndentPrefix, ' ');
+ const int SpacesInPrefix = llvm::count(IndentPrefix, ' ');
// On the first line of the comment section we calculate how many spaces
// are to be added or removed, all lines after that just get only the
@@ -784,12 +785,11 @@ BreakableLineCommentSection::BreakableLineCommentSection(
// e.g. from "///" to "//".
if (i == 0 || OriginalPrefix[i].rtrim(Blanks) !=
OriginalPrefix[i - 1].rtrim(Blanks)) {
- if (SpacesInPrefix < Style.SpacesInLineCommentPrefix.Minimum &&
- Lines[i].size() > IndentPrefix.size() &&
+ if (SpacesInPrefix < Minimum && Lines[i].size() > IndentPrefix.size() &&
isAlphanumeric(Lines[i][IndentPrefix.size()])) {
- FirstLineSpaceChange =
- Style.SpacesInLineCommentPrefix.Minimum - SpacesInPrefix;
- } else if (SpacesInPrefix > Style.SpacesInLineCommentPrefix.Maximum) {
+ FirstLineSpaceChange = Minimum - SpacesInPrefix;
+ } else if (static_cast<unsigned>(SpacesInPrefix) >
+ Style.SpacesInLineCommentPrefix.Maximum) {
FirstLineSpaceChange =
Style.SpacesInLineCommentPrefix.Maximum - SpacesInPrefix;
} else {
@@ -800,10 +800,9 @@ BreakableLineCommentSection::BreakableLineCommentSection(
if (Lines[i].size() != IndentPrefix.size()) {
PrefixSpaceChange[i] = FirstLineSpaceChange;
- if (SpacesInPrefix + PrefixSpaceChange[i] <
- Style.SpacesInLineCommentPrefix.Minimum) {
- PrefixSpaceChange[i] += Style.SpacesInLineCommentPrefix.Minimum -
- (SpacesInPrefix + PrefixSpaceChange[i]);
+ if (SpacesInPrefix + PrefixSpaceChange[i] < Minimum) {
+ PrefixSpaceChange[i] +=
+ Minimum - (SpacesInPrefix + PrefixSpaceChange[i]);
}
assert(Lines[i].size() > IndentPrefix.size());
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index b487440a06a3..33c8cfdb0829 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -3597,6 +3597,11 @@ TEST_F(FormatTestComments, SpaceAtLineCommentBegin) {
" // World\n"
"}",
format(WrapCode, Style));
+ EXPECT_EQ("// x\n"
+ "// y",
+ format("// x\n"
+ "// y",
+ Style));
Style.SpacesInLineCommentPrefix = {3, 3};
EXPECT_EQ("// Lorem ipsum\n"