From b995a09c034fef67cafc7f4f09c84e1cd53d9f7f Mon Sep 17 00:00:00 2001 From: Jason Heeris Date: Fri, 1 Jan 2021 15:20:06 +0800 Subject: [PATCH 1/3] Add an annotation to skip including header files generated from specific schema files. Some schema files (eg. those that only decalare annotations) do not actually result in any generated C code. They do not need to have a corresponding include directive for C files generated from schemas that include them. This introduces a "donotinclude" annotation that takes the Cap'n Proto ID (a UInt64) of any such files and skips generating the include directive for them. --- compiler/c.capnp | 4 +++ compiler/capnpc-c.c | 85 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/compiler/c.capnp b/compiler/c.capnp index 9a0a1d3..2af08b9 100644 --- a/compiler/c.capnp +++ b/compiler/c.capnp @@ -39,3 +39,7 @@ annotation fieldgetset @0xf72bc690355d66de (file): Void; # generate getter & setter functions for accessing fields # # allows grabbing/putting values without de-/encoding the entire struct. + +annotation donotinclude @0x8c99797357b357e9 (file): UInt64; +# do not generate an include directive for an import statement for the file with +# the given ID diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index 8499e0d..685aa4f 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -37,6 +37,12 @@ struct node { struct field *fields; }; +struct id_bst { + uint64_t id; + struct id_bst *left; + struct id_bst *right; +}; + static struct str SRC = STR_INIT, HDR = STR_INIT; static struct capn g_valcapn; static struct capn_segment g_valseg; @@ -75,6 +81,68 @@ static void insert_node(struct node *s) { g_node_tree = capn_tree_insert(g_node_tree, &s->hdr); } +static struct id_bst * insert_id(struct id_bst * bst, uint64_t id) +{ + struct id_bst ** current = &bst; + + while (*current) + { + if (id > (*current)->id) + { + current = &(*current)->right; + } + else if (id < (*current)->id) + { + current = &(*current)->left; + } + else + { + return bst; + } + } + + *current = malloc(sizeof **current); + (*current)->id = id; + (*current)->left = NULL; + (*current)->right = NULL; + + return bst; +} + +static bool contains_id(struct id_bst * bst, uint64_t id) +{ + struct id_bst * current = bst; + + while (current) + { + if (id == current->id) + { + return true; + } + else if (id < current->id) + { + current = current->left; + } + else + { + current = current->right; + } + } + + return false; +} + +static void free_id_bst(struct id_bst * bst) +{ + if (bst) + { + free_id_bst(bst->left); + free_id_bst(bst->right); + free(bst); + } +} + + /* resolve_names recursively follows the nestedNodes tree in order to * set node->name. * It also builds up the list of nodes within a file (file_nodes and @@ -1240,6 +1308,7 @@ int main() { char *p; const char *nameinfix = NULL; FILE *srcf, *hdrf; + struct id_bst * donotinclude_ids = NULL; g_valc = 0; g_valseg.len = 0; @@ -1276,6 +1345,14 @@ int main() { case 0xf72bc690355d66deUL: /* $C::fieldgetset */ g_fieldgetset = 1; break; + case 0x8c99797357b357e9UL: /* $C::donotinclude */ + if (v.which != Value_uint64) + { + fprintf(stderr, "schema breakage on $C::donotinclude annotation\n"); + exit(2); + } + donotinclude_ids = insert_id(donotinclude_ids, v.uint64); + break; } } if (!nameinfix) @@ -1303,12 +1380,20 @@ int main() { struct CodeGeneratorRequest_RequestedFile_Import im; get_CodeGeneratorRequest_RequestedFile_Import(&im, file_req.imports, j); + // Check if this import is in the "do not include" list. + if (contains_id(donotinclude_ids, im.id)) + { + continue; + } + // Ignore leading slashes when generating C file #include's. // This signifies an absolute import in a library directory. const char *base_path = im.name.str[0] == '/' ? &im.name.str[1] : im.name.str; str_addf(&HDR, "#include \"%s%s.h\"\n", base_path, nameinfix); } + free_id_bst(donotinclude_ids); + str_addf(&HDR, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); declare(file_node, "struct %s;\n", 1); From fe3a57de131c34cc954a1859327153333a554d34 Mon Sep 17 00:00:00 2001 From: Jason Heeris Date: Fri, 1 Jan 2021 21:15:15 +0800 Subject: [PATCH 2/3] Added extra line break after structure size block. --- compiler/capnpc-c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index 685aa4f..f21e913 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -1043,7 +1043,7 @@ static void define_struct(struct node *n) { // adding the ability to get the structure size str_addf(&HDR, "\nstatic const size_t %s_word_count = %d;\n", n->name.str, n->n._struct.dataWordCount); str_addf(&HDR, "\nstatic const size_t %s_pointer_count = %d;\n", n->name.str, n->n._struct.pointerCount); - str_addf(&HDR, "\nstatic const size_t %s_struct_bytes_count = %d;\n", n->name.str, 8 * (n->n._struct.pointerCount + n->n._struct.dataWordCount)); + str_addf(&HDR, "\nstatic const size_t %s_struct_bytes_count = %d;\n\n", n->name.str, 8 * (n->n._struct.pointerCount + n->n._struct.dataWordCount)); str_addf(&SRC, "%s_list new_%s_list(struct capn_segment *s, int len) {\n", n->name.str, n->name.str); str_addf(&SRC, "\t%s_list p;\n", n->name.str); From 9153fc39c4f918f5004816b8cd8db017aedd54bd Mon Sep 17 00:00:00 2001 From: Jason Heeris Date: Fri, 1 Jan 2021 21:26:02 +0800 Subject: [PATCH 3/3] Add an annotation to typedef structs and enums. A new annotation 'typedefto' allows you to make a typedef in the generated code for structs and enums (but not union 'which' enums). --- compiler/c.capnp | 3 +++ compiler/capnpc-c.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/compiler/c.capnp b/compiler/c.capnp index 2af08b9..92c955a 100644 --- a/compiler/c.capnp +++ b/compiler/c.capnp @@ -43,3 +43,6 @@ annotation fieldgetset @0xf72bc690355d66de (file): Void; annotation donotinclude @0x8c99797357b357e9 (file): UInt64; # do not generate an include directive for an import statement for the file with # the given ID + +annotation typedefto @0xcefaf27713042144 (struct, enum): Text; +# generate a typedef for the annotated struct or enum declaration diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index f21e913..2512f13 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -191,6 +191,24 @@ static void define_enum(struct node *n) { str_addf(&HDR, "\n\t%s_%s = %d", n->name.str, e.name.str, i); } str_addf(&HDR, "\n};\n"); + + for (i = capn_len(n->n.annotations)-1; i >= 0; i--) { + struct Annotation a; + struct Value v; + get_Annotation(&a, n->n.annotations, i); + read_Value(&v, a.value); + + switch (a.id) { + case 0xcefaf27713042144UL: + if (v.which != Value_text) { + fprintf(stderr, "schema breakage on $C::typedefto annotation\n"); + exit(2); + } + + str_addf(&HDR, "\ntypedef enum %s %s;\n", n->name.str, v.text.str); + break; + } + } } static void decode_value(struct value* v, Type_ptr type, Value_ptr value, const char *symbol) { @@ -1007,6 +1025,7 @@ static void define_group(struct strings *s, struct node *n, const char *group_na static void define_struct(struct node *n) { static struct strings s; + int i; str_reset(&s.dtab); str_reset(&s.ftab); @@ -1034,6 +1053,24 @@ static void define_struct(struct node *n) { str_add(&HDR, s.decl.str, s.decl.len); str_addf(&HDR, "};\n"); + for (i = capn_len(n->n.annotations)-1; i >= 0; i--) { + struct Annotation a; + struct Value v; + get_Annotation(&a, n->n.annotations, i); + read_Value(&v, a.value); + + switch (a.id) { + case 0xcefaf27713042144UL: + if (v.which != Value_text) { + fprintf(stderr, "schema breakage on $C::typedefto annotation\n"); + exit(2); + } + + str_addf(&HDR, "\ntypedef struct %s %s;\n", n->name.str, v.text.str); + break; + } + } + str_addf(&SRC, "\n%s_ptr new_%s(struct capn_segment *s) {\n", n->name.str, n->name.str); str_addf(&SRC, "\t%s_ptr p;\n", n->name.str); str_addf(&SRC, "\tp.p = capn_new_struct(s, %d, %d);\n", 8*n->n._struct.dataWordCount, n->n._struct.pointerCount);