]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - ftepp.c
Some CLZ for other toolchains.
[xonotic/gmqcc.git] / ftepp.c
diff --git a/ftepp.c b/ftepp.c
index fc6732e5647d9548f0e4199ec5a9de1aef4b1739..8f452754d74b85b3f2787cfba12ee6ddbba112b6 100644 (file)
--- a/ftepp.c
+++ b/ftepp.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012, 2013
+ * Copyright (C) 2012, 2013, 2014
  *     Wolfgang Bumiller
  *     Dale Weiler
  *
@@ -530,18 +530,24 @@ static bool ftepp_define(ftepp_t *ftepp)
         case TOKEN_IDENT:
         case TOKEN_TYPENAME:
         case TOKEN_KEYWORD:
-            for (i = 0; i < GMQCC_ARRAY_COUNT(ftepp_math_constants); i++) {
-                if (!strcmp(ftepp_math_constants[i][0], ftepp_tokval(ftepp))) {
-                    mathconstant = true;
-                    break;
+            if (OPTS_FLAG(FTEPP_MATHDEFS)) {
+                for (i = 0; i < GMQCC_ARRAY_COUNT(ftepp_math_constants); i++) {
+                    if (!strcmp(ftepp_math_constants[i][0], ftepp_tokval(ftepp))) {
+                        mathconstant = true;
+                        break;
+                    }
                 }
             }
 
             macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
 
-            /* ignore creating a math macro if one is already present */
-            if (macro && mathconstant)
-                break;
+            if (OPTS_FLAG(FTEPP_MATHDEFS)) {
+                /* user defined ones take precedence */
+                if (macro && mathconstant) {
+                    ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
+                    macro = NULL;
+                }
+            }
 
             if (macro && ftepp->output_on) {
                 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
@@ -757,6 +763,7 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
     lex_file *inlex;
 
     bool      old_inmacro;
+    bool      strip = false;
 
     int nextok;
 
@@ -821,6 +828,7 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
                     if (nextok == '#') {
                         /* raw concatenation */
                         ++o;
+                        strip = true;
                         break;
                     }
                     if ( (nextok == TOKEN_IDENT    ||
@@ -829,6 +837,7 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
                         macro_params_find(macro, macro->output[o+1]->value, &pi))
                     {
                         ++o;
+
                         ftepp_stringify(ftepp, &params[pi]);
                         break;
                     }
@@ -839,7 +848,15 @@ static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *param
                 ftepp_out(ftepp, "\n", false);
                 break;
             default:
-                ftepp_out(ftepp, out->value, false);
+                buffer = out->value;
+                #define buffer_stripable(X) ((X) == ' ' || (X) == '\t')
+                if (vec_size(macro->output) > o + 1 && macro->output[o+1]->token == '#' && buffer_stripable(*buffer))
+                    buffer++;
+                if (strip) {
+                    while (buffer_stripable(*buffer)) buffer++;
+                    strip = false;
+                }
+                ftepp_out(ftepp, buffer, false);
                 break;
         }
     }
@@ -1898,9 +1915,11 @@ ftepp_t *ftepp_create()
     ftepp_add_macro(ftepp, "__NULL__", "nil");
 
     /* add all the math constants if they can be */
-    for (i = 0; i < GMQCC_ARRAY_COUNT(ftepp_math_constants); i++)
-        if (!ftepp_macro_find(ftepp, ftepp_math_constants[i][0]))
-            ftepp_add_macro(ftepp, ftepp_math_constants[i][0], ftepp_math_constants[i][1]);
+    if (OPTS_FLAG(FTEPP_MATHDEFS)) {
+        for (i = 0; i < GMQCC_ARRAY_COUNT(ftepp_math_constants); i++)
+            if (!ftepp_macro_find(ftepp, ftepp_math_constants[i][0]))
+                ftepp_add_macro(ftepp, ftepp_math_constants[i][0], ftepp_math_constants[i][1]);
+    }
 
     return ftepp;
 }