Skip to content

Commit

Permalink
Added UPB_DESC() macro for all bootstrap references to proto symbols.
Browse files Browse the repository at this point in the history
We have previously been using Copybara to rewrite these names, but for bootstrapping we will want to be able to sometimes use OSS names inside google3.

PiperOrigin-RevId: 500294974
  • Loading branch information
haberman committed Jan 7, 2023
1 parent 955a57f commit 204b9ee
Show file tree
Hide file tree
Showing 36 changed files with 226 additions and 192 deletions.
10 changes: 10 additions & 0 deletions upb/port/def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,13 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#else
#define UPB_DEPRECATED
#endif

// begin:google_only
// #define UPB_IS_GOOGLE3
// end:google_only

#if defined(UPB_IS_GOOGLE3)
#define UPB_DESC(sym) proto2_##sym
#else
#define UPB_DESC(sym) google_protobuf_##sym
#endif
2 changes: 2 additions & 0 deletions upb/port/undef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,5 @@
#undef UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3
#undef UPB_DEPRECATED
#undef UPB_GNUC_MIN
#undef UPB_DESC
#undef UPB_IS_GOOGLE3
22 changes: 11 additions & 11 deletions upb/reflection/def_builder_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@

// We want to copy the options verbatim into the destination options proto.
// We use serialize+parse as our deep copy.
#define UPB_DEF_SET_OPTIONS(target, desc_type, options_type, proto) \
if (google_protobuf_##desc_type##_has_options(proto)) { \
size_t size; \
char* pb = google_protobuf_##options_type##_serialize( \
google_protobuf_##desc_type##_options(proto), ctx->tmp_arena, &size); \
if (!pb) _upb_DefBuilder_OomErr(ctx); \
target = \
google_protobuf_##options_type##_parse(pb, size, _upb_DefBuilder_Arena(ctx)); \
if (!target) _upb_DefBuilder_OomErr(ctx); \
} else { \
target = (const google_protobuf_##options_type*)kUpbDefOptDefault; \
#define UPB_DEF_SET_OPTIONS(target, desc_type, options_type, proto) \
if (UPB_DESC(desc_type##_has_options)(proto)) { \
size_t size; \
char* pb = UPB_DESC(options_type##_serialize)( \
UPB_DESC(desc_type##_options)(proto), ctx->tmp_arena, &size); \
if (!pb) _upb_DefBuilder_OomErr(ctx); \
target = \
UPB_DESC(options_type##_parse)(pb, size, _upb_DefBuilder_Arena(ctx)); \
if (!target) _upb_DefBuilder_OomErr(ctx); \
} else { \
target = (const UPB_DESC(options_type)*)kUpbDefOptDefault; \
}

#ifdef __cplusplus
Expand Down
15 changes: 8 additions & 7 deletions upb/reflection/def_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ static void remove_filedef(upb_DefPool* s, upb_FileDef* file) {
}

static const upb_FileDef* _upb_DefPool_AddFile(
upb_DefPool* s, const google_protobuf_FileDescriptorProto* file_proto,
upb_DefPool* s, const UPB_DESC(FileDescriptorProto) * file_proto,
const upb_MiniTableFile* layout, upb_Status* status) {
const upb_StringView name = google_protobuf_FileDescriptorProto_name(file_proto);
const upb_StringView name = UPB_DESC(FileDescriptorProto_name)(file_proto);

if (name.size == 0) {
upb_Status_SetErrorFormat(status,
Expand Down Expand Up @@ -335,9 +335,10 @@ static const upb_FileDef* _upb_DefPool_AddFile(
return ctx.file;
}

const upb_FileDef* upb_DefPool_AddFile(
upb_DefPool* s, const google_protobuf_FileDescriptorProto* file_proto,
upb_Status* status) {
const upb_FileDef* upb_DefPool_AddFile(upb_DefPool* s,
const UPB_DESC(FileDescriptorProto) *
file_proto,
upb_Status* status) {
return _upb_DefPool_AddFile(s, file_proto, NULL, status);
}

Expand All @@ -346,7 +347,7 @@ bool _upb_DefPool_LoadDefInitEx(upb_DefPool* s, const _upb_DefPool_Init* init,
/* Since this function should never fail (it would indicate a bug in upb) we
* print errors to stderr instead of returning error status to the user. */
_upb_DefPool_Init** deps = init->deps;
google_protobuf_FileDescriptorProto* file;
UPB_DESC(FileDescriptorProto) * file;
upb_Arena* arena;
upb_Status status;

Expand All @@ -362,7 +363,7 @@ bool _upb_DefPool_LoadDefInitEx(upb_DefPool* s, const _upb_DefPool_Init* init,
if (!_upb_DefPool_LoadDefInitEx(s, *deps, rebuild_minitable)) goto err;
}

file = google_protobuf_FileDescriptorProto_parse_ex(
file = UPB_DESC(FileDescriptorProto_parse_ex)(
init->descriptor.data, init->descriptor.size, NULL,
kUpb_DecodeOption_AliasString, arena);
s->bytes_loaded += init->descriptor.size;
Expand Down
7 changes: 4 additions & 3 deletions upb/reflection/def_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ const upb_ServiceDef* upb_DefPool_FindServiceByNameWithSize(
const upb_FileDef* upb_DefPool_FindFileContainingSymbol(const upb_DefPool* s,
const char* name);

const upb_FileDef* upb_DefPool_AddFile(
upb_DefPool* s, const google_protobuf_FileDescriptorProto* file_proto,
upb_Status* status);
const upb_FileDef* upb_DefPool_AddFile(upb_DefPool* s,
const UPB_DESC(FileDescriptorProto) *
file_proto,
upb_Status* status);

const upb_ExtensionRegistry* upb_DefPool_ExtensionRegistry(
const upb_DefPool* s);
Expand Down
26 changes: 14 additions & 12 deletions upb/reflection/enum_def.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include "upb/port/def.inc"

struct upb_EnumDef {
const google_protobuf_EnumOptions* opts;
const UPB_DESC(EnumOptions) * opts;
const upb_MiniTableEnum* layout; // Only for proto2.
const upb_FileDef* file;
const upb_MessageDef* containing_type; // Could be merged with "file".
Expand Down Expand Up @@ -89,7 +89,7 @@ bool _upb_EnumDef_Insert(upb_EnumDef* e, upb_EnumValueDef* v, upb_Arena* a) {
return true;
}

const google_protobuf_EnumOptions* upb_EnumDef_Options(const upb_EnumDef* e) {
const UPB_DESC(EnumOptions) * upb_EnumDef_Options(const upb_EnumDef* e) {
return e->opts;
}

Expand Down Expand Up @@ -237,25 +237,25 @@ static upb_StringView* _upb_EnumReservedNames_New(
}

static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
const google_protobuf_EnumDescriptorProto* enum_proto,
const UPB_DESC(EnumDescriptorProto) * enum_proto,
upb_EnumDef* e) {
const google_protobuf_EnumValueDescriptorProto* const* values;
const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* res_ranges;
const UPB_DESC(EnumValueDescriptorProto)* const* values;
const UPB_DESC(EnumDescriptorProto_EnumReservedRange)* const* res_ranges;
const upb_StringView* res_names;
upb_StringView name;
size_t n_value, n_res_range, n_res_name;

// Must happen before _upb_DefBuilder_Add()
e->file = _upb_DefBuilder_File(ctx);

name = google_protobuf_EnumDescriptorProto_name(enum_proto);
name = UPB_DESC(EnumDescriptorProto_name)(enum_proto);
_upb_DefBuilder_CheckIdentNotFull(ctx, name);

e->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
_upb_DefBuilder_Add(ctx, e->full_name,
_upb_DefType_Pack(e, UPB_DEFTYPE_ENUM));

values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n_value);
values = UPB_DESC(EnumDescriptorProto_value)(enum_proto, &n_value);

bool ok = upb_strtable_init(&e->ntoi, n_value, ctx->arena);
if (!ok) _upb_DefBuilder_OomErr(ctx);
Expand All @@ -274,11 +274,12 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
}

res_ranges =
google_protobuf_EnumDescriptorProto_reserved_range(enum_proto, &n_res_range);
UPB_DESC(EnumDescriptorProto_reserved_range)(enum_proto, &n_res_range);
e->res_range_count = n_res_range;
e->res_ranges = _upb_EnumReservedRanges_New(ctx, n_res_range, res_ranges, e);

res_names = google_protobuf_EnumDescriptorProto_reserved_name(enum_proto, &n_res_name);
res_names =
UPB_DESC(EnumDescriptorProto_reserved_name)(enum_proto, &n_res_name);
e->res_name_count = n_res_name;
e->res_names = _upb_EnumReservedNames_New(ctx, n_res_name, res_names);

Expand All @@ -298,9 +299,10 @@ static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
}
}

upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n,
const google_protobuf_EnumDescriptorProto* const* protos,
const upb_MessageDef* containing_type) {
upb_EnumDef* _upb_EnumDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto) * const* protos,
const upb_MessageDef* containing_type) {
_upb_DefType_CheckPadding(sizeof(upb_EnumDef));

// If a containing type is defined then get the full name from that.
Expand Down
2 changes: 1 addition & 1 deletion upb/reflection/enum_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
upb_StringView* out);

const char* upb_EnumDef_Name(const upb_EnumDef* e);
const google_protobuf_EnumOptions* upb_EnumDef_Options(const upb_EnumDef* e);
const UPB_DESC(EnumOptions) * upb_EnumDef_Options(const upb_EnumDef* e);

upb_StringView upb_EnumDef_ReservedName(const upb_EnumDef* e, int i);
int upb_EnumDef_ReservedNameCount(const upb_EnumDef* e);
Expand Down
7 changes: 4 additions & 3 deletions upb/reflection/enum_def_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ bool _upb_EnumDef_Insert(upb_EnumDef* e, upb_EnumValueDef* v, upb_Arena* a);
const upb_MiniTableEnum* _upb_EnumDef_MiniTable(const upb_EnumDef* e);

// Allocate and initialize an array of |n| enum defs.
upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n,
const google_protobuf_EnumDescriptorProto* const* protos,
const upb_MessageDef* containing_type);
upb_EnumDef* _upb_EnumDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto) * const* protos,
const upb_MessageDef* containing_type);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
6 changes: 3 additions & 3 deletions upb/reflection/enum_reserved_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ int32_t upb_EnumReservedRange_End(const upb_EnumReservedRange* r) {

upb_EnumReservedRange* _upb_EnumReservedRanges_New(
upb_DefBuilder* ctx, int n,
const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* protos,
const UPB_DESC(EnumDescriptorProto_EnumReservedRange) * const* protos,
const upb_EnumDef* e) {
upb_EnumReservedRange* r =
_upb_DefBuilder_Alloc(ctx, sizeof(upb_EnumReservedRange) * n);

for (int i = 0; i < n; i++) {
const int32_t start =
google_protobuf_EnumDescriptorProto_EnumReservedRange_start(protos[i]);
UPB_DESC(EnumDescriptorProto_EnumReservedRange_start)(protos[i]);
const int32_t end =
google_protobuf_EnumDescriptorProto_EnumReservedRange_end(protos[i]);
UPB_DESC(EnumDescriptorProto_EnumReservedRange_end)(protos[i]);

// A full validation would also check that each range is disjoint, and that
// none of the fields overlap with the extension ranges, but we are just
Expand Down
2 changes: 1 addition & 1 deletion upb/reflection/enum_reserved_range_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ upb_EnumReservedRange* _upb_EnumReservedRange_At(const upb_EnumReservedRange* r,
// Allocate and initialize an array of |n| reserved ranges owned by |e|.
upb_EnumReservedRange* _upb_EnumReservedRanges_New(
upb_DefBuilder* ctx, int n,
const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* protos,
const UPB_DESC(EnumDescriptorProto_EnumReservedRange) * const* protos,
const upb_EnumDef* e);

#ifdef __cplusplus
Expand Down
15 changes: 8 additions & 7 deletions upb/reflection/enum_value_def.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "upb/port/def.inc"

struct upb_EnumValueDef {
const google_protobuf_EnumValueOptions* opts;
const UPB_DESC(EnumValueOptions) * opts;
const upb_EnumDef* parent;
const char* full_name;
int32_t number;
Expand Down Expand Up @@ -66,8 +66,8 @@ const upb_EnumValueDef** _upb_EnumValueDefs_Sorted(const upb_EnumValueDef* v,
return (const upb_EnumValueDef**)out;
}

const google_protobuf_EnumValueOptions* upb_EnumValueDef_Options(
const upb_EnumValueDef* v) {
const UPB_DESC(EnumValueOptions) *
upb_EnumValueDef_Options(const upb_EnumValueDef* v) {
return v->opts;
}

Expand Down Expand Up @@ -95,13 +95,14 @@ uint32_t upb_EnumValueDef_Index(const upb_EnumValueDef* v) {
}

static void create_enumvaldef(upb_DefBuilder* ctx, const char* prefix,
const google_protobuf_EnumValueDescriptorProto* val_proto,
const UPB_DESC(EnumValueDescriptorProto) *
val_proto,
upb_EnumDef* e, upb_EnumValueDef* v) {
upb_StringView name = google_protobuf_EnumValueDescriptorProto_name(val_proto);
upb_StringView name = UPB_DESC(EnumValueDescriptorProto_name)(val_proto);

v->parent = e; // Must happen prior to _upb_DefBuilder_Add()
v->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
v->number = google_protobuf_EnumValueDescriptorProto_number(val_proto);
v->number = UPB_DESC(EnumValueDescriptorProto_number)(val_proto);
_upb_DefBuilder_Add(ctx, v->full_name,
_upb_DefType_Pack(v, UPB_DEFTYPE_ENUMVAL));

Expand All @@ -115,7 +116,7 @@ static void create_enumvaldef(upb_DefBuilder* ctx, const char* prefix,
// Allocate and initialize an array of |n| enum value defs owned by |e|.
upb_EnumValueDef* _upb_EnumValueDefs_New(
upb_DefBuilder* ctx, const char* prefix, int n,
const google_protobuf_EnumValueDescriptorProto* const* protos, upb_EnumDef* e,
const UPB_DESC(EnumValueDescriptorProto) * const* protos, upb_EnumDef* e,
bool* is_sorted) {
_upb_DefType_CheckPadding(sizeof(upb_EnumValueDef));

Expand Down
4 changes: 2 additions & 2 deletions upb/reflection/enum_value_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ bool upb_EnumValueDef_HasOptions(const upb_EnumValueDef* v);
uint32_t upb_EnumValueDef_Index(const upb_EnumValueDef* v);
const char* upb_EnumValueDef_Name(const upb_EnumValueDef* v);
int32_t upb_EnumValueDef_Number(const upb_EnumValueDef* v);
const google_protobuf_EnumValueOptions* upb_EnumValueDef_Options(
const upb_EnumValueDef* v);
const UPB_DESC(EnumValueOptions) *
upb_EnumValueDef_Options(const upb_EnumValueDef* v);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
2 changes: 1 addition & 1 deletion upb/reflection/enum_value_def_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ upb_EnumValueDef* _upb_EnumValueDef_At(const upb_EnumValueDef* v, int i);
// Allocate and initialize an array of |n| enum value defs owned by |e|.
upb_EnumValueDef* _upb_EnumValueDefs_New(
upb_DefBuilder* ctx, const char* prefix, int n,
const google_protobuf_EnumValueDescriptorProto* const* protos, upb_EnumDef* e,
const UPB_DESC(EnumValueDescriptorProto) * const* protos, upb_EnumDef* e,
bool* is_sorted);

const upb_EnumValueDef** _upb_EnumValueDefs_Sorted(const upb_EnumValueDef* v,
Expand Down
20 changes: 10 additions & 10 deletions upb/reflection/extension_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "upb/port/def.inc"

struct upb_ExtensionRange {
const google_protobuf_ExtensionRangeOptions* opts;
const UPB_DESC(ExtensionRangeOptions) * opts;
int32_t start;
int32_t end;
};
Expand All @@ -43,8 +43,8 @@ upb_ExtensionRange* _upb_ExtensionRange_At(const upb_ExtensionRange* r, int i) {
return (upb_ExtensionRange*)&r[i];
}

const google_protobuf_ExtensionRangeOptions* upb_ExtensionRange_Options(
const upb_ExtensionRange* r) {
const UPB_DESC(ExtensionRangeOptions) *
upb_ExtensionRange_Options(const upb_ExtensionRange* r) {
return r->opts;
}

Expand All @@ -60,19 +60,19 @@ int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r) { return r->end; }

upb_ExtensionRange* _upb_ExtensionRanges_New(
upb_DefBuilder* ctx, int n,
const google_protobuf_DescriptorProto_ExtensionRange* const* protos,
const UPB_DESC(DescriptorProto_ExtensionRange) * const* protos,
const upb_MessageDef* m) {
upb_ExtensionRange* r =
_upb_DefBuilder_Alloc(ctx, sizeof(upb_ExtensionRange) * n);

for (int i = 0; i < n; i++) {
const int32_t start =
google_protobuf_DescriptorProto_ExtensionRange_start(protos[i]);
const int32_t end = google_protobuf_DescriptorProto_ExtensionRange_end(protos[i]);
const int32_t max =
google_protobuf_MessageOptions_message_set_wire_format(upb_MessageDef_Options(m))
? INT32_MAX
: kUpb_MaxFieldNumber + 1;
UPB_DESC(DescriptorProto_ExtensionRange_start)(protos[i]);
const int32_t end = UPB_DESC(DescriptorProto_ExtensionRange_end)(protos[i]);
const int32_t max = UPB_DESC(MessageOptions_message_set_wire_format)(
upb_MessageDef_Options(m))
? INT32_MAX
: kUpb_MaxFieldNumber + 1;

// A full validation would also check that each range is disjoint, and that
// none of the fields overlap with the extension ranges, but we are just
Expand Down
4 changes: 2 additions & 2 deletions upb/reflection/extension_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ int32_t upb_ExtensionRange_Start(const upb_ExtensionRange* r);
int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r);

bool upb_ExtensionRange_HasOptions(const upb_ExtensionRange* r);
const google_protobuf_ExtensionRangeOptions* upb_ExtensionRange_Options(
const upb_ExtensionRange* r);
const UPB_DESC(ExtensionRangeOptions) *
upb_ExtensionRange_Options(const upb_ExtensionRange* r);

#ifdef __cplusplus
} /* extern "C" */
Expand Down
2 changes: 1 addition & 1 deletion upb/reflection/extension_range_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ upb_ExtensionRange* _upb_ExtensionRange_At(const upb_ExtensionRange* r, int i);
// Allocate and initialize an array of |n| extension ranges owned by |m|.
upb_ExtensionRange* _upb_ExtensionRanges_New(
upb_DefBuilder* ctx, int n,
const google_protobuf_DescriptorProto_ExtensionRange* const* protos,
const UPB_DESC(DescriptorProto_ExtensionRange) * const* protos,
const upb_MessageDef* m);

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 204b9ee

Please sign in to comment.