sstwcw updated this revision to Diff 537701.
sstwcw added a comment.

- Add tests for non-whitespace sequences


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D154091/new/

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
@@ -14832,6 +14832,69 @@
             "\"/and\"",
             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
 
+  // Escape sequences should be recognized.  When the limit is 13 columns,
+  // another character can fit on the first line between the escape sequence and
+  // the closing quote.  We know the escape sequence is recognized when the
+  // program doesn't put the character following the escape sequence on the
+  // first line.
+  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));
+  verifyFormat(R"(x = "some\f"
+    "text";)",
+               R"(x = "some\ftext";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "some\r"
+    "text";)",
+               R"(x = "some\rtext";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "some\v"
+    "text";)",
+               R"(x = "some\vtext";)", getLLVMStyleWithColumns(13));
+  // A newline outside of the column limit should not cause a break.
+  verifyFormat(R"(x = "something "
+    "so\nshort";)",
+               R"(x = "something so\nshort";)", getLLVMStyleWithColumns(16));
+  verifyFormat(R"(x = "something "
+    "so\nshort";)",
+               R"(x = "something so\nshort";)", getLLVMStyleWithColumns(19));
+  verifyFormat(R"(x = "something so\n"
+    "short";)",
+               R"(x = "something so\nshort";)", getLLVMStyleWithColumns(20));
+  // Escape sequences for characters that are not whitespace should not be
+  // treated as whitespace.
+  verifyFormat(R"(x = "some\at"
+    "ext";)",
+               R"(x = "some\atext";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "some\bt"
+    "ext";)",
+               R"(x = "some\btext";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "some\et"
+    "ext";)",
+               R"(x = "some\etext";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "s\157me"
+    "text";)",
+               R"(x = "s\157metext";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "s\x6fme"
+    "text";)",
+               R"(x = "s\x6fmetext";)", getLLVMStyleWithColumns(13));
+  verifyFormat(R"(x = "s\u006fm"
+    "etext";)",
+               R"(x = "s\u006fmetext";)", getLLVMStyleWithColumns(14));
+  verifyFormat(R"(x = "s\U0000006fm"
+    "etext";)",
+               R"(x = "s\U0000006fmetext";)", getLLVMStyleWithColumns(18));
+
   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)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to