[clang] [clang-format] Don't always break before << between string literals (PR #92214)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Tue May 14 21:30:24 PDT 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/92214
Instead, leave the line wrapping as is.
Fixes #43887.
Fixes #44363.
>From 40eeb958cef55465fdcee66ab385928c1f202e50 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 14 May 2024 19:14:15 -0700
Subject: [PATCH] [clang-format] Don't always break before << between string
literals
Instead, leave the line wrapping as is.
Fixes #43887.
Fixes #44363.
---
clang/lib/Format/TokenAnnotator.cpp | 7 +++++--
clang/unittests/Format/FormatTest.cpp | 11 +++++++++++
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index d0aa0838423e4..7c4c76a91f2c5 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -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:
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e6f8e4a06515e..6f57f10e12e88 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10539,6 +10539,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 cfe-commits
mailing list