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 bool ftepp_preprocess(ftepp_t *ftepp);
442 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
444 char *old_string = ftepp->output_string;
445 lex_file *old_lexer = ftepp->lex;
452 if (!vec_size(macro->output))
455 ftepp->output_string = NULL;
456 for (o = 0; o < vec_size(macro->output); ++o) {
457 pptoken *out = macro->output[o];
458 switch (out->token) {
462 if (!macro_params_find(macro, out->value, &pi)) {
463 ftepp_out(ftepp, out->value, false);
466 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
467 out = params[pi].tokens[pv];
468 if (out->token == TOKEN_EOL)
469 ftepp_out(ftepp, "\n", false);
471 ftepp_out(ftepp, out->value, false);
476 if (o + 1 < vec_size(macro->output) && macro->output[o+1]->token == '#') {
477 /* raw concatenation */
481 ftepp_out(ftepp, "#", false);
484 ftepp_out(ftepp, "\n", false);
487 ftepp_out(ftepp, out->value, false);
491 vec_push(ftepp->output_string, 0);
492 /* Now run the preprocessor recursively on this string buffer */
494 printf("__________\n%s\n=========\n", ftepp->output_string);
496 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
498 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
502 ftepp->output_string = old_string;
504 if (!ftepp_preprocess(ftepp)) {
505 lex_close(ftepp->lex);
511 ftepp->lex = old_lexer;
512 ftepp->output_string = old_string;
516 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
519 macroparam *params = NULL;
522 if (!macro->has_params) {
523 if (!ftepp_macro_expand(ftepp, macro, NULL))
530 if (!ftepp_skipallwhite(ftepp))
533 if (ftepp->token != '(') {
534 ftepp_error(ftepp, "expected macro parameters in parenthesis");
539 if (!ftepp_macro_call_params(ftepp, ¶ms))
542 if (vec_size(params) != vec_size(macro->params)) {
543 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
544 (unsigned int)vec_size(macro->params),
545 (unsigned int)vec_size(params));
550 if (!ftepp_macro_expand(ftepp, macro, params))
555 for (o = 0; o < vec_size(params); ++o)
556 macroparam_clean(¶ms[o]);
562 * #if - the FTEQCC way:
563 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
564 * <numbers> => True if the number is not 0
565 * !<factor> => True if the factor yields false
566 * !!<factor> => ERROR on 2 or more unary nots
567 * <macro> => becomes the macro's FIRST token regardless of parameters
568 * <e> && <e> => True if both expressions are true
569 * <e> || <e> => True if either expression is true
571 * <ident> => False (remember for macros the <macro> rule applies instead)
572 * Unary + and - are weird and wrong in fteqcc so we don't allow them
573 * parenthesis in expressions are allowed
574 * parameter lists on macros are errors
575 * No mathematical calculations are executed
577 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
582 if (!ftepp_skipspace(ftepp))
585 while (ftepp->token == '!') {
588 if (!ftepp_skipspace(ftepp))
592 switch (ftepp->token) {
596 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
598 if (!ftepp_skipspace(ftepp))
600 if (ftepp->token != '(') {
601 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
605 if (!ftepp_skipspace(ftepp))
607 if (ftepp->token != TOKEN_IDENT &&
608 ftepp->token != TOKEN_TYPENAME &&
609 ftepp->token != TOKEN_KEYWORD)
611 ftepp_error(ftepp, "defined() used on an unexpected token type");
614 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
617 if (!ftepp_skipspace(ftepp))
619 if (ftepp->token != ')') {
620 ftepp_error(ftepp, "expected closing paren");
626 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
627 if (!macro || !vec_size(macro->output)) {
630 /* This does not expand recursively! */
631 switch (macro->output[0]->token) {
633 *out = !!(macro->output[0]->constval.f);
635 case TOKEN_FLOATCONST:
636 *out = !!(macro->output[0]->constval.f);
644 case TOKEN_STRINGCONST:
648 *out = !!(ftepp->lex->tok.constval.i);
650 case TOKEN_FLOATCONST:
651 *out = !!(ftepp->lex->tok.constval.f);
656 if (!ftepp_if_expr(ftepp, out))
658 if (ftepp->token != ')') {
659 ftepp_error(ftepp, "expected closing paren in #if expression");
665 ftepp_error(ftepp, "junk in #if");
671 ftepp->lex->flags.noops = false;
673 if (!ftepp_skipspace(ftepp))
675 ftepp->lex->flags.noops = true;
677 if (ftepp->token == ')')
680 if (ftepp->token != TOKEN_OPERATOR)
683 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
684 !strcmp(ftepp_tokval(ftepp), "||"))
687 char opc = ftepp_tokval(ftepp)[0];
690 if (!ftepp_if_expr(ftepp, &next))
700 ftepp_error(ftepp, "junk after #if");
705 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
709 memset(cond, 0, sizeof(*cond));
710 (void)ftepp_next(ftepp);
712 if (!ftepp_skipspace(ftepp))
714 if (ftepp->token == TOKEN_EOL) {
715 ftepp_error(ftepp, "expected expression for #if-directive");
719 if (!ftepp_if_expr(ftepp, &result))
727 * ifdef is rather simple
729 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
732 memset(cond, 0, sizeof(*cond));
733 (void)ftepp_next(ftepp);
734 if (!ftepp_skipspace(ftepp))
737 switch (ftepp->token) {
741 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
744 ftepp_error(ftepp, "expected macro name");
748 (void)ftepp_next(ftepp);
749 if (!ftepp_skipspace(ftepp))
751 /* relaxing this condition
752 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
753 ftepp_error(ftepp, "stray tokens after #ifdef");
762 * undef is also simple
764 static bool ftepp_undef(ftepp_t *ftepp)
766 (void)ftepp_next(ftepp);
767 if (!ftepp_skipspace(ftepp))
770 if (ftepp->output_on) {
771 switch (ftepp->token) {
775 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
778 ftepp_error(ftepp, "expected macro name");
783 (void)ftepp_next(ftepp);
784 if (!ftepp_skipspace(ftepp))
786 /* relaxing this condition
787 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
788 ftepp_error(ftepp, "stray tokens after #ifdef");
795 /* Basic structure handlers */
796 static bool ftepp_else_allowed(ftepp_t *ftepp)
798 if (!vec_size(ftepp->conditions)) {
799 ftepp_error(ftepp, "#else without #if");
802 if (vec_last(ftepp->conditions).had_else) {
803 ftepp_error(ftepp, "multiple #else for a single #if");
809 static bool ftepp_hash(ftepp_t *ftepp)
814 lex_ctx ctx = ftepp_ctx(ftepp);
816 if (!ftepp_skipspace(ftepp))
819 switch (ftepp->token) {
823 if (!strcmp(ftepp_tokval(ftepp), "define")) {
824 return ftepp_define(ftepp);
826 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
827 return ftepp_undef(ftepp);
829 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
830 if (!ftepp_ifdef(ftepp, &cond))
832 cond.was_on = cond.on;
833 vec_push(ftepp->conditions, cond);
834 ftepp->output_on = ftepp->output_on && cond.on;
837 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
838 if (!ftepp_ifdef(ftepp, &cond))
841 cond.was_on = cond.on;
842 vec_push(ftepp->conditions, cond);
843 ftepp->output_on = ftepp->output_on && cond.on;
846 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
847 if (!ftepp_else_allowed(ftepp))
849 if (!ftepp_ifdef(ftepp, &cond))
851 pc = &vec_last(ftepp->conditions);
852 pc->on = !pc->was_on && cond.on;
853 pc->was_on = pc->was_on || pc->on;
854 ftepp_update_output_condition(ftepp);
857 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
858 if (!ftepp_else_allowed(ftepp))
860 if (!ftepp_ifdef(ftepp, &cond))
863 pc = &vec_last(ftepp->conditions);
864 pc->on = !pc->was_on && cond.on;
865 pc->was_on = pc->was_on || pc->on;
866 ftepp_update_output_condition(ftepp);
869 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
870 if (!ftepp_else_allowed(ftepp))
872 if (!ftepp_if(ftepp, &cond))
874 pc = &vec_last(ftepp->conditions);
875 pc->on = !pc->was_on && cond.on;
876 pc->was_on = pc->was_on || pc->on;
877 ftepp_update_output_condition(ftepp);
880 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
881 if (!ftepp_if(ftepp, &cond))
883 cond.was_on = cond.on;
884 vec_push(ftepp->conditions, cond);
885 ftepp->output_on = ftepp->output_on && cond.on;
888 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
889 if (!ftepp_else_allowed(ftepp))
891 pc = &vec_last(ftepp->conditions);
892 pc->on = !pc->was_on;
895 ftepp_update_output_condition(ftepp);
898 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
899 if (!vec_size(ftepp->conditions)) {
900 ftepp_error(ftepp, "#endif without #if");
903 vec_pop(ftepp->conditions);
905 ftepp_update_output_condition(ftepp);
909 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
914 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
917 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
920 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
923 /* Builtins! Don't forget the builtins! */
925 case TOKEN_FLOATCONST:
926 ftepp_out(ftepp, "#", false);
929 if (!ftepp_skipspace(ftepp))
934 static bool ftepp_preprocess(ftepp_t *ftepp)
939 ftepp->lex->flags.preprocessing = true;
940 ftepp->lex->flags.mergelines = false;
941 ftepp->lex->flags.noops = true;
946 if (ftepp->token >= TOKEN_EOF)
949 ftepp->newline = newline;
952 /* For the sake of FTE compatibility... FU, really */
953 ftepp->newline = newline = true;
956 switch (ftepp->token) {
960 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
962 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
966 if (!ftepp_macro_call(ftepp, macro))
967 ftepp->token = TOKEN_ERROR;
970 if (!ftepp->newline) {
971 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
975 ftepp->lex->flags.mergelines = true;
976 if (ftepp_next(ftepp) >= TOKEN_EOF) {
977 ftepp_error(ftepp, "error in preprocessor directive");
978 ftepp->token = TOKEN_ERROR;
981 if (!ftepp_hash(ftepp))
982 ftepp->token = TOKEN_ERROR;
983 ftepp->lex->flags.mergelines = false;
987 ftepp_out(ftepp, "\n", true);
991 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
995 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
997 newline = ftepp->token == TOKEN_EOF;
1001 /* Like in parser.c - files keep the previous state so we have one global
1002 * preprocessor. Except here we will want to warn about dangling #ifs.
1004 static ftepp_t *ftepp;
1006 static bool ftepp_preprocess_done()
1009 lex_close(ftepp->lex);
1011 if (vec_size(ftepp->conditions)) {
1012 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1018 bool ftepp_preprocess_file(const char *filename)
1020 ftepp->lex = lex_open(filename);
1022 con_out("failed to open file \"%s\"\n", filename);
1025 if (!ftepp_preprocess(ftepp)) {
1026 ftepp_delete(ftepp);
1029 return ftepp_preprocess_done();
1032 bool ftepp_preprocess_string(const char *name, const char *str)
1034 ftepp_t *ftepp = ftepp_new();
1035 ftepp->lex = lex_open_string(str, strlen(str), name);
1037 con_out("failed to create lexer for string \"%s\"\n", name);
1040 if (!ftepp_preprocess(ftepp)) {
1041 ftepp_delete(ftepp);
1044 return ftepp_preprocess_done();
1049 ftepp = ftepp_new();
1053 const char *ftepp_get()
1055 return ftepp->output_string;
1060 vec_free(ftepp->output_string);
1067 ftepp_delete(ftepp);