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;
70 #define ftepp_tokval(f) ((f)->lex->tok.value)
71 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
73 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
80 con_vprintmsg(LVL_ERROR, ctx.file, ctx.line, "error", fmt, ap);
84 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
91 con_vprintmsg(LVL_ERROR, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
95 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
98 int lvl = LVL_WARNING;
100 if (!OPTS_WARN(warntype))
109 con_vprintmsg(lvl, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
114 static pptoken *pptoken_make(ftepp_t *ftepp)
116 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
117 token->token = ftepp->token;
119 if (token->token == TOKEN_WHITE)
120 token->value = util_strdup(" ");
123 token->value = util_strdup(ftepp_tokval(ftepp));
125 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
129 static void pptoken_delete(pptoken *self)
135 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
137 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
138 memset(macro, 0, sizeof(*macro));
139 macro->name = util_strdup(name);
143 static void ppmacro_delete(ppmacro *self)
146 for (i = 0; i < vec_size(self->params); ++i)
147 mem_d(self->params[i]);
148 vec_free(self->params);
149 for (i = 0; i < vec_size(self->output); ++i)
150 pptoken_delete(self->output[i]);
151 vec_free(self->output);
156 static ftepp_t* ftepp_new()
160 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
161 memset(ftepp, 0, sizeof(*ftepp));
163 ftepp->output_on = true;
168 static void ftepp_delete(ftepp_t *self)
172 mem_d(self->itemname);
173 for (i = 0; i < vec_size(self->macros); ++i)
174 ppmacro_delete(self->macros[i]);
175 vec_free(self->macros);
176 vec_free(self->conditions);
178 lex_close(self->lex);
182 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
184 if (ignore_cond || ftepp->output_on)
189 data = vec_add(ftepp->output_string, len);
190 memcpy(data, str, len);
194 static void ftepp_update_output_condition(ftepp_t *ftepp)
197 ftepp->output_on = true;
198 for (i = 0; i < vec_size(ftepp->conditions); ++i)
199 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
202 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
205 for (i = 0; i < vec_size(ftepp->macros); ++i) {
206 if (!strcmp(name, ftepp->macros[i]->name))
207 return ftepp->macros[i];
212 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
215 for (i = 0; i < vec_size(ftepp->macros); ++i) {
216 if (!strcmp(name, ftepp->macros[i]->name)) {
217 vec_remove(ftepp->macros, i, 1);
223 static inline int ftepp_next(ftepp_t *ftepp)
225 return (ftepp->token = lex_do(ftepp->lex));
228 /* Important: this does not skip newlines! */
229 static bool ftepp_skipspace(ftepp_t *ftepp)
231 if (ftepp->token != TOKEN_WHITE)
233 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
234 if (ftepp->token >= TOKEN_EOF) {
235 ftepp_error(ftepp, "unexpected end of preprocessor directive");
241 /* this one skips EOLs as well */
242 static bool ftepp_skipallwhite(ftepp_t *ftepp)
244 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
248 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
249 if (ftepp->token >= TOKEN_EOF) {
250 ftepp_error(ftepp, "unexpected end of preprocessor directive");
257 * The huge macro parsing code...
259 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
263 if (!ftepp_skipspace(ftepp))
265 if (ftepp->token == ')')
267 switch (ftepp->token) {
273 ftepp_error(ftepp, "unexpected token in parameter list");
276 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
278 if (!ftepp_skipspace(ftepp))
280 } while (ftepp->token == ',');
281 if (ftepp->token != ')') {
282 ftepp_error(ftepp, "expected closing paren after macro parameter list");
286 /* skipspace happens in ftepp_define */
290 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
293 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
294 ptok = pptoken_make(ftepp);
295 vec_push(macro->output, ptok);
298 /* recursive expansion can cause EOFs here */
299 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
300 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
306 static bool ftepp_define(ftepp_t *ftepp)
309 (void)ftepp_next(ftepp);
310 if (!ftepp_skipspace(ftepp))
313 switch (ftepp->token) {
317 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
318 if (macro && ftepp->output_on) {
319 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
321 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
323 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
326 ftepp_error(ftepp, "expected macro name");
330 (void)ftepp_next(ftepp);
332 if (ftepp->token == '(') {
333 macro->has_params = true;
334 if (!ftepp_define_params(ftepp, macro))
338 if (!ftepp_skipspace(ftepp))
341 if (!ftepp_define_body(ftepp, macro))
344 if (ftepp->output_on)
345 vec_push(ftepp->macros, macro);
347 ppmacro_delete(macro);
353 * When a macro is used we have to handle parameters as well
354 * as special-concatenation via ## or stringification via #
356 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
357 * this kind of parens. Curly braces or [] don't count towards the
364 static void macroparam_clean(macroparam *self)
367 for (i = 0; i < vec_size(self->tokens); ++i)
368 pptoken_delete(self->tokens[i]);
369 vec_free(self->tokens);
372 /* need to leave the last token up */
373 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
375 macroparam *params = NULL;
381 if (!ftepp_skipallwhite(ftepp))
383 while (ftepp->token != ')') {
385 if (!ftepp_skipallwhite(ftepp))
387 while (parens || ftepp->token != ',') {
388 if (ftepp->token == '(')
390 else if (ftepp->token == ')') {
395 ptok = pptoken_make(ftepp);
396 vec_push(mp.tokens, ptok);
397 if (ftepp_next(ftepp) >= TOKEN_EOF) {
398 ftepp_error(ftepp, "unexpected EOF in macro call");
402 vec_push(params, mp);
404 if (ftepp->token == ')')
406 if (ftepp->token != ',') {
407 ftepp_error(ftepp, "expected closing paren or comma in macro call");
410 if (ftepp_next(ftepp) >= TOKEN_EOF) {
411 ftepp_error(ftepp, "unexpected EOF in macro call");
415 /* need to leave that up
416 if (ftepp_next(ftepp) >= TOKEN_EOF) {
417 ftepp_error(ftepp, "unexpected EOF in macro call");
421 *out_params = params;
426 macroparam_clean(&mp);
427 for (i = 0; i < vec_size(params); ++i)
428 macroparam_clean(¶ms[i]);
433 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
436 for (i = 0; i < vec_size(macro->params); ++i) {
437 if (!strcmp(macro->params[i], name)) {
445 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
450 switch (token->token) {
451 case TOKEN_STRINGCONST:
454 /* in preprocessor mode strings already are string,
455 * so we don't get actual newline bytes here.
456 * Still need to escape backslashes and quotes.
459 case '\\': ftepp_out(ftepp, "\\\\", false); break;
460 case '"': ftepp_out(ftepp, "\\\"", false); break;
463 ftepp_out(ftepp, chs, false);
470 ftepp_out(ftepp, " ", false);
473 ftepp_out(ftepp, "\\n", false);
476 ftepp_out(ftepp, token->value, false);
481 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
484 ftepp_out(ftepp, "\"", false);
485 for (i = 0; i < vec_size(param->tokens); ++i)
486 ftepp_stringify_token(ftepp, param->tokens[i]);
487 ftepp_out(ftepp, "\"", false);
490 static void ftepp_recursion_header(ftepp_t *ftepp)
492 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
495 static void ftepp_recursion_footer(ftepp_t *ftepp)
497 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
500 static bool ftepp_preprocess(ftepp_t *ftepp);
501 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
503 char *old_string = ftepp->output_string;
504 lex_file *old_lexer = ftepp->lex;
513 if (!vec_size(macro->output))
516 ftepp->output_string = NULL;
517 for (o = 0; o < vec_size(macro->output); ++o) {
518 pptoken *out = macro->output[o];
519 switch (out->token) {
523 if (!macro_params_find(macro, out->value, &pi)) {
524 ftepp_out(ftepp, out->value, false);
527 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
528 out = params[pi].tokens[pv];
529 if (out->token == TOKEN_EOL)
530 ftepp_out(ftepp, "\n", false);
532 ftepp_out(ftepp, out->value, false);
537 if (o + 1 < vec_size(macro->output)) {
538 nextok = macro->output[o+1]->token;
540 /* raw concatenation */
544 if ( (nextok == TOKEN_IDENT ||
545 nextok == TOKEN_KEYWORD ||
546 nextok == TOKEN_TYPENAME) &&
547 macro_params_find(macro, macro->output[o+1]->value, &pi))
550 ftepp_stringify(ftepp, ¶ms[pi]);
554 ftepp_out(ftepp, "#", false);
557 ftepp_out(ftepp, "\n", false);
560 ftepp_out(ftepp, out->value, false);
564 vec_push(ftepp->output_string, 0);
565 /* Now run the preprocessor recursively on this string buffer */
567 printf("__________\n%s\n=========\n", ftepp->output_string);
569 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
571 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
575 ftepp->output_string = old_string;
577 ftepp_recursion_header(ftepp);
578 if (!ftepp_preprocess(ftepp)) {
579 lex_close(ftepp->lex);
583 ftepp_recursion_footer(ftepp);
584 old_string = ftepp->output_string;
587 ftepp->lex = old_lexer;
588 ftepp->output_string = old_string;
592 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
595 macroparam *params = NULL;
598 if (!macro->has_params) {
599 if (!ftepp_macro_expand(ftepp, macro, NULL))
606 if (!ftepp_skipallwhite(ftepp))
609 if (ftepp->token != '(') {
610 ftepp_error(ftepp, "expected macro parameters in parenthesis");
615 if (!ftepp_macro_call_params(ftepp, ¶ms))
618 if (vec_size(params) != vec_size(macro->params)) {
619 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
620 (unsigned int)vec_size(macro->params),
621 (unsigned int)vec_size(params));
626 if (!ftepp_macro_expand(ftepp, macro, params))
631 for (o = 0; o < vec_size(params); ++o)
632 macroparam_clean(¶ms[o]);
638 * #if - the FTEQCC way:
639 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
640 * <numbers> => True if the number is not 0
641 * !<factor> => True if the factor yields false
642 * !!<factor> => ERROR on 2 or more unary nots
643 * <macro> => becomes the macro's FIRST token regardless of parameters
644 * <e> && <e> => True if both expressions are true
645 * <e> || <e> => True if either expression is true
647 * <ident> => False (remember for macros the <macro> rule applies instead)
648 * Unary + and - are weird and wrong in fteqcc so we don't allow them
649 * parenthesis in expressions are allowed
650 * parameter lists on macros are errors
651 * No mathematical calculations are executed
653 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
658 if (!ftepp_skipspace(ftepp))
661 while (ftepp->token == '!') {
664 if (!ftepp_skipspace(ftepp))
668 switch (ftepp->token) {
672 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
674 if (!ftepp_skipspace(ftepp))
676 if (ftepp->token != '(') {
677 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
681 if (!ftepp_skipspace(ftepp))
683 if (ftepp->token != TOKEN_IDENT &&
684 ftepp->token != TOKEN_TYPENAME &&
685 ftepp->token != TOKEN_KEYWORD)
687 ftepp_error(ftepp, "defined() used on an unexpected token type");
690 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
693 if (!ftepp_skipspace(ftepp))
695 if (ftepp->token != ')') {
696 ftepp_error(ftepp, "expected closing paren");
702 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
703 if (!macro || !vec_size(macro->output)) {
706 /* This does not expand recursively! */
707 switch (macro->output[0]->token) {
709 *out = !!(macro->output[0]->constval.f);
711 case TOKEN_FLOATCONST:
712 *out = !!(macro->output[0]->constval.f);
720 case TOKEN_STRINGCONST:
724 *out = !!(ftepp->lex->tok.constval.i);
726 case TOKEN_FLOATCONST:
727 *out = !!(ftepp->lex->tok.constval.f);
732 if (!ftepp_if_expr(ftepp, out))
734 if (ftepp->token != ')') {
735 ftepp_error(ftepp, "expected closing paren in #if expression");
741 ftepp_error(ftepp, "junk in #if");
747 ftepp->lex->flags.noops = false;
749 if (!ftepp_skipspace(ftepp))
751 ftepp->lex->flags.noops = true;
753 if (ftepp->token == ')')
756 if (ftepp->token != TOKEN_OPERATOR)
759 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
760 !strcmp(ftepp_tokval(ftepp), "||"))
763 char opc = ftepp_tokval(ftepp)[0];
766 if (!ftepp_if_expr(ftepp, &next))
776 ftepp_error(ftepp, "junk after #if");
781 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
785 memset(cond, 0, sizeof(*cond));
786 (void)ftepp_next(ftepp);
788 if (!ftepp_skipspace(ftepp))
790 if (ftepp->token == TOKEN_EOL) {
791 ftepp_error(ftepp, "expected expression for #if-directive");
795 if (!ftepp_if_expr(ftepp, &result))
803 * ifdef is rather simple
805 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
808 memset(cond, 0, sizeof(*cond));
809 (void)ftepp_next(ftepp);
810 if (!ftepp_skipspace(ftepp))
813 switch (ftepp->token) {
817 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
820 ftepp_error(ftepp, "expected macro name");
824 (void)ftepp_next(ftepp);
825 if (!ftepp_skipspace(ftepp))
827 /* relaxing this condition
828 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
829 ftepp_error(ftepp, "stray tokens after #ifdef");
838 * undef is also simple
840 static bool ftepp_undef(ftepp_t *ftepp)
842 (void)ftepp_next(ftepp);
843 if (!ftepp_skipspace(ftepp))
846 if (ftepp->output_on) {
847 switch (ftepp->token) {
851 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
854 ftepp_error(ftepp, "expected macro name");
859 (void)ftepp_next(ftepp);
860 if (!ftepp_skipspace(ftepp))
862 /* relaxing this condition
863 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
864 ftepp_error(ftepp, "stray tokens after #ifdef");
871 /* Special unescape-string function which skips a leading quote
872 * and stops at a quote, not just at \0
874 static void unescape(const char *str, char *out) {
876 while (*str && *str != '"') {
880 case '\\': *out++ = *str; break;
881 case '"': *out++ = *str; break;
882 case 'a': *out++ = '\a'; break;
883 case 'b': *out++ = '\b'; break;
884 case 'r': *out++ = '\r'; break;
885 case 'n': *out++ = '\n'; break;
886 case 't': *out++ = '\t'; break;
887 case 'f': *out++ = '\f'; break;
888 case 'v': *out++ = '\v'; break;
903 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
905 char *filename = NULL;
908 if (ftepp->itemname) {
909 const char *last_slash;
910 last_slash = strrchr(ftepp->itemname, '/');
912 len = last_slash - ftepp->itemname;
913 memcpy(vec_add(filename, len), ftepp->itemname, len);
914 vec_push(filename, '/');
917 len = strlen(ftepp->itemname);
918 memcpy(vec_add(filename, len), ftepp->itemname, len);
919 if (vec_last(filename) != '/')
920 vec_push(filename, '/');
924 memcpy(vec_add(filename, len), file, len);
925 vec_push(filename, 0);
931 * FIXME: do we need/want a -I option?
932 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
934 static bool ftepp_include(ftepp_t *ftepp)
936 lex_file *old_lexer = ftepp->lex;
942 (void)ftepp_next(ftepp);
943 if (!ftepp_skipspace(ftepp))
946 if (ftepp->token != TOKEN_STRINGCONST) {
947 ftepp_error(ftepp, "expected filename to include");
951 ctx = ftepp_ctx(ftepp);
953 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
955 ftepp_out(ftepp, "\n#pragma file(", false);
956 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
957 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
959 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
960 inlex = lex_open(filename);
962 ftepp_error(ftepp, "failed to open include file `%s`", filename);
968 if (!ftepp_preprocess(ftepp)) {
969 lex_close(ftepp->lex);
970 ftepp->lex = old_lexer;
973 lex_close(ftepp->lex);
974 ftepp->lex = old_lexer;
976 ftepp_out(ftepp, "\n#pragma file(", false);
977 ftepp_out(ftepp, ctx.file, false);
978 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
979 ftepp_out(ftepp, lineno, false);
982 (void)ftepp_next(ftepp);
983 if (!ftepp_skipspace(ftepp))
985 if (ftepp->token != TOKEN_EOL) {
986 ftepp_error(ftepp, "stray tokens after #include");
989 (void)ftepp_next(ftepp);
994 /* Basic structure handlers */
995 static bool ftepp_else_allowed(ftepp_t *ftepp)
997 if (!vec_size(ftepp->conditions)) {
998 ftepp_error(ftepp, "#else without #if");
1001 if (vec_last(ftepp->conditions).had_else) {
1002 ftepp_error(ftepp, "multiple #else for a single #if");
1008 static bool ftepp_hash(ftepp_t *ftepp)
1013 lex_ctx ctx = ftepp_ctx(ftepp);
1015 if (!ftepp_skipspace(ftepp))
1018 switch (ftepp->token) {
1021 case TOKEN_TYPENAME:
1022 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1023 return ftepp_define(ftepp);
1025 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1026 return ftepp_undef(ftepp);
1028 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1029 if (!ftepp_ifdef(ftepp, &cond))
1031 cond.was_on = cond.on;
1032 vec_push(ftepp->conditions, cond);
1033 ftepp->output_on = ftepp->output_on && cond.on;
1036 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1037 if (!ftepp_ifdef(ftepp, &cond))
1040 cond.was_on = cond.on;
1041 vec_push(ftepp->conditions, cond);
1042 ftepp->output_on = ftepp->output_on && cond.on;
1045 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1046 if (!ftepp_else_allowed(ftepp))
1048 if (!ftepp_ifdef(ftepp, &cond))
1050 pc = &vec_last(ftepp->conditions);
1051 pc->on = !pc->was_on && cond.on;
1052 pc->was_on = pc->was_on || pc->on;
1053 ftepp_update_output_condition(ftepp);
1056 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1057 if (!ftepp_else_allowed(ftepp))
1059 if (!ftepp_ifdef(ftepp, &cond))
1062 pc = &vec_last(ftepp->conditions);
1063 pc->on = !pc->was_on && cond.on;
1064 pc->was_on = pc->was_on || pc->on;
1065 ftepp_update_output_condition(ftepp);
1068 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1069 if (!ftepp_else_allowed(ftepp))
1071 if (!ftepp_if(ftepp, &cond))
1073 pc = &vec_last(ftepp->conditions);
1074 pc->on = !pc->was_on && cond.on;
1075 pc->was_on = pc->was_on || pc->on;
1076 ftepp_update_output_condition(ftepp);
1079 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1080 if (!ftepp_if(ftepp, &cond))
1082 cond.was_on = cond.on;
1083 vec_push(ftepp->conditions, cond);
1084 ftepp->output_on = ftepp->output_on && cond.on;
1087 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1088 if (!ftepp_else_allowed(ftepp))
1090 pc = &vec_last(ftepp->conditions);
1091 pc->on = !pc->was_on;
1092 pc->had_else = true;
1094 ftepp_update_output_condition(ftepp);
1097 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1098 if (!vec_size(ftepp->conditions)) {
1099 ftepp_error(ftepp, "#endif without #if");
1102 vec_pop(ftepp->conditions);
1104 ftepp_update_output_condition(ftepp);
1107 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1108 return ftepp_include(ftepp);
1110 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1111 ftepp_out(ftepp, "#", false);
1115 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1120 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1123 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1126 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1129 /* Builtins! Don't forget the builtins! */
1130 case TOKEN_INTCONST:
1131 case TOKEN_FLOATCONST:
1132 ftepp_out(ftepp, "#", false);
1135 if (!ftepp_skipspace(ftepp))
1140 static bool ftepp_preprocess(ftepp_t *ftepp)
1143 bool newline = true;
1145 ftepp->lex->flags.preprocessing = true;
1146 ftepp->lex->flags.mergelines = false;
1147 ftepp->lex->flags.noops = true;
1152 if (ftepp->token >= TOKEN_EOF)
1155 ftepp->newline = newline;
1158 /* For the sake of FTE compatibility... FU, really */
1159 ftepp->newline = newline = true;
1162 switch (ftepp->token) {
1165 case TOKEN_TYPENAME:
1166 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1168 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1172 if (!ftepp_macro_call(ftepp, macro))
1173 ftepp->token = TOKEN_ERROR;
1176 if (!ftepp->newline) {
1177 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1181 ftepp->lex->flags.mergelines = true;
1182 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1183 ftepp_error(ftepp, "error in preprocessor directive");
1184 ftepp->token = TOKEN_ERROR;
1187 if (!ftepp_hash(ftepp))
1188 ftepp->token = TOKEN_ERROR;
1189 ftepp->lex->flags.mergelines = false;
1193 ftepp_out(ftepp, "\n", true);
1197 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1201 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1203 newline = ftepp->token == TOKEN_EOF;
1207 /* Like in parser.c - files keep the previous state so we have one global
1208 * preprocessor. Except here we will want to warn about dangling #ifs.
1210 static ftepp_t *ftepp;
1212 static bool ftepp_preprocess_done()
1215 lex_close(ftepp->lex);
1217 if (vec_size(ftepp->conditions)) {
1218 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1221 if (ftepp->itemname) {
1222 mem_d(ftepp->itemname);
1223 ftepp->itemname = NULL;
1228 bool ftepp_preprocess_file(const char *filename)
1230 ftepp->lex = lex_open(filename);
1231 ftepp->itemname = util_strdup(filename);
1233 con_out("failed to open file \"%s\"\n", filename);
1236 if (!ftepp_preprocess(ftepp)) {
1237 ftepp_delete(ftepp);
1240 return ftepp_preprocess_done();
1243 bool ftepp_preprocess_string(const char *name, const char *str)
1245 ftepp->lex = lex_open_string(str, strlen(str), name);
1246 ftepp->itemname = util_strdup(name);
1248 con_out("failed to create lexer for string \"%s\"\n", name);
1251 if (!ftepp_preprocess(ftepp)) {
1252 ftepp_delete(ftepp);
1255 return ftepp_preprocess_done();
1260 ftepp = ftepp_new();
1264 const char *ftepp_get()
1266 return ftepp->output_string;
1271 vec_free(ftepp->output_string);
1278 ftepp_delete(ftepp);