Fixed "make descriptorgen".

pull/13171/head
Joshua Haberman 14 years ago
parent 56984e8db8
commit 57abebaaf9
  1. 119
      tools/upbc.c
  2. 5
      upb/def.c
  3. 1
      upb/def.h
  4. 60
      upb/descriptor_const.h
  5. 2
      upb/pb/glue.c
  6. 4
      upb/upb.h

@ -13,59 +13,39 @@
#include <inttypes.h>
#include <stdarg.h>
#include <stdlib.h>
#include "upb_def.h"
#include "upb_msg.h"
#include "upb_glue.h"
#include "upb_strstream.h"
#include "upb_decoder.h"
#include "upb/bytestream.h"
#include "upb/def.h"
#include "upb/msg.h"
#include "upb/pb/glue.h"
/* These are in-place string transformations that do not change the length of
* 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)
{
upb_strlen_t len = upb_string_len(str);
char *buf = upb_string_getrwbuf(str, len);
for(int32_t i = 0; i < len; i++)
if(buf[i] == '.' || buf[i] == '/')
buf[i] = '_';
static void to_cident(char *str) {
for (; *str; ++str) {
if(*str == '.' || *str == '/') *str = '_';
}
}
// Convert to C proprocessor identifier: foo.bar.Baz -> FOO_BAR_BAZ.
static void to_preproc(upb_string *str)
{
static void to_preproc(char *str) {
to_cident(str);
upb_strlen_t len = upb_string_len(str);
char *buf = upb_string_getrwbuf(str, len);
for(int32_t i = 0; i < len; i++)
buf[i] = toupper(buf[i]);
}
static int my_memrchr(const char *data, char c, size_t len)
{
int off = len-1;
while(off > 0 && data[off] != c) --off;
return off;
for (; *str; ++str) *str = toupper(*str);
}
/* The _const.h file defines the constants (enums) defined in the .proto
* file. */
static void write_const_h(upb_def *defs[], int num_entries, char *outfile_name,
FILE *stream)
{
FILE *stream) {
/* Header file prologue. */
upb_string *include_guard_name = upb_strdupc(outfile_name);
char *include_guard_name = strdup(outfile_name);
to_preproc(include_guard_name);
/* A bit cheesy, but will do the job. */
upb_strlen_t len = upb_string_len(include_guard_name);
char *buf = upb_string_getrwbuf(include_guard_name, len);
buf[len-1] = 'C';
fputs("/* This file was generated by upbc (the upb compiler). "
"Do not edit. */\n\n", stream),
fprintf(stream, "#ifndef " UPB_STRFMT "\n", UPB_STRARG(include_guard_name));
fprintf(stream, "#define " UPB_STRFMT "\n\n", UPB_STRARG(include_guard_name));
fprintf(stream, "#ifndef %s\n", include_guard_name);
fprintf(stream, "#define %s\n\n", include_guard_name);
fputs("#ifdef __cplusplus\n", stream);
fputs("extern \"C\" {\n", stream);
fputs("#endif\n\n", stream);
@ -75,34 +55,29 @@ static void write_const_h(upb_def *defs[], int num_entries, char *outfile_name,
for(int i = 0; i < num_entries; i++) { /* Foreach enum */
if(defs[i]->type != UPB_DEF_ENUM) continue;
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);
char *enum_name = strdup(upb_def_fqname(UPB_UPCAST(enumdef)));
char *enum_val_prefix = strdup(enum_name);
to_cident(enum_name);
const char *data = upb_string_getrobuf(enum_val_prefix);
upb_strlen_t len = upb_string_len(enum_val_prefix);
upb_strlen_t lastsep = my_memrchr(data, UPB_SYMBOL_SEPARATOR, len);
upb_string_getrwbuf(enum_val_prefix, lastsep + 1);
to_preproc(enum_val_prefix);
fprintf(stream, "typedef enum " UPB_STRFMT " {\n", UPB_STRARG(enum_name));
fprintf(stream, "typedef enum %s {\n", enum_name);
bool first = true;
/* Foreach enum value. */
for (upb_enum_iter iter = upb_enum_begin(enumdef);
!upb_enum_done(iter);
iter = upb_enum_next(enumdef, iter)) {
upb_string *value_name = upb_strdup(upb_enum_iter_name(iter));
char *value_name = strdup(upb_enum_iter_name(iter));
uint32_t value = upb_enum_iter_number(iter);
to_preproc(value_name);
/* " GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13," */
/* " GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_UINT32 = 13," */
if (!first) fputs(",\n", stream);
first = false;
fprintf(stream, " " UPB_STRFMT UPB_STRFMT " = %" PRIu32,
UPB_STRARG(enum_val_prefix), UPB_STRARG(value_name), upb_enum_iter_number(iter));
upb_string_unref(value_name);
fprintf(stream, " %s_%s = %" PRIu32, enum_val_prefix, value_name, value);
free(value_name);
}
fprintf(stream, "\n} " UPB_STRFMT ";\n\n", UPB_STRARG(enum_name));
upb_string_unref(enum_name);
upb_string_unref(enum_val_prefix);
fprintf(stream, "\n} %s;\n\n", enum_name);
free(enum_name);
free(enum_val_prefix);
}
/* Constants for field names and numbers. */
@ -110,30 +85,32 @@ static void write_const_h(upb_def *defs[], int num_entries, char *outfile_name,
for(int i = 0; i < num_entries; i++) { /* Foreach enum */
upb_msgdef *m = upb_dyncast_msgdef(defs[i]);
if(!m) continue;
upb_string *msg_name = upb_strdup(UPB_UPCAST(m)->fqname);
upb_string *msg_val_prefix = upb_strdup(msg_name);
char *msg_name = strdup(upb_def_fqname(UPB_UPCAST(m)));
char *msg_val_prefix = strdup(msg_name);
to_preproc(msg_val_prefix);
upb_msg_iter i;
for(i = upb_msg_begin(m); !upb_msg_done(i); i = upb_msg_next(m, i)) {
upb_fielddef *f = upb_msg_iter_field(i);
upb_string *preproc_field_name = upb_strdup(f->name);
char *preproc_field_name = strdup(f->name);
to_preproc(preproc_field_name);
fprintf(stream, "#define " UPB_STRFMT "_" UPB_STRFMT "_FIELDNUM %d\n",
UPB_STRARG(msg_val_prefix), UPB_STRARG(preproc_field_name), f->number);
fprintf(stream, "#define " UPB_STRFMT "_" UPB_STRFMT "_FIELDNAME \""
UPB_STRFMT "\"\n", UPB_STRARG(msg_val_prefix), UPB_STRARG(preproc_field_name), UPB_STRARG(f->name));
upb_string_unref(preproc_field_name);
fprintf(stream, "#define %s_%s__FIELDNUM %d\n",
msg_val_prefix, preproc_field_name, upb_fielddef_number(f));
fprintf(stream, "#define %s_%s__FIELDNAME \"%s\"\n",
msg_val_prefix, preproc_field_name, f->name);
fprintf(stream, "#define %s_%s__FIELDTYPE %d\n\n",
msg_val_prefix, preproc_field_name, f->type);
free(preproc_field_name);
}
upb_string_unref(msg_val_prefix);
upb_string_unref(msg_name);
free(msg_val_prefix);
free(msg_name);
}
/* Epilogue. */
fputs("#ifdef __cplusplus\n", stream);
fputs("} /* extern \"C\" */\n", stream);
fputs("#endif\n\n", stream);
fprintf(stream, "#endif /* " UPB_STRFMT " */\n", UPB_STRARG(include_guard_name));
upb_string_unref(include_guard_name);
fprintf(stream, "#endif /* %s */\n", include_guard_name);
free(include_guard_name);
}
const char usage[] =
@ -146,15 +123,13 @@ const char usage[] =
" of using the input file as a basename.\n"
;
void usage_err(char *err)
{
void usage_err(char *err) {
fprintf(stderr, "upbc: %s\n\n", err);
fputs(usage, stderr);
exit(1);
}
void error(char *err, ...)
{
void error(char *err, ...) {
va_list args;
va_start(args, err);
fprintf(stderr, "upbc: ");
@ -163,8 +138,7 @@ void error(char *err, ...)
exit(1);
}
int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
/* Parse arguments. */
char *outfile_base = NULL, *input_file = NULL;
for(int i = 1; i < argc; i++) {
@ -184,7 +158,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);
size_t len;
char *descriptor = upb_readfile(input_file, &len);
if(!descriptor)
error("Couldn't read input file.");
@ -192,9 +167,9 @@ int main(int argc, char *argv[])
// importing descriptor.proto.
upb_symtab *s = upb_symtab_new();
upb_status status = UPB_STATUS_INIT;
upb_parsedesc(s, descriptor, &status);
upb_read_descriptor(s, descriptor, len, &status);
if(!upb_ok(&status)) {
upb_printerr(&status);
upb_status_print(&status, stderr);
error("Failed to parse input file descriptor\n");
}
upb_status_uninit(&status);
@ -213,7 +188,7 @@ int main(int argc, char *argv[])
write_const_h(defs, symcount, h_const_filename, h_const_file);
for (int i = 0; i < symcount; i++) upb_def_unref(defs[i]);
free(defs);
upb_string_unref(descriptor);
free(descriptor);
upb_symtab_unref(s);
fclose(h_const_file);

@ -172,8 +172,9 @@ upb_enumdef *upb_enumdef_dup(upb_enumdef *e) {
bool upb_enumdef_addval(upb_enumdef *e, char *name, int32_t num) {
if (upb_enumdef_iton(e, num) || upb_enumdef_ntoi(e, name, NULL))
return false;
upb_iton_ent ent = {0, strdup(name)};
upb_strtable_insert(&e->ntoi, name, &num);
upb_inttable_insert(&e->iton, num, strdup(name));
upb_inttable_insert(&e->iton, num, &ent);
return true;
}
@ -715,11 +716,11 @@ bool upb_symtab_add(upb_symtab *s, upb_def **defs, int n, upb_status *status) {
case UPB_TYPE(SINT64): upb_value_setint64(&f->defaultval, 0); break;
case UPB_TYPE(INT32):
case UPB_TYPE(SINT32):
case UPB_TYPE(ENUM):
case UPB_TYPE(SFIXED32): upb_value_setint32(&f->defaultval, 0); break;
case UPB_TYPE(UINT32):
case UPB_TYPE(FIXED32): upb_value_setuint32(&f->defaultval, 0); break;
case UPB_TYPE(BOOL): upb_value_setbool(&f->defaultval, false); break;
case UPB_TYPE(ENUM): // Will be resolved by upb_resolve().
case UPB_TYPE(STRING):
case UPB_TYPE(BYTES):
case UPB_TYPE(GROUP):

@ -276,7 +276,6 @@ typedef struct _upb_enumdef {
} upb_enumdef;
typedef struct {
upb_strtable_entry e;
uint32_t value;
} upb_ntoi_ent;

@ -1,7 +1,7 @@
/* This file was generated by upbc (the upb compiler). Do not edit. */
#ifndef SRC_DESCRIPTOR_CONST_C
#define SRC_DESCRIPTOR_CONST_C
#ifndef UPB_DESCRIPTOR_CONST_H
#define UPB_DESCRIPTOR_CONST_H
#ifdef __cplusplus
extern "C" {
@ -10,42 +10,42 @@ extern "C" {
/* Enums. */
typedef enum google_protobuf_FieldOptions_CType {
GOOGLE_PROTOBUF_FIELDOPTIONS_STRING = 0,
GOOGLE_PROTOBUF_FIELDOPTIONS_STRING_PIECE = 2,
GOOGLE_PROTOBUF_FIELDOPTIONS_CORD = 1
GOOGLE_PROTOBUF_FIELDOPTIONS_CTYPE_STRING = 0,
GOOGLE_PROTOBUF_FIELDOPTIONS_CTYPE_CORD = 1,
GOOGLE_PROTOBUF_FIELDOPTIONS_CTYPE_STRING_PIECE = 2
} google_protobuf_FieldOptions_CType;
typedef enum google_protobuf_FieldDescriptorProto_Type {
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_DOUBLE = 1,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FLOAT = 2,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT64 = 3,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT64 = 4,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 = 5,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED64 = 6,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED32 = 7,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BOOL = 8,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING = 9,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP = 10,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE = 11,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES = 12,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ENUM = 14,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED32 = 15,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED64 = 16,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT32 = 17,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64 = 18
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_DOUBLE = 1,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_FLOAT = 2,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_INT64 = 3,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_UINT64 = 4,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_INT32 = 5,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_FIXED64 = 6,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_FIXED32 = 7,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_BOOL = 8,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_STRING = 9,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_GROUP = 10,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_MESSAGE = 11,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_BYTES = 12,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_UINT32 = 13,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_ENUM = 14,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_SFIXED32 = 15,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_SFIXED64 = 16,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_SINT32 = 17,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_SINT64 = 18
} google_protobuf_FieldDescriptorProto_Type;
typedef enum google_protobuf_FieldDescriptorProto_Label {
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_OPTIONAL = 1,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REQUIRED = 2,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED = 3
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_LABEL_OPTIONAL = 1,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_LABEL_REQUIRED = 2,
GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_LABEL_REPEATED = 3
} google_protobuf_FieldDescriptorProto_Label;
typedef enum google_protobuf_FileOptions_OptimizeMode {
GOOGLE_PROTOBUF_FILEOPTIONS_SPEED = 1,
GOOGLE_PROTOBUF_FILEOPTIONS_CODE_SIZE = 2,
GOOGLE_PROTOBUF_FILEOPTIONS_LITE_RUNTIME = 3
GOOGLE_PROTOBUF_FILEOPTIONS_OPTIMIZEMODE_SPEED = 1,
GOOGLE_PROTOBUF_FILEOPTIONS_OPTIMIZEMODE_CODE_SIZE = 2,
GOOGLE_PROTOBUF_FILEOPTIONS_OPTIMIZEMODE_LITE_RUNTIME = 3
} google_protobuf_FileOptions_OptimizeMode;
/* Constants for field names and numbers. */
@ -346,4 +346,4 @@ typedef enum google_protobuf_FileOptions_OptimizeMode {
} /* extern "C" */
#endif
#endif /* SRC_DESCRIPTOR_CONST_C */
#endif /* UPB_DESCRIPTOR_CONST_H */

@ -77,7 +77,7 @@ void upb_read_descriptor(upb_symtab *symtab, const char *str, size_t len,
for(int i = 0; i < n; i++) {
upb_def *def = defs[i];
upb_msgdef *md = upb_dyncast_msgdef(def);
if (!md) return;
if (!md) continue;
// For field in msgdef:
upb_msg_iter i;
for(i = upb_msg_begin(md); !upb_msg_done(i); i = upb_msg_next(md, i)) {

@ -106,8 +106,8 @@ enum upb_wire_type {
typedef uint8_t upb_fieldtype_t;
// For referencing the type constants tersely.
#define UPB_TYPE(type) GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ ## type
#define UPB_LABEL(type) GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_ ## type
#define UPB_TYPE(type) GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_ ## type
#define UPB_LABEL(type) GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_LABEL_ ## type
// Info for a given field type.
typedef struct {

Loading…
Cancel
Save