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;
68 #define ftepp_tokval(f) ((f)->lex->tok.value)
69 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
71 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
78 con_vprintmsg(LVL_ERROR, ctx.file, ctx.line, "error", fmt, ap);
82 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
89 con_vprintmsg(LVL_ERROR, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
93 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
96 int lvl = LVL_WARNING;
98 if (!OPTS_WARN(warntype))
107 con_vprintmsg(lvl, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
112 static pptoken *pptoken_make(ftepp_t *ftepp)
114 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
115 token->token = ftepp->token;
117 if (token->token == TOKEN_WHITE)
118 token->value = util_strdup(" ");
121 token->value = util_strdup(ftepp_tokval(ftepp));
123 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
127 static void pptoken_delete(pptoken *self)
133 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
135 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
136 memset(macro, 0, sizeof(*macro));
137 macro->name = util_strdup(name);
141 static void ppmacro_delete(ppmacro *self)
144 for (i = 0; i < vec_size(self->params); ++i)
145 mem_d(self->params[i]);
146 vec_free(self->params);
147 for (i = 0; i < vec_size(self->output); ++i)
148 pptoken_delete(self->output[i]);
149 vec_free(self->output);
154 static ftepp_t* ftepp_new()
158 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
159 memset(ftepp, 0, sizeof(*ftepp));
161 ftepp->output_on = true;
166 static void ftepp_delete(ftepp_t *self)
169 for (i = 0; i < vec_size(self->macros); ++i)
170 ppmacro_delete(self->macros[i]);
171 vec_free(self->macros);
172 vec_free(self->conditions);
174 lex_close(self->lex);
178 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
180 if (ignore_cond || ftepp->output_on)
185 data = vec_add(ftepp->output_string, len);
186 memcpy(data, str, len);
190 static void ftepp_update_output_condition(ftepp_t *ftepp)
193 ftepp->output_on = true;
194 for (i = 0; i < vec_size(ftepp->conditions); ++i)
195 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
198 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
201 for (i = 0; i < vec_size(ftepp->macros); ++i) {
202 if (!strcmp(name, ftepp->macros[i]->name))
203 return ftepp->macros[i];
208 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
211 for (i = 0; i < vec_size(ftepp->macros); ++i) {
212 if (!strcmp(name, ftepp->macros[i]->name)) {
213 vec_remove(ftepp->macros, i, 1);
219 static inline int ftepp_next(ftepp_t *ftepp)
221 return (ftepp->token = lex_do(ftepp->lex));
224 /* Important: this does not skip newlines! */
225 static bool ftepp_skipspace(ftepp_t *ftepp)
227 if (ftepp->token != TOKEN_WHITE)
229 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
230 if (ftepp->token >= TOKEN_EOF) {
231 ftepp_error(ftepp, "unexpected end of preprocessor directive");
237 /* this one skips EOLs as well */
238 static bool ftepp_skipallwhite(ftepp_t *ftepp)
240 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
244 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
245 if (ftepp->token >= TOKEN_EOF) {
246 ftepp_error(ftepp, "unexpected end of preprocessor directive");
253 * The huge macro parsing code...
255 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
259 if (!ftepp_skipspace(ftepp))
261 if (ftepp->token == ')')
263 switch (ftepp->token) {
269 ftepp_error(ftepp, "unexpected token in parameter list");
272 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
274 if (!ftepp_skipspace(ftepp))
276 } while (ftepp->token == ',');
277 if (ftepp->token != ')') {
278 ftepp_error(ftepp, "expected closing paren after macro parameter list");
282 /* skipspace happens in ftepp_define */
286 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
289 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
290 ptok = pptoken_make(ftepp);
291 vec_push(macro->output, ptok);
294 /* recursive expansion can cause EOFs here */
295 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
296 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
302 static bool ftepp_define(ftepp_t *ftepp)
305 (void)ftepp_next(ftepp);
306 if (!ftepp_skipspace(ftepp))
309 switch (ftepp->token) {
313 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
314 if (macro && ftepp->output_on) {
315 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
317 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
319 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
322 ftepp_error(ftepp, "expected macro name");
326 (void)ftepp_next(ftepp);
328 if (ftepp->token == '(') {
329 macro->has_params = true;
330 if (!ftepp_define_params(ftepp, macro))
334 if (!ftepp_skipspace(ftepp))
337 if (!ftepp_define_body(ftepp, macro))
340 if (ftepp->output_on)
341 vec_push(ftepp->macros, macro);
343 ppmacro_delete(macro);
349 * When a macro is used we have to handle parameters as well
350 * as special-concatenation via ## or stringification via #
352 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
353 * this kind of parens. Curly braces or [] don't count towards the
360 static void macroparam_clean(macroparam *self)
363 for (i = 0; i < vec_size(self->tokens); ++i)
364 pptoken_delete(self->tokens[i]);
365 vec_free(self->tokens);
368 /* need to leave the last token up */
369 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
371 macroparam *params = NULL;
377 if (!ftepp_skipallwhite(ftepp))
379 while (ftepp->token != ')') {
381 if (!ftepp_skipallwhite(ftepp))
383 while (parens || ftepp->token != ',') {
384 if (ftepp->token == '(')
386 else if (ftepp->token == ')') {
391 ptok = pptoken_make(ftepp);
392 vec_push(mp.tokens, ptok);
393 if (ftepp_next(ftepp) >= TOKEN_EOF) {
394 ftepp_error(ftepp, "unexpected EOF in macro call");
398 vec_push(params, mp);
400 if (ftepp->token == ')')
402 if (ftepp->token != ',') {
403 ftepp_error(ftepp, "expected closing paren or comma in macro call");
406 if (ftepp_next(ftepp) >= TOKEN_EOF) {
407 ftepp_error(ftepp, "unexpected EOF in macro call");
411 /* need to leave that up
412 if (ftepp_next(ftepp) >= TOKEN_EOF) {
413 ftepp_error(ftepp, "unexpected EOF in macro call");
417 *out_params = params;
422 macroparam_clean(&mp);
423 for (i = 0; i < vec_size(params); ++i)
424 macroparam_clean(¶ms[i]);
429 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
432 for (i = 0; i < vec_size(macro->params); ++i) {
433 if (!strcmp(macro->params[i], name)) {
441 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
446 switch (token->token) {
447 case TOKEN_STRINGCONST:
450 /* in preprocessor mode strings already are string,
451 * so we don't get actual newline bytes here.
452 * Still need to escape backslashes and quotes.
455 case '\\': ftepp_out(ftepp, "\\\\", false); break;
456 case '"': ftepp_out(ftepp, "\\\"", false); break;
459 ftepp_out(ftepp, chs, false);
466 ftepp_out(ftepp, " ", false);
469 ftepp_out(ftepp, "\\n", false);
472 ftepp_out(ftepp, token->value, false);
477 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
480 ftepp_out(ftepp, "\"", false);
481 for (i = 0; i < vec_size(param->tokens); ++i)
482 ftepp_stringify_token(ftepp, param->tokens[i]);
483 ftepp_out(ftepp, "\"", false);
486 static void ftepp_recursion_header(ftepp_t *ftepp)
488 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
491 static void ftepp_recursion_footer(ftepp_t *ftepp)
493 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
496 static bool ftepp_preprocess(ftepp_t *ftepp);
497 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
499 char *old_string = ftepp->output_string;
500 lex_file *old_lexer = ftepp->lex;
509 if (!vec_size(macro->output))
512 ftepp->output_string = NULL;
513 for (o = 0; o < vec_size(macro->output); ++o) {
514 pptoken *out = macro->output[o];
515 switch (out->token) {
519 if (!macro_params_find(macro, out->value, &pi)) {
520 ftepp_out(ftepp, out->value, false);
523 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
524 out = params[pi].tokens[pv];
525 if (out->token == TOKEN_EOL)
526 ftepp_out(ftepp, "\n", false);
528 ftepp_out(ftepp, out->value, false);
533 if (o + 1 < vec_size(macro->output)) {
534 nextok = macro->output[o+1]->token;
536 /* raw concatenation */
540 if ( (nextok == TOKEN_IDENT ||
541 nextok == TOKEN_KEYWORD ||
542 nextok == TOKEN_TYPENAME) &&
543 macro_params_find(macro, macro->output[o+1]->value, &pi))
546 ftepp_stringify(ftepp, ¶ms[pi]);
550 ftepp_out(ftepp, "#", false);
553 ftepp_out(ftepp, "\n", false);
556 ftepp_out(ftepp, out->value, false);
560 vec_push(ftepp->output_string, 0);
561 /* Now run the preprocessor recursively on this string buffer */
563 printf("__________\n%s\n=========\n", ftepp->output_string);
565 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
567 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
571 ftepp->output_string = old_string;
573 ftepp_recursion_header(ftepp);
574 if (!ftepp_preprocess(ftepp)) {
575 lex_close(ftepp->lex);
579 ftepp_recursion_footer(ftepp);
580 old_string = ftepp->output_string;
583 ftepp->lex = old_lexer;
584 ftepp->output_string = old_string;
588 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
591 macroparam *params = NULL;
594 if (!macro->has_params) {
595 if (!ftepp_macro_expand(ftepp, macro, NULL))
602 if (!ftepp_skipallwhite(ftepp))
605 if (ftepp->token != '(') {
606 ftepp_error(ftepp, "expected macro parameters in parenthesis");
611 if (!ftepp_macro_call_params(ftepp, ¶ms))
614 if (vec_size(params) != vec_size(macro->params)) {
615 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
616 (unsigned int)vec_size(macro->params),
617 (unsigned int)vec_size(params));
622 if (!ftepp_macro_expand(ftepp, macro, params))
627 for (o = 0; o < vec_size(params); ++o)
628 macroparam_clean(¶ms[o]);
634 * #if - the FTEQCC way:
635 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
636 * <numbers> => True if the number is not 0
637 * !<factor> => True if the factor yields false
638 * !!<factor> => ERROR on 2 or more unary nots
639 * <macro> => becomes the macro's FIRST token regardless of parameters
640 * <e> && <e> => True if both expressions are true
641 * <e> || <e> => True if either expression is true
643 * <ident> => False (remember for macros the <macro> rule applies instead)
644 * Unary + and - are weird and wrong in fteqcc so we don't allow them
645 * parenthesis in expressions are allowed
646 * parameter lists on macros are errors
647 * No mathematical calculations are executed
649 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
654 if (!ftepp_skipspace(ftepp))
657 while (ftepp->token == '!') {
660 if (!ftepp_skipspace(ftepp))
664 switch (ftepp->token) {
668 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
670 if (!ftepp_skipspace(ftepp))
672 if (ftepp->token != '(') {
673 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
677 if (!ftepp_skipspace(ftepp))
679 if (ftepp->token != TOKEN_IDENT &&
680 ftepp->token != TOKEN_TYPENAME &&
681 ftepp->token != TOKEN_KEYWORD)
683 ftepp_error(ftepp, "defined() used on an unexpected token type");
686 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
689 if (!ftepp_skipspace(ftepp))
691 if (ftepp->token != ')') {
692 ftepp_error(ftepp, "expected closing paren");
698 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
699 if (!macro || !vec_size(macro->output)) {
702 /* This does not expand recursively! */
703 switch (macro->output[0]->token) {
705 *out = !!(macro->output[0]->constval.f);
707 case TOKEN_FLOATCONST:
708 *out = !!(macro->output[0]->constval.f);
716 case TOKEN_STRINGCONST:
720 *out = !!(ftepp->lex->tok.constval.i);
722 case TOKEN_FLOATCONST:
723 *out = !!(ftepp->lex->tok.constval.f);
728 if (!ftepp_if_expr(ftepp, out))
730 if (ftepp->token != ')') {
731 ftepp_error(ftepp, "expected closing paren in #if expression");
737 ftepp_error(ftepp, "junk in #if");
743 ftepp->lex->flags.noops = false;
745 if (!ftepp_skipspace(ftepp))
747 ftepp->lex->flags.noops = true;
749 if (ftepp->token == ')')
752 if (ftepp->token != TOKEN_OPERATOR)
755 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
756 !strcmp(ftepp_tokval(ftepp), "||"))
759 char opc = ftepp_tokval(ftepp)[0];
762 if (!ftepp_if_expr(ftepp, &next))
772 ftepp_error(ftepp, "junk after #if");
777 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
781 memset(cond, 0, sizeof(*cond));
782 (void)ftepp_next(ftepp);
784 if (!ftepp_skipspace(ftepp))
786 if (ftepp->token == TOKEN_EOL) {
787 ftepp_error(ftepp, "expected expression for #if-directive");
791 if (!ftepp_if_expr(ftepp, &result))
799 * ifdef is rather simple
801 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
804 memset(cond, 0, sizeof(*cond));
805 (void)ftepp_next(ftepp);
806 if (!ftepp_skipspace(ftepp))
809 switch (ftepp->token) {
813 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
816 ftepp_error(ftepp, "expected macro name");
820 (void)ftepp_next(ftepp);
821 if (!ftepp_skipspace(ftepp))
823 /* relaxing this condition
824 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
825 ftepp_error(ftepp, "stray tokens after #ifdef");
834 * undef is also simple
836 static bool ftepp_undef(ftepp_t *ftepp)
838 (void)ftepp_next(ftepp);
839 if (!ftepp_skipspace(ftepp))
842 if (ftepp->output_on) {
843 switch (ftepp->token) {
847 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
850 ftepp_error(ftepp, "expected macro name");
855 (void)ftepp_next(ftepp);
856 if (!ftepp_skipspace(ftepp))
858 /* relaxing this condition
859 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
860 ftepp_error(ftepp, "stray tokens after #ifdef");
867 /* Basic structure handlers */
868 static bool ftepp_else_allowed(ftepp_t *ftepp)
870 if (!vec_size(ftepp->conditions)) {
871 ftepp_error(ftepp, "#else without #if");
874 if (vec_last(ftepp->conditions).had_else) {
875 ftepp_error(ftepp, "multiple #else for a single #if");
881 static bool ftepp_hash(ftepp_t *ftepp)
886 lex_ctx ctx = ftepp_ctx(ftepp);
888 if (!ftepp_skipspace(ftepp))
891 switch (ftepp->token) {
895 if (!strcmp(ftepp_tokval(ftepp), "define")) {
896 return ftepp_define(ftepp);
898 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
899 return ftepp_undef(ftepp);
901 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
902 if (!ftepp_ifdef(ftepp, &cond))
904 cond.was_on = cond.on;
905 vec_push(ftepp->conditions, cond);
906 ftepp->output_on = ftepp->output_on && cond.on;
909 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
910 if (!ftepp_ifdef(ftepp, &cond))
913 cond.was_on = cond.on;
914 vec_push(ftepp->conditions, cond);
915 ftepp->output_on = ftepp->output_on && cond.on;
918 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
919 if (!ftepp_else_allowed(ftepp))
921 if (!ftepp_ifdef(ftepp, &cond))
923 pc = &vec_last(ftepp->conditions);
924 pc->on = !pc->was_on && cond.on;
925 pc->was_on = pc->was_on || pc->on;
926 ftepp_update_output_condition(ftepp);
929 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
930 if (!ftepp_else_allowed(ftepp))
932 if (!ftepp_ifdef(ftepp, &cond))
935 pc = &vec_last(ftepp->conditions);
936 pc->on = !pc->was_on && cond.on;
937 pc->was_on = pc->was_on || pc->on;
938 ftepp_update_output_condition(ftepp);
941 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
942 if (!ftepp_else_allowed(ftepp))
944 if (!ftepp_if(ftepp, &cond))
946 pc = &vec_last(ftepp->conditions);
947 pc->on = !pc->was_on && cond.on;
948 pc->was_on = pc->was_on || pc->on;
949 ftepp_update_output_condition(ftepp);
952 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
953 if (!ftepp_if(ftepp, &cond))
955 cond.was_on = cond.on;
956 vec_push(ftepp->conditions, cond);
957 ftepp->output_on = ftepp->output_on && cond.on;
960 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
961 if (!ftepp_else_allowed(ftepp))
963 pc = &vec_last(ftepp->conditions);
964 pc->on = !pc->was_on;
967 ftepp_update_output_condition(ftepp);
970 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
971 if (!vec_size(ftepp->conditions)) {
972 ftepp_error(ftepp, "#endif without #if");
975 vec_pop(ftepp->conditions);
977 ftepp_update_output_condition(ftepp);
981 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
986 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
989 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
992 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
995 /* Builtins! Don't forget the builtins! */
997 case TOKEN_FLOATCONST:
998 ftepp_out(ftepp, "#", false);
1001 if (!ftepp_skipspace(ftepp))
1006 static bool ftepp_preprocess(ftepp_t *ftepp)
1009 bool newline = true;
1011 ftepp->lex->flags.preprocessing = true;
1012 ftepp->lex->flags.mergelines = false;
1013 ftepp->lex->flags.noops = true;
1018 if (ftepp->token >= TOKEN_EOF)
1021 ftepp->newline = newline;
1024 /* For the sake of FTE compatibility... FU, really */
1025 ftepp->newline = newline = true;
1028 switch (ftepp->token) {
1031 case TOKEN_TYPENAME:
1032 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1034 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1038 if (!ftepp_macro_call(ftepp, macro))
1039 ftepp->token = TOKEN_ERROR;
1042 if (!ftepp->newline) {
1043 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1047 ftepp->lex->flags.mergelines = true;
1048 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1049 ftepp_error(ftepp, "error in preprocessor directive");
1050 ftepp->token = TOKEN_ERROR;
1053 if (!ftepp_hash(ftepp))
1054 ftepp->token = TOKEN_ERROR;
1055 ftepp->lex->flags.mergelines = false;
1059 ftepp_out(ftepp, "\n", true);
1063 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1067 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1069 newline = ftepp->token == TOKEN_EOF;
1073 /* Like in parser.c - files keep the previous state so we have one global
1074 * preprocessor. Except here we will want to warn about dangling #ifs.
1076 static ftepp_t *ftepp;
1078 static bool ftepp_preprocess_done()
1081 lex_close(ftepp->lex);
1083 if (vec_size(ftepp->conditions)) {
1084 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1090 bool ftepp_preprocess_file(const char *filename)
1092 ftepp->lex = lex_open(filename);
1094 con_out("failed to open file \"%s\"\n", filename);
1097 if (!ftepp_preprocess(ftepp)) {
1098 ftepp_delete(ftepp);
1101 return ftepp_preprocess_done();
1104 bool ftepp_preprocess_string(const char *name, const char *str)
1106 ftepp_t *ftepp = ftepp_new();
1107 ftepp->lex = lex_open_string(str, strlen(str), name);
1109 con_out("failed to create lexer for string \"%s\"\n", name);
1112 if (!ftepp_preprocess(ftepp)) {
1113 ftepp_delete(ftepp);
1116 return ftepp_preprocess_done();
1121 ftepp = ftepp_new();
1125 const char *ftepp_get()
1127 return ftepp->output_string;
1132 vec_free(ftepp->output_string);
1139 ftepp_delete(ftepp);