From f503b6985438025bd8fee75a547c9aec8b0a0c74 Mon Sep 17 00:00:00 2001 From: Paul Hoad Date: Sat, 23 Mar 2019 14:24:30 +0000 Subject: [PATCH] [clang-format] correctly format protobuf fields named "enum". Summary: Similar to TypeScript, "enum" is not a reserved word. Reviewers: krasimir, MyDeveloperDay Reviewed By: MyDeveloperDay Subscribers: MyDeveloperDay, cfe-commits Tags: #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D59629 Patch by: dchai (Donald Chai) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@356833 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineParser.cpp | 4 ++++ unittests/Format/FormatTestProto.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 814e8143e1..5c7ab1240c 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -2018,6 +2018,10 @@ bool UnwrappedLineParser::parseEnum() { 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(); diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp index f4196f731f..d5683b5b84 100644 --- a/unittests/Format/FormatTestProto.cpp +++ b/unittests/Format/FormatTestProto.cpp @@ -107,6 +107,12 @@ TEST_F(FormatTestProto, FormatsEnums) { "};"); } +TEST_F(FormatTestProto, EnumAsFieldName) { + verifyFormat("message SomeMessage {\n" + " required int32 enum = 1;\n" + "}"); +} + TEST_F(FormatTestProto, UnderstandsReturns) { verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);"); } -- 2.40.0