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 (void)ftepp_next(ftepp);
315 if (!ftepp_skipspace(ftepp))
318 switch (ftepp->token) {
322 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
323 if (macro && ftepp->output_on) {
324 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
326 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
328 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
331 ftepp_error(ftepp, "expected macro name");
335 (void)ftepp_next(ftepp);
337 if (ftepp->token == '(') {
338 macro->has_params = true;
339 if (!ftepp_define_params(ftepp, macro))
343 if (!ftepp_skipspace(ftepp))
346 if (!ftepp_define_body(ftepp, macro))
349 if (ftepp->output_on)
350 vec_push(ftepp->macros, macro);
352 ppmacro_delete(macro);
358 * When a macro is used we have to handle parameters as well
359 * as special-concatenation via ## or stringification via #
361 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
362 * this kind of parens. Curly braces or [] don't count towards the
369 static void macroparam_clean(macroparam *self)
372 for (i = 0; i < vec_size(self->tokens); ++i)
373 pptoken_delete(self->tokens[i]);
374 vec_free(self->tokens);
377 /* need to leave the last token up */
378 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
380 macroparam *params = NULL;
386 if (!ftepp_skipallwhite(ftepp))
388 while (ftepp->token != ')') {
390 if (!ftepp_skipallwhite(ftepp))
392 while (parens || ftepp->token != ',') {
393 if (ftepp->token == '(')
395 else if (ftepp->token == ')') {
400 ptok = pptoken_make(ftepp);
401 vec_push(mp.tokens, ptok);
402 if (ftepp_next(ftepp) >= TOKEN_EOF) {
403 ftepp_error(ftepp, "unexpected EOF in macro call");
407 vec_push(params, mp);
409 if (ftepp->token == ')')
411 if (ftepp->token != ',') {
412 ftepp_error(ftepp, "expected closing paren or comma in macro call");
415 if (ftepp_next(ftepp) >= TOKEN_EOF) {
416 ftepp_error(ftepp, "unexpected EOF in macro call");
420 /* need to leave that up
421 if (ftepp_next(ftepp) >= TOKEN_EOF) {
422 ftepp_error(ftepp, "unexpected EOF in macro call");
426 *out_params = params;
431 macroparam_clean(&mp);
432 for (i = 0; i < vec_size(params); ++i)
433 macroparam_clean(¶ms[i]);
438 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
441 for (i = 0; i < vec_size(macro->params); ++i) {
442 if (!strcmp(macro->params[i], name)) {
450 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
455 switch (token->token) {
456 case TOKEN_STRINGCONST:
459 /* in preprocessor mode strings already are string,
460 * so we don't get actual newline bytes here.
461 * Still need to escape backslashes and quotes.
464 case '\\': ftepp_out(ftepp, "\\\\", false); break;
465 case '"': ftepp_out(ftepp, "\\\"", false); break;
468 ftepp_out(ftepp, chs, false);
475 ftepp_out(ftepp, " ", false);
478 ftepp_out(ftepp, "\\n", false);
481 ftepp_out(ftepp, token->value, false);
486 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
489 ftepp_out(ftepp, "\"", false);
490 for (i = 0; i < vec_size(param->tokens); ++i)
491 ftepp_stringify_token(ftepp, param->tokens[i]);
492 ftepp_out(ftepp, "\"", false);
495 static void ftepp_recursion_header(ftepp_t *ftepp)
497 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
500 static void ftepp_recursion_footer(ftepp_t *ftepp)
502 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
505 static bool ftepp_preprocess(ftepp_t *ftepp);
506 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
508 char *old_string = ftepp->output_string;
509 lex_file *old_lexer = ftepp->lex;
518 if (!vec_size(macro->output))
521 ftepp->output_string = NULL;
522 for (o = 0; o < vec_size(macro->output); ++o) {
523 pptoken *out = macro->output[o];
524 switch (out->token) {
528 if (!macro_params_find(macro, out->value, &pi)) {
529 ftepp_out(ftepp, out->value, false);
532 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
533 out = params[pi].tokens[pv];
534 if (out->token == TOKEN_EOL)
535 ftepp_out(ftepp, "\n", false);
537 ftepp_out(ftepp, out->value, false);
542 if (o + 1 < vec_size(macro->output)) {
543 nextok = macro->output[o+1]->token;
545 /* raw concatenation */
549 if ( (nextok == TOKEN_IDENT ||
550 nextok == TOKEN_KEYWORD ||
551 nextok == TOKEN_TYPENAME) &&
552 macro_params_find(macro, macro->output[o+1]->value, &pi))
555 ftepp_stringify(ftepp, ¶ms[pi]);
559 ftepp_out(ftepp, "#", false);
562 ftepp_out(ftepp, "\n", false);
565 ftepp_out(ftepp, out->value, false);
569 vec_push(ftepp->output_string, 0);
570 /* Now run the preprocessor recursively on this string buffer */
572 printf("__________\n%s\n=========\n", ftepp->output_string);
574 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
576 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
580 ftepp->output_string = old_string;
582 ftepp_recursion_header(ftepp);
583 if (!ftepp_preprocess(ftepp)) {
584 lex_close(ftepp->lex);
588 ftepp_recursion_footer(ftepp);
589 old_string = ftepp->output_string;
592 ftepp->lex = old_lexer;
593 ftepp->output_string = old_string;
597 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
600 macroparam *params = NULL;
603 if (!macro->has_params) {
604 if (!ftepp_macro_expand(ftepp, macro, NULL))
611 if (!ftepp_skipallwhite(ftepp))
614 if (ftepp->token != '(') {
615 ftepp_error(ftepp, "expected macro parameters in parenthesis");
620 if (!ftepp_macro_call_params(ftepp, ¶ms))
623 if (vec_size(params) != vec_size(macro->params)) {
624 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
625 (unsigned int)vec_size(macro->params),
626 (unsigned int)vec_size(params));
631 if (!ftepp_macro_expand(ftepp, macro, params))
636 for (o = 0; o < vec_size(params); ++o)
637 macroparam_clean(¶ms[o]);
643 * #if - the FTEQCC way:
644 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
645 * <numbers> => True if the number is not 0
646 * !<factor> => True if the factor yields false
647 * !!<factor> => ERROR on 2 or more unary nots
648 * <macro> => becomes the macro's FIRST token regardless of parameters
649 * <e> && <e> => True if both expressions are true
650 * <e> || <e> => True if either expression is true
652 * <ident> => False (remember for macros the <macro> rule applies instead)
653 * Unary + and - are weird and wrong in fteqcc so we don't allow them
654 * parenthesis in expressions are allowed
655 * parameter lists on macros are errors
656 * No mathematical calculations are executed
658 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
659 static bool ftepp_if_op(ftepp_t *ftepp)
661 ftepp->lex->flags.noops = false;
663 if (!ftepp_skipspace(ftepp))
665 ftepp->lex->flags.noops = true;
668 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
673 if (!ftepp_skipspace(ftepp))
676 while (ftepp->token == '!') {
679 if (!ftepp_skipspace(ftepp))
683 switch (ftepp->token) {
687 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
689 if (!ftepp_skipspace(ftepp))
691 if (ftepp->token != '(') {
692 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
696 if (!ftepp_skipspace(ftepp))
698 if (ftepp->token != TOKEN_IDENT &&
699 ftepp->token != TOKEN_TYPENAME &&
700 ftepp->token != TOKEN_KEYWORD)
702 ftepp_error(ftepp, "defined() used on an unexpected token type");
705 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
708 if (!ftepp_skipspace(ftepp))
710 if (ftepp->token != ')') {
711 ftepp_error(ftepp, "expected closing paren");
717 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
718 if (!macro || !vec_size(macro->output)) {
722 /* This does not expand recursively! */
723 switch (macro->output[0]->token) {
725 *value_out = macro->output[0]->constval.i;
726 *out = !!(macro->output[0]->constval.i);
728 case TOKEN_FLOATCONST:
729 *value_out = macro->output[0]->constval.f;
730 *out = !!(macro->output[0]->constval.f);
738 case TOKEN_STRINGCONST:
742 *value_out = ftepp->lex->tok.constval.i;
743 *out = !!(ftepp->lex->tok.constval.i);
745 case TOKEN_FLOATCONST:
746 *value_out = ftepp->lex->tok.constval.f;
747 *out = !!(ftepp->lex->tok.constval.f);
752 if (!ftepp_if_expr(ftepp, out, value_out))
754 if (ftepp->token != ')') {
755 ftepp_error(ftepp, "expected closing paren in #if expression");
761 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
766 *value_out = (*out ? 1 : 0);
772 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
774 if (!ftepp_next(ftepp))
776 return ftepp_if_value(ftepp, out, value_out);
780 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
782 if (!ftepp_if_value(ftepp, out, value_out))
785 if (!ftepp_if_op(ftepp))
788 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
791 /* FTEQCC is all right-associative and no precedence here */
792 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
793 !strcmp(ftepp_tokval(ftepp), "||"))
796 char opc = ftepp_tokval(ftepp)[0];
800 if (!ftepp_next(ftepp))
802 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
810 *value_out = (*out ? 1 : 0);
813 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
814 !strcmp(ftepp_tokval(ftepp), "!=") ||
815 !strcmp(ftepp_tokval(ftepp), ">=") ||
816 !strcmp(ftepp_tokval(ftepp), "<=") ||
817 !strcmp(ftepp_tokval(ftepp), ">") ||
818 !strcmp(ftepp_tokval(ftepp), "<"))
821 const char opc0 = ftepp_tokval(ftepp)[0];
822 const char opc1 = ftepp_tokval(ftepp)[1];
825 if (!ftepp_next(ftepp))
827 if (!ftepp_if_expr(ftepp, &next, &other))
831 *out = (*value_out == other);
832 else if (opc0 == '!')
833 *out = (*value_out != other);
834 else if (opc0 == '>') {
835 if (opc1 == '=') *out = (*value_out >= other);
836 else *out = (*value_out > other);
838 else if (opc0 == '<') {
839 if (opc1 == '=') *out = (*value_out <= other);
840 else *out = (*value_out < other);
842 *value_out = (*out ? 1 : 0);
847 ftepp_error(ftepp, "junk after #if");
852 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
857 memset(cond, 0, sizeof(*cond));
858 (void)ftepp_next(ftepp);
860 if (!ftepp_skipspace(ftepp))
862 if (ftepp->token == TOKEN_EOL) {
863 ftepp_error(ftepp, "expected expression for #if-directive");
867 if (!ftepp_if_expr(ftepp, &result, &dummy))
875 * ifdef is rather simple
877 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
880 memset(cond, 0, sizeof(*cond));
881 (void)ftepp_next(ftepp);
882 if (!ftepp_skipspace(ftepp))
885 switch (ftepp->token) {
889 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
892 ftepp_error(ftepp, "expected macro name");
896 (void)ftepp_next(ftepp);
897 if (!ftepp_skipspace(ftepp))
899 /* relaxing this condition
900 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
901 ftepp_error(ftepp, "stray tokens after #ifdef");
910 * undef is also simple
912 static bool ftepp_undef(ftepp_t *ftepp)
914 (void)ftepp_next(ftepp);
915 if (!ftepp_skipspace(ftepp))
918 if (ftepp->output_on) {
919 switch (ftepp->token) {
923 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
926 ftepp_error(ftepp, "expected macro name");
931 (void)ftepp_next(ftepp);
932 if (!ftepp_skipspace(ftepp))
934 /* relaxing this condition
935 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
936 ftepp_error(ftepp, "stray tokens after #ifdef");
943 /* Special unescape-string function which skips a leading quote
944 * and stops at a quote, not just at \0
946 static void unescape(const char *str, char *out) {
948 while (*str && *str != '"') {
952 case '\\': *out++ = *str; break;
953 case '"': *out++ = *str; break;
954 case 'a': *out++ = '\a'; break;
955 case 'b': *out++ = '\b'; break;
956 case 'r': *out++ = '\r'; break;
957 case 'n': *out++ = '\n'; break;
958 case 't': *out++ = '\t'; break;
959 case 'f': *out++ = '\f'; break;
960 case 'v': *out++ = '\v'; break;
975 static char *ftepp_include_find_path(const char *file, const char *pathfile)
978 char *filename = NULL;
979 const char *last_slash;
985 last_slash = strrchr(pathfile, '/');
988 len = last_slash - pathfile;
989 memcpy(vec_add(filename, len), pathfile, len);
990 vec_push(filename, '/');
993 len = strlen(pathfile);
994 memcpy(vec_add(filename, len), pathfile, len);
995 if (vec_last(filename) != '/')
996 vec_push(filename, '/');
1000 memcpy(vec_add(filename, len+1), file, len);
1001 vec_last(filename) = 0;
1003 fp = util_fopen(filename, "rb");
1012 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1014 char *filename = NULL;
1016 filename = ftepp_include_find_path(file, ftepp->includename);
1018 filename = ftepp_include_find_path(file, ftepp->itemname);
1024 * FIXME: do we need/want a -I option?
1025 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1027 static bool ftepp_include(ftepp_t *ftepp)
1029 lex_file *old_lexer = ftepp->lex;
1034 char *old_includename;
1036 (void)ftepp_next(ftepp);
1037 if (!ftepp_skipspace(ftepp))
1040 if (ftepp->token != TOKEN_STRINGCONST) {
1041 ftepp_error(ftepp, "expected filename to include");
1045 ctx = ftepp_ctx(ftepp);
1047 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1049 ftepp_out(ftepp, "\n#pragma file(", false);
1050 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1051 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1053 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1055 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1058 inlex = lex_open(filename);
1060 ftepp_error(ftepp, "failed to open include file `%s`", filename);
1065 old_includename = ftepp->includename;
1066 ftepp->includename = filename;
1067 if (!ftepp_preprocess(ftepp)) {
1068 vec_free(ftepp->includename);
1069 ftepp->includename = old_includename;
1070 lex_close(ftepp->lex);
1071 ftepp->lex = old_lexer;
1074 vec_free(ftepp->includename);
1075 ftepp->includename = old_includename;
1076 lex_close(ftepp->lex);
1077 ftepp->lex = old_lexer;
1079 ftepp_out(ftepp, "\n#pragma file(", false);
1080 ftepp_out(ftepp, ctx.file, false);
1081 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1082 ftepp_out(ftepp, lineno, false);
1085 (void)ftepp_next(ftepp);
1086 if (!ftepp_skipspace(ftepp))
1088 if (ftepp->token != TOKEN_EOL) {
1089 ftepp_error(ftepp, "stray tokens after #include");
1092 (void)ftepp_next(ftepp);
1097 /* Basic structure handlers */
1098 static bool ftepp_else_allowed(ftepp_t *ftepp)
1100 if (!vec_size(ftepp->conditions)) {
1101 ftepp_error(ftepp, "#else without #if");
1104 if (vec_last(ftepp->conditions).had_else) {
1105 ftepp_error(ftepp, "multiple #else for a single #if");
1111 static bool ftepp_hash(ftepp_t *ftepp)
1116 lex_ctx ctx = ftepp_ctx(ftepp);
1118 if (!ftepp_skipspace(ftepp))
1121 switch (ftepp->token) {
1124 case TOKEN_TYPENAME:
1125 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1126 return ftepp_define(ftepp);
1128 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1129 return ftepp_undef(ftepp);
1131 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1132 if (!ftepp_ifdef(ftepp, &cond))
1134 cond.was_on = cond.on;
1135 vec_push(ftepp->conditions, cond);
1136 ftepp->output_on = ftepp->output_on && cond.on;
1139 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1140 if (!ftepp_ifdef(ftepp, &cond))
1143 cond.was_on = cond.on;
1144 vec_push(ftepp->conditions, cond);
1145 ftepp->output_on = ftepp->output_on && cond.on;
1148 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1149 if (!ftepp_else_allowed(ftepp))
1151 if (!ftepp_ifdef(ftepp, &cond))
1153 pc = &vec_last(ftepp->conditions);
1154 pc->on = !pc->was_on && cond.on;
1155 pc->was_on = pc->was_on || pc->on;
1156 ftepp_update_output_condition(ftepp);
1159 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1160 if (!ftepp_else_allowed(ftepp))
1162 if (!ftepp_ifdef(ftepp, &cond))
1165 pc = &vec_last(ftepp->conditions);
1166 pc->on = !pc->was_on && cond.on;
1167 pc->was_on = pc->was_on || pc->on;
1168 ftepp_update_output_condition(ftepp);
1171 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1172 if (!ftepp_else_allowed(ftepp))
1174 if (!ftepp_if(ftepp, &cond))
1176 pc = &vec_last(ftepp->conditions);
1177 pc->on = !pc->was_on && cond.on;
1178 pc->was_on = pc->was_on || pc->on;
1179 ftepp_update_output_condition(ftepp);
1182 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1183 if (!ftepp_if(ftepp, &cond))
1185 cond.was_on = cond.on;
1186 vec_push(ftepp->conditions, cond);
1187 ftepp->output_on = ftepp->output_on && cond.on;
1190 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1191 if (!ftepp_else_allowed(ftepp))
1193 pc = &vec_last(ftepp->conditions);
1194 pc->on = !pc->was_on;
1195 pc->had_else = true;
1197 ftepp_update_output_condition(ftepp);
1200 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1201 if (!vec_size(ftepp->conditions)) {
1202 ftepp_error(ftepp, "#endif without #if");
1205 vec_pop(ftepp->conditions);
1207 ftepp_update_output_condition(ftepp);
1210 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1211 return ftepp_include(ftepp);
1213 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1214 ftepp_out(ftepp, "#", false);
1218 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1221 /* break; never reached */
1223 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1226 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1229 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1232 /* Builtins! Don't forget the builtins! */
1233 case TOKEN_INTCONST:
1234 case TOKEN_FLOATCONST:
1235 ftepp_out(ftepp, "#", false);
1238 if (!ftepp_skipspace(ftepp))
1243 static bool ftepp_preprocess(ftepp_t *ftepp)
1246 bool newline = true;
1248 ftepp->lex->flags.preprocessing = true;
1249 ftepp->lex->flags.mergelines = false;
1250 ftepp->lex->flags.noops = true;
1255 if (ftepp->token >= TOKEN_EOF)
1258 ftepp->newline = newline;
1261 /* For the sake of FTE compatibility... FU, really */
1262 ftepp->newline = newline = true;
1265 switch (ftepp->token) {
1268 case TOKEN_TYPENAME:
1269 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1271 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1275 if (!ftepp_macro_call(ftepp, macro))
1276 ftepp->token = TOKEN_ERROR;
1279 if (!ftepp->newline) {
1280 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1284 ftepp->lex->flags.mergelines = true;
1285 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1286 ftepp_error(ftepp, "error in preprocessor directive");
1287 ftepp->token = TOKEN_ERROR;
1290 if (!ftepp_hash(ftepp))
1291 ftepp->token = TOKEN_ERROR;
1292 ftepp->lex->flags.mergelines = false;
1296 ftepp_out(ftepp, "\n", true);
1300 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1304 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1306 /* force a 0 at the end but don't count it as added to the output */
1307 vec_push(ftepp->output_string, 0);
1308 vec_shrinkby(ftepp->output_string, 1);
1310 newline = ftepp->token == TOKEN_EOF;
1314 /* Like in parser.c - files keep the previous state so we have one global
1315 * preprocessor. Except here we will want to warn about dangling #ifs.
1317 static ftepp_t *ftepp;
1319 static bool ftepp_preprocess_done()
1322 if (vec_size(ftepp->conditions)) {
1323 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1326 lex_close(ftepp->lex);
1328 if (ftepp->itemname) {
1329 mem_d(ftepp->itemname);
1330 ftepp->itemname = NULL;
1335 bool ftepp_preprocess_file(const char *filename)
1337 ftepp->lex = lex_open(filename);
1338 ftepp->itemname = util_strdup(filename);
1340 con_out("failed to open file \"%s\"\n", filename);
1343 if (!ftepp_preprocess(ftepp))
1345 return ftepp_preprocess_done();
1348 bool ftepp_preprocess_string(const char *name, const char *str)
1350 ftepp->lex = lex_open_string(str, strlen(str), name);
1351 ftepp->itemname = util_strdup(name);
1353 con_out("failed to create lexer for string \"%s\"\n", name);
1356 if (!ftepp_preprocess(ftepp))
1358 return ftepp_preprocess_done();
1363 ftepp = ftepp_new();
1367 /* set the right macro based on the selected standard */
1368 if (opts_standard == COMPILER_FTEQCC)
1369 ftepp_add_define(NULL, "FTEQCC");
1370 else if (opts_standard == COMPILER_GMQCC)
1371 ftepp_add_define(NULL, "GMQCC");
1372 else if (opts_standard == COMPILER_QCC)
1373 ftepp_add_define(NULL, "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);