diff options
author | katherine <k@airen-no-jikken.icu> | 2019-05-29 17:11:35 -0700 |
---|---|---|
committer | katherine <k@airen-no-jikken.icu> | 2019-05-29 17:11:35 -0700 |
commit | 58e3054794cde4148267d81c80cc3109fbd19298 (patch) | |
tree | 9ff89bc2297dbea6e16773e35e09f62c7d53e735 /src | |
parent | ea615e59178d76f887bc8e36cb8063095da20731 (diff) | |
download | confconf-58e3054794cde4148267d81c80cc3109fbd19298.tar.gz |
update usage checking
Diffstat (limited to 'src')
-rw-r--r-- | src/analyse.c | 151 | ||||
-rw-r--r-- | src/analyse.h | 14 | ||||
-rw-r--r-- | src/gen.c | 62 | ||||
-rw-r--r-- | src/parse.c | 7 | ||||
-rw-r--r-- | src/parse.h | 2 |
5 files changed, 91 insertions, 145 deletions
diff --git a/src/analyse.c b/src/analyse.c index e0e5fa6..416b10a 100644 --- a/src/analyse.c +++ b/src/analyse.c @@ -7,31 +7,27 @@ struct analyse_result_s analyse(struct parse_result_s pr) { struct analyse_result_s ar = { - .uses_array = false, .uses_hash = false, - .uses_bool = false, - .uses_string = false, - .uses_id = false, - .uses_int = false, - .uses_intl = false, - .uses_intll = false, - .uses_uint = false, - .uses_uintl = false, - .uses_uintll = false, - .uses_float = false, - .uses_double = false, - .uses_doublel = false, + .uses_array = false, }; struct parse_deftype_s *dcur, *dtmp; struct parse_var_s *vcur, *vtmp; struct analyse_tree_s *cur; unsigned i; + + /*************** + * BUILD TREES * + ***************/ + ar.deftype_tree.branch_count = 0; ar.deftype_tree.is_terminal = false; if (pr.deftypes != NULL) { HASH_ITER(hh, pr.deftypes, dcur, dtmp) { + if (!dcur->is_used) + continue; + cur = &(ar.deftype_tree); /* walk down the tree, creating nodes when necessary */ @@ -85,111 +81,50 @@ struct analyse_result_s analyse(struct parse_result_s pr) } } - for (i = 0; i < PARSE_TYPE_ARRAY_BOOL; i++) - ar.hash_types[i] = false; + + /************************ + * CALCULATE USED TYPES * + ************************/ + + for (i = PARSE_TYPE_BOOL; i < PARSE_TYPE_HASH_DEFTYPE; i++) + ar.uses_type[i] = false; + + HASH_ITER(hh, pr.deftypes, dcur, dtmp) { + if (!dcur->is_used) + continue; + + for (i = 0; i < dcur->member_list_len; i++) { + if (dcur->member_type_list[i] >= PARSE_TYPE_HASH_BOOL) { + ar.uses_hash = true; + ar.uses_type[dcur->member_type_list[i] + - PARSE_TYPE_HASH_BOOL] = true; + } else if (dcur->member_type_list[i] >= PARSE_TYPE_ARRAY_BOOL) { + ar.uses_array = true; + ar.uses_type[dcur->member_type_list[i] + - PARSE_TYPE_ARRAY_BOOL] = true; + } + + ar.uses_type[dcur->member_type_list[i]] = true; + } + } HASH_ITER(hh, pr.vars, vcur, vtmp) { if (vcur->type >= PARSE_TYPE_HASH_BOOL) { ar.uses_hash = true; - ar.uses_id = true; - ar.hash_types[vcur->type - PARSE_TYPE_HASH_BOOL] = true; + ar.uses_type[vcur->type - PARSE_TYPE_HASH_BOOL] = true; } else if (vcur->type >= PARSE_TYPE_ARRAY_BOOL) { ar.uses_array = true; + ar.uses_type[vcur->type - PARSE_TYPE_ARRAY_BOOL] = true; } - switch (vcur->type - - (PARSE_TYPE_ARRAY_BOOL * (vcur->type >= PARSE_TYPE_ARRAY_BOOL)) - - (PARSE_TYPE_ARRAY_BOOL * (vcur->type >= PARSE_TYPE_HASH_BOOL)) + if (vcur->type == PARSE_TYPE_DEFTYPE + || vcur->type == PARSE_TYPE_ARRAY_DEFTYPE + || vcur->type == PARSE_TYPE_HASH_DEFTYPE ) { - case PARSE_TYPE_BOOL: - ar.uses_bool = true; - break; - case PARSE_TYPE_STRING: - ar.uses_string = true; - break; - case PARSE_TYPE_ID: - ar.uses_id = true; - break; - case PARSE_TYPE_INT: - ar.uses_int = true; - break; - case PARSE_TYPE_INTL: - ar.uses_intl = true; - break; - case PARSE_TYPE_INTLL: - ar.uses_intll = true; - break; - case PARSE_TYPE_UINT: - ar.uses_uint = true; - break; - case PARSE_TYPE_UINTL: - ar.uses_uintl = true; - break; - case PARSE_TYPE_UINTLL: - ar.uses_uintll = true; - break; - case PARSE_TYPE_FLOAT: - ar.uses_float = true; - break; - case PARSE_TYPE_DOUBLE: - ar.uses_double = true; - break; - case PARSE_TYPE_DOUBLEL: - ar.uses_doublel = true; - break; - default: - HASH_FIND_STR(pr.deftypes, vcur->deftype_name, dcur); - assert(dcur != NULL); - for (i = 0; i < dcur->member_list_len; i++) { - switch (dcur->member_type_list[i] - - (PARSE_TYPE_ARRAY_BOOL * - (dcur->member_type_list[i] >= PARSE_TYPE_ARRAY_BOOL) - ) - - (PARSE_TYPE_ARRAY_BOOL * - (dcur->member_type_list[i] >= PARSE_TYPE_HASH_BOOL) - ) - ) { - case PARSE_TYPE_BOOL: - ar.uses_bool = true; - break; - case PARSE_TYPE_STRING: - ar.uses_string = true; - break; - case PARSE_TYPE_ID: - ar.uses_id = true; - break; - case PARSE_TYPE_INT: - ar.uses_int = true; - break; - case PARSE_TYPE_INTL: - ar.uses_intl = true; - break; - case PARSE_TYPE_INTLL: - ar.uses_intll = true; - break; - case PARSE_TYPE_UINT: - ar.uses_uint = true; - break; - case PARSE_TYPE_UINTL: - ar.uses_uintl = true; - break; - case PARSE_TYPE_UINTLL: - ar.uses_uintll = true; - break; - case PARSE_TYPE_FLOAT: - ar.uses_float = true; - break; - case PARSE_TYPE_DOUBLE: - ar.uses_double = true; - break; - case PARSE_TYPE_DOUBLEL: - ar.uses_doublel = true; - break; - default: - assert(false); - } - } + continue; } + + ar.uses_type[vcur->type] = true; } return ar; diff --git a/src/analyse.h b/src/analyse.h index 77594c2..539e003 100644 --- a/src/analyse.h +++ b/src/analyse.h @@ -18,21 +18,9 @@ struct analyse_tree_s { }; struct analyse_result_s { + bool uses_type[PARSE_TYPE_HASH_DEFTYPE]; bool uses_array; bool uses_hash; - bool uses_bool; - bool uses_string; - bool uses_id; - bool uses_int; - bool uses_intl; - bool uses_intll; - bool uses_uint; - bool uses_uintl; - bool uses_uintll; - bool uses_float; - bool uses_double; - bool uses_doublel; - bool hash_types[PARSE_TYPE_ARRAY_BOOL]; struct analyse_tree_s deftype_tree; struct analyse_tree_s var_tree; }; @@ -31,29 +31,29 @@ void gen(FILE *f, struct parse_result_s pr, struct analyse_result_s ar) fprintf(f, sheaderp1); fprintf(f, sheaderp2); - /* built-in types */ - if (ar.uses_bool) + if (ar.uses_type[PARSE_TYPE_BOOL]) fprintf(f, sbool); - if (ar.uses_string) + if (ar.uses_type[PARSE_TYPE_STRING]) fprintf(f, sstring); - if (ar.uses_int) + if (ar.uses_type[PARSE_TYPE_INT]) fprintf(f, sint); - if (ar.uses_intl) + if (ar.uses_type[PARSE_TYPE_INTL]) fprintf(f, sintl); - if (ar.uses_intll) + if (ar.uses_type[PARSE_TYPE_INTLL]) fprintf(f, sintll); - if (ar.uses_uint) + if (ar.uses_type[PARSE_TYPE_UINT]) fprintf(f, suint); - if (ar.uses_uintl) + if (ar.uses_type[PARSE_TYPE_UINTL]) fprintf(f, suintl); - if (ar.uses_uintll) + if (ar.uses_type[PARSE_TYPE_UINTLL]) fprintf(f, suintll); - if (ar.uses_float) + if (ar.uses_type[PARSE_TYPE_FLOAT]) fprintf(f, sfloat); - if (ar.uses_double) + if (ar.uses_type[PARSE_TYPE_DOUBLE]) fprintf(f, sdouble); - if (ar.uses_doublel) + if (ar.uses_type[PARSE_TYPE_DOUBLEL]) fprintf(f, sdoublel); + if (ar.uses_hash) fprintf(f, shash); @@ -69,6 +69,9 @@ void gen(FILE *f, struct parse_result_s pr, struct analyse_result_s ar) /* structs */ HASH_ITER(hh, pr.deftypes, dcur, dtmp) { + if (!dcur->is_used) + continue; + fprintf(f, "struct confconf_type_%s_%s {\n", dcur->name, pr.suffix); @@ -131,20 +134,31 @@ void gen(FILE *f, struct parse_result_s pr, struct analyse_result_s ar) " union {\n" "%s%s%s%s%s%s%s%s%s%s%s", pr.suffix, - (ar.uses_bool ? " bool b;\n" : ""), - (ar.uses_string ? " char *s;\n" : ""), - (ar.uses_int ? " int i;\n" : ""), - (ar.uses_intl ? " long int il;\n" : ""), - (ar.uses_intll ? " long long int ill;\n" : ""), - (ar.uses_uint ? " unsigned u;\n" : ""), - (ar.uses_uintl ? " long unsigned ul;\n" : ""), - (ar.uses_uintll ? " long long unsigned ull;\n" : ""), - (ar.uses_float ? " float f;\n" : ""), - (ar.uses_double ? " double d;\n" : ""), - (ar.uses_doublel ? " long double dl;\n" : "") + (ar.uses_type[PARSE_TYPE_HASH_BOOL] + ? " bool b;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_STRING] + ? " char *s;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_INT] + ? " int i;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_INTL] + ? " long int il;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_INTLL] + ? " long long int ill;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_UINT] + ? " unsigned u;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_UINTL] + ? " long unsigned ul;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_UINTLL] + ? " long long unsigned ull;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_FLOAT] + ? " float f;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_DOUBLE] + ? " double d;\n" : ""), + (ar.uses_type[PARSE_TYPE_HASH_DOUBLEL] + ? " long double dl;\n" : "") ); HASH_ITER(hh, pr.deftypes, dcur, dtmp) { - if (dcur->is_used) { + if (dcur->is_used && dcur->is_in_hash) { fprintf(f, " %s confconf_type_%s_%s type_%s;\n", (dcur->is_union ? "union" : "struct"), dcur->name, pr.suffix, dcur->name); diff --git a/src/parse.c b/src/parse.c index 57db35c..a70f28d 100644 --- a/src/parse.c +++ b/src/parse.c @@ -112,6 +112,8 @@ static void sub_parse_deftype(size_t line, size_t col, bool is_union) .col = col, .is_used = false, .is_union = is_union, + .is_in_array = false, + .is_in_hash = false, .member_list_len = 0, }; unsigned i, j; @@ -366,6 +368,11 @@ struct parse_result_s parse(FILE *f, const char *fname) vcur->name, vcur->deftype_name); } dcur->is_used = true; + if (vcur->type == PARSE_TYPE_ARRAY_DEFTYPE) + dcur->is_in_array = true; + else if (vcur->type == PARSE_TYPE_HASH_DEFTYPE) + dcur->is_in_hash = true; + default: continue; } diff --git a/src/parse.h b/src/parse.h index 5771a07..89cf4fc 100644 --- a/src/parse.h +++ b/src/parse.h @@ -61,6 +61,8 @@ struct parse_deftype_s { size_t col; bool is_used; bool is_union; + bool is_in_array; + bool is_in_hash; unsigned member_list_len; enum parse_type_e member_type_list[PARSE_DEFTYPE_MAX_LEN]; char member_name_list[PARSE_DEFTYPE_MAX_LEN][TOK_MAX_LEN]; |