From a87ba1c59cb9f3381216cfe4e1a6a1e5425225ca 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) llvm-svn: 356833 --- clang/lib/Format/UnwrappedLineParser.cpp | 4 ++++ clang/unittests/Format/FormatTestProto.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 814e814..5c7ab12 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/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/clang/unittests/Format/FormatTestProto.cpp b/clang/unittests/Format/FormatTestProto.cpp index f4196f7..d5683b5 100644 --- a/clang/unittests/Format/FormatTestProto.cpp +++ b/clang/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.7.4