Skip to content

Commit

Permalink
Move generator shared support code to common target.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 458257330
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Jun 30, 2022
1 parent 125db89 commit 46e306b
Show file tree
Hide file tree
Showing 12 changed files with 880 additions and 520 deletions.
2 changes: 1 addition & 1 deletion DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static const upb_msglayout_field upb_test_MessageName__fields[2] = {
{2, UPB_SIZE(8, 8), 2, 0, 5, _UPB_MODE_SCALAR},
};
const upb_msglayout upb_test_MessageName_msginit = {
const upb_msglayout upb_test_MessageName_msg_init = {
NULL,
&upb_test_MessageName__fields[0],
UPB_SIZE(16, 16), 2, false, 2, 255,
Expand Down
55 changes: 24 additions & 31 deletions python/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,16 +324,15 @@ static PyObject* PyUpb_Descriptor_GetOneofs(PyObject* _self, void* closure) {
static PyObject* PyUpb_Descriptor_GetOptions(PyObject* _self, PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(
self, upb_MessageDef_Options(self->def),
&google_protobuf_MessageOptions_msginit,
self, upb_MessageDef_Options(self->def), &google_protobuf_MessageOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".MessageOptions");
}

static PyObject* PyUpb_Descriptor_CopyToProto(PyObject* _self,
PyObject* py_proto) {
return PyUpb_DescriptorBase_CopyToProto(
_self, (PyUpb_ToProto_Func*)&upb_MessageDef_ToProto,
&google_protobuf_DescriptorProto_msginit,
&google_protobuf_DescriptorProto_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".DescriptorProto", py_proto);
}

Expand Down Expand Up @@ -734,17 +733,16 @@ static PyObject* PyUpb_EnumDescriptor_GetHasOptions(PyObject* _self,
static PyObject* PyUpb_EnumDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(self, upb_EnumDef_Options(self->def),
&google_protobuf_EnumOptions_msginit,
PYUPB_DESCRIPTOR_PROTO_PACKAGE
".EnumOptions");
return PyUpb_DescriptorBase_GetOptions(
self, upb_EnumDef_Options(self->def), &google_protobuf_EnumOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumOptions");
}

static PyObject* PyUpb_EnumDescriptor_CopyToProto(PyObject* _self,
PyObject* py_proto) {
return PyUpb_DescriptorBase_CopyToProto(
_self, (PyUpb_ToProto_Func*)&upb_EnumDef_ToProto,
&google_protobuf_EnumDescriptorProto_msginit,
&google_protobuf_EnumDescriptorProto_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumDescriptorProto", py_proto);
}

Expand Down Expand Up @@ -825,7 +823,7 @@ static PyObject* PyUpb_EnumValueDescriptor_GetOptions(PyObject* _self,
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(
self, upb_EnumValueDef_Options(self->def),
&google_protobuf_EnumValueOptions_msginit,
&google_protobuf_EnumValueOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".EnumValueOptions");
}

Expand Down Expand Up @@ -1023,10 +1021,9 @@ static PyObject* PyUpb_FieldDescriptor_GetHasPresence(
static PyObject* PyUpb_FieldDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(self, upb_FieldDef_Options(self->def),
&google_protobuf_FieldOptions_msginit,
PYUPB_DESCRIPTOR_PROTO_PACKAGE
".FieldOptions");
return PyUpb_DescriptorBase_GetOptions(
self, upb_FieldDef_Options(self->def), &google_protobuf_FieldOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".FieldOptions");
}

static PyGetSetDef PyUpb_FieldDescriptor_Getters[] = {
Expand Down Expand Up @@ -1163,7 +1160,7 @@ static PyObject* PyUpb_FileDescriptor_GetSerializedPb(PyObject* self,
void* closure) {
return PyUpb_DescriptorBase_GetSerializedProto(
self, (PyUpb_ToProto_Func*)&upb_FileDef_ToProto,
&google_protobuf_FileDescriptorProto_msginit);
&google_protobuf_FileDescriptorProto_msg_init);
}

static PyObject* PyUpb_FileDescriptor_GetMessageTypesByName(PyObject* _self,
Expand Down Expand Up @@ -1265,17 +1262,16 @@ static PyObject* PyUpb_FileDescriptor_GetHasOptions(PyObject* _self,
static PyObject* PyUpb_FileDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(self, upb_FileDef_Options(self->def),
&google_protobuf_FileOptions_msginit,
PYUPB_DESCRIPTOR_PROTO_PACKAGE
".FileOptions");
return PyUpb_DescriptorBase_GetOptions(
self, upb_FileDef_Options(self->def), &google_protobuf_FileOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".FileOptions");
}

static PyObject* PyUpb_FileDescriptor_CopyToProto(PyObject* _self,
PyObject* py_proto) {
return PyUpb_DescriptorBase_CopyToProto(
_self, (PyUpb_ToProto_Func*)&upb_FileDef_ToProto,
&google_protobuf_FileDescriptorProto_msginit,
&google_protobuf_FileDescriptorProto_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".FileDescriptorProto", py_proto);
}

Expand Down Expand Up @@ -1379,17 +1375,16 @@ static PyObject* PyUpb_MethodDescriptor_GetOutputType(PyObject* self,
static PyObject* PyUpb_MethodDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(self, upb_MethodDef_Options(self->def),
&google_protobuf_MethodOptions_msginit,
PYUPB_DESCRIPTOR_PROTO_PACKAGE
".MethodOptions");
return PyUpb_DescriptorBase_GetOptions(
self, upb_MethodDef_Options(self->def), &google_protobuf_MethodOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".MethodOptions");
}

static PyObject* PyUpb_MethodDescriptor_CopyToProto(PyObject* _self,
PyObject* py_proto) {
return PyUpb_DescriptorBase_CopyToProto(
_self, (PyUpb_ToProto_Func*)&upb_MethodDef_ToProto,
&google_protobuf_MethodDescriptorProto_msginit,
&google_protobuf_MethodDescriptorProto_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".MethodDescriptorProto", py_proto);
}

Expand Down Expand Up @@ -1484,10 +1479,9 @@ static PyObject* PyUpb_OneofDescriptor_GetFields(PyObject* _self,
static PyObject* PyUpb_OneofDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(self, upb_OneofDef_Options(self->def),
&google_protobuf_OneofOptions_msginit,
PYUPB_DESCRIPTOR_PROTO_PACKAGE
".OneofOptions");
return PyUpb_DescriptorBase_GetOptions(
self, upb_OneofDef_Options(self->def), &google_protobuf_OneofOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".OneofOptions");
}

static PyGetSetDef PyUpb_OneofDescriptor_Getters[] = {
Expand Down Expand Up @@ -1586,16 +1580,15 @@ static PyObject* PyUpb_ServiceDescriptor_GetOptions(PyObject* _self,
PyObject* args) {
PyUpb_DescriptorBase* self = (void*)_self;
return PyUpb_DescriptorBase_GetOptions(
self, upb_ServiceDef_Options(self->def),
&google_protobuf_ServiceOptions_msginit,
self, upb_ServiceDef_Options(self->def), &google_protobuf_ServiceOptions_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".ServiceOptions");
}

static PyObject* PyUpb_ServiceDescriptor_CopyToProto(PyObject* _self,
PyObject* py_proto) {
return PyUpb_DescriptorBase_CopyToProto(
_self, (PyUpb_ToProto_Func*)&upb_ServiceDef_ToProto,
&google_protobuf_ServiceDescriptorProto_msginit,
&google_protobuf_ServiceDescriptorProto_msg_init,
PYUPB_DESCRIPTOR_PROTO_PACKAGE ".ServiceDescriptorProto", py_proto);
}

Expand Down
12 changes: 6 additions & 6 deletions upb/mini_table_accessors_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ const uint64_t kTestUInt64 = 0xFEDCBAFF87654321;

const upb_MiniTable_Field* find_proto3_field(int field_number) {
return upb_MiniTable_FindFieldByNumber(
&protobuf_test_messages_proto3_TestAllTypesProto3_msginit, field_number);
&protobuf_test_messages_proto3_TestAllTypesProto3_msg_init, field_number);
}

const upb_MiniTable_Field* find_proto2_field(int field_number) {
return upb_MiniTable_FindFieldByNumber(
&protobuf_test_messages_proto2_TestAllTypesProto2_msginit, field_number);
&protobuf_test_messages_proto2_TestAllTypesProto2_msg_init, field_number);
}

TEST(GeneratedCode, HazzersProto2) {
Expand Down Expand Up @@ -275,7 +275,7 @@ TEST(GeneratedCode, SubMessage) {

const upb_MiniTable_Field* nested_message_a_field =
upb_MiniTable_FindFieldByNumber(
&protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_msginit,
&protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_msg_init,
kFieldOptionalNestedMessageA);
EXPECT_EQ(5, upb_MiniTable_GetInt32(sub_message, nested_message_a_field));

Expand All @@ -292,7 +292,7 @@ TEST(GeneratedCode, SubMessage) {
upb_MiniTable_SetMessage(msg, optional_message_field, new_nested_message);

upb_Message* mutable_message = upb_MiniTable_GetMutableMessage(
msg, &protobuf_test_messages_proto2_TestAllTypesProto2_msginit,
msg, &protobuf_test_messages_proto2_TestAllTypesProto2_msg_init,
optional_message_field, arena);
EXPECT_EQ(
true,
Expand Down Expand Up @@ -365,10 +365,10 @@ TEST(GeneratedCode, GetMutableMessage) {
const upb_MiniTable_Field* optional_message_field =
find_proto2_field(kFieldOptionalNestedMessage);
upb_Message* msg1 = upb_MiniTable_GetMutableMessage(
msg, &protobuf_test_messages_proto2_TestAllTypesProto2_msginit,
msg, &protobuf_test_messages_proto2_TestAllTypesProto2_msg_init,
optional_message_field, arena);
upb_Message* msg2 = upb_MiniTable_GetMutableMessage(
msg, &protobuf_test_messages_proto2_TestAllTypesProto2_msginit,
msg, &protobuf_test_messages_proto2_TestAllTypesProto2_msg_init,
optional_message_field, arena);
// Verify that newly constructed sub message is stored in msg.
EXPECT_EQ(msg1, msg2);
Expand Down
8 changes: 4 additions & 4 deletions upb/msg_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ TEST(MessageTest, DecodeRequiredFieldsTopLevelMessage) {

// Fails, because required fields are missing.
EXPECT_EQ(kUpb_DecodeStatus_MissingRequired,
upb_Decode(NULL, 0, test_msg, &upb_test_TestRequiredFields_msginit,
upb_Decode(NULL, 0, test_msg, &upb_test_TestRequiredFields_msg_init,
NULL, kUpb_DecodeOption_CheckRequired, arena.ptr()));

upb_test_TestRequiredFields_set_required_int32(test_msg, 1);
Expand All @@ -327,7 +327,7 @@ TEST(MessageTest, DecodeRequiredFieldsTopLevelMessage) {
// payload is not empty.
EXPECT_EQ(kUpb_DecodeStatus_MissingRequired,
upb_Decode(serialized, size, test_msg,
&upb_test_TestRequiredFields_msginit, NULL,
&upb_test_TestRequiredFields_msg_init, NULL,
kUpb_DecodeOption_CheckRequired, arena.ptr()));

empty_msg = upb_test_EmptyMessage_new(arena.ptr());
Expand All @@ -337,7 +337,7 @@ TEST(MessageTest, DecodeRequiredFieldsTopLevelMessage) {

// Succeeds, because required fields are present (though not in the input).
EXPECT_EQ(kUpb_DecodeStatus_Ok,
upb_Decode(NULL, 0, test_msg, &upb_test_TestRequiredFields_msginit,
upb_Decode(NULL, 0, test_msg, &upb_test_TestRequiredFields_msg_init,
NULL, kUpb_DecodeOption_CheckRequired, arena.ptr()));

// Serialize a complete payload.
Expand All @@ -356,7 +356,7 @@ TEST(MessageTest, DecodeRequiredFieldsTopLevelMessage) {
test_msg2, upb_test_TestRequiredFields_new(arena.ptr()));
EXPECT_EQ(kUpb_DecodeStatus_Ok,
upb_Decode(serialized, size, test_msg2,
&upb_test_TestRequiredFields_msginit, NULL,
&upb_test_TestRequiredFields_msg_init, NULL,
kUpb_DecodeOption_CheckRequired, arena.ptr()));
}

Expand Down
39 changes: 37 additions & 2 deletions upbc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,57 @@ upb_proto_reflection_library(

cc_library(
name = "common",
srcs = ["common.cc"],
hdrs = ["common.h"],
srcs = [
"common.cc",
],
hdrs = [
"common.h",
],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "file_layout",
srcs = [
"file_layout.cc",
],
hdrs = [
"file_layout.h",
],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":common",
"//:mini_table",
"//:upb",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/strings",
"@com_google_protobuf//:protoc_lib",
],
)

cc_library(
name = "keywords",
srcs = [
"keywords.cc",
],
hdrs = [
"keywords.h",
],
copts = UPB_DEFAULT_CPPOPTS,
)

cc_binary(
name = "protoc-gen-upb",
srcs = ["protoc-gen-upb.cc"],
copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"],
deps = [
":common",
":file_layout",
"//:mini_table",
"//:port",
"//:upb",
Expand Down
8 changes: 8 additions & 0 deletions upbc/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ std::string HeaderFilename(const google::protobuf::FileDescriptor* file) {
return StripExtension(file->name()) + ".upb.h";
}

std::string MessageInit(const protobuf::Descriptor* descriptor) {
return MessageName(descriptor) + "_msg_init";
}

std::string EnumInit(const protobuf::EnumDescriptor* descriptor) {
return ToCIdent(descriptor->full_name()) + "_enum_init";
}

} // namespace upbc
13 changes: 7 additions & 6 deletions upbc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@

#include <vector>

#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/descriptor.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/substitute.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/zero_copy_stream.h"

namespace upbc {

class Output {
public:
Output(google::protobuf::io::ZeroCopyOutputStream* stream)
: stream_(stream) {}
Output(google::protobuf::io::ZeroCopyOutputStream* stream) : stream_(stream) {}
~Output() { stream_->BackUp((int)size_); }

template <class... Arg>
Expand Down Expand Up @@ -101,12 +100,14 @@ class Output {
std::string StripExtension(absl::string_view fname);
std::string ToCIdent(absl::string_view str);
std::string ToPreproc(absl::string_view str);
void EmitFileWarning(const google::protobuf::FileDescriptor* file,
Output& output);
void EmitFileWarning(const google::protobuf::FileDescriptor* file, Output& output);
std::string MessageName(const google::protobuf::Descriptor* descriptor);
std::string FileLayoutName(const google::protobuf::FileDescriptor* file);
std::string HeaderFilename(const google::protobuf::FileDescriptor* file);

std::string MessageInit(const google::protobuf::Descriptor* descriptor);
std::string EnumInit(const google::protobuf::EnumDescriptor* descriptor);

} // namespace upbc

#endif // UPBC_COMMON_H
Loading

0 comments on commit 46e306b

Please sign in to comment.