From: Krasimir Georgiev Date: Tue, 27 Feb 2018 19:07:47 +0000 (+0000) Subject: [clang-format] Format operator key in protos X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9e5c2f4ede18c614ec3d32cc9c6e8f8c4d19a4d5;p=clang [clang-format] Format operator key in protos 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326227 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index f56230ee88..494b98cd73 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -759,6 +759,9 @@ private: 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)) diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp index 1d6b7502e0..6db44c765b 100644 --- a/unittests/Format/FormatTestProto.cpp +++ b/unittests/Format/FormatTestProto.cpp @@ -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 diff --git a/unittests/Format/FormatTestTextProto.cpp b/unittests/Format/FormatTestTextProto.cpp index 17cde61d2a..1102055ea3 100644 --- a/unittests/Format/FormatTestTextProto.cpp +++ b/unittests/Format/FormatTestTextProto.cpp @@ -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