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)
663 if (!ftepp_skipspace(ftepp))
666 while (ftepp->token == '!') {
669 if (!ftepp_skipspace(ftepp))
673 switch (ftepp->token) {
677 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
679 if (!ftepp_skipspace(ftepp))
681 if (ftepp->token != '(') {
682 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
686 if (!ftepp_skipspace(ftepp))
688 if (ftepp->token != TOKEN_IDENT &&
689 ftepp->token != TOKEN_TYPENAME &&
690 ftepp->token != TOKEN_KEYWORD)
692 ftepp_error(ftepp, "defined() used on an unexpected token type");
695 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
698 if (!ftepp_skipspace(ftepp))
700 if (ftepp->token != ')') {
701 ftepp_error(ftepp, "expected closing paren");
707 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
708 if (!macro || !vec_size(macro->output)) {
711 /* This does not expand recursively! */
712 switch (macro->output[0]->token) {
714 *out = !!(macro->output[0]->constval.f);
716 case TOKEN_FLOATCONST:
717 *out = !!(macro->output[0]->constval.f);
725 case TOKEN_STRINGCONST:
729 *out = !!(ftepp->lex->tok.constval.i);
731 case TOKEN_FLOATCONST:
732 *out = !!(ftepp->lex->tok.constval.f);
737 if (!ftepp_if_expr(ftepp, out))
739 if (ftepp->token != ')') {
740 ftepp_error(ftepp, "expected closing paren in #if expression");
746 ftepp_error(ftepp, "junk in #if");
752 ftepp->lex->flags.noops = false;
754 if (!ftepp_skipspace(ftepp))
756 ftepp->lex->flags.noops = true;
758 if (ftepp->token == ')')
761 if (ftepp->token != TOKEN_OPERATOR)
764 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
765 !strcmp(ftepp_tokval(ftepp), "||"))
768 char opc = ftepp_tokval(ftepp)[0];
771 if (!ftepp_if_expr(ftepp, &next))
781 ftepp_error(ftepp, "junk after #if");
786 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
790 memset(cond, 0, sizeof(*cond));
791 (void)ftepp_next(ftepp);
793 if (!ftepp_skipspace(ftepp))
795 if (ftepp->token == TOKEN_EOL) {
796 ftepp_error(ftepp, "expected expression for #if-directive");
800 if (!ftepp_if_expr(ftepp, &result))
808 * ifdef is rather simple
810 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
813 memset(cond, 0, sizeof(*cond));
814 (void)ftepp_next(ftepp);
815 if (!ftepp_skipspace(ftepp))
818 switch (ftepp->token) {
822 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
825 ftepp_error(ftepp, "expected macro name");
829 (void)ftepp_next(ftepp);
830 if (!ftepp_skipspace(ftepp))
832 /* relaxing this condition
833 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
834 ftepp_error(ftepp, "stray tokens after #ifdef");
843 * undef is also simple
845 static bool ftepp_undef(ftepp_t *ftepp)
847 (void)ftepp_next(ftepp);
848 if (!ftepp_skipspace(ftepp))
851 if (ftepp->output_on) {
852 switch (ftepp->token) {
856 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
859 ftepp_error(ftepp, "expected macro name");
864 (void)ftepp_next(ftepp);
865 if (!ftepp_skipspace(ftepp))
867 /* relaxing this condition
868 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
869 ftepp_error(ftepp, "stray tokens after #ifdef");
876 /* Special unescape-string function which skips a leading quote
877 * and stops at a quote, not just at \0
879 static void unescape(const char *str, char *out) {
881 while (*str && *str != '"') {
885 case '\\': *out++ = *str; break;
886 case '"': *out++ = *str; break;
887 case 'a': *out++ = '\a'; break;
888 case 'b': *out++ = '\b'; break;
889 case 'r': *out++ = '\r'; break;
890 case 'n': *out++ = '\n'; break;
891 case 't': *out++ = '\t'; break;
892 case 'f': *out++ = '\f'; break;
893 case 'v': *out++ = '\v'; break;
908 static char *ftepp_include_find_path(const char *file, const char *pathfile)
911 char *filename = NULL;
912 const char *last_slash;
918 last_slash = strrchr(pathfile, '/');
921 len = last_slash - pathfile;
922 memcpy(vec_add(filename, len), pathfile, len);
923 vec_push(filename, '/');
926 len = strlen(pathfile);
927 memcpy(vec_add(filename, len), pathfile, len);
928 if (vec_last(filename) != '/')
929 vec_push(filename, '/');
933 memcpy(vec_add(filename, len+1), file, len);
934 vec_last(filename) = 0;
936 fp = util_fopen(filename, "rb");
945 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
947 char *filename = NULL;
949 filename = ftepp_include_find_path(file, ftepp->includename);
951 filename = ftepp_include_find_path(file, ftepp->itemname);
957 * FIXME: do we need/want a -I option?
958 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
960 static bool ftepp_include(ftepp_t *ftepp)
962 lex_file *old_lexer = ftepp->lex;
967 char *old_includename;
969 (void)ftepp_next(ftepp);
970 if (!ftepp_skipspace(ftepp))
973 if (ftepp->token != TOKEN_STRINGCONST) {
974 ftepp_error(ftepp, "expected filename to include");
978 ctx = ftepp_ctx(ftepp);
980 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
982 ftepp_out(ftepp, "\n#pragma file(", false);
983 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
984 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
986 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
988 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
991 inlex = lex_open(filename);
993 ftepp_error(ftepp, "failed to open include file `%s`", filename);
998 old_includename = ftepp->includename;
999 ftepp->includename = filename;
1000 if (!ftepp_preprocess(ftepp)) {
1001 vec_free(ftepp->includename);
1002 ftepp->includename = old_includename;
1003 lex_close(ftepp->lex);
1004 ftepp->lex = old_lexer;
1007 vec_free(ftepp->includename);
1008 ftepp->includename = old_includename;
1009 lex_close(ftepp->lex);
1010 ftepp->lex = old_lexer;
1012 ftepp_out(ftepp, "\n#pragma file(", false);
1013 ftepp_out(ftepp, ctx.file, false);
1014 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1015 ftepp_out(ftepp, lineno, false);
1018 (void)ftepp_next(ftepp);
1019 if (!ftepp_skipspace(ftepp))
1021 if (ftepp->token != TOKEN_EOL) {
1022 ftepp_error(ftepp, "stray tokens after #include");
1025 (void)ftepp_next(ftepp);
1030 /* Basic structure handlers */
1031 static bool ftepp_else_allowed(ftepp_t *ftepp)
1033 if (!vec_size(ftepp->conditions)) {
1034 ftepp_error(ftepp, "#else without #if");
1037 if (vec_last(ftepp->conditions).had_else) {
1038 ftepp_error(ftepp, "multiple #else for a single #if");
1044 static bool ftepp_hash(ftepp_t *ftepp)
1049 lex_ctx ctx = ftepp_ctx(ftepp);
1051 if (!ftepp_skipspace(ftepp))
1054 switch (ftepp->token) {
1057 case TOKEN_TYPENAME:
1058 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1059 return ftepp_define(ftepp);
1061 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1062 return ftepp_undef(ftepp);
1064 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1065 if (!ftepp_ifdef(ftepp, &cond))
1067 cond.was_on = cond.on;
1068 vec_push(ftepp->conditions, cond);
1069 ftepp->output_on = ftepp->output_on && cond.on;
1072 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1073 if (!ftepp_ifdef(ftepp, &cond))
1076 cond.was_on = cond.on;
1077 vec_push(ftepp->conditions, cond);
1078 ftepp->output_on = ftepp->output_on && cond.on;
1081 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1082 if (!ftepp_else_allowed(ftepp))
1084 if (!ftepp_ifdef(ftepp, &cond))
1086 pc = &vec_last(ftepp->conditions);
1087 pc->on = !pc->was_on && cond.on;
1088 pc->was_on = pc->was_on || pc->on;
1089 ftepp_update_output_condition(ftepp);
1092 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1093 if (!ftepp_else_allowed(ftepp))
1095 if (!ftepp_ifdef(ftepp, &cond))
1098 pc = &vec_last(ftepp->conditions);
1099 pc->on = !pc->was_on && cond.on;
1100 pc->was_on = pc->was_on || pc->on;
1101 ftepp_update_output_condition(ftepp);
1104 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1105 if (!ftepp_else_allowed(ftepp))
1107 if (!ftepp_if(ftepp, &cond))
1109 pc = &vec_last(ftepp->conditions);
1110 pc->on = !pc->was_on && cond.on;
1111 pc->was_on = pc->was_on || pc->on;
1112 ftepp_update_output_condition(ftepp);
1115 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1116 if (!ftepp_if(ftepp, &cond))
1118 cond.was_on = cond.on;
1119 vec_push(ftepp->conditions, cond);
1120 ftepp->output_on = ftepp->output_on && cond.on;
1123 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1124 if (!ftepp_else_allowed(ftepp))
1126 pc = &vec_last(ftepp->conditions);
1127 pc->on = !pc->was_on;
1128 pc->had_else = true;
1130 ftepp_update_output_condition(ftepp);
1133 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1134 if (!vec_size(ftepp->conditions)) {
1135 ftepp_error(ftepp, "#endif without #if");
1138 vec_pop(ftepp->conditions);
1140 ftepp_update_output_condition(ftepp);
1143 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1144 return ftepp_include(ftepp);
1146 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1147 ftepp_out(ftepp, "#", false);
1151 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1154 /* break; never reached */
1156 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1159 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1162 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1165 /* Builtins! Don't forget the builtins! */
1166 case TOKEN_INTCONST:
1167 case TOKEN_FLOATCONST:
1168 ftepp_out(ftepp, "#", false);
1171 if (!ftepp_skipspace(ftepp))
1176 static bool ftepp_preprocess(ftepp_t *ftepp)
1179 bool newline = true;
1181 ftepp->lex->flags.preprocessing = true;
1182 ftepp->lex->flags.mergelines = false;
1183 ftepp->lex->flags.noops = true;
1188 if (ftepp->token >= TOKEN_EOF)
1191 ftepp->newline = newline;
1194 /* For the sake of FTE compatibility... FU, really */
1195 ftepp->newline = newline = true;
1198 switch (ftepp->token) {
1201 case TOKEN_TYPENAME:
1202 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1204 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1208 if (!ftepp_macro_call(ftepp, macro))
1209 ftepp->token = TOKEN_ERROR;
1212 if (!ftepp->newline) {
1213 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1217 ftepp->lex->flags.mergelines = true;
1218 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1219 ftepp_error(ftepp, "error in preprocessor directive");
1220 ftepp->token = TOKEN_ERROR;
1223 if (!ftepp_hash(ftepp))
1224 ftepp->token = TOKEN_ERROR;
1225 ftepp->lex->flags.mergelines = false;
1229 ftepp_out(ftepp, "\n", true);
1233 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1237 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1239 newline = ftepp->token == TOKEN_EOF;
1243 /* Like in parser.c - files keep the previous state so we have one global
1244 * preprocessor. Except here we will want to warn about dangling #ifs.
1246 static ftepp_t *ftepp;
1248 static bool ftepp_preprocess_done()
1251 lex_close(ftepp->lex);
1253 if (vec_size(ftepp->conditions)) {
1254 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1257 if (ftepp->itemname) {
1258 mem_d(ftepp->itemname);
1259 ftepp->itemname = NULL;
1264 bool ftepp_preprocess_file(const char *filename)
1266 ftepp->lex = lex_open(filename);
1267 ftepp->itemname = util_strdup(filename);
1269 con_out("failed to open file \"%s\"\n", filename);
1272 if (!ftepp_preprocess(ftepp))
1274 return ftepp_preprocess_done();
1277 bool ftepp_preprocess_string(const char *name, const char *str)
1279 ftepp->lex = lex_open_string(str, strlen(str), name);
1280 ftepp->itemname = util_strdup(name);
1282 con_out("failed to create lexer for string \"%s\"\n", name);
1285 if (!ftepp_preprocess(ftepp))
1287 return ftepp_preprocess_done();
1292 ftepp = ftepp_new();
1295 ftepp_add_define(NULL, "GMQCC");
1299 void ftepp_add_define(const char *source, const char *name)
1302 lex_ctx ctx = { "__builtin__", 0 };
1304 macro = ppmacro_new(ctx, name);
1305 vec_push(ftepp->macros, macro);
1308 const char *ftepp_get()
1310 return ftepp->output_string;
1315 vec_free(ftepp->output_string);
1322 ftepp_delete(ftepp);