]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix parsing of msg{field}-style proto options
authorKrasimir Georgiev <krasimir@google.com>
Thu, 29 Jun 2017 13:30:41 +0000 (13:30 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Thu, 29 Jun 2017 13:30:41 +0000 (13:30 +0000)
Summary:
This patch makes the `{` in `msg_field{field: OK}` in a proto option scope be
treated as an assignment operator. Previosly the added test case was formatted
as:
```
option (MyProto.options) = {
  field_a: OK
  field_b{field_c: OK} field_d: OKOKOK field_e: OK
}
```

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@306672 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 767096b60ef29f31a442556a42d887321a3fa539..d78a37532fe8811068da8c0a77ad7ab5086dccae 100644 (file)
@@ -1570,8 +1570,10 @@ private:
       const FormatToken *NextNonComment = Current->getNextNonComment();
       if (Current->is(TT_ConditionalExpr))
         return prec::Conditional;
-      if (NextNonComment && NextNonComment->is(tok::colon) &&
-          NextNonComment->is(TT_DictLiteral))
+      if (NextNonComment && Current->is(TT_SelectorName) &&
+          (NextNonComment->is(TT_DictLiteral) ||
+           (Style.Language == FormatStyle::LK_Proto &&
+            NextNonComment->is(tok::less))))
         return prec::Assignment;
       if (Current->is(TT_JsComputedPropertyName))
         return prec::Assignment;
index 0b052bd4c649cf74c0a8c9679c8a9893d8c9f922..2e3b9311d12c6a0d8a504a417ad8c5b3efd9f38b 100644 (file)
@@ -201,6 +201,12 @@ TEST_F(FormatTestProto, FormatsOptions) {
                "  field_c: \"OK\"\n"
                "  msg_field{field_d: 123}\n"
                "};");
+  verifyFormat("option (MyProto.options) = {\n"
+               "  field_a: OK\n"
+               "  field_b{field_c: OK}\n"
+               "  field_d: OKOKOK\n"
+               "  field_e: OK\n"
+               "}");
 
   // Support syntax with <> instead of {}.
   verifyFormat("option (MyProto.options) = {\n"
@@ -208,6 +214,13 @@ TEST_F(FormatTestProto, FormatsOptions) {
                "  msg_field: <field_d: 123>\n"
                "};");
 
+  verifyFormat("option (MyProto.options) = {\n"
+               "  field_a: OK\n"
+               "  field_b<field_c: OK>\n"
+               "  field_d: OKOKOK\n"
+               "  field_e: OK\n"
+               "}");
+
   verifyFormat("option (MyProto.options) = {\n"
                "  msg_field: <>\n"
                "  field_c: \"OK\",\n"