]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
less globals
authorDale Weiler <killfieldengine@gmail.com>
Thu, 25 Apr 2013 12:08:13 +0000 (12:08 +0000)
committerDale Weiler <killfieldengine@gmail.com>
Thu, 25 Apr 2013 12:08:13 +0000 (12:08 +0000)
ftepp.c
gmqcc.h
parser.c

diff --git a/ftepp.c b/ftepp.c
index 6fb8c6f8caeb93fd5c8c158abd1c35932a00b18f..595b520fec5439f34a1614f737f9f83f7ef3c150 100644 (file)
--- a/ftepp.c
+++ b/ftepp.c
@@ -65,8 +65,7 @@ typedef struct ftepp_s {
     bool         output_on;
     ppcondition *conditions;
     /*ppmacro    **macros;*/
-    ht           macros; /* hashtable<string, ppmacro*> */
-
+    ht           macros;  /* hashtable<string, ppmacro*> */
     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 bea5cf19893fe3904bf3e7933a6bef30bc03b514..0382dbb0823de720d74e2b3468f2165376e8281b 100644 (file)
--- 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 ========================*/
 /*===================================================================*/
index 8f9d014b8bc241672fbfd881c09fea87ea96b1ce..c81fd2c4db714d9e410b726accbe3ca19321cf48 100644 (file)
--- 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;
                 }
 
                 /*