Improve line breaking before multi-line strings.
authorDaniel Jasper <djasper@google.com>
Wed, 17 Jul 2013 15:38:19 +0000 (15:38 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 17 Jul 2013 15:38:19 +0000 (15:38 +0000)
The AlwaysBreakBeforeMultilineStrings rule does not really make sense
if it does not a column gain.

Before (in Google style):
  f(
      "aaaa"
      "bbbb");

After:
  f("aaaa"
    "bbbb");

llvm-svn: 186515

clang/lib/Format/Format.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

index 125283a..a4495ba 100644 (file)
@@ -1177,6 +1177,14 @@ private:
         !Current.isTrailingComment() &&
         !Current.isOneOf(tok::r_paren, tok::r_brace))
       return true;
+    if (Style.AlwaysBreakBeforeMultilineStrings &&
+        State.Column > State.Stack.back().Indent &&
+        Current.is(tok::string_literal) && Previous.isNot(tok::lessless) &&
+        Previous.Type != TT_InlineASMColon &&
+        ((Current.getNextNonComment() &&
+          Current.getNextNonComment()->is(tok::string_literal)) ||
+         (Current.TokenText.find("\\\n") != StringRef::npos)))
+      return true;
 
     // If we need to break somewhere inside the LHS of a binary expression, we
     // should also break after the operator. Otherwise, the formatting would
index 67a0fa8..021fd24 100644 (file)
@@ -963,18 +963,12 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
     } else if (Current->Previous->ClosesTemplateDeclaration &&
                Style.AlwaysBreakTemplateDeclarations) {
       Current->MustBreakBefore = true;
-    } else if (Style.AlwaysBreakBeforeMultilineStrings &&
-               Current->is(tok::string_literal) &&
-               Current->Previous->isNot(tok::lessless) &&
-               Current->Previous->Type != TT_InlineASMColon &&
-               ((Current->getNextNonComment() &&
-                 Current->getNextNonComment()->is(tok::string_literal)) ||
-                (Current->TokenText.find("\\\n") != StringRef::npos))) {
-      Current->MustBreakBefore = true;
     }
     Current->CanBreakBefore =
         Current->MustBreakBefore || canBreakBefore(Line, *Current);
-    if (Current->MustBreakBefore)
+    if (Current->MustBreakBefore ||
+        (Current->is(tok::string_literal) &&
+         Current->TokenText.find("\\\n") != StringRef::npos))
       Current->TotalLength = Current->Previous->TotalLength + Style.ColumnLimit;
     else
       Current->TotalLength = Current->Previous->TotalLength +
index 3b8f911..353fcc7 100644 (file)
@@ -2981,27 +2981,34 @@ TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
   FormatStyle Break = getLLVMStyle();
   Break.AlwaysBreakBeforeMultilineStrings = true;
-  EXPECT_EQ("aaaa = \"bbbb\"\n"
-            "       \"cccc\";",
-            format("aaaa=\"bbbb\" \"cccc\";", NoBreak));
-  EXPECT_EQ("aaaa =\n"
-            "    \"bbbb\"\n"
-            "    \"cccc\";",
-            format("aaaa=\"bbbb\" \"cccc\";", Break));
-  EXPECT_EQ("aaaa(\"bbbb\"\n"
-            "     \"cccc\");",
-            format("aaaa(\"bbbb\" \"cccc\");", NoBreak));
-  EXPECT_EQ("aaaa(\n"
-            "    \"bbbb\"\n"
-            "    \"cccc\");",
-            format("aaaa(\"bbbb\" \"cccc\");", Break));
-  EXPECT_EQ("aaaa(qqq, \"bbbb\"\n"
-            "          \"cccc\");",
-            format("aaaa(qqq, \"bbbb\" \"cccc\");", NoBreak));
-  EXPECT_EQ("aaaa(qqq,\n"
-            "     \"bbbb\"\n"
-            "     \"cccc\");",
-            format("aaaa(qqq, \"bbbb\" \"cccc\");", Break));
+  verifyFormat("aaaa = \"bbbb\"\n"
+               "       \"cccc\";",
+               NoBreak);
+  verifyFormat("aaaa =\n"
+               "    \"bbbb\"\n"
+               "    \"cccc\";",
+               Break);
+  verifyFormat("aaaa(\"bbbb\"\n"
+               "     \"cccc\");",
+               NoBreak);
+  verifyFormat("aaaa(\n"
+               "    \"bbbb\"\n"
+               "    \"cccc\");",
+               Break);
+  verifyFormat("aaaa(qqq, \"bbbb\"\n"
+               "          \"cccc\");",
+               NoBreak);
+  verifyFormat("aaaa(qqq,\n"
+               "     \"bbbb\"\n"
+               "     \"cccc\");",
+               Break);
+
+  // Don't break if there is no column gain.
+  verifyFormat("f(\"aaaa\"\n"
+               "  \"bbbb\");",
+               Break);
+
+  // Treat literals with escaped newlines like multi-line string literals.
   EXPECT_EQ("x = \"a\\\n"
             "b\\\n"
             "c\";",