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
36 /* a copy from the lexer */
50 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
63 ppcondition *conditions;
74 char *(*func)(lex_file *);
78 * Implement the predef subsystem now. We can do this safely with the
79 * help of lexer contexts.
81 static uint32_t ftepp_predef_countval = 0;
82 static uint32_t ftepp_predef_randval = 0;
85 char *ftepp_predef_line(lex_file *context) {
87 util_asprintf(&value, "%d", (int)context->line);
91 char *ftepp_predef_file(lex_file *context) {
92 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
93 char *value = (char*)mem_a(length);
94 memset (value, 0, length);
95 sprintf(value, "\"%s\"", context->name);
99 /* __COUNTER_LAST__ */
100 char *ftepp_predef_counterlast(lex_file *context) {
102 util_asprintf(&value, "%u", ftepp_predef_countval);
108 char *ftepp_predef_counter(lex_file *context) {
110 ftepp_predef_countval ++;
111 util_asprintf(&value, "%u", ftepp_predef_countval);
117 char *ftepp_predef_random(lex_file *context) {
119 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
120 util_asprintf(&value, "%u", ftepp_predef_randval);
125 /* __RANDOM_LAST__ */
126 char *ftepp_predef_randomlast(lex_file *context) {
128 util_asprintf(&value, "%u", ftepp_predef_randval);
134 static const predef_t ftepp_predefs[] = {
135 { "__LINE__", &ftepp_predef_line },
136 { "__FILE__", &ftepp_predef_file },
137 { "__COUNTER__", &ftepp_predef_counter },
138 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
139 { "__RANDOM__", &ftepp_predef_random },
140 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
143 #define ftepp_tokval(f) ((f)->lex->tok.value)
144 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
146 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
153 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
157 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
164 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
168 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
174 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
179 static pptoken *pptoken_make(ftepp_t *ftepp)
181 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
182 token->token = ftepp->token;
184 if (token->token == TOKEN_WHITE)
185 token->value = util_strdup(" ");
188 token->value = util_strdup(ftepp_tokval(ftepp));
190 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
194 static void pptoken_delete(pptoken *self)
200 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
202 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
205 memset(macro, 0, sizeof(*macro));
206 macro->name = util_strdup(name);
210 static void ppmacro_delete(ppmacro *self)
213 for (i = 0; i < vec_size(self->params); ++i)
214 mem_d(self->params[i]);
215 vec_free(self->params);
216 for (i = 0; i < vec_size(self->output); ++i)
217 pptoken_delete(self->output[i]);
218 vec_free(self->output);
223 static ftepp_t* ftepp_new()
227 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
228 memset(ftepp, 0, sizeof(*ftepp));
230 ftepp->output_on = true;
235 static void ftepp_flush_do(ftepp_t *self)
237 vec_free(self->output_string);
240 static void ftepp_delete(ftepp_t *self)
243 ftepp_flush_do(self);
245 mem_d(self->itemname);
246 if (self->includename)
247 vec_free(self->includename);
248 for (i = 0; i < vec_size(self->macros); ++i)
249 ppmacro_delete(self->macros[i]);
250 vec_free(self->macros);
251 vec_free(self->conditions);
253 lex_close(self->lex);
257 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
259 if (ignore_cond || ftepp->output_on)
264 data = vec_add(ftepp->output_string, len);
265 memcpy(data, str, len);
269 static void ftepp_update_output_condition(ftepp_t *ftepp)
272 ftepp->output_on = true;
273 for (i = 0; i < vec_size(ftepp->conditions); ++i)
274 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
277 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
280 for (i = 0; i < vec_size(ftepp->macros); ++i) {
281 if (!strcmp(name, ftepp->macros[i]->name))
282 return ftepp->macros[i];
287 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
290 for (i = 0; i < vec_size(ftepp->macros); ++i) {
291 if (!strcmp(name, ftepp->macros[i]->name)) {
292 vec_remove(ftepp->macros, i, 1);
298 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
300 return (ftepp->token = lex_do(ftepp->lex));
303 /* Important: this does not skip newlines! */
304 static bool ftepp_skipspace(ftepp_t *ftepp)
306 if (ftepp->token != TOKEN_WHITE)
308 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
309 if (ftepp->token >= TOKEN_EOF) {
310 ftepp_error(ftepp, "unexpected end of preprocessor directive");
316 /* this one skips EOLs as well */
317 static bool ftepp_skipallwhite(ftepp_t *ftepp)
319 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
323 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
324 if (ftepp->token >= TOKEN_EOF) {
325 ftepp_error(ftepp, "unexpected end of preprocessor directive");
332 * The huge macro parsing code...
334 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
338 if (!ftepp_skipspace(ftepp))
340 if (ftepp->token == ')')
342 switch (ftepp->token) {
346 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
349 macro->variadic = true;
352 ftepp_error(ftepp, "unexpected token in parameter list");
356 if (!ftepp_skipspace(ftepp))
358 if (macro->variadic && ftepp->token != ')') {
359 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
362 } while (ftepp->token == ',');
363 if (ftepp->token != ')') {
364 ftepp_error(ftepp, "expected closing paren after macro parameter list");
368 /* skipspace happens in ftepp_define */
372 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
375 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
376 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__"))
377 ftepp->token = TOKEN_VA_ARGS;
378 ptok = pptoken_make(ftepp);
379 vec_push(macro->output, ptok);
382 /* recursive expansion can cause EOFs here */
383 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
384 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
390 static bool ftepp_define(ftepp_t *ftepp)
393 size_t l = ftepp_ctx(ftepp).line;
395 (void)ftepp_next(ftepp);
396 if (!ftepp_skipspace(ftepp))
399 switch (ftepp->token) {
403 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
404 if (macro && ftepp->output_on) {
405 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
407 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
409 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
412 ftepp_error(ftepp, "expected macro name");
416 (void)ftepp_next(ftepp);
418 if (ftepp->token == '(') {
419 macro->has_params = true;
420 if (!ftepp_define_params(ftepp, macro))
424 if (!ftepp_skipspace(ftepp))
427 if (!ftepp_define_body(ftepp, macro))
430 if (ftepp->output_on)
431 vec_push(ftepp->macros, macro);
433 ppmacro_delete(macro);
436 for (; l < ftepp_ctx(ftepp).line; ++l)
437 ftepp_out(ftepp, "\n", true);
442 * When a macro is used we have to handle parameters as well
443 * as special-concatenation via ## or stringification via #
445 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
446 * this kind of parens. Curly braces or [] don't count towards the
453 static void macroparam_clean(macroparam *self)
456 for (i = 0; i < vec_size(self->tokens); ++i)
457 pptoken_delete(self->tokens[i]);
458 vec_free(self->tokens);
461 /* need to leave the last token up */
462 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
464 macroparam *params = NULL;
470 if (!ftepp_skipallwhite(ftepp))
472 while (ftepp->token != ')') {
474 if (!ftepp_skipallwhite(ftepp))
476 while (parens || ftepp->token != ',') {
477 if (ftepp->token == '(')
479 else if (ftepp->token == ')') {
484 ptok = pptoken_make(ftepp);
485 vec_push(mp.tokens, ptok);
486 if (ftepp_next(ftepp) >= TOKEN_EOF) {
487 ftepp_error(ftepp, "unexpected EOF in macro call");
491 vec_push(params, mp);
493 if (ftepp->token == ')')
495 if (ftepp->token != ',') {
496 ftepp_error(ftepp, "expected closing paren or comma in macro call");
499 if (ftepp_next(ftepp) >= TOKEN_EOF) {
500 ftepp_error(ftepp, "unexpected EOF in macro call");
504 /* need to leave that up
505 if (ftepp_next(ftepp) >= TOKEN_EOF) {
506 ftepp_error(ftepp, "unexpected EOF in macro call");
510 *out_params = params;
515 macroparam_clean(&mp);
516 for (i = 0; i < vec_size(params); ++i)
517 macroparam_clean(¶ms[i]);
522 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
525 for (i = 0; i < vec_size(macro->params); ++i) {
526 if (!strcmp(macro->params[i], name)) {
534 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
539 switch (token->token) {
540 case TOKEN_STRINGCONST:
543 /* in preprocessor mode strings already are string,
544 * so we don't get actual newline bytes here.
545 * Still need to escape backslashes and quotes.
548 case '\\': ftepp_out(ftepp, "\\\\", false); break;
549 case '"': ftepp_out(ftepp, "\\\"", false); break;
552 ftepp_out(ftepp, chs, false);
559 ftepp_out(ftepp, " ", false);
562 ftepp_out(ftepp, "\\n", false);
565 ftepp_out(ftepp, token->value, false);
570 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
573 ftepp_out(ftepp, "\"", false);
574 for (i = 0; i < vec_size(param->tokens); ++i)
575 ftepp_stringify_token(ftepp, param->tokens[i]);
576 ftepp_out(ftepp, "\"", false);
579 static void ftepp_recursion_header(ftepp_t *ftepp)
581 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
584 static void ftepp_recursion_footer(ftepp_t *ftepp)
586 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
589 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
593 for (i = 0; i < vec_size(param->tokens); ++i) {
594 out = param->tokens[i];
595 if (out->token == TOKEN_EOL)
596 ftepp_out(ftepp, "\n", false);
598 ftepp_out(ftepp, out->value, false);
602 static bool ftepp_preprocess(ftepp_t *ftepp);
603 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
605 char *old_string = ftepp->output_string;
606 lex_file *old_lexer = ftepp->lex;
607 size_t vararg_start = vec_size(macro->params);
616 if (vararg_start < vec_size(params))
617 varargs = vec_size(params) - vararg_start;
622 if (!vec_size(macro->output))
625 ftepp->output_string = NULL;
626 for (o = 0; o < vec_size(macro->output); ++o) {
627 pptoken *out = macro->output[o];
628 switch (out->token) {
630 if (!macro->variadic) {
631 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
637 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
638 for (++pi; pi < varargs; ++pi) {
639 ftepp_out(ftepp, ", ", false);
640 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
646 if (!macro_params_find(macro, out->value, &pi)) {
647 ftepp_out(ftepp, out->value, false);
650 ftepp_param_out(ftepp, ¶ms[pi]);
653 if (o + 1 < vec_size(macro->output)) {
654 nextok = macro->output[o+1]->token;
656 /* raw concatenation */
660 if ( (nextok == TOKEN_IDENT ||
661 nextok == TOKEN_KEYWORD ||
662 nextok == TOKEN_TYPENAME) &&
663 macro_params_find(macro, macro->output[o+1]->value, &pi))
666 ftepp_stringify(ftepp, ¶ms[pi]);
670 ftepp_out(ftepp, "#", false);
673 ftepp_out(ftepp, "\n", false);
676 ftepp_out(ftepp, out->value, false);
680 vec_push(ftepp->output_string, 0);
681 /* Now run the preprocessor recursively on this string buffer */
683 printf("__________\n%s\n=========\n", ftepp->output_string);
685 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
687 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
691 ftepp->output_string = old_string;
692 inlex->line = ftepp->lex->line;
693 inlex->sline = ftepp->lex->sline;
695 ftepp_recursion_header(ftepp);
696 if (!ftepp_preprocess(ftepp)) {
697 vec_free(ftepp->lex->open_string);
698 old_string = ftepp->output_string;
699 lex_close(ftepp->lex);
703 vec_free(ftepp->lex->open_string);
704 ftepp_recursion_footer(ftepp);
705 old_string = ftepp->output_string;
708 ftepp->lex = old_lexer;
709 ftepp->output_string = old_string;
713 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
716 macroparam *params = NULL;
719 if (!macro->has_params) {
720 if (!ftepp_macro_expand(ftepp, macro, NULL))
727 if (!ftepp_skipallwhite(ftepp))
730 if (ftepp->token != '(') {
731 ftepp_error(ftepp, "expected macro parameters in parenthesis");
736 if (!ftepp_macro_call_params(ftepp, ¶ms))
739 if ( vec_size(params) < vec_size(macro->params) ||
740 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
742 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
743 (macro->variadic ? " at least" : ""),
744 (unsigned int)vec_size(macro->params),
745 (unsigned int)vec_size(params));
750 if (!ftepp_macro_expand(ftepp, macro, params))
755 for (o = 0; o < vec_size(params); ++o)
756 macroparam_clean(¶ms[o]);
762 * #if - the FTEQCC way:
763 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
764 * <numbers> => True if the number is not 0
765 * !<factor> => True if the factor yields false
766 * !!<factor> => ERROR on 2 or more unary nots
767 * <macro> => becomes the macro's FIRST token regardless of parameters
768 * <e> && <e> => True if both expressions are true
769 * <e> || <e> => True if either expression is true
771 * <ident> => False (remember for macros the <macro> rule applies instead)
772 * Unary + and - are weird and wrong in fteqcc so we don't allow them
773 * parenthesis in expressions are allowed
774 * parameter lists on macros are errors
775 * No mathematical calculations are executed
777 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
778 static bool ftepp_if_op(ftepp_t *ftepp)
780 ftepp->lex->flags.noops = false;
782 if (!ftepp_skipspace(ftepp))
784 ftepp->lex->flags.noops = true;
787 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
793 if (!ftepp_skipspace(ftepp))
796 while (ftepp->token == '!') {
799 if (!ftepp_skipspace(ftepp))
803 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
807 if (!ftepp_skipspace(ftepp))
811 switch (ftepp->token) {
815 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
817 if (!ftepp_skipspace(ftepp))
819 if (ftepp->token != '(') {
820 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
824 if (!ftepp_skipspace(ftepp))
826 if (ftepp->token != TOKEN_IDENT &&
827 ftepp->token != TOKEN_TYPENAME &&
828 ftepp->token != TOKEN_KEYWORD)
830 ftepp_error(ftepp, "defined() used on an unexpected token type");
833 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
836 if (!ftepp_skipspace(ftepp))
838 if (ftepp->token != ')') {
839 ftepp_error(ftepp, "expected closing paren");
845 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
846 if (!macro || !vec_size(macro->output)) {
850 /* This does not expand recursively! */
851 switch (macro->output[0]->token) {
853 *value_out = macro->output[0]->constval.i;
854 *out = !!(macro->output[0]->constval.i);
856 case TOKEN_FLOATCONST:
857 *value_out = macro->output[0]->constval.f;
858 *out = !!(macro->output[0]->constval.f);
866 case TOKEN_STRINGCONST:
871 *value_out = ftepp->lex->tok.constval.i;
872 *out = !!(ftepp->lex->tok.constval.i);
874 case TOKEN_FLOATCONST:
875 *value_out = ftepp->lex->tok.constval.f;
876 *out = !!(ftepp->lex->tok.constval.f);
881 if (!ftepp_if_expr(ftepp, out, value_out))
883 if (ftepp->token != ')') {
884 ftepp_error(ftepp, "expected closing paren in #if expression");
890 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
892 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
896 *value_out = -*value_out;
899 *value_out = (*out ? 1 : 0);
905 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
907 if (!ftepp_next(ftepp))
909 return ftepp_if_value(ftepp, out, value_out);
913 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
915 if (!ftepp_if_value(ftepp, out, value_out))
918 if (!ftepp_if_op(ftepp))
921 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
924 /* FTEQCC is all right-associative and no precedence here */
925 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
926 !strcmp(ftepp_tokval(ftepp), "||"))
929 char opc = ftepp_tokval(ftepp)[0];
933 if (!ftepp_next(ftepp))
935 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
943 *value_out = (*out ? 1 : 0);
946 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
947 !strcmp(ftepp_tokval(ftepp), "!=") ||
948 !strcmp(ftepp_tokval(ftepp), ">=") ||
949 !strcmp(ftepp_tokval(ftepp), "<=") ||
950 !strcmp(ftepp_tokval(ftepp), ">") ||
951 !strcmp(ftepp_tokval(ftepp), "<"))
954 const char opc0 = ftepp_tokval(ftepp)[0];
955 const char opc1 = ftepp_tokval(ftepp)[1];
958 if (!ftepp_next(ftepp))
960 if (!ftepp_if_expr(ftepp, &next, &other))
964 *out = (*value_out == other);
965 else if (opc0 == '!')
966 *out = (*value_out != other);
967 else if (opc0 == '>') {
968 if (opc1 == '=') *out = (*value_out >= other);
969 else *out = (*value_out > other);
971 else if (opc0 == '<') {
972 if (opc1 == '=') *out = (*value_out <= other);
973 else *out = (*value_out < other);
975 *value_out = (*out ? 1 : 0);
980 ftepp_error(ftepp, "junk after #if");
985 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
990 memset(cond, 0, sizeof(*cond));
991 (void)ftepp_next(ftepp);
993 if (!ftepp_skipspace(ftepp))
995 if (ftepp->token == TOKEN_EOL) {
996 ftepp_error(ftepp, "expected expression for #if-directive");
1000 if (!ftepp_if_expr(ftepp, &result, &dummy))
1008 * ifdef is rather simple
1010 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1013 memset(cond, 0, sizeof(*cond));
1014 (void)ftepp_next(ftepp);
1015 if (!ftepp_skipspace(ftepp))
1018 switch (ftepp->token) {
1020 case TOKEN_TYPENAME:
1022 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1025 ftepp_error(ftepp, "expected macro name");
1029 (void)ftepp_next(ftepp);
1030 if (!ftepp_skipspace(ftepp))
1032 /* relaxing this condition
1033 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1034 ftepp_error(ftepp, "stray tokens after #ifdef");
1043 * undef is also simple
1045 static bool ftepp_undef(ftepp_t *ftepp)
1047 (void)ftepp_next(ftepp);
1048 if (!ftepp_skipspace(ftepp))
1051 if (ftepp->output_on) {
1052 switch (ftepp->token) {
1054 case TOKEN_TYPENAME:
1056 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1059 ftepp_error(ftepp, "expected macro name");
1064 (void)ftepp_next(ftepp);
1065 if (!ftepp_skipspace(ftepp))
1067 /* relaxing this condition
1068 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1069 ftepp_error(ftepp, "stray tokens after #ifdef");
1076 /* Special unescape-string function which skips a leading quote
1077 * and stops at a quote, not just at \0
1079 static void unescape(const char *str, char *out) {
1081 while (*str && *str != '"') {
1085 case '\\': *out++ = *str; break;
1086 case '"': *out++ = *str; break;
1087 case 'a': *out++ = '\a'; break;
1088 case 'b': *out++ = '\b'; break;
1089 case 'r': *out++ = '\r'; break;
1090 case 'n': *out++ = '\n'; break;
1091 case 't': *out++ = '\t'; break;
1092 case 'f': *out++ = '\f'; break;
1093 case 'v': *out++ = '\v'; break;
1108 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1111 char *filename = NULL;
1112 const char *last_slash;
1118 last_slash = strrchr(pathfile, '/');
1121 len = last_slash - pathfile;
1122 memcpy(vec_add(filename, len), pathfile, len);
1123 vec_push(filename, '/');
1127 memcpy(vec_add(filename, len+1), file, len);
1128 vec_last(filename) = 0;
1130 fp = file_open(filename, "rb");
1139 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1141 char *filename = NULL;
1143 filename = ftepp_include_find_path(file, ftepp->includename);
1145 filename = ftepp_include_find_path(file, ftepp->itemname);
1149 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1150 char *message = NULL;
1152 if (!ftepp_skipspace(ftepp))
1155 /* handle the odd non string constant case so it works like C */
1156 if (ftepp->token != TOKEN_STRINGCONST) {
1158 vec_upload(message, "#warning", 8);
1160 while (ftepp->token != TOKEN_EOL) {
1161 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1164 vec_push(message, '\0');
1165 store = ftepp_warn(ftepp, WARN_CPP, message);
1170 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1171 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1174 static void ftepp_directive_error(ftepp_t *ftepp) {
1175 char *message = NULL;
1177 if (!ftepp_skipspace(ftepp))
1180 /* handle the odd non string constant case so it works like C */
1181 if (ftepp->token != TOKEN_STRINGCONST) {
1182 vec_upload(message, "#error", 6);
1184 while (ftepp->token != TOKEN_EOL) {
1185 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1188 vec_push(message, '\0');
1189 ftepp_error(ftepp, message);
1194 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1195 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1200 * FIXME: do we need/want a -I option?
1201 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1203 static bool ftepp_include(ftepp_t *ftepp)
1205 lex_file *old_lexer = ftepp->lex;
1210 char *old_includename;
1212 (void)ftepp_next(ftepp);
1213 if (!ftepp_skipspace(ftepp))
1216 if (ftepp->token != TOKEN_STRINGCONST) {
1217 ftepp_error(ftepp, "expected filename to include");
1221 ctx = ftepp_ctx(ftepp);
1223 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1225 ftepp_out(ftepp, "\n#pragma file(", false);
1226 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1227 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1229 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1231 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1234 inlex = lex_open(filename);
1236 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1241 old_includename = ftepp->includename;
1242 ftepp->includename = filename;
1243 if (!ftepp_preprocess(ftepp)) {
1244 vec_free(ftepp->includename);
1245 ftepp->includename = old_includename;
1246 lex_close(ftepp->lex);
1247 ftepp->lex = old_lexer;
1250 vec_free(ftepp->includename);
1251 ftepp->includename = old_includename;
1252 lex_close(ftepp->lex);
1253 ftepp->lex = old_lexer;
1255 ftepp_out(ftepp, "\n#pragma file(", false);
1256 ftepp_out(ftepp, ctx.file, false);
1257 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1258 ftepp_out(ftepp, lineno, false);
1261 (void)ftepp_next(ftepp);
1262 if (!ftepp_skipspace(ftepp))
1264 if (ftepp->token != TOKEN_EOL) {
1265 ftepp_error(ftepp, "stray tokens after #include");
1268 (void)ftepp_next(ftepp);
1273 /* Basic structure handlers */
1274 static bool ftepp_else_allowed(ftepp_t *ftepp)
1276 if (!vec_size(ftepp->conditions)) {
1277 ftepp_error(ftepp, "#else without #if");
1280 if (vec_last(ftepp->conditions).had_else) {
1281 ftepp_error(ftepp, "multiple #else for a single #if");
1287 static bool ftepp_hash(ftepp_t *ftepp)
1292 lex_ctx ctx = ftepp_ctx(ftepp);
1294 if (!ftepp_skipspace(ftepp))
1297 switch (ftepp->token) {
1300 case TOKEN_TYPENAME:
1301 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1302 return ftepp_define(ftepp);
1304 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1305 return ftepp_undef(ftepp);
1307 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1308 if (!ftepp_ifdef(ftepp, &cond))
1310 cond.was_on = cond.on;
1311 vec_push(ftepp->conditions, cond);
1312 ftepp->output_on = ftepp->output_on && cond.on;
1315 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1316 if (!ftepp_ifdef(ftepp, &cond))
1319 cond.was_on = cond.on;
1320 vec_push(ftepp->conditions, cond);
1321 ftepp->output_on = ftepp->output_on && cond.on;
1324 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1325 if (!ftepp_else_allowed(ftepp))
1327 if (!ftepp_ifdef(ftepp, &cond))
1329 pc = &vec_last(ftepp->conditions);
1330 pc->on = !pc->was_on && cond.on;
1331 pc->was_on = pc->was_on || pc->on;
1332 ftepp_update_output_condition(ftepp);
1335 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1336 if (!ftepp_else_allowed(ftepp))
1338 if (!ftepp_ifdef(ftepp, &cond))
1341 pc = &vec_last(ftepp->conditions);
1342 pc->on = !pc->was_on && cond.on;
1343 pc->was_on = pc->was_on || pc->on;
1344 ftepp_update_output_condition(ftepp);
1347 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1348 if (!ftepp_else_allowed(ftepp))
1350 if (!ftepp_if(ftepp, &cond))
1352 pc = &vec_last(ftepp->conditions);
1353 pc->on = !pc->was_on && cond.on;
1354 pc->was_on = pc->was_on || pc->on;
1355 ftepp_update_output_condition(ftepp);
1358 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1359 if (!ftepp_if(ftepp, &cond))
1361 cond.was_on = cond.on;
1362 vec_push(ftepp->conditions, cond);
1363 ftepp->output_on = ftepp->output_on && cond.on;
1366 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1367 if (!ftepp_else_allowed(ftepp))
1369 pc = &vec_last(ftepp->conditions);
1370 pc->on = !pc->was_on;
1371 pc->had_else = true;
1373 ftepp_update_output_condition(ftepp);
1376 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1377 if (!vec_size(ftepp->conditions)) {
1378 ftepp_error(ftepp, "#endif without #if");
1381 vec_pop(ftepp->conditions);
1383 ftepp_update_output_condition(ftepp);
1386 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1387 return ftepp_include(ftepp);
1389 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1390 ftepp_out(ftepp, "#", false);
1393 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1394 ftepp_directive_warning(ftepp);
1397 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1398 ftepp_directive_error(ftepp);
1402 if (ftepp->output_on) {
1403 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1410 /* break; never reached */
1412 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1415 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1418 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1421 /* Builtins! Don't forget the builtins! */
1422 case TOKEN_INTCONST:
1423 case TOKEN_FLOATCONST:
1424 ftepp_out(ftepp, "#", false);
1427 if (!ftepp_skipspace(ftepp))
1432 static bool ftepp_preprocess(ftepp_t *ftepp)
1435 bool newline = true;
1438 char *expand = NULL;
1441 ftepp->lex->flags.preprocessing = true;
1442 ftepp->lex->flags.mergelines = false;
1443 ftepp->lex->flags.noops = true;
1448 if (ftepp->token >= TOKEN_EOF)
1454 switch (ftepp->token) {
1457 case TOKEN_TYPENAME:
1458 /* is it a predef? */
1459 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1460 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1461 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1462 expand = ftepp_predefs[i].func(ftepp->lex);
1463 ftepp_out(ftepp, expand, false);
1464 ftepp_next(ftepp); /* skip */
1466 mem_d(expand); /* free memory */
1472 if (ftepp->output_on)
1473 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1478 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1482 if (!ftepp_macro_call(ftepp, macro))
1483 ftepp->token = TOKEN_ERROR;
1487 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1491 ftepp->lex->flags.mergelines = true;
1492 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1493 ftepp_error(ftepp, "error in preprocessor directive");
1494 ftepp->token = TOKEN_ERROR;
1497 if (!ftepp_hash(ftepp))
1498 ftepp->token = TOKEN_ERROR;
1499 ftepp->lex->flags.mergelines = false;
1503 ftepp_out(ftepp, "\n", true);
1507 /* same as default but don't set newline=false */
1508 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1513 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1517 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1519 /* force a 0 at the end but don't count it as added to the output */
1520 vec_push(ftepp->output_string, 0);
1521 vec_shrinkby(ftepp->output_string, 1);
1523 return (ftepp->token == TOKEN_EOF);
1526 /* Like in parser.c - files keep the previous state so we have one global
1527 * preprocessor. Except here we will want to warn about dangling #ifs.
1529 static ftepp_t *ftepp;
1531 static bool ftepp_preprocess_done()
1534 if (vec_size(ftepp->conditions)) {
1535 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1538 lex_close(ftepp->lex);
1540 if (ftepp->itemname) {
1541 mem_d(ftepp->itemname);
1542 ftepp->itemname = NULL;
1547 bool ftepp_preprocess_file(const char *filename)
1549 ftepp->lex = lex_open(filename);
1550 ftepp->itemname = util_strdup(filename);
1552 con_out("failed to open file \"%s\"\n", filename);
1555 if (!ftepp_preprocess(ftepp))
1557 return ftepp_preprocess_done();
1560 bool ftepp_preprocess_string(const char *name, const char *str)
1562 ftepp->lex = lex_open_string(str, strlen(str), name);
1563 ftepp->itemname = util_strdup(name);
1565 con_out("failed to create lexer for string \"%s\"\n", name);
1568 if (!ftepp_preprocess(ftepp))
1570 return ftepp_preprocess_done();
1574 void ftepp_add_macro(const char *name, const char *value) {
1575 char *create = NULL;
1577 /* use saner path for empty macros */
1579 ftepp_add_define("__builtin__", name);
1583 vec_upload(create, "#define ", 8);
1584 vec_upload(create, name, strlen(name));
1585 vec_push (create, ' ');
1586 vec_upload(create, value, strlen(value));
1587 vec_push (create, 0);
1589 ftepp_preprocess_string("__builtin__", create);
1598 ftepp = ftepp_new();
1602 memset(minor, 0, sizeof(minor));
1603 memset(major, 0, sizeof(major));
1605 /* set the right macro based on the selected standard */
1606 ftepp_add_define(NULL, "GMQCC");
1607 if (opts.standard == COMPILER_FTEQCC) {
1608 ftepp_add_define(NULL, "__STD_FTEQCC__");
1617 } else if (opts.standard == COMPILER_GMQCC) {
1618 ftepp_add_define(NULL, "__STD_GMQCC__");
1619 sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR);
1620 sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR);
1621 } else if (opts.standard == COMPILER_QCC) {
1622 ftepp_add_define(NULL, "__STD_QCC__");
1633 ftepp_add_macro("__STD_VERSION_MINOR__", minor);
1634 ftepp_add_macro("__STD_VERSION_MAJOR__", major);
1639 void ftepp_add_define(const char *source, const char *name)
1642 lex_ctx ctx = { "__builtin__", 0 };
1644 macro = ppmacro_new(ctx, name);
1645 vec_push(ftepp->macros, macro);
1648 const char *ftepp_get()
1650 return ftepp->output_string;
1655 ftepp_flush_do(ftepp);
1662 ftepp_delete(ftepp);