5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 /* a copy from the lexer */
49 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
62 ppcondition *conditions;
71 #define ftepp_tokval(f) ((f)->lex->tok.value)
72 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
74 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
81 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
85 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
92 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
96 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
99 int lvl = LVL_WARNING;
101 if (!OPTS_WARN(warntype))
110 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, lvl, "error", fmt, ap);
115 static pptoken *pptoken_make(ftepp_t *ftepp)
117 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
118 token->token = ftepp->token;
120 if (token->token == TOKEN_WHITE)
121 token->value = util_strdup(" ");
124 token->value = util_strdup(ftepp_tokval(ftepp));
126 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
130 static void pptoken_delete(pptoken *self)
136 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
138 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
141 memset(macro, 0, sizeof(*macro));
142 macro->name = util_strdup(name);
146 static void ppmacro_delete(ppmacro *self)
149 for (i = 0; i < vec_size(self->params); ++i)
150 mem_d(self->params[i]);
151 vec_free(self->params);
152 for (i = 0; i < vec_size(self->output); ++i)
153 pptoken_delete(self->output[i]);
154 vec_free(self->output);
159 static ftepp_t* ftepp_new()
163 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
164 memset(ftepp, 0, sizeof(*ftepp));
166 ftepp->output_on = true;
171 static void ftepp_delete(ftepp_t *self)
175 mem_d(self->itemname);
176 if (self->includename)
177 vec_free(self->includename);
178 for (i = 0; i < vec_size(self->macros); ++i)
179 ppmacro_delete(self->macros[i]);
180 vec_free(self->macros);
181 vec_free(self->conditions);
183 lex_close(self->lex);
187 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
189 if (ignore_cond || ftepp->output_on)
194 data = vec_add(ftepp->output_string, len);
195 memcpy(data, str, len);
199 static void ftepp_update_output_condition(ftepp_t *ftepp)
202 ftepp->output_on = true;
203 for (i = 0; i < vec_size(ftepp->conditions); ++i)
204 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
207 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
210 for (i = 0; i < vec_size(ftepp->macros); ++i) {
211 if (!strcmp(name, ftepp->macros[i]->name))
212 return ftepp->macros[i];
217 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
220 for (i = 0; i < vec_size(ftepp->macros); ++i) {
221 if (!strcmp(name, ftepp->macros[i]->name)) {
222 vec_remove(ftepp->macros, i, 1);
228 static inline int ftepp_next(ftepp_t *ftepp)
230 return (ftepp->token = lex_do(ftepp->lex));
233 /* Important: this does not skip newlines! */
234 static bool ftepp_skipspace(ftepp_t *ftepp)
236 if (ftepp->token != TOKEN_WHITE)
238 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
239 if (ftepp->token >= TOKEN_EOF) {
240 ftepp_error(ftepp, "unexpected end of preprocessor directive");
246 /* this one skips EOLs as well */
247 static bool ftepp_skipallwhite(ftepp_t *ftepp)
249 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
253 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
254 if (ftepp->token >= TOKEN_EOF) {
255 ftepp_error(ftepp, "unexpected end of preprocessor directive");
262 * The huge macro parsing code...
264 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
268 if (!ftepp_skipspace(ftepp))
270 if (ftepp->token == ')')
272 switch (ftepp->token) {
278 ftepp_error(ftepp, "unexpected token in parameter list");
281 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
283 if (!ftepp_skipspace(ftepp))
285 } while (ftepp->token == ',');
286 if (ftepp->token != ')') {
287 ftepp_error(ftepp, "expected closing paren after macro parameter list");
291 /* skipspace happens in ftepp_define */
295 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
298 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
299 ptok = pptoken_make(ftepp);
300 vec_push(macro->output, ptok);
303 /* recursive expansion can cause EOFs here */
304 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
305 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
311 static bool ftepp_define(ftepp_t *ftepp)
314 size_t l = ftepp_ctx(ftepp).line;
316 (void)ftepp_next(ftepp);
317 if (!ftepp_skipspace(ftepp))
320 switch (ftepp->token) {
324 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
325 if (macro && ftepp->output_on) {
326 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
328 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
330 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
333 ftepp_error(ftepp, "expected macro name");
337 (void)ftepp_next(ftepp);
339 if (ftepp->token == '(') {
340 macro->has_params = true;
341 if (!ftepp_define_params(ftepp, macro))
345 if (!ftepp_skipspace(ftepp))
348 if (!ftepp_define_body(ftepp, macro))
351 if (ftepp->output_on)
352 vec_push(ftepp->macros, macro);
354 ppmacro_delete(macro);
357 for (; l < ftepp_ctx(ftepp).line; ++l)
358 ftepp_out(ftepp, "\n", true);
363 * When a macro is used we have to handle parameters as well
364 * as special-concatenation via ## or stringification via #
366 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
367 * this kind of parens. Curly braces or [] don't count towards the
374 static void macroparam_clean(macroparam *self)
377 for (i = 0; i < vec_size(self->tokens); ++i)
378 pptoken_delete(self->tokens[i]);
379 vec_free(self->tokens);
382 /* need to leave the last token up */
383 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
385 macroparam *params = NULL;
391 if (!ftepp_skipallwhite(ftepp))
393 while (ftepp->token != ')') {
395 if (!ftepp_skipallwhite(ftepp))
397 while (parens || ftepp->token != ',') {
398 if (ftepp->token == '(')
400 else if (ftepp->token == ')') {
405 ptok = pptoken_make(ftepp);
406 vec_push(mp.tokens, ptok);
407 if (ftepp_next(ftepp) >= TOKEN_EOF) {
408 ftepp_error(ftepp, "unexpected EOF in macro call");
412 vec_push(params, mp);
414 if (ftepp->token == ')')
416 if (ftepp->token != ',') {
417 ftepp_error(ftepp, "expected closing paren or comma in macro call");
420 if (ftepp_next(ftepp) >= TOKEN_EOF) {
421 ftepp_error(ftepp, "unexpected EOF in macro call");
425 /* need to leave that up
426 if (ftepp_next(ftepp) >= TOKEN_EOF) {
427 ftepp_error(ftepp, "unexpected EOF in macro call");
431 *out_params = params;
436 macroparam_clean(&mp);
437 for (i = 0; i < vec_size(params); ++i)
438 macroparam_clean(¶ms[i]);
443 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
446 for (i = 0; i < vec_size(macro->params); ++i) {
447 if (!strcmp(macro->params[i], name)) {
455 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
460 switch (token->token) {
461 case TOKEN_STRINGCONST:
464 /* in preprocessor mode strings already are string,
465 * so we don't get actual newline bytes here.
466 * Still need to escape backslashes and quotes.
469 case '\\': ftepp_out(ftepp, "\\\\", false); break;
470 case '"': ftepp_out(ftepp, "\\\"", false); break;
473 ftepp_out(ftepp, chs, false);
480 ftepp_out(ftepp, " ", false);
483 ftepp_out(ftepp, "\\n", false);
486 ftepp_out(ftepp, token->value, false);
491 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
494 ftepp_out(ftepp, "\"", false);
495 for (i = 0; i < vec_size(param->tokens); ++i)
496 ftepp_stringify_token(ftepp, param->tokens[i]);
497 ftepp_out(ftepp, "\"", false);
500 static void ftepp_recursion_header(ftepp_t *ftepp)
502 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
505 static void ftepp_recursion_footer(ftepp_t *ftepp)
507 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
510 static bool ftepp_preprocess(ftepp_t *ftepp);
511 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
513 char *old_string = ftepp->output_string;
514 lex_file *old_lexer = ftepp->lex;
523 if (!vec_size(macro->output))
526 ftepp->output_string = NULL;
527 for (o = 0; o < vec_size(macro->output); ++o) {
528 pptoken *out = macro->output[o];
529 switch (out->token) {
533 if (!macro_params_find(macro, out->value, &pi)) {
534 ftepp_out(ftepp, out->value, false);
537 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
538 out = params[pi].tokens[pv];
539 if (out->token == TOKEN_EOL)
540 ftepp_out(ftepp, "\n", false);
542 ftepp_out(ftepp, out->value, false);
547 if (o + 1 < vec_size(macro->output)) {
548 nextok = macro->output[o+1]->token;
550 /* raw concatenation */
554 if ( (nextok == TOKEN_IDENT ||
555 nextok == TOKEN_KEYWORD ||
556 nextok == TOKEN_TYPENAME) &&
557 macro_params_find(macro, macro->output[o+1]->value, &pi))
560 ftepp_stringify(ftepp, ¶ms[pi]);
564 ftepp_out(ftepp, "#", false);
567 ftepp_out(ftepp, "\n", false);
570 ftepp_out(ftepp, out->value, false);
574 vec_push(ftepp->output_string, 0);
575 /* Now run the preprocessor recursively on this string buffer */
577 printf("__________\n%s\n=========\n", ftepp->output_string);
579 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
581 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
585 ftepp->output_string = old_string;
587 ftepp_recursion_header(ftepp);
588 if (!ftepp_preprocess(ftepp)) {
589 lex_close(ftepp->lex);
593 ftepp_recursion_footer(ftepp);
594 old_string = ftepp->output_string;
597 ftepp->lex = old_lexer;
598 ftepp->output_string = old_string;
602 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
605 macroparam *params = NULL;
608 if (!macro->has_params) {
609 if (!ftepp_macro_expand(ftepp, macro, NULL))
616 if (!ftepp_skipallwhite(ftepp))
619 if (ftepp->token != '(') {
620 ftepp_error(ftepp, "expected macro parameters in parenthesis");
625 if (!ftepp_macro_call_params(ftepp, ¶ms))
628 if (vec_size(params) != vec_size(macro->params)) {
629 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
630 (unsigned int)vec_size(macro->params),
631 (unsigned int)vec_size(params));
636 if (!ftepp_macro_expand(ftepp, macro, params))
641 for (o = 0; o < vec_size(params); ++o)
642 macroparam_clean(¶ms[o]);
648 * #if - the FTEQCC way:
649 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
650 * <numbers> => True if the number is not 0
651 * !<factor> => True if the factor yields false
652 * !!<factor> => ERROR on 2 or more unary nots
653 * <macro> => becomes the macro's FIRST token regardless of parameters
654 * <e> && <e> => True if both expressions are true
655 * <e> || <e> => True if either expression is true
657 * <ident> => False (remember for macros the <macro> rule applies instead)
658 * Unary + and - are weird and wrong in fteqcc so we don't allow them
659 * parenthesis in expressions are allowed
660 * parameter lists on macros are errors
661 * No mathematical calculations are executed
663 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
664 static bool ftepp_if_op(ftepp_t *ftepp)
666 ftepp->lex->flags.noops = false;
668 if (!ftepp_skipspace(ftepp))
670 ftepp->lex->flags.noops = true;
673 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
678 if (!ftepp_skipspace(ftepp))
681 while (ftepp->token == '!') {
684 if (!ftepp_skipspace(ftepp))
688 switch (ftepp->token) {
692 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
694 if (!ftepp_skipspace(ftepp))
696 if (ftepp->token != '(') {
697 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
701 if (!ftepp_skipspace(ftepp))
703 if (ftepp->token != TOKEN_IDENT &&
704 ftepp->token != TOKEN_TYPENAME &&
705 ftepp->token != TOKEN_KEYWORD)
707 ftepp_error(ftepp, "defined() used on an unexpected token type");
710 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
713 if (!ftepp_skipspace(ftepp))
715 if (ftepp->token != ')') {
716 ftepp_error(ftepp, "expected closing paren");
722 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
723 if (!macro || !vec_size(macro->output)) {
727 /* This does not expand recursively! */
728 switch (macro->output[0]->token) {
730 *value_out = macro->output[0]->constval.i;
731 *out = !!(macro->output[0]->constval.i);
733 case TOKEN_FLOATCONST:
734 *value_out = macro->output[0]->constval.f;
735 *out = !!(macro->output[0]->constval.f);
743 case TOKEN_STRINGCONST:
747 *value_out = ftepp->lex->tok.constval.i;
748 *out = !!(ftepp->lex->tok.constval.i);
750 case TOKEN_FLOATCONST:
751 *value_out = ftepp->lex->tok.constval.f;
752 *out = !!(ftepp->lex->tok.constval.f);
757 if (!ftepp_if_expr(ftepp, out, value_out))
759 if (ftepp->token != ')') {
760 ftepp_error(ftepp, "expected closing paren in #if expression");
766 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
771 *value_out = (*out ? 1 : 0);
777 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
779 if (!ftepp_next(ftepp))
781 return ftepp_if_value(ftepp, out, value_out);
785 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
787 if (!ftepp_if_value(ftepp, out, value_out))
790 if (!ftepp_if_op(ftepp))
793 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
796 /* FTEQCC is all right-associative and no precedence here */
797 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
798 !strcmp(ftepp_tokval(ftepp), "||"))
801 char opc = ftepp_tokval(ftepp)[0];
805 if (!ftepp_next(ftepp))
807 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
815 *value_out = (*out ? 1 : 0);
818 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
819 !strcmp(ftepp_tokval(ftepp), "!=") ||
820 !strcmp(ftepp_tokval(ftepp), ">=") ||
821 !strcmp(ftepp_tokval(ftepp), "<=") ||
822 !strcmp(ftepp_tokval(ftepp), ">") ||
823 !strcmp(ftepp_tokval(ftepp), "<"))
826 const char opc0 = ftepp_tokval(ftepp)[0];
827 const char opc1 = ftepp_tokval(ftepp)[1];
830 if (!ftepp_next(ftepp))
832 if (!ftepp_if_expr(ftepp, &next, &other))
836 *out = (*value_out == other);
837 else if (opc0 == '!')
838 *out = (*value_out != other);
839 else if (opc0 == '>') {
840 if (opc1 == '=') *out = (*value_out >= other);
841 else *out = (*value_out > other);
843 else if (opc0 == '<') {
844 if (opc1 == '=') *out = (*value_out <= other);
845 else *out = (*value_out < other);
847 *value_out = (*out ? 1 : 0);
852 ftepp_error(ftepp, "junk after #if");
857 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
862 memset(cond, 0, sizeof(*cond));
863 (void)ftepp_next(ftepp);
865 if (!ftepp_skipspace(ftepp))
867 if (ftepp->token == TOKEN_EOL) {
868 ftepp_error(ftepp, "expected expression for #if-directive");
872 if (!ftepp_if_expr(ftepp, &result, &dummy))
880 * ifdef is rather simple
882 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
885 memset(cond, 0, sizeof(*cond));
886 (void)ftepp_next(ftepp);
887 if (!ftepp_skipspace(ftepp))
890 switch (ftepp->token) {
894 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
897 ftepp_error(ftepp, "expected macro name");
901 (void)ftepp_next(ftepp);
902 if (!ftepp_skipspace(ftepp))
904 /* relaxing this condition
905 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
906 ftepp_error(ftepp, "stray tokens after #ifdef");
915 * undef is also simple
917 static bool ftepp_undef(ftepp_t *ftepp)
919 (void)ftepp_next(ftepp);
920 if (!ftepp_skipspace(ftepp))
923 if (ftepp->output_on) {
924 switch (ftepp->token) {
928 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
931 ftepp_error(ftepp, "expected macro name");
936 (void)ftepp_next(ftepp);
937 if (!ftepp_skipspace(ftepp))
939 /* relaxing this condition
940 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
941 ftepp_error(ftepp, "stray tokens after #ifdef");
948 /* Special unescape-string function which skips a leading quote
949 * and stops at a quote, not just at \0
951 static void unescape(const char *str, char *out) {
953 while (*str && *str != '"') {
957 case '\\': *out++ = *str; break;
958 case '"': *out++ = *str; break;
959 case 'a': *out++ = '\a'; break;
960 case 'b': *out++ = '\b'; break;
961 case 'r': *out++ = '\r'; break;
962 case 'n': *out++ = '\n'; break;
963 case 't': *out++ = '\t'; break;
964 case 'f': *out++ = '\f'; break;
965 case 'v': *out++ = '\v'; break;
980 static char *ftepp_include_find_path(const char *file, const char *pathfile)
983 char *filename = NULL;
984 const char *last_slash;
990 last_slash = strrchr(pathfile, '/');
993 len = last_slash - pathfile;
994 memcpy(vec_add(filename, len), pathfile, len);
995 vec_push(filename, '/');
998 len = strlen(pathfile);
999 memcpy(vec_add(filename, len), pathfile, len);
1000 if (vec_last(filename) != '/')
1001 vec_push(filename, '/');
1005 memcpy(vec_add(filename, len+1), file, len);
1006 vec_last(filename) = 0;
1008 fp = util_fopen(filename, "rb");
1017 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1019 char *filename = NULL;
1021 filename = ftepp_include_find_path(file, ftepp->includename);
1023 filename = ftepp_include_find_path(file, ftepp->itemname);
1029 * FIXME: do we need/want a -I option?
1030 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1032 static bool ftepp_include(ftepp_t *ftepp)
1034 lex_file *old_lexer = ftepp->lex;
1039 char *old_includename;
1041 (void)ftepp_next(ftepp);
1042 if (!ftepp_skipspace(ftepp))
1045 if (ftepp->token != TOKEN_STRINGCONST) {
1046 ftepp_error(ftepp, "expected filename to include");
1050 ctx = ftepp_ctx(ftepp);
1052 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1054 ftepp_out(ftepp, "\n#pragma file(", false);
1055 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1056 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1058 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1060 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1063 inlex = lex_open(filename);
1065 ftepp_error(ftepp, "failed to open include file `%s`", filename);
1070 old_includename = ftepp->includename;
1071 ftepp->includename = filename;
1072 if (!ftepp_preprocess(ftepp)) {
1073 vec_free(ftepp->includename);
1074 ftepp->includename = old_includename;
1075 lex_close(ftepp->lex);
1076 ftepp->lex = old_lexer;
1079 vec_free(ftepp->includename);
1080 ftepp->includename = old_includename;
1081 lex_close(ftepp->lex);
1082 ftepp->lex = old_lexer;
1084 ftepp_out(ftepp, "\n#pragma file(", false);
1085 ftepp_out(ftepp, ctx.file, false);
1086 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1087 ftepp_out(ftepp, lineno, false);
1090 (void)ftepp_next(ftepp);
1091 if (!ftepp_skipspace(ftepp))
1093 if (ftepp->token != TOKEN_EOL) {
1094 ftepp_error(ftepp, "stray tokens after #include");
1097 (void)ftepp_next(ftepp);
1102 /* Basic structure handlers */
1103 static bool ftepp_else_allowed(ftepp_t *ftepp)
1105 if (!vec_size(ftepp->conditions)) {
1106 ftepp_error(ftepp, "#else without #if");
1109 if (vec_last(ftepp->conditions).had_else) {
1110 ftepp_error(ftepp, "multiple #else for a single #if");
1116 static bool ftepp_hash(ftepp_t *ftepp)
1121 lex_ctx ctx = ftepp_ctx(ftepp);
1123 if (!ftepp_skipspace(ftepp))
1126 switch (ftepp->token) {
1129 case TOKEN_TYPENAME:
1130 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1131 return ftepp_define(ftepp);
1133 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1134 return ftepp_undef(ftepp);
1136 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1137 if (!ftepp_ifdef(ftepp, &cond))
1139 cond.was_on = cond.on;
1140 vec_push(ftepp->conditions, cond);
1141 ftepp->output_on = ftepp->output_on && cond.on;
1144 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1145 if (!ftepp_ifdef(ftepp, &cond))
1148 cond.was_on = cond.on;
1149 vec_push(ftepp->conditions, cond);
1150 ftepp->output_on = ftepp->output_on && cond.on;
1153 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1154 if (!ftepp_else_allowed(ftepp))
1156 if (!ftepp_ifdef(ftepp, &cond))
1158 pc = &vec_last(ftepp->conditions);
1159 pc->on = !pc->was_on && cond.on;
1160 pc->was_on = pc->was_on || pc->on;
1161 ftepp_update_output_condition(ftepp);
1164 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1165 if (!ftepp_else_allowed(ftepp))
1167 if (!ftepp_ifdef(ftepp, &cond))
1170 pc = &vec_last(ftepp->conditions);
1171 pc->on = !pc->was_on && cond.on;
1172 pc->was_on = pc->was_on || pc->on;
1173 ftepp_update_output_condition(ftepp);
1176 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1177 if (!ftepp_else_allowed(ftepp))
1179 if (!ftepp_if(ftepp, &cond))
1181 pc = &vec_last(ftepp->conditions);
1182 pc->on = !pc->was_on && cond.on;
1183 pc->was_on = pc->was_on || pc->on;
1184 ftepp_update_output_condition(ftepp);
1187 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1188 if (!ftepp_if(ftepp, &cond))
1190 cond.was_on = cond.on;
1191 vec_push(ftepp->conditions, cond);
1192 ftepp->output_on = ftepp->output_on && cond.on;
1195 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1196 if (!ftepp_else_allowed(ftepp))
1198 pc = &vec_last(ftepp->conditions);
1199 pc->on = !pc->was_on;
1200 pc->had_else = true;
1202 ftepp_update_output_condition(ftepp);
1205 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1206 if (!vec_size(ftepp->conditions)) {
1207 ftepp_error(ftepp, "#endif without #if");
1210 vec_pop(ftepp->conditions);
1212 ftepp_update_output_condition(ftepp);
1215 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1216 return ftepp_include(ftepp);
1218 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1219 ftepp_out(ftepp, "#", false);
1223 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1226 /* break; never reached */
1228 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1231 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1234 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1237 /* Builtins! Don't forget the builtins! */
1238 case TOKEN_INTCONST:
1239 case TOKEN_FLOATCONST:
1240 ftepp_out(ftepp, "#", false);
1243 if (!ftepp_skipspace(ftepp))
1248 static bool ftepp_preprocess(ftepp_t *ftepp)
1251 bool newline = true;
1253 ftepp->lex->flags.preprocessing = true;
1254 ftepp->lex->flags.mergelines = false;
1255 ftepp->lex->flags.noops = true;
1260 if (ftepp->token >= TOKEN_EOF)
1263 ftepp->newline = newline;
1266 /* For the sake of FTE compatibility... FU, really */
1267 ftepp->newline = newline = true;
1270 switch (ftepp->token) {
1273 case TOKEN_TYPENAME:
1274 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1276 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1280 if (!ftepp_macro_call(ftepp, macro))
1281 ftepp->token = TOKEN_ERROR;
1284 if (!ftepp->newline) {
1285 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1289 ftepp->lex->flags.mergelines = true;
1290 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1291 ftepp_error(ftepp, "error in preprocessor directive");
1292 ftepp->token = TOKEN_ERROR;
1295 if (!ftepp_hash(ftepp))
1296 ftepp->token = TOKEN_ERROR;
1297 ftepp->lex->flags.mergelines = false;
1301 ftepp_out(ftepp, "\n", true);
1305 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1309 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1311 /* force a 0 at the end but don't count it as added to the output */
1312 vec_push(ftepp->output_string, 0);
1313 vec_shrinkby(ftepp->output_string, 1);
1315 newline = ftepp->token == TOKEN_EOF;
1319 /* Like in parser.c - files keep the previous state so we have one global
1320 * preprocessor. Except here we will want to warn about dangling #ifs.
1322 static ftepp_t *ftepp;
1324 static bool ftepp_preprocess_done()
1327 if (vec_size(ftepp->conditions)) {
1328 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1331 lex_close(ftepp->lex);
1333 if (ftepp->itemname) {
1334 mem_d(ftepp->itemname);
1335 ftepp->itemname = NULL;
1340 bool ftepp_preprocess_file(const char *filename)
1342 ftepp->lex = lex_open(filename);
1343 ftepp->itemname = util_strdup(filename);
1345 con_out("failed to open file \"%s\"\n", filename);
1348 if (!ftepp_preprocess(ftepp))
1350 return ftepp_preprocess_done();
1353 bool ftepp_preprocess_string(const char *name, const char *str)
1355 ftepp->lex = lex_open_string(str, strlen(str), name);
1356 ftepp->itemname = util_strdup(name);
1358 con_out("failed to create lexer for string \"%s\"\n", name);
1361 if (!ftepp_preprocess(ftepp))
1363 return ftepp_preprocess_done();
1368 ftepp = ftepp_new();
1372 /* set the right macro based on the selected standard */
1373 ftepp_add_define(NULL, "GMQCC");
1374 if (opts_standard == COMPILER_FTEQCC)
1375 ftepp_add_define(NULL, "__STD_FTEQCC__");
1376 else if (opts_standard == COMPILER_GMQCC)
1377 ftepp_add_define(NULL, "__STD_GMQCC__");
1378 else if (opts_standard == COMPILER_QCC)
1379 ftepp_add_define(NULL, "__STD_QCC__");
1384 void ftepp_add_define(const char *source, const char *name)
1387 lex_ctx ctx = { "__builtin__", 0 };
1389 macro = ppmacro_new(ctx, name);
1390 vec_push(ftepp->macros, macro);
1393 const char *ftepp_get()
1395 return ftepp->output_string;
1400 vec_free(ftepp->output_string);
1407 ftepp_delete(ftepp);