Skip to content

Commit 8c0fe0d

Browse files
authored
release/18.x: [clang-format] Don't always break before << between str… (#94091)
…ing literals (#92214)
1 parent 7e6ece9 commit 8c0fe0d

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

clang/lib/Format/TokenAnnotator.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -5159,9 +5159,11 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
51595159
return true;
51605160
if (Left.IsUnterminatedLiteral)
51615161
return true;
5162-
if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
5163-
Right.Next->is(tok::string_literal)) {
5164-
return true;
5162+
if (const auto *BeforeLeft = Left.Previous, *AfterRight = Right.Next;
5163+
BeforeLeft && BeforeLeft->is(tok::lessless) &&
5164+
Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
5165+
AfterRight->is(tok::string_literal)) {
5166+
return Right.NewlinesBefore > 0;
51655167
}
51665168
if (Right.is(TT_RequiresClause)) {
51675169
switch (Style.RequiresClausePosition) {

clang/unittests/Format/FormatTest.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -10426,6 +10426,17 @@ TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
1042610426
" bbbbbbbbbbbbbbbbbbbbbbb);");
1042710427
}
1042810428

10429+
TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
10430+
verifyFormat("QStringList() << \"foo\" << \"bar\";");
10431+
10432+
verifyNoChange("QStringList() << \"foo\"\n"
10433+
" << \"bar\";");
10434+
10435+
verifyFormat("log_error(log, \"foo\" << \"bar\");",
10436+
"log_error(log, \"foo\"\n"
10437+
" << \"bar\");");
10438+
}
10439+
1042910440
TEST_F(FormatTest, UnderstandsEquals) {
1043010441
verifyFormat(
1043110442
"aaaaaaaaaaaaaaaaa =\n"

0 commit comments

Comments
 (0)