diff options
author | Daniel Jasper <djasper@google.com> | 2018-03-12 10:32:18 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2018-03-12 10:32:18 +0000 |
commit | 9c95dfe65863122539837ed174d07cd5ecff6010 (patch) | |
tree | 24d2ef801e40e5e3ecff8f8fdceb9b0f88a0d02e | |
parent | 19e238746b2def4c2944dd8e2a95a45119a401cb (diff) | |
download | bcm5719-llvm-9c95dfe65863122539837ed174d07cd5ecff6010.tar.gz bcm5719-llvm-9c95dfe65863122539837ed174d07cd5ecff6010.zip |
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.
llvm-svn: 327255
-rw-r--r-- | clang/lib/Format/Format.cpp | 1 | ||||
-rw-r--r-- | clang/lib/Format/FormatTokenLexer.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 2 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestProto.cpp | 5 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTestTextProto.cpp | 17 |
5 files changed, 24 insertions, 5 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 5807db9407f..5c7ce851783 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/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/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index 199d2974c5c..284a3a2b92b 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/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/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 97a201d267e..1e17a7d3a41 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/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/clang/unittests/Format/FormatTestProto.cpp b/clang/unittests/Format/FormatTestProto.cpp index 6db44c765b4..66c5d3778d4 100644 --- a/clang/unittests/Format/FormatTestProto.cpp +++ b/clang/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/clang/unittests/Format/FormatTestTextProto.cpp b/clang/unittests/Format/FormatTestTextProto.cpp index 39a2e71cbbd..1f2f2b61236 100644 --- a/clang/unittests/Format/FormatTestTextProto.cpp +++ b/clang/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 <>"); |