From 101d5ad1f3e864f3b6442b6063151ffb47178099 Mon Sep 17 00:00:00 2001 From: katherine Date: Thu, 23 May 2019 03:23:59 -0700 Subject: add basetype-use-checks to analyse --- configure | 6 ++-- src/analyse.c | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- src/analyse.h | 12 +++++++ src/gen.c | 38 ++++++++++++++++++++ src/gen.h | 5 +++ src/main.c | 39 +++++++++++++++++++++ 6 files changed, 207 insertions(+), 4 deletions(-) diff --git a/configure b/configure index 458369e..26a510e 100755 --- a/configure +++ b/configure @@ -64,16 +64,16 @@ fi printf %s\\n ' printf "%s\n%s\n\n%s%s%s\n\n%s\n" \' printf %s\\n ' "#ifndef CONFCONF_VERSION_H" \' printf %s\\n ' "#define CONFCONF_VERSION_H" \' - printf %s\\n ' "#define VERSION \"confconf " "`git describe --always --tags`" "\"" \' + printf %s\\n ' "#define VERSION \"confconf-" "`git describe --always --tags`" "\"" \' printf %s\\n ' "#endif" > src/version.h' printf %s\\n '' printf %s\\n "$sources" | (while IFS= read -r s; do - $dgen $CFLAGS -MM -MG -MT ${objdir}/release/`printf %s $s | sed -e 's/\.c$/\.o/'` "$srcdir/$s" | sed -e "s/version.h/$srcdir\/version.h/g" + $dgen $CFLAGS -MM -MG -MT ${objdir}/release/`printf %s $s | sed -e 's/\.c$/\.o/'` "$srcdir/$s" | sed -e "s/version.h/$srcdir\/version.h/" printf %s\\n " \$(CC) -c \$(CFLAGS) -o ${objdir}/release/`printf %s $s | sed -e 's/\.c$/\.o/'` ${srcdir}/$s" done) printf %s\\n '' printf %s\\n "$sources" | (while IFS= read -r s; do - $dgen $CFLAGS -MM -MG -MT ${objdir}/debug/`printf %s $s | sed -e 's/\.c$/\.o/'` "$srcdir/$s" | sed -e "s/version.h/$srcdir\/version.h/g" + $dgen $CFLAGS -MM -MG -MT ${objdir}/debug/`printf %s $s | sed -e 's/\.c$/\.o/'` "$srcdir/$s" | sed -e "s/version.h/$srcdir\/version.h/" printf %s\\n " \$(CC) -c \$(CFLAGSDEBUG) -o ${objdir}/debug/`printf %s $s | sed -e 's/\.c$/\.o/'` ${srcdir}/$s" done) printf %s\\n '' diff --git a/src/analyse.c b/src/analyse.c index 9510f5a..e454f6c 100644 --- a/src/analyse.c +++ b/src/analyse.c @@ -6,7 +6,20 @@ struct analyse_result_s analyse(struct parse_result_s pr) { - struct analyse_result_s ar; + struct analyse_result_s ar = { + .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, + }; struct parse_deftype_s *dtp, *tmp_dtp; struct parse_var_s *vtp, *tmp_vtp; struct analyse_tree_s *cur; @@ -70,6 +83,102 @@ struct analyse_result_s analyse(struct parse_result_s pr) } } + HASH_ITER(hh, pr.vars, vtp, tmp_vtp) { + switch (vtp->type + - (PARSE_TYPE_ARRAY_BOOL * (vtp->type >= PARSE_TYPE_ARRAY_BOOL)) + - (PARSE_TYPE_ARRAY_BOOL * (vtp->type >= 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: + HASH_FIND_STR(pr.deftypes, vtp->deftype_name, dtp); + assert(dtp != NULL); + for (i = 0; i < dtp->member_list_len; i++) { + switch (dtp->member_type_list[i] + - (PARSE_TYPE_ARRAY_BOOL * + (dtp->member_type_list[i] >= PARSE_TYPE_ARRAY_BOOL) + ) + - (PARSE_TYPE_ARRAY_BOOL * + (dtp->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); + } + } + } + } + return ar; } diff --git a/src/analyse.h b/src/analyse.h index 8e21db7..19f1b96 100644 --- a/src/analyse.h +++ b/src/analyse.h @@ -18,6 +18,18 @@ struct analyse_tree_s { }; struct analyse_result_s { + 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; struct analyse_tree_s deftype_tree; struct analyse_tree_s var_tree; }; diff --git a/src/gen.c b/src/gen.c index e69de29..98e067b 100644 --- a/src/gen.c +++ b/src/gen.c @@ -0,0 +1,38 @@ +#include "gen.h" + +#include "version.h" + +#include + +static const char header[] = +"#ifndef CONFCONF_PRIV_H\n" +"#define CONFCONF_PRIV_H\n" +"\n" +"#include \n" +"#include \n" +"#include \n" +"\n" +"struct confconf_priv_fstate {\n" +" void *f;\n" +" int (*gcp)(void *);\n" +" size_t line;\n" +" size_t col;\n" +" size_t byte;\n" +"};\n" +"\n" +"#endif\n" +; + +void gen(FILE *f, struct parse_result_s pr, struct analyse_result_s ar) +{ + time_t t; + struct tm *ti; + + time(&t); + ti = localtime(&t); + + fprintf(f, "/* generated by %s on %04d-%02d-%02d */\n", + VERSION, ti->tm_year + 1900, ti->tm_mon + 1, ti->tm_mday); + + fprintf(f, header); +} diff --git a/src/gen.h b/src/gen.h index f3cb932..a5b2899 100644 --- a/src/gen.h +++ b/src/gen.h @@ -1,4 +1,9 @@ #ifndef CONFCONF_GEN_H #define CONFCONF_GEN_H +#include "parse.h" +#include "analyse.h" + +void gen(FILE *f, struct parse_result_s pr, struct analyse_result_s ar); + #endif diff --git a/src/main.c b/src/main.c index 0c0e510..77f3c55 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include "opt.h" #include "parse.h" #include "analyse.h" +#include "gen.h" static void print_tree(struct analyse_tree_s *t) { @@ -25,8 +26,10 @@ static void print_tree(struct analyse_tree_s *t) int main(int argc, char **argv) { + FILE *fo = stdout; FILE *fi = stdin; const char *finame = "stdin"; + const char *foname; struct parse_result_s pr; struct analyse_result_s ar; @@ -38,6 +41,12 @@ int main(int argc, char **argv) TRY(fi != NULL, "could not read file `%s`", finame); } + if (opt_outfile_str() != NULL) { + foname = opt_infile_str(); + fo = fopen(foname, "w"); + TRY(fo != NULL, "could not write to file `%s`", foname); + } + pr = parse(fi, finame); if (fi != stdin) @@ -50,6 +59,36 @@ int main(int argc, char **argv) print_tree(&ar.var_tree); puts(""); + if (ar.uses_bool) + puts("bool"); + if (ar.uses_string) + puts("string"); + if (ar.uses_id) + puts("id"); + if (ar.uses_int) + puts("int"); + if (ar.uses_intl) + puts("intl"); + if (ar.uses_intll) + puts("intll"); + if (ar.uses_uint) + puts("uint"); + if (ar.uses_uintl) + puts("uintl"); + if (ar.uses_uintll) + puts("uintll"); + if (ar.uses_float) + puts("float"); + if (ar.uses_double) + puts("double"); + if (ar.uses_doublel) + puts("doublel"); + + gen(fo, pr, ar); + + if (fo != stdout) + fclose(fo); + parse_result_wipe(&pr); analyse_result_wipe(&ar); -- cgit v1.2.3