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 char *(*func)(lex_file *);
79 * Implement the predef subsystem now. We can do this safely with the
80 * help of lexer contexts.
82 static uint32_t ftepp_predef_countval = 0;
83 static uint32_t ftepp_predef_randval = 0;
86 char *ftepp_predef_date(lex_file *context) {
89 char *value = mem_a(82);
90 /* 82 is enough for strftime but we also have " " in our string */
96 itime = localtime(&rtime);
98 strftime(value, 82, "\"%b %d %Y\"", itime);
104 char *ftepp_predef_line(lex_file *context) {
106 util_asprintf(&value, "%d", (int)context->line);
110 char *ftepp_predef_file(lex_file *context) {
111 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
112 char *value = (char*)mem_a(length);
113 memset (value, 0, length);
114 sprintf(value, "\"%s\"", context->name);
118 /* __COUNTER_LAST__ */
119 char *ftepp_predef_counterlast(lex_file *context) {
121 util_asprintf(&value, "%u", ftepp_predef_countval);
127 char *ftepp_predef_counter(lex_file *context) {
129 ftepp_predef_countval ++;
130 util_asprintf(&value, "%u", ftepp_predef_countval);
136 char *ftepp_predef_random(lex_file *context) {
138 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
139 util_asprintf(&value, "%u", ftepp_predef_randval);
144 /* __RANDOM_LAST__ */
145 char *ftepp_predef_randomlast(lex_file *context) {
147 util_asprintf(&value, "%u", ftepp_predef_randval);
153 static const predef_t ftepp_predefs[] = {
154 { "__LINE__", &ftepp_predef_line },
155 { "__FILE__", &ftepp_predef_file },
156 { "__COUNTER__", &ftepp_predef_counter },
157 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
158 { "__RANDOM__", &ftepp_predef_random },
159 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
160 { "__DATE__", &ftepp_predef_date }
163 #define ftepp_tokval(f) ((f)->lex->tok.value)
164 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
166 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
173 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
177 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
184 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
188 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
194 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
199 static pptoken *pptoken_make(ftepp_t *ftepp)
201 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
202 token->token = ftepp->token;
204 if (token->token == TOKEN_WHITE)
205 token->value = util_strdup(" ");
208 token->value = util_strdup(ftepp_tokval(ftepp));
210 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
214 static void pptoken_delete(pptoken *self)
220 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
222 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
225 memset(macro, 0, sizeof(*macro));
226 macro->name = util_strdup(name);
230 static void ppmacro_delete(ppmacro *self)
233 for (i = 0; i < vec_size(self->params); ++i)
234 mem_d(self->params[i]);
235 vec_free(self->params);
236 for (i = 0; i < vec_size(self->output); ++i)
237 pptoken_delete(self->output[i]);
238 vec_free(self->output);
243 static ftepp_t* ftepp_new()
247 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
248 memset(ftepp, 0, sizeof(*ftepp));
250 ftepp->output_on = true;
255 static void ftepp_flush_do(ftepp_t *self)
257 vec_free(self->output_string);
260 static void ftepp_delete(ftepp_t *self)
263 ftepp_flush_do(self);
265 mem_d(self->itemname);
266 if (self->includename)
267 vec_free(self->includename);
268 for (i = 0; i < vec_size(self->macros); ++i)
269 ppmacro_delete(self->macros[i]);
270 vec_free(self->macros);
271 vec_free(self->conditions);
273 lex_close(self->lex);
277 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
279 if (ignore_cond || ftepp->output_on)
284 data = vec_add(ftepp->output_string, len);
285 memcpy(data, str, len);
289 static void ftepp_update_output_condition(ftepp_t *ftepp)
292 ftepp->output_on = true;
293 for (i = 0; i < vec_size(ftepp->conditions); ++i)
294 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
297 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
300 for (i = 0; i < vec_size(ftepp->macros); ++i) {
301 if (!strcmp(name, ftepp->macros[i]->name))
302 return ftepp->macros[i];
307 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
310 for (i = 0; i < vec_size(ftepp->macros); ++i) {
311 if (!strcmp(name, ftepp->macros[i]->name)) {
312 vec_remove(ftepp->macros, i, 1);
318 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
320 return (ftepp->token = lex_do(ftepp->lex));
323 /* Important: this does not skip newlines! */
324 static bool ftepp_skipspace(ftepp_t *ftepp)
326 if (ftepp->token != TOKEN_WHITE)
328 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
329 if (ftepp->token >= TOKEN_EOF) {
330 ftepp_error(ftepp, "unexpected end of preprocessor directive");
336 /* this one skips EOLs as well */
337 static bool ftepp_skipallwhite(ftepp_t *ftepp)
339 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
343 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
344 if (ftepp->token >= TOKEN_EOF) {
345 ftepp_error(ftepp, "unexpected end of preprocessor directive");
352 * The huge macro parsing code...
354 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
358 if (!ftepp_skipspace(ftepp))
360 if (ftepp->token == ')')
362 switch (ftepp->token) {
366 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
369 macro->variadic = true;
372 ftepp_error(ftepp, "unexpected token in parameter list");
376 if (!ftepp_skipspace(ftepp))
378 if (macro->variadic && ftepp->token != ')') {
379 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
382 } while (ftepp->token == ',');
383 if (ftepp->token != ')') {
384 ftepp_error(ftepp, "expected closing paren after macro parameter list");
388 /* skipspace happens in ftepp_define */
392 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
395 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
396 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__"))
397 ftepp->token = TOKEN_VA_ARGS;
398 ptok = pptoken_make(ftepp);
399 vec_push(macro->output, ptok);
402 /* recursive expansion can cause EOFs here */
403 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
404 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
410 static bool ftepp_define(ftepp_t *ftepp)
413 size_t l = ftepp_ctx(ftepp).line;
415 (void)ftepp_next(ftepp);
416 if (!ftepp_skipspace(ftepp))
419 switch (ftepp->token) {
423 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
424 if (macro && ftepp->output_on) {
425 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
427 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
429 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
432 ftepp_error(ftepp, "expected macro name");
436 (void)ftepp_next(ftepp);
438 if (ftepp->token == '(') {
439 macro->has_params = true;
440 if (!ftepp_define_params(ftepp, macro))
444 if (!ftepp_skipspace(ftepp))
447 if (!ftepp_define_body(ftepp, macro))
450 if (ftepp->output_on)
451 vec_push(ftepp->macros, macro);
453 ppmacro_delete(macro);
456 for (; l < ftepp_ctx(ftepp).line; ++l)
457 ftepp_out(ftepp, "\n", true);
462 * When a macro is used we have to handle parameters as well
463 * as special-concatenation via ## or stringification via #
465 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
466 * this kind of parens. Curly braces or [] don't count towards the
473 static void macroparam_clean(macroparam *self)
476 for (i = 0; i < vec_size(self->tokens); ++i)
477 pptoken_delete(self->tokens[i]);
478 vec_free(self->tokens);
481 /* need to leave the last token up */
482 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
484 macroparam *params = NULL;
490 if (!ftepp_skipallwhite(ftepp))
492 while (ftepp->token != ')') {
494 if (!ftepp_skipallwhite(ftepp))
496 while (parens || ftepp->token != ',') {
497 if (ftepp->token == '(')
499 else if (ftepp->token == ')') {
504 ptok = pptoken_make(ftepp);
505 vec_push(mp.tokens, ptok);
506 if (ftepp_next(ftepp) >= TOKEN_EOF) {
507 ftepp_error(ftepp, "unexpected EOF in macro call");
511 vec_push(params, mp);
513 if (ftepp->token == ')')
515 if (ftepp->token != ',') {
516 ftepp_error(ftepp, "expected closing paren or comma in macro call");
519 if (ftepp_next(ftepp) >= TOKEN_EOF) {
520 ftepp_error(ftepp, "unexpected EOF in macro call");
524 /* need to leave that up
525 if (ftepp_next(ftepp) >= TOKEN_EOF) {
526 ftepp_error(ftepp, "unexpected EOF in macro call");
530 *out_params = params;
535 macroparam_clean(&mp);
536 for (i = 0; i < vec_size(params); ++i)
537 macroparam_clean(¶ms[i]);
542 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
545 for (i = 0; i < vec_size(macro->params); ++i) {
546 if (!strcmp(macro->params[i], name)) {
554 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
559 switch (token->token) {
560 case TOKEN_STRINGCONST:
563 /* in preprocessor mode strings already are string,
564 * so we don't get actual newline bytes here.
565 * Still need to escape backslashes and quotes.
568 case '\\': ftepp_out(ftepp, "\\\\", false); break;
569 case '"': ftepp_out(ftepp, "\\\"", false); break;
572 ftepp_out(ftepp, chs, false);
579 ftepp_out(ftepp, " ", false);
582 ftepp_out(ftepp, "\\n", false);
585 ftepp_out(ftepp, token->value, false);
590 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
593 ftepp_out(ftepp, "\"", false);
594 for (i = 0; i < vec_size(param->tokens); ++i)
595 ftepp_stringify_token(ftepp, param->tokens[i]);
596 ftepp_out(ftepp, "\"", false);
599 static void ftepp_recursion_header(ftepp_t *ftepp)
601 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
604 static void ftepp_recursion_footer(ftepp_t *ftepp)
606 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
609 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
613 for (i = 0; i < vec_size(param->tokens); ++i) {
614 out = param->tokens[i];
615 if (out->token == TOKEN_EOL)
616 ftepp_out(ftepp, "\n", false);
618 ftepp_out(ftepp, out->value, false);
622 static bool ftepp_preprocess(ftepp_t *ftepp);
623 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
625 char *old_string = ftepp->output_string;
626 lex_file *old_lexer = ftepp->lex;
627 size_t vararg_start = vec_size(macro->params);
636 if (vararg_start < vec_size(params))
637 varargs = vec_size(params) - vararg_start;
642 if (!vec_size(macro->output))
645 ftepp->output_string = NULL;
646 for (o = 0; o < vec_size(macro->output); ++o) {
647 pptoken *out = macro->output[o];
648 switch (out->token) {
650 if (!macro->variadic) {
651 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
657 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
658 for (++pi; pi < varargs; ++pi) {
659 ftepp_out(ftepp, ", ", false);
660 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
666 if (!macro_params_find(macro, out->value, &pi)) {
667 ftepp_out(ftepp, out->value, false);
670 ftepp_param_out(ftepp, ¶ms[pi]);
673 if (o + 1 < vec_size(macro->output)) {
674 nextok = macro->output[o+1]->token;
676 /* raw concatenation */
680 if ( (nextok == TOKEN_IDENT ||
681 nextok == TOKEN_KEYWORD ||
682 nextok == TOKEN_TYPENAME) &&
683 macro_params_find(macro, macro->output[o+1]->value, &pi))
686 ftepp_stringify(ftepp, ¶ms[pi]);
690 ftepp_out(ftepp, "#", false);
693 ftepp_out(ftepp, "\n", false);
696 ftepp_out(ftepp, out->value, false);
700 vec_push(ftepp->output_string, 0);
701 /* Now run the preprocessor recursively on this string buffer */
703 printf("__________\n%s\n=========\n", ftepp->output_string);
705 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
707 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
711 ftepp->output_string = old_string;
712 inlex->line = ftepp->lex->line;
713 inlex->sline = ftepp->lex->sline;
715 ftepp_recursion_header(ftepp);
716 if (!ftepp_preprocess(ftepp)) {
717 vec_free(ftepp->lex->open_string);
718 old_string = ftepp->output_string;
719 lex_close(ftepp->lex);
723 vec_free(ftepp->lex->open_string);
724 ftepp_recursion_footer(ftepp);
725 old_string = ftepp->output_string;
728 ftepp->lex = old_lexer;
729 ftepp->output_string = old_string;
733 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
736 macroparam *params = NULL;
739 if (!macro->has_params) {
740 if (!ftepp_macro_expand(ftepp, macro, NULL))
747 if (!ftepp_skipallwhite(ftepp))
750 if (ftepp->token != '(') {
751 ftepp_error(ftepp, "expected macro parameters in parenthesis");
756 if (!ftepp_macro_call_params(ftepp, ¶ms))
759 if ( vec_size(params) < vec_size(macro->params) ||
760 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
762 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
763 (macro->variadic ? " at least" : ""),
764 (unsigned int)vec_size(macro->params),
765 (unsigned int)vec_size(params));
770 if (!ftepp_macro_expand(ftepp, macro, params))
775 for (o = 0; o < vec_size(params); ++o)
776 macroparam_clean(¶ms[o]);
782 * #if - the FTEQCC way:
783 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
784 * <numbers> => True if the number is not 0
785 * !<factor> => True if the factor yields false
786 * !!<factor> => ERROR on 2 or more unary nots
787 * <macro> => becomes the macro's FIRST token regardless of parameters
788 * <e> && <e> => True if both expressions are true
789 * <e> || <e> => True if either expression is true
791 * <ident> => False (remember for macros the <macro> rule applies instead)
792 * Unary + and - are weird and wrong in fteqcc so we don't allow them
793 * parenthesis in expressions are allowed
794 * parameter lists on macros are errors
795 * No mathematical calculations are executed
797 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
798 static bool ftepp_if_op(ftepp_t *ftepp)
800 ftepp->lex->flags.noops = false;
802 if (!ftepp_skipspace(ftepp))
804 ftepp->lex->flags.noops = true;
807 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
812 if (!ftepp_skipspace(ftepp))
815 while (ftepp->token == '!') {
818 if (!ftepp_skipspace(ftepp))
822 switch (ftepp->token) {
826 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
828 if (!ftepp_skipspace(ftepp))
830 if (ftepp->token != '(') {
831 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
835 if (!ftepp_skipspace(ftepp))
837 if (ftepp->token != TOKEN_IDENT &&
838 ftepp->token != TOKEN_TYPENAME &&
839 ftepp->token != TOKEN_KEYWORD)
841 ftepp_error(ftepp, "defined() used on an unexpected token type");
844 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
847 if (!ftepp_skipspace(ftepp))
849 if (ftepp->token != ')') {
850 ftepp_error(ftepp, "expected closing paren");
856 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
857 if (!macro || !vec_size(macro->output)) {
861 /* This does not expand recursively! */
862 switch (macro->output[0]->token) {
864 *value_out = macro->output[0]->constval.i;
865 *out = !!(macro->output[0]->constval.i);
867 case TOKEN_FLOATCONST:
868 *value_out = macro->output[0]->constval.f;
869 *out = !!(macro->output[0]->constval.f);
877 case TOKEN_STRINGCONST:
881 *value_out = ftepp->lex->tok.constval.i;
882 *out = !!(ftepp->lex->tok.constval.i);
884 case TOKEN_FLOATCONST:
885 *value_out = ftepp->lex->tok.constval.f;
886 *out = !!(ftepp->lex->tok.constval.f);
891 if (!ftepp_if_expr(ftepp, out, value_out))
893 if (ftepp->token != ')') {
894 ftepp_error(ftepp, "expected closing paren in #if expression");
900 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
905 *value_out = (*out ? 1 : 0);
911 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
913 if (!ftepp_next(ftepp))
915 return ftepp_if_value(ftepp, out, value_out);
919 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
921 if (!ftepp_if_value(ftepp, out, value_out))
924 if (!ftepp_if_op(ftepp))
927 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
930 /* FTEQCC is all right-associative and no precedence here */
931 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
932 !strcmp(ftepp_tokval(ftepp), "||"))
935 char opc = ftepp_tokval(ftepp)[0];
939 if (!ftepp_next(ftepp))
941 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
949 *value_out = (*out ? 1 : 0);
952 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
953 !strcmp(ftepp_tokval(ftepp), "!=") ||
954 !strcmp(ftepp_tokval(ftepp), ">=") ||
955 !strcmp(ftepp_tokval(ftepp), "<=") ||
956 !strcmp(ftepp_tokval(ftepp), ">") ||
957 !strcmp(ftepp_tokval(ftepp), "<"))
960 const char opc0 = ftepp_tokval(ftepp)[0];
961 const char opc1 = ftepp_tokval(ftepp)[1];
964 if (!ftepp_next(ftepp))
966 if (!ftepp_if_expr(ftepp, &next, &other))
970 *out = (*value_out == other);
971 else if (opc0 == '!')
972 *out = (*value_out != other);
973 else if (opc0 == '>') {
974 if (opc1 == '=') *out = (*value_out >= other);
975 else *out = (*value_out > other);
977 else if (opc0 == '<') {
978 if (opc1 == '=') *out = (*value_out <= other);
979 else *out = (*value_out < other);
981 *value_out = (*out ? 1 : 0);
986 ftepp_error(ftepp, "junk after #if");
991 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
996 memset(cond, 0, sizeof(*cond));
997 (void)ftepp_next(ftepp);
999 if (!ftepp_skipspace(ftepp))
1001 if (ftepp->token == TOKEN_EOL) {
1002 ftepp_error(ftepp, "expected expression for #if-directive");
1006 if (!ftepp_if_expr(ftepp, &result, &dummy))
1014 * ifdef is rather simple
1016 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1019 memset(cond, 0, sizeof(*cond));
1020 (void)ftepp_next(ftepp);
1021 if (!ftepp_skipspace(ftepp))
1024 switch (ftepp->token) {
1026 case TOKEN_TYPENAME:
1028 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1031 ftepp_error(ftepp, "expected macro name");
1035 (void)ftepp_next(ftepp);
1036 if (!ftepp_skipspace(ftepp))
1038 /* relaxing this condition
1039 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1040 ftepp_error(ftepp, "stray tokens after #ifdef");
1049 * undef is also simple
1051 static bool ftepp_undef(ftepp_t *ftepp)
1053 (void)ftepp_next(ftepp);
1054 if (!ftepp_skipspace(ftepp))
1057 if (ftepp->output_on) {
1058 switch (ftepp->token) {
1060 case TOKEN_TYPENAME:
1062 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1065 ftepp_error(ftepp, "expected macro name");
1070 (void)ftepp_next(ftepp);
1071 if (!ftepp_skipspace(ftepp))
1073 /* relaxing this condition
1074 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1075 ftepp_error(ftepp, "stray tokens after #ifdef");
1082 /* Special unescape-string function which skips a leading quote
1083 * and stops at a quote, not just at \0
1085 static void unescape(const char *str, char *out) {
1087 while (*str && *str != '"') {
1091 case '\\': *out++ = *str; break;
1092 case '"': *out++ = *str; break;
1093 case 'a': *out++ = '\a'; break;
1094 case 'b': *out++ = '\b'; break;
1095 case 'r': *out++ = '\r'; break;
1096 case 'n': *out++ = '\n'; break;
1097 case 't': *out++ = '\t'; break;
1098 case 'f': *out++ = '\f'; break;
1099 case 'v': *out++ = '\v'; break;
1114 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1117 char *filename = NULL;
1118 const char *last_slash;
1124 last_slash = strrchr(pathfile, '/');
1127 len = last_slash - pathfile;
1128 memcpy(vec_add(filename, len), pathfile, len);
1129 vec_push(filename, '/');
1133 memcpy(vec_add(filename, len+1), file, len);
1134 vec_last(filename) = 0;
1136 fp = file_open(filename, "rb");
1145 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1147 char *filename = NULL;
1149 filename = ftepp_include_find_path(file, ftepp->includename);
1151 filename = ftepp_include_find_path(file, ftepp->itemname);
1155 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1156 char *message = NULL;
1158 if (!ftepp_skipspace(ftepp))
1161 /* handle the odd non string constant case so it works like C */
1162 if (ftepp->token != TOKEN_STRINGCONST) {
1164 vec_upload(message, "#warning", 8);
1166 while (ftepp->token != TOKEN_EOL) {
1167 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1170 vec_push(message, '\0');
1171 store = ftepp_warn(ftepp, WARN_CPP, message);
1176 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1177 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1180 static void ftepp_directive_error(ftepp_t *ftepp) {
1181 char *message = NULL;
1183 if (!ftepp_skipspace(ftepp))
1186 /* handle the odd non string constant case so it works like C */
1187 if (ftepp->token != TOKEN_STRINGCONST) {
1188 vec_upload(message, "#error", 6);
1190 while (ftepp->token != TOKEN_EOL) {
1191 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1194 vec_push(message, '\0');
1195 ftepp_error(ftepp, message);
1200 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1201 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1206 * FIXME: do we need/want a -I option?
1207 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1209 static bool ftepp_include(ftepp_t *ftepp)
1211 lex_file *old_lexer = ftepp->lex;
1216 char *old_includename;
1218 (void)ftepp_next(ftepp);
1219 if (!ftepp_skipspace(ftepp))
1222 if (ftepp->token != TOKEN_STRINGCONST) {
1223 ftepp_error(ftepp, "expected filename to include");
1227 ctx = ftepp_ctx(ftepp);
1229 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1231 ftepp_out(ftepp, "\n#pragma file(", false);
1232 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1233 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1235 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1237 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1240 inlex = lex_open(filename);
1242 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1247 old_includename = ftepp->includename;
1248 ftepp->includename = filename;
1249 if (!ftepp_preprocess(ftepp)) {
1250 vec_free(ftepp->includename);
1251 ftepp->includename = old_includename;
1252 lex_close(ftepp->lex);
1253 ftepp->lex = old_lexer;
1256 vec_free(ftepp->includename);
1257 ftepp->includename = old_includename;
1258 lex_close(ftepp->lex);
1259 ftepp->lex = old_lexer;
1261 ftepp_out(ftepp, "\n#pragma file(", false);
1262 ftepp_out(ftepp, ctx.file, false);
1263 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1264 ftepp_out(ftepp, lineno, false);
1267 (void)ftepp_next(ftepp);
1268 if (!ftepp_skipspace(ftepp))
1270 if (ftepp->token != TOKEN_EOL) {
1271 ftepp_error(ftepp, "stray tokens after #include");
1274 (void)ftepp_next(ftepp);
1279 /* Basic structure handlers */
1280 static bool ftepp_else_allowed(ftepp_t *ftepp)
1282 if (!vec_size(ftepp->conditions)) {
1283 ftepp_error(ftepp, "#else without #if");
1286 if (vec_last(ftepp->conditions).had_else) {
1287 ftepp_error(ftepp, "multiple #else for a single #if");
1293 static bool ftepp_hash(ftepp_t *ftepp)
1298 lex_ctx ctx = ftepp_ctx(ftepp);
1300 if (!ftepp_skipspace(ftepp))
1303 switch (ftepp->token) {
1306 case TOKEN_TYPENAME:
1307 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1308 return ftepp_define(ftepp);
1310 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1311 return ftepp_undef(ftepp);
1313 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1314 if (!ftepp_ifdef(ftepp, &cond))
1316 cond.was_on = cond.on;
1317 vec_push(ftepp->conditions, cond);
1318 ftepp->output_on = ftepp->output_on && cond.on;
1321 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1322 if (!ftepp_ifdef(ftepp, &cond))
1325 cond.was_on = cond.on;
1326 vec_push(ftepp->conditions, cond);
1327 ftepp->output_on = ftepp->output_on && cond.on;
1330 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1331 if (!ftepp_else_allowed(ftepp))
1333 if (!ftepp_ifdef(ftepp, &cond))
1335 pc = &vec_last(ftepp->conditions);
1336 pc->on = !pc->was_on && cond.on;
1337 pc->was_on = pc->was_on || pc->on;
1338 ftepp_update_output_condition(ftepp);
1341 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1342 if (!ftepp_else_allowed(ftepp))
1344 if (!ftepp_ifdef(ftepp, &cond))
1347 pc = &vec_last(ftepp->conditions);
1348 pc->on = !pc->was_on && cond.on;
1349 pc->was_on = pc->was_on || pc->on;
1350 ftepp_update_output_condition(ftepp);
1353 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1354 if (!ftepp_else_allowed(ftepp))
1356 if (!ftepp_if(ftepp, &cond))
1358 pc = &vec_last(ftepp->conditions);
1359 pc->on = !pc->was_on && cond.on;
1360 pc->was_on = pc->was_on || pc->on;
1361 ftepp_update_output_condition(ftepp);
1364 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1365 if (!ftepp_if(ftepp, &cond))
1367 cond.was_on = cond.on;
1368 vec_push(ftepp->conditions, cond);
1369 ftepp->output_on = ftepp->output_on && cond.on;
1372 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1373 if (!ftepp_else_allowed(ftepp))
1375 pc = &vec_last(ftepp->conditions);
1376 pc->on = !pc->was_on;
1377 pc->had_else = true;
1379 ftepp_update_output_condition(ftepp);
1382 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1383 if (!vec_size(ftepp->conditions)) {
1384 ftepp_error(ftepp, "#endif without #if");
1387 vec_pop(ftepp->conditions);
1389 ftepp_update_output_condition(ftepp);
1392 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1393 return ftepp_include(ftepp);
1395 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1396 ftepp_out(ftepp, "#", false);
1399 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1400 ftepp_directive_warning(ftepp);
1403 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1404 ftepp_directive_error(ftepp);
1408 if (ftepp->output_on) {
1409 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1416 /* break; never reached */
1418 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1421 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1424 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1427 /* Builtins! Don't forget the builtins! */
1428 case TOKEN_INTCONST:
1429 case TOKEN_FLOATCONST:
1430 ftepp_out(ftepp, "#", false);
1433 if (!ftepp_skipspace(ftepp))
1438 static bool ftepp_preprocess(ftepp_t *ftepp)
1441 bool newline = true;
1444 char *expand = NULL;
1447 ftepp->lex->flags.preprocessing = true;
1448 ftepp->lex->flags.mergelines = false;
1449 ftepp->lex->flags.noops = true;
1454 if (ftepp->token >= TOKEN_EOF)
1460 switch (ftepp->token) {
1463 case TOKEN_TYPENAME:
1464 /* is it a predef? */
1465 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1466 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1467 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1468 expand = ftepp_predefs[i].func(ftepp->lex);
1469 ftepp_out(ftepp, expand, false);
1470 ftepp_next(ftepp); /* skip */
1472 mem_d(expand); /* free memory */
1478 if (ftepp->output_on)
1479 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1484 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1488 if (!ftepp_macro_call(ftepp, macro))
1489 ftepp->token = TOKEN_ERROR;
1493 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1497 ftepp->lex->flags.mergelines = true;
1498 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1499 ftepp_error(ftepp, "error in preprocessor directive");
1500 ftepp->token = TOKEN_ERROR;
1503 if (!ftepp_hash(ftepp))
1504 ftepp->token = TOKEN_ERROR;
1505 ftepp->lex->flags.mergelines = false;
1509 ftepp_out(ftepp, "\n", true);
1513 /* same as default but don't set newline=false */
1514 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1519 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1523 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1525 /* force a 0 at the end but don't count it as added to the output */
1526 vec_push(ftepp->output_string, 0);
1527 vec_shrinkby(ftepp->output_string, 1);
1529 return (ftepp->token == TOKEN_EOF);
1532 /* Like in parser.c - files keep the previous state so we have one global
1533 * preprocessor. Except here we will want to warn about dangling #ifs.
1535 static ftepp_t *ftepp;
1537 static bool ftepp_preprocess_done()
1540 if (vec_size(ftepp->conditions)) {
1541 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1544 lex_close(ftepp->lex);
1546 if (ftepp->itemname) {
1547 mem_d(ftepp->itemname);
1548 ftepp->itemname = NULL;
1553 bool ftepp_preprocess_file(const char *filename)
1555 ftepp->lex = lex_open(filename);
1556 ftepp->itemname = util_strdup(filename);
1558 con_out("failed to open file \"%s\"\n", filename);
1561 if (!ftepp_preprocess(ftepp))
1563 return ftepp_preprocess_done();
1566 bool ftepp_preprocess_string(const char *name, const char *str)
1568 ftepp->lex = lex_open_string(str, strlen(str), name);
1569 ftepp->itemname = util_strdup(name);
1571 con_out("failed to create lexer for string \"%s\"\n", name);
1574 if (!ftepp_preprocess(ftepp))
1576 return ftepp_preprocess_done();
1580 void ftepp_add_macro(const char *name, const char *value) {
1581 char *create = NULL;
1583 /* use saner path for empty macros */
1585 ftepp_add_define("__builtin__", name);
1589 vec_upload(create, "#define ", 8);
1590 vec_upload(create, name, strlen(name));
1591 vec_push (create, ' ');
1592 vec_upload(create, value, strlen(value));
1593 vec_push (create, 0);
1595 ftepp_preprocess_string("__builtin__", create);
1604 ftepp = ftepp_new();
1608 memset(minor, 0, sizeof(minor));
1609 memset(major, 0, sizeof(major));
1611 /* set the right macro based on the selected standard */
1612 ftepp_add_define(NULL, "GMQCC");
1613 if (opts.standard == COMPILER_FTEQCC) {
1614 ftepp_add_define(NULL, "__STD_FTEQCC__");
1623 } else if (opts.standard == COMPILER_GMQCC) {
1624 ftepp_add_define(NULL, "__STD_GMQCC__");
1625 sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR);
1626 sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR);
1627 } else if (opts.standard == COMPILER_QCC) {
1628 ftepp_add_define(NULL, "__STD_QCC__");
1639 ftepp_add_macro("__STD_VERSION_MINOR__", minor);
1640 ftepp_add_macro("__STD_VERSION_MAJOR__", major);
1645 void ftepp_add_define(const char *source, const char *name)
1648 lex_ctx ctx = { "__builtin__", 0 };
1650 macro = ppmacro_new(ctx, name);
1651 vec_push(ftepp->macros, macro);
1654 const char *ftepp_get()
1656 return ftepp->output_string;
1661 ftepp_flush_do(ftepp);
1668 ftepp_delete(ftepp);