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
This commit is contained in:
Daniel Jasper
2018-03-12 10:32:18 +00:00
parent 19e238746b
commit 9c95dfe658
5 changed files with 24 additions and 5 deletions

View 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.

View 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);
}

View 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;
}

View 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"

View 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 <>");