[clang] [clang-format] Option to insert spaces before the closing `*/` (PR #162105)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 26 07:54:35 PDT 2025
================
@@ -332,6 +332,181 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) {
verifyNoCrash(StringRef("/*\\\0\n/", 6));
}
+TEST_F(FormatTestComments, InsertsSpaceAfterOpeningBlockComment) {
+ FormatStyle Style = getLLVMStyle();
+ Style.SpaceInComments.AfterOpeningComment =
+ FormatStyle::CommentSpaceMode::Always;
+ Style.ReflowComments = FormatStyle::RCS_Never;
+
+ verifyFormat("foo(/* comment */);", "foo(/*comment */);", Style);
+ verifyFormat("foo(/* Logger=*/nullptr);", "foo(/*Logger=*/nullptr);", Style);
+ verifyFormat("/* comment */", "/*comment */", Style);
+ verifyFormat("/* comment before code */\nint x;",
+ "/*comment before code */\nint x;", Style);
+ verifyFormat("/* \ncomment */", "/*\ncomment */", Style);
+ verifyFormat("/* \ncomment */\nint x;", "/*\ncomment */\nint x;", Style);
+ verifyFormat("/* \ncomment line\n*/", "/*\ncomment line\n*/", Style);
+ verifyFormat("/* \n * comment star\n*/", "/*\n * comment star\n*/", Style);
+ verifyFormat("/* comment line\nnext */", "/*comment line\nnext */", Style);
+ EXPECT_EQ("/*\n*/", format("/*\n*/", Style));
----------------
Men-cotton wrote:
Thanks for the suggestion!
I agree that using `verifyFormat` is the better approach here.
I looked into it, and found that the single-argument version, `verifyFormat("/*\n*/", Style)`, fails. This is because the internal `messUp()` function removes the newline from the empty comment (turning it into `/**/`), which then doesn't match the expected output. This also happens for the other two test cases where I was using `EXPECT_EQ`. This behavior of `messUp()` seems to be a known characteristic, as it's mentioned in the comments of the `AlignTrailingCommentsAcrossEmptyLines` test.
By switching to the two-argument version, like `verifyFormat("/*\n*/", "/*\n*/", Style)`, the tests pass. This version avoids the `messUp()` call and is more robust than a plain `EXPECT_EQ` because it also performs a stability check.
For these reasons, I believe changing it to the two-argument `verifyFormat` is the best path forward. Let me know what you think.
https://github.com/llvm/llvm-project/pull/162105
More information about the cfe-commits
mailing list