Skip to content

Commit

Permalink
[clang-format] Format operator key in protos
Browse files Browse the repository at this point in the history
Summary: This fixes a glitch where ``operator: value`` in a text proto would mess up the underlying formatting since it gets parsed as a kw_operator instead of an identifier.

Subscribers: klimek, cfe-commits

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

llvm-svn: 326227
  • Loading branch information
krasimirgg committed Feb 27, 2018
1 parent e8436e8 commit 0aa4b4c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -759,6 +759,9 @@ class AnnotatingParser {
Tok->Type = TT_BinaryOperator;
break;
case tok::kw_operator:
if (Style.Language == FormatStyle::LK_TextProto ||
Style.Language == FormatStyle::LK_Proto)
break;
while (CurrentToken &&
!CurrentToken->isOneOf(tok::l_paren, tok::semi, tok::r_paren)) {
if (CurrentToken->isOneOf(tok::star, tok::amp))
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/FormatTestProto.cpp
Expand Up @@ -478,5 +478,17 @@ TEST_F(FormatTestProto, FormatsRepeatedListInitializersInOptions) {
"};");
}

TEST_F(FormatTestProto, AcceptsOperatorAsKeyInOptions) {
verifyFormat("option (MyProto.options) = {\n"
" bbbbbbbbb: <\n"
" ccccccccccccccccccccccc: <\n"
" operator: 1\n"
" operator: 2\n"
" operator { key: value }\n"
" >\n"
" >\n"
"};");
}

} // end namespace tooling
} // end namespace clang
12 changes: 12 additions & 0 deletions clang/unittests/Format/FormatTestTextProto.cpp
Expand Up @@ -440,5 +440,17 @@ TEST_F(FormatTestTextProto, FormatsRepeatedListInitializers) {
Style.Cpp11BracedListStyle = true;
verifyFormat("keys: [1]", Style);
}

TEST_F(FormatTestTextProto, AcceptsOperatorAsKey) {
verifyFormat("aaaaaaaaaaa: <\n"
" bbbbbbbbb: <\n"
" ccccccccccccccccccccccc: <\n"
" operator: 1\n"
" operator: 2\n"
" operator { key: value }\n"
" >\n"
" >\n"
">");
}
} // end namespace tooling
} // end namespace clang

0 comments on commit 0aa4b4c

Please sign in to comment.