2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #define HT_MACROS 1024
42 /* a copy from the lexer */
56 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
63 typedef struct ftepp_s {
69 ppcondition *conditions;
71 ht macros; /* hashtable<string, ppmacro*> */
78 uint32_t predef_countval;
79 uint32_t predef_randval;
83 static char *ftepp_predef_date(ftepp_t *context) {
84 const struct tm *itime = NULL;
85 char *value = (char*)mem_a(82);
91 itime = util_localtime(&rtime);
92 strftime(value, 82, "\"%b %d %Y\"", itime);
98 static char *ftepp_predef_time(ftepp_t *context) {
99 const struct tm *itime = NULL;
100 char *value = (char*)mem_a(82);
106 itime = util_localtime(&rtime);
107 strftime(value, 82, "\"%X\"", itime);
113 static char *ftepp_predef_line(ftepp_t *context) {
116 util_asprintf(&value, "%d", (int)context->lex->line);
120 static char *ftepp_predef_file(ftepp_t *context) {
121 size_t length = strlen(context->lex->name) + 3; /* two quotes and a terminator */
122 char *value = (char*)mem_a(length);
124 util_snprintf(value, length, "\"%s\"", context->lex->name);
127 /* __COUNTER_LAST__ */
128 static char *ftepp_predef_counterlast(ftepp_t *context) {
130 util_asprintf(&value, "%u", context->predef_countval);
134 static char *ftepp_predef_counter(ftepp_t *context) {
137 context->predef_countval ++;
138 util_asprintf(&value, "%u", context->predef_countval);
143 static char *ftepp_predef_random(ftepp_t *context) {
146 context->predef_randval = (util_rand() % 0xFF) + 1;
147 util_asprintf(&value, "%u", context->predef_randval);
150 /* __RANDOM_LAST__ */
151 static char *ftepp_predef_randomlast(ftepp_t *context) {
154 util_asprintf(&value, "%u", context->predef_randval);
158 static char *ftepp_predef_timestamp(ftepp_t *context) {
164 if (stat(context->lex->name, &finfo))
165 return util_strdup("\"<failed to determine timestamp>\"");
167 find = util_ctime(&finfo.st_mtime);
168 value = (char*)mem_a(strlen(find) + 1);
169 memcpy(&value[1], find, (size = strlen(find)) - 1);
179 char *(*func)(ftepp_t *);
182 static const ftepp_predef_t ftepp_predefs[] = {
183 { "__LINE__", &ftepp_predef_line },
184 { "__FILE__", &ftepp_predef_file },
185 { "__COUNTER__", &ftepp_predef_counter },
186 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
187 { "__RANDOM__", &ftepp_predef_random },
188 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
189 { "__DATE__", &ftepp_predef_date },
190 { "__TIME__", &ftepp_predef_time },
191 { "__TIME_STAMP__", &ftepp_predef_timestamp }
194 static GMQCC_INLINE size_t ftepp_predef_index(const char *name) {
195 /* no hashtable here, we simply check for one to exist the naive way */
197 for(i = 1; i < GMQCC_ARRAY_COUNT(ftepp_predefs) + 1; i++)
198 if (!strcmp(ftepp_predefs[i-1].name, name))
203 bool ftepp_predef_exists(const char *name);
204 bool ftepp_predef_exists(const char *name) {
205 return ftepp_predef_index(name) != 0;
208 /* singleton because we're allowed */
209 static GMQCC_INLINE char *(*ftepp_predef(const char *name))(ftepp_t *context) {
210 size_t i = ftepp_predef_index(name);
211 return (i != 0) ? ftepp_predefs[i-1].func : NULL;
214 #define ftepp_tokval(f) ((f)->lex->tok.value)
215 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
217 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx_t ctx, const char *fmt, ...)
224 con_cvprintmsg(ctx, LVL_ERROR, "error", fmt, ap);
228 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
235 con_cvprintmsg(ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
239 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
245 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
250 static pptoken *pptoken_make(ftepp_t *ftepp)
252 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
253 token->token = ftepp->token;
255 if (token->token == TOKEN_WHITE)
256 token->value = util_strdup(" ");
259 token->value = util_strdup(ftepp_tokval(ftepp));
261 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
265 static GMQCC_INLINE void pptoken_delete(pptoken *self)
271 static ppmacro *ppmacro_new(lex_ctx_t ctx, const char *name)
273 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
276 memset(macro, 0, sizeof(*macro));
277 macro->name = util_strdup(name);
281 static void ppmacro_delete(ppmacro *self)
284 for (i = 0; i < vec_size(self->params); ++i)
285 mem_d(self->params[i]);
286 vec_free(self->params);
287 for (i = 0; i < vec_size(self->output); ++i)
288 pptoken_delete(self->output[i]);
289 vec_free(self->output);
294 static ftepp_t* ftepp_new(void)
298 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
299 memset(ftepp, 0, sizeof(*ftepp));
301 ftepp->macros = util_htnew(HT_MACROS);
302 ftepp->output_on = true;
303 ftepp->predef_countval = 0;
304 ftepp->predef_randval = 0;
309 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
311 vec_free(self->output_string);
314 static void ftepp_delete(ftepp_t *self)
316 ftepp_flush_do(self);
318 mem_d(self->itemname);
319 if (self->includename)
320 vec_free(self->includename);
322 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
324 vec_free(self->conditions);
326 lex_close(self->lex);
330 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
332 if (ignore_cond || ftepp->output_on)
337 data = vec_add(ftepp->output_string, len);
338 memcpy(data, str, len);
342 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
345 ftepp->output_on = true;
346 for (i = 0; i < vec_size(ftepp->conditions); ++i)
347 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
350 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
352 return (ppmacro*)util_htget(ftepp->macros, name);
355 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
357 util_htrm(ftepp->macros, name, (void (*)(void*))&ppmacro_delete);
360 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
362 return (ftepp->token = lex_do(ftepp->lex));
365 /* Important: this does not skip newlines! */
366 static bool ftepp_skipspace(ftepp_t *ftepp)
368 if (ftepp->token != TOKEN_WHITE)
370 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
371 if (ftepp->token >= TOKEN_EOF) {
372 ftepp_error(ftepp, "unexpected end of preprocessor directive");
378 /* this one skips EOLs as well */
379 static bool ftepp_skipallwhite(ftepp_t *ftepp)
381 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
385 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
386 if (ftepp->token >= TOKEN_EOF) {
387 ftepp_error(ftepp, "unexpected end of preprocessor directive");
394 * The huge macro parsing code...
396 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
400 if (!ftepp_skipspace(ftepp))
402 if (ftepp->token == ')')
404 switch (ftepp->token) {
408 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
411 macro->variadic = true;
414 ftepp_error(ftepp, "unexpected token in parameter list");
418 if (!ftepp_skipspace(ftepp))
420 if (macro->variadic && ftepp->token != ')') {
421 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
424 } while (ftepp->token == ',');
426 if (ftepp->token != ')') {
427 ftepp_error(ftepp, "expected closing paren after macro parameter list");
431 /* skipspace happens in ftepp_define */
435 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
438 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
439 bool subscript = false;
441 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
442 subscript = !!(ftepp_next(ftepp) == '#');
444 if (subscript && ftepp_next(ftepp) != '#') {
445 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
447 } else if (subscript) {
448 if (ftepp_next(ftepp) == '[') {
449 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
450 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
454 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
456 if (ftepp_next(ftepp) != ']') {
457 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
462 * mark it as an array to be handled later as such and not
463 * as traditional __VA_ARGS__
465 ftepp->token = TOKEN_VA_ARGS_ARRAY;
466 ptok = pptoken_make(ftepp);
467 ptok->constval.i = index;
468 vec_push(macro->output, ptok);
471 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
475 int old = ftepp->token;
476 ftepp->token = TOKEN_VA_ARGS;
477 ptok = pptoken_make(ftepp);
478 vec_push(macro->output, ptok);
482 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
483 ftepp->token = TOKEN_VA_COUNT;
484 ptok = pptoken_make(ftepp);
485 vec_push(macro->output, ptok);
488 ptok = pptoken_make(ftepp);
489 vec_push(macro->output, ptok);
493 /* recursive expansion can cause EOFs here */
494 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
495 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
501 static const char *ftepp_math_constants[][2] = {
502 { "M_E", "2.7182818284590452354" }, /* e */
503 { "M_LOG2E", "1.4426950408889634074" }, /* log_2 e */
504 { "M_LOG10E", "0.43429448190325182765" }, /* log_10 e */
505 { "M_LN2", "0.69314718055994530942" }, /* log_e 2 */
506 { "M_LN10", "2.30258509299404568402" }, /* log_e 10 */
507 { "M_PI", "3.14159265358979323846" }, /* pi */
508 { "M_PI_2", "1.57079632679489661923" }, /* pi/2 */
509 { "M_PI_4", "0.78539816339744830962" }, /* pi/4 */
510 { "M_1_PI", "0.31830988618379067154" }, /* 1/pi */
511 { "M_2_PI", "0.63661977236758134308" }, /* 2/pi */
512 { "M_2_SQRTPI", "1.12837916709551257390" }, /* 2/sqrt(pi) */
513 { "M_SQRT2", "1.41421356237309504880" }, /* sqrt(2) */
514 { "M_SQRT1_2", "0.70710678118654752440" }, /* 1/sqrt(2) */
515 { "M_TAU", "6.28318530717958647692" } /* pi*2 */
518 static bool ftepp_define(ftepp_t *ftepp)
520 ppmacro *macro = NULL;
521 size_t l = ftepp_ctx(ftepp).line;
523 bool mathconstant = false;
525 (void)ftepp_next(ftepp);
526 if (!ftepp_skipspace(ftepp))
529 switch (ftepp->token) {
533 if (OPTS_FLAG(FTEPP_MATHDEFS)) {
534 for (i = 0; i < GMQCC_ARRAY_COUNT(ftepp_math_constants); i++) {
535 if (!strcmp(ftepp_math_constants[i][0], ftepp_tokval(ftepp))) {
542 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
544 if (OPTS_FLAG(FTEPP_MATHDEFS)) {
545 /* user defined ones take precedence */
546 if (macro && mathconstant) {
547 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
552 if (macro && ftepp->output_on) {
553 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
555 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
557 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
560 ftepp_error(ftepp, "expected macro name");
564 (void)ftepp_next(ftepp);
566 if (ftepp->token == '(') {
567 macro->has_params = true;
568 if (!ftepp_define_params(ftepp, macro)) {
569 ppmacro_delete(macro);
574 if (!ftepp_skipspace(ftepp)) {
575 ppmacro_delete(macro);
579 if (!ftepp_define_body(ftepp, macro)) {
580 ppmacro_delete(macro);
584 if (ftepp->output_on)
585 util_htset(ftepp->macros, macro->name, (void*)macro);
587 ppmacro_delete(macro);
590 for (; l < ftepp_ctx(ftepp).line; ++l)
591 ftepp_out(ftepp, "\n", true);
596 * When a macro is used we have to handle parameters as well
597 * as special-concatenation via ## or stringification via #
599 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
600 * this kind of parens. Curly braces or [] don't count towards the
607 static void macroparam_clean(macroparam *self)
610 for (i = 0; i < vec_size(self->tokens); ++i)
611 pptoken_delete(self->tokens[i]);
612 vec_free(self->tokens);
615 /* need to leave the last token up */
616 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
618 macroparam *params = NULL;
624 if (!ftepp_skipallwhite(ftepp))
626 while (ftepp->token != ')') {
628 if (!ftepp_skipallwhite(ftepp))
630 while (parens || ftepp->token != ',') {
631 if (ftepp->token == '(')
633 else if (ftepp->token == ')') {
638 ptok = pptoken_make(ftepp);
639 vec_push(mp.tokens, ptok);
640 if (ftepp_next(ftepp) >= TOKEN_EOF) {
641 ftepp_error(ftepp, "unexpected end of file in macro call");
645 vec_push(params, mp);
647 if (ftepp->token == ')')
649 if (ftepp->token != ',') {
650 ftepp_error(ftepp, "expected closing paren or comma in macro call");
653 if (ftepp_next(ftepp) >= TOKEN_EOF) {
654 ftepp_error(ftepp, "unexpected end of file in macro call");
658 *out_params = params;
663 macroparam_clean(&mp);
664 for (i = 0; i < vec_size(params); ++i)
665 macroparam_clean(¶ms[i]);
670 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
673 for (i = 0; i < vec_size(macro->params); ++i) {
674 if (!strcmp(macro->params[i], name)) {
682 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
687 switch (token->token) {
688 case TOKEN_STRINGCONST:
691 /* in preprocessor mode strings already are string,
692 * so we don't get actual newline bytes here.
693 * Still need to escape backslashes and quotes.
696 case '\\': ftepp_out(ftepp, "\\\\", false); break;
697 case '"': ftepp_out(ftepp, "\\\"", false); break;
700 ftepp_out(ftepp, chs, false);
707 ftepp_out(ftepp, " ", false);
710 ftepp_out(ftepp, "\\n", false);
713 ftepp_out(ftepp, token->value, false);
718 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
721 ftepp_out(ftepp, "\"", false);
722 for (i = 0; i < vec_size(param->tokens); ++i)
723 ftepp_stringify_token(ftepp, param->tokens[i]);
724 ftepp_out(ftepp, "\"", false);
727 static void ftepp_recursion_header(ftepp_t *ftepp)
729 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
732 static void ftepp_recursion_footer(ftepp_t *ftepp)
734 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
737 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
741 for (i = 0; i < vec_size(param->tokens); ++i) {
742 out = param->tokens[i];
743 if (out->token == TOKEN_EOL)
744 ftepp_out(ftepp, "\n", false);
746 ftepp_out(ftepp, out->value, false);
750 static bool ftepp_preprocess(ftepp_t *ftepp);
751 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
754 char *old_string = ftepp->output_string;
756 lex_file *old_lexer = ftepp->lex;
757 size_t vararg_start = vec_size(macro->params);
769 if (vararg_start < vec_size(params))
770 varargs = vec_size(params) - vararg_start;
775 if (!vec_size(macro->output))
778 ftepp->output_string = NULL;
779 for (o = 0; o < vec_size(macro->output); ++o) {
780 pptoken *out = macro->output[o];
781 switch (out->token) {
783 if (!macro->variadic) {
784 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
785 vec_free(old_string);
792 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
793 for (++pi; pi < varargs; ++pi) {
794 ftepp_out(ftepp, ", ", false);
795 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
799 case TOKEN_VA_ARGS_ARRAY:
800 if ((size_t)out->constval.i >= varargs) {
801 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
802 vec_free(old_string);
806 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
810 util_asprintf(&buffer, "%d", varargs);
811 ftepp_out(ftepp, buffer, false);
818 if (!macro_params_find(macro, out->value, &pi)) {
819 ftepp_out(ftepp, out->value, false);
822 ftepp_param_out(ftepp, ¶ms[pi]);
825 if (o + 1 < vec_size(macro->output)) {
826 nextok = macro->output[o+1]->token;
828 /* raw concatenation */
832 if ( (nextok == TOKEN_IDENT ||
833 nextok == TOKEN_KEYWORD ||
834 nextok == TOKEN_TYPENAME) &&
835 macro_params_find(macro, macro->output[o+1]->value, &pi))
838 ftepp_stringify(ftepp, ¶ms[pi]);
842 ftepp_out(ftepp, "#", false);
845 ftepp_out(ftepp, "\n", false);
848 ftepp_out(ftepp, out->value, false);
852 vec_push(ftepp->output_string, 0);
853 /* Now run the preprocessor recursively on this string buffer */
855 printf("__________\n%s\n=========\n", ftepp->output_string);
857 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
859 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
864 inlex->line = ftepp->lex->line;
865 inlex->sline = ftepp->lex->sline;
868 old_inmacro = ftepp->in_macro;
869 ftepp->in_macro = true;
870 ftepp->output_string = NULL;
871 if (!ftepp_preprocess(ftepp)) {
872 ftepp->in_macro = old_inmacro;
873 vec_free(ftepp->lex->open_string);
874 vec_free(ftepp->output_string);
875 lex_close(ftepp->lex);
879 ftepp->in_macro = old_inmacro;
880 vec_free(ftepp->lex->open_string);
881 lex_close(ftepp->lex);
883 inner_string = ftepp->output_string;
884 ftepp->output_string = old_string;
886 has_newlines = (strchr(inner_string, '\n') != NULL);
888 if (has_newlines && !old_inmacro)
889 ftepp_recursion_header(ftepp);
891 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
892 vec_free(inner_string);
894 if (has_newlines && !old_inmacro)
895 ftepp_recursion_footer(ftepp);
897 if (resetline && !ftepp->in_macro) {
899 util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
900 ftepp_out(ftepp, lineno, false);
903 old_string = ftepp->output_string;
905 ftepp->lex = old_lexer;
906 ftepp->output_string = old_string;
910 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
913 macroparam *params = NULL;
917 if (!macro->has_params) {
918 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
925 if (!ftepp_skipallwhite(ftepp))
928 if (ftepp->token != '(') {
929 ftepp_error(ftepp, "expected macro parameters in parenthesis");
934 paramline = ftepp->lex->sline;
935 if (!ftepp_macro_call_params(ftepp, ¶ms))
938 if ( vec_size(params) < vec_size(macro->params) ||
939 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
941 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
942 (macro->variadic ? " at least" : ""),
943 (unsigned int)vec_size(macro->params),
944 (unsigned int)vec_size(params));
949 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
954 for (o = 0; o < vec_size(params); ++o)
955 macroparam_clean(¶ms[o]);
961 * #if - the FTEQCC way:
962 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
963 * <numbers> => True if the number is not 0
964 * !<factor> => True if the factor yields false
965 * !!<factor> => ERROR on 2 or more unary nots
966 * <macro> => becomes the macro's FIRST token regardless of parameters
967 * <e> && <e> => True if both expressions are true
968 * <e> || <e> => True if either expression is true
970 * <ident> => False (remember for macros the <macro> rule applies instead)
971 * Unary + and - are weird and wrong in fteqcc so we don't allow them
972 * parenthesis in expressions are allowed
973 * parameter lists on macros are errors
974 * No mathematical calculations are executed
976 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
977 static bool ftepp_if_op(ftepp_t *ftepp)
979 ftepp->lex->flags.noops = false;
981 if (!ftepp_skipspace(ftepp))
983 ftepp->lex->flags.noops = true;
986 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
992 if (!ftepp_skipspace(ftepp))
995 while (ftepp->token == '!') {
998 if (!ftepp_skipspace(ftepp))
1002 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
1006 if (!ftepp_skipspace(ftepp))
1010 switch (ftepp->token) {
1012 case TOKEN_TYPENAME:
1014 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
1016 if (!ftepp_skipspace(ftepp))
1018 if (ftepp->token != '(') {
1019 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
1023 if (!ftepp_skipspace(ftepp))
1025 if (ftepp->token != TOKEN_IDENT &&
1026 ftepp->token != TOKEN_TYPENAME &&
1027 ftepp->token != TOKEN_KEYWORD)
1029 ftepp_error(ftepp, "defined() used on an unexpected token type");
1032 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1035 if (!ftepp_skipspace(ftepp))
1037 if (ftepp->token != ')') {
1038 ftepp_error(ftepp, "expected closing paren");
1044 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1045 if (!macro || !vec_size(macro->output)) {
1049 /* This does not expand recursively! */
1050 switch (macro->output[0]->token) {
1051 case TOKEN_INTCONST:
1052 *value_out = macro->output[0]->constval.i;
1053 *out = !!(macro->output[0]->constval.i);
1055 case TOKEN_FLOATCONST:
1056 *value_out = macro->output[0]->constval.f;
1057 *out = !!(macro->output[0]->constval.f);
1065 case TOKEN_STRINGCONST:
1069 case TOKEN_INTCONST:
1070 *value_out = ftepp->lex->tok.constval.i;
1071 *out = !!(ftepp->lex->tok.constval.i);
1073 case TOKEN_FLOATCONST:
1074 *value_out = ftepp->lex->tok.constval.f;
1075 *out = !!(ftepp->lex->tok.constval.f);
1080 if (!ftepp_if_expr(ftepp, out, value_out))
1082 if (ftepp->token != ')') {
1083 ftepp_error(ftepp, "expected closing paren in #if expression");
1089 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1090 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1091 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1095 *value_out = -*value_out;
1098 *value_out = (*out ? 1 : 0);
1104 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1106 if (!ftepp_next(ftepp))
1108 return ftepp_if_value(ftepp, out, value_out);
1112 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1114 if (!ftepp_if_value(ftepp, out, value_out))
1117 if (!ftepp_if_op(ftepp))
1120 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1123 /* FTEQCC is all right-associative and no precedence here */
1124 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1125 !strcmp(ftepp_tokval(ftepp), "||"))
1128 char opc = ftepp_tokval(ftepp)[0];
1132 if (!ftepp_next(ftepp))
1134 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1138 *out = *out && next;
1140 *out = *out || next;
1142 *value_out = (*out ? 1 : 0);
1145 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1146 !strcmp(ftepp_tokval(ftepp), "!=") ||
1147 !strcmp(ftepp_tokval(ftepp), ">=") ||
1148 !strcmp(ftepp_tokval(ftepp), "<=") ||
1149 !strcmp(ftepp_tokval(ftepp), ">") ||
1150 !strcmp(ftepp_tokval(ftepp), "<"))
1153 const char opc0 = ftepp_tokval(ftepp)[0];
1154 const char opc1 = ftepp_tokval(ftepp)[1];
1157 if (!ftepp_next(ftepp))
1159 if (!ftepp_if_expr(ftepp, &next, &other))
1163 *out = (*value_out == other);
1164 else if (opc0 == '!')
1165 *out = (*value_out != other);
1166 else if (opc0 == '>') {
1167 if (opc1 == '=') *out = (*value_out >= other);
1168 else *out = (*value_out > other);
1170 else if (opc0 == '<') {
1171 if (opc1 == '=') *out = (*value_out <= other);
1172 else *out = (*value_out < other);
1174 *value_out = (*out ? 1 : 0);
1179 ftepp_error(ftepp, "junk after #if");
1184 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1186 bool result = false;
1189 memset(cond, 0, sizeof(*cond));
1190 (void)ftepp_next(ftepp);
1192 if (!ftepp_skipspace(ftepp))
1194 if (ftepp->token == TOKEN_EOL) {
1195 ftepp_error(ftepp, "expected expression for #if-directive");
1199 if (!ftepp_if_expr(ftepp, &result, &dummy))
1207 * ifdef is rather simple
1209 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1212 memset(cond, 0, sizeof(*cond));
1213 (void)ftepp_next(ftepp);
1214 if (!ftepp_skipspace(ftepp))
1217 switch (ftepp->token) {
1219 case TOKEN_TYPENAME:
1221 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1224 ftepp_error(ftepp, "expected macro name");
1228 (void)ftepp_next(ftepp);
1229 if (!ftepp_skipspace(ftepp))
1231 /* relaxing this condition
1232 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1233 ftepp_error(ftepp, "stray tokens after #ifdef");
1242 * undef is also simple
1244 static bool ftepp_undef(ftepp_t *ftepp)
1246 (void)ftepp_next(ftepp);
1247 if (!ftepp_skipspace(ftepp))
1250 if (ftepp->output_on) {
1251 switch (ftepp->token) {
1253 case TOKEN_TYPENAME:
1255 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1258 ftepp_error(ftepp, "expected macro name");
1263 (void)ftepp_next(ftepp);
1264 if (!ftepp_skipspace(ftepp))
1266 /* relaxing this condition
1267 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1268 ftepp_error(ftepp, "stray tokens after #ifdef");
1275 /* Special unescape-string function which skips a leading quote
1276 * and stops at a quote, not just at \0
1278 static void unescape(const char *str, char *out) {
1280 while (*str && *str != '"') {
1284 case '\\': *out++ = *str; break;
1285 case '"': *out++ = *str; break;
1286 case 'a': *out++ = '\a'; break;
1287 case 'b': *out++ = '\b'; break;
1288 case 'r': *out++ = '\r'; break;
1289 case 'n': *out++ = '\n'; break;
1290 case 't': *out++ = '\t'; break;
1291 case 'f': *out++ = '\f'; break;
1292 case 'v': *out++ = '\v'; break;
1307 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1310 char *filename = NULL;
1311 const char *last_slash;
1317 last_slash = strrchr(pathfile, '/');
1320 len = last_slash - pathfile;
1321 memcpy(vec_add(filename, len), pathfile, len);
1322 vec_push(filename, '/');
1326 memcpy(vec_add(filename, len+1), file, len);
1327 vec_last(filename) = 0;
1329 fp = fs_file_open(filename, "rb");
1338 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1340 char *filename = NULL;
1342 filename = ftepp_include_find_path(file, ftepp->includename);
1344 filename = ftepp_include_find_path(file, ftepp->itemname);
1348 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1349 char *message = NULL;
1351 if (!ftepp_skipspace(ftepp))
1354 /* handle the odd non string constant case so it works like C */
1355 if (ftepp->token != TOKEN_STRINGCONST) {
1357 vec_append(message, 8, "#warning");
1359 while (ftepp->token != TOKEN_EOL) {
1360 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1363 vec_push(message, '\0');
1364 if (ftepp->output_on)
1365 store = ftepp_warn(ftepp, WARN_CPP, message);
1372 if (!ftepp->output_on)
1375 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1376 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1379 static void ftepp_directive_error(ftepp_t *ftepp) {
1380 char *message = NULL;
1382 if (!ftepp_skipspace(ftepp))
1385 /* handle the odd non string constant case so it works like C */
1386 if (ftepp->token != TOKEN_STRINGCONST) {
1387 vec_append(message, 6, "#error");
1389 while (ftepp->token != TOKEN_EOL) {
1390 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1393 vec_push(message, '\0');
1394 if (ftepp->output_on)
1395 ftepp_error(ftepp, message);
1400 if (!ftepp->output_on)
1403 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1404 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1407 static void ftepp_directive_message(ftepp_t *ftepp) {
1408 char *message = NULL;
1410 if (!ftepp_skipspace(ftepp))
1413 /* handle the odd non string constant case so it works like C */
1414 if (ftepp->token != TOKEN_STRINGCONST) {
1415 vec_append(message, 8, "#message");
1417 while (ftepp->token != TOKEN_EOL) {
1418 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1421 vec_push(message, '\0');
1422 if (ftepp->output_on)
1423 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1428 if (!ftepp->output_on)
1431 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1432 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1437 * FIXME: do we need/want a -I option?
1438 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1440 static bool ftepp_include(ftepp_t *ftepp)
1442 lex_file *old_lexer = ftepp->lex;
1447 char *old_includename;
1449 (void)ftepp_next(ftepp);
1450 if (!ftepp_skipspace(ftepp))
1453 if (ftepp->token != TOKEN_STRINGCONST) {
1454 ftepp_error(ftepp, "expected filename to include");
1458 if (!ftepp->output_on) {
1463 ctx = ftepp_ctx(ftepp);
1465 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1467 ftepp_out(ftepp, "\n#pragma file(", false);
1468 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1469 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1471 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1473 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1476 inlex = lex_open(filename);
1478 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1483 old_includename = ftepp->includename;
1484 ftepp->includename = filename;
1485 if (!ftepp_preprocess(ftepp)) {
1486 vec_free(ftepp->includename);
1487 ftepp->includename = old_includename;
1488 lex_close(ftepp->lex);
1489 ftepp->lex = old_lexer;
1492 vec_free(ftepp->includename);
1493 ftepp->includename = old_includename;
1494 lex_close(ftepp->lex);
1495 ftepp->lex = old_lexer;
1497 ftepp_out(ftepp, "\n#pragma file(", false);
1498 ftepp_out(ftepp, ctx.file, false);
1499 util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1500 ftepp_out(ftepp, lineno, false);
1503 (void)ftepp_next(ftepp);
1504 if (!ftepp_skipspace(ftepp))
1506 if (ftepp->token != TOKEN_EOL) {
1507 ftepp_error(ftepp, "stray tokens after #include");
1510 (void)ftepp_next(ftepp);
1515 /* Basic structure handlers */
1516 static bool ftepp_else_allowed(ftepp_t *ftepp)
1518 if (!vec_size(ftepp->conditions)) {
1519 ftepp_error(ftepp, "#else without #if");
1522 if (vec_last(ftepp->conditions).had_else) {
1523 ftepp_error(ftepp, "multiple #else for a single #if");
1529 static GMQCC_INLINE void ftepp_inmacro(ftepp_t *ftepp, const char *hash) {
1530 if (ftepp->in_macro)
1531 (void)!ftepp_warn(ftepp, WARN_DIRECTIVE_INMACRO, "`#%s` directive in macro", hash);
1534 static bool ftepp_hash(ftepp_t *ftepp)
1539 lex_ctx_t ctx = ftepp_ctx(ftepp);
1541 if (!ftepp_skipspace(ftepp))
1544 switch (ftepp->token) {
1547 case TOKEN_TYPENAME:
1548 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1549 ftepp_inmacro(ftepp, "define");
1550 return ftepp_define(ftepp);
1552 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1553 ftepp_inmacro(ftepp, "undef");
1554 return ftepp_undef(ftepp);
1556 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1557 ftepp_inmacro(ftepp, "ifdef");
1558 if (!ftepp_ifdef(ftepp, &cond))
1560 cond.was_on = cond.on;
1561 vec_push(ftepp->conditions, cond);
1562 ftepp->output_on = ftepp->output_on && cond.on;
1565 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1566 ftepp_inmacro(ftepp, "ifndef");
1567 if (!ftepp_ifdef(ftepp, &cond))
1570 cond.was_on = cond.on;
1571 vec_push(ftepp->conditions, cond);
1572 ftepp->output_on = ftepp->output_on && cond.on;
1575 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1576 ftepp_inmacro(ftepp, "elifdef");
1577 if (!ftepp_else_allowed(ftepp))
1579 if (!ftepp_ifdef(ftepp, &cond))
1581 pc = &vec_last(ftepp->conditions);
1582 pc->on = !pc->was_on && cond.on;
1583 pc->was_on = pc->was_on || pc->on;
1584 ftepp_update_output_condition(ftepp);
1587 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1588 ftepp_inmacro(ftepp, "elifndef");
1589 if (!ftepp_else_allowed(ftepp))
1591 if (!ftepp_ifdef(ftepp, &cond))
1594 pc = &vec_last(ftepp->conditions);
1595 pc->on = !pc->was_on && cond.on;
1596 pc->was_on = pc->was_on || pc->on;
1597 ftepp_update_output_condition(ftepp);
1600 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1601 ftepp_inmacro(ftepp, "elif");
1602 if (!ftepp_else_allowed(ftepp))
1604 if (!ftepp_if(ftepp, &cond))
1606 pc = &vec_last(ftepp->conditions);
1607 pc->on = !pc->was_on && cond.on;
1608 pc->was_on = pc->was_on || pc->on;
1609 ftepp_update_output_condition(ftepp);
1612 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1613 ftepp_inmacro(ftepp, "if");
1614 if (!ftepp_if(ftepp, &cond))
1616 cond.was_on = cond.on;
1617 vec_push(ftepp->conditions, cond);
1618 ftepp->output_on = ftepp->output_on && cond.on;
1621 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1622 ftepp_inmacro(ftepp, "else");
1623 if (!ftepp_else_allowed(ftepp))
1625 pc = &vec_last(ftepp->conditions);
1626 pc->on = !pc->was_on;
1627 pc->had_else = true;
1629 ftepp_update_output_condition(ftepp);
1632 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1633 ftepp_inmacro(ftepp, "endif");
1634 if (!vec_size(ftepp->conditions)) {
1635 ftepp_error(ftepp, "#endif without #if");
1638 vec_pop(ftepp->conditions);
1640 ftepp_update_output_condition(ftepp);
1643 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1644 ftepp_inmacro(ftepp, "include");
1645 return ftepp_include(ftepp);
1647 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1648 ftepp_out(ftepp, "#", false);
1651 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1652 ftepp_directive_warning(ftepp);
1655 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1656 ftepp_directive_error(ftepp);
1659 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1660 ftepp_directive_message(ftepp);
1664 if (ftepp->output_on) {
1665 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1672 /* break; never reached */
1674 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1677 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1680 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1683 /* Builtins! Don't forget the builtins! */
1684 case TOKEN_INTCONST:
1685 case TOKEN_FLOATCONST:
1686 ftepp_out(ftepp, "#", false);
1689 if (!ftepp_skipspace(ftepp))
1694 static bool ftepp_preprocess(ftepp_t *ftepp)
1697 bool newline = true;
1700 char *expand = NULL;
1702 ftepp->lex->flags.preprocessing = true;
1703 ftepp->lex->flags.mergelines = false;
1704 ftepp->lex->flags.noops = true;
1709 if (ftepp->token >= TOKEN_EOF)
1715 switch (ftepp->token) {
1718 case TOKEN_TYPENAME:
1719 /* is it a predef? */
1720 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1721 char *(*predef)(ftepp_t*) = ftepp_predef(ftepp_tokval(ftepp));
1723 expand = predef(ftepp);
1724 ftepp_out (ftepp, expand, false);
1732 if (ftepp->output_on)
1733 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1738 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1742 if (!ftepp_macro_call(ftepp, macro))
1743 ftepp->token = TOKEN_ERROR;
1747 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1751 ftepp->lex->flags.mergelines = true;
1752 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1753 ftepp_error(ftepp, "error in preprocessor directive");
1754 ftepp->token = TOKEN_ERROR;
1757 if (!ftepp_hash(ftepp))
1758 ftepp->token = TOKEN_ERROR;
1759 ftepp->lex->flags.mergelines = false;
1763 ftepp_out(ftepp, "\n", true);
1767 /* same as default but don't set newline=false */
1768 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1773 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1777 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1779 /* force a 0 at the end but don't count it as added to the output */
1780 vec_push(ftepp->output_string, 0);
1781 vec_shrinkby(ftepp->output_string, 1);
1783 return (ftepp->token == TOKEN_EOF);
1786 /* Like in parser.c - files keep the previous state so we have one global
1787 * preprocessor. Except here we will want to warn about dangling #ifs.
1789 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1792 if (vec_size(ftepp->conditions)) {
1793 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1796 lex_close(ftepp->lex);
1798 if (ftepp->itemname) {
1799 mem_d(ftepp->itemname);
1800 ftepp->itemname = NULL;
1805 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1807 ftepp->lex = lex_open(filename);
1808 ftepp->itemname = util_strdup(filename);
1810 con_out("failed to open file \"%s\"\n", filename);
1813 if (!ftepp_preprocess(ftepp))
1815 return ftepp_preprocess_done(ftepp);
1818 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1820 ftepp->lex = lex_open_string(str, strlen(str), name);
1821 ftepp->itemname = util_strdup(name);
1823 con_out("failed to create lexer for string \"%s\"\n", name);
1826 if (!ftepp_preprocess(ftepp))
1828 return ftepp_preprocess_done(ftepp);
1832 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1833 char *create = NULL;
1835 /* use saner path for empty macros */
1837 ftepp_add_define(ftepp, "__builtin__", name);
1841 vec_append(create, 8, "#define ");
1842 vec_append(create, strlen(name), name);
1843 vec_push (create, ' ');
1844 vec_append(create, strlen(value), value);
1845 vec_push (create, 0);
1847 ftepp_preprocess_string(ftepp, "__builtin__", create);
1851 ftepp_t *ftepp_create()
1858 ftepp = ftepp_new();
1862 memset(minor, 0, sizeof(minor));
1863 memset(major, 0, sizeof(major));
1865 /* set the right macro based on the selected standard */
1866 ftepp_add_define(ftepp, NULL, "GMQCC");
1867 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1868 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1877 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1878 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1879 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1880 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1881 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1882 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1883 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1884 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1885 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1886 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1897 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1898 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1901 * We're going to just make __NULL__ nil, which works for 60% of the
1902 * cases of __NULL_ for fteqcc.
1904 ftepp_add_macro(ftepp, "__NULL__", "nil");
1906 /* add all the math constants if they can be */
1907 if (OPTS_FLAG(FTEPP_MATHDEFS)) {
1908 for (i = 0; i < GMQCC_ARRAY_COUNT(ftepp_math_constants); i++)
1909 if (!ftepp_macro_find(ftepp, ftepp_math_constants[i][0]))
1910 ftepp_add_macro(ftepp, ftepp_math_constants[i][0], ftepp_math_constants[i][1]);
1916 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1919 lex_ctx_t ctx = { "__builtin__", 0, 0 };
1921 macro = ppmacro_new(ctx, name);
1922 /*vec_push(ftepp->macros, macro);*/
1923 util_htset(ftepp->macros, name, macro);
1926 const char *ftepp_get(ftepp_t *ftepp)
1928 return ftepp->output_string;
1931 void ftepp_flush(ftepp_t *ftepp)
1933 ftepp_flush_do(ftepp);
1936 void ftepp_finish(ftepp_t *ftepp)
1940 ftepp_delete(ftepp);