r324591 - [clang-format] Do not break before long string literals in protos

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 8 02:47:12 PST 2018


Author: krasimir
Date: Thu Feb  8 02:47:12 2018
New Revision: 324591

URL: http://llvm.org/viewvc/llvm-project?rev=324591&view=rev
Log:
[clang-format] Do not break before long string literals in protos

Summary:
This patch is a follow-up to r323319 (which disables string literal breaking for
text protos) and it disables breaking before long string literals.

For example this:
```
keyyyyy: "long string literal"
```
used to get broken into:
```
keyyyyy:
    "long string literal"
```

While at it, I also enabled it for LK_Proto and fixed a bug in the mustBreak code.

Reviewers: djasper, sammccall

Reviewed By: djasper

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D42957

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestProto.cpp
    cfe/trunk/unittests/Format/FormatTestTextProto.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=324591&r1=324590&r2=324591&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Thu Feb  8 02:47:12 2018
@@ -1992,7 +1992,7 @@ bool ContinuationIndenter::nextIsMultili
   if (Current.getNextNonComment() &&
       Current.getNextNonComment()->isStringLiteral())
     return true; // Implicit concatenation.
-  if (Style.ColumnLimit != 0 &&
+  if (Style.ColumnLimit != 0 && Style.BreakStringLiterals &&
       State.Column + Current.ColumnWidth + Current.UnbreakableTailLength >
           Style.ColumnLimit)
     return true; // String will be split.

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=324591&r1=324590&r2=324591&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Feb  8 02:47:12 2018
@@ -686,11 +686,6 @@ FormatStyle getGoogleStyle(FormatStyle::
     FormatStyle GoogleStyle = getGoogleStyle(FormatStyle::LK_Proto);
     GoogleStyle.Language = FormatStyle::LK_TextProto;
 
-    // Text protos are currently mostly formatted inside C++ raw string literals
-    // and often the current breaking behavior of string literals is not
-    // beneficial there. Investigate turning this on once proper string reflow
-    // has been implemented.
-    GoogleStyle.BreakStringLiterals = false;
     return GoogleStyle;
   }
 
@@ -762,6 +757,12 @@ FormatStyle getGoogleStyle(FormatStyle::
     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
     GoogleStyle.SpacesInContainerLiterals = false;
     GoogleStyle.Cpp11BracedListStyle = false;
+    // This affects protocol buffer options specifications and text protos.
+    // Text protos are currently mostly formatted inside C++ raw string literals
+    // and often the current breaking behavior of string literals is not
+    // beneficial there. Investigate turning this on once proper string reflow
+    // has been implemented.
+    GoogleStyle.BreakStringLiterals = false;
   } else if (Language == FormatStyle::LK_ObjC) {
     GoogleStyle.ColumnLimit = 100;
   }

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=324591&r1=324590&r2=324591&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Thu Feb  8 02:47:12 2018
@@ -2830,11 +2830,17 @@ bool TokenAnnotator::canBreakBefore(cons
   if (Right.is(TT_ObjCMethodExpr) && !Right.is(tok::r_square) &&
       Left.isNot(TT_SelectorName))
     return true;
+
   if (Right.is(tok::colon) &&
       !Right.isOneOf(TT_CtorInitializerColon, TT_InlineASMColon))
     return false;
-  if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr))
+  if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
+    if ((Style.Language == FormatStyle::LK_Proto ||
+         Style.Language == FormatStyle::LK_TextProto) &&
+        Right.isStringLiteral())
+      return false;
     return true;
+  }
   if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
                                     Right.Next->is(TT_ObjCMethodExpr)))
     return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.

Modified: cfe/trunk/unittests/Format/FormatTestProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestProto.cpp?rev=324591&r1=324590&r2=324591&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestProto.cpp Thu Feb  8 02:47:12 2018
@@ -412,5 +412,14 @@ TEST_F(FormatTestProto, FormatsImports)
                "}");
 }
 
+TEST_F(FormatTestProto, KeepsLongStringLiteralsOnSameLine) {
+  verifyFormat(
+      "option (MyProto.options) = {\n"
+      "  foo: {\n"
+      "    text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasaaaaaaaa\"\n"
+      "  }\n"
+      "}");
+}
+
 } // end namespace tooling
 } // end namespace clang

Modified: cfe/trunk/unittests/Format/FormatTestTextProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestTextProto.cpp?rev=324591&r1=324590&r2=324591&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestTextProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestTextProto.cpp Thu Feb  8 02:47:12 2018
@@ -306,5 +306,12 @@ TEST_F(FormatTestTextProto, DiscardsUnbr
       "  }\n"
       "}");
 }
+
+TEST_F(FormatTestTextProto, KeepsLongStringLiteralsOnSameLine) {
+  verifyFormat(
+      "foo: {\n"
+      "  text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasaaaaaaaaaa\"\n"
+      "}");
+}
 } // end namespace tooling
 } // end namespace clang




More information about the cfe-commits mailing list