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: fix source code info location for missing label #6436

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/google/protobuf/compiler/parser.cc
Expand Up @@ -902,10 +902,8 @@ bool Parser::ParseMessageField(FieldDescriptorProto* field,
const LocationRecorder& field_location,
const FileDescriptorProto* containing_file) {
{
LocationRecorder location(field_location,
FieldDescriptorProto::kLabelFieldNumber);
FieldDescriptorProto::Label label;
if (ParseLabel(&label, containing_file)) {
if (ParseLabel(&label, field_location, containing_file)) {
field->set_label(label);
if (label == FieldDescriptorProto::LABEL_OPTIONAL &&
syntax_identifier_ == "proto3") {
Expand Down Expand Up @@ -2206,18 +2204,22 @@ bool Parser::ParseMethodOptions(const LocationRecorder& parent_location,
// -------------------------------------------------------------------

bool Parser::ParseLabel(FieldDescriptorProto::Label* label,
const LocationRecorder& field_location,
const FileDescriptorProto* containing_file) {
if (!LookingAt("optional") && !LookingAt("repeated") && !LookingAt("required")) {
return false;
}
LocationRecorder location(field_location,
FieldDescriptorProto::kLabelFieldNumber);
if (TryConsume("optional")) {
*label = FieldDescriptorProto::LABEL_OPTIONAL;
return true;
} else if (TryConsume("repeated")) {
*label = FieldDescriptorProto::LABEL_REPEATED;
return true;
} else if (TryConsume("required")) {
} else {
Consume("required");
*label = FieldDescriptorProto::LABEL_REQUIRED;
return true;
}
return false;
return true;
}

bool Parser::ParseType(FieldDescriptorProto::Type* type,
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/compiler/parser.h
Expand Up @@ -440,6 +440,7 @@ class PROTOBUF_EXPORT Parser {
// Parse "required", "optional", or "repeated" and fill in "label"
// with the value. Returns true if such a label is consumed.
bool ParseLabel(FieldDescriptorProto::Label* label,
const LocationRecorder& field_location,
const FileDescriptorProto* containing_file);

// Parse a type name and fill in "type" (if it is a primitive) or
Expand Down
29 changes: 29 additions & 0 deletions src/google/protobuf/compiler/parser_unittest.cc
Expand Up @@ -2808,6 +2808,35 @@ TEST_F(SourceInfoTest, Fields) {
EXPECT_TRUE(HasSpan(file_.message_type(0), "name"));
}

TEST_F(SourceInfoTest, Proto3Fields) {
EXPECT_TRUE(
Parse("syntax = \"proto3\";\n"
"message Foo {\n"
" $a$int32$b$ $c$bar$d$ = $e$1$f$;$g$\n"
" $h$repeated$i$ $j$X.Y$k$ $l$baz$m$ = $n$2$o$;$p$\n"
"}\n"));

const FieldDescriptorProto& field1 = file_.message_type(0).field(0);
const FieldDescriptorProto& field2 = file_.message_type(0).field(1);

EXPECT_TRUE(HasSpan('a', 'g', field1));
EXPECT_TRUE(HasSpan('a', 'b', field1, "type"));
EXPECT_TRUE(HasSpan('c', 'd', field1, "name"));
EXPECT_TRUE(HasSpan('e', 'f', field1, "number"));

EXPECT_TRUE(HasSpan('h', 'p', field2));
EXPECT_TRUE(HasSpan('h', 'i', field2, "label"));
EXPECT_TRUE(HasSpan('j', 'k', field2, "type_name"));
EXPECT_TRUE(HasSpan('l', 'm', field2, "name"));
EXPECT_TRUE(HasSpan('n', 'o', field2, "number"));

// Ignore these.
EXPECT_TRUE(HasSpan(file_));
EXPECT_TRUE(HasSpan(file_, "syntax"));
EXPECT_TRUE(HasSpan(file_.message_type(0)));
EXPECT_TRUE(HasSpan(file_.message_type(0), "name"));
}

TEST_F(SourceInfoTest, Extensions) {
EXPECT_TRUE(
Parse("$a$extend $b$Foo$c$ {\n"
Expand Down