[clang-format] Format operator key in protos
authorKrasimir Georgiev <krasimir@google.com>
Tue, 27 Feb 2018 19:07:47 +0000 (19:07 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Tue, 27 Feb 2018 19:07:47 +0000 (19:07 +0000)
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

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTestProto.cpp
clang/unittests/Format/FormatTestTextProto.cpp

index f56230e..494b98c 100644 (file)
@@ -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))
index 1d6b750..6db44c7 100644 (file)
@@ -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
index 17cde61..1102055 100644 (file)
@@ -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