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)
138 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
139 memset(macro, 0, sizeof(*macro));
140 macro->name = util_strdup(name);
144 static void ppmacro_delete(ppmacro *self)
147 for (i = 0; i < vec_size(self->params); ++i)
148 mem_d(self->params[i]);
149 vec_free(self->params);
150 for (i = 0; i < vec_size(self->output); ++i)
151 pptoken_delete(self->output[i]);
152 vec_free(self->output);
157 static ftepp_t* ftepp_new()
161 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
162 memset(ftepp, 0, sizeof(*ftepp));
164 ftepp->output_on = true;
169 static void ftepp_delete(ftepp_t *self)
173 mem_d(self->itemname);
174 for (i = 0; i < vec_size(self->macros); ++i)
175 ppmacro_delete(self->macros[i]);
176 vec_free(self->macros);
177 vec_free(self->conditions);
179 lex_close(self->lex);
183 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
185 if (ignore_cond || ftepp->output_on)
190 data = vec_add(ftepp->output_string, len);
191 memcpy(data, str, len);
195 static void ftepp_update_output_condition(ftepp_t *ftepp)
198 ftepp->output_on = true;
199 for (i = 0; i < vec_size(ftepp->conditions); ++i)
200 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
203 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
206 for (i = 0; i < vec_size(ftepp->macros); ++i) {
207 if (!strcmp(name, ftepp->macros[i]->name))
208 return ftepp->macros[i];
213 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
216 for (i = 0; i < vec_size(ftepp->macros); ++i) {
217 if (!strcmp(name, ftepp->macros[i]->name)) {
218 vec_remove(ftepp->macros, i, 1);
224 static inline int ftepp_next(ftepp_t *ftepp)
226 return (ftepp->token = lex_do(ftepp->lex));
229 /* Important: this does not skip newlines! */
230 static bool ftepp_skipspace(ftepp_t *ftepp)
232 if (ftepp->token != TOKEN_WHITE)
234 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
235 if (ftepp->token >= TOKEN_EOF) {
236 ftepp_error(ftepp, "unexpected end of preprocessor directive");
242 /* this one skips EOLs as well */
243 static bool ftepp_skipallwhite(ftepp_t *ftepp)
245 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
249 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
250 if (ftepp->token >= TOKEN_EOF) {
251 ftepp_error(ftepp, "unexpected end of preprocessor directive");
258 * The huge macro parsing code...
260 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
264 if (!ftepp_skipspace(ftepp))
266 if (ftepp->token == ')')
268 switch (ftepp->token) {
274 ftepp_error(ftepp, "unexpected token in parameter list");
277 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
279 if (!ftepp_skipspace(ftepp))
281 } while (ftepp->token == ',');
282 if (ftepp->token != ')') {
283 ftepp_error(ftepp, "expected closing paren after macro parameter list");
287 /* skipspace happens in ftepp_define */
291 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
294 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
295 ptok = pptoken_make(ftepp);
296 vec_push(macro->output, ptok);
299 /* recursive expansion can cause EOFs here */
300 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
301 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
307 static bool ftepp_define(ftepp_t *ftepp)
310 (void)ftepp_next(ftepp);
311 if (!ftepp_skipspace(ftepp))
314 switch (ftepp->token) {
318 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
319 if (macro && ftepp->output_on) {
320 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
322 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
324 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
327 ftepp_error(ftepp, "expected macro name");
331 (void)ftepp_next(ftepp);
333 if (ftepp->token == '(') {
334 macro->has_params = true;
335 if (!ftepp_define_params(ftepp, macro))
339 if (!ftepp_skipspace(ftepp))
342 if (!ftepp_define_body(ftepp, macro))
345 if (ftepp->output_on)
346 vec_push(ftepp->macros, macro);
348 ppmacro_delete(macro);
354 * When a macro is used we have to handle parameters as well
355 * as special-concatenation via ## or stringification via #
357 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
358 * this kind of parens. Curly braces or [] don't count towards the
365 static void macroparam_clean(macroparam *self)
368 for (i = 0; i < vec_size(self->tokens); ++i)
369 pptoken_delete(self->tokens[i]);
370 vec_free(self->tokens);
373 /* need to leave the last token up */
374 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
376 macroparam *params = NULL;
382 if (!ftepp_skipallwhite(ftepp))
384 while (ftepp->token != ')') {
386 if (!ftepp_skipallwhite(ftepp))
388 while (parens || ftepp->token != ',') {
389 if (ftepp->token == '(')
391 else if (ftepp->token == ')') {
396 ptok = pptoken_make(ftepp);
397 vec_push(mp.tokens, ptok);
398 if (ftepp_next(ftepp) >= TOKEN_EOF) {
399 ftepp_error(ftepp, "unexpected EOF in macro call");
403 vec_push(params, mp);
405 if (ftepp->token == ')')
407 if (ftepp->token != ',') {
408 ftepp_error(ftepp, "expected closing paren or comma in macro call");
411 if (ftepp_next(ftepp) >= TOKEN_EOF) {
412 ftepp_error(ftepp, "unexpected EOF in macro call");
416 /* need to leave that up
417 if (ftepp_next(ftepp) >= TOKEN_EOF) {
418 ftepp_error(ftepp, "unexpected EOF in macro call");
422 *out_params = params;
427 macroparam_clean(&mp);
428 for (i = 0; i < vec_size(params); ++i)
429 macroparam_clean(¶ms[i]);
434 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
437 for (i = 0; i < vec_size(macro->params); ++i) {
438 if (!strcmp(macro->params[i], name)) {
446 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
451 switch (token->token) {
452 case TOKEN_STRINGCONST:
455 /* in preprocessor mode strings already are string,
456 * so we don't get actual newline bytes here.
457 * Still need to escape backslashes and quotes.
460 case '\\': ftepp_out(ftepp, "\\\\", false); break;
461 case '"': ftepp_out(ftepp, "\\\"", false); break;
464 ftepp_out(ftepp, chs, false);
471 ftepp_out(ftepp, " ", false);
474 ftepp_out(ftepp, "\\n", false);
477 ftepp_out(ftepp, token->value, false);
482 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
485 ftepp_out(ftepp, "\"", false);
486 for (i = 0; i < vec_size(param->tokens); ++i)
487 ftepp_stringify_token(ftepp, param->tokens[i]);
488 ftepp_out(ftepp, "\"", false);
491 static void ftepp_recursion_header(ftepp_t *ftepp)
493 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
496 static void ftepp_recursion_footer(ftepp_t *ftepp)
498 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
501 static bool ftepp_preprocess(ftepp_t *ftepp);
502 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
504 char *old_string = ftepp->output_string;
505 lex_file *old_lexer = ftepp->lex;
514 if (!vec_size(macro->output))
517 ftepp->output_string = NULL;
518 for (o = 0; o < vec_size(macro->output); ++o) {
519 pptoken *out = macro->output[o];
520 switch (out->token) {
524 if (!macro_params_find(macro, out->value, &pi)) {
525 ftepp_out(ftepp, out->value, false);
528 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
529 out = params[pi].tokens[pv];
530 if (out->token == TOKEN_EOL)
531 ftepp_out(ftepp, "\n", false);
533 ftepp_out(ftepp, out->value, false);
538 if (o + 1 < vec_size(macro->output)) {
539 nextok = macro->output[o+1]->token;
541 /* raw concatenation */
545 if ( (nextok == TOKEN_IDENT ||
546 nextok == TOKEN_KEYWORD ||
547 nextok == TOKEN_TYPENAME) &&
548 macro_params_find(macro, macro->output[o+1]->value, &pi))
551 ftepp_stringify(ftepp, ¶ms[pi]);
555 ftepp_out(ftepp, "#", false);
558 ftepp_out(ftepp, "\n", false);
561 ftepp_out(ftepp, out->value, false);
565 vec_push(ftepp->output_string, 0);
566 /* Now run the preprocessor recursively on this string buffer */
568 printf("__________\n%s\n=========\n", ftepp->output_string);
570 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
572 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
576 ftepp->output_string = old_string;
578 ftepp_recursion_header(ftepp);
579 if (!ftepp_preprocess(ftepp)) {
580 lex_close(ftepp->lex);
584 ftepp_recursion_footer(ftepp);
585 old_string = ftepp->output_string;
588 ftepp->lex = old_lexer;
589 ftepp->output_string = old_string;
593 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
596 macroparam *params = NULL;
599 if (!macro->has_params) {
600 if (!ftepp_macro_expand(ftepp, macro, NULL))
607 if (!ftepp_skipallwhite(ftepp))
610 if (ftepp->token != '(') {
611 ftepp_error(ftepp, "expected macro parameters in parenthesis");
616 if (!ftepp_macro_call_params(ftepp, ¶ms))
619 if (vec_size(params) != vec_size(macro->params)) {
620 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
621 (unsigned int)vec_size(macro->params),
622 (unsigned int)vec_size(params));
627 if (!ftepp_macro_expand(ftepp, macro, params))
632 for (o = 0; o < vec_size(params); ++o)
633 macroparam_clean(¶ms[o]);
639 * #if - the FTEQCC way:
640 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
641 * <numbers> => True if the number is not 0
642 * !<factor> => True if the factor yields false
643 * !!<factor> => ERROR on 2 or more unary nots
644 * <macro> => becomes the macro's FIRST token regardless of parameters
645 * <e> && <e> => True if both expressions are true
646 * <e> || <e> => True if either expression is true
648 * <ident> => False (remember for macros the <macro> rule applies instead)
649 * Unary + and - are weird and wrong in fteqcc so we don't allow them
650 * parenthesis in expressions are allowed
651 * parameter lists on macros are errors
652 * No mathematical calculations are executed
654 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
659 if (!ftepp_skipspace(ftepp))
662 while (ftepp->token == '!') {
665 if (!ftepp_skipspace(ftepp))
669 switch (ftepp->token) {
673 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
675 if (!ftepp_skipspace(ftepp))
677 if (ftepp->token != '(') {
678 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
682 if (!ftepp_skipspace(ftepp))
684 if (ftepp->token != TOKEN_IDENT &&
685 ftepp->token != TOKEN_TYPENAME &&
686 ftepp->token != TOKEN_KEYWORD)
688 ftepp_error(ftepp, "defined() used on an unexpected token type");
691 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
694 if (!ftepp_skipspace(ftepp))
696 if (ftepp->token != ')') {
697 ftepp_error(ftepp, "expected closing paren");
703 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
704 if (!macro || !vec_size(macro->output)) {
707 /* This does not expand recursively! */
708 switch (macro->output[0]->token) {
710 *out = !!(macro->output[0]->constval.f);
712 case TOKEN_FLOATCONST:
713 *out = !!(macro->output[0]->constval.f);
721 case TOKEN_STRINGCONST:
725 *out = !!(ftepp->lex->tok.constval.i);
727 case TOKEN_FLOATCONST:
728 *out = !!(ftepp->lex->tok.constval.f);
733 if (!ftepp_if_expr(ftepp, out))
735 if (ftepp->token != ')') {
736 ftepp_error(ftepp, "expected closing paren in #if expression");
742 ftepp_error(ftepp, "junk in #if");
748 ftepp->lex->flags.noops = false;
750 if (!ftepp_skipspace(ftepp))
752 ftepp->lex->flags.noops = true;
754 if (ftepp->token == ')')
757 if (ftepp->token != TOKEN_OPERATOR)
760 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
761 !strcmp(ftepp_tokval(ftepp), "||"))
764 char opc = ftepp_tokval(ftepp)[0];
767 if (!ftepp_if_expr(ftepp, &next))
777 ftepp_error(ftepp, "junk after #if");
782 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
786 memset(cond, 0, sizeof(*cond));
787 (void)ftepp_next(ftepp);
789 if (!ftepp_skipspace(ftepp))
791 if (ftepp->token == TOKEN_EOL) {
792 ftepp_error(ftepp, "expected expression for #if-directive");
796 if (!ftepp_if_expr(ftepp, &result))
804 * ifdef is rather simple
806 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
809 memset(cond, 0, sizeof(*cond));
810 (void)ftepp_next(ftepp);
811 if (!ftepp_skipspace(ftepp))
814 switch (ftepp->token) {
818 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
821 ftepp_error(ftepp, "expected macro name");
825 (void)ftepp_next(ftepp);
826 if (!ftepp_skipspace(ftepp))
828 /* relaxing this condition
829 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
830 ftepp_error(ftepp, "stray tokens after #ifdef");
839 * undef is also simple
841 static bool ftepp_undef(ftepp_t *ftepp)
843 (void)ftepp_next(ftepp);
844 if (!ftepp_skipspace(ftepp))
847 if (ftepp->output_on) {
848 switch (ftepp->token) {
852 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
855 ftepp_error(ftepp, "expected macro name");
860 (void)ftepp_next(ftepp);
861 if (!ftepp_skipspace(ftepp))
863 /* relaxing this condition
864 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
865 ftepp_error(ftepp, "stray tokens after #ifdef");
872 /* Special unescape-string function which skips a leading quote
873 * and stops at a quote, not just at \0
875 static void unescape(const char *str, char *out) {
877 while (*str && *str != '"') {
881 case '\\': *out++ = *str; break;
882 case '"': *out++ = *str; break;
883 case 'a': *out++ = '\a'; break;
884 case 'b': *out++ = '\b'; break;
885 case 'r': *out++ = '\r'; break;
886 case 'n': *out++ = '\n'; break;
887 case 't': *out++ = '\t'; break;
888 case 'f': *out++ = '\f'; break;
889 case 'v': *out++ = '\v'; break;
904 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
906 char *filename = NULL;
909 if (ftepp->itemname) {
910 const char *last_slash;
911 last_slash = strrchr(ftepp->itemname, '/');
913 len = last_slash - ftepp->itemname;
914 memcpy(vec_add(filename, len), ftepp->itemname, len);
915 vec_push(filename, '/');
918 len = strlen(ftepp->itemname);
919 memcpy(vec_add(filename, len), ftepp->itemname, len);
920 if (vec_last(filename) != '/')
921 vec_push(filename, '/');
925 memcpy(vec_add(filename, len), file, len);
926 vec_push(filename, 0);
932 * FIXME: do we need/want a -I option?
933 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
935 static bool ftepp_include(ftepp_t *ftepp)
937 lex_file *old_lexer = ftepp->lex;
943 (void)ftepp_next(ftepp);
944 if (!ftepp_skipspace(ftepp))
947 if (ftepp->token != TOKEN_STRINGCONST) {
948 ftepp_error(ftepp, "expected filename to include");
952 ctx = ftepp_ctx(ftepp);
954 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
956 ftepp_out(ftepp, "\n#pragma file(", false);
957 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
958 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
960 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
961 inlex = lex_open(filename);
963 ftepp_error(ftepp, "failed to open include file `%s`", filename);
969 if (!ftepp_preprocess(ftepp)) {
970 lex_close(ftepp->lex);
971 ftepp->lex = old_lexer;
974 lex_close(ftepp->lex);
975 ftepp->lex = old_lexer;
977 ftepp_out(ftepp, "\n#pragma file(", false);
978 ftepp_out(ftepp, ctx.file, false);
979 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
980 ftepp_out(ftepp, lineno, false);
983 (void)ftepp_next(ftepp);
984 if (!ftepp_skipspace(ftepp))
986 if (ftepp->token != TOKEN_EOL) {
987 ftepp_error(ftepp, "stray tokens after #include");
990 (void)ftepp_next(ftepp);
995 /* Basic structure handlers */
996 static bool ftepp_else_allowed(ftepp_t *ftepp)
998 if (!vec_size(ftepp->conditions)) {
999 ftepp_error(ftepp, "#else without #if");
1002 if (vec_last(ftepp->conditions).had_else) {
1003 ftepp_error(ftepp, "multiple #else for a single #if");
1009 static bool ftepp_hash(ftepp_t *ftepp)
1014 lex_ctx ctx = ftepp_ctx(ftepp);
1016 if (!ftepp_skipspace(ftepp))
1019 switch (ftepp->token) {
1022 case TOKEN_TYPENAME:
1023 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1024 return ftepp_define(ftepp);
1026 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1027 return ftepp_undef(ftepp);
1029 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1030 if (!ftepp_ifdef(ftepp, &cond))
1032 cond.was_on = cond.on;
1033 vec_push(ftepp->conditions, cond);
1034 ftepp->output_on = ftepp->output_on && cond.on;
1037 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1038 if (!ftepp_ifdef(ftepp, &cond))
1041 cond.was_on = cond.on;
1042 vec_push(ftepp->conditions, cond);
1043 ftepp->output_on = ftepp->output_on && cond.on;
1046 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1047 if (!ftepp_else_allowed(ftepp))
1049 if (!ftepp_ifdef(ftepp, &cond))
1051 pc = &vec_last(ftepp->conditions);
1052 pc->on = !pc->was_on && cond.on;
1053 pc->was_on = pc->was_on || pc->on;
1054 ftepp_update_output_condition(ftepp);
1057 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1058 if (!ftepp_else_allowed(ftepp))
1060 if (!ftepp_ifdef(ftepp, &cond))
1063 pc = &vec_last(ftepp->conditions);
1064 pc->on = !pc->was_on && cond.on;
1065 pc->was_on = pc->was_on || pc->on;
1066 ftepp_update_output_condition(ftepp);
1069 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1070 if (!ftepp_else_allowed(ftepp))
1072 if (!ftepp_if(ftepp, &cond))
1074 pc = &vec_last(ftepp->conditions);
1075 pc->on = !pc->was_on && cond.on;
1076 pc->was_on = pc->was_on || pc->on;
1077 ftepp_update_output_condition(ftepp);
1080 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1081 if (!ftepp_if(ftepp, &cond))
1083 cond.was_on = cond.on;
1084 vec_push(ftepp->conditions, cond);
1085 ftepp->output_on = ftepp->output_on && cond.on;
1088 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1089 if (!ftepp_else_allowed(ftepp))
1091 pc = &vec_last(ftepp->conditions);
1092 pc->on = !pc->was_on;
1093 pc->had_else = true;
1095 ftepp_update_output_condition(ftepp);
1098 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1099 if (!vec_size(ftepp->conditions)) {
1100 ftepp_error(ftepp, "#endif without #if");
1103 vec_pop(ftepp->conditions);
1105 ftepp_update_output_condition(ftepp);
1108 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1109 return ftepp_include(ftepp);
1111 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1112 ftepp_out(ftepp, "#", false);
1116 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1119 /* break; never reached */
1121 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1124 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1127 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1130 /* Builtins! Don't forget the builtins! */
1131 case TOKEN_INTCONST:
1132 case TOKEN_FLOATCONST:
1133 ftepp_out(ftepp, "#", false);
1136 if (!ftepp_skipspace(ftepp))
1141 static bool ftepp_preprocess(ftepp_t *ftepp)
1144 bool newline = true;
1146 ftepp->lex->flags.preprocessing = true;
1147 ftepp->lex->flags.mergelines = false;
1148 ftepp->lex->flags.noops = true;
1153 if (ftepp->token >= TOKEN_EOF)
1156 ftepp->newline = newline;
1159 /* For the sake of FTE compatibility... FU, really */
1160 ftepp->newline = newline = true;
1163 switch (ftepp->token) {
1166 case TOKEN_TYPENAME:
1167 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1169 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1173 if (!ftepp_macro_call(ftepp, macro))
1174 ftepp->token = TOKEN_ERROR;
1177 if (!ftepp->newline) {
1178 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1182 ftepp->lex->flags.mergelines = true;
1183 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1184 ftepp_error(ftepp, "error in preprocessor directive");
1185 ftepp->token = TOKEN_ERROR;
1188 if (!ftepp_hash(ftepp))
1189 ftepp->token = TOKEN_ERROR;
1190 ftepp->lex->flags.mergelines = false;
1194 ftepp_out(ftepp, "\n", true);
1198 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1202 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1204 newline = ftepp->token == TOKEN_EOF;
1208 /* Like in parser.c - files keep the previous state so we have one global
1209 * preprocessor. Except here we will want to warn about dangling #ifs.
1211 static ftepp_t *ftepp;
1213 static bool ftepp_preprocess_done()
1216 lex_close(ftepp->lex);
1218 if (vec_size(ftepp->conditions)) {
1219 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1222 if (ftepp->itemname) {
1223 mem_d(ftepp->itemname);
1224 ftepp->itemname = NULL;
1229 bool ftepp_preprocess_file(const char *filename)
1231 ftepp->lex = lex_open(filename);
1232 ftepp->itemname = util_strdup(filename);
1234 con_out("failed to open file \"%s\"\n", filename);
1237 if (!ftepp_preprocess(ftepp)) {
1238 ftepp_delete(ftepp);
1241 return ftepp_preprocess_done();
1244 bool ftepp_preprocess_string(const char *name, const char *str)
1246 ftepp->lex = lex_open_string(str, strlen(str), name);
1247 ftepp->itemname = util_strdup(name);
1249 con_out("failed to create lexer for string \"%s\"\n", name);
1252 if (!ftepp_preprocess(ftepp)) {
1253 ftepp_delete(ftepp);
1256 return ftepp_preprocess_done();
1261 ftepp = ftepp_new();
1265 const char *ftepp_get()
1267 return ftepp->output_string;
1272 vec_free(ftepp->output_string);
1279 ftepp_delete(ftepp);