Skip to content

Commit

Permalink
Unconditionally generate unknown field accessors.
Browse files Browse the repository at this point in the history
This moves us closer to unifying proto2 and proto3 into editions.

PiperOrigin-RevId: 513268016
  • Loading branch information
mkruskal-google authored and Copybara-Service committed Mar 1, 2023
1 parent 7fc47e2 commit dd8a3cf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
5 changes: 0 additions & 5 deletions src/google/protobuf/compiler/cpp/helpers.h
Expand Up @@ -323,11 +323,6 @@ std::string SafeFunctionName(const Descriptor* descriptor,
const FieldDescriptor* field,
absl::string_view prefix);

// Returns true if generated messages have public unknown fields accessors
inline bool PublicUnknownFieldsAccessors(const Descriptor* message) {
return message->file()->syntax() != FileDescriptor::SYNTAX_PROTO3;
}

// Returns the optimize mode for <file>, respecting <options.enforce_lite>.
FileOptions_OptimizeMode GetOptimizeFor(const FileDescriptor* file,
const Options& options);
Expand Down
18 changes: 8 additions & 10 deletions src/google/protobuf/compiler/cpp/message.cc
Expand Up @@ -1288,16 +1288,14 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
"}\n"
"\n");

if (PublicUnknownFieldsAccessors(descriptor_)) {
format(
"inline const $unknown_fields_type$& unknown_fields() const {\n"
" return $unknown_fields$;\n"
"}\n"
"inline $unknown_fields_type$* mutable_unknown_fields() {\n"
" return $mutable_unknown_fields$;\n"
"}\n"
"\n");
}
format(
"inline const $unknown_fields_type$& unknown_fields() const {\n"
" return $unknown_fields$;\n"
"}\n"
"inline $unknown_fields_type$* mutable_unknown_fields() {\n"
" return $mutable_unknown_fields$;\n"
"}\n"
"\n");

// Only generate this member if it's not disabled.
if (HasDescriptorMethods(descriptor_->file(), options_) &&
Expand Down

0 comments on commit dd8a3cf

Please sign in to comment.