[clang] [clang-format] Fix extraneous space in block comments ending with **/ (PR #190209)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 2 09:31:34 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Frank515 (frank-515)
<details>
<summary>Changes</summary>
This patch fixes an issue where clang-format incorrectly inserts an extra space
when reflowing block comments that end with `**/` instead of `*/`.
Fixes #<!-- -->187764
---
Full diff: https://github.com/llvm/llvm-project/pull/190209.diff
2 Files Affected:
- (modified) clang/lib/Format/BreakableToken.cpp (+10-6)
- (modified) clang/unittests/Format/FormatTestComments.cpp (+9)
``````````diff
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index b60daffc0eb1c..902d3f1d587ef 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -55,8 +55,7 @@ static StringRef getLineCommentIndentPrefix(StringRef Comment,
static BreakableToken::Split
getCommentSplit(StringRef Text, unsigned ContentStartColumn,
unsigned ColumnLimit, unsigned TabWidth,
- encoding::Encoding Encoding, const FormatStyle &Style,
- bool DecorationEndsWithStar = false) {
+ encoding::Encoding Encoding, const FormatStyle &Style) {
LLVM_DEBUG(llvm::dbgs() << "Comment split: \"" << Text
<< "\", Column limit: " << ColumnLimit
<< ", Content start: " << ContentStartColumn << "\n");
@@ -145,9 +144,7 @@ getCommentSplit(StringRef Text, unsigned ContentStartColumn,
if (SpaceOffset == 1 && Text[SpaceOffset - 1] == '*')
return BreakableToken::Split(StringRef::npos, 0);
StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(Blanks);
- StringRef AfterCut = Text.substr(SpaceOffset);
- if (!DecorationEndsWithStar)
- AfterCut = AfterCut.ltrim(Blanks);
+ StringRef AfterCut = Text.substr(SpaceOffset).ltrim(Blanks);
return BreakableToken::Split(BeforeCut.size(),
AfterCut.begin() - BeforeCut.end());
}
@@ -601,7 +598,7 @@ BreakableToken::Split BreakableBlockComment::getSplit(
return Split(StringRef::npos, 0);
return getCommentSplit(Content[LineIndex].substr(TailOffset),
ContentStartColumn, ColumnLimit, Style.TabWidth,
- Encoding, Style, Decoration.ends_with("*"));
+ Encoding, Style);
}
void BreakableBlockComment::adjustWhitespace(unsigned LineIndex,
@@ -703,6 +700,7 @@ void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
WhitespaceManager &Whitespaces) const {
StringRef Text = Content[LineIndex].substr(TailOffset);
StringRef Prefix = Decoration;
+ std::string PrefixStorage;
// We need this to account for the case when we have a decoration "* " for all
// the lines except for the last one, where the star in "*/" acts as a
// decoration.
@@ -713,6 +711,12 @@ void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
Prefix = "";
if (LocalIndentAtLineBreak >= 2)
LocalIndentAtLineBreak -= 2;
+ } else if (!Prefix.empty() && Prefix.ends_with("*") && Split.second > 0) {
+ // getCommentSplit trims the whitespace at the split. Keep a visible space
+ // after star-only decorations by adding it to the inserted prefix.
+ PrefixStorage = Prefix.str();
+ PrefixStorage.push_back(' ');
+ Prefix = PrefixStorage;
}
// The split offset is from the beginning of the line. Convert it to an offset
// from the beginning of the token text.
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 684d3014fa7bb..3c821a4363ca3 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -1588,6 +1588,15 @@ TEST_F(FormatTestComments, ReflowsComments) {
" long */",
Style20);
+ auto Style80 = getLLVMStyleWithColumns(80);
+ Style80.PenaltyExcessCharacter = 10;
+ StringRef ClosingStars = "/**\n"
+ " * This test verifies the special code path. It "
+ "currently exists for code coverage.\n"
+ " **/\n"
+ "void test() {}";
+ EXPECT_EQ(ClosingStars, format(ClosingStars, Style80));
+
// Reflow two short lines; keep the postfix of the last one.
verifyFormat("/* long long long\n"
" * long long long */",
``````````
</details>
https://github.com/llvm/llvm-project/pull/190209
More information about the cfe-commits
mailing list