[llvm-branch-commits] [clang] release/18.x: [clang-format] Don't always break before << between str… (PR #94091)

Owen Pan via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri May 31 20:22:29 PDT 2024


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/94091

…ing literals (#92214)

>From 88711473d2affcb21703e7b4f78420c512192acf Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 31 May 2024 20:13:27 -0700
Subject: [PATCH] release/18.x: [clang-format] Don't always break before <<
 between string literals (#92214)

---
 clang/lib/Format/TokenAnnotator.cpp   |  8 +++++---
 clang/unittests/Format/FormatTest.cpp | 11 +++++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index c1f1662481922..628fe46cc348e 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5159,9 +5159,11 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
     return true;
   if (Left.IsUnterminatedLiteral)
     return true;
-  if (Right.is(tok::lessless) && Right.Next && Left.is(tok::string_literal) &&
-      Right.Next->is(tok::string_literal)) {
-    return true;
+  if (const auto *BeforeLeft = Left.Previous, *AfterRight = Right.Next;
+      BeforeLeft && BeforeLeft->is(tok::lessless) &&
+      Left.is(tok::string_literal) && Right.is(tok::lessless) && AfterRight &&
+      AfterRight->is(tok::string_literal)) {
+    return Right.NewlinesBefore > 0;
   }
   if (Right.is(TT_RequiresClause)) {
     switch (Style.RequiresClausePosition) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 0161a1685eb12..d69632f7f0f8c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10426,6 +10426,17 @@ TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
 }
 
+TEST_F(FormatTest, WrapBeforeInsertionOperatorbetweenStringLiterals) {
+  verifyFormat("QStringList() << \"foo\" << \"bar\";");
+
+  verifyNoChange("QStringList() << \"foo\"\n"
+                 "              << \"bar\";");
+
+  verifyFormat("log_error(log, \"foo\" << \"bar\");",
+               "log_error(log, \"foo\"\n"
+               "                   << \"bar\");");
+}
+
 TEST_F(FormatTest, UnderstandsEquals) {
   verifyFormat(
       "aaaaaaaaaaaaaaaaa =\n"



More information about the llvm-branch-commits mailing list