[clang-format] Fix misaligned trailing comments in the presence of an empty block comment.

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.

Reviewed By: MyDeveloperDay, HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D118475
This commit is contained in:
Marek Kurdej 2022-01-28 10:37:22 +01:00
parent be2147db05
commit 64df51624f
2 changed files with 9 additions and 1 deletions

View File

@ -555,7 +555,9 @@ unsigned BreakableBlockComment::getRemainingLength(unsigned LineIndex,
// 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;

View File

@ -2842,6 +2842,12 @@ TEST_F(FormatTestComments, AlignTrailingComments) {
"#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) {