From b995a09c034fef67cafc7f4f09c84e1cd53d9f7f Mon Sep 17 00:00:00 2001 From: Jason Heeris Date: Fri, 1 Jan 2021 15:20:06 +0800 Subject: [PATCH] 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);