2 * Copyright (C) 2012, 2013
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
28 #define HT_MACROS 1024
38 /* a copy from the lexer */
52 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
59 typedef struct ftepp_s {
65 ppcondition *conditions;
67 ht macros; /* hashtable<string, ppmacro*> */
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_date(lex_file *context) {
85 struct tm *itime = NULL;
87 char *value = (char*)mem_a(82);
88 /* 82 is enough for strftime but we also have " " in our string */
96 localtime_s(itime, &rtime);
98 itime = localtime(&rtime);
101 strftime(value, 82, "\"%b %d %Y\"", itime);
107 char *ftepp_predef_time(lex_file *context) {
108 struct tm *itime = NULL;
110 char *value = (char*)mem_a(82);
111 /* 82 is enough for strftime but we also have " " in our string */
119 localtime_s(itime, &rtime);
121 itime = localtime(&rtime);
124 strftime(value, 82, "\"%X\"", itime);
130 char *ftepp_predef_line(lex_file *context) {
132 util_asprintf(&value, "%d", (int)context->line);
136 char *ftepp_predef_file(lex_file *context) {
137 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
138 char *value = (char*)mem_a(length);
139 util_snprintf(value, length, "\"%s\"", context->name);
143 /* __COUNTER_LAST__ */
144 char *ftepp_predef_counterlast(lex_file *context) {
146 util_asprintf(&value, "%u", ftepp_predef_countval);
152 char *ftepp_predef_counter(lex_file *context) {
154 ftepp_predef_countval ++;
155 util_asprintf(&value, "%u", ftepp_predef_countval);
161 char *ftepp_predef_random(lex_file *context) {
163 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
164 util_asprintf(&value, "%u", ftepp_predef_randval);
169 /* __RANDOM_LAST__ */
170 char *ftepp_predef_randomlast(lex_file *context) {
172 util_asprintf(&value, "%u", ftepp_predef_randval);
178 const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = {
179 { "__LINE__", &ftepp_predef_line },
180 { "__FILE__", &ftepp_predef_file },
181 { "__COUNTER__", &ftepp_predef_counter },
182 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
183 { "__RANDOM__", &ftepp_predef_random },
184 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
185 { "__DATE__", &ftepp_predef_date },
186 { "__TIME__", &ftepp_predef_time }
189 #define ftepp_tokval(f) ((f)->lex->tok.value)
190 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
192 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
199 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
203 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
210 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
214 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
220 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
225 static pptoken *pptoken_make(ftepp_t *ftepp)
227 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
228 token->token = ftepp->token;
230 if (token->token == TOKEN_WHITE)
231 token->value = util_strdup(" ");
234 token->value = util_strdup(ftepp_tokval(ftepp));
236 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
240 static GMQCC_INLINE void pptoken_delete(pptoken *self)
246 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
248 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
251 memset(macro, 0, sizeof(*macro));
252 macro->name = util_strdup(name);
256 static void ppmacro_delete(ppmacro *self)
259 for (i = 0; i < vec_size(self->params); ++i)
260 mem_d(self->params[i]);
261 vec_free(self->params);
262 for (i = 0; i < vec_size(self->output); ++i)
263 pptoken_delete(self->output[i]);
264 vec_free(self->output);
269 static ftepp_t* ftepp_new()
273 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
274 memset(ftepp, 0, sizeof(*ftepp));
276 ftepp->macros = util_htnew(HT_MACROS);
277 ftepp->output_on = true;
282 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
284 vec_free(self->output_string);
287 static void ftepp_delete(ftepp_t *self)
289 ftepp_flush_do(self);
291 mem_d(self->itemname);
292 if (self->includename)
293 vec_free(self->includename);
295 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
297 vec_free(self->conditions);
299 lex_close(self->lex);
303 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
305 if (ignore_cond || ftepp->output_on)
310 data = vec_add(ftepp->output_string, len);
311 memcpy(data, str, len);
315 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
318 ftepp->output_on = true;
319 for (i = 0; i < vec_size(ftepp->conditions); ++i)
320 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
323 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
325 return (ppmacro*)util_htget(ftepp->macros, name);
328 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
330 util_htrm(ftepp->macros, name, NULL);
333 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
335 return (ftepp->token = lex_do(ftepp->lex));
338 /* Important: this does not skip newlines! */
339 static bool ftepp_skipspace(ftepp_t *ftepp)
341 if (ftepp->token != TOKEN_WHITE)
343 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
344 if (ftepp->token >= TOKEN_EOF) {
345 ftepp_error(ftepp, "unexpected end of preprocessor directive");
351 /* this one skips EOLs as well */
352 static bool ftepp_skipallwhite(ftepp_t *ftepp)
354 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
358 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
359 if (ftepp->token >= TOKEN_EOF) {
360 ftepp_error(ftepp, "unexpected end of preprocessor directive");
367 * The huge macro parsing code...
369 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
373 if (!ftepp_skipspace(ftepp))
375 if (ftepp->token == ')')
377 switch (ftepp->token) {
381 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
384 macro->variadic = true;
387 ftepp_error(ftepp, "unexpected token in parameter list");
391 if (!ftepp_skipspace(ftepp))
393 if (macro->variadic && ftepp->token != ')') {
394 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
397 } while (ftepp->token == ',');
399 if (ftepp->token != ')') {
400 ftepp_error(ftepp, "expected closing paren after macro parameter list");
404 /* skipspace happens in ftepp_define */
408 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
411 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
412 bool subscript = false;
414 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
415 subscript = !!(ftepp_next(ftepp) == '#');
417 if (subscript && ftepp_next(ftepp) != '#') {
418 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
420 } else if (subscript) {
421 if (ftepp_next(ftepp) == '[') {
422 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
423 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
427 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
429 if (ftepp_next(ftepp) != ']') {
430 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
435 * mark it as an array to be handled later as such and not
436 * as traditional __VA_ARGS__
438 ftepp->token = TOKEN_VA_ARGS_ARRAY;
439 ptok = pptoken_make(ftepp);
440 ptok->constval.i = index;
441 vec_push(macro->output, ptok);
444 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
448 int old = ftepp->token;
449 ftepp->token = TOKEN_VA_ARGS;
450 ptok = pptoken_make(ftepp);
451 vec_push(macro->output, ptok);
455 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
456 ftepp->token = TOKEN_VA_COUNT;
457 ptok = pptoken_make(ftepp);
458 vec_push(macro->output, ptok);
461 ptok = pptoken_make(ftepp);
462 vec_push(macro->output, ptok);
466 /* recursive expansion can cause EOFs here */
467 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
468 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
474 static bool ftepp_define(ftepp_t *ftepp)
476 ppmacro *macro = NULL;
477 size_t l = ftepp_ctx(ftepp).line;
479 (void)ftepp_next(ftepp);
480 if (!ftepp_skipspace(ftepp))
483 switch (ftepp->token) {
487 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
488 if (macro && ftepp->output_on) {
489 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
491 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
493 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
496 ftepp_error(ftepp, "expected macro name");
500 (void)ftepp_next(ftepp);
502 if (ftepp->token == '(') {
503 macro->has_params = true;
504 if (!ftepp_define_params(ftepp, macro)) {
505 ppmacro_delete(macro);
510 if (!ftepp_skipspace(ftepp)) {
511 ppmacro_delete(macro);
515 if (!ftepp_define_body(ftepp, macro)) {
516 ppmacro_delete(macro);
521 if (ftepp->output_on)
522 vec_push(ftepp->macros, macro);
524 if (ftepp->output_on)
525 util_htset(ftepp->macros, macro->name, (void*)macro);
527 ppmacro_delete(macro);
530 for (; l < ftepp_ctx(ftepp).line; ++l)
531 ftepp_out(ftepp, "\n", true);
536 * When a macro is used we have to handle parameters as well
537 * as special-concatenation via ## or stringification via #
539 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
540 * this kind of parens. Curly braces or [] don't count towards the
547 static void macroparam_clean(macroparam *self)
550 for (i = 0; i < vec_size(self->tokens); ++i)
551 pptoken_delete(self->tokens[i]);
552 vec_free(self->tokens);
555 /* need to leave the last token up */
556 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
558 macroparam *params = NULL;
564 if (!ftepp_skipallwhite(ftepp))
566 while (ftepp->token != ')') {
568 if (!ftepp_skipallwhite(ftepp))
570 while (parens || ftepp->token != ',') {
571 if (ftepp->token == '(')
573 else if (ftepp->token == ')') {
578 ptok = pptoken_make(ftepp);
579 vec_push(mp.tokens, ptok);
580 if (ftepp_next(ftepp) >= TOKEN_EOF) {
581 ftepp_error(ftepp, "unexpected EOF in macro call");
585 vec_push(params, mp);
587 if (ftepp->token == ')')
589 if (ftepp->token != ',') {
590 ftepp_error(ftepp, "expected closing paren or comma in macro call");
593 if (ftepp_next(ftepp) >= TOKEN_EOF) {
594 ftepp_error(ftepp, "unexpected EOF in macro call");
598 /* need to leave that up
599 if (ftepp_next(ftepp) >= TOKEN_EOF) {
600 ftepp_error(ftepp, "unexpected EOF in macro call");
604 *out_params = params;
609 macroparam_clean(&mp);
610 for (i = 0; i < vec_size(params); ++i)
611 macroparam_clean(¶ms[i]);
616 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
619 for (i = 0; i < vec_size(macro->params); ++i) {
620 if (!strcmp(macro->params[i], name)) {
628 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
633 switch (token->token) {
634 case TOKEN_STRINGCONST:
637 /* in preprocessor mode strings already are string,
638 * so we don't get actual newline bytes here.
639 * Still need to escape backslashes and quotes.
642 case '\\': ftepp_out(ftepp, "\\\\", false); break;
643 case '"': ftepp_out(ftepp, "\\\"", false); break;
646 ftepp_out(ftepp, chs, false);
653 ftepp_out(ftepp, " ", false);
656 ftepp_out(ftepp, "\\n", false);
659 ftepp_out(ftepp, token->value, false);
664 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
667 ftepp_out(ftepp, "\"", false);
668 for (i = 0; i < vec_size(param->tokens); ++i)
669 ftepp_stringify_token(ftepp, param->tokens[i]);
670 ftepp_out(ftepp, "\"", false);
673 static void ftepp_recursion_header(ftepp_t *ftepp)
675 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
678 static void ftepp_recursion_footer(ftepp_t *ftepp)
680 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
683 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
687 for (i = 0; i < vec_size(param->tokens); ++i) {
688 out = param->tokens[i];
689 if (out->token == TOKEN_EOL)
690 ftepp_out(ftepp, "\n", false);
692 ftepp_out(ftepp, out->value, false);
696 static bool ftepp_preprocess(ftepp_t *ftepp);
697 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
700 char *old_string = ftepp->output_string;
702 lex_file *old_lexer = ftepp->lex;
703 size_t vararg_start = vec_size(macro->params);
715 if (vararg_start < vec_size(params))
716 varargs = vec_size(params) - vararg_start;
721 if (!vec_size(macro->output))
724 ftepp->output_string = NULL;
725 for (o = 0; o < vec_size(macro->output); ++o) {
726 pptoken *out = macro->output[o];
727 switch (out->token) {
729 if (!macro->variadic) {
730 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
731 vec_free(old_string);
738 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
739 for (++pi; pi < varargs; ++pi) {
740 ftepp_out(ftepp, ", ", false);
741 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
745 case TOKEN_VA_ARGS_ARRAY:
746 if ((size_t)out->constval.i >= varargs) {
747 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
748 vec_free(old_string);
752 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
756 util_asprintf(&buffer, "%d", varargs);
757 ftepp_out(ftepp, buffer, false);
764 if (!macro_params_find(macro, out->value, &pi)) {
765 ftepp_out(ftepp, out->value, false);
768 ftepp_param_out(ftepp, ¶ms[pi]);
771 if (o + 1 < vec_size(macro->output)) {
772 nextok = macro->output[o+1]->token;
774 /* raw concatenation */
778 if ( (nextok == TOKEN_IDENT ||
779 nextok == TOKEN_KEYWORD ||
780 nextok == TOKEN_TYPENAME) &&
781 macro_params_find(macro, macro->output[o+1]->value, &pi))
784 ftepp_stringify(ftepp, ¶ms[pi]);
788 ftepp_out(ftepp, "#", false);
791 ftepp_out(ftepp, "\n", false);
794 ftepp_out(ftepp, out->value, false);
798 vec_push(ftepp->output_string, 0);
799 /* Now run the preprocessor recursively on this string buffer */
801 printf("__________\n%s\n=========\n", ftepp->output_string);
803 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
805 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
810 inlex->line = ftepp->lex->line;
811 inlex->sline = ftepp->lex->sline;
814 old_inmacro = ftepp->in_macro;
815 ftepp->in_macro = true;
816 ftepp->output_string = NULL;
817 if (!ftepp_preprocess(ftepp)) {
818 ftepp->in_macro = old_inmacro;
819 vec_free(ftepp->lex->open_string);
820 vec_free(ftepp->output_string);
821 lex_close(ftepp->lex);
825 ftepp->in_macro = old_inmacro;
826 vec_free(ftepp->lex->open_string);
827 lex_close(ftepp->lex);
829 inner_string = ftepp->output_string;
830 ftepp->output_string = old_string;
832 has_newlines = (strchr(inner_string, '\n') != NULL);
834 if (has_newlines && !old_inmacro)
835 ftepp_recursion_header(ftepp);
837 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
838 vec_free(inner_string);
840 if (has_newlines && !old_inmacro)
841 ftepp_recursion_footer(ftepp);
843 if (resetline && !ftepp->in_macro) {
845 util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
846 ftepp_out(ftepp, lineno, false);
849 old_string = ftepp->output_string;
851 ftepp->lex = old_lexer;
852 ftepp->output_string = old_string;
856 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
859 macroparam *params = NULL;
863 if (!macro->has_params) {
864 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
871 if (!ftepp_skipallwhite(ftepp))
874 if (ftepp->token != '(') {
875 ftepp_error(ftepp, "expected macro parameters in parenthesis");
880 paramline = ftepp->lex->sline;
881 if (!ftepp_macro_call_params(ftepp, ¶ms))
884 if ( vec_size(params) < vec_size(macro->params) ||
885 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
887 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
888 (macro->variadic ? " at least" : ""),
889 (unsigned int)vec_size(macro->params),
890 (unsigned int)vec_size(params));
895 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
900 for (o = 0; o < vec_size(params); ++o)
901 macroparam_clean(¶ms[o]);
907 * #if - the FTEQCC way:
908 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
909 * <numbers> => True if the number is not 0
910 * !<factor> => True if the factor yields false
911 * !!<factor> => ERROR on 2 or more unary nots
912 * <macro> => becomes the macro's FIRST token regardless of parameters
913 * <e> && <e> => True if both expressions are true
914 * <e> || <e> => True if either expression is true
916 * <ident> => False (remember for macros the <macro> rule applies instead)
917 * Unary + and - are weird and wrong in fteqcc so we don't allow them
918 * parenthesis in expressions are allowed
919 * parameter lists on macros are errors
920 * No mathematical calculations are executed
922 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
923 static bool ftepp_if_op(ftepp_t *ftepp)
925 ftepp->lex->flags.noops = false;
927 if (!ftepp_skipspace(ftepp))
929 ftepp->lex->flags.noops = true;
932 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
938 if (!ftepp_skipspace(ftepp))
941 while (ftepp->token == '!') {
944 if (!ftepp_skipspace(ftepp))
948 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
952 if (!ftepp_skipspace(ftepp))
956 switch (ftepp->token) {
960 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
962 if (!ftepp_skipspace(ftepp))
964 if (ftepp->token != '(') {
965 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
969 if (!ftepp_skipspace(ftepp))
971 if (ftepp->token != TOKEN_IDENT &&
972 ftepp->token != TOKEN_TYPENAME &&
973 ftepp->token != TOKEN_KEYWORD)
975 ftepp_error(ftepp, "defined() used on an unexpected token type");
978 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
981 if (!ftepp_skipspace(ftepp))
983 if (ftepp->token != ')') {
984 ftepp_error(ftepp, "expected closing paren");
990 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
991 if (!macro || !vec_size(macro->output)) {
995 /* This does not expand recursively! */
996 switch (macro->output[0]->token) {
998 *value_out = macro->output[0]->constval.i;
999 *out = !!(macro->output[0]->constval.i);
1001 case TOKEN_FLOATCONST:
1002 *value_out = macro->output[0]->constval.f;
1003 *out = !!(macro->output[0]->constval.f);
1011 case TOKEN_STRINGCONST:
1015 case TOKEN_INTCONST:
1016 *value_out = ftepp->lex->tok.constval.i;
1017 *out = !!(ftepp->lex->tok.constval.i);
1019 case TOKEN_FLOATCONST:
1020 *value_out = ftepp->lex->tok.constval.f;
1021 *out = !!(ftepp->lex->tok.constval.f);
1026 if (!ftepp_if_expr(ftepp, out, value_out))
1028 if (ftepp->token != ')') {
1029 ftepp_error(ftepp, "expected closing paren in #if expression");
1035 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1036 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1037 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1041 *value_out = -*value_out;
1044 *value_out = (*out ? 1 : 0);
1050 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1052 if (!ftepp_next(ftepp))
1054 return ftepp_if_value(ftepp, out, value_out);
1058 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1060 if (!ftepp_if_value(ftepp, out, value_out))
1063 if (!ftepp_if_op(ftepp))
1066 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1069 /* FTEQCC is all right-associative and no precedence here */
1070 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1071 !strcmp(ftepp_tokval(ftepp), "||"))
1074 char opc = ftepp_tokval(ftepp)[0];
1078 if (!ftepp_next(ftepp))
1080 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1084 *out = *out && next;
1086 *out = *out || next;
1088 *value_out = (*out ? 1 : 0);
1091 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1092 !strcmp(ftepp_tokval(ftepp), "!=") ||
1093 !strcmp(ftepp_tokval(ftepp), ">=") ||
1094 !strcmp(ftepp_tokval(ftepp), "<=") ||
1095 !strcmp(ftepp_tokval(ftepp), ">") ||
1096 !strcmp(ftepp_tokval(ftepp), "<"))
1099 const char opc0 = ftepp_tokval(ftepp)[0];
1100 const char opc1 = ftepp_tokval(ftepp)[1];
1103 if (!ftepp_next(ftepp))
1105 if (!ftepp_if_expr(ftepp, &next, &other))
1109 *out = (*value_out == other);
1110 else if (opc0 == '!')
1111 *out = (*value_out != other);
1112 else if (opc0 == '>') {
1113 if (opc1 == '=') *out = (*value_out >= other);
1114 else *out = (*value_out > other);
1116 else if (opc0 == '<') {
1117 if (opc1 == '=') *out = (*value_out <= other);
1118 else *out = (*value_out < other);
1120 *value_out = (*out ? 1 : 0);
1125 ftepp_error(ftepp, "junk after #if");
1130 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1132 bool result = false;
1135 memset(cond, 0, sizeof(*cond));
1136 (void)ftepp_next(ftepp);
1138 if (!ftepp_skipspace(ftepp))
1140 if (ftepp->token == TOKEN_EOL) {
1141 ftepp_error(ftepp, "expected expression for #if-directive");
1145 if (!ftepp_if_expr(ftepp, &result, &dummy))
1153 * ifdef is rather simple
1155 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1158 memset(cond, 0, sizeof(*cond));
1159 (void)ftepp_next(ftepp);
1160 if (!ftepp_skipspace(ftepp))
1163 switch (ftepp->token) {
1165 case TOKEN_TYPENAME:
1167 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1170 ftepp_error(ftepp, "expected macro name");
1174 (void)ftepp_next(ftepp);
1175 if (!ftepp_skipspace(ftepp))
1177 /* relaxing this condition
1178 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1179 ftepp_error(ftepp, "stray tokens after #ifdef");
1188 * undef is also simple
1190 static bool ftepp_undef(ftepp_t *ftepp)
1192 (void)ftepp_next(ftepp);
1193 if (!ftepp_skipspace(ftepp))
1196 if (ftepp->output_on) {
1197 switch (ftepp->token) {
1199 case TOKEN_TYPENAME:
1201 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1204 ftepp_error(ftepp, "expected macro name");
1209 (void)ftepp_next(ftepp);
1210 if (!ftepp_skipspace(ftepp))
1212 /* relaxing this condition
1213 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1214 ftepp_error(ftepp, "stray tokens after #ifdef");
1221 /* Special unescape-string function which skips a leading quote
1222 * and stops at a quote, not just at \0
1224 static void unescape(const char *str, char *out) {
1226 while (*str && *str != '"') {
1230 case '\\': *out++ = *str; break;
1231 case '"': *out++ = *str; break;
1232 case 'a': *out++ = '\a'; break;
1233 case 'b': *out++ = '\b'; break;
1234 case 'r': *out++ = '\r'; break;
1235 case 'n': *out++ = '\n'; break;
1236 case 't': *out++ = '\t'; break;
1237 case 'f': *out++ = '\f'; break;
1238 case 'v': *out++ = '\v'; break;
1253 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1256 char *filename = NULL;
1257 const char *last_slash;
1263 last_slash = strrchr(pathfile, '/');
1266 len = last_slash - pathfile;
1267 memcpy(vec_add(filename, len), pathfile, len);
1268 vec_push(filename, '/');
1272 memcpy(vec_add(filename, len+1), file, len);
1273 vec_last(filename) = 0;
1275 fp = fs_file_open(filename, "rb");
1284 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1286 char *filename = NULL;
1288 filename = ftepp_include_find_path(file, ftepp->includename);
1290 filename = ftepp_include_find_path(file, ftepp->itemname);
1294 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1295 char *message = NULL;
1297 if (!ftepp_skipspace(ftepp))
1300 /* handle the odd non string constant case so it works like C */
1301 if (ftepp->token != TOKEN_STRINGCONST) {
1303 vec_upload(message, "#warning", 8);
1305 while (ftepp->token != TOKEN_EOL) {
1306 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1309 vec_push(message, '\0');
1310 if (ftepp->output_on)
1311 store = ftepp_warn(ftepp, WARN_CPP, message);
1318 if (!ftepp->output_on)
1321 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1322 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1325 static void ftepp_directive_error(ftepp_t *ftepp) {
1326 char *message = NULL;
1328 if (!ftepp_skipspace(ftepp))
1331 /* handle the odd non string constant case so it works like C */
1332 if (ftepp->token != TOKEN_STRINGCONST) {
1333 vec_upload(message, "#error", 6);
1335 while (ftepp->token != TOKEN_EOL) {
1336 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1339 vec_push(message, '\0');
1340 if (ftepp->output_on)
1341 ftepp_error(ftepp, message);
1346 if (!ftepp->output_on)
1349 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1350 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1353 static void ftepp_directive_message(ftepp_t *ftepp) {
1354 char *message = NULL;
1356 if (!ftepp_skipspace(ftepp))
1359 /* handle the odd non string constant case so it works like C */
1360 if (ftepp->token != TOKEN_STRINGCONST) {
1361 vec_upload(message, "#message", 8);
1363 while (ftepp->token != TOKEN_EOL) {
1364 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1367 vec_push(message, '\0');
1368 if (ftepp->output_on)
1369 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1374 if (!ftepp->output_on)
1377 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1378 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1383 * FIXME: do we need/want a -I option?
1384 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1386 static bool ftepp_include(ftepp_t *ftepp)
1388 lex_file *old_lexer = ftepp->lex;
1393 char *old_includename;
1395 (void)ftepp_next(ftepp);
1396 if (!ftepp_skipspace(ftepp))
1399 if (ftepp->token != TOKEN_STRINGCONST) {
1400 ftepp_error(ftepp, "expected filename to include");
1404 if (!ftepp->output_on) {
1409 ctx = ftepp_ctx(ftepp);
1411 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1413 ftepp_out(ftepp, "\n#pragma file(", false);
1414 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1415 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1417 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1419 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1422 inlex = lex_open(filename);
1424 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1429 old_includename = ftepp->includename;
1430 ftepp->includename = filename;
1431 if (!ftepp_preprocess(ftepp)) {
1432 vec_free(ftepp->includename);
1433 ftepp->includename = old_includename;
1434 lex_close(ftepp->lex);
1435 ftepp->lex = old_lexer;
1438 vec_free(ftepp->includename);
1439 ftepp->includename = old_includename;
1440 lex_close(ftepp->lex);
1441 ftepp->lex = old_lexer;
1443 ftepp_out(ftepp, "\n#pragma file(", false);
1444 ftepp_out(ftepp, ctx.file, false);
1445 util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1446 ftepp_out(ftepp, lineno, false);
1449 (void)ftepp_next(ftepp);
1450 if (!ftepp_skipspace(ftepp))
1452 if (ftepp->token != TOKEN_EOL) {
1453 ftepp_error(ftepp, "stray tokens after #include");
1456 (void)ftepp_next(ftepp);
1461 /* Basic structure handlers */
1462 static bool ftepp_else_allowed(ftepp_t *ftepp)
1464 if (!vec_size(ftepp->conditions)) {
1465 ftepp_error(ftepp, "#else without #if");
1468 if (vec_last(ftepp->conditions).had_else) {
1469 ftepp_error(ftepp, "multiple #else for a single #if");
1475 static bool ftepp_hash(ftepp_t *ftepp)
1480 lex_ctx ctx = ftepp_ctx(ftepp);
1482 if (!ftepp_skipspace(ftepp))
1485 switch (ftepp->token) {
1488 case TOKEN_TYPENAME:
1489 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1490 return ftepp_define(ftepp);
1492 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1493 return ftepp_undef(ftepp);
1495 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1496 if (!ftepp_ifdef(ftepp, &cond))
1498 cond.was_on = cond.on;
1499 vec_push(ftepp->conditions, cond);
1500 ftepp->output_on = ftepp->output_on && cond.on;
1503 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1504 if (!ftepp_ifdef(ftepp, &cond))
1507 cond.was_on = cond.on;
1508 vec_push(ftepp->conditions, cond);
1509 ftepp->output_on = ftepp->output_on && cond.on;
1512 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1513 if (!ftepp_else_allowed(ftepp))
1515 if (!ftepp_ifdef(ftepp, &cond))
1517 pc = &vec_last(ftepp->conditions);
1518 pc->on = !pc->was_on && cond.on;
1519 pc->was_on = pc->was_on || pc->on;
1520 ftepp_update_output_condition(ftepp);
1523 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1524 if (!ftepp_else_allowed(ftepp))
1526 if (!ftepp_ifdef(ftepp, &cond))
1529 pc = &vec_last(ftepp->conditions);
1530 pc->on = !pc->was_on && cond.on;
1531 pc->was_on = pc->was_on || pc->on;
1532 ftepp_update_output_condition(ftepp);
1535 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1536 if (!ftepp_else_allowed(ftepp))
1538 if (!ftepp_if(ftepp, &cond))
1540 pc = &vec_last(ftepp->conditions);
1541 pc->on = !pc->was_on && cond.on;
1542 pc->was_on = pc->was_on || pc->on;
1543 ftepp_update_output_condition(ftepp);
1546 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1547 if (!ftepp_if(ftepp, &cond))
1549 cond.was_on = cond.on;
1550 vec_push(ftepp->conditions, cond);
1551 ftepp->output_on = ftepp->output_on && cond.on;
1554 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1555 if (!ftepp_else_allowed(ftepp))
1557 pc = &vec_last(ftepp->conditions);
1558 pc->on = !pc->was_on;
1559 pc->had_else = true;
1561 ftepp_update_output_condition(ftepp);
1564 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1565 if (!vec_size(ftepp->conditions)) {
1566 ftepp_error(ftepp, "#endif without #if");
1569 vec_pop(ftepp->conditions);
1571 ftepp_update_output_condition(ftepp);
1574 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1575 return ftepp_include(ftepp);
1577 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1578 ftepp_out(ftepp, "#", false);
1581 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1582 ftepp_directive_warning(ftepp);
1585 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1586 ftepp_directive_error(ftepp);
1589 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1590 ftepp_directive_message(ftepp);
1594 if (ftepp->output_on) {
1595 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1602 /* break; never reached */
1604 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1607 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1610 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1613 /* Builtins! Don't forget the builtins! */
1614 case TOKEN_INTCONST:
1615 case TOKEN_FLOATCONST:
1616 ftepp_out(ftepp, "#", false);
1619 if (!ftepp_skipspace(ftepp))
1624 static bool ftepp_preprocess(ftepp_t *ftepp)
1627 bool newline = true;
1630 char *expand = NULL;
1633 ftepp->lex->flags.preprocessing = true;
1634 ftepp->lex->flags.mergelines = false;
1635 ftepp->lex->flags.noops = true;
1640 if (ftepp->token >= TOKEN_EOF)
1646 switch (ftepp->token) {
1649 case TOKEN_TYPENAME:
1650 /* is it a predef? */
1651 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1652 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1653 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1654 expand = ftepp_predefs[i].func(ftepp->lex);
1655 ftepp_out(ftepp, expand, false);
1656 ftepp_next(ftepp); /* skip */
1658 mem_d(expand); /* free memory */
1664 if (ftepp->output_on)
1665 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1670 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1674 if (!ftepp_macro_call(ftepp, macro))
1675 ftepp->token = TOKEN_ERROR;
1679 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1683 ftepp->lex->flags.mergelines = true;
1684 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1685 ftepp_error(ftepp, "error in preprocessor directive");
1686 ftepp->token = TOKEN_ERROR;
1689 if (!ftepp_hash(ftepp))
1690 ftepp->token = TOKEN_ERROR;
1691 ftepp->lex->flags.mergelines = false;
1695 ftepp_out(ftepp, "\n", true);
1699 /* same as default but don't set newline=false */
1700 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1705 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1709 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1711 /* force a 0 at the end but don't count it as added to the output */
1712 vec_push(ftepp->output_string, 0);
1713 vec_shrinkby(ftepp->output_string, 1);
1715 return (ftepp->token == TOKEN_EOF);
1718 /* Like in parser.c - files keep the previous state so we have one global
1719 * preprocessor. Except here we will want to warn about dangling #ifs.
1721 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1724 if (vec_size(ftepp->conditions)) {
1725 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1728 lex_close(ftepp->lex);
1730 if (ftepp->itemname) {
1731 mem_d(ftepp->itemname);
1732 ftepp->itemname = NULL;
1737 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1739 ftepp->lex = lex_open(filename);
1740 ftepp->itemname = util_strdup(filename);
1742 con_out("failed to open file \"%s\"\n", filename);
1745 if (!ftepp_preprocess(ftepp))
1747 return ftepp_preprocess_done(ftepp);
1750 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1752 ftepp->lex = lex_open_string(str, strlen(str), name);
1753 ftepp->itemname = util_strdup(name);
1755 con_out("failed to create lexer for string \"%s\"\n", name);
1758 if (!ftepp_preprocess(ftepp))
1760 return ftepp_preprocess_done(ftepp);
1764 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1765 char *create = NULL;
1767 /* use saner path for empty macros */
1769 ftepp_add_define(ftepp, "__builtin__", name);
1773 vec_upload(create, "#define ", 8);
1774 vec_upload(create, name, strlen(name));
1775 vec_push (create, ' ');
1776 vec_upload(create, value, strlen(value));
1777 vec_push (create, 0);
1779 ftepp_preprocess_string(ftepp, "__builtin__", create);
1783 ftepp_t *ftepp_create()
1789 ftepp = ftepp_new();
1793 memset(minor, 0, sizeof(minor));
1794 memset(major, 0, sizeof(major));
1796 /* set the right macro based on the selected standard */
1797 ftepp_add_define(ftepp, NULL, "GMQCC");
1798 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1799 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1808 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1809 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1810 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1811 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1812 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1813 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1814 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1815 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1816 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1817 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1828 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1829 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1834 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1837 lex_ctx ctx = { "__builtin__", 0 };
1839 macro = ppmacro_new(ctx, name);
1840 /*vec_push(ftepp->macros, macro);*/
1841 util_htset(ftepp->macros, name, macro);
1844 const char *ftepp_get(ftepp_t *ftepp)
1846 return ftepp->output_string;
1849 void ftepp_flush(ftepp_t *ftepp)
1851 ftepp_flush_do(ftepp);
1854 void ftepp_finish(ftepp_t *ftepp)
1858 ftepp_delete(ftepp);