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)
171 for (i = 0; i < vec_size(self->macros); ++i)
172 ppmacro_delete(self->macros[i]);
173 vec_free(self->macros);
174 vec_free(self->conditions);
176 lex_close(self->lex);
177 if (self->output_file)
178 fclose(self->output_file);
182 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
184 if (ignore_cond || ftepp->output_on)
188 if (!ftepp->to_string) {
189 fprintf((ftepp->output_file ? ftepp->output_file : stdout), "%s", str);
193 data = vec_add(ftepp->output, len);
194 memcpy(data, str, len);
198 static void ftepp_update_output_condition(ftepp_t *ftepp)
201 ftepp->output_on = true;
202 for (i = 0; i < vec_size(ftepp->conditions); ++i)
203 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
206 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
209 for (i = 0; i < vec_size(ftepp->macros); ++i) {
210 if (!strcmp(name, ftepp->macros[i]->name))
211 return ftepp->macros[i];
216 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
219 for (i = 0; i < vec_size(ftepp->macros); ++i) {
220 if (!strcmp(name, ftepp->macros[i]->name)) {
221 vec_remove(ftepp->macros, i, 1);
227 static inline int ftepp_next(ftepp_t *ftepp)
229 return (ftepp->token = lex_do(ftepp->lex));
232 /* Important: this does not skip newlines! */
233 static bool ftepp_skipspace(ftepp_t *ftepp)
235 if (ftepp->token != TOKEN_WHITE)
237 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
238 if (ftepp->token >= TOKEN_EOF) {
239 ftepp_error(ftepp, "unexpected end of preprocessor directive");
245 /* this one skips EOLs as well */
246 static bool ftepp_skipallwhite(ftepp_t *ftepp)
248 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
252 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
253 if (ftepp->token >= TOKEN_EOF) {
254 ftepp_error(ftepp, "unexpected end of preprocessor directive");
261 * The huge macro parsing code...
263 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
267 if (!ftepp_skipspace(ftepp))
269 if (ftepp->token == ')')
271 switch (ftepp->token) {
277 ftepp_error(ftepp, "unexpected token in parameter list");
280 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
282 if (!ftepp_skipspace(ftepp))
284 } while (ftepp->token == ',');
285 if (ftepp->token != ')') {
286 ftepp_error(ftepp, "expected closing paren after macro parameter list");
290 /* skipspace happens in ftepp_define */
294 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
297 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
298 ptok = pptoken_make(ftepp);
299 vec_push(macro->output, ptok);
302 /* recursive expansion can cause EOFs here */
303 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
304 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
310 static bool ftepp_define(ftepp_t *ftepp)
313 (void)ftepp_next(ftepp);
314 if (!ftepp_skipspace(ftepp))
317 switch (ftepp->token) {
321 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
322 if (macro && ftepp->output_on) {
323 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
325 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
327 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
330 ftepp_error(ftepp, "expected macro name");
334 (void)ftepp_next(ftepp);
336 if (ftepp->token == '(') {
337 macro->has_params = true;
338 if (!ftepp_define_params(ftepp, macro))
342 if (!ftepp_skipspace(ftepp))
345 if (!ftepp_define_body(ftepp, macro))
348 if (ftepp->output_on)
349 vec_push(ftepp->macros, macro);
351 ppmacro_delete(macro);
357 * When a macro is used we have to handle parameters as well
358 * as special-concatenation via ## or stringification via #
360 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
361 * this kind of parens. Curly braces or [] don't count towards the
368 static void macroparam_clean(macroparam *self)
371 for (i = 0; i < vec_size(self->tokens); ++i)
372 pptoken_delete(self->tokens[i]);
373 vec_free(self->tokens);
376 /* need to leave the last token up */
377 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
379 macroparam *params = NULL;
385 if (!ftepp_skipallwhite(ftepp))
387 while (ftepp->token != ')') {
389 if (!ftepp_skipallwhite(ftepp))
391 while (parens || ftepp->token != ',') {
392 if (ftepp->token == '(')
394 else if (ftepp->token == ')') {
399 ptok = pptoken_make(ftepp);
400 vec_push(mp.tokens, ptok);
401 if (ftepp_next(ftepp) >= TOKEN_EOF) {
402 ftepp_error(ftepp, "unexpected EOF in macro call");
406 vec_push(params, mp);
408 if (ftepp->token == ')')
410 if (ftepp->token != ',') {
411 ftepp_error(ftepp, "expected closing paren or comma in macro call");
414 if (ftepp_next(ftepp) >= TOKEN_EOF) {
415 ftepp_error(ftepp, "unexpected EOF in macro call");
419 /* need to leave that up
420 if (ftepp_next(ftepp) >= TOKEN_EOF) {
421 ftepp_error(ftepp, "unexpected EOF in macro call");
425 *out_params = params;
430 macroparam_clean(&mp);
431 for (i = 0; i < vec_size(params); ++i)
432 macroparam_clean(¶ms[i]);
437 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
440 for (i = 0; i < vec_size(macro->params); ++i) {
441 if (!strcmp(macro->params[i], name)) {
449 static bool ftepp_preprocess(ftepp_t *ftepp);
450 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
452 char *old_string = ftepp->output;
453 bool old_string_flag = ftepp->to_string;
454 lex_file *old_lexer = ftepp->lex;
461 if (!vec_size(macro->output))
464 ftepp->output = NULL;
465 ftepp->to_string = true;
466 for (o = 0; o < vec_size(macro->output); ++o) {
467 pptoken *out = macro->output[o];
468 switch (out->token) {
472 if (!macro_params_find(macro, out->value, &pi)) {
473 ftepp_out(ftepp, out->value, false);
476 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
477 out = params[pi].tokens[pv];
478 if (out->token == TOKEN_EOL)
479 ftepp_out(ftepp, "\n", false);
481 ftepp_out(ftepp, out->value, false);
486 if (o + 1 < vec_size(macro->output) && macro->output[o+1]->token == '#') {
487 /* raw concatenation */
491 ftepp_out(ftepp, "#", false);
494 ftepp_out(ftepp, "\n", false);
497 ftepp_out(ftepp, out->value, false);
501 vec_push(ftepp->output, 0);
502 /* Now run the preprocessor recursively on this string buffer */
504 printf("__________\n%s\n=========\n", ftepp->output);
506 inlex = lex_open_string(ftepp->output, vec_size(ftepp->output)-1, ftepp->lex->name);
508 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
512 ftepp->output = old_string;
513 ftepp->to_string = old_string_flag;
515 if (!ftepp_preprocess(ftepp)) {
516 lex_close(ftepp->lex);
522 ftepp->lex = old_lexer;
523 ftepp->output = old_string;
524 ftepp->to_string = old_string_flag;
528 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
531 macroparam *params = NULL;
534 if (!macro->has_params) {
535 if (!ftepp_macro_expand(ftepp, macro, NULL))
542 if (!ftepp_skipallwhite(ftepp))
545 if (ftepp->token != '(') {
546 ftepp_error(ftepp, "expected macro parameters in parenthesis");
551 if (!ftepp_macro_call_params(ftepp, ¶ms))
554 if (vec_size(params) != vec_size(macro->params)) {
555 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
556 (unsigned int)vec_size(macro->params),
557 (unsigned int)vec_size(params));
562 if (!ftepp_macro_expand(ftepp, macro, params))
567 for (o = 0; o < vec_size(params); ++o)
568 macroparam_clean(¶ms[o]);
574 * #if - the FTEQCC way:
575 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
576 * <numbers> => True if the number is not 0
577 * !<factor> => True if the factor yields false
578 * !!<factor> => ERROR on 2 or more unary nots
579 * <macro> => becomes the macro's FIRST token regardless of parameters
580 * <e> && <e> => True if both expressions are true
581 * <e> || <e> => True if either expression is true
583 * <ident> => False (remember for macros the <macro> rule applies instead)
584 * Unary + and - are weird and wrong in fteqcc so we don't allow them
585 * parenthesis in expressions are allowed
586 * parameter lists on macros are errors
587 * No mathematical calculations are executed
589 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
594 if (!ftepp_skipspace(ftepp))
597 while (ftepp->token == '!') {
600 if (!ftepp_skipspace(ftepp))
604 switch (ftepp->token) {
608 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
610 if (!ftepp_skipspace(ftepp))
612 if (ftepp->token != '(') {
613 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
617 if (!ftepp_skipspace(ftepp))
619 if (ftepp->token != TOKEN_IDENT &&
620 ftepp->token != TOKEN_TYPENAME &&
621 ftepp->token != TOKEN_KEYWORD)
623 ftepp_error(ftepp, "defined() used on an unexpected token type");
626 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
629 if (!ftepp_skipspace(ftepp))
631 if (ftepp->token != ')') {
632 ftepp_error(ftepp, "expected closing paren");
638 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
639 if (!macro || !vec_size(macro->output)) {
642 /* This does not expand recursively! */
643 switch (macro->output[0]->token) {
645 *out = !!(macro->output[0]->constval.f);
647 case TOKEN_FLOATCONST:
648 *out = !!(macro->output[0]->constval.f);
656 case TOKEN_STRINGCONST:
660 *out = !!(ftepp->lex->tok.constval.i);
662 case TOKEN_FLOATCONST:
663 *out = !!(ftepp->lex->tok.constval.f);
668 if (!ftepp_if_expr(ftepp, out))
670 if (ftepp->token != ')') {
671 ftepp_error(ftepp, "expected closing paren in #if expression");
677 ftepp_error(ftepp, "junk in #if");
683 ftepp->lex->flags.noops = false;
685 if (!ftepp_skipspace(ftepp))
687 ftepp->lex->flags.noops = true;
689 if (ftepp->token == ')')
692 if (ftepp->token != TOKEN_OPERATOR)
695 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
696 !strcmp(ftepp_tokval(ftepp), "||"))
699 char opc = ftepp_tokval(ftepp)[0];
702 if (!ftepp_if_expr(ftepp, &next))
712 ftepp_error(ftepp, "junk after #if");
717 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
721 memset(cond, 0, sizeof(*cond));
722 (void)ftepp_next(ftepp);
724 if (!ftepp_skipspace(ftepp))
726 if (ftepp->token == TOKEN_EOL) {
727 ftepp_error(ftepp, "expected expression for #if-directive");
731 if (!ftepp_if_expr(ftepp, &result))
739 * ifdef is rather simple
741 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
744 memset(cond, 0, sizeof(*cond));
745 (void)ftepp_next(ftepp);
746 if (!ftepp_skipspace(ftepp))
749 switch (ftepp->token) {
753 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
756 ftepp_error(ftepp, "expected macro name");
760 (void)ftepp_next(ftepp);
761 if (!ftepp_skipspace(ftepp))
763 /* relaxing this condition
764 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
765 ftepp_error(ftepp, "stray tokens after #ifdef");
774 * undef is also simple
776 static bool ftepp_undef(ftepp_t *ftepp)
778 (void)ftepp_next(ftepp);
779 if (!ftepp_skipspace(ftepp))
782 if (ftepp->output_on) {
783 switch (ftepp->token) {
787 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
790 ftepp_error(ftepp, "expected macro name");
795 (void)ftepp_next(ftepp);
796 if (!ftepp_skipspace(ftepp))
798 /* relaxing this condition
799 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
800 ftepp_error(ftepp, "stray tokens after #ifdef");
807 /* Basic structure handlers */
808 static bool ftepp_else_allowed(ftepp_t *ftepp)
810 if (!vec_size(ftepp->conditions)) {
811 ftepp_error(ftepp, "#else without #if");
814 if (vec_last(ftepp->conditions).had_else) {
815 ftepp_error(ftepp, "multiple #else for a single #if");
821 static bool ftepp_hash(ftepp_t *ftepp)
826 lex_ctx ctx = ftepp_ctx(ftepp);
828 if (!ftepp_skipspace(ftepp))
831 switch (ftepp->token) {
835 if (!strcmp(ftepp_tokval(ftepp), "define")) {
836 return ftepp_define(ftepp);
838 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
839 return ftepp_undef(ftepp);
841 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
842 if (!ftepp_ifdef(ftepp, &cond))
844 cond.was_on = cond.on;
845 vec_push(ftepp->conditions, cond);
846 ftepp->output_on = ftepp->output_on && cond.on;
849 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
850 if (!ftepp_ifdef(ftepp, &cond))
853 cond.was_on = cond.on;
854 vec_push(ftepp->conditions, cond);
855 ftepp->output_on = ftepp->output_on && cond.on;
858 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
859 if (!ftepp_else_allowed(ftepp))
861 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), "elifndef")) {
870 if (!ftepp_else_allowed(ftepp))
872 if (!ftepp_ifdef(ftepp, &cond))
875 pc = &vec_last(ftepp->conditions);
876 pc->on = !pc->was_on && cond.on;
877 pc->was_on = pc->was_on || pc->on;
878 ftepp_update_output_condition(ftepp);
881 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
882 if (!ftepp_else_allowed(ftepp))
884 if (!ftepp_if(ftepp, &cond))
886 pc = &vec_last(ftepp->conditions);
887 pc->on = !pc->was_on && cond.on;
888 pc->was_on = pc->was_on || pc->on;
889 ftepp_update_output_condition(ftepp);
892 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
893 if (!ftepp_if(ftepp, &cond))
895 cond.was_on = cond.on;
896 vec_push(ftepp->conditions, cond);
897 ftepp->output_on = ftepp->output_on && cond.on;
900 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
901 if (!ftepp_else_allowed(ftepp))
903 pc = &vec_last(ftepp->conditions);
904 pc->on = !pc->was_on;
907 ftepp_update_output_condition(ftepp);
910 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
911 if (!vec_size(ftepp->conditions)) {
912 ftepp_error(ftepp, "#endif without #if");
915 vec_pop(ftepp->conditions);
917 ftepp_update_output_condition(ftepp);
921 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
926 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
929 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
932 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
935 /* Builtins! Don't forget the builtins! */
937 case TOKEN_FLOATCONST:
938 ftepp_out(ftepp, "#", false);
941 if (!ftepp_skipspace(ftepp))
946 static bool ftepp_preprocess(ftepp_t *ftepp)
951 ftepp->lex->flags.preprocessing = true;
952 ftepp->lex->flags.mergelines = false;
953 ftepp->lex->flags.noops = true;
958 if (ftepp->token >= TOKEN_EOF)
961 ftepp->newline = newline;
964 /* For the sake of FTE compatibility... FU, really */
965 ftepp->newline = newline = true;
968 switch (ftepp->token) {
972 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
974 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
978 if (!ftepp_macro_call(ftepp, macro))
979 ftepp->token = TOKEN_ERROR;
982 if (!ftepp->newline) {
983 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
987 ftepp->lex->flags.mergelines = true;
988 if (ftepp_next(ftepp) >= TOKEN_EOF) {
989 ftepp_error(ftepp, "error in preprocessor directive");
990 ftepp->token = TOKEN_ERROR;
993 if (!ftepp_hash(ftepp))
994 ftepp->token = TOKEN_ERROR;
995 ftepp->lex->flags.mergelines = false;
999 ftepp_out(ftepp, "\n", true);
1003 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1007 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1009 newline = ftepp->token == TOKEN_EOF;
1013 /* Like in parser.c - files keep the previous state so we have one global
1014 * preprocessor. Except here we will want to warn about dangling #ifs.
1016 static ftepp_t *ftepp;
1018 static bool ftepp_preprocess_done()
1021 lex_close(ftepp->lex);
1023 if (vec_size(ftepp->conditions)) {
1024 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1030 bool ftepp_preprocess_file(const char *filename)
1032 ftepp->lex = lex_open(filename);
1034 con_out("failed to open file \"%s\"\n", filename);
1037 if (!ftepp_preprocess(ftepp)) {
1038 ftepp_delete(ftepp);
1041 return ftepp_preprocess_done();
1044 bool ftepp_preprocess_string(const char *name, const char *str)
1046 ftepp_t *ftepp = ftepp_new();
1047 ftepp->lex = lex_open_string(str, strlen(str), name);
1049 con_out("failed to create lexer for string \"%s\"\n", name);
1052 if (!ftepp_preprocess(ftepp)) {
1053 ftepp_delete(ftepp);
1056 return ftepp_preprocess_done();
1059 bool ftepp_init(FILE *out)
1061 ftepp = ftepp_new();
1062 ftepp->output_file = out;
1070 ftepp_delete(ftepp);