Skip to content

Commit

Permalink
Add proto hooks for unknown fields.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 531306472
  • Loading branch information
protobuf-github-bot authored and copybara-github committed May 11, 2023
1 parent 0c51f79 commit 56d1b0f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_) &&
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/compiler/cpp/tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/google/protobuf/field_access_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 56d1b0f

Please sign in to comment.