[PATCH] D154091: [clang-format] Prefer breaking long strings at new lines

sstwcw via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 29 08:32:09 PDT 2023


sstwcw created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
sstwcw requested review of this revision.

Previously, escape sequences in string literals were not recognized
when the program considered where to break string literals.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154091

Files:
  clang/lib/Format/BreakableToken.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -14807,6 +14807,23 @@
             "\"/and\"",
             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
 
+  // Escape sequences should be recognized.
+  verifyFormat(R"(x = "some\n"
+    "text";)",
+               R"(x = "some\ntext";)", getLLVMStyleWithColumns(12));
+  verifyFormat(R"(x = "some\n"
+    "text";)",
+               R"(x = "some\ntext";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "some\n"
+    " text";)",
+               R"(x = "some\n text";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "some\t"
+    "text";)",
+               R"(x = "some\ttext";)", getLLVMStyleWithColumns(12));
+  verifyFormat(R"(x = "some\t"
+    "text";)",
+               R"(x = "some\ttext";)", getLLVMStyleWithColumns(13));
+
   EXPECT_EQ("variable =\n"
             "    \"long string \"\n"
             "    \"literal\";",
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -176,13 +176,15 @@
   if (ColumnLimit <= UsedColumns)
     return BreakableToken::Split(StringRef::npos, 0);
   unsigned MaxSplit = ColumnLimit - UsedColumns;
-  StringRef::size_type SpaceOffset = 0;
+  StringRef::size_type NewLine = 0;
+  StringRef::size_type AfterSpace = 0;
   StringRef::size_type SlashOffset = 0;
   StringRef::size_type WordStartOffset = 0;
   StringRef::size_type SplitPoint = 0;
   for (unsigned Chars = 0;;) {
     unsigned Advance;
-    if (Text[0] == '\\') {
+    bool EscapeSequence = Text[0] == '\\';
+    if (EscapeSequence) {
       Advance = encoding::getEscapeSequenceLength(Text);
       Chars += Advance;
     } else {
@@ -194,8 +196,21 @@
     if (Chars > MaxSplit || Text.size() <= Advance)
       break;
 
+    if (EscapeSequence && Advance == 2) {
+      switch (Text[1]) {
+      case 'n':
+        NewLine = SplitPoint + 2;
+        break;
+      case 'f':
+      case 'r':
+      case 't':
+      case 'v':
+        AfterSpace = SplitPoint + 2;
+        break;
+      }
+    }
     if (IsBlank(Text[0]))
-      SpaceOffset = SplitPoint;
+      AfterSpace = SplitPoint + 1;
     if (Text[0] == '/')
       SlashOffset = SplitPoint;
     if (Advance == 1 && !isAlphanumeric(Text[0]))
@@ -205,8 +220,10 @@
     Text = Text.substr(Advance);
   }
 
-  if (SpaceOffset != 0)
-    return BreakableToken::Split(SpaceOffset + 1, 0);
+  if (NewLine != 0)
+    return BreakableToken::Split(NewLine, 0);
+  if (AfterSpace >= 2)
+    return BreakableToken::Split(AfterSpace, 0);
   if (SlashOffset != 0)
     return BreakableToken::Split(SlashOffset + 1, 0);
   if (WordStartOffset != 0)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154091.535816.patch
Type: text/x-patch
Size: 2868 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230629/0a1c3a85/attachment.bin>


More information about the cfe-commits mailing list