Add proto hooks for unknown fields.

PiperOrigin-RevId: 531306472
pull/12770/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 0c51f796a4
commit 56d1b0f3f5
  1. 20
      src/google/protobuf/compiler/cpp/message.cc
  2. 2
      src/google/protobuf/compiler/cpp/tracker.cc
  3. 6
      src/google/protobuf/field_access_listener.h

@ -1302,14 +1302,18 @@ void MessageGenerator::GenerateClassDefinition(io::Printer* p) {
"}\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");
p->Emit(R"cc(
inline const $unknown_fields_type$& unknown_fields() const {
$annotate_unknown_fields$;
return $unknown_fields$;
}
inline $unknown_fields_type$* mutable_unknown_fields() {
$annotate_mutable_unknown_fields$;
return $mutable_unknown_fields$;
}
)cc");
// Adding a blank line to be consistent with the previous version.
p->Emit("\n");
// Only generate this member if it's not disabled.
if (HasDescriptorMethods(descriptor_->file(), options_) &&

@ -180,6 +180,8 @@ std::vector<Sub> MakeTrackerCalls(const Descriptor* message,
Call("reflection", "OnGetMetadata").This(absl::nullopt),
Call("bytesize", "OnByteSize"),
Call("mergefrom", "OnMergeFrom").This("_this").Arg("&from"),
Call("unknown_fields", "OnUnknownFields"),
Call("mutable_unknown_fields", "OnMutableUnknownFields"),
// "Has" is here as users calling "has" on a repeated field is a
// mistake.

@ -128,6 +128,12 @@ struct NoOpAccessListener {
template <int kFieldNum>
static void OnSize(const MessageLite* msg, const void* field) {}
// unknown_fields()
static void OnUnknownFields(const MessageLite* msg) {}
// mutable_unknown_fields()
static void OnMutableUnknownFields(const MessageLite* msg) {}
static void OnHasExtension(const MessageLite* msg, int extension_tag,
const void* field) {}
// TODO(b/190614678): Support clear in the proto compiler.

Loading…
Cancel
Save