From fa5710f1cad50ddd17605c73d4661acbf265a469 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 2 Jan 2010 15:30:51 -0800 Subject: upb_string* -> upb_strptr, to follow aliasing rules. --- tools/upbc.c | 64 ++++++++++++++++++++++++++---------------------------------- 1 file changed, 28 insertions(+), 36 deletions(-) (limited to 'tools/upbc.c') diff --git a/tools/upbc.c b/tools/upbc.c index ed80d39..274aa00 100644 --- a/tools/upbc.c +++ b/tools/upbc.c @@ -21,7 +21,7 @@ * the string (and thus never need to re-allocate). */ // Convert to C identifier: foo.bar.Baz -> foo_bar_Baz. -static void to_cident(upb_string *str) +static void to_cident(upb_strptr str) { upb_strlen_t len = upb_strlen(str); char *buf = upb_string_getrwbuf(str, len); @@ -31,7 +31,7 @@ static void to_cident(upb_string *str) } // Convert to C proprocessor identifier: foo.bar.Baz -> FOO_BAR_BAZ. -static void to_preproc(upb_string *str) +static void to_preproc(upb_strptr str) { to_cident(str); upb_strlen_t len = upb_strlen(str); @@ -65,7 +65,7 @@ static void write_const_h(struct upb_def *defs[], int num_entries, char *outfile_name, FILE *stream) { /* Header file prologue. */ - upb_string *include_guard_name = upb_strdupc(outfile_name); + upb_strptr include_guard_name = upb_strdupc(outfile_name); to_preproc(include_guard_name); /* A bit cheesy, but will do the job. */ upb_strlen_t len = upb_strlen(include_guard_name); @@ -85,8 +85,8 @@ static void write_const_h(struct upb_def *defs[], int num_entries, for(int i = 0; i < num_entries; i++) { /* Foreach enum */ if(defs[i]->type != UPB_DEF_ENUM) continue; struct upb_enumdef *enumdef = upb_downcast_enumdef(defs[i]); - upb_string *enum_name = upb_strdup(UPB_UPCAST(enumdef)->fqname); - upb_string *enum_val_prefix = upb_strdup(enum_name); + upb_strptr enum_name = upb_strdup(UPB_UPCAST(enumdef)->fqname); + upb_strptr enum_val_prefix = upb_strdup(enum_name); to_cident(enum_name); const char *data = upb_string_getrobuf(enum_val_prefix); @@ -100,7 +100,7 @@ static void write_const_h(struct upb_def *defs[], int num_entries, bool first = true; /* Foreach enum value. */ for(upb_enum_begin(&iter, enumdef); !upb_enum_done(&iter); upb_enum_next(&iter)) { - upb_string *value_name = upb_strdup(iter.name); + upb_strptr value_name = upb_strdup(iter.name); to_preproc(value_name); /* " GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13," */ if (!first) fputs(",\n", stream); @@ -130,7 +130,7 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, char *descriptor_cident, FILE *stream) { /* Header file prologue. */ - upb_string *include_guard_name = upb_strdupc(outfile_name); + upb_strptr include_guard_name = upb_strdupc(outfile_name); to_preproc(include_guard_name); fputs("/* This file was generated by upbc (the upb compiler). " "Do not edit. */\n\n", stream), @@ -155,7 +155,7 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, for(int i = 0; i < num_defs; i++) { /* Foreach message */ struct upb_msgdef *m = upb_dyncast_msgdef(defs[i]); if(!m) continue; - upb_string *msg_name = upb_strdup(UPB_UPCAST(m)->fqname); + upb_strptr msg_name = upb_strdup(UPB_UPCAST(m)->fqname); to_cident(msg_name); fprintf(stream, "struct " UPB_STRFMT ";\n", UPB_STRARG(msg_name)); fprintf(stream, "typedef struct " UPB_STRFMT "\n " UPB_STRFMT ";\n\n", @@ -168,7 +168,7 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, for(int i = 0; i < num_defs; i++) { /* Foreach message */ struct upb_msgdef *m = upb_dyncast_msgdef(defs[i]); if(!m) continue; - upb_string *msg_name = upb_strdup(UPB_UPCAST(m)->fqname); + upb_strptr msg_name = upb_strdup(UPB_UPCAST(m)->fqname); to_cident(msg_name); fprintf(stream, "struct " UPB_STRFMT " {\n", UPB_STRARG(msg_name)); fputs(" upb_data base;\n", stream); @@ -186,9 +186,9 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, for(upb_field_count_t j = 0; j < m->num_fields; j++) { struct upb_fielddef *f = &m->fields[j]; if(upb_issubmsg(f)) { - upb_string *type_name = upb_strdup(f->def->fqname); + upb_strptr type_name = upb_strdup(f->def->fqname); to_cident(type_name); - if(f->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED) { + if(f->label == UPB_LABEL(REPEATED)) { fprintf(stream, " UPB_MSG_ARRAY(" UPB_STRFMT ")* " UPB_STRFMT ";\n", UPB_STRARG(type_name), UPB_STRARG(f->name)); } else { @@ -196,25 +196,13 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, UPB_STRARG(type_name), UPB_STRARG(f->name)); } upb_string_unref(type_name); - } else if(f->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED) { - static char* c_types[] = { - "", "struct upb_double_array*", "struct upb_float_array*", - "struct upb_int64_array*", "struct upb_uint64_array*", - "struct upb_int32_array*", "struct upb_uint64_array*", - "struct upb_uint32_array*", "struct upb_bool_array*", - "struct upb_string_array*", "", "", - "struct upb_string_array*", "struct upb_uint32_array*", - "struct upb_uint32_array*", "struct upb_int32_array*", - "struct upb_int64_array*", "struct upb_int32_array*", - "struct upb_int64_array*" - }; - fprintf(stream, " %s " UPB_STRFMT ";\n", - c_types[f->type], UPB_STRARG(f->name)); + } else if(f->label == UPB_LABEL(REPEATED)) { + fprintf(stream, " upb_array* " UPB_STRFMT ";\n", UPB_STRARG(f->name)); } else { static char* c_types[] = { "", "double", "float", "int64_t", "uint64_t", "int32_t", "uint64_t", - "uint32_t", "bool", "upb_string*", "", "", - "upb_string*", "uint32_t", "int32_t", "int32_t", "int64_t", + "uint32_t", "bool", "upb_strptr", "", "", + "upb_strptr", "uint32_t", "int32_t", "int32_t", "int64_t", "int32_t", "int64_t" }; fprintf(stream, " %s " UPB_STRFMT ";\n", @@ -246,7 +234,7 @@ struct strtable_entry { struct typetable_entry { struct upb_strtable_entry e; struct upb_fielddef *field; - upb_string *cident; /* Type name converted with to_cident(). */ + upb_strptr cident; /* Type name converted with to_cident(). */ /* A list of all values of this type, in an established order. */ union upb_value *values; int values_size, values_len; @@ -316,11 +304,11 @@ static void add_strings_from_msg(upb_msg *msg, struct upb_msgdef *md, struct typetable_entry *get_or_insert_typeentry(struct upb_strtable *t, struct upb_fielddef *f) { - upb_string *type_name = upb_issubmsg(f) ? upb_strdup(f->def->fqname) : + upb_strptr type_name = upb_issubmsg(f) ? upb_strdup(f->def->fqname) : upb_strdupc(upb_type_info[f->type].ctype); struct typetable_entry *type_e = upb_strtable_lookup(t, type_name); if(type_e == NULL) { - upb_string *cident = upb_strdup(type_name); + upb_strptr cident = upb_strdup(type_name); to_cident(cident); struct typetable_entry new_type_e = { .e = {.key = type_name}, .field = f, .cident = cident, @@ -434,7 +422,7 @@ static void write_message_c(upb_msg *msg, struct upb_msgdef *md, int col = 2; int offset = 0; for(int i = 0; i < size; i++) { - upb_string *s = str_entries[i]->e.key; + upb_strptr s = str_entries[i]->e.key; str_entries[i]->offset = offset; str_entries[i]->num = i; const char *buf = upb_string_getrobuf(s); @@ -452,7 +440,7 @@ static void write_message_c(upb_msg *msg, struct upb_msgdef *md, fputs("static upb_norefcount_string strings[] = {\n", stream); for(int i = 0; i < size; i++) { struct strtable_entry *e = str_entries[i]; - fprintf(stream, " UPB_STRLIT_LEN(&strdata[%d], %d),\n", e->offset, upb_strlen(e->e.key)); + fprintf(stream, " UPB_STATIC_STRING_INIT_LEN(&strdata[%d], %d),\n", e->offset, upb_strlen(e->e.key)); } fputs("};\n\n", stream); free(str_entries); @@ -527,7 +515,11 @@ static void write_message_c(upb_msg *msg, struct upb_msgdef *md, union upb_value val = upb_msg_get(msgdata, f); fprintf(stream, " ." UPB_STRFMT " = ", UPB_STRARG(f->name)); if(!upb_msg_has(msgdata, f)) { - fputs("0, /* Not set. */", stream); + if(upb_isstring(f) && !upb_isarray(f)) { + fputs("UPB_STRING_NULL_INITIALIZER, /* Not set. */", stream); + } else { + fputs("0, /* Not set. */", stream); + } } else if(upb_isstring(f)) { if(upb_isarray(f)) { fputs("Ack, string arrays are not supported yet!\n", stderr); @@ -535,7 +527,7 @@ static void write_message_c(upb_msg *msg, struct upb_msgdef *md, } else { struct strtable_entry *str_e = upb_strtable_lookup(&strings, val.str); assert(str_e); - fprintf(stream, "(upb_string*)&strings[%d], /* \"" UPB_STRFMT "\" */", + fprintf(stream, "UPB_STATIC_STRING_PTR_INIT(strings[%d]), /* \"" UPB_STRFMT "\" */", str_e->num, UPB_STRARG(val.str)); } } else if(upb_isarray(f)) { @@ -666,8 +658,8 @@ int main(int argc, char *argv[]) if(!outfile_base) outfile_base = input_file; // Read and parse input file. - upb_string *descriptor = upb_strreadfile(input_file); - if(!descriptor) + upb_strptr descriptor = upb_strreadfile(input_file); + if(upb_string_isnull(descriptor)) error("Couldn't read input file."); struct upb_symtab *s = upb_symtab_new(); upb_msg *fds_msg = upb_msg_new(s->fds_msgdef); -- cgit v1.2.3