]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Since like with parsing, the preprocessor state has to be preserved across files...
authorWolfgang (Blub) Bumiller <blub@speed.at>
Sun, 18 Nov 2012 10:54:11 +0000 (11:54 +0100)
committerWolfgang (Blub) Bumiller <blub@speed.at>
Sun, 18 Nov 2012 10:54:11 +0000 (11:54 +0100)
ftepp.c
gmqcc.h
main.c
opts.def

diff --git a/ftepp.c b/ftepp.c
index 59ec54504402efdfdbf70c737405e5d4f73f07bd..865cd4e6f5e0867160de6bda34a7030099002d41 100644 (file)
--- a/ftepp.c
+++ b/ftepp.c
@@ -152,7 +152,7 @@ static void ppmacro_delete(ppmacro *self)
     mem_d(self);
 }
 
-static ftepp_t* ftepp_init()
+static ftepp_t* ftepp_new()
 {
     ftepp_t *ftepp;
 
@@ -509,6 +509,7 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
     ftepp->output_string = old_string_flag;
     ftepp->lex = inlex;
     if (!ftepp_preprocess(ftepp)) {
+        lex_close(ftepp->lex);
         retval = false;
         goto cleanup;
     }
@@ -1005,9 +1006,25 @@ static bool ftepp_preprocess(ftepp_t *ftepp)
     return newline;
 }
 
+/* Like in parser.c - files keep the previous state so we have one global
+ * preprocessor. Except here we will want to warn about dangling #ifs.
+ */
+static ftepp_t *ftepp;
+
+static bool ftepp_preprocess_done()
+{
+    bool retval = true;
+    lex_close(ftepp->lex);
+    ftepp->lex = NULL;
+    if (vec_size(ftepp->conditions)) {
+        if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
+            retval = false;
+    }
+    return retval;
+}
+
 bool ftepp_preprocess_file(const char *filename)
 {
-    ftepp_t *ftepp = ftepp_init();
     ftepp->lex = lex_open(filename);
     if (!ftepp->lex) {
         con_out("failed to open file \"%s\"\n", filename);
@@ -1017,13 +1034,12 @@ bool ftepp_preprocess_file(const char *filename)
         ftepp_delete(ftepp);
         return false;
     }
-    ftepp_delete(ftepp);
-    return true;
+    return ftepp_preprocess_done();
 }
 
 bool ftepp_preprocess_string(const char *name, const char *str)
 {
-    ftepp_t *ftepp = ftepp_init();
+    ftepp_t *ftepp = ftepp_new();
     ftepp->lex = lex_open_string(str, strlen(str), name);
     if (!ftepp->lex) {
         con_out("failed to create lexer for string \"%s\"\n", name);
@@ -1033,6 +1049,16 @@ bool ftepp_preprocess_string(const char *name, const char *str)
         ftepp_delete(ftepp);
         return false;
     }
+    return ftepp_preprocess_done();
+}
+
+bool ftepp_init()
+{
+    ftepp = ftepp_new();
+    return !!ftepp;
+}
+
+void ftepp_finish()
+{
     ftepp_delete(ftepp);
-    return true;
 }
diff --git a/gmqcc.h b/gmqcc.h
index 4f92eaf1067d293f8dfd9a9a58fba8f3f1195a75..d99a778ed174e507f175394b5b9029c518646cf7 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -755,8 +755,10 @@ void parser_cleanup       ();
 /*===================================================================*/
 /*====================== ftepp.c commandline ========================*/
 /*===================================================================*/
+bool ftepp_init             ();
 bool ftepp_preprocess_file  (const char *filename);
 bool ftepp_preprocess_string(const char *name, const char *str);
+void ftepp_finish           ();
 
 /*===================================================================*/
 /*======================= main.c commandline ========================*/
diff --git a/main.c b/main.c
index 2db65f9f6e43b42e748c661c6a28e634c77d1a6d..d1ac7f12e46560dde15d5d94bd50e7dd23f9b733 100644 (file)
--- a/main.c
+++ b/main.c
@@ -436,6 +436,7 @@ int main(int argc, char **argv) {
     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
     options_set(opts_warn, WARN_PREPROCESSOR, true);
+    options_set(opts_warn, WARN_MULTIFILE_IF, true);
 
     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
 
@@ -464,10 +465,19 @@ int main(int argc, char **argv) {
         con_out("standard = %i\n", opts_standard);
     }
 
-    if (!parser_init()) {
-        con_out("failed to initialize parser\n");
-        retval = 1;
-        goto cleanup;
+    if (!opts_pp_only) {
+        if (!parser_init()) {
+            con_err("failed to initialize parser\n");
+            retval = 1;
+            goto cleanup;
+        }
+    }
+    if (opts_pp_only || opts_standard == COMPILER_FTEQCC) {
+        if (!ftepp_init()) {
+            con_err("failed to initialize parser\n");
+            retval = 1;
+            goto cleanup;
+        }
     }
 
     util_debug("COM", "starting ...\n");
index 1c362d32b8983725ddfd55c271538ad5d0281a89..b9589892c56c3a499acb576878de0b63f8ed62ac 100644 (file)
--- a/opts.def
+++ b/opts.def
@@ -53,6 +53,7 @@
     GMQCC_DEFINE_FLAG(END_SYS_FIELDS)
     GMQCC_DEFINE_FLAG(ASSIGN_FUNCTION_TYPES)
     GMQCC_DEFINE_FLAG(PREPROCESSOR)
+    GMQCC_DEFINE_FLAG(MULTIFILE_IF)
 #endif
 
 /* some cleanup so we don't have to */