Skip to content

Commit 1c3e2fe

Browse files
nbeloglazovcopybara-github
authored andcommitted
Annotate FieldNumber constant emitted for proto extended fields.
Example: extend MyMessage { string new_string = 123; } Generates the following C++ constant: kNewStringFieldNumber = 123; Clicking on it leads to the generated files instead of the proto file. This change fixes that. Tested: added test case for testing annotations emitted for `extend` fields PiperOrigin-RevId: 842244563
1 parent ccdb652 commit 1c3e2fe

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/google/protobuf/compiler/cpp/extension.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ void ExtensionGenerator::GenerateFeatureDefaults(io::Printer* p) const {
131131

132132
void ExtensionGenerator::GenerateDeclaration(io::Printer* p) const {
133133
auto var = p->WithVars(variables_);
134-
auto annotate = p->WithAnnotations({{"name", descriptor_}});
134+
auto annotate = p->WithAnnotations(
135+
{{"name", descriptor_}, {"constant_name", descriptor_}});
135136
p->Emit(
136137
{
137138
{"constant_qualifier",

src/google/protobuf/compiler/cpp/metadata_test.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,36 @@ TEST_F(CppMetadataTest, AnnotatesLazyMessageSemantics) {
572572
{"mutable_mfield", Annotation::ALIAS},
573573
{"clear_mfield", Annotation::SET}});
574574
}
575+
576+
constexpr absl::string_view kExtensionFieldTestFile = R"(
577+
syntax = "proto2";
578+
package foo;
579+
message Message {
580+
extensions 100 to 199;
581+
}
582+
extend Message {
583+
optional int32 extfield = 100;
584+
}
585+
)";
586+
587+
TEST_F(CppMetadataTest, AnnotatesExtensionSemantics) {
588+
FileDescriptorProto file;
589+
GeneratedCodeInfo info;
590+
std::string pb_h;
591+
atu::AddFile("test.proto", kExtensionFieldTestFile);
592+
EXPECT_TRUE(CaptureMetadata("test.proto", &file, &pb_h, &info, nullptr,
593+
nullptr, nullptr));
594+
EXPECT_EQ("extfield", file.extension(0).name());
595+
596+
// Check annotations for `extfield`.
597+
std::vector<int> field_path{
598+
FileDescriptorProto::kExtensionFieldNumber,
599+
0,
600+
};
601+
ExpectAnnotationsForPathContain(info, "test.proto", pb_h, field_path,
602+
{{"extfield", Annotation::NONE},
603+
{"kExtfieldFieldNumber", Annotation::NONE}});
604+
}
575605
} // namespace
576606
} // namespace cpp
577607
} // namespace compiler

0 commit comments

Comments
 (0)