Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoc: parser rejects explicit use of map_entry option #13479

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/google/protobuf/compiler/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,26 @@ bool Parser::ParseMessageBlock(DescriptorProto* message,
if (message->reserved_range_size() > 0) {
AdjustReservedRangesWithMaxEndNumber(message);
}

// check if map_entry option was explicitly set
if (message->has_options()) {
const MessageOptions& opts = message->options();
for (int i = 0; i < opts.uninterpreted_option_size(); i++) {
const UninterpretedOption& opt = opts.uninterpreted_option(i);
if (opt.name_size() > 0 && !opt.name(0).is_extension()
&& opt.name(0).name_part() == "map_entry") {
int line = -1, col = 0;
if (source_location_table_ != nullptr) {
source_location_table_->Find(&opt,
DescriptorPool::ErrorCollector::OPTION_NAME,
&line, &col);
}
RecordError(line, col,
"map_entry should not be set explicitly. "
"Use map<KeyType, ValueType> instead.");
}
}
}
return true;
}

Expand Down
7 changes: 4 additions & 3 deletions src/google/protobuf/compiler/parser_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ class ParserTest : public testing::Test {
// input.
void ExpectHasEarlyExitErrors(const char* text, const char* expected_errors) {
SetupParser(text);
SourceLocationTable source_locations;
parser_->RecordSourceLocationsTo(&source_locations);
FileDescriptorProto file;
EXPECT_FALSE(parser_->Parse(input_.get(), &file));
EXPECT_EQ(expected_errors, error_collector_.text_);
Expand Down Expand Up @@ -2235,17 +2237,16 @@ TEST_F(ParserValidationErrorTest, EnumValueAliasError) {
}

TEST_F(ParserValidationErrorTest, ExplicitlyMapEntryError) {
ExpectHasValidationErrors(
ExpectHasErrors(
"message Foo {\n"
" message ValueEntry {\n"
" option map_entry = true;\n"
" optional int32 key = 1;\n"
" optional int32 value = 2;\n"
" extensions 99 to 999;\n"
" }\n"
" repeated ValueEntry value = 1;\n"
"}",
"7:11: map_entry should not be set explicitly. Use "
"2:11: map_entry should not be set explicitly. Use "
"map<KeyType, ValueType> instead.\n");
}

Expand Down