[clang-format] Make checking for a record more robust and avoid a loop.

This commit is contained in:
Marek Kurdej 2022-02-16 23:05:34 +01:00
parent 8ad39fbaf2
commit ef39235cb9
2 changed files with 33 additions and 4 deletions

View File

@ -312,10 +312,15 @@ private:
break;
// Check if the found line starts a record.
for (const FormatToken *RecordTok = (*J)->Last; RecordTok;
RecordTok = RecordTok->Previous)
if (RecordTok->is(tok::l_brace))
return isRecordLBrace(*RecordTok);
const FormatToken *LastNonComment = (*J)->Last;
assert(LastNonComment);
if (LastNonComment->is(tok::comment)) {
LastNonComment = LastNonComment->getPreviousNonComment();
// There must be another token (usually `{`), because we chose a
// line that has a smaller level.
assert(LastNonComment);
}
return isRecordLBrace(*LastNonComment);
}
}

View File

@ -3809,6 +3809,18 @@ TEST_F(FormatTest, FormatsNamespaces) {
" }\n"
"} // namespace\n",
ShortInlineFunctions);
verifyFormat("namespace { /* comment */\n"
" void f() {\n"
" return;\n"
" }\n"
"} // namespace\n",
ShortInlineFunctions);
verifyFormat("namespace { // comment\n"
" void f() {\n"
" return;\n"
" }\n"
"} // namespace\n",
ShortInlineFunctions);
verifyFormat("namespace {\n"
" int some_int;\n"
" void f() {\n"
@ -3828,6 +3840,18 @@ TEST_F(FormatTest, FormatsNamespaces) {
" };\n"
"} // namespace\n",
ShortInlineFunctions);
verifyFormat("namespace {\n"
" class X { /* comment */\n"
" void f() { return; }\n"
" };\n"
"} // namespace\n",
ShortInlineFunctions);
verifyFormat("namespace {\n"
" class X { // comment\n"
" void f() { return; }\n"
" };\n"
"} // namespace\n",
ShortInlineFunctions);
verifyFormat("namespace {\n"
" struct X {\n"
" void f() { return; }\n"