6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 /* a copy from the lexer */
50 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
62 ppcondition *conditions;
73 char *(*func)(lex_file *);
77 * Implement the predef subsystem now. We can do this safely with the
78 * help of lexer contexts.
80 static uint32_t ftepp_predef_countval = 0;
81 static uint32_t ftepp_predef_randval = 0;
84 char *ftepp_predef_line(lex_file *context) {
85 char *value = (char*)mem_a(128);
86 sprintf(value, "%d", (int)context->line);
90 char *ftepp_predef_file(lex_file *context) {
91 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
92 char *value = (char*)mem_a(length);
93 memset (value, 0, length);
94 sprintf(value, "\"%s\"", context->name);
98 /* __COUNTER_LAST__ */
99 char *ftepp_predef_counterlast(lex_file *context) {
100 char *value = (char*)mem_a(128);
101 sprintf(value, "%u", ftepp_predef_countval);
107 char *ftepp_predef_counter(lex_file *context) {
108 char *value = (char*)mem_a(128);
109 ftepp_predef_countval ++;
110 sprintf(value, "%u", ftepp_predef_countval);
116 char *ftepp_predef_random(lex_file *context) {
117 char *value = (char*)mem_a(128);
118 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
119 sprintf(value, "%u", ftepp_predef_randval);
124 /* __RANDOM_LAST__ */
125 char *ftepp_predef_randomlast(lex_file *context) {
126 char *value = (char*)mem_a(128);
127 sprintf(value, "%u", ftepp_predef_randval);
133 static const predef_t ftepp_predefs[] = {
134 { "__LINE__", &ftepp_predef_line },
135 { "__FILE__", &ftepp_predef_file },
136 { "__COUNTER__", &ftepp_predef_counter },
137 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
138 { "__RANDOM__", &ftepp_predef_random },
139 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
142 #define ftepp_tokval(f) ((f)->lex->tok.value)
143 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
145 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
152 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
156 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
163 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
167 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
173 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
178 static pptoken *pptoken_make(ftepp_t *ftepp)
180 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
181 token->token = ftepp->token;
183 if (token->token == TOKEN_WHITE)
184 token->value = util_strdup(" ");
187 token->value = util_strdup(ftepp_tokval(ftepp));
189 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
193 static void pptoken_delete(pptoken *self)
199 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
201 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
204 memset(macro, 0, sizeof(*macro));
205 macro->name = util_strdup(name);
209 static void ppmacro_delete(ppmacro *self)
212 for (i = 0; i < vec_size(self->params); ++i)
213 mem_d(self->params[i]);
214 vec_free(self->params);
215 for (i = 0; i < vec_size(self->output); ++i)
216 pptoken_delete(self->output[i]);
217 vec_free(self->output);
222 static ftepp_t* ftepp_new()
226 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
227 memset(ftepp, 0, sizeof(*ftepp));
229 ftepp->output_on = true;
234 static void ftepp_delete(ftepp_t *self)
238 mem_d(self->itemname);
239 if (self->includename)
240 vec_free(self->includename);
241 for (i = 0; i < vec_size(self->macros); ++i)
242 ppmacro_delete(self->macros[i]);
243 vec_free(self->macros);
244 vec_free(self->conditions);
246 lex_close(self->lex);
250 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
252 if (ignore_cond || ftepp->output_on)
257 data = vec_add(ftepp->output_string, len);
258 memcpy(data, str, len);
262 static void ftepp_update_output_condition(ftepp_t *ftepp)
265 ftepp->output_on = true;
266 for (i = 0; i < vec_size(ftepp->conditions); ++i)
267 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
270 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
273 for (i = 0; i < vec_size(ftepp->macros); ++i) {
274 if (!strcmp(name, ftepp->macros[i]->name))
275 return ftepp->macros[i];
280 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
283 for (i = 0; i < vec_size(ftepp->macros); ++i) {
284 if (!strcmp(name, ftepp->macros[i]->name)) {
285 vec_remove(ftepp->macros, i, 1);
291 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
293 return (ftepp->token = lex_do(ftepp->lex));
296 /* Important: this does not skip newlines! */
297 static bool ftepp_skipspace(ftepp_t *ftepp)
299 if (ftepp->token != TOKEN_WHITE)
301 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
302 if (ftepp->token >= TOKEN_EOF) {
303 ftepp_error(ftepp, "unexpected end of preprocessor directive");
309 /* this one skips EOLs as well */
310 static bool ftepp_skipallwhite(ftepp_t *ftepp)
312 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
316 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
317 if (ftepp->token >= TOKEN_EOF) {
318 ftepp_error(ftepp, "unexpected end of preprocessor directive");
325 * The huge macro parsing code...
327 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
331 if (!ftepp_skipspace(ftepp))
333 if (ftepp->token == ')')
335 switch (ftepp->token) {
341 ftepp_error(ftepp, "unexpected token in parameter list");
344 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
346 if (!ftepp_skipspace(ftepp))
348 } while (ftepp->token == ',');
349 if (ftepp->token != ')') {
350 ftepp_error(ftepp, "expected closing paren after macro parameter list");
354 /* skipspace happens in ftepp_define */
358 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
361 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
362 ptok = pptoken_make(ftepp);
363 vec_push(macro->output, ptok);
366 /* recursive expansion can cause EOFs here */
367 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
368 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
374 static bool ftepp_define(ftepp_t *ftepp)
377 size_t l = ftepp_ctx(ftepp).line;
379 (void)ftepp_next(ftepp);
380 if (!ftepp_skipspace(ftepp))
383 switch (ftepp->token) {
387 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
388 if (macro && ftepp->output_on) {
389 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
391 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
393 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
396 ftepp_error(ftepp, "expected macro name");
400 (void)ftepp_next(ftepp);
402 if (ftepp->token == '(') {
403 macro->has_params = true;
404 if (!ftepp_define_params(ftepp, macro))
408 if (!ftepp_skipspace(ftepp))
411 if (!ftepp_define_body(ftepp, macro))
414 if (ftepp->output_on)
415 vec_push(ftepp->macros, macro);
417 ppmacro_delete(macro);
420 for (; l < ftepp_ctx(ftepp).line; ++l)
421 ftepp_out(ftepp, "\n", true);
426 * When a macro is used we have to handle parameters as well
427 * as special-concatenation via ## or stringification via #
429 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
430 * this kind of parens. Curly braces or [] don't count towards the
437 static void macroparam_clean(macroparam *self)
440 for (i = 0; i < vec_size(self->tokens); ++i)
441 pptoken_delete(self->tokens[i]);
442 vec_free(self->tokens);
445 /* need to leave the last token up */
446 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
448 macroparam *params = NULL;
454 if (!ftepp_skipallwhite(ftepp))
456 while (ftepp->token != ')') {
458 if (!ftepp_skipallwhite(ftepp))
460 while (parens || ftepp->token != ',') {
461 if (ftepp->token == '(')
463 else if (ftepp->token == ')') {
468 ptok = pptoken_make(ftepp);
469 vec_push(mp.tokens, ptok);
470 if (ftepp_next(ftepp) >= TOKEN_EOF) {
471 ftepp_error(ftepp, "unexpected EOF in macro call");
475 vec_push(params, mp);
477 if (ftepp->token == ')')
479 if (ftepp->token != ',') {
480 ftepp_error(ftepp, "expected closing paren or comma in macro call");
483 if (ftepp_next(ftepp) >= TOKEN_EOF) {
484 ftepp_error(ftepp, "unexpected EOF in macro call");
488 /* need to leave that up
489 if (ftepp_next(ftepp) >= TOKEN_EOF) {
490 ftepp_error(ftepp, "unexpected EOF in macro call");
494 *out_params = params;
499 macroparam_clean(&mp);
500 for (i = 0; i < vec_size(params); ++i)
501 macroparam_clean(¶ms[i]);
506 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
509 for (i = 0; i < vec_size(macro->params); ++i) {
510 if (!strcmp(macro->params[i], name)) {
518 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
523 switch (token->token) {
524 case TOKEN_STRINGCONST:
527 /* in preprocessor mode strings already are string,
528 * so we don't get actual newline bytes here.
529 * Still need to escape backslashes and quotes.
532 case '\\': ftepp_out(ftepp, "\\\\", false); break;
533 case '"': ftepp_out(ftepp, "\\\"", false); break;
536 ftepp_out(ftepp, chs, false);
543 ftepp_out(ftepp, " ", false);
546 ftepp_out(ftepp, "\\n", false);
549 ftepp_out(ftepp, token->value, false);
554 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
557 ftepp_out(ftepp, "\"", false);
558 for (i = 0; i < vec_size(param->tokens); ++i)
559 ftepp_stringify_token(ftepp, param->tokens[i]);
560 ftepp_out(ftepp, "\"", false);
563 static void ftepp_recursion_header(ftepp_t *ftepp)
565 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
568 static void ftepp_recursion_footer(ftepp_t *ftepp)
570 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
573 static bool ftepp_preprocess(ftepp_t *ftepp);
574 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
576 char *old_string = ftepp->output_string;
577 lex_file *old_lexer = ftepp->lex;
586 if (!vec_size(macro->output))
589 ftepp->output_string = NULL;
590 for (o = 0; o < vec_size(macro->output); ++o) {
591 pptoken *out = macro->output[o];
592 switch (out->token) {
596 if (!macro_params_find(macro, out->value, &pi)) {
597 ftepp_out(ftepp, out->value, false);
600 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
601 out = params[pi].tokens[pv];
602 if (out->token == TOKEN_EOL)
603 ftepp_out(ftepp, "\n", false);
605 ftepp_out(ftepp, out->value, false);
610 if (o + 1 < vec_size(macro->output)) {
611 nextok = macro->output[o+1]->token;
613 /* raw concatenation */
617 if ( (nextok == TOKEN_IDENT ||
618 nextok == TOKEN_KEYWORD ||
619 nextok == TOKEN_TYPENAME) &&
620 macro_params_find(macro, macro->output[o+1]->value, &pi))
623 ftepp_stringify(ftepp, ¶ms[pi]);
627 ftepp_out(ftepp, "#", false);
630 ftepp_out(ftepp, "\n", false);
633 ftepp_out(ftepp, out->value, false);
637 vec_push(ftepp->output_string, 0);
638 /* Now run the preprocessor recursively on this string buffer */
640 printf("__________\n%s\n=========\n", ftepp->output_string);
642 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
644 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
648 ftepp->output_string = old_string;
650 ftepp_recursion_header(ftepp);
651 if (!ftepp_preprocess(ftepp)) {
652 lex_close(ftepp->lex);
656 ftepp_recursion_footer(ftepp);
657 old_string = ftepp->output_string;
660 ftepp->lex = old_lexer;
661 ftepp->output_string = old_string;
665 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
668 macroparam *params = NULL;
671 if (!macro->has_params) {
672 if (!ftepp_macro_expand(ftepp, macro, NULL))
679 if (!ftepp_skipallwhite(ftepp))
682 if (ftepp->token != '(') {
683 ftepp_error(ftepp, "expected macro parameters in parenthesis");
688 if (!ftepp_macro_call_params(ftepp, ¶ms))
691 if (vec_size(params) != vec_size(macro->params)) {
692 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
693 (unsigned int)vec_size(macro->params),
694 (unsigned int)vec_size(params));
699 if (!ftepp_macro_expand(ftepp, macro, params))
704 for (o = 0; o < vec_size(params); ++o)
705 macroparam_clean(¶ms[o]);
711 * #if - the FTEQCC way:
712 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
713 * <numbers> => True if the number is not 0
714 * !<factor> => True if the factor yields false
715 * !!<factor> => ERROR on 2 or more unary nots
716 * <macro> => becomes the macro's FIRST token regardless of parameters
717 * <e> && <e> => True if both expressions are true
718 * <e> || <e> => True if either expression is true
720 * <ident> => False (remember for macros the <macro> rule applies instead)
721 * Unary + and - are weird and wrong in fteqcc so we don't allow them
722 * parenthesis in expressions are allowed
723 * parameter lists on macros are errors
724 * No mathematical calculations are executed
726 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
727 static bool ftepp_if_op(ftepp_t *ftepp)
729 ftepp->lex->flags.noops = false;
731 if (!ftepp_skipspace(ftepp))
733 ftepp->lex->flags.noops = true;
736 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
741 if (!ftepp_skipspace(ftepp))
744 while (ftepp->token == '!') {
747 if (!ftepp_skipspace(ftepp))
751 switch (ftepp->token) {
755 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
757 if (!ftepp_skipspace(ftepp))
759 if (ftepp->token != '(') {
760 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
764 if (!ftepp_skipspace(ftepp))
766 if (ftepp->token != TOKEN_IDENT &&
767 ftepp->token != TOKEN_TYPENAME &&
768 ftepp->token != TOKEN_KEYWORD)
770 ftepp_error(ftepp, "defined() used on an unexpected token type");
773 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
776 if (!ftepp_skipspace(ftepp))
778 if (ftepp->token != ')') {
779 ftepp_error(ftepp, "expected closing paren");
785 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
786 if (!macro || !vec_size(macro->output)) {
790 /* This does not expand recursively! */
791 switch (macro->output[0]->token) {
793 *value_out = macro->output[0]->constval.i;
794 *out = !!(macro->output[0]->constval.i);
796 case TOKEN_FLOATCONST:
797 *value_out = macro->output[0]->constval.f;
798 *out = !!(macro->output[0]->constval.f);
806 case TOKEN_STRINGCONST:
810 *value_out = ftepp->lex->tok.constval.i;
811 *out = !!(ftepp->lex->tok.constval.i);
813 case TOKEN_FLOATCONST:
814 *value_out = ftepp->lex->tok.constval.f;
815 *out = !!(ftepp->lex->tok.constval.f);
820 if (!ftepp_if_expr(ftepp, out, value_out))
822 if (ftepp->token != ')') {
823 ftepp_error(ftepp, "expected closing paren in #if expression");
829 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
834 *value_out = (*out ? 1 : 0);
840 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
842 if (!ftepp_next(ftepp))
844 return ftepp_if_value(ftepp, out, value_out);
848 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
850 if (!ftepp_if_value(ftepp, out, value_out))
853 if (!ftepp_if_op(ftepp))
856 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
859 /* FTEQCC is all right-associative and no precedence here */
860 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
861 !strcmp(ftepp_tokval(ftepp), "||"))
864 char opc = ftepp_tokval(ftepp)[0];
868 if (!ftepp_next(ftepp))
870 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
878 *value_out = (*out ? 1 : 0);
881 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
882 !strcmp(ftepp_tokval(ftepp), "!=") ||
883 !strcmp(ftepp_tokval(ftepp), ">=") ||
884 !strcmp(ftepp_tokval(ftepp), "<=") ||
885 !strcmp(ftepp_tokval(ftepp), ">") ||
886 !strcmp(ftepp_tokval(ftepp), "<"))
889 const char opc0 = ftepp_tokval(ftepp)[0];
890 const char opc1 = ftepp_tokval(ftepp)[1];
893 if (!ftepp_next(ftepp))
895 if (!ftepp_if_expr(ftepp, &next, &other))
899 *out = (*value_out == other);
900 else if (opc0 == '!')
901 *out = (*value_out != other);
902 else if (opc0 == '>') {
903 if (opc1 == '=') *out = (*value_out >= other);
904 else *out = (*value_out > other);
906 else if (opc0 == '<') {
907 if (opc1 == '=') *out = (*value_out <= other);
908 else *out = (*value_out < other);
910 *value_out = (*out ? 1 : 0);
915 ftepp_error(ftepp, "junk after #if");
920 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
925 memset(cond, 0, sizeof(*cond));
926 (void)ftepp_next(ftepp);
928 if (!ftepp_skipspace(ftepp))
930 if (ftepp->token == TOKEN_EOL) {
931 ftepp_error(ftepp, "expected expression for #if-directive");
935 if (!ftepp_if_expr(ftepp, &result, &dummy))
943 * ifdef is rather simple
945 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
948 memset(cond, 0, sizeof(*cond));
949 (void)ftepp_next(ftepp);
950 if (!ftepp_skipspace(ftepp))
953 switch (ftepp->token) {
957 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
960 ftepp_error(ftepp, "expected macro name");
964 (void)ftepp_next(ftepp);
965 if (!ftepp_skipspace(ftepp))
967 /* relaxing this condition
968 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
969 ftepp_error(ftepp, "stray tokens after #ifdef");
978 * undef is also simple
980 static bool ftepp_undef(ftepp_t *ftepp)
982 (void)ftepp_next(ftepp);
983 if (!ftepp_skipspace(ftepp))
986 if (ftepp->output_on) {
987 switch (ftepp->token) {
991 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
994 ftepp_error(ftepp, "expected macro name");
999 (void)ftepp_next(ftepp);
1000 if (!ftepp_skipspace(ftepp))
1002 /* relaxing this condition
1003 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1004 ftepp_error(ftepp, "stray tokens after #ifdef");
1011 /* Special unescape-string function which skips a leading quote
1012 * and stops at a quote, not just at \0
1014 static void unescape(const char *str, char *out) {
1016 while (*str && *str != '"') {
1020 case '\\': *out++ = *str; break;
1021 case '"': *out++ = *str; break;
1022 case 'a': *out++ = '\a'; break;
1023 case 'b': *out++ = '\b'; break;
1024 case 'r': *out++ = '\r'; break;
1025 case 'n': *out++ = '\n'; break;
1026 case 't': *out++ = '\t'; break;
1027 case 'f': *out++ = '\f'; break;
1028 case 'v': *out++ = '\v'; break;
1043 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1046 char *filename = NULL;
1047 const char *last_slash;
1053 last_slash = strrchr(pathfile, '/');
1056 len = last_slash - pathfile;
1057 memcpy(vec_add(filename, len), pathfile, len);
1058 vec_push(filename, '/');
1062 memcpy(vec_add(filename, len+1), file, len);
1063 vec_last(filename) = 0;
1065 fp = file_open(filename, "rb");
1074 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1076 char *filename = NULL;
1078 filename = ftepp_include_find_path(file, ftepp->includename);
1080 filename = ftepp_include_find_path(file, ftepp->itemname);
1084 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1085 char *message = NULL;
1087 if (!ftepp_skipspace(ftepp))
1090 /* handle the odd non string constant case so it works like C */
1091 if (ftepp->token != TOKEN_STRINGCONST) {
1093 vec_upload(message, "#warning", 8);
1095 while (ftepp->token != TOKEN_EOL) {
1096 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1099 vec_push(message, '\0');
1100 store = ftepp_warn(ftepp, WARN_CPP, message);
1105 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1106 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1109 static void ftepp_directive_error(ftepp_t *ftepp) {
1110 char *message = NULL;
1112 if (!ftepp_skipspace(ftepp))
1115 /* handle the odd non string constant case so it works like C */
1116 if (ftepp->token != TOKEN_STRINGCONST) {
1117 vec_upload(message, "#error", 6);
1119 while (ftepp->token != TOKEN_EOL) {
1120 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1123 vec_push(message, '\0');
1124 ftepp_error(ftepp, message);
1129 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1130 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1135 * FIXME: do we need/want a -I option?
1136 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1138 static bool ftepp_include(ftepp_t *ftepp)
1140 lex_file *old_lexer = ftepp->lex;
1145 char *old_includename;
1147 (void)ftepp_next(ftepp);
1148 if (!ftepp_skipspace(ftepp))
1151 if (ftepp->token != TOKEN_STRINGCONST) {
1152 ftepp_error(ftepp, "expected filename to include");
1156 ctx = ftepp_ctx(ftepp);
1158 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1160 ftepp_out(ftepp, "\n#pragma file(", false);
1161 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1162 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1164 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1166 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1169 inlex = lex_open(filename);
1171 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1176 old_includename = ftepp->includename;
1177 ftepp->includename = filename;
1178 if (!ftepp_preprocess(ftepp)) {
1179 vec_free(ftepp->includename);
1180 ftepp->includename = old_includename;
1181 lex_close(ftepp->lex);
1182 ftepp->lex = old_lexer;
1185 vec_free(ftepp->includename);
1186 ftepp->includename = old_includename;
1187 lex_close(ftepp->lex);
1188 ftepp->lex = old_lexer;
1190 ftepp_out(ftepp, "\n#pragma file(", false);
1191 ftepp_out(ftepp, ctx.file, false);
1192 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1193 ftepp_out(ftepp, lineno, false);
1196 (void)ftepp_next(ftepp);
1197 if (!ftepp_skipspace(ftepp))
1199 if (ftepp->token != TOKEN_EOL) {
1200 ftepp_error(ftepp, "stray tokens after #include");
1203 (void)ftepp_next(ftepp);
1208 /* Basic structure handlers */
1209 static bool ftepp_else_allowed(ftepp_t *ftepp)
1211 if (!vec_size(ftepp->conditions)) {
1212 ftepp_error(ftepp, "#else without #if");
1215 if (vec_last(ftepp->conditions).had_else) {
1216 ftepp_error(ftepp, "multiple #else for a single #if");
1222 static bool ftepp_hash(ftepp_t *ftepp)
1227 lex_ctx ctx = ftepp_ctx(ftepp);
1229 if (!ftepp_skipspace(ftepp))
1232 switch (ftepp->token) {
1235 case TOKEN_TYPENAME:
1236 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1237 return ftepp_define(ftepp);
1239 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1240 return ftepp_undef(ftepp);
1242 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1243 if (!ftepp_ifdef(ftepp, &cond))
1245 cond.was_on = cond.on;
1246 vec_push(ftepp->conditions, cond);
1247 ftepp->output_on = ftepp->output_on && cond.on;
1250 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1251 if (!ftepp_ifdef(ftepp, &cond))
1254 cond.was_on = cond.on;
1255 vec_push(ftepp->conditions, cond);
1256 ftepp->output_on = ftepp->output_on && cond.on;
1259 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1260 if (!ftepp_else_allowed(ftepp))
1262 if (!ftepp_ifdef(ftepp, &cond))
1264 pc = &vec_last(ftepp->conditions);
1265 pc->on = !pc->was_on && cond.on;
1266 pc->was_on = pc->was_on || pc->on;
1267 ftepp_update_output_condition(ftepp);
1270 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1271 if (!ftepp_else_allowed(ftepp))
1273 if (!ftepp_ifdef(ftepp, &cond))
1276 pc = &vec_last(ftepp->conditions);
1277 pc->on = !pc->was_on && cond.on;
1278 pc->was_on = pc->was_on || pc->on;
1279 ftepp_update_output_condition(ftepp);
1282 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1283 if (!ftepp_else_allowed(ftepp))
1285 if (!ftepp_if(ftepp, &cond))
1287 pc = &vec_last(ftepp->conditions);
1288 pc->on = !pc->was_on && cond.on;
1289 pc->was_on = pc->was_on || pc->on;
1290 ftepp_update_output_condition(ftepp);
1293 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1294 if (!ftepp_if(ftepp, &cond))
1296 cond.was_on = cond.on;
1297 vec_push(ftepp->conditions, cond);
1298 ftepp->output_on = ftepp->output_on && cond.on;
1301 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1302 if (!ftepp_else_allowed(ftepp))
1304 pc = &vec_last(ftepp->conditions);
1305 pc->on = !pc->was_on;
1306 pc->had_else = true;
1308 ftepp_update_output_condition(ftepp);
1311 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1312 if (!vec_size(ftepp->conditions)) {
1313 ftepp_error(ftepp, "#endif without #if");
1316 vec_pop(ftepp->conditions);
1318 ftepp_update_output_condition(ftepp);
1321 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1322 return ftepp_include(ftepp);
1324 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1325 ftepp_out(ftepp, "#", false);
1328 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1329 ftepp_directive_warning(ftepp);
1332 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1333 ftepp_directive_error(ftepp);
1337 if (ftepp->output_on) {
1338 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1345 /* break; never reached */
1347 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1350 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1353 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1356 /* Builtins! Don't forget the builtins! */
1357 case TOKEN_INTCONST:
1358 case TOKEN_FLOATCONST:
1359 ftepp_out(ftepp, "#", false);
1362 if (!ftepp_skipspace(ftepp))
1367 static bool ftepp_preprocess(ftepp_t *ftepp)
1370 bool newline = true;
1373 char *expand = NULL;
1376 ftepp->lex->flags.preprocessing = true;
1377 ftepp->lex->flags.mergelines = false;
1378 ftepp->lex->flags.noops = true;
1383 if (ftepp->token >= TOKEN_EOF)
1389 switch (ftepp->token) {
1392 case TOKEN_TYPENAME:
1393 /* is it a predef? */
1394 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1395 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1396 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1397 expand = ftepp_predefs[i].func(ftepp->lex);
1398 ftepp_out(ftepp, expand, false);
1399 ftepp_next(ftepp); /* skip */
1401 mem_d(expand); /* free memory */
1407 if (ftepp->output_on)
1408 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1413 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1417 if (!ftepp_macro_call(ftepp, macro))
1418 ftepp->token = TOKEN_ERROR;
1422 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1426 ftepp->lex->flags.mergelines = true;
1427 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1428 ftepp_error(ftepp, "error in preprocessor directive");
1429 ftepp->token = TOKEN_ERROR;
1432 if (!ftepp_hash(ftepp))
1433 ftepp->token = TOKEN_ERROR;
1434 ftepp->lex->flags.mergelines = false;
1438 ftepp_out(ftepp, "\n", true);
1442 /* same as default but don't set newline=false */
1443 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1448 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1452 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1454 /* force a 0 at the end but don't count it as added to the output */
1455 vec_push(ftepp->output_string, 0);
1456 vec_shrinkby(ftepp->output_string, 1);
1458 return (ftepp->token == TOKEN_EOF);
1461 /* Like in parser.c - files keep the previous state so we have one global
1462 * preprocessor. Except here we will want to warn about dangling #ifs.
1464 static ftepp_t *ftepp;
1466 static bool ftepp_preprocess_done()
1469 if (vec_size(ftepp->conditions)) {
1470 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1473 lex_close(ftepp->lex);
1475 if (ftepp->itemname) {
1476 mem_d(ftepp->itemname);
1477 ftepp->itemname = NULL;
1482 bool ftepp_preprocess_file(const char *filename)
1484 ftepp->lex = lex_open(filename);
1485 ftepp->itemname = util_strdup(filename);
1487 con_out("failed to open file \"%s\"\n", filename);
1490 if (!ftepp_preprocess(ftepp))
1492 return ftepp_preprocess_done();
1495 bool ftepp_preprocess_string(const char *name, const char *str)
1497 ftepp->lex = lex_open_string(str, strlen(str), name);
1498 ftepp->itemname = util_strdup(name);
1500 con_out("failed to create lexer for string \"%s\"\n", name);
1503 if (!ftepp_preprocess(ftepp))
1505 return ftepp_preprocess_done();
1509 void ftepp_add_macro(const char *name, const char *value) {
1510 char *create = NULL;
1512 /* use saner path for empty macros */
1514 ftepp_add_define("__builtin__", name);
1518 vec_upload(create, "#define ", 8);
1519 vec_upload(create, name, strlen(name));
1520 vec_push (create, ' ');
1521 vec_upload(create, value, strlen(value));
1522 vec_push (create, 0);
1524 ftepp_preprocess_string("__builtin__", create);
1533 ftepp = ftepp_new();
1537 memset(minor, 0, sizeof(minor));
1538 memset(major, 0, sizeof(major));
1540 /* set the right macro based on the selected standard */
1541 ftepp_add_define(NULL, "GMQCC");
1542 if (opts.standard == COMPILER_FTEQCC) {
1543 ftepp_add_define(NULL, "__STD_FTEQCC__");
1552 } else if (opts.standard == COMPILER_GMQCC) {
1553 ftepp_add_define(NULL, "__STD_GMQCC__");
1554 sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR);
1555 sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR);
1556 } else if (opts.standard == COMPILER_QCC) {
1557 ftepp_add_define(NULL, "__STD_QCC__");
1568 ftepp_add_macro("__STD_VERSION_MINOR__", minor);
1569 ftepp_add_macro("__STD_VERSION_MAJOR__", major);
1574 void ftepp_add_define(const char *source, const char *name)
1577 lex_ctx ctx = { "__builtin__", 0 };
1579 macro = ppmacro_new(ctx, name);
1580 vec_push(ftepp->macros, macro);
1583 const char *ftepp_get()
1585 return ftepp->output_string;
1590 vec_free(ftepp->output_string);
1597 ftepp_delete(ftepp);