From e9d79d2441732264e2b990a5b2dc76d13724db07 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Wed, 16 Mar 2016 18:07:32 -0700 Subject: Added upb::FileDef, which represents the file defs are declared in. It is entirely optional: MessageDef/EnumDef can still exist on their own. But this can represent a def's file when it is desirable to do so (eg. for code generators). This approach will require that we change the way we handle extensions. But I think it will be a good change overall. Specifically, we previously handled extensions by duplicating the extended message and then adding the extension as a regular field to the duplicated message. This required also duplicating any messages that could reach the extended message. In the new world we will need a way of declaring and looking up extensions separately from the message being extended. This change also involves some notable changes to the generated code: - files are now called foo.upbdefs.h instead of foo.upb.h. This reflects the fact that we might possibly generate several different output files for a .proto file, and this one is just for defs. - we no longer generate selectors in the .h file. - the upbdefs.c no longer vends a SymbolTable. Now it vends the individual messages (and possibly a FileDef later). I think this will compose better once we can generate files where one generated files imports another. We also make the descriptor reader vend a list of FileDefs now. This is the best conceptual match for parsing a FileDescriptorSet. --- tests/test_def.c | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'tests/test_def.c') diff --git a/tests/test_def.c b/tests/test_def.c index 52d780b..6f3fb8f 100644 --- a/tests/test_def.c +++ b/tests/test_def.c @@ -38,15 +38,45 @@ static void test_noreftracking() { upb_msgdef_unref(md, &md); } +static char *upb_readfile(const char *filename, size_t *len) { + long size; + char *buf; + FILE *f = fopen(filename, "rb"); + if(!f) return NULL; + if(fseek(f, 0, SEEK_END) != 0) goto error; + size = ftell(f); + if(size < 0) goto error; + if(fseek(f, 0, SEEK_SET) != 0) goto error; + buf = malloc(size + 1); + if(size && fread(buf, size, 1, f) != 1) goto error; + fclose(f); + if (len) *len = size; + return buf; + +error: + fclose(f); + return NULL; +} + static upb_symtab *load_test_proto(void *owner) { upb_symtab *s = upb_symtab_new(owner); upb_status status = UPB_STATUS_INIT; + size_t len; + char *data = upb_readfile(descriptor_file, &len); + upb_filedef **files; ASSERT(s); - if (!upb_load_descriptor_file_into_symtab(s, descriptor_file, &status)) { - fprintf(stderr, "Error loading descriptor file: %s\n", - upb_status_errmsg(&status)); - ASSERT(false); + ASSERT(data); + files = upb_loaddescriptor(data, len, &files, &status); + ASSERT(files); + free(data); + + while (*files) { + bool ok = upb_symtab_addfile(s, *files, &status); + ASSERT(ok); + upb_filedef_unref(*files, &files); + files++; } + ASSERT(!upb_symtab_isfrozen(s)); upb_symtab_freeze(s); ASSERT(upb_symtab_isfrozen(s)); -- cgit v1.2.3