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 size_t l = ftepp_ctx(ftepp).line;
299 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
300 ptok = pptoken_make(ftepp);
301 vec_push(macro->output, ptok);
304 /* recursive expansion can cause EOFs here */
305 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
306 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
309 for (; l < ftepp_ctx(ftepp).line; ++l)
310 ftepp_out(ftepp, "\n", true);
314 static bool ftepp_define(ftepp_t *ftepp)
317 (void)ftepp_next(ftepp);
318 if (!ftepp_skipspace(ftepp))
321 switch (ftepp->token) {
325 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
326 if (macro && ftepp->output_on) {
327 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
329 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
331 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
334 ftepp_error(ftepp, "expected macro name");
338 (void)ftepp_next(ftepp);
340 if (ftepp->token == '(') {
341 macro->has_params = true;
342 if (!ftepp_define_params(ftepp, macro))
346 if (!ftepp_skipspace(ftepp))
349 if (!ftepp_define_body(ftepp, macro))
352 if (ftepp->output_on)
353 vec_push(ftepp->macros, macro);
355 ppmacro_delete(macro);
361 * When a macro is used we have to handle parameters as well
362 * as special-concatenation via ## or stringification via #
364 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
365 * this kind of parens. Curly braces or [] don't count towards the
372 static void macroparam_clean(macroparam *self)
375 for (i = 0; i < vec_size(self->tokens); ++i)
376 pptoken_delete(self->tokens[i]);
377 vec_free(self->tokens);
380 /* need to leave the last token up */
381 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
383 macroparam *params = NULL;
389 if (!ftepp_skipallwhite(ftepp))
391 while (ftepp->token != ')') {
393 if (!ftepp_skipallwhite(ftepp))
395 while (parens || ftepp->token != ',') {
396 if (ftepp->token == '(')
398 else if (ftepp->token == ')') {
403 ptok = pptoken_make(ftepp);
404 vec_push(mp.tokens, ptok);
405 if (ftepp_next(ftepp) >= TOKEN_EOF) {
406 ftepp_error(ftepp, "unexpected EOF in macro call");
410 vec_push(params, mp);
412 if (ftepp->token == ')')
414 if (ftepp->token != ',') {
415 ftepp_error(ftepp, "expected closing paren or comma in macro call");
418 if (ftepp_next(ftepp) >= TOKEN_EOF) {
419 ftepp_error(ftepp, "unexpected EOF in macro call");
423 /* need to leave that up
424 if (ftepp_next(ftepp) >= TOKEN_EOF) {
425 ftepp_error(ftepp, "unexpected EOF in macro call");
429 *out_params = params;
434 macroparam_clean(&mp);
435 for (i = 0; i < vec_size(params); ++i)
436 macroparam_clean(¶ms[i]);
441 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
444 for (i = 0; i < vec_size(macro->params); ++i) {
445 if (!strcmp(macro->params[i], name)) {
453 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
458 switch (token->token) {
459 case TOKEN_STRINGCONST:
462 /* in preprocessor mode strings already are string,
463 * so we don't get actual newline bytes here.
464 * Still need to escape backslashes and quotes.
467 case '\\': ftepp_out(ftepp, "\\\\", false); break;
468 case '"': ftepp_out(ftepp, "\\\"", false); break;
471 ftepp_out(ftepp, chs, false);
478 ftepp_out(ftepp, " ", false);
481 ftepp_out(ftepp, "\\n", false);
484 ftepp_out(ftepp, token->value, false);
489 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
492 ftepp_out(ftepp, "\"", false);
493 for (i = 0; i < vec_size(param->tokens); ++i)
494 ftepp_stringify_token(ftepp, param->tokens[i]);
495 ftepp_out(ftepp, "\"", false);
498 static void ftepp_recursion_header(ftepp_t *ftepp)
500 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
503 static void ftepp_recursion_footer(ftepp_t *ftepp)
505 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
508 static bool ftepp_preprocess(ftepp_t *ftepp);
509 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
511 char *old_string = ftepp->output_string;
512 lex_file *old_lexer = ftepp->lex;
521 if (!vec_size(macro->output))
524 ftepp->output_string = NULL;
525 for (o = 0; o < vec_size(macro->output); ++o) {
526 pptoken *out = macro->output[o];
527 switch (out->token) {
531 if (!macro_params_find(macro, out->value, &pi)) {
532 ftepp_out(ftepp, out->value, false);
535 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
536 out = params[pi].tokens[pv];
537 if (out->token == TOKEN_EOL)
538 ftepp_out(ftepp, "\n", false);
540 ftepp_out(ftepp, out->value, false);
545 if (o + 1 < vec_size(macro->output)) {
546 nextok = macro->output[o+1]->token;
548 /* raw concatenation */
552 if ( (nextok == TOKEN_IDENT ||
553 nextok == TOKEN_KEYWORD ||
554 nextok == TOKEN_TYPENAME) &&
555 macro_params_find(macro, macro->output[o+1]->value, &pi))
558 ftepp_stringify(ftepp, ¶ms[pi]);
562 ftepp_out(ftepp, "#", false);
565 ftepp_out(ftepp, "\n", false);
568 ftepp_out(ftepp, out->value, false);
572 vec_push(ftepp->output_string, 0);
573 /* Now run the preprocessor recursively on this string buffer */
575 printf("__________\n%s\n=========\n", ftepp->output_string);
577 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
579 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
583 ftepp->output_string = old_string;
585 ftepp_recursion_header(ftepp);
586 if (!ftepp_preprocess(ftepp)) {
587 lex_close(ftepp->lex);
591 ftepp_recursion_footer(ftepp);
592 old_string = ftepp->output_string;
595 ftepp->lex = old_lexer;
596 ftepp->output_string = old_string;
600 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
603 macroparam *params = NULL;
606 if (!macro->has_params) {
607 if (!ftepp_macro_expand(ftepp, macro, NULL))
614 if (!ftepp_skipallwhite(ftepp))
617 if (ftepp->token != '(') {
618 ftepp_error(ftepp, "expected macro parameters in parenthesis");
623 if (!ftepp_macro_call_params(ftepp, ¶ms))
626 if (vec_size(params) != vec_size(macro->params)) {
627 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
628 (unsigned int)vec_size(macro->params),
629 (unsigned int)vec_size(params));
634 if (!ftepp_macro_expand(ftepp, macro, params))
639 for (o = 0; o < vec_size(params); ++o)
640 macroparam_clean(¶ms[o]);
646 * #if - the FTEQCC way:
647 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
648 * <numbers> => True if the number is not 0
649 * !<factor> => True if the factor yields false
650 * !!<factor> => ERROR on 2 or more unary nots
651 * <macro> => becomes the macro's FIRST token regardless of parameters
652 * <e> && <e> => True if both expressions are true
653 * <e> || <e> => True if either expression is true
655 * <ident> => False (remember for macros the <macro> rule applies instead)
656 * Unary + and - are weird and wrong in fteqcc so we don't allow them
657 * parenthesis in expressions are allowed
658 * parameter lists on macros are errors
659 * No mathematical calculations are executed
661 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
662 static bool ftepp_if_op(ftepp_t *ftepp)
664 ftepp->lex->flags.noops = false;
666 if (!ftepp_skipspace(ftepp))
668 ftepp->lex->flags.noops = true;
671 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
676 if (!ftepp_skipspace(ftepp))
679 while (ftepp->token == '!') {
682 if (!ftepp_skipspace(ftepp))
686 switch (ftepp->token) {
690 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
692 if (!ftepp_skipspace(ftepp))
694 if (ftepp->token != '(') {
695 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
699 if (!ftepp_skipspace(ftepp))
701 if (ftepp->token != TOKEN_IDENT &&
702 ftepp->token != TOKEN_TYPENAME &&
703 ftepp->token != TOKEN_KEYWORD)
705 ftepp_error(ftepp, "defined() used on an unexpected token type");
708 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
711 if (!ftepp_skipspace(ftepp))
713 if (ftepp->token != ')') {
714 ftepp_error(ftepp, "expected closing paren");
720 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
721 if (!macro || !vec_size(macro->output)) {
725 /* This does not expand recursively! */
726 switch (macro->output[0]->token) {
728 *value_out = macro->output[0]->constval.i;
729 *out = !!(macro->output[0]->constval.i);
731 case TOKEN_FLOATCONST:
732 *value_out = macro->output[0]->constval.f;
733 *out = !!(macro->output[0]->constval.f);
741 case TOKEN_STRINGCONST:
745 *value_out = ftepp->lex->tok.constval.i;
746 *out = !!(ftepp->lex->tok.constval.i);
748 case TOKEN_FLOATCONST:
749 *value_out = ftepp->lex->tok.constval.f;
750 *out = !!(ftepp->lex->tok.constval.f);
755 if (!ftepp_if_expr(ftepp, out, value_out))
757 if (ftepp->token != ')') {
758 ftepp_error(ftepp, "expected closing paren in #if expression");
764 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
769 *value_out = (*out ? 1 : 0);
775 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
777 if (!ftepp_next(ftepp))
779 return ftepp_if_value(ftepp, out, value_out);
783 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
785 if (!ftepp_if_value(ftepp, out, value_out))
788 if (!ftepp_if_op(ftepp))
791 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
794 /* FTEQCC is all right-associative and no precedence here */
795 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
796 !strcmp(ftepp_tokval(ftepp), "||"))
799 char opc = ftepp_tokval(ftepp)[0];
803 if (!ftepp_next(ftepp))
805 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
813 *value_out = (*out ? 1 : 0);
816 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
817 !strcmp(ftepp_tokval(ftepp), "!=") ||
818 !strcmp(ftepp_tokval(ftepp), ">=") ||
819 !strcmp(ftepp_tokval(ftepp), "<=") ||
820 !strcmp(ftepp_tokval(ftepp), ">") ||
821 !strcmp(ftepp_tokval(ftepp), "<"))
824 const char opc0 = ftepp_tokval(ftepp)[0];
825 const char opc1 = ftepp_tokval(ftepp)[1];
828 if (!ftepp_next(ftepp))
830 if (!ftepp_if_expr(ftepp, &next, &other))
834 *out = (*value_out == other);
835 else if (opc0 == '!')
836 *out = (*value_out != other);
837 else if (opc0 == '>') {
838 if (opc1 == '=') *out = (*value_out >= other);
839 else *out = (*value_out > other);
841 else if (opc0 == '<') {
842 if (opc1 == '=') *out = (*value_out <= other);
843 else *out = (*value_out < other);
845 *value_out = (*out ? 1 : 0);
850 ftepp_error(ftepp, "junk after #if");
855 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
860 memset(cond, 0, sizeof(*cond));
861 (void)ftepp_next(ftepp);
863 if (!ftepp_skipspace(ftepp))
865 if (ftepp->token == TOKEN_EOL) {
866 ftepp_error(ftepp, "expected expression for #if-directive");
870 if (!ftepp_if_expr(ftepp, &result, &dummy))
878 * ifdef is rather simple
880 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
883 memset(cond, 0, sizeof(*cond));
884 (void)ftepp_next(ftepp);
885 if (!ftepp_skipspace(ftepp))
888 switch (ftepp->token) {
892 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
895 ftepp_error(ftepp, "expected macro name");
899 (void)ftepp_next(ftepp);
900 if (!ftepp_skipspace(ftepp))
902 /* relaxing this condition
903 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
904 ftepp_error(ftepp, "stray tokens after #ifdef");
913 * undef is also simple
915 static bool ftepp_undef(ftepp_t *ftepp)
917 (void)ftepp_next(ftepp);
918 if (!ftepp_skipspace(ftepp))
921 if (ftepp->output_on) {
922 switch (ftepp->token) {
926 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
929 ftepp_error(ftepp, "expected macro name");
934 (void)ftepp_next(ftepp);
935 if (!ftepp_skipspace(ftepp))
937 /* relaxing this condition
938 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
939 ftepp_error(ftepp, "stray tokens after #ifdef");
946 /* Special unescape-string function which skips a leading quote
947 * and stops at a quote, not just at \0
949 static void unescape(const char *str, char *out) {
951 while (*str && *str != '"') {
955 case '\\': *out++ = *str; break;
956 case '"': *out++ = *str; break;
957 case 'a': *out++ = '\a'; break;
958 case 'b': *out++ = '\b'; break;
959 case 'r': *out++ = '\r'; break;
960 case 'n': *out++ = '\n'; break;
961 case 't': *out++ = '\t'; break;
962 case 'f': *out++ = '\f'; break;
963 case 'v': *out++ = '\v'; break;
978 static char *ftepp_include_find_path(const char *file, const char *pathfile)
981 char *filename = NULL;
982 const char *last_slash;
988 last_slash = strrchr(pathfile, '/');
991 len = last_slash - pathfile;
992 memcpy(vec_add(filename, len), pathfile, len);
993 vec_push(filename, '/');
996 len = strlen(pathfile);
997 memcpy(vec_add(filename, len), pathfile, len);
998 if (vec_last(filename) != '/')
999 vec_push(filename, '/');
1003 memcpy(vec_add(filename, len+1), file, len);
1004 vec_last(filename) = 0;
1006 fp = util_fopen(filename, "rb");
1015 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1017 char *filename = NULL;
1019 filename = ftepp_include_find_path(file, ftepp->includename);
1021 filename = ftepp_include_find_path(file, ftepp->itemname);
1027 * FIXME: do we need/want a -I option?
1028 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1030 static bool ftepp_include(ftepp_t *ftepp)
1032 lex_file *old_lexer = ftepp->lex;
1037 char *old_includename;
1039 (void)ftepp_next(ftepp);
1040 if (!ftepp_skipspace(ftepp))
1043 if (ftepp->token != TOKEN_STRINGCONST) {
1044 ftepp_error(ftepp, "expected filename to include");
1048 ctx = ftepp_ctx(ftepp);
1050 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1052 ftepp_out(ftepp, "\n#pragma file(", false);
1053 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1054 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1056 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1058 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1061 inlex = lex_open(filename);
1063 ftepp_error(ftepp, "failed to open include file `%s`", filename);
1068 old_includename = ftepp->includename;
1069 ftepp->includename = filename;
1070 if (!ftepp_preprocess(ftepp)) {
1071 vec_free(ftepp->includename);
1072 ftepp->includename = old_includename;
1073 lex_close(ftepp->lex);
1074 ftepp->lex = old_lexer;
1077 vec_free(ftepp->includename);
1078 ftepp->includename = old_includename;
1079 lex_close(ftepp->lex);
1080 ftepp->lex = old_lexer;
1082 ftepp_out(ftepp, "\n#pragma file(", false);
1083 ftepp_out(ftepp, ctx.file, false);
1084 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1085 ftepp_out(ftepp, lineno, false);
1088 (void)ftepp_next(ftepp);
1089 if (!ftepp_skipspace(ftepp))
1091 if (ftepp->token != TOKEN_EOL) {
1092 ftepp_error(ftepp, "stray tokens after #include");
1095 (void)ftepp_next(ftepp);
1100 /* Basic structure handlers */
1101 static bool ftepp_else_allowed(ftepp_t *ftepp)
1103 if (!vec_size(ftepp->conditions)) {
1104 ftepp_error(ftepp, "#else without #if");
1107 if (vec_last(ftepp->conditions).had_else) {
1108 ftepp_error(ftepp, "multiple #else for a single #if");
1114 static bool ftepp_hash(ftepp_t *ftepp)
1119 lex_ctx ctx = ftepp_ctx(ftepp);
1121 if (!ftepp_skipspace(ftepp))
1124 switch (ftepp->token) {
1127 case TOKEN_TYPENAME:
1128 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1129 return ftepp_define(ftepp);
1131 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1132 return ftepp_undef(ftepp);
1134 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1135 if (!ftepp_ifdef(ftepp, &cond))
1137 cond.was_on = cond.on;
1138 vec_push(ftepp->conditions, cond);
1139 ftepp->output_on = ftepp->output_on && cond.on;
1142 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1143 if (!ftepp_ifdef(ftepp, &cond))
1146 cond.was_on = cond.on;
1147 vec_push(ftepp->conditions, cond);
1148 ftepp->output_on = ftepp->output_on && cond.on;
1151 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1152 if (!ftepp_else_allowed(ftepp))
1154 if (!ftepp_ifdef(ftepp, &cond))
1156 pc = &vec_last(ftepp->conditions);
1157 pc->on = !pc->was_on && cond.on;
1158 pc->was_on = pc->was_on || pc->on;
1159 ftepp_update_output_condition(ftepp);
1162 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1163 if (!ftepp_else_allowed(ftepp))
1165 if (!ftepp_ifdef(ftepp, &cond))
1168 pc = &vec_last(ftepp->conditions);
1169 pc->on = !pc->was_on && cond.on;
1170 pc->was_on = pc->was_on || pc->on;
1171 ftepp_update_output_condition(ftepp);
1174 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1175 if (!ftepp_else_allowed(ftepp))
1177 if (!ftepp_if(ftepp, &cond))
1179 pc = &vec_last(ftepp->conditions);
1180 pc->on = !pc->was_on && cond.on;
1181 pc->was_on = pc->was_on || pc->on;
1182 ftepp_update_output_condition(ftepp);
1185 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1186 if (!ftepp_if(ftepp, &cond))
1188 cond.was_on = cond.on;
1189 vec_push(ftepp->conditions, cond);
1190 ftepp->output_on = ftepp->output_on && cond.on;
1193 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1194 if (!ftepp_else_allowed(ftepp))
1196 pc = &vec_last(ftepp->conditions);
1197 pc->on = !pc->was_on;
1198 pc->had_else = true;
1200 ftepp_update_output_condition(ftepp);
1203 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1204 if (!vec_size(ftepp->conditions)) {
1205 ftepp_error(ftepp, "#endif without #if");
1208 vec_pop(ftepp->conditions);
1210 ftepp_update_output_condition(ftepp);
1213 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1214 return ftepp_include(ftepp);
1216 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1217 ftepp_out(ftepp, "#", false);
1221 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)
1261 ftepp->newline = newline;
1264 /* For the sake of FTE compatibility... FU, really */
1265 ftepp->newline = newline = true;
1268 switch (ftepp->token) {
1271 case TOKEN_TYPENAME:
1272 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1274 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1278 if (!ftepp_macro_call(ftepp, macro))
1279 ftepp->token = TOKEN_ERROR;
1282 if (!ftepp->newline) {
1283 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1287 ftepp->lex->flags.mergelines = true;
1288 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1289 ftepp_error(ftepp, "error in preprocessor directive");
1290 ftepp->token = TOKEN_ERROR;
1293 if (!ftepp_hash(ftepp))
1294 ftepp->token = TOKEN_ERROR;
1295 ftepp->lex->flags.mergelines = false;
1299 ftepp_out(ftepp, "\n", true);
1303 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1307 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1309 /* force a 0 at the end but don't count it as added to the output */
1310 vec_push(ftepp->output_string, 0);
1311 vec_shrinkby(ftepp->output_string, 1);
1313 newline = ftepp->token == TOKEN_EOF;
1317 /* Like in parser.c - files keep the previous state so we have one global
1318 * preprocessor. Except here we will want to warn about dangling #ifs.
1320 static ftepp_t *ftepp;
1322 static bool ftepp_preprocess_done()
1325 if (vec_size(ftepp->conditions)) {
1326 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1329 lex_close(ftepp->lex);
1331 if (ftepp->itemname) {
1332 mem_d(ftepp->itemname);
1333 ftepp->itemname = NULL;
1338 bool ftepp_preprocess_file(const char *filename)
1340 ftepp->lex = lex_open(filename);
1341 ftepp->itemname = util_strdup(filename);
1343 con_out("failed to open file \"%s\"\n", filename);
1346 if (!ftepp_preprocess(ftepp))
1348 return ftepp_preprocess_done();
1351 bool ftepp_preprocess_string(const char *name, const char *str)
1353 ftepp->lex = lex_open_string(str, strlen(str), name);
1354 ftepp->itemname = util_strdup(name);
1356 con_out("failed to create lexer for string \"%s\"\n", name);
1359 if (!ftepp_preprocess(ftepp))
1361 return ftepp_preprocess_done();
1366 ftepp = ftepp_new();
1370 /* set the right macro based on the selected standard */
1371 ftepp_add_define(NULL, "GMQCC");
1372 if (opts_standard == COMPILER_FTEQCC)
1373 ftepp_add_define(NULL, "__STD_FTEQCC__");
1374 else if (opts_standard == COMPILER_GMQCC)
1375 ftepp_add_define(NULL, "__STD_GMQCC__");
1376 else if (opts_standard == COMPILER_QCC)
1377 ftepp_add_define(NULL, "__STD_QCC__");
1382 void ftepp_add_define(const char *source, const char *name)
1385 lex_ctx ctx = { "__builtin__", 0 };
1387 macro = ppmacro_new(ctx, name);
1388 vec_push(ftepp->macros, macro);
1391 const char *ftepp_get()
1393 return ftepp->output_string;
1398 vec_free(ftepp->output_string);
1405 ftepp_delete(ftepp);