Skip to content

release/18.x: [clang-format] Don't always break before << between str… #94091

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
Jun 5, 2024

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jun 1, 2024

…ing literals (#92214)

@llvmbot
Copy link
Member

llvmbot commented Jun 1, 2024

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

…ing literals (#92214)


Full diff: https://github.com/llvm/llvm-project/pull/94091.diff

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+5-3)
  • (modified) clang/unittests/Format/FormatTest.cpp (+11)
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"

@owenca owenca added this to the LLVM 18.X Release milestone Jun 1, 2024
@owenca
Copy link
Contributor Author

owenca commented Jun 1, 2024

@tstellar can we also get this one into 18.1.7?

@tstellar
Copy link
Collaborator

tstellar commented Jun 5, 2024

@owenca I'm only going to take regression fixes for 18.1.7.

@owenca
Copy link
Contributor Author

owenca commented Jun 5, 2024

@tstellar strictly speaking, this is a regression fix. (It's a churn within 18.x release cycle.)

  1. Risk level: low IMO. (The other code owner @mydeveloperday approved it to be included in 18.1.7.)
  2. Last known working version: 18.1.4

@tstellar tstellar merged commit 8c0fe0d into llvm:release/18.x Jun 5, 2024
17 of 21 checks passed
@tstellar
Copy link
Collaborator

tstellar commented Jun 5, 2024

@owenca Do you have a release note we can put in the release announcement?

@owenca
Copy link
Contributor Author

owenca commented Jun 6, 2024

@tstellar thanks!

@owenca Do you have a release note we can put in the release announcement?

Release note:
Fixes clang-format (since 18.1.1) regressions on breaking before a stream insertion operator (<<) when both operands are string literals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

4 participants