Skip to content

Commit d0cd5a8

Browse files
mkurdejtstellar
authored andcommitted
[clang-format] Fix SpacesInLineCommentPrefix deleting tokens.
Fixes llvm#53799. Reviewed By: HazardyKnusperkeks, owenpan Differential Revision: https://reviews.llvm.org/D119680
1 parent 3cd9df8 commit d0cd5a8

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

clang/lib/Format/BreakableToken.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ BreakableLineCommentSection::BreakableLineCommentSection(
753753
assert(Tok.is(TT_LineComment) &&
754754
"line comment section must start with a line comment");
755755
FormatToken *LineTok = nullptr;
756+
const int Minimum = Style.SpacesInLineCommentPrefix.Minimum;
756757
// How many spaces we changed in the first line of the section, this will be
757758
// applied in all following lines
758759
int FirstLineSpaceChange = 0;
@@ -775,7 +776,7 @@ BreakableLineCommentSection::BreakableLineCommentSection(
775776
Lines[i] = Lines[i].ltrim(Blanks);
776777
StringRef IndentPrefix = getLineCommentIndentPrefix(Lines[i], Style);
777778
OriginalPrefix[i] = IndentPrefix;
778-
const unsigned SpacesInPrefix = llvm::count(IndentPrefix, ' ');
779+
const int SpacesInPrefix = llvm::count(IndentPrefix, ' ');
779780

780781
// On the first line of the comment section we calculate how many spaces
781782
// are to be added or removed, all lines after that just get only the
@@ -784,12 +785,11 @@ BreakableLineCommentSection::BreakableLineCommentSection(
784785
// e.g. from "///" to "//".
785786
if (i == 0 || OriginalPrefix[i].rtrim(Blanks) !=
786787
OriginalPrefix[i - 1].rtrim(Blanks)) {
787-
if (SpacesInPrefix < Style.SpacesInLineCommentPrefix.Minimum &&
788-
Lines[i].size() > IndentPrefix.size() &&
788+
if (SpacesInPrefix < Minimum && Lines[i].size() > IndentPrefix.size() &&
789789
isAlphanumeric(Lines[i][IndentPrefix.size()])) {
790-
FirstLineSpaceChange =
791-
Style.SpacesInLineCommentPrefix.Minimum - SpacesInPrefix;
792-
} else if (SpacesInPrefix > Style.SpacesInLineCommentPrefix.Maximum) {
790+
FirstLineSpaceChange = Minimum - SpacesInPrefix;
791+
} else if (static_cast<unsigned>(SpacesInPrefix) >
792+
Style.SpacesInLineCommentPrefix.Maximum) {
793793
FirstLineSpaceChange =
794794
Style.SpacesInLineCommentPrefix.Maximum - SpacesInPrefix;
795795
} else {
@@ -800,10 +800,9 @@ BreakableLineCommentSection::BreakableLineCommentSection(
800800
if (Lines[i].size() != IndentPrefix.size()) {
801801
PrefixSpaceChange[i] = FirstLineSpaceChange;
802802

803-
if (SpacesInPrefix + PrefixSpaceChange[i] <
804-
Style.SpacesInLineCommentPrefix.Minimum) {
805-
PrefixSpaceChange[i] += Style.SpacesInLineCommentPrefix.Minimum -
806-
(SpacesInPrefix + PrefixSpaceChange[i]);
803+
if (SpacesInPrefix + PrefixSpaceChange[i] < Minimum) {
804+
PrefixSpaceChange[i] +=
805+
Minimum - (SpacesInPrefix + PrefixSpaceChange[i]);
807806
}
808807

809808
assert(Lines[i].size() > IndentPrefix.size());

clang/unittests/Format/FormatTestComments.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3597,6 +3597,11 @@ TEST_F(FormatTestComments, SpaceAtLineCommentBegin) {
35973597
" // World\n"
35983598
"}",
35993599
format(WrapCode, Style));
3600+
EXPECT_EQ("// x\n"
3601+
"// y",
3602+
format("// x\n"
3603+
"// y",
3604+
Style));
36003605

36013606
Style.SpacesInLineCommentPrefix = {3, 3};
36023607
EXPECT_EQ("// Lorem ipsum\n"

0 commit comments

Comments
 (0)