From: Dale Weiler Date: Thu, 25 Apr 2013 12:08:13 +0000 (+0000) Subject: less globals X-Git-Tag: 0.2.9~14 X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=commitdiff_plain;h=9fee84f250dcad087d117be541588a2c122137b4 less globals --- diff --git a/ftepp.c b/ftepp.c index 6fb8c6f..595b520 100644 --- a/ftepp.c +++ b/ftepp.c @@ -65,8 +65,7 @@ typedef struct ftepp_s { bool output_on; ppcondition *conditions; /*ppmacro **macros;*/ - ht macros; /* hashtable */ - + ht macros; /* hashtable */ char *output_string; char *itemname; @@ -198,7 +197,12 @@ char *ftepp_predef_timestamp(lex_file *context) { return value; } -const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = { +typedef struct { + const char *name; + char *(*func)(lex_file *); +} ftepp_predef_t; + +static const ftepp_predef_t ftepp_predefs[] = { { "__LINE__", &ftepp_predef_line }, { "__FILE__", &ftepp_predef_file }, { "__COUNTER__", &ftepp_predef_counter }, @@ -210,6 +214,25 @@ const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = { { "__TIME_STAMP__", &ftepp_predef_timestamp } }; +static GMQCC_INLINE int ftepp_predef_index(const char *name) { + /* no hashtable here, we simply check for one to exist the naive way */ + int i; + for(i = 0; i < (int)(sizeof(ftepp_predefs)/sizeof(*ftepp_predefs)); i++) + if (!strcmp(ftepp_predefs[i].name, name)) + return i; + return -1; +} + +bool ftepp_predef_exists(const char *name) { + return ftepp_predef_index(name) != -1; +} + +/* singleton because we're allowed */ +static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) { + int i = ftepp_predef_index(name); + return (i != -1) ? ftepp_predefs[i].func : NULL; +} + #define ftepp_tokval(f) ((f)->lex->tok.value) #define ftepp_ctx(f) ((f)->lex->tok.ctx) @@ -1652,7 +1675,6 @@ static bool ftepp_preprocess(ftepp_t *ftepp) /* predef stuff */ char *expand = NULL; - size_t i; ftepp->lex->flags.preprocessing = true; ftepp->lex->flags.mergelines = false; @@ -1673,15 +1695,14 @@ static bool ftepp_preprocess(ftepp_t *ftepp) case TOKEN_TYPENAME: /* is it a predef? */ if (OPTS_FLAG(FTEPP_PREDEFS)) { - for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) { - if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) { - expand = ftepp_predefs[i].func(ftepp->lex); - ftepp_out(ftepp, expand, false); - ftepp_next(ftepp); /* skip */ - - mem_d(expand); /* free memory */ - break; - } + char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp)); + if (predef) { + expand = predef(ftepp->lex); + ftepp_out (ftepp, expand, false); + ftepp_next(ftepp); + + mem_d(expand); + break; } } diff --git a/gmqcc.h b/gmqcc.h index bea5cf1..0382dbb 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -1011,11 +1011,6 @@ void parser_cleanup (struct parser_s *parser); struct lex_file_s; struct ftepp_s; -typedef struct { - const char *name; - char *(*func)(struct lex_file_s *); -} ftepp_predef_t; - /* * line, file, counter, counter_last, random, random_last, date, time * time_stamp. @@ -1033,8 +1028,6 @@ void ftepp_flush (struct ftepp_s *ftepp); void ftepp_add_define (struct ftepp_s *ftepp, const char *source, const char *name); void ftepp_add_macro (struct ftepp_s *ftepp, const char *name, const char *value); -extern const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT]; - /*===================================================================*/ /*======================= main.c commandline ========================*/ /*===================================================================*/ diff --git a/parser.c b/parser.c index 8f9d014..c81fd2c 100644 --- a/parser.c +++ b/parser.c @@ -1788,6 +1788,9 @@ static ast_expression* parse_vararg(parser_t *parser) return out; } +/* not to be exposed */ +extern bool ftepp_predef_exists(const char *name); + static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels) { if (OPTS_FLAG(TRANSLATABLE_STRINGS) && @@ -1918,13 +1921,9 @@ static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels) * i've done this thousands of times already myself. Lets check for * it in the predef table. And diagnose it better :) */ - if (!OPTS_FLAG(FTEPP_PREDEFS)) { - for (i = 0; i < sizeof(ftepp_predefs)/sizeof(*ftepp_predefs); i++) { - if (!strcmp(ftepp_predefs[i].name, parser_tokval(parser))) { - parseerror(parser, "unexpected ident: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser)); - return false; - } - } + if (!OPTS_FLAG(FTEPP_PREDEFS) && ftepp_predef_exists(parser_tokval(parser))) { + parseerror(parser, "unexpected ident: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser)); + return false; } /*