diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc index a3ae2a6057..c8f5dfe487 100644 --- a/src/google/protobuf/compiler/cpp/message.cc +++ b/src/google/protobuf/compiler/cpp/message.cc @@ -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_) && diff --git a/src/google/protobuf/compiler/cpp/tracker.cc b/src/google/protobuf/compiler/cpp/tracker.cc index db9fbe225a..43662babc2 100644 --- a/src/google/protobuf/compiler/cpp/tracker.cc +++ b/src/google/protobuf/compiler/cpp/tracker.cc @@ -180,6 +180,8 @@ std::vector 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. diff --git a/src/google/protobuf/field_access_listener.h b/src/google/protobuf/field_access_listener.h index fd54f0e63e..162674c3dd 100644 --- a/src/google/protobuf/field_access_listener.h +++ b/src/google/protobuf/field_access_listener.h @@ -128,6 +128,12 @@ struct NoOpAccessListener { template 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.