Skip to content

[clang-format] Don't always break before << between string literals #92214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5601,10 +5601,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return true;
if (Left.IsUnterminatedLiteral)
return true;
if (Right.is(tok::lessless) && AfterRight && Left.is(tok::string_literal) &&

if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
AfterRight->is(tok::string_literal)) {
return true;
return Right.NewlinesBefore > 0;
}

if (Right.is(TT_RequiresClause)) {
switch (Style.RequiresClausePosition) {
case FormatStyle::RCPS_OwnLine:
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10539,6 +10539,17 @@ TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
" bbbbbbbbbbbbbbbbbbbbbbb);");
}

TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
verifyFormat("QStringList() << \"foo\" << \"bar\";");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will verifyNoChange here be better suitable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the single-argument version of verifyFormat is a more stringent test than verifyNoChange as the former also tests the input with all optional whitespace characters between tokens removed.


verifyNoChange("QStringList() << \"foo\"\n"
" << \"bar\";");

verifyFormat("log_error(log, \"foo\" << \"bar\");",
"log_error(log, \"foo\"\n"
" << \"bar\");");
}

TEST_F(FormatTest, UnderstandsEquals) {
verifyFormat(
"aaaaaaaaaaaaaaaaa =\n"
Expand Down
Loading