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;
71 #define ftepp_tokval(f) ((f)->lex->tok.value)
72 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
74 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
81 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
85 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
92 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
96 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
99 int lvl = LVL_WARNING;
101 if (!OPTS_WARN(warntype))
110 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, lvl, "error", fmt, ap);
115 static pptoken *pptoken_make(ftepp_t *ftepp)
117 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
118 token->token = ftepp->token;
120 if (token->token == TOKEN_WHITE)
121 token->value = util_strdup(" ");
124 token->value = util_strdup(ftepp_tokval(ftepp));
126 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
130 static void pptoken_delete(pptoken *self)
136 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
138 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
141 memset(macro, 0, sizeof(*macro));
142 macro->name = util_strdup(name);
146 static void ppmacro_delete(ppmacro *self)
149 for (i = 0; i < vec_size(self->params); ++i)
150 mem_d(self->params[i]);
151 vec_free(self->params);
152 for (i = 0; i < vec_size(self->output); ++i)
153 pptoken_delete(self->output[i]);
154 vec_free(self->output);
159 static ftepp_t* ftepp_new()
163 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
164 memset(ftepp, 0, sizeof(*ftepp));
166 ftepp->output_on = true;
171 static void ftepp_delete(ftepp_t *self)
175 mem_d(self->itemname);
176 if (self->includename)
177 vec_free(self->includename);
178 for (i = 0; i < vec_size(self->macros); ++i)
179 ppmacro_delete(self->macros[i]);
180 vec_free(self->macros);
181 vec_free(self->conditions);
183 lex_close(self->lex);
187 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
189 if (ignore_cond || ftepp->output_on)
194 data = vec_add(ftepp->output_string, len);
195 memcpy(data, str, len);
199 static void ftepp_update_output_condition(ftepp_t *ftepp)
202 ftepp->output_on = true;
203 for (i = 0; i < vec_size(ftepp->conditions); ++i)
204 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
207 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
210 for (i = 0; i < vec_size(ftepp->macros); ++i) {
211 if (!strcmp(name, ftepp->macros[i]->name))
212 return ftepp->macros[i];
217 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
220 for (i = 0; i < vec_size(ftepp->macros); ++i) {
221 if (!strcmp(name, ftepp->macros[i]->name)) {
222 vec_remove(ftepp->macros, i, 1);
228 static inline int ftepp_next(ftepp_t *ftepp)
230 return (ftepp->token = lex_do(ftepp->lex));
233 /* Important: this does not skip newlines! */
234 static bool ftepp_skipspace(ftepp_t *ftepp)
236 if (ftepp->token != TOKEN_WHITE)
238 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
239 if (ftepp->token >= TOKEN_EOF) {
240 ftepp_error(ftepp, "unexpected end of preprocessor directive");
246 /* this one skips EOLs as well */
247 static bool ftepp_skipallwhite(ftepp_t *ftepp)
249 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
253 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
254 if (ftepp->token >= TOKEN_EOF) {
255 ftepp_error(ftepp, "unexpected end of preprocessor directive");
262 * The huge macro parsing code...
264 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
268 if (!ftepp_skipspace(ftepp))
270 if (ftepp->token == ')')
272 switch (ftepp->token) {
278 ftepp_error(ftepp, "unexpected token in parameter list");
281 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
283 if (!ftepp_skipspace(ftepp))
285 } while (ftepp->token == ',');
286 if (ftepp->token != ')') {
287 ftepp_error(ftepp, "expected closing paren after macro parameter list");
291 /* skipspace happens in ftepp_define */
295 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
298 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
299 ptok = pptoken_make(ftepp);
300 vec_push(macro->output, ptok);
303 /* recursive expansion can cause EOFs here */
304 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
305 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
311 static bool ftepp_define(ftepp_t *ftepp)
314 size_t l = ftepp_ctx(ftepp).line;
316 (void)ftepp_next(ftepp);
317 if (!ftepp_skipspace(ftepp))
320 switch (ftepp->token) {
324 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
325 if (macro && ftepp->output_on) {
326 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
328 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
330 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
333 ftepp_error(ftepp, "expected macro name");
337 (void)ftepp_next(ftepp);
339 if (ftepp->token == '(') {
340 macro->has_params = true;
341 if (!ftepp_define_params(ftepp, macro))
345 if (!ftepp_skipspace(ftepp))
348 if (!ftepp_define_body(ftepp, macro))
351 if (ftepp->output_on)
352 vec_push(ftepp->macros, macro);
354 ppmacro_delete(macro);
357 for (; l < ftepp_ctx(ftepp).line; ++l)
358 ftepp_out(ftepp, "\n", true);
363 * When a macro is used we have to handle parameters as well
364 * as special-concatenation via ## or stringification via #
366 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
367 * this kind of parens. Curly braces or [] don't count towards the
374 static void macroparam_clean(macroparam *self)
377 for (i = 0; i < vec_size(self->tokens); ++i)
378 pptoken_delete(self->tokens[i]);
379 vec_free(self->tokens);
382 /* need to leave the last token up */
383 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
385 macroparam *params = NULL;
391 if (!ftepp_skipallwhite(ftepp))
393 while (ftepp->token != ')') {
395 if (!ftepp_skipallwhite(ftepp))
397 while (parens || ftepp->token != ',') {
398 if (ftepp->token == '(')
400 else if (ftepp->token == ')') {
405 ptok = pptoken_make(ftepp);
406 vec_push(mp.tokens, ptok);
407 if (ftepp_next(ftepp) >= TOKEN_EOF) {
408 ftepp_error(ftepp, "unexpected EOF in macro call");
412 vec_push(params, mp);
414 if (ftepp->token == ')')
416 if (ftepp->token != ',') {
417 ftepp_error(ftepp, "expected closing paren or comma in macro call");
420 if (ftepp_next(ftepp) >= TOKEN_EOF) {
421 ftepp_error(ftepp, "unexpected EOF in macro call");
425 /* need to leave that up
426 if (ftepp_next(ftepp) >= TOKEN_EOF) {
427 ftepp_error(ftepp, "unexpected EOF in macro call");
431 *out_params = params;
436 macroparam_clean(&mp);
437 for (i = 0; i < vec_size(params); ++i)
438 macroparam_clean(¶ms[i]);
443 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
446 for (i = 0; i < vec_size(macro->params); ++i) {
447 if (!strcmp(macro->params[i], name)) {
455 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
460 switch (token->token) {
461 case TOKEN_STRINGCONST:
464 /* in preprocessor mode strings already are string,
465 * so we don't get actual newline bytes here.
466 * Still need to escape backslashes and quotes.
469 case '\\': ftepp_out(ftepp, "\\\\", false); break;
470 case '"': ftepp_out(ftepp, "\\\"", false); break;
473 ftepp_out(ftepp, chs, false);
480 ftepp_out(ftepp, " ", false);
483 ftepp_out(ftepp, "\\n", false);
486 ftepp_out(ftepp, token->value, false);
491 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
494 ftepp_out(ftepp, "\"", false);
495 for (i = 0; i < vec_size(param->tokens); ++i)
496 ftepp_stringify_token(ftepp, param->tokens[i]);
497 ftepp_out(ftepp, "\"", false);
500 static void ftepp_recursion_header(ftepp_t *ftepp)
502 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
505 static void ftepp_recursion_footer(ftepp_t *ftepp)
507 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
510 static bool ftepp_preprocess(ftepp_t *ftepp);
511 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
513 char *old_string = ftepp->output_string;
514 lex_file *old_lexer = ftepp->lex;
523 if (!vec_size(macro->output))
526 ftepp->output_string = NULL;
527 for (o = 0; o < vec_size(macro->output); ++o) {
528 pptoken *out = macro->output[o];
529 switch (out->token) {
533 if (!macro_params_find(macro, out->value, &pi)) {
534 ftepp_out(ftepp, out->value, false);
537 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
538 out = params[pi].tokens[pv];
539 if (out->token == TOKEN_EOL)
540 ftepp_out(ftepp, "\n", false);
542 ftepp_out(ftepp, out->value, false);
547 if (o + 1 < vec_size(macro->output)) {
548 nextok = macro->output[o+1]->token;
550 /* raw concatenation */
554 if ( (nextok == TOKEN_IDENT ||
555 nextok == TOKEN_KEYWORD ||
556 nextok == TOKEN_TYPENAME) &&
557 macro_params_find(macro, macro->output[o+1]->value, &pi))
560 ftepp_stringify(ftepp, ¶ms[pi]);
564 ftepp_out(ftepp, "#", false);
567 ftepp_out(ftepp, "\n", false);
570 ftepp_out(ftepp, out->value, false);
574 vec_push(ftepp->output_string, 0);
575 /* Now run the preprocessor recursively on this string buffer */
577 printf("__________\n%s\n=========\n", ftepp->output_string);
579 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
581 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
585 ftepp->output_string = old_string;
587 ftepp_recursion_header(ftepp);
588 if (!ftepp_preprocess(ftepp)) {
589 lex_close(ftepp->lex);
593 ftepp_recursion_footer(ftepp);
594 old_string = ftepp->output_string;
597 ftepp->lex = old_lexer;
598 ftepp->output_string = old_string;
602 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
605 macroparam *params = NULL;
608 if (!macro->has_params) {
609 if (!ftepp_macro_expand(ftepp, macro, NULL))
616 if (!ftepp_skipallwhite(ftepp))
619 if (ftepp->token != '(') {
620 ftepp_error(ftepp, "expected macro parameters in parenthesis");
625 if (!ftepp_macro_call_params(ftepp, ¶ms))
628 if (vec_size(params) != vec_size(macro->params)) {
629 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
630 (unsigned int)vec_size(macro->params),
631 (unsigned int)vec_size(params));
636 if (!ftepp_macro_expand(ftepp, macro, params))
641 for (o = 0; o < vec_size(params); ++o)
642 macroparam_clean(¶ms[o]);
648 * #if - the FTEQCC way:
649 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
650 * <numbers> => True if the number is not 0
651 * !<factor> => True if the factor yields false
652 * !!<factor> => ERROR on 2 or more unary nots
653 * <macro> => becomes the macro's FIRST token regardless of parameters
654 * <e> && <e> => True if both expressions are true
655 * <e> || <e> => True if either expression is true
657 * <ident> => False (remember for macros the <macro> rule applies instead)
658 * Unary + and - are weird and wrong in fteqcc so we don't allow them
659 * parenthesis in expressions are allowed
660 * parameter lists on macros are errors
661 * No mathematical calculations are executed
663 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
664 static bool ftepp_if_op(ftepp_t *ftepp)
666 ftepp->lex->flags.noops = false;
668 if (!ftepp_skipspace(ftepp))
670 ftepp->lex->flags.noops = true;
673 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
678 if (!ftepp_skipspace(ftepp))
681 while (ftepp->token == '!') {
684 if (!ftepp_skipspace(ftepp))
688 switch (ftepp->token) {
692 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
694 if (!ftepp_skipspace(ftepp))
696 if (ftepp->token != '(') {
697 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
701 if (!ftepp_skipspace(ftepp))
703 if (ftepp->token != TOKEN_IDENT &&
704 ftepp->token != TOKEN_TYPENAME &&
705 ftepp->token != TOKEN_KEYWORD)
707 ftepp_error(ftepp, "defined() used on an unexpected token type");
710 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
713 if (!ftepp_skipspace(ftepp))
715 if (ftepp->token != ')') {
716 ftepp_error(ftepp, "expected closing paren");
722 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
723 if (!macro || !vec_size(macro->output)) {
727 /* This does not expand recursively! */
728 switch (macro->output[0]->token) {
730 *value_out = macro->output[0]->constval.i;
731 *out = !!(macro->output[0]->constval.i);
733 case TOKEN_FLOATCONST:
734 *value_out = macro->output[0]->constval.f;
735 *out = !!(macro->output[0]->constval.f);
743 case TOKEN_STRINGCONST:
747 *value_out = ftepp->lex->tok.constval.i;
748 *out = !!(ftepp->lex->tok.constval.i);
750 case TOKEN_FLOATCONST:
751 *value_out = ftepp->lex->tok.constval.f;
752 *out = !!(ftepp->lex->tok.constval.f);
757 if (!ftepp_if_expr(ftepp, out, value_out))
759 if (ftepp->token != ')') {
760 ftepp_error(ftepp, "expected closing paren in #if expression");
766 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
771 *value_out = (*out ? 1 : 0);
777 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
779 if (!ftepp_next(ftepp))
781 return ftepp_if_value(ftepp, out, value_out);
785 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
787 if (!ftepp_if_value(ftepp, out, value_out))
790 if (!ftepp_if_op(ftepp))
793 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
796 /* FTEQCC is all right-associative and no precedence here */
797 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
798 !strcmp(ftepp_tokval(ftepp), "||"))
801 char opc = ftepp_tokval(ftepp)[0];
805 if (!ftepp_next(ftepp))
807 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
815 *value_out = (*out ? 1 : 0);
818 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
819 !strcmp(ftepp_tokval(ftepp), "!=") ||
820 !strcmp(ftepp_tokval(ftepp), ">=") ||
821 !strcmp(ftepp_tokval(ftepp), "<=") ||
822 !strcmp(ftepp_tokval(ftepp), ">") ||
823 !strcmp(ftepp_tokval(ftepp), "<"))
826 const char opc0 = ftepp_tokval(ftepp)[0];
827 const char opc1 = ftepp_tokval(ftepp)[1];
830 if (!ftepp_next(ftepp))
832 if (!ftepp_if_expr(ftepp, &next, &other))
836 *out = (*value_out == other);
837 else if (opc0 == '!')
838 *out = (*value_out != other);
839 else if (opc0 == '>') {
840 if (opc1 == '=') *out = (*value_out >= other);
841 else *out = (*value_out > other);
843 else if (opc0 == '<') {
844 if (opc1 == '=') *out = (*value_out <= other);
845 else *out = (*value_out < other);
847 *value_out = (*out ? 1 : 0);
852 ftepp_error(ftepp, "junk after #if");
857 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
862 memset(cond, 0, sizeof(*cond));
863 (void)ftepp_next(ftepp);
865 if (!ftepp_skipspace(ftepp))
867 if (ftepp->token == TOKEN_EOL) {
868 ftepp_error(ftepp, "expected expression for #if-directive");
872 if (!ftepp_if_expr(ftepp, &result, &dummy))
880 * ifdef is rather simple
882 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
885 memset(cond, 0, sizeof(*cond));
886 (void)ftepp_next(ftepp);
887 if (!ftepp_skipspace(ftepp))
890 switch (ftepp->token) {
894 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
897 ftepp_error(ftepp, "expected macro name");
901 (void)ftepp_next(ftepp);
902 if (!ftepp_skipspace(ftepp))
904 /* relaxing this condition
905 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
906 ftepp_error(ftepp, "stray tokens after #ifdef");
915 * undef is also simple
917 static bool ftepp_undef(ftepp_t *ftepp)
919 (void)ftepp_next(ftepp);
920 if (!ftepp_skipspace(ftepp))
923 if (ftepp->output_on) {
924 switch (ftepp->token) {
928 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
931 ftepp_error(ftepp, "expected macro name");
936 (void)ftepp_next(ftepp);
937 if (!ftepp_skipspace(ftepp))
939 /* relaxing this condition
940 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
941 ftepp_error(ftepp, "stray tokens after #ifdef");
948 /* Special unescape-string function which skips a leading quote
949 * and stops at a quote, not just at \0
951 static void unescape(const char *str, char *out) {
953 while (*str && *str != '"') {
957 case '\\': *out++ = *str; break;
958 case '"': *out++ = *str; break;
959 case 'a': *out++ = '\a'; break;
960 case 'b': *out++ = '\b'; break;
961 case 'r': *out++ = '\r'; break;
962 case 'n': *out++ = '\n'; break;
963 case 't': *out++ = '\t'; break;
964 case 'f': *out++ = '\f'; break;
965 case 'v': *out++ = '\v'; break;
980 static char *ftepp_include_find_path(const char *file, const char *pathfile)
983 char *filename = NULL;
984 const char *last_slash;
990 last_slash = strrchr(pathfile, '/');
993 len = last_slash - pathfile;
994 memcpy(vec_add(filename, len), pathfile, len);
995 vec_push(filename, '/');
999 memcpy(vec_add(filename, len+1), file, len);
1000 vec_last(filename) = 0;
1002 fp = util_fopen(filename, "rb");
1011 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1013 char *filename = NULL;
1015 filename = ftepp_include_find_path(file, ftepp->includename);
1017 filename = ftepp_include_find_path(file, ftepp->itemname);
1023 * FIXME: do we need/want a -I option?
1024 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1026 static bool ftepp_include(ftepp_t *ftepp)
1028 lex_file *old_lexer = ftepp->lex;
1033 char *old_includename;
1035 (void)ftepp_next(ftepp);
1036 if (!ftepp_skipspace(ftepp))
1039 if (ftepp->token != TOKEN_STRINGCONST) {
1040 ftepp_error(ftepp, "expected filename to include");
1044 ctx = ftepp_ctx(ftepp);
1046 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1048 ftepp_out(ftepp, "\n#pragma file(", false);
1049 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1050 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1052 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1054 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1057 inlex = lex_open(filename);
1059 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1064 old_includename = ftepp->includename;
1065 ftepp->includename = filename;
1066 if (!ftepp_preprocess(ftepp)) {
1067 vec_free(ftepp->includename);
1068 ftepp->includename = old_includename;
1069 lex_close(ftepp->lex);
1070 ftepp->lex = old_lexer;
1073 vec_free(ftepp->includename);
1074 ftepp->includename = old_includename;
1075 lex_close(ftepp->lex);
1076 ftepp->lex = old_lexer;
1078 ftepp_out(ftepp, "\n#pragma file(", false);
1079 ftepp_out(ftepp, ctx.file, false);
1080 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1081 ftepp_out(ftepp, lineno, false);
1084 (void)ftepp_next(ftepp);
1085 if (!ftepp_skipspace(ftepp))
1087 if (ftepp->token != TOKEN_EOL) {
1088 ftepp_error(ftepp, "stray tokens after #include");
1091 (void)ftepp_next(ftepp);
1096 /* Basic structure handlers */
1097 static bool ftepp_else_allowed(ftepp_t *ftepp)
1099 if (!vec_size(ftepp->conditions)) {
1100 ftepp_error(ftepp, "#else without #if");
1103 if (vec_last(ftepp->conditions).had_else) {
1104 ftepp_error(ftepp, "multiple #else for a single #if");
1110 static bool ftepp_hash(ftepp_t *ftepp)
1115 lex_ctx ctx = ftepp_ctx(ftepp);
1117 if (!ftepp_skipspace(ftepp))
1120 switch (ftepp->token) {
1123 case TOKEN_TYPENAME:
1124 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1125 return ftepp_define(ftepp);
1127 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1128 return ftepp_undef(ftepp);
1130 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1131 if (!ftepp_ifdef(ftepp, &cond))
1133 cond.was_on = cond.on;
1134 vec_push(ftepp->conditions, cond);
1135 ftepp->output_on = ftepp->output_on && cond.on;
1138 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1139 if (!ftepp_ifdef(ftepp, &cond))
1142 cond.was_on = cond.on;
1143 vec_push(ftepp->conditions, cond);
1144 ftepp->output_on = ftepp->output_on && cond.on;
1147 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1148 if (!ftepp_else_allowed(ftepp))
1150 if (!ftepp_ifdef(ftepp, &cond))
1152 pc = &vec_last(ftepp->conditions);
1153 pc->on = !pc->was_on && cond.on;
1154 pc->was_on = pc->was_on || pc->on;
1155 ftepp_update_output_condition(ftepp);
1158 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1159 if (!ftepp_else_allowed(ftepp))
1161 if (!ftepp_ifdef(ftepp, &cond))
1164 pc = &vec_last(ftepp->conditions);
1165 pc->on = !pc->was_on && cond.on;
1166 pc->was_on = pc->was_on || pc->on;
1167 ftepp_update_output_condition(ftepp);
1170 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1171 if (!ftepp_else_allowed(ftepp))
1173 if (!ftepp_if(ftepp, &cond))
1175 pc = &vec_last(ftepp->conditions);
1176 pc->on = !pc->was_on && cond.on;
1177 pc->was_on = pc->was_on || pc->on;
1178 ftepp_update_output_condition(ftepp);
1181 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1182 if (!ftepp_if(ftepp, &cond))
1184 cond.was_on = cond.on;
1185 vec_push(ftepp->conditions, cond);
1186 ftepp->output_on = ftepp->output_on && cond.on;
1189 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1190 if (!ftepp_else_allowed(ftepp))
1192 pc = &vec_last(ftepp->conditions);
1193 pc->on = !pc->was_on;
1194 pc->had_else = true;
1196 ftepp_update_output_condition(ftepp);
1199 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1200 if (!vec_size(ftepp->conditions)) {
1201 ftepp_error(ftepp, "#endif without #if");
1204 vec_pop(ftepp->conditions);
1206 ftepp_update_output_condition(ftepp);
1209 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1210 return ftepp_include(ftepp);
1212 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1213 ftepp_out(ftepp, "#", false);
1217 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1220 /* break; never reached */
1222 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1225 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1228 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1231 /* Builtins! Don't forget the builtins! */
1232 case TOKEN_INTCONST:
1233 case TOKEN_FLOATCONST:
1234 ftepp_out(ftepp, "#", false);
1237 if (!ftepp_skipspace(ftepp))
1242 static bool ftepp_preprocess(ftepp_t *ftepp)
1245 bool newline = true;
1247 ftepp->lex->flags.preprocessing = true;
1248 ftepp->lex->flags.mergelines = false;
1249 ftepp->lex->flags.noops = true;
1254 if (ftepp->token >= TOKEN_EOF)
1257 ftepp->newline = newline;
1260 /* For the sake of FTE compatibility... FU, really */
1261 ftepp->newline = newline = true;
1264 switch (ftepp->token) {
1267 case TOKEN_TYPENAME:
1268 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1270 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1274 if (!ftepp_macro_call(ftepp, macro))
1275 ftepp->token = TOKEN_ERROR;
1278 if (!ftepp->newline) {
1279 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1283 ftepp->lex->flags.mergelines = true;
1284 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1285 ftepp_error(ftepp, "error in preprocessor directive");
1286 ftepp->token = TOKEN_ERROR;
1289 if (!ftepp_hash(ftepp))
1290 ftepp->token = TOKEN_ERROR;
1291 ftepp->lex->flags.mergelines = false;
1295 ftepp_out(ftepp, "\n", true);
1299 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1303 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1305 /* force a 0 at the end but don't count it as added to the output */
1306 vec_push(ftepp->output_string, 0);
1307 vec_shrinkby(ftepp->output_string, 1);
1309 newline = ftepp->token == TOKEN_EOF;
1313 /* Like in parser.c - files keep the previous state so we have one global
1314 * preprocessor. Except here we will want to warn about dangling #ifs.
1316 static ftepp_t *ftepp;
1318 static bool ftepp_preprocess_done()
1321 if (vec_size(ftepp->conditions)) {
1322 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1325 lex_close(ftepp->lex);
1327 if (ftepp->itemname) {
1328 mem_d(ftepp->itemname);
1329 ftepp->itemname = NULL;
1334 bool ftepp_preprocess_file(const char *filename)
1336 ftepp->lex = lex_open(filename);
1337 ftepp->itemname = util_strdup(filename);
1339 con_out("failed to open file \"%s\"\n", filename);
1342 if (!ftepp_preprocess(ftepp))
1344 return ftepp_preprocess_done();
1347 bool ftepp_preprocess_string(const char *name, const char *str)
1349 ftepp->lex = lex_open_string(str, strlen(str), name);
1350 ftepp->itemname = util_strdup(name);
1352 con_out("failed to create lexer for string \"%s\"\n", name);
1355 if (!ftepp_preprocess(ftepp))
1357 return ftepp_preprocess_done();
1362 ftepp = ftepp_new();
1366 /* set the right macro based on the selected standard */
1367 ftepp_add_define(NULL, "GMQCC");
1368 if (opts_standard == COMPILER_FTEQCC)
1369 ftepp_add_define(NULL, "__STD_FTEQCC__");
1370 else if (opts_standard == COMPILER_GMQCC)
1371 ftepp_add_define(NULL, "__STD_GMQCC__");
1372 else if (opts_standard == COMPILER_QCC)
1373 ftepp_add_define(NULL, "__STD_QCC__");
1378 void ftepp_add_define(const char *source, const char *name)
1381 lex_ctx ctx = { "__builtin__", 0 };
1383 macro = ppmacro_new(ctx, name);
1384 vec_push(ftepp->macros, macro);
1387 const char *ftepp_get()
1389 return ftepp->output_string;
1394 vec_free(ftepp->output_string);
1401 ftepp_delete(ftepp);