Skip to content

Commit acffab7

Browse files
Internal change
PiperOrigin-RevId: 850074730
1 parent 9100b0a commit acffab7

File tree

4 files changed

+60
-31
lines changed

4 files changed

+60
-31
lines changed

src/google/protobuf/compiler/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ cc_library(
7171
"//src/google/protobuf/io:io_win32",
7272
"//src/google/protobuf/io:tokenizer",
7373
"@abseil-cpp//absl/base",
74+
"@abseil-cpp//absl/base:core_headers",
7475
"@abseil-cpp//absl/cleanup",
7576
"@abseil-cpp//absl/container:flat_hash_map",
7677
"@abseil-cpp//absl/container:flat_hash_set",
@@ -552,9 +553,11 @@ cc_test(
552553
"//src/google/protobuf:test_util2",
553554
"//src/google/protobuf/compiler:retention",
554555
"//src/google/protobuf/io",
556+
"//src/google/protobuf/io:tokenizer",
555557
"//src/google/protobuf/stubs",
556558
"//src/google/protobuf/testing",
557559
"//src/google/protobuf/testing:file",
560+
"@abseil-cpp//absl/base:core_headers",
558561
"@abseil-cpp//absl/container:flat_hash_map",
559562
"@abseil-cpp//absl/log:absl_check",
560563
"@abseil-cpp//absl/memory",

src/google/protobuf/compiler/parser.cc

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include "google/protobuf/compiler/parser.h"
1515

16+
#include <cassert>
17+
#include <climits>
1618
#include <cstddef>
1719
#include <cstdint>
1820
#include <limits>
@@ -21,6 +23,7 @@
2123
#include <utility>
2224
#include <vector>
2325

26+
#include "absl/base/attributes.h"
2427
#include "absl/cleanup/cleanup.h"
2528
#include "absl/container/flat_hash_map.h"
2629
#include "absl/container/flat_hash_set.h"
@@ -75,10 +78,29 @@ const TypeNameMap& GetTypeNameTable() {
7578
return *table;
7679
}
7780

78-
const TypeNameMap& GetUnstableVarintTypeNameTable() {
81+
const TypeNameMap& GetFixedFirstTypeNameTable() {
7982
static auto* table = new auto([]() {
8083
TypeNameMap result;
8184

85+
result["double"] = FieldDescriptorProto::TYPE_DOUBLE;
86+
result["float"] = FieldDescriptorProto::TYPE_FLOAT;
87+
result["uint64"] = FieldDescriptorProto::TYPE_UINT64;
88+
result["fixed64"] = FieldDescriptorProto::TYPE_FIXED64;
89+
result["fixed32"] = FieldDescriptorProto::TYPE_FIXED32;
90+
result["bool"] = FieldDescriptorProto::TYPE_BOOL;
91+
result["string"] = FieldDescriptorProto::TYPE_STRING;
92+
result["group"] = FieldDescriptorProto::TYPE_GROUP;
93+
94+
result["bytes"] = FieldDescriptorProto::TYPE_BYTES;
95+
result["uint32"] = FieldDescriptorProto::TYPE_UINT32;
96+
result["sfixed32"] = FieldDescriptorProto::TYPE_SFIXED32;
97+
result["sfixed64"] = FieldDescriptorProto::TYPE_SFIXED64;
98+
result["int32"] = FieldDescriptorProto::TYPE_INT32;
99+
result["int64"] = FieldDescriptorProto::TYPE_INT64;
100+
result["sint32"] = FieldDescriptorProto::TYPE_SINT32;
101+
result["sint64"] = FieldDescriptorProto::TYPE_SINT64;
102+
103+
// New types added to preserve legacy behavior.
82104
result["varint32"] = FieldDescriptorProto::TYPE_INT32;
83105
result["varint64"] = FieldDescriptorProto::TYPE_INT64;
84106
result["uvarint32"] = FieldDescriptorProto::TYPE_UINT32;
@@ -468,7 +490,7 @@ void Parser::LocationRecorder::AttachComments(
468490
if (!trailing->empty()) {
469491
location_->mutable_trailing_comments()->swap(*trailing);
470492
}
471-
for (int i = 0; i < detached_comments->size(); ++i) {
493+
for (size_t i = 0; i < detached_comments->size(); ++i) {
472494
location_->add_leading_detached_comments()->swap((*detached_comments)[i]);
473495
}
474496
detached_comments->clear();
@@ -1315,7 +1337,7 @@ bool Parser::ParseDefaultAssignment(
13151337
DO(ConsumeInteger64(max_value, &value,
13161338
"Expected integer for field default value."));
13171339
// And stringify it again.
1318-
default_value->append(absl::StrCat(value));
1340+
absl::StrAppend(default_value, value);
13191341
break;
13201342
}
13211343

@@ -1338,7 +1360,7 @@ bool Parser::ParseDefaultAssignment(
13381360
DO(ConsumeInteger64(max_value, &value,
13391361
"Expected integer for field default value."));
13401362
// And stringify it again.
1341-
default_value->append(absl::StrCat(value));
1363+
absl::StrAppend(default_value, value);
13421364
break;
13431365
}
13441366

@@ -2427,9 +2449,16 @@ bool Parser::ParseLabel(FieldDescriptorProto::Label* label,
24272449
return true;
24282450
}
24292451

2452+
bool Parser::ShouldUseFixedFirstType() const {
2453+
return syntax_identifier_ == "editions" &&
2454+
edition_ == Edition::EDITION_UNSTABLE;
2455+
}
2456+
24302457
bool Parser::ParseType(FieldDescriptorProto::Type* type,
24312458
std::string* type_name) {
2432-
const auto& type_names_table = GetTypeNameTable();
2459+
const auto& type_names_table = ShouldUseFixedFirstType()
2460+
? GetFixedFirstTypeNameTable()
2461+
: GetTypeNameTable();
24332462
auto iter = type_names_table.find(input_->current().text);
24342463
if (iter != type_names_table.end()) {
24352464
if (syntax_identifier_ == "editions" &&
@@ -2442,17 +2471,6 @@ bool Parser::ParseType(FieldDescriptorProto::Type* type,
24422471
*type = iter->second;
24432472
input_->Next();
24442473
} else {
2445-
// EDITION_UNSTABLE supports new numeric types.
2446-
if (syntax_identifier_ == "editions" &&
2447-
edition_ == Edition::EDITION_UNSTABLE) {
2448-
const auto& type_names_table = GetUnstableVarintTypeNameTable();
2449-
auto iter = type_names_table.find(input_->current().text);
2450-
if (iter != type_names_table.end()) {
2451-
*type = iter->second;
2452-
input_->Next();
2453-
return true;
2454-
}
2455-
}
24562474
DO(ParseUserDefinedType(type_name));
24572475
}
24582476
return true;

src/google/protobuf/compiler/parser.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <string>
1919
#include <type_traits>
2020
#include <utility>
21+
#include <vector>
2122

2223
#include "absl/container/flat_hash_map.h"
2324
#include "absl/strings/string_view.h"
@@ -124,6 +125,8 @@ class PROTOBUF_EXPORT Parser final {
124125
// ending after the closing '}' is encountered and consumed, or at EOF.
125126
void SkipRestOfBlock();
126127

128+
bool ShouldUseFixedFirstType() const;
129+
127130
// -----------------------------------------------------------------
128131
// Single-token consuming helpers
129132
//
@@ -372,10 +375,10 @@ class PROTOBUF_EXPORT Parser final {
372375
bool ParseMessageStatement(DescriptorProto* message,
373376
const LocationRecorder& message_location,
374377
const FileDescriptorProto* containing_file);
375-
bool ParseEnumStatement(EnumDescriptorProto* message,
378+
bool ParseEnumStatement(EnumDescriptorProto* enum_type,
376379
const LocationRecorder& enum_location,
377380
const FileDescriptorProto* containing_file);
378-
bool ParseServiceStatement(ServiceDescriptorProto* message,
381+
bool ParseServiceStatement(ServiceDescriptorProto* service,
379382
const LocationRecorder& service_location,
380383
const FileDescriptorProto* containing_file);
381384

@@ -422,13 +425,13 @@ class PROTOBUF_EXPORT Parser final {
422425
bool ParseReservedIdentifier(std::string* name, ErrorMaker error_message);
423426
bool ParseReservedNumbers(DescriptorProto* message,
424427
const LocationRecorder& parent_location);
425-
bool ParseReserved(EnumDescriptorProto* message,
426-
const LocationRecorder& message_location);
427-
bool ParseReservedNames(EnumDescriptorProto* message,
428+
bool ParseReserved(EnumDescriptorProto* proto,
429+
const LocationRecorder& enum_location);
430+
bool ParseReservedNames(EnumDescriptorProto* proto,
428431
const LocationRecorder& parent_location);
429-
bool ParseReservedIdentifiers(EnumDescriptorProto* message,
432+
bool ParseReservedIdentifiers(EnumDescriptorProto* proto,
430433
const LocationRecorder& parent_location);
431-
bool ParseReservedNumbers(EnumDescriptorProto* message,
434+
bool ParseReservedNumbers(EnumDescriptorProto* proto,
432435
const LocationRecorder& parent_location);
433436

434437
// Parse an "extend" declaration. (See also comments for

src/google/protobuf/compiler/parser_unittest.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,36 @@
1313

1414
#include <algorithm>
1515
#include <cmath>
16+
#include <cstddef>
1617
#include <memory>
1718
#include <string>
1819
#include <utility>
1920
#include <vector>
2021

22+
#include "google/protobuf/testing/file.h"
2123
#include "google/protobuf/any.pb.h"
2224
#include "google/protobuf/descriptor.pb.h"
2325
#include <gmock/gmock.h>
2426
#include "google/protobuf/testing/googletest.h"
2527
#include <gtest/gtest.h>
28+
#include "absl/base/macros.h"
2629
#include "absl/container/flat_hash_map.h"
2730
#include "absl/log/absl_check.h"
2831
#include "absl/memory/memory.h"
32+
#include "absl/strings/match.h"
2933
#include "absl/strings/str_cat.h"
3034
#include "absl/strings/str_join.h"
35+
#include "absl/strings/string_view.h"
3136
#include "absl/strings/substitute.h"
32-
#include "google/protobuf/compiler/retention.h"
3337
#include "google/protobuf/descriptor.h"
38+
#include "google/protobuf/io/tokenizer.h"
39+
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
3440
#include "google/protobuf/test_util2.h"
3541
#include "google/protobuf/text_format.h"
3642
#include "google/protobuf/unittest.pb.h"
3743
#include "google/protobuf/unittest_custom_options.pb.h"
3844
#include "google/protobuf/unittest_import.pb.h"
3945
#include "google/protobuf/unittest_import_public.pb.h"
40-
#include "google/protobuf/wire_format.h"
4146

4247

4348
// Must be included last.
@@ -232,8 +237,8 @@ TEST_F(ParserTest, WarnIfSyntaxIdentifierOmitted) {
232237
FileDescriptorProto file;
233238
CaptureTestStderr();
234239
EXPECT_TRUE(parser_->Parse(input_.get(), &file));
235-
EXPECT_TRUE(GetCapturedTestStderr().find("No edition or syntax specified") !=
236-
std::string::npos);
240+
EXPECT_TRUE(absl::StrContains(GetCapturedTestStderr(),
241+
"No edition or syntax specified"));
237242
}
238243

239244
TEST_F(ParserTest, RegressionNestedOpenBraceDoNotStackOverflow) {
@@ -3356,7 +3361,7 @@ TEST_F(ParseDescriptorDebugTest, TestCommentsInDebugString) {
33563361
const std::string debug_string =
33573362
descriptor->DebugStringWithOptions(debug_string_options);
33583363

3359-
for (int i = 0; i < ABSL_ARRAYSIZE(expected_comments); ++i) {
3364+
for (size_t i = 0; i < ABSL_ARRAYSIZE(expected_comments); ++i) {
33603365
std::string::size_type found_pos =
33613366
debug_string.find(expected_comments[i]);
33623367
EXPECT_TRUE(found_pos != std::string::npos)
@@ -3391,9 +3396,9 @@ TEST_F(ParseDescriptorDebugTest, TestMaps) {
33913396
// Make sure the debug string uses map syntax and does not have the auto
33923397
// generated entry.
33933398
std::string debug_string = file->DebugString();
3394-
EXPECT_TRUE(debug_string.find("map<") != std::string::npos);
3395-
EXPECT_TRUE(debug_string.find("option map_entry") == std::string::npos);
3396-
EXPECT_TRUE(debug_string.find("MapEntry") == std::string::npos);
3399+
EXPECT_TRUE(absl::StrContains(debug_string, "map<"));
3400+
EXPECT_TRUE(!absl::StrContains(debug_string, "option map_entry"));
3401+
EXPECT_TRUE(!absl::StrContains(debug_string, "MapEntry"));
33973402

33983403
// Make sure the descriptor debug string is parsable.
33993404
FileDescriptorProto parsed;

0 commit comments

Comments
 (0)