From: Daniel Jasper Date: Mon, 12 Mar 2018 10:32:18 +0000 (+0000) Subject: clang-format: Properly handle implicit string concatenation in text protos X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ab55018afcf7651d15b7e0aa4b4cf0f9e25904d2;p=clang clang-format: Properly handle implicit string concatenation in text protos 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 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 5807db9407..5c7ce85178 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -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. diff --git a/lib/Format/FormatTokenLexer.cpp b/lib/Format/FormatTokenLexer.cpp index 199d2974c5..284a3a2b92 100644 --- a/lib/Format/FormatTokenLexer.cpp +++ b/lib/Format/FormatTokenLexer.cpp @@ -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); } diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 97a201d267..1e17a7d3a4 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; } diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp index 6db44c765b..66c5d3778d 100644 --- a/unittests/Format/FormatTestProto.cpp +++ b/unittests/Format/FormatTestProto.cpp @@ -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" diff --git a/unittests/Format/FormatTestTextProto.cpp b/unittests/Format/FormatTestTextProto.cpp index 39a2e71cbb..1f2f2b6123 100644 --- a/unittests/Format/FormatTestTextProto.cpp +++ b/unittests/Format/FormatTestTextProto.cpp @@ -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 <>");