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
37 /* a copy from the lexer */
51 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
64 ppcondition *conditions;
75 * Implement the predef subsystem now. We can do this safely with the
76 * help of lexer contexts.
78 static uint32_t ftepp_predef_countval = 0;
79 static uint32_t ftepp_predef_randval = 0;
82 char *ftepp_predef_date(lex_file *context) {
85 char *value = (char*)mem_a(82);
86 /* 82 is enough for strftime but we also have " " in our string */
92 itime = localtime(&rtime);
94 strftime(value, 82, "\"%b %d %Y\"", itime);
100 char *ftepp_predef_time(lex_file *context) {
103 char *value = (char*)mem_a(82);
104 /* 82 is enough for strftime but we also have " " in our string */
110 itime = localtime(&rtime);
112 strftime(value, 82, "\"%X\"", itime);
118 char *ftepp_predef_line(lex_file *context) {
120 util_asprintf(&value, "%d", (int)context->line);
124 char *ftepp_predef_file(lex_file *context) {
125 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
126 char *value = (char*)mem_a(length);
127 snprintf(value, length, "\"%s\"", context->name);
131 /* __COUNTER_LAST__ */
132 char *ftepp_predef_counterlast(lex_file *context) {
134 util_asprintf(&value, "%u", ftepp_predef_countval);
140 char *ftepp_predef_counter(lex_file *context) {
142 ftepp_predef_countval ++;
143 util_asprintf(&value, "%u", ftepp_predef_countval);
149 char *ftepp_predef_random(lex_file *context) {
151 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
152 util_asprintf(&value, "%u", ftepp_predef_randval);
157 /* __RANDOM_LAST__ */
158 char *ftepp_predef_randomlast(lex_file *context) {
160 util_asprintf(&value, "%u", ftepp_predef_randval);
166 const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = {
167 { "__LINE__", &ftepp_predef_line },
168 { "__FILE__", &ftepp_predef_file },
169 { "__COUNTER__", &ftepp_predef_counter },
170 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
171 { "__RANDOM__", &ftepp_predef_random },
172 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
173 { "__DATE__", &ftepp_predef_date },
174 { "__TIME__", &ftepp_predef_time }
177 #define ftepp_tokval(f) ((f)->lex->tok.value)
178 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
180 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
187 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
191 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
198 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
202 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
208 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
213 static pptoken *pptoken_make(ftepp_t *ftepp)
215 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
216 token->token = ftepp->token;
218 if (token->token == TOKEN_WHITE)
219 token->value = util_strdup(" ");
222 token->value = util_strdup(ftepp_tokval(ftepp));
224 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
228 static void pptoken_delete(pptoken *self)
234 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
236 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
239 memset(macro, 0, sizeof(*macro));
240 macro->name = util_strdup(name);
244 static void ppmacro_delete(ppmacro *self)
247 for (i = 0; i < vec_size(self->params); ++i)
248 mem_d(self->params[i]);
249 vec_free(self->params);
250 for (i = 0; i < vec_size(self->output); ++i)
251 pptoken_delete(self->output[i]);
252 vec_free(self->output);
257 static ftepp_t* ftepp_new()
261 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
262 memset(ftepp, 0, sizeof(*ftepp));
264 ftepp->output_on = true;
269 static void ftepp_flush_do(ftepp_t *self)
271 vec_free(self->output_string);
274 static void ftepp_delete(ftepp_t *self)
277 ftepp_flush_do(self);
279 mem_d(self->itemname);
280 if (self->includename)
281 vec_free(self->includename);
282 for (i = 0; i < vec_size(self->macros); ++i)
283 ppmacro_delete(self->macros[i]);
284 vec_free(self->macros);
285 vec_free(self->conditions);
287 lex_close(self->lex);
291 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
293 if (ignore_cond || ftepp->output_on)
298 data = vec_add(ftepp->output_string, len);
299 memcpy(data, str, len);
303 static void ftepp_update_output_condition(ftepp_t *ftepp)
306 ftepp->output_on = true;
307 for (i = 0; i < vec_size(ftepp->conditions); ++i)
308 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
311 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
314 for (i = 0; i < vec_size(ftepp->macros); ++i) {
315 if (!strcmp(name, ftepp->macros[i]->name))
316 return ftepp->macros[i];
321 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
324 for (i = 0; i < vec_size(ftepp->macros); ++i) {
325 if (!strcmp(name, ftepp->macros[i]->name)) {
326 vec_remove(ftepp->macros, i, 1);
332 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
334 return (ftepp->token = lex_do(ftepp->lex));
337 /* Important: this does not skip newlines! */
338 static bool ftepp_skipspace(ftepp_t *ftepp)
340 if (ftepp->token != TOKEN_WHITE)
342 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
343 if (ftepp->token >= TOKEN_EOF) {
344 ftepp_error(ftepp, "unexpected end of preprocessor directive");
350 /* this one skips EOLs as well */
351 static bool ftepp_skipallwhite(ftepp_t *ftepp)
353 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
357 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
358 if (ftepp->token >= TOKEN_EOF) {
359 ftepp_error(ftepp, "unexpected end of preprocessor directive");
366 * The huge macro parsing code...
368 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
372 if (!ftepp_skipspace(ftepp))
374 if (ftepp->token == ')')
376 switch (ftepp->token) {
380 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
383 macro->variadic = true;
386 ftepp_error(ftepp, "unexpected token in parameter list");
390 if (!ftepp_skipspace(ftepp))
392 if (macro->variadic && ftepp->token != ')') {
393 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
396 } while (ftepp->token == ',');
397 if (ftepp->token != ')') {
398 ftepp_error(ftepp, "expected closing paren after macro parameter list");
402 /* skipspace happens in ftepp_define */
406 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
409 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
410 bool subscript = false;
412 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
413 subscript = !!(ftepp_next(ftepp) == '#');
415 if (subscript && ftepp_next(ftepp) != '#') {
416 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
418 } else if (subscript) {
419 if (ftepp_next(ftepp) == '[') {
420 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
421 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
425 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
427 if (ftepp_next(ftepp) != ']') {
428 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
433 * mark it as an array to be handled later as such and not
434 * as traditional __VA_ARGS__
436 ftepp->token = TOKEN_VA_ARGS_ARRAY;
437 ptok = pptoken_make(ftepp);
438 ptok->constval.i = index;
439 vec_push(macro->output, ptok);
442 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
446 int old = ftepp->token;
447 ftepp->token = TOKEN_VA_ARGS;
448 ptok = pptoken_make(ftepp);
449 vec_push(macro->output, ptok);
453 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
454 ftepp->token = TOKEN_VA_COUNT;
455 ptok = pptoken_make(ftepp);
456 vec_push(macro->output, ptok);
459 ptok = pptoken_make(ftepp);
460 vec_push(macro->output, ptok);
464 /* recursive expansion can cause EOFs here */
465 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
466 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
472 static bool ftepp_define(ftepp_t *ftepp)
474 ppmacro *macro = NULL;
475 size_t l = ftepp_ctx(ftepp).line;
477 (void)ftepp_next(ftepp);
478 if (!ftepp_skipspace(ftepp))
481 switch (ftepp->token) {
485 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
486 if (macro && ftepp->output_on) {
487 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
489 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
491 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
494 ftepp_error(ftepp, "expected macro name");
498 (void)ftepp_next(ftepp);
500 if (ftepp->token == '(') {
501 macro->has_params = true;
502 if (!ftepp_define_params(ftepp, macro)) {
503 ppmacro_delete(macro);
508 if (!ftepp_skipspace(ftepp)) {
509 ppmacro_delete(macro);
513 if (!ftepp_define_body(ftepp, macro)) {
514 ppmacro_delete(macro);
518 if (ftepp->output_on)
519 vec_push(ftepp->macros, macro);
521 ppmacro_delete(macro);
524 for (; l < ftepp_ctx(ftepp).line; ++l)
525 ftepp_out(ftepp, "\n", true);
530 * When a macro is used we have to handle parameters as well
531 * as special-concatenation via ## or stringification via #
533 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
534 * this kind of parens. Curly braces or [] don't count towards the
541 static void macroparam_clean(macroparam *self)
544 for (i = 0; i < vec_size(self->tokens); ++i)
545 pptoken_delete(self->tokens[i]);
546 vec_free(self->tokens);
549 /* need to leave the last token up */
550 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
552 macroparam *params = NULL;
558 if (!ftepp_skipallwhite(ftepp))
560 while (ftepp->token != ')') {
562 if (!ftepp_skipallwhite(ftepp))
564 while (parens || ftepp->token != ',') {
565 if (ftepp->token == '(')
567 else if (ftepp->token == ')') {
572 ptok = pptoken_make(ftepp);
573 vec_push(mp.tokens, ptok);
574 if (ftepp_next(ftepp) >= TOKEN_EOF) {
575 ftepp_error(ftepp, "unexpected EOF in macro call");
579 vec_push(params, mp);
581 if (ftepp->token == ')')
583 if (ftepp->token != ',') {
584 ftepp_error(ftepp, "expected closing paren or comma in macro call");
587 if (ftepp_next(ftepp) >= TOKEN_EOF) {
588 ftepp_error(ftepp, "unexpected EOF in macro call");
592 /* need to leave that up
593 if (ftepp_next(ftepp) >= TOKEN_EOF) {
594 ftepp_error(ftepp, "unexpected EOF in macro call");
598 *out_params = params;
603 macroparam_clean(&mp);
604 for (i = 0; i < vec_size(params); ++i)
605 macroparam_clean(¶ms[i]);
610 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
613 for (i = 0; i < vec_size(macro->params); ++i) {
614 if (!strcmp(macro->params[i], name)) {
622 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
627 switch (token->token) {
628 case TOKEN_STRINGCONST:
631 /* in preprocessor mode strings already are string,
632 * so we don't get actual newline bytes here.
633 * Still need to escape backslashes and quotes.
636 case '\\': ftepp_out(ftepp, "\\\\", false); break;
637 case '"': ftepp_out(ftepp, "\\\"", false); break;
640 ftepp_out(ftepp, chs, false);
647 ftepp_out(ftepp, " ", false);
650 ftepp_out(ftepp, "\\n", false);
653 ftepp_out(ftepp, token->value, false);
658 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
661 ftepp_out(ftepp, "\"", false);
662 for (i = 0; i < vec_size(param->tokens); ++i)
663 ftepp_stringify_token(ftepp, param->tokens[i]);
664 ftepp_out(ftepp, "\"", false);
667 static void ftepp_recursion_header(ftepp_t *ftepp)
669 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
672 static void ftepp_recursion_footer(ftepp_t *ftepp)
674 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
677 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
681 for (i = 0; i < vec_size(param->tokens); ++i) {
682 out = param->tokens[i];
683 if (out->token == TOKEN_EOL)
684 ftepp_out(ftepp, "\n", false);
686 ftepp_out(ftepp, out->value, false);
690 static bool ftepp_preprocess(ftepp_t *ftepp);
691 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
694 char *old_string = ftepp->output_string;
696 lex_file *old_lexer = ftepp->lex;
697 size_t vararg_start = vec_size(macro->params);
709 if (vararg_start < vec_size(params))
710 varargs = vec_size(params) - vararg_start;
715 if (!vec_size(macro->output))
718 ftepp->output_string = NULL;
719 for (o = 0; o < vec_size(macro->output); ++o) {
720 pptoken *out = macro->output[o];
721 switch (out->token) {
723 if (!macro->variadic) {
724 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
725 vec_free(old_string);
732 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
733 for (++pi; pi < varargs; ++pi) {
734 ftepp_out(ftepp, ", ", false);
735 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
739 case TOKEN_VA_ARGS_ARRAY:
740 if ((size_t)out->constval.i >= varargs) {
741 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
742 vec_free(old_string);
746 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
750 util_asprintf(&buffer, "%d", varargs);
751 ftepp_out(ftepp, buffer, false);
758 if (!macro_params_find(macro, out->value, &pi)) {
759 ftepp_out(ftepp, out->value, false);
762 ftepp_param_out(ftepp, ¶ms[pi]);
765 if (o + 1 < vec_size(macro->output)) {
766 nextok = macro->output[o+1]->token;
768 /* raw concatenation */
772 if ( (nextok == TOKEN_IDENT ||
773 nextok == TOKEN_KEYWORD ||
774 nextok == TOKEN_TYPENAME) &&
775 macro_params_find(macro, macro->output[o+1]->value, &pi))
778 ftepp_stringify(ftepp, ¶ms[pi]);
782 ftepp_out(ftepp, "#", false);
785 ftepp_out(ftepp, "\n", false);
788 ftepp_out(ftepp, out->value, false);
792 vec_push(ftepp->output_string, 0);
793 /* Now run the preprocessor recursively on this string buffer */
795 printf("__________\n%s\n=========\n", ftepp->output_string);
797 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
799 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
804 inlex->line = ftepp->lex->line;
805 inlex->sline = ftepp->lex->sline;
808 old_inmacro = ftepp->in_macro;
809 ftepp->in_macro = true;
810 ftepp->output_string = NULL;
811 if (!ftepp_preprocess(ftepp)) {
812 ftepp->in_macro = old_inmacro;
813 vec_free(ftepp->lex->open_string);
814 vec_free(ftepp->output_string);
815 lex_close(ftepp->lex);
819 ftepp->in_macro = old_inmacro;
820 vec_free(ftepp->lex->open_string);
821 lex_close(ftepp->lex);
823 inner_string = ftepp->output_string;
824 ftepp->output_string = old_string;
826 has_newlines = (strchr(inner_string, '\n') != NULL);
828 if (has_newlines && !old_inmacro)
829 ftepp_recursion_header(ftepp);
831 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
832 vec_free(inner_string);
834 if (has_newlines && !old_inmacro)
835 ftepp_recursion_footer(ftepp);
837 if (resetline && !ftepp->in_macro) {
839 snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
840 ftepp_out(ftepp, lineno, false);
843 old_string = ftepp->output_string;
845 ftepp->lex = old_lexer;
846 ftepp->output_string = old_string;
850 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
853 macroparam *params = NULL;
857 if (!macro->has_params) {
858 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
865 if (!ftepp_skipallwhite(ftepp))
868 if (ftepp->token != '(') {
869 ftepp_error(ftepp, "expected macro parameters in parenthesis");
874 paramline = ftepp->lex->sline;
875 if (!ftepp_macro_call_params(ftepp, ¶ms))
878 if ( vec_size(params) < vec_size(macro->params) ||
879 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
881 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
882 (macro->variadic ? " at least" : ""),
883 (unsigned int)vec_size(macro->params),
884 (unsigned int)vec_size(params));
889 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
894 for (o = 0; o < vec_size(params); ++o)
895 macroparam_clean(¶ms[o]);
901 * #if - the FTEQCC way:
902 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
903 * <numbers> => True if the number is not 0
904 * !<factor> => True if the factor yields false
905 * !!<factor> => ERROR on 2 or more unary nots
906 * <macro> => becomes the macro's FIRST token regardless of parameters
907 * <e> && <e> => True if both expressions are true
908 * <e> || <e> => True if either expression is true
910 * <ident> => False (remember for macros the <macro> rule applies instead)
911 * Unary + and - are weird and wrong in fteqcc so we don't allow them
912 * parenthesis in expressions are allowed
913 * parameter lists on macros are errors
914 * No mathematical calculations are executed
916 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
917 static bool ftepp_if_op(ftepp_t *ftepp)
919 ftepp->lex->flags.noops = false;
921 if (!ftepp_skipspace(ftepp))
923 ftepp->lex->flags.noops = true;
926 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
932 if (!ftepp_skipspace(ftepp))
935 while (ftepp->token == '!') {
938 if (!ftepp_skipspace(ftepp))
942 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
946 if (!ftepp_skipspace(ftepp))
950 switch (ftepp->token) {
954 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
956 if (!ftepp_skipspace(ftepp))
958 if (ftepp->token != '(') {
959 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
963 if (!ftepp_skipspace(ftepp))
965 if (ftepp->token != TOKEN_IDENT &&
966 ftepp->token != TOKEN_TYPENAME &&
967 ftepp->token != TOKEN_KEYWORD)
969 ftepp_error(ftepp, "defined() used on an unexpected token type");
972 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
975 if (!ftepp_skipspace(ftepp))
977 if (ftepp->token != ')') {
978 ftepp_error(ftepp, "expected closing paren");
984 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
985 if (!macro || !vec_size(macro->output)) {
989 /* This does not expand recursively! */
990 switch (macro->output[0]->token) {
992 *value_out = macro->output[0]->constval.i;
993 *out = !!(macro->output[0]->constval.i);
995 case TOKEN_FLOATCONST:
996 *value_out = macro->output[0]->constval.f;
997 *out = !!(macro->output[0]->constval.f);
1005 case TOKEN_STRINGCONST:
1009 case TOKEN_INTCONST:
1010 *value_out = ftepp->lex->tok.constval.i;
1011 *out = !!(ftepp->lex->tok.constval.i);
1013 case TOKEN_FLOATCONST:
1014 *value_out = ftepp->lex->tok.constval.f;
1015 *out = !!(ftepp->lex->tok.constval.f);
1020 if (!ftepp_if_expr(ftepp, out, value_out))
1022 if (ftepp->token != ')') {
1023 ftepp_error(ftepp, "expected closing paren in #if expression");
1029 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1030 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1031 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1035 *value_out = -*value_out;
1038 *value_out = (*out ? 1 : 0);
1044 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1046 if (!ftepp_next(ftepp))
1048 return ftepp_if_value(ftepp, out, value_out);
1052 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1054 if (!ftepp_if_value(ftepp, out, value_out))
1057 if (!ftepp_if_op(ftepp))
1060 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1063 /* FTEQCC is all right-associative and no precedence here */
1064 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1065 !strcmp(ftepp_tokval(ftepp), "||"))
1068 char opc = ftepp_tokval(ftepp)[0];
1072 if (!ftepp_next(ftepp))
1074 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1078 *out = *out && next;
1080 *out = *out || next;
1082 *value_out = (*out ? 1 : 0);
1085 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1086 !strcmp(ftepp_tokval(ftepp), "!=") ||
1087 !strcmp(ftepp_tokval(ftepp), ">=") ||
1088 !strcmp(ftepp_tokval(ftepp), "<=") ||
1089 !strcmp(ftepp_tokval(ftepp), ">") ||
1090 !strcmp(ftepp_tokval(ftepp), "<"))
1093 const char opc0 = ftepp_tokval(ftepp)[0];
1094 const char opc1 = ftepp_tokval(ftepp)[1];
1097 if (!ftepp_next(ftepp))
1099 if (!ftepp_if_expr(ftepp, &next, &other))
1103 *out = (*value_out == other);
1104 else if (opc0 == '!')
1105 *out = (*value_out != other);
1106 else if (opc0 == '>') {
1107 if (opc1 == '=') *out = (*value_out >= other);
1108 else *out = (*value_out > other);
1110 else if (opc0 == '<') {
1111 if (opc1 == '=') *out = (*value_out <= other);
1112 else *out = (*value_out < other);
1114 *value_out = (*out ? 1 : 0);
1119 ftepp_error(ftepp, "junk after #if");
1124 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1126 bool result = false;
1129 memset(cond, 0, sizeof(*cond));
1130 (void)ftepp_next(ftepp);
1132 if (!ftepp_skipspace(ftepp))
1134 if (ftepp->token == TOKEN_EOL) {
1135 ftepp_error(ftepp, "expected expression for #if-directive");
1139 if (!ftepp_if_expr(ftepp, &result, &dummy))
1147 * ifdef is rather simple
1149 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1152 memset(cond, 0, sizeof(*cond));
1153 (void)ftepp_next(ftepp);
1154 if (!ftepp_skipspace(ftepp))
1157 switch (ftepp->token) {
1159 case TOKEN_TYPENAME:
1161 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1164 ftepp_error(ftepp, "expected macro name");
1168 (void)ftepp_next(ftepp);
1169 if (!ftepp_skipspace(ftepp))
1171 /* relaxing this condition
1172 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1173 ftepp_error(ftepp, "stray tokens after #ifdef");
1182 * undef is also simple
1184 static bool ftepp_undef(ftepp_t *ftepp)
1186 (void)ftepp_next(ftepp);
1187 if (!ftepp_skipspace(ftepp))
1190 if (ftepp->output_on) {
1191 switch (ftepp->token) {
1193 case TOKEN_TYPENAME:
1195 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1198 ftepp_error(ftepp, "expected macro name");
1203 (void)ftepp_next(ftepp);
1204 if (!ftepp_skipspace(ftepp))
1206 /* relaxing this condition
1207 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1208 ftepp_error(ftepp, "stray tokens after #ifdef");
1215 /* Special unescape-string function which skips a leading quote
1216 * and stops at a quote, not just at \0
1218 static void unescape(const char *str, char *out) {
1220 while (*str && *str != '"') {
1224 case '\\': *out++ = *str; break;
1225 case '"': *out++ = *str; break;
1226 case 'a': *out++ = '\a'; break;
1227 case 'b': *out++ = '\b'; break;
1228 case 'r': *out++ = '\r'; break;
1229 case 'n': *out++ = '\n'; break;
1230 case 't': *out++ = '\t'; break;
1231 case 'f': *out++ = '\f'; break;
1232 case 'v': *out++ = '\v'; break;
1247 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1250 char *filename = NULL;
1251 const char *last_slash;
1257 last_slash = strrchr(pathfile, '/');
1260 len = last_slash - pathfile;
1261 memcpy(vec_add(filename, len), pathfile, len);
1262 vec_push(filename, '/');
1266 memcpy(vec_add(filename, len+1), file, len);
1267 vec_last(filename) = 0;
1269 fp = fs_file_open(filename, "rb");
1278 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1280 char *filename = NULL;
1282 filename = ftepp_include_find_path(file, ftepp->includename);
1284 filename = ftepp_include_find_path(file, ftepp->itemname);
1288 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1289 char *message = NULL;
1291 if (!ftepp_skipspace(ftepp))
1294 /* handle the odd non string constant case so it works like C */
1295 if (ftepp->token != TOKEN_STRINGCONST) {
1297 vec_upload(message, "#warning", 8);
1299 while (ftepp->token != TOKEN_EOL) {
1300 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1303 vec_push(message, '\0');
1304 if (ftepp->output_on)
1305 store = ftepp_warn(ftepp, WARN_CPP, message);
1312 if (!ftepp->output_on)
1315 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1316 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1319 static void ftepp_directive_error(ftepp_t *ftepp) {
1320 char *message = NULL;
1322 if (!ftepp_skipspace(ftepp))
1325 /* handle the odd non string constant case so it works like C */
1326 if (ftepp->token != TOKEN_STRINGCONST) {
1327 vec_upload(message, "#error", 6);
1329 while (ftepp->token != TOKEN_EOL) {
1330 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1333 vec_push(message, '\0');
1334 if (ftepp->output_on)
1335 ftepp_error(ftepp, message);
1340 if (!ftepp->output_on)
1343 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1344 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1347 static void ftepp_directive_message(ftepp_t *ftepp) {
1348 char *message = NULL;
1350 if (!ftepp_skipspace(ftepp))
1353 /* handle the odd non string constant case so it works like C */
1354 if (ftepp->token != TOKEN_STRINGCONST) {
1355 vec_upload(message, "#message", 8);
1357 while (ftepp->token != TOKEN_EOL) {
1358 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1361 vec_push(message, '\0');
1362 if (ftepp->output_on)
1363 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1368 if (!ftepp->output_on)
1371 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1372 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1377 * FIXME: do we need/want a -I option?
1378 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1380 static bool ftepp_include(ftepp_t *ftepp)
1382 lex_file *old_lexer = ftepp->lex;
1387 char *old_includename;
1389 (void)ftepp_next(ftepp);
1390 if (!ftepp_skipspace(ftepp))
1393 if (ftepp->token != TOKEN_STRINGCONST) {
1394 ftepp_error(ftepp, "expected filename to include");
1398 if (!ftepp->output_on) {
1403 ctx = ftepp_ctx(ftepp);
1405 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1407 ftepp_out(ftepp, "\n#pragma file(", false);
1408 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1409 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1411 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1413 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1416 inlex = lex_open(filename);
1418 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1423 old_includename = ftepp->includename;
1424 ftepp->includename = filename;
1425 if (!ftepp_preprocess(ftepp)) {
1426 vec_free(ftepp->includename);
1427 ftepp->includename = old_includename;
1428 lex_close(ftepp->lex);
1429 ftepp->lex = old_lexer;
1432 vec_free(ftepp->includename);
1433 ftepp->includename = old_includename;
1434 lex_close(ftepp->lex);
1435 ftepp->lex = old_lexer;
1437 ftepp_out(ftepp, "\n#pragma file(", false);
1438 ftepp_out(ftepp, ctx.file, false);
1439 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1440 ftepp_out(ftepp, lineno, false);
1443 (void)ftepp_next(ftepp);
1444 if (!ftepp_skipspace(ftepp))
1446 if (ftepp->token != TOKEN_EOL) {
1447 ftepp_error(ftepp, "stray tokens after #include");
1450 (void)ftepp_next(ftepp);
1455 /* Basic structure handlers */
1456 static bool ftepp_else_allowed(ftepp_t *ftepp)
1458 if (!vec_size(ftepp->conditions)) {
1459 ftepp_error(ftepp, "#else without #if");
1462 if (vec_last(ftepp->conditions).had_else) {
1463 ftepp_error(ftepp, "multiple #else for a single #if");
1469 static bool ftepp_hash(ftepp_t *ftepp)
1474 lex_ctx ctx = ftepp_ctx(ftepp);
1476 if (!ftepp_skipspace(ftepp))
1479 switch (ftepp->token) {
1482 case TOKEN_TYPENAME:
1483 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1484 return ftepp_define(ftepp);
1486 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1487 return ftepp_undef(ftepp);
1489 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1490 if (!ftepp_ifdef(ftepp, &cond))
1492 cond.was_on = cond.on;
1493 vec_push(ftepp->conditions, cond);
1494 ftepp->output_on = ftepp->output_on && cond.on;
1497 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1498 if (!ftepp_ifdef(ftepp, &cond))
1501 cond.was_on = cond.on;
1502 vec_push(ftepp->conditions, cond);
1503 ftepp->output_on = ftepp->output_on && cond.on;
1506 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1507 if (!ftepp_else_allowed(ftepp))
1509 if (!ftepp_ifdef(ftepp, &cond))
1511 pc = &vec_last(ftepp->conditions);
1512 pc->on = !pc->was_on && cond.on;
1513 pc->was_on = pc->was_on || pc->on;
1514 ftepp_update_output_condition(ftepp);
1517 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1518 if (!ftepp_else_allowed(ftepp))
1520 if (!ftepp_ifdef(ftepp, &cond))
1523 pc = &vec_last(ftepp->conditions);
1524 pc->on = !pc->was_on && cond.on;
1525 pc->was_on = pc->was_on || pc->on;
1526 ftepp_update_output_condition(ftepp);
1529 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1530 if (!ftepp_else_allowed(ftepp))
1532 if (!ftepp_if(ftepp, &cond))
1534 pc = &vec_last(ftepp->conditions);
1535 pc->on = !pc->was_on && cond.on;
1536 pc->was_on = pc->was_on || pc->on;
1537 ftepp_update_output_condition(ftepp);
1540 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1541 if (!ftepp_if(ftepp, &cond))
1543 cond.was_on = cond.on;
1544 vec_push(ftepp->conditions, cond);
1545 ftepp->output_on = ftepp->output_on && cond.on;
1548 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1549 if (!ftepp_else_allowed(ftepp))
1551 pc = &vec_last(ftepp->conditions);
1552 pc->on = !pc->was_on;
1553 pc->had_else = true;
1555 ftepp_update_output_condition(ftepp);
1558 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1559 if (!vec_size(ftepp->conditions)) {
1560 ftepp_error(ftepp, "#endif without #if");
1563 vec_pop(ftepp->conditions);
1565 ftepp_update_output_condition(ftepp);
1568 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1569 return ftepp_include(ftepp);
1571 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1572 ftepp_out(ftepp, "#", false);
1575 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1576 ftepp_directive_warning(ftepp);
1579 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1580 ftepp_directive_error(ftepp);
1583 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1584 ftepp_directive_message(ftepp);
1588 if (ftepp->output_on) {
1589 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1596 /* break; never reached */
1598 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1601 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1604 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1607 /* Builtins! Don't forget the builtins! */
1608 case TOKEN_INTCONST:
1609 case TOKEN_FLOATCONST:
1610 ftepp_out(ftepp, "#", false);
1613 if (!ftepp_skipspace(ftepp))
1618 static bool ftepp_preprocess(ftepp_t *ftepp)
1621 bool newline = true;
1624 char *expand = NULL;
1627 ftepp->lex->flags.preprocessing = true;
1628 ftepp->lex->flags.mergelines = false;
1629 ftepp->lex->flags.noops = true;
1634 if (ftepp->token >= TOKEN_EOF)
1640 switch (ftepp->token) {
1643 case TOKEN_TYPENAME:
1644 /* is it a predef? */
1645 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1646 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1647 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1648 expand = ftepp_predefs[i].func(ftepp->lex);
1649 ftepp_out(ftepp, expand, false);
1650 ftepp_next(ftepp); /* skip */
1652 mem_d(expand); /* free memory */
1658 if (ftepp->output_on)
1659 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1664 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1668 if (!ftepp_macro_call(ftepp, macro))
1669 ftepp->token = TOKEN_ERROR;
1673 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1677 ftepp->lex->flags.mergelines = true;
1678 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1679 ftepp_error(ftepp, "error in preprocessor directive");
1680 ftepp->token = TOKEN_ERROR;
1683 if (!ftepp_hash(ftepp))
1684 ftepp->token = TOKEN_ERROR;
1685 ftepp->lex->flags.mergelines = false;
1689 ftepp_out(ftepp, "\n", true);
1693 /* same as default but don't set newline=false */
1694 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1699 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1703 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1705 /* force a 0 at the end but don't count it as added to the output */
1706 vec_push(ftepp->output_string, 0);
1707 vec_shrinkby(ftepp->output_string, 1);
1709 return (ftepp->token == TOKEN_EOF);
1712 /* Like in parser.c - files keep the previous state so we have one global
1713 * preprocessor. Except here we will want to warn about dangling #ifs.
1715 static ftepp_t *ftepp;
1717 static bool ftepp_preprocess_done()
1720 if (vec_size(ftepp->conditions)) {
1721 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1724 lex_close(ftepp->lex);
1726 if (ftepp->itemname) {
1727 mem_d(ftepp->itemname);
1728 ftepp->itemname = NULL;
1733 bool ftepp_preprocess_file(const char *filename)
1735 ftepp->lex = lex_open(filename);
1736 ftepp->itemname = util_strdup(filename);
1738 con_out("failed to open file \"%s\"\n", filename);
1741 if (!ftepp_preprocess(ftepp))
1743 return ftepp_preprocess_done();
1746 bool ftepp_preprocess_string(const char *name, const char *str)
1748 ftepp->lex = lex_open_string(str, strlen(str), name);
1749 ftepp->itemname = util_strdup(name);
1751 con_out("failed to create lexer for string \"%s\"\n", name);
1754 if (!ftepp_preprocess(ftepp))
1756 return ftepp_preprocess_done();
1760 void ftepp_add_macro(const char *name, const char *value) {
1761 char *create = NULL;
1763 /* use saner path for empty macros */
1765 ftepp_add_define("__builtin__", name);
1769 vec_upload(create, "#define ", 8);
1770 vec_upload(create, name, strlen(name));
1771 vec_push (create, ' ');
1772 vec_upload(create, value, strlen(value));
1773 vec_push (create, 0);
1775 ftepp_preprocess_string("__builtin__", create);
1784 ftepp = ftepp_new();
1788 memset(minor, 0, sizeof(minor));
1789 memset(major, 0, sizeof(major));
1791 /* set the right macro based on the selected standard */
1792 ftepp_add_define(NULL, "GMQCC");
1793 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1794 ftepp_add_define(NULL, "__STD_FTEQCC__");
1803 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1804 ftepp_add_define(NULL, "__STD_GMQCC__");
1805 snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1806 snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1807 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1808 ftepp_add_define(NULL, "__STD_QCCX__");
1809 snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1810 snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1811 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1812 ftepp_add_define(NULL, "__STD_QCC__");
1823 ftepp_add_macro("__STD_VERSION_MINOR__", minor);
1824 ftepp_add_macro("__STD_VERSION_MAJOR__", major);
1829 void ftepp_add_define(const char *source, const char *name)
1832 lex_ctx ctx = { "__builtin__", 0 };
1834 macro = ppmacro_new(ctx, name);
1835 vec_push(ftepp->macros, macro);
1838 const char *ftepp_get()
1840 return ftepp->output_string;
1845 ftepp_flush_do(ftepp);
1852 ftepp_delete(ftepp);