Skip to content

Commit 418b4a7

Browse files
[clang-format] Respect spaces in line comment section...
... without an active column limit. Before line comments were not touched at all with ColumnLimit == 0. Differential Revision: https://reviews.llvm.org/D96896
1 parent 8174f33 commit 418b4a7

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,11 @@ ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
19931993
// We don't insert backslashes when breaking line comments.
19941994
ColumnLimit = Style.ColumnLimit;
19951995
}
1996+
if (ColumnLimit == 0) {
1997+
// To make the rest of the function easier set the column limit to the
1998+
// maximum, if there should be no limit.
1999+
ColumnLimit = std::numeric_limits<decltype(ColumnLimit)>::max();
2000+
}
19962001
if (Current.UnbreakableTailLength >= ColumnLimit)
19972002
return {0, false};
19982003
// ColumnWidth was already accounted into State.Column before calling

clang/unittests/Format/FormatTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19253,6 +19253,33 @@ TEST_F(FormatTest, IndentAccessModifiers) {
1925319253
"};\n",
1925419254
Style);
1925519255
}
19256+
19257+
TEST_F(FormatTest, LimitlessStringsAndComments) {
19258+
auto Style = getLLVMStyleWithColumns(0);
19259+
constexpr StringRef Code =
19260+
"/**\n"
19261+
" * This is a multiline comment with quite some long lines, at least for "
19262+
"the LLVM Style.\n"
19263+
" * We will redo this with strings and line comments. Just to check if "
19264+
"everything is working.\n"
19265+
" */\n"
19266+
"bool foo() {\n"
19267+
" /* Single line multi line comment. */\n"
19268+
" const std::string String = \"This is a multiline string with quite "
19269+
"some long lines, at least for the LLVM Style.\"\n"
19270+
" \"We already did it with multi line "
19271+
"comments, and we will do it with line comments. Just to check if "
19272+
"everything is working.\";\n"
19273+
" // This is a line comment (block) with quite some long lines, at "
19274+
"least for the LLVM Style.\n"
19275+
" // We already did this with multi line comments and strings. Just to "
19276+
"check if everything is working.\n"
19277+
" const std::string SmallString = \"Hello World\";\n"
19278+
" // Small line comment\n"
19279+
" return String.size() > SmallString.size();\n"
19280+
"}";
19281+
EXPECT_EQ(Code, format(Code, Style));
19282+
}
1925619283
} // namespace
1925719284
} // namespace format
1925819285
} // namespace clang

clang/unittests/Format/FormatTestComments.cpp

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3793,6 +3793,189 @@ TEST_F(FormatTestComments, SpaceAtLineCommentBegin) {
37933793
"int i;// A Comment to be moved\n"
37943794
" // with indent\n",
37953795
Style));
3796+
3797+
Style = getLLVMStyleWithColumns(0);
3798+
EXPECT_EQ("// Free comment without space\n"
3799+
"\n"
3800+
"// Free comment with 3 spaces\n"
3801+
"\n"
3802+
"/// Free Doxygen without space\n"
3803+
"\n"
3804+
"/// Free Doxygen with 3 spaces\n"
3805+
"\n"
3806+
"/// A Doxygen Comment with a nested list:\n"
3807+
"/// - Foo\n"
3808+
"/// - Bar\n"
3809+
"/// - Baz\n"
3810+
"/// - End\n"
3811+
"/// of the inner list\n"
3812+
"/// .\n"
3813+
"/// .\n"
3814+
"\n"
3815+
"namespace Foo {\n"
3816+
"bool bar(bool b) {\n"
3817+
" bool ret1 = true; ///< Doxygenstyle without space\n"
3818+
" bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3819+
" if (b) {\n"
3820+
" // Foo\n"
3821+
"\n"
3822+
" // In function comment\n"
3823+
" ret2 = false;\n"
3824+
" } // End of if\n"
3825+
"\n"
3826+
" // if (ret1) {\n"
3827+
" // return ret2;\n"
3828+
" // }\n"
3829+
"\n"
3830+
" // if (ret1) {\n"
3831+
" // return ret2;\n"
3832+
" // }\n"
3833+
"\n"
3834+
" return ret1 && ret2;\n"
3835+
"}\n"
3836+
"} // namespace Foo\n"
3837+
"\n"
3838+
"namespace Bar {\n"
3839+
"int foo();\n"
3840+
"} // namespace Bar\n"
3841+
"//@Nothing added because of the non ascii char\n"
3842+
"\n"
3843+
"//@ Nothing removed because of the non ascii char\n"
3844+
"\n"
3845+
"// Comment to move to the left\n"
3846+
"// But not this?\n"
3847+
"// @but this\n"
3848+
"\n"
3849+
"// Comment to move to the right\n"
3850+
"//@ this stays\n"
3851+
"\n"
3852+
"//} will not move\n"
3853+
"\n"
3854+
"// vv will only move\n"
3855+
"// } if the line above does\n",
3856+
format(Code, Style));
3857+
3858+
Style.SpacesInLineCommentPrefix = {0, 0};
3859+
EXPECT_EQ("//Free comment without space\n"
3860+
"\n"
3861+
"//Free comment with 3 spaces\n"
3862+
"\n"
3863+
"///Free Doxygen without space\n"
3864+
"\n"
3865+
"///Free Doxygen with 3 spaces\n"
3866+
"\n"
3867+
"///A Doxygen Comment with a nested list:\n"
3868+
"///- Foo\n"
3869+
"///- Bar\n"
3870+
"/// - Baz\n" // Here we keep the relative indentation
3871+
"/// - End\n"
3872+
"/// of the inner list\n"
3873+
"/// .\n"
3874+
"///.\n"
3875+
"\n"
3876+
"namespace Foo {\n"
3877+
"bool bar(bool b) {\n"
3878+
" bool ret1 = true; ///<Doxygenstyle without space\n"
3879+
" bool ret2 = true; ///<Doxygenstyle with 3 spaces\n"
3880+
" if (b) {\n"
3881+
" //Foo\n"
3882+
"\n"
3883+
" //In function comment\n"
3884+
" ret2 = false;\n"
3885+
" } //End of if\n"
3886+
"\n"
3887+
" //if (ret1) {\n"
3888+
" // return ret2;\n"
3889+
" //}\n"
3890+
"\n"
3891+
" //if (ret1) {\n"
3892+
" // return ret2;\n"
3893+
" //}\n"
3894+
"\n"
3895+
" return ret1 && ret2;\n"
3896+
"}\n"
3897+
"} //namespace Foo\n"
3898+
"\n"
3899+
"namespace Bar {\n"
3900+
"int foo();\n"
3901+
"} //namespace Bar\n"
3902+
"//@Nothing added because of the non ascii char\n"
3903+
"\n"
3904+
"//@ Nothing removed because of the non ascii char\n"
3905+
"\n"
3906+
"//Comment to move to the left\n"
3907+
"//But not this?\n"
3908+
"//@but this\n"
3909+
"\n"
3910+
"//Comment to move to the right\n"
3911+
"//@ this stays\n"
3912+
"\n"
3913+
"//} will not move\n"
3914+
"\n"
3915+
"//vv will only move\n"
3916+
"//} if the line above does\n",
3917+
format(Code, Style));
3918+
3919+
Style.SpacesInLineCommentPrefix = {2, -1u};
3920+
EXPECT_EQ("// Free comment without space\n"
3921+
"\n"
3922+
"// Free comment with 3 spaces\n"
3923+
"\n"
3924+
"/// Free Doxygen without space\n"
3925+
"\n"
3926+
"/// Free Doxygen with 3 spaces\n"
3927+
"\n"
3928+
"/// A Doxygen Comment with a nested list:\n"
3929+
"/// - Foo\n"
3930+
"/// - Bar\n"
3931+
"/// - Baz\n"
3932+
"/// - End\n"
3933+
"/// of the inner list\n"
3934+
"/// .\n"
3935+
"/// .\n"
3936+
"\n"
3937+
"namespace Foo {\n"
3938+
"bool bar(bool b) {\n"
3939+
" bool ret1 = true; ///< Doxygenstyle without space\n"
3940+
" bool ret2 = true; ///< Doxygenstyle with 3 spaces\n"
3941+
" if (b) {\n"
3942+
" // Foo\n"
3943+
"\n"
3944+
" // In function comment\n"
3945+
" ret2 = false;\n"
3946+
" } // End of if\n"
3947+
"\n"
3948+
" // if (ret1) {\n"
3949+
" // return ret2;\n"
3950+
" // }\n"
3951+
"\n"
3952+
" // if (ret1) {\n"
3953+
" // return ret2;\n"
3954+
" // }\n"
3955+
"\n"
3956+
" return ret1 && ret2;\n"
3957+
"}\n"
3958+
"} // namespace Foo\n"
3959+
"\n"
3960+
"namespace Bar {\n"
3961+
"int foo();\n"
3962+
"} // namespace Bar\n"
3963+
"//@Nothing added because of the non ascii char\n"
3964+
"\n"
3965+
"//@ Nothing removed because of the non ascii char\n"
3966+
"\n"
3967+
"// Comment to move to the left\n"
3968+
"// But not this?\n"
3969+
"// @but this\n"
3970+
"\n"
3971+
"// Comment to move to the right\n"
3972+
"//@ this stays\n"
3973+
"\n"
3974+
"//} will not move\n"
3975+
"\n"
3976+
"// vv will only move\n"
3977+
"// } if the line above does\n",
3978+
format(Code, Style));
37963979
}
37973980

37983981
} // end namespace

0 commit comments

Comments
 (0)