[clang] ef39235 - [clang-format] Make checking for a record more robust and avoid a loop.
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 16 14:06:01 PST 2022
Author: Marek Kurdej
Date: 2022-02-16T23:05:49+01:00
New Revision: ef39235cb94289281d610e4df52ad2a746d6af61
URL: https://github.com/llvm/llvm-project/commit/ef39235cb94289281d610e4df52ad2a746d6af61
DIFF: https://github.com/llvm/llvm-project/commit/ef39235cb94289281d610e4df52ad2a746d6af61.diff
LOG: [clang-format] Make checking for a record more robust and avoid a loop.
Added:
Modified:
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 16fa2e7b50f1..dbf1e4cbbf6a 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -312,10 +312,15 @@ class LineJoiner {
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);
}
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 6e5dd3284633..73503696741a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"
More information about the cfe-commits
mailing list