]> granicus.if.org Git - clang/commitdiff
clang-format: Properly handle implicit string concatenation in text protos
authorDaniel Jasper <djasper@google.com>
Mon, 12 Mar 2018 10:32:18 +0000 (10:32 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 12 Mar 2018 10:32:18 +0000 (10:32 +0000)
Three issues to fix:
- char_constants weren't properly treated as string literals
- Prevening the break after "label: " does not make sense in concunction
  with AlwaysBreakBeforeMultilineStrings. It leads to situations where
  clang-format just cannot find a viable format (it must break and yet
  it must not break).
- AlwaysBreakBeforeMultilineStrings should not be on for LK_TextProto in
  Google style.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@327255 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
lib/Format/FormatTokenLexer.cpp
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestProto.cpp
unittests/Format/FormatTestTextProto.cpp

index 5807db9407fb9ea53cea69786489afda14d06952..5c7ce8517832a9ccd99532fb1e549fa2160d28ee 100644 (file)
@@ -766,6 +766,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
     GoogleStyle.JavaScriptWrapImports = false;
   } else if (Language == FormatStyle::LK_Proto) {
     GoogleStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
+    GoogleStyle.AlwaysBreakBeforeMultilineStrings = false;
     GoogleStyle.SpacesInContainerLiterals = false;
     GoogleStyle.Cpp11BracedListStyle = false;
     // This affects protocol buffer options specifications and text protos.
index 199d2974c5c74290106a6f84bed9214cbe6b7210..284a3a2b92bb9f5fe382e3f2da388d6cc586a230 100644 (file)
@@ -691,7 +691,9 @@ void FormatTokenLexer::readRawToken(FormatToken &Tok) {
     }
   }
 
-  if (Style.Language == FormatStyle::LK_JavaScript &&
+  if ((Style.Language == FormatStyle::LK_JavaScript ||
+       Style.Language == FormatStyle::LK_Proto ||
+       Style.Language == FormatStyle::LK_TextProto) &&
       Tok.is(tok::char_constant)) {
     Tok.Tok.setKind(tok::string_literal);
   }
index 97a201d267e8eb039ff369b60be63729dcb2a52d..1e17a7d3a415e5bf3cce2102c0453d022f6ddfdb 100644 (file)
@@ -2912,7 +2912,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
   if (Left.is(tok::colon) && Left.isOneOf(TT_DictLiteral, TT_ObjCMethodExpr)) {
     if ((Style.Language == FormatStyle::LK_Proto ||
          Style.Language == FormatStyle::LK_TextProto) &&
-        Right.isStringLiteral())
+        !Style.AlwaysBreakBeforeMultilineStrings && Right.isStringLiteral())
       return false;
     return true;
   }
index 6db44c765b4f918729b2dd26f829d124d95fb19e..66c5d3778d4a3c3f3b39bf0e9c5631ae085c3ee3 100644 (file)
@@ -158,9 +158,8 @@ TEST_F(FormatTestProto, MessageFieldAttributes) {
                "    key: 'a'  //\n"
                "  }\n"
                "];");
-  verifyFormat("optional string test = 1 [default =\n"
-               "                              \"test\"\n"
-               "                              \"test\"];");
+  verifyFormat("optional string test = 1 [default = \"test\"\n"
+               "                                    \"test\"];");
   verifyFormat("optional Aaaaaaaa aaaaaaaa = 12 [\n"
                "  (aaa) = aaaa,\n"
                "  (bbbbbbbbbbbbbbbbbbbbbbbbbb) = {\n"
index 39a2e71cbbd13168322e4c52a2ad9b687fc80b0c..1f2f2b61236d0df5aae4c11c7fbbc9c1a7516414 100644 (file)
@@ -143,6 +143,23 @@ TEST_F(FormatTestTextProto, AddsNewlinesAfterTrailingComments) {
                "}");
 }
 
+TEST_F(FormatTestTextProto, ImplicitStringLiteralConcatenation) {
+  verifyFormat("field_a: 'aaaaa'\n"
+               "         'bbbbb'");
+  verifyFormat("field_a: \"aaaaa\"\n"
+               "         \"bbbbb\"");
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+  Style.AlwaysBreakBeforeMultilineStrings = true;
+  verifyFormat("field_a:\n"
+               "    'aaaaa'\n"
+               "    'bbbbb'",
+               Style);
+  verifyFormat("field_a:\n"
+               "    \"aaaaa\"\n"
+               "    \"bbbbb\"",
+               Style);
+}
+
 TEST_F(FormatTestTextProto, SupportsAngleBracketMessageFields) {
   // Single-line tests
   verifyFormat("msg_field <>");