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` */
61 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_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
84 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
91 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "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_cvprintmsg((void*)&ftepp->lex->tok.ctx, lvl, "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));
140 memset(macro, 0, sizeof(*macro));
141 macro->name = util_strdup(name);
145 static void ppmacro_delete(ppmacro *self)
148 for (i = 0; i < vec_size(self->params); ++i)
149 mem_d(self->params[i]);
150 vec_free(self->params);
151 for (i = 0; i < vec_size(self->output); ++i)
152 pptoken_delete(self->output[i]);
153 vec_free(self->output);
158 static ftepp_t* ftepp_new()
162 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
163 memset(ftepp, 0, sizeof(*ftepp));
165 ftepp->output_on = true;
170 static void ftepp_delete(ftepp_t *self)
174 mem_d(self->itemname);
175 if (self->includename)
176 vec_free(self->includename);
177 for (i = 0; i < vec_size(self->macros); ++i)
178 ppmacro_delete(self->macros[i]);
179 vec_free(self->macros);
180 vec_free(self->conditions);
182 lex_close(self->lex);
186 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
188 if (ignore_cond || ftepp->output_on)
193 data = vec_add(ftepp->output_string, 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 size_t l = ftepp_ctx(ftepp).line;
315 (void)ftepp_next(ftepp);
316 if (!ftepp_skipspace(ftepp))
319 switch (ftepp->token) {
323 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
324 if (macro && ftepp->output_on) {
325 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
327 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
329 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
332 ftepp_error(ftepp, "expected macro name");
336 (void)ftepp_next(ftepp);
338 if (ftepp->token == '(') {
339 macro->has_params = true;
340 if (!ftepp_define_params(ftepp, macro))
344 if (!ftepp_skipspace(ftepp))
347 if (!ftepp_define_body(ftepp, macro))
350 if (ftepp->output_on)
351 vec_push(ftepp->macros, macro);
353 ppmacro_delete(macro);
356 for (; l < ftepp_ctx(ftepp).line; ++l)
357 ftepp_out(ftepp, "\n", true);
362 * When a macro is used we have to handle parameters as well
363 * as special-concatenation via ## or stringification via #
365 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
366 * this kind of parens. Curly braces or [] don't count towards the
373 static void macroparam_clean(macroparam *self)
376 for (i = 0; i < vec_size(self->tokens); ++i)
377 pptoken_delete(self->tokens[i]);
378 vec_free(self->tokens);
381 /* need to leave the last token up */
382 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
384 macroparam *params = NULL;
390 if (!ftepp_skipallwhite(ftepp))
392 while (ftepp->token != ')') {
394 if (!ftepp_skipallwhite(ftepp))
396 while (parens || ftepp->token != ',') {
397 if (ftepp->token == '(')
399 else if (ftepp->token == ')') {
404 ptok = pptoken_make(ftepp);
405 vec_push(mp.tokens, ptok);
406 if (ftepp_next(ftepp) >= TOKEN_EOF) {
407 ftepp_error(ftepp, "unexpected EOF in macro call");
411 vec_push(params, mp);
413 if (ftepp->token == ')')
415 if (ftepp->token != ',') {
416 ftepp_error(ftepp, "expected closing paren or comma in macro call");
419 if (ftepp_next(ftepp) >= TOKEN_EOF) {
420 ftepp_error(ftepp, "unexpected EOF in macro call");
424 /* need to leave that up
425 if (ftepp_next(ftepp) >= TOKEN_EOF) {
426 ftepp_error(ftepp, "unexpected EOF in macro call");
430 *out_params = params;
435 macroparam_clean(&mp);
436 for (i = 0; i < vec_size(params); ++i)
437 macroparam_clean(¶ms[i]);
442 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
445 for (i = 0; i < vec_size(macro->params); ++i) {
446 if (!strcmp(macro->params[i], name)) {
454 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
459 switch (token->token) {
460 case TOKEN_STRINGCONST:
463 /* in preprocessor mode strings already are string,
464 * so we don't get actual newline bytes here.
465 * Still need to escape backslashes and quotes.
468 case '\\': ftepp_out(ftepp, "\\\\", false); break;
469 case '"': ftepp_out(ftepp, "\\\"", false); break;
472 ftepp_out(ftepp, chs, false);
479 ftepp_out(ftepp, " ", false);
482 ftepp_out(ftepp, "\\n", false);
485 ftepp_out(ftepp, token->value, false);
490 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
493 ftepp_out(ftepp, "\"", false);
494 for (i = 0; i < vec_size(param->tokens); ++i)
495 ftepp_stringify_token(ftepp, param->tokens[i]);
496 ftepp_out(ftepp, "\"", false);
499 static void ftepp_recursion_header(ftepp_t *ftepp)
501 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
504 static void ftepp_recursion_footer(ftepp_t *ftepp)
506 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
509 static bool ftepp_preprocess(ftepp_t *ftepp);
510 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
512 char *old_string = ftepp->output_string;
513 lex_file *old_lexer = ftepp->lex;
522 if (!vec_size(macro->output))
525 ftepp->output_string = NULL;
526 for (o = 0; o < vec_size(macro->output); ++o) {
527 pptoken *out = macro->output[o];
528 switch (out->token) {
532 if (!macro_params_find(macro, out->value, &pi)) {
533 ftepp_out(ftepp, out->value, false);
536 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
537 out = params[pi].tokens[pv];
538 if (out->token == TOKEN_EOL)
539 ftepp_out(ftepp, "\n", false);
541 ftepp_out(ftepp, out->value, false);
546 if (o + 1 < vec_size(macro->output)) {
547 nextok = macro->output[o+1]->token;
549 /* raw concatenation */
553 if ( (nextok == TOKEN_IDENT ||
554 nextok == TOKEN_KEYWORD ||
555 nextok == TOKEN_TYPENAME) &&
556 macro_params_find(macro, macro->output[o+1]->value, &pi))
559 ftepp_stringify(ftepp, ¶ms[pi]);
563 ftepp_out(ftepp, "#", false);
566 ftepp_out(ftepp, "\n", false);
569 ftepp_out(ftepp, out->value, false);
573 vec_push(ftepp->output_string, 0);
574 /* Now run the preprocessor recursively on this string buffer */
576 printf("__________\n%s\n=========\n", ftepp->output_string);
578 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
580 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
584 ftepp->output_string = old_string;
586 ftepp_recursion_header(ftepp);
587 if (!ftepp_preprocess(ftepp)) {
588 lex_close(ftepp->lex);
592 ftepp_recursion_footer(ftepp);
593 old_string = ftepp->output_string;
596 ftepp->lex = old_lexer;
597 ftepp->output_string = old_string;
601 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
604 macroparam *params = NULL;
607 if (!macro->has_params) {
608 if (!ftepp_macro_expand(ftepp, macro, NULL))
615 if (!ftepp_skipallwhite(ftepp))
618 if (ftepp->token != '(') {
619 ftepp_error(ftepp, "expected macro parameters in parenthesis");
624 if (!ftepp_macro_call_params(ftepp, ¶ms))
627 if (vec_size(params) != vec_size(macro->params)) {
628 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
629 (unsigned int)vec_size(macro->params),
630 (unsigned int)vec_size(params));
635 if (!ftepp_macro_expand(ftepp, macro, params))
640 for (o = 0; o < vec_size(params); ++o)
641 macroparam_clean(¶ms[o]);
647 * #if - the FTEQCC way:
648 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
649 * <numbers> => True if the number is not 0
650 * !<factor> => True if the factor yields false
651 * !!<factor> => ERROR on 2 or more unary nots
652 * <macro> => becomes the macro's FIRST token regardless of parameters
653 * <e> && <e> => True if both expressions are true
654 * <e> || <e> => True if either expression is true
656 * <ident> => False (remember for macros the <macro> rule applies instead)
657 * Unary + and - are weird and wrong in fteqcc so we don't allow them
658 * parenthesis in expressions are allowed
659 * parameter lists on macros are errors
660 * No mathematical calculations are executed
662 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
663 static bool ftepp_if_op(ftepp_t *ftepp)
665 ftepp->lex->flags.noops = false;
667 if (!ftepp_skipspace(ftepp))
669 ftepp->lex->flags.noops = true;
672 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
677 if (!ftepp_skipspace(ftepp))
680 while (ftepp->token == '!') {
683 if (!ftepp_skipspace(ftepp))
687 switch (ftepp->token) {
691 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
693 if (!ftepp_skipspace(ftepp))
695 if (ftepp->token != '(') {
696 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
700 if (!ftepp_skipspace(ftepp))
702 if (ftepp->token != TOKEN_IDENT &&
703 ftepp->token != TOKEN_TYPENAME &&
704 ftepp->token != TOKEN_KEYWORD)
706 ftepp_error(ftepp, "defined() used on an unexpected token type");
709 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
712 if (!ftepp_skipspace(ftepp))
714 if (ftepp->token != ')') {
715 ftepp_error(ftepp, "expected closing paren");
721 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
722 if (!macro || !vec_size(macro->output)) {
726 /* This does not expand recursively! */
727 switch (macro->output[0]->token) {
729 *value_out = macro->output[0]->constval.i;
730 *out = !!(macro->output[0]->constval.i);
732 case TOKEN_FLOATCONST:
733 *value_out = macro->output[0]->constval.f;
734 *out = !!(macro->output[0]->constval.f);
742 case TOKEN_STRINGCONST:
746 *value_out = ftepp->lex->tok.constval.i;
747 *out = !!(ftepp->lex->tok.constval.i);
749 case TOKEN_FLOATCONST:
750 *value_out = ftepp->lex->tok.constval.f;
751 *out = !!(ftepp->lex->tok.constval.f);
756 if (!ftepp_if_expr(ftepp, out, value_out))
758 if (ftepp->token != ')') {
759 ftepp_error(ftepp, "expected closing paren in #if expression");
765 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
770 *value_out = (*out ? 1 : 0);
776 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
778 if (!ftepp_next(ftepp))
780 return ftepp_if_value(ftepp, out, value_out);
784 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
786 if (!ftepp_if_value(ftepp, out, value_out))
789 if (!ftepp_if_op(ftepp))
792 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
795 /* FTEQCC is all right-associative and no precedence here */
796 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
797 !strcmp(ftepp_tokval(ftepp), "||"))
800 char opc = ftepp_tokval(ftepp)[0];
804 if (!ftepp_next(ftepp))
806 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
814 *value_out = (*out ? 1 : 0);
817 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
818 !strcmp(ftepp_tokval(ftepp), "!=") ||
819 !strcmp(ftepp_tokval(ftepp), ">=") ||
820 !strcmp(ftepp_tokval(ftepp), "<=") ||
821 !strcmp(ftepp_tokval(ftepp), ">") ||
822 !strcmp(ftepp_tokval(ftepp), "<"))
825 const char opc0 = ftepp_tokval(ftepp)[0];
826 const char opc1 = ftepp_tokval(ftepp)[1];
829 if (!ftepp_next(ftepp))
831 if (!ftepp_if_expr(ftepp, &next, &other))
835 *out = (*value_out == other);
836 else if (opc0 == '!')
837 *out = (*value_out != other);
838 else if (opc0 == '>') {
839 if (opc1 == '=') *out = (*value_out >= other);
840 else *out = (*value_out > other);
842 else if (opc0 == '<') {
843 if (opc1 == '=') *out = (*value_out <= other);
844 else *out = (*value_out < other);
846 *value_out = (*out ? 1 : 0);
851 ftepp_error(ftepp, "junk after #if");
856 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
861 memset(cond, 0, sizeof(*cond));
862 (void)ftepp_next(ftepp);
864 if (!ftepp_skipspace(ftepp))
866 if (ftepp->token == TOKEN_EOL) {
867 ftepp_error(ftepp, "expected expression for #if-directive");
871 if (!ftepp_if_expr(ftepp, &result, &dummy))
879 * ifdef is rather simple
881 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
884 memset(cond, 0, sizeof(*cond));
885 (void)ftepp_next(ftepp);
886 if (!ftepp_skipspace(ftepp))
889 switch (ftepp->token) {
893 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
896 ftepp_error(ftepp, "expected macro name");
900 (void)ftepp_next(ftepp);
901 if (!ftepp_skipspace(ftepp))
903 /* relaxing this condition
904 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
905 ftepp_error(ftepp, "stray tokens after #ifdef");
914 * undef is also simple
916 static bool ftepp_undef(ftepp_t *ftepp)
918 (void)ftepp_next(ftepp);
919 if (!ftepp_skipspace(ftepp))
922 if (ftepp->output_on) {
923 switch (ftepp->token) {
927 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
930 ftepp_error(ftepp, "expected macro name");
935 (void)ftepp_next(ftepp);
936 if (!ftepp_skipspace(ftepp))
938 /* relaxing this condition
939 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
940 ftepp_error(ftepp, "stray tokens after #ifdef");
947 /* Special unescape-string function which skips a leading quote
948 * and stops at a quote, not just at \0
950 static void unescape(const char *str, char *out) {
952 while (*str && *str != '"') {
956 case '\\': *out++ = *str; break;
957 case '"': *out++ = *str; break;
958 case 'a': *out++ = '\a'; break;
959 case 'b': *out++ = '\b'; break;
960 case 'r': *out++ = '\r'; break;
961 case 'n': *out++ = '\n'; break;
962 case 't': *out++ = '\t'; break;
963 case 'f': *out++ = '\f'; break;
964 case 'v': *out++ = '\v'; break;
979 static char *ftepp_include_find_path(const char *file, const char *pathfile)
982 char *filename = NULL;
983 const char *last_slash;
989 last_slash = strrchr(pathfile, '/');
992 len = last_slash - pathfile;
993 memcpy(vec_add(filename, len), pathfile, len);
994 vec_push(filename, '/');
998 memcpy(vec_add(filename, len+1), file, len);
999 vec_last(filename) = 0;
1001 fp = util_fopen(filename, "rb");
1010 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1012 char *filename = NULL;
1014 filename = ftepp_include_find_path(file, ftepp->includename);
1016 filename = ftepp_include_find_path(file, ftepp->itemname);
1022 * FIXME: do we need/want a -I option?
1023 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1025 static bool ftepp_include(ftepp_t *ftepp)
1027 lex_file *old_lexer = ftepp->lex;
1032 char *old_includename;
1034 (void)ftepp_next(ftepp);
1035 if (!ftepp_skipspace(ftepp))
1038 if (ftepp->token != TOKEN_STRINGCONST) {
1039 ftepp_error(ftepp, "expected filename to include");
1043 ctx = ftepp_ctx(ftepp);
1045 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1047 ftepp_out(ftepp, "\n#pragma file(", false);
1048 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1049 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1051 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1053 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1056 inlex = lex_open(filename);
1058 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1063 old_includename = ftepp->includename;
1064 ftepp->includename = filename;
1065 if (!ftepp_preprocess(ftepp)) {
1066 vec_free(ftepp->includename);
1067 ftepp->includename = old_includename;
1068 lex_close(ftepp->lex);
1069 ftepp->lex = old_lexer;
1072 vec_free(ftepp->includename);
1073 ftepp->includename = old_includename;
1074 lex_close(ftepp->lex);
1075 ftepp->lex = old_lexer;
1077 ftepp_out(ftepp, "\n#pragma file(", false);
1078 ftepp_out(ftepp, ctx.file, false);
1079 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1080 ftepp_out(ftepp, lineno, false);
1083 (void)ftepp_next(ftepp);
1084 if (!ftepp_skipspace(ftepp))
1086 if (ftepp->token != TOKEN_EOL) {
1087 ftepp_error(ftepp, "stray tokens after #include");
1090 (void)ftepp_next(ftepp);
1095 /* Basic structure handlers */
1096 static bool ftepp_else_allowed(ftepp_t *ftepp)
1098 if (!vec_size(ftepp->conditions)) {
1099 ftepp_error(ftepp, "#else without #if");
1102 if (vec_last(ftepp->conditions).had_else) {
1103 ftepp_error(ftepp, "multiple #else for a single #if");
1109 static bool ftepp_hash(ftepp_t *ftepp)
1114 lex_ctx ctx = ftepp_ctx(ftepp);
1116 if (!ftepp_skipspace(ftepp))
1119 switch (ftepp->token) {
1122 case TOKEN_TYPENAME:
1123 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1124 return ftepp_define(ftepp);
1126 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1127 return ftepp_undef(ftepp);
1129 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1130 if (!ftepp_ifdef(ftepp, &cond))
1132 cond.was_on = cond.on;
1133 vec_push(ftepp->conditions, cond);
1134 ftepp->output_on = ftepp->output_on && cond.on;
1137 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1138 if (!ftepp_ifdef(ftepp, &cond))
1141 cond.was_on = cond.on;
1142 vec_push(ftepp->conditions, cond);
1143 ftepp->output_on = ftepp->output_on && cond.on;
1146 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1147 if (!ftepp_else_allowed(ftepp))
1149 if (!ftepp_ifdef(ftepp, &cond))
1151 pc = &vec_last(ftepp->conditions);
1152 pc->on = !pc->was_on && cond.on;
1153 pc->was_on = pc->was_on || pc->on;
1154 ftepp_update_output_condition(ftepp);
1157 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1158 if (!ftepp_else_allowed(ftepp))
1160 if (!ftepp_ifdef(ftepp, &cond))
1163 pc = &vec_last(ftepp->conditions);
1164 pc->on = !pc->was_on && cond.on;
1165 pc->was_on = pc->was_on || pc->on;
1166 ftepp_update_output_condition(ftepp);
1169 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1170 if (!ftepp_else_allowed(ftepp))
1172 if (!ftepp_if(ftepp, &cond))
1174 pc = &vec_last(ftepp->conditions);
1175 pc->on = !pc->was_on && cond.on;
1176 pc->was_on = pc->was_on || pc->on;
1177 ftepp_update_output_condition(ftepp);
1180 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1181 if (!ftepp_if(ftepp, &cond))
1183 cond.was_on = cond.on;
1184 vec_push(ftepp->conditions, cond);
1185 ftepp->output_on = ftepp->output_on && cond.on;
1188 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1189 if (!ftepp_else_allowed(ftepp))
1191 pc = &vec_last(ftepp->conditions);
1192 pc->on = !pc->was_on;
1193 pc->had_else = true;
1195 ftepp_update_output_condition(ftepp);
1198 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1199 if (!vec_size(ftepp->conditions)) {
1200 ftepp_error(ftepp, "#endif without #if");
1203 vec_pop(ftepp->conditions);
1205 ftepp_update_output_condition(ftepp);
1208 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1209 return ftepp_include(ftepp);
1211 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1212 ftepp_out(ftepp, "#", false);
1216 if (ftepp->output_on) {
1217 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1224 /* break; never reached */
1226 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1229 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1232 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1235 /* Builtins! Don't forget the builtins! */
1236 case TOKEN_INTCONST:
1237 case TOKEN_FLOATCONST:
1238 ftepp_out(ftepp, "#", false);
1241 if (!ftepp_skipspace(ftepp))
1246 static bool ftepp_preprocess(ftepp_t *ftepp)
1249 bool newline = true;
1251 ftepp->lex->flags.preprocessing = true;
1252 ftepp->lex->flags.mergelines = false;
1253 ftepp->lex->flags.noops = true;
1258 if (ftepp->token >= TOKEN_EOF)
1264 switch (ftepp->token) {
1267 case TOKEN_TYPENAME:
1268 if (ftepp->output_on)
1269 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1273 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1277 if (!ftepp_macro_call(ftepp, macro))
1278 ftepp->token = TOKEN_ERROR;
1282 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1286 ftepp->lex->flags.mergelines = true;
1287 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1288 ftepp_error(ftepp, "error in preprocessor directive");
1289 ftepp->token = TOKEN_ERROR;
1292 if (!ftepp_hash(ftepp))
1293 ftepp->token = TOKEN_ERROR;
1294 ftepp->lex->flags.mergelines = false;
1298 ftepp_out(ftepp, "\n", true);
1302 /* same as default but don't set newline=false */
1303 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1308 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1312 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1314 /* force a 0 at the end but don't count it as added to the output */
1315 vec_push(ftepp->output_string, 0);
1316 vec_shrinkby(ftepp->output_string, 1);
1318 return (ftepp->token == TOKEN_EOF);
1321 /* Like in parser.c - files keep the previous state so we have one global
1322 * preprocessor. Except here we will want to warn about dangling #ifs.
1324 static ftepp_t *ftepp;
1326 static bool ftepp_preprocess_done()
1329 if (vec_size(ftepp->conditions)) {
1330 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1333 lex_close(ftepp->lex);
1335 if (ftepp->itemname) {
1336 mem_d(ftepp->itemname);
1337 ftepp->itemname = NULL;
1342 bool ftepp_preprocess_file(const char *filename)
1344 ftepp->lex = lex_open(filename);
1345 ftepp->itemname = util_strdup(filename);
1347 con_out("failed to open file \"%s\"\n", filename);
1350 if (!ftepp_preprocess(ftepp))
1352 return ftepp_preprocess_done();
1355 bool ftepp_preprocess_string(const char *name, const char *str)
1357 ftepp->lex = lex_open_string(str, strlen(str), name);
1358 ftepp->itemname = util_strdup(name);
1360 con_out("failed to create lexer for string \"%s\"\n", name);
1363 if (!ftepp_preprocess(ftepp))
1365 return ftepp_preprocess_done();
1370 ftepp = ftepp_new();
1374 /* set the right macro based on the selected standard */
1375 ftepp_add_define(NULL, "GMQCC");
1376 if (opts_standard == COMPILER_FTEQCC)
1377 ftepp_add_define(NULL, "__STD_FTEQCC__");
1378 else if (opts_standard == COMPILER_GMQCC)
1379 ftepp_add_define(NULL, "__STD_GMQCC__");
1380 else if (opts_standard == COMPILER_QCC)
1381 ftepp_add_define(NULL, "__STD_QCC__");
1386 void ftepp_add_define(const char *source, const char *name)
1389 lex_ctx ctx = { "__builtin__", 0 };
1391 macro = ppmacro_new(ctx, name);
1392 vec_push(ftepp->macros, macro);
1395 const char *ftepp_get()
1397 return ftepp->output_string;
1402 vec_free(ftepp->output_string);
1409 ftepp_delete(ftepp);