]> granicus.if.org Git - clang/commitdiff
clang-format: Enable formatting of protocol buffer definitions.
authorDaniel Jasper <djasper@google.com>
Fri, 30 Aug 2013 10:36:58 +0000 (10:36 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 30 Aug 2013 10:36:58 +0000 (10:36 +0000)
Almost by accident, clang-format seems to be able to format protocol
buffer definitions (https://code.google.com/p/protobuf/).

The only change is that a space is required between numeric constants
and opening square brackets (for default values). While this might in
theory be used for array subscripts (int val = 4[MyArray]), I have not
seen this pattern in practice much. If this is wrong, we can make this
smarter in the future.

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

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index ce837d42ccc1bba752a0fc17bed7ace9e96483b1..d2cb5a8723b725233d42bfcb7467fe48a579efe4 100644 (file)
@@ -1206,7 +1206,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
     return Left.Type == TT_ObjCArrayLiteral && Right.isNot(tok::r_square);
   if (Right.is(tok::r_square))
     return Right.Type == TT_ObjCArrayLiteral;
-  if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr)
+  if (Right.is(tok::l_square) && Right.Type != TT_ObjCMethodExpr &&
+      Left.isNot(tok::numeric_constant))
     return false;
   if (Left.is(tok::colon))
     return Left.Type != TT_ObjCMethodExpr;
index aef1dae6fdf40ac68ea6f825cff65a2ee0da37f9..c30133a4b8ae268ad2c262c7fb910f577ef7ae11 100644 (file)
@@ -6242,5 +6242,14 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) {
             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
 }
 
+TEST_F(FormatTest, FormatsProtocolBufferDefinitions) {
+  // It seems that clang-format can format protocol buffer definitions
+  // (see https://code.google.com/p/protobuf/).
+  verifyFormat("message SomeMessage {\n"
+               "  required int32 field1 = 1;\n"
+               "  optional string field2 = 2 [default = \"2\"]\n"
+               "}");
+}
+
 } // end namespace tooling
 } // end namespace clang