Skip to content

Commit 8fe39e6

Browse files
authored
[clang-format] Don't always break before << between string literals (#92214)
Instead, leave the line wrapping as is. Fixes #43887. Fixes #44363.
1 parent bf1d417 commit 8fe39e6

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5601,10 +5601,13 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
56015601
return true;
56025602
if (Left.IsUnterminatedLiteral)
56035603
return true;
5604-
if (Right.is(tok::lessless) && AfterRight && Left.is(tok::string_literal) &&
5604+
5605+
if (BeforeLeft && BeforeLeft->is(tok::lessless) &&
5606+
Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
56055607
AfterRight->is(tok::string_literal)) {
5606-
return true;
5608+
return Right.NewlinesBefore > 0;
56075609
}
5610+
56085611
if (Right.is(TT_RequiresClause)) {
56095612
switch (Style.RequiresClausePosition) {
56105613
case FormatStyle::RCPS_OwnLine:

clang/unittests/Format/FormatTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10539,6 +10539,17 @@ TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
1053910539
" bbbbbbbbbbbbbbbbbbbbbbb);");
1054010540
}
1054110541

10542+
TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
10543+
verifyFormat("QStringList() << \"foo\" << \"bar\";");
10544+
10545+
verifyNoChange("QStringList() << \"foo\"\n"
10546+
" << \"bar\";");
10547+
10548+
verifyFormat("log_error(log, \"foo\" << \"bar\");",
10549+
"log_error(log, \"foo\"\n"
10550+
" << \"bar\");");
10551+
}
10552+
1054210553
TEST_F(FormatTest, UnderstandsEquals) {
1054310554
verifyFormat(
1054410555
"aaaaaaaaaaaaaaaaa =\n"

0 commit comments

Comments
 (0)