[PATCH] D59629: [clang-format] correctly format protobuf fields named "enum".

Donald Chai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 20 21:35:39 PDT 2019


dchai created this revision.
dchai added a reviewer: krasimir.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Similar to TypeScript, "enum" is not a reserved word.


Repository:
  rC Clang

https://reviews.llvm.org/D59629

Files:
  lib/Format/UnwrappedLineParser.cpp
  unittests/Format/FormatTestProto.cpp


Index: unittests/Format/FormatTestProto.cpp
===================================================================
--- unittests/Format/FormatTestProto.cpp
+++ unittests/Format/FormatTestProto.cpp
@@ -107,6 +107,12 @@
                "};");
 }
 
+TEST_F(FormatTestProto, EnumAsFieldName) {
+  verifyFormat("message SomeMessage {\n"
+               "  required int32 enum = 1;\n"
+               "}");
+}
+
 TEST_F(FormatTestProto, UnderstandsReturns) {
   verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
 }
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -2018,6 +2018,10 @@
       FormatTok->isOneOf(tok::colon, tok::question))
     return false;
 
+  // In protobuf, "enum" can be used as a field name.
+  if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
+    return false;
+
   // Eat up enum class ...
   if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
     nextToken();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D59629.191630.patch
Type: text/x-patch
Size: 1088 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190321/f8b60afe/attachment.bin>


More information about the cfe-commits mailing list