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
31 #define HT_MACROS 1024
42 /* a copy from the lexer */
56 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
63 typedef struct ftepp_s {
69 ppcondition *conditions;
71 ht macros; /* hashtable<string, ppmacro*> */
80 * Implement the predef subsystem now. We can do this safely with the
81 * help of lexer contexts.
83 static uint32_t ftepp_predef_countval = 0;
84 static uint32_t ftepp_predef_randval = 0;
87 static char *ftepp_predef_date(lex_file *context) {
88 const struct tm *itime = NULL;
89 char *value = (char*)mem_a(82);
95 itime = util_localtime(&rtime);
96 strftime(value, 82, "\"%b %d %Y\"", itime);
102 static char *ftepp_predef_time(lex_file *context) {
103 const struct tm *itime = NULL;
104 char *value = (char*)mem_a(82);
110 itime = util_localtime(&rtime);
111 strftime(value, 82, "\"%X\"", itime);
117 static char *ftepp_predef_line(lex_file *context) {
119 util_asprintf(&value, "%d", (int)context->line);
123 static char *ftepp_predef_file(lex_file *context) {
124 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
125 char *value = (char*)mem_a(length);
126 util_snprintf(value, length, "\"%s\"", context->name);
130 /* __COUNTER_LAST__ */
131 static char *ftepp_predef_counterlast(lex_file *context) {
133 util_asprintf(&value, "%u", ftepp_predef_countval);
139 static char *ftepp_predef_counter(lex_file *context) {
141 ftepp_predef_countval ++;
142 util_asprintf(&value, "%u", ftepp_predef_countval);
148 static char *ftepp_predef_random(lex_file *context) {
150 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
151 util_asprintf(&value, "%u", ftepp_predef_randval);
156 /* __RANDOM_LAST__ */
157 static char *ftepp_predef_randomlast(lex_file *context) {
159 util_asprintf(&value, "%u", ftepp_predef_randval);
165 static char *ftepp_predef_timestamp(lex_file *context) {
171 if (stat(context->name, &finfo))
172 return util_strdup("\"<failed to determine timestamp>\"");
174 find = util_ctime(&finfo.st_mtime);
175 value = (char*)mem_a(strlen(find) + 1);
176 memcpy(&value[1], find, (size = strlen(find)) - 1);
186 char *(*func)(lex_file *);
189 static const ftepp_predef_t ftepp_predefs[] = {
190 { "__LINE__", &ftepp_predef_line },
191 { "__FILE__", &ftepp_predef_file },
192 { "__COUNTER__", &ftepp_predef_counter },
193 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
194 { "__RANDOM__", &ftepp_predef_random },
195 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
196 { "__DATE__", &ftepp_predef_date },
197 { "__TIME__", &ftepp_predef_time },
198 { "__TIME_STAMP__", &ftepp_predef_timestamp }
201 static GMQCC_INLINE size_t ftepp_predef_index(const char *name) {
202 /* no hashtable here, we simply check for one to exist the naive way */
204 for(i = 1; i < GMQCC_ARRAY_COUNT(ftepp_predefs) + 1; i++)
205 if (!strcmp(ftepp_predefs[i-1].name, name))
210 bool ftepp_predef_exists(const char *name);
211 bool ftepp_predef_exists(const char *name) {
212 return ftepp_predef_index(name) != 0;
215 /* singleton because we're allowed */
216 static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) {
217 size_t i = ftepp_predef_index(name);
218 return (i != 0) ? ftepp_predefs[i-1].func : NULL;
221 #define ftepp_tokval(f) ((f)->lex->tok.value)
222 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
224 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx_t ctx, const char *fmt, ...)
231 con_cvprintmsg(ctx, LVL_ERROR, "error", fmt, ap);
235 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
242 con_cvprintmsg(ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
246 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
252 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
257 static pptoken *pptoken_make(ftepp_t *ftepp)
259 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
260 token->token = ftepp->token;
262 if (token->token == TOKEN_WHITE)
263 token->value = util_strdup(" ");
266 token->value = util_strdup(ftepp_tokval(ftepp));
268 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
272 static GMQCC_INLINE void pptoken_delete(pptoken *self)
278 static ppmacro *ppmacro_new(lex_ctx_t ctx, const char *name)
280 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
283 memset(macro, 0, sizeof(*macro));
284 macro->name = util_strdup(name);
288 static void ppmacro_delete(ppmacro *self)
291 for (i = 0; i < vec_size(self->params); ++i)
292 mem_d(self->params[i]);
293 vec_free(self->params);
294 for (i = 0; i < vec_size(self->output); ++i)
295 pptoken_delete(self->output[i]);
296 vec_free(self->output);
301 static ftepp_t* ftepp_new(void)
305 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
306 memset(ftepp, 0, sizeof(*ftepp));
308 ftepp->macros = util_htnew(HT_MACROS);
309 ftepp->output_on = true;
314 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
316 vec_free(self->output_string);
319 static void ftepp_delete(ftepp_t *self)
321 ftepp_flush_do(self);
323 mem_d(self->itemname);
324 if (self->includename)
325 vec_free(self->includename);
327 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
329 vec_free(self->conditions);
331 lex_close(self->lex);
335 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
337 if (ignore_cond || ftepp->output_on)
342 data = vec_add(ftepp->output_string, len);
343 memcpy(data, str, len);
347 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
350 ftepp->output_on = true;
351 for (i = 0; i < vec_size(ftepp->conditions); ++i)
352 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
355 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
357 return (ppmacro*)util_htget(ftepp->macros, name);
360 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
362 util_htrm(ftepp->macros, name, (void (*)(void*))&ppmacro_delete);
365 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
367 return (ftepp->token = lex_do(ftepp->lex));
370 /* Important: this does not skip newlines! */
371 static bool ftepp_skipspace(ftepp_t *ftepp)
373 if (ftepp->token != TOKEN_WHITE)
375 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
376 if (ftepp->token >= TOKEN_EOF) {
377 ftepp_error(ftepp, "unexpected end of preprocessor directive");
383 /* this one skips EOLs as well */
384 static bool ftepp_skipallwhite(ftepp_t *ftepp)
386 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
390 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
391 if (ftepp->token >= TOKEN_EOF) {
392 ftepp_error(ftepp, "unexpected end of preprocessor directive");
399 * The huge macro parsing code...
401 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
405 if (!ftepp_skipspace(ftepp))
407 if (ftepp->token == ')')
409 switch (ftepp->token) {
413 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
416 macro->variadic = true;
419 ftepp_error(ftepp, "unexpected token in parameter list");
423 if (!ftepp_skipspace(ftepp))
425 if (macro->variadic && ftepp->token != ')') {
426 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
429 } while (ftepp->token == ',');
431 if (ftepp->token != ')') {
432 ftepp_error(ftepp, "expected closing paren after macro parameter list");
436 /* skipspace happens in ftepp_define */
440 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
443 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
444 bool subscript = false;
446 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
447 subscript = !!(ftepp_next(ftepp) == '#');
449 if (subscript && ftepp_next(ftepp) != '#') {
450 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
452 } else if (subscript) {
453 if (ftepp_next(ftepp) == '[') {
454 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
455 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
459 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
461 if (ftepp_next(ftepp) != ']') {
462 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
467 * mark it as an array to be handled later as such and not
468 * as traditional __VA_ARGS__
470 ftepp->token = TOKEN_VA_ARGS_ARRAY;
471 ptok = pptoken_make(ftepp);
472 ptok->constval.i = index;
473 vec_push(macro->output, ptok);
476 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
480 int old = ftepp->token;
481 ftepp->token = TOKEN_VA_ARGS;
482 ptok = pptoken_make(ftepp);
483 vec_push(macro->output, ptok);
487 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
488 ftepp->token = TOKEN_VA_COUNT;
489 ptok = pptoken_make(ftepp);
490 vec_push(macro->output, ptok);
493 ptok = pptoken_make(ftepp);
494 vec_push(macro->output, ptok);
498 /* recursive expansion can cause EOFs here */
499 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
500 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
506 static bool ftepp_define(ftepp_t *ftepp)
508 ppmacro *macro = NULL;
509 size_t l = ftepp_ctx(ftepp).line;
511 (void)ftepp_next(ftepp);
512 if (!ftepp_skipspace(ftepp))
515 switch (ftepp->token) {
519 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
520 if (macro && ftepp->output_on) {
521 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
523 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
525 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
528 ftepp_error(ftepp, "expected macro name");
532 (void)ftepp_next(ftepp);
534 if (ftepp->token == '(') {
535 macro->has_params = true;
536 if (!ftepp_define_params(ftepp, macro)) {
537 ppmacro_delete(macro);
542 if (!ftepp_skipspace(ftepp)) {
543 ppmacro_delete(macro);
547 if (!ftepp_define_body(ftepp, macro)) {
548 ppmacro_delete(macro);
552 if (ftepp->output_on)
553 util_htset(ftepp->macros, macro->name, (void*)macro);
555 ppmacro_delete(macro);
558 for (; l < ftepp_ctx(ftepp).line; ++l)
559 ftepp_out(ftepp, "\n", true);
564 * When a macro is used we have to handle parameters as well
565 * as special-concatenation via ## or stringification via #
567 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
568 * this kind of parens. Curly braces or [] don't count towards the
575 static void macroparam_clean(macroparam *self)
578 for (i = 0; i < vec_size(self->tokens); ++i)
579 pptoken_delete(self->tokens[i]);
580 vec_free(self->tokens);
583 /* need to leave the last token up */
584 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
586 macroparam *params = NULL;
592 if (!ftepp_skipallwhite(ftepp))
594 while (ftepp->token != ')') {
596 if (!ftepp_skipallwhite(ftepp))
598 while (parens || ftepp->token != ',') {
599 if (ftepp->token == '(')
601 else if (ftepp->token == ')') {
606 ptok = pptoken_make(ftepp);
607 vec_push(mp.tokens, ptok);
608 if (ftepp_next(ftepp) >= TOKEN_EOF) {
609 ftepp_error(ftepp, "unexpected end of file in macro call");
613 vec_push(params, mp);
615 if (ftepp->token == ')')
617 if (ftepp->token != ',') {
618 ftepp_error(ftepp, "expected closing paren or comma in macro call");
621 if (ftepp_next(ftepp) >= TOKEN_EOF) {
622 ftepp_error(ftepp, "unexpected end of file in macro call");
626 *out_params = params;
631 macroparam_clean(&mp);
632 for (i = 0; i < vec_size(params); ++i)
633 macroparam_clean(¶ms[i]);
638 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
641 for (i = 0; i < vec_size(macro->params); ++i) {
642 if (!strcmp(macro->params[i], name)) {
650 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
655 switch (token->token) {
656 case TOKEN_STRINGCONST:
659 /* in preprocessor mode strings already are string,
660 * so we don't get actual newline bytes here.
661 * Still need to escape backslashes and quotes.
664 case '\\': ftepp_out(ftepp, "\\\\", false); break;
665 case '"': ftepp_out(ftepp, "\\\"", false); break;
668 ftepp_out(ftepp, chs, false);
675 ftepp_out(ftepp, " ", false);
678 ftepp_out(ftepp, "\\n", false);
681 ftepp_out(ftepp, token->value, false);
686 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
689 ftepp_out(ftepp, "\"", false);
690 for (i = 0; i < vec_size(param->tokens); ++i)
691 ftepp_stringify_token(ftepp, param->tokens[i]);
692 ftepp_out(ftepp, "\"", false);
695 static void ftepp_recursion_header(ftepp_t *ftepp)
697 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
700 static void ftepp_recursion_footer(ftepp_t *ftepp)
702 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
705 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
709 for (i = 0; i < vec_size(param->tokens); ++i) {
710 out = param->tokens[i];
711 if (out->token == TOKEN_EOL)
712 ftepp_out(ftepp, "\n", false);
714 ftepp_out(ftepp, out->value, false);
718 static bool ftepp_preprocess(ftepp_t *ftepp);
719 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
722 char *old_string = ftepp->output_string;
724 lex_file *old_lexer = ftepp->lex;
725 size_t vararg_start = vec_size(macro->params);
737 if (vararg_start < vec_size(params))
738 varargs = vec_size(params) - vararg_start;
743 if (!vec_size(macro->output))
746 ftepp->output_string = NULL;
747 for (o = 0; o < vec_size(macro->output); ++o) {
748 pptoken *out = macro->output[o];
749 switch (out->token) {
751 if (!macro->variadic) {
752 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
753 vec_free(old_string);
760 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
761 for (++pi; pi < varargs; ++pi) {
762 ftepp_out(ftepp, ", ", false);
763 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
767 case TOKEN_VA_ARGS_ARRAY:
768 if ((size_t)out->constval.i >= varargs) {
769 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
770 vec_free(old_string);
774 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
778 util_asprintf(&buffer, "%d", varargs);
779 ftepp_out(ftepp, buffer, false);
786 if (!macro_params_find(macro, out->value, &pi)) {
787 ftepp_out(ftepp, out->value, false);
790 ftepp_param_out(ftepp, ¶ms[pi]);
793 if (o + 1 < vec_size(macro->output)) {
794 nextok = macro->output[o+1]->token;
796 /* raw concatenation */
800 if ( (nextok == TOKEN_IDENT ||
801 nextok == TOKEN_KEYWORD ||
802 nextok == TOKEN_TYPENAME) &&
803 macro_params_find(macro, macro->output[o+1]->value, &pi))
806 ftepp_stringify(ftepp, ¶ms[pi]);
810 ftepp_out(ftepp, "#", false);
813 ftepp_out(ftepp, "\n", false);
816 ftepp_out(ftepp, out->value, false);
820 vec_push(ftepp->output_string, 0);
821 /* Now run the preprocessor recursively on this string buffer */
823 printf("__________\n%s\n=========\n", ftepp->output_string);
825 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
827 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
832 inlex->line = ftepp->lex->line;
833 inlex->sline = ftepp->lex->sline;
836 old_inmacro = ftepp->in_macro;
837 ftepp->in_macro = true;
838 ftepp->output_string = NULL;
839 if (!ftepp_preprocess(ftepp)) {
840 ftepp->in_macro = old_inmacro;
841 vec_free(ftepp->lex->open_string);
842 vec_free(ftepp->output_string);
843 lex_close(ftepp->lex);
847 ftepp->in_macro = old_inmacro;
848 vec_free(ftepp->lex->open_string);
849 lex_close(ftepp->lex);
851 inner_string = ftepp->output_string;
852 ftepp->output_string = old_string;
854 has_newlines = (strchr(inner_string, '\n') != NULL);
856 if (has_newlines && !old_inmacro)
857 ftepp_recursion_header(ftepp);
859 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
860 vec_free(inner_string);
862 if (has_newlines && !old_inmacro)
863 ftepp_recursion_footer(ftepp);
865 if (resetline && !ftepp->in_macro) {
867 util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
868 ftepp_out(ftepp, lineno, false);
871 old_string = ftepp->output_string;
873 ftepp->lex = old_lexer;
874 ftepp->output_string = old_string;
878 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
881 macroparam *params = NULL;
885 if (!macro->has_params) {
886 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
893 if (!ftepp_skipallwhite(ftepp))
896 if (ftepp->token != '(') {
897 ftepp_error(ftepp, "expected macro parameters in parenthesis");
902 paramline = ftepp->lex->sline;
903 if (!ftepp_macro_call_params(ftepp, ¶ms))
906 if ( vec_size(params) < vec_size(macro->params) ||
907 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
909 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
910 (macro->variadic ? " at least" : ""),
911 (unsigned int)vec_size(macro->params),
912 (unsigned int)vec_size(params));
917 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
922 for (o = 0; o < vec_size(params); ++o)
923 macroparam_clean(¶ms[o]);
929 * #if - the FTEQCC way:
930 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
931 * <numbers> => True if the number is not 0
932 * !<factor> => True if the factor yields false
933 * !!<factor> => ERROR on 2 or more unary nots
934 * <macro> => becomes the macro's FIRST token regardless of parameters
935 * <e> && <e> => True if both expressions are true
936 * <e> || <e> => True if either expression is true
938 * <ident> => False (remember for macros the <macro> rule applies instead)
939 * Unary + and - are weird and wrong in fteqcc so we don't allow them
940 * parenthesis in expressions are allowed
941 * parameter lists on macros are errors
942 * No mathematical calculations are executed
944 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
945 static bool ftepp_if_op(ftepp_t *ftepp)
947 ftepp->lex->flags.noops = false;
949 if (!ftepp_skipspace(ftepp))
951 ftepp->lex->flags.noops = true;
954 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
960 if (!ftepp_skipspace(ftepp))
963 while (ftepp->token == '!') {
966 if (!ftepp_skipspace(ftepp))
970 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
974 if (!ftepp_skipspace(ftepp))
978 switch (ftepp->token) {
982 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
984 if (!ftepp_skipspace(ftepp))
986 if (ftepp->token != '(') {
987 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
991 if (!ftepp_skipspace(ftepp))
993 if (ftepp->token != TOKEN_IDENT &&
994 ftepp->token != TOKEN_TYPENAME &&
995 ftepp->token != TOKEN_KEYWORD)
997 ftepp_error(ftepp, "defined() used on an unexpected token type");
1000 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1003 if (!ftepp_skipspace(ftepp))
1005 if (ftepp->token != ')') {
1006 ftepp_error(ftepp, "expected closing paren");
1012 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1013 if (!macro || !vec_size(macro->output)) {
1017 /* This does not expand recursively! */
1018 switch (macro->output[0]->token) {
1019 case TOKEN_INTCONST:
1020 *value_out = macro->output[0]->constval.i;
1021 *out = !!(macro->output[0]->constval.i);
1023 case TOKEN_FLOATCONST:
1024 *value_out = macro->output[0]->constval.f;
1025 *out = !!(macro->output[0]->constval.f);
1033 case TOKEN_STRINGCONST:
1037 case TOKEN_INTCONST:
1038 *value_out = ftepp->lex->tok.constval.i;
1039 *out = !!(ftepp->lex->tok.constval.i);
1041 case TOKEN_FLOATCONST:
1042 *value_out = ftepp->lex->tok.constval.f;
1043 *out = !!(ftepp->lex->tok.constval.f);
1048 if (!ftepp_if_expr(ftepp, out, value_out))
1050 if (ftepp->token != ')') {
1051 ftepp_error(ftepp, "expected closing paren in #if expression");
1057 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1058 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1059 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1063 *value_out = -*value_out;
1066 *value_out = (*out ? 1 : 0);
1072 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1074 if (!ftepp_next(ftepp))
1076 return ftepp_if_value(ftepp, out, value_out);
1080 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1082 if (!ftepp_if_value(ftepp, out, value_out))
1085 if (!ftepp_if_op(ftepp))
1088 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1091 /* FTEQCC is all right-associative and no precedence here */
1092 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1093 !strcmp(ftepp_tokval(ftepp), "||"))
1096 char opc = ftepp_tokval(ftepp)[0];
1100 if (!ftepp_next(ftepp))
1102 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1106 *out = *out && next;
1108 *out = *out || next;
1110 *value_out = (*out ? 1 : 0);
1113 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1114 !strcmp(ftepp_tokval(ftepp), "!=") ||
1115 !strcmp(ftepp_tokval(ftepp), ">=") ||
1116 !strcmp(ftepp_tokval(ftepp), "<=") ||
1117 !strcmp(ftepp_tokval(ftepp), ">") ||
1118 !strcmp(ftepp_tokval(ftepp), "<"))
1121 const char opc0 = ftepp_tokval(ftepp)[0];
1122 const char opc1 = ftepp_tokval(ftepp)[1];
1125 if (!ftepp_next(ftepp))
1127 if (!ftepp_if_expr(ftepp, &next, &other))
1131 *out = (*value_out == other);
1132 else if (opc0 == '!')
1133 *out = (*value_out != other);
1134 else if (opc0 == '>') {
1135 if (opc1 == '=') *out = (*value_out >= other);
1136 else *out = (*value_out > other);
1138 else if (opc0 == '<') {
1139 if (opc1 == '=') *out = (*value_out <= other);
1140 else *out = (*value_out < other);
1142 *value_out = (*out ? 1 : 0);
1147 ftepp_error(ftepp, "junk after #if");
1152 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1154 bool result = false;
1157 memset(cond, 0, sizeof(*cond));
1158 (void)ftepp_next(ftepp);
1160 if (!ftepp_skipspace(ftepp))
1162 if (ftepp->token == TOKEN_EOL) {
1163 ftepp_error(ftepp, "expected expression for #if-directive");
1167 if (!ftepp_if_expr(ftepp, &result, &dummy))
1175 * ifdef is rather simple
1177 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1180 memset(cond, 0, sizeof(*cond));
1181 (void)ftepp_next(ftepp);
1182 if (!ftepp_skipspace(ftepp))
1185 switch (ftepp->token) {
1187 case TOKEN_TYPENAME:
1189 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1192 ftepp_error(ftepp, "expected macro name");
1196 (void)ftepp_next(ftepp);
1197 if (!ftepp_skipspace(ftepp))
1199 /* relaxing this condition
1200 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1201 ftepp_error(ftepp, "stray tokens after #ifdef");
1210 * undef is also simple
1212 static bool ftepp_undef(ftepp_t *ftepp)
1214 (void)ftepp_next(ftepp);
1215 if (!ftepp_skipspace(ftepp))
1218 if (ftepp->output_on) {
1219 switch (ftepp->token) {
1221 case TOKEN_TYPENAME:
1223 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1226 ftepp_error(ftepp, "expected macro name");
1231 (void)ftepp_next(ftepp);
1232 if (!ftepp_skipspace(ftepp))
1234 /* relaxing this condition
1235 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1236 ftepp_error(ftepp, "stray tokens after #ifdef");
1243 /* Special unescape-string function which skips a leading quote
1244 * and stops at a quote, not just at \0
1246 static void unescape(const char *str, char *out) {
1248 while (*str && *str != '"') {
1252 case '\\': *out++ = *str; break;
1253 case '"': *out++ = *str; break;
1254 case 'a': *out++ = '\a'; break;
1255 case 'b': *out++ = '\b'; break;
1256 case 'r': *out++ = '\r'; break;
1257 case 'n': *out++ = '\n'; break;
1258 case 't': *out++ = '\t'; break;
1259 case 'f': *out++ = '\f'; break;
1260 case 'v': *out++ = '\v'; break;
1275 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1278 char *filename = NULL;
1279 const char *last_slash;
1285 last_slash = strrchr(pathfile, '/');
1288 len = last_slash - pathfile;
1289 memcpy(vec_add(filename, len), pathfile, len);
1290 vec_push(filename, '/');
1294 memcpy(vec_add(filename, len+1), file, len);
1295 vec_last(filename) = 0;
1297 fp = fs_file_open(filename, "rb");
1306 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1308 char *filename = NULL;
1310 filename = ftepp_include_find_path(file, ftepp->includename);
1312 filename = ftepp_include_find_path(file, ftepp->itemname);
1316 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1317 char *message = NULL;
1319 if (!ftepp_skipspace(ftepp))
1322 /* handle the odd non string constant case so it works like C */
1323 if (ftepp->token != TOKEN_STRINGCONST) {
1325 vec_append(message, 8, "#warning");
1327 while (ftepp->token != TOKEN_EOL) {
1328 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1331 vec_push(message, '\0');
1332 if (ftepp->output_on)
1333 store = ftepp_warn(ftepp, WARN_CPP, message);
1340 if (!ftepp->output_on)
1343 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1344 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1347 static void ftepp_directive_error(ftepp_t *ftepp) {
1348 char *message = NULL;
1350 if (!ftepp_skipspace(ftepp))
1353 /* handle the odd non string constant case so it works like C */
1354 if (ftepp->token != TOKEN_STRINGCONST) {
1355 vec_append(message, 6, "#error");
1357 while (ftepp->token != TOKEN_EOL) {
1358 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1361 vec_push(message, '\0');
1362 if (ftepp->output_on)
1363 ftepp_error(ftepp, message);
1368 if (!ftepp->output_on)
1371 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1372 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1375 static void ftepp_directive_message(ftepp_t *ftepp) {
1376 char *message = NULL;
1378 if (!ftepp_skipspace(ftepp))
1381 /* handle the odd non string constant case so it works like C */
1382 if (ftepp->token != TOKEN_STRINGCONST) {
1383 vec_append(message, 8, "#message");
1385 while (ftepp->token != TOKEN_EOL) {
1386 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1389 vec_push(message, '\0');
1390 if (ftepp->output_on)
1391 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1396 if (!ftepp->output_on)
1399 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1400 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1405 * FIXME: do we need/want a -I option?
1406 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1408 static bool ftepp_include(ftepp_t *ftepp)
1410 lex_file *old_lexer = ftepp->lex;
1415 char *old_includename;
1417 (void)ftepp_next(ftepp);
1418 if (!ftepp_skipspace(ftepp))
1421 if (ftepp->token != TOKEN_STRINGCONST) {
1422 ftepp_error(ftepp, "expected filename to include");
1426 if (!ftepp->output_on) {
1431 ctx = ftepp_ctx(ftepp);
1433 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1435 ftepp_out(ftepp, "\n#pragma file(", false);
1436 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1437 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1439 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1441 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1444 inlex = lex_open(filename);
1446 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1451 old_includename = ftepp->includename;
1452 ftepp->includename = filename;
1453 if (!ftepp_preprocess(ftepp)) {
1454 vec_free(ftepp->includename);
1455 ftepp->includename = old_includename;
1456 lex_close(ftepp->lex);
1457 ftepp->lex = old_lexer;
1460 vec_free(ftepp->includename);
1461 ftepp->includename = old_includename;
1462 lex_close(ftepp->lex);
1463 ftepp->lex = old_lexer;
1465 ftepp_out(ftepp, "\n#pragma file(", false);
1466 ftepp_out(ftepp, ctx.file, false);
1467 util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1468 ftepp_out(ftepp, lineno, false);
1471 (void)ftepp_next(ftepp);
1472 if (!ftepp_skipspace(ftepp))
1474 if (ftepp->token != TOKEN_EOL) {
1475 ftepp_error(ftepp, "stray tokens after #include");
1478 (void)ftepp_next(ftepp);
1483 /* Basic structure handlers */
1484 static bool ftepp_else_allowed(ftepp_t *ftepp)
1486 if (!vec_size(ftepp->conditions)) {
1487 ftepp_error(ftepp, "#else without #if");
1490 if (vec_last(ftepp->conditions).had_else) {
1491 ftepp_error(ftepp, "multiple #else for a single #if");
1497 static GMQCC_INLINE void ftepp_inmacro(ftepp_t *ftepp, const char *hash) {
1498 if (ftepp->in_macro)
1499 (void)!ftepp_warn(ftepp, WARN_DIRECTIVE_INMACRO, "`#%s` directive in macro", hash);
1502 static bool ftepp_hash(ftepp_t *ftepp)
1507 lex_ctx_t ctx = ftepp_ctx(ftepp);
1509 if (!ftepp_skipspace(ftepp))
1512 switch (ftepp->token) {
1515 case TOKEN_TYPENAME:
1516 case TOKEN_ATTRIBUTE_CLOSE:
1517 case TOKEN_ATTRIBUTE_OPEN:
1518 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1519 ftepp_inmacro(ftepp, "define");
1520 return ftepp_define(ftepp);
1522 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1523 ftepp_inmacro(ftepp, "undef");
1524 return ftepp_undef(ftepp);
1526 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1527 ftepp_inmacro(ftepp, "ifdef");
1528 if (!ftepp_ifdef(ftepp, &cond))
1530 cond.was_on = cond.on;
1531 vec_push(ftepp->conditions, cond);
1532 ftepp->output_on = ftepp->output_on && cond.on;
1535 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1536 ftepp_inmacro(ftepp, "ifndef");
1537 if (!ftepp_ifdef(ftepp, &cond))
1540 cond.was_on = cond.on;
1541 vec_push(ftepp->conditions, cond);
1542 ftepp->output_on = ftepp->output_on && cond.on;
1545 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1546 ftepp_inmacro(ftepp, "elifdef");
1547 if (!ftepp_else_allowed(ftepp))
1549 if (!ftepp_ifdef(ftepp, &cond))
1551 pc = &vec_last(ftepp->conditions);
1552 pc->on = !pc->was_on && cond.on;
1553 pc->was_on = pc->was_on || pc->on;
1554 ftepp_update_output_condition(ftepp);
1557 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1558 ftepp_inmacro(ftepp, "elifndef");
1559 if (!ftepp_else_allowed(ftepp))
1561 if (!ftepp_ifdef(ftepp, &cond))
1564 pc = &vec_last(ftepp->conditions);
1565 pc->on = !pc->was_on && cond.on;
1566 pc->was_on = pc->was_on || pc->on;
1567 ftepp_update_output_condition(ftepp);
1570 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1571 ftepp_inmacro(ftepp, "elif");
1572 if (!ftepp_else_allowed(ftepp))
1574 if (!ftepp_if(ftepp, &cond))
1576 pc = &vec_last(ftepp->conditions);
1577 pc->on = !pc->was_on && cond.on;
1578 pc->was_on = pc->was_on || pc->on;
1579 ftepp_update_output_condition(ftepp);
1582 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1583 ftepp_inmacro(ftepp, "if");
1584 if (!ftepp_if(ftepp, &cond))
1586 cond.was_on = cond.on;
1587 vec_push(ftepp->conditions, cond);
1588 ftepp->output_on = ftepp->output_on && cond.on;
1591 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1592 ftepp_inmacro(ftepp, "else");
1593 if (!ftepp_else_allowed(ftepp))
1595 pc = &vec_last(ftepp->conditions);
1596 pc->on = !pc->was_on;
1597 pc->had_else = true;
1599 ftepp_update_output_condition(ftepp);
1602 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1603 ftepp_inmacro(ftepp, "endif");
1604 if (!vec_size(ftepp->conditions)) {
1605 ftepp_error(ftepp, "#endif without #if");
1608 vec_pop(ftepp->conditions);
1610 ftepp_update_output_condition(ftepp);
1613 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1614 ftepp_inmacro(ftepp, "include");
1615 return ftepp_include(ftepp);
1617 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1618 ftepp_out(ftepp, "#", false);
1621 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1622 ftepp_directive_warning(ftepp);
1625 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1626 ftepp_directive_error(ftepp);
1629 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1630 ftepp_directive_message(ftepp);
1634 if (ftepp->output_on) {
1635 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1642 /* break; never reached */
1644 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1647 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1650 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1653 /* Builtins! Don't forget the builtins! */
1654 case TOKEN_INTCONST:
1655 case TOKEN_FLOATCONST:
1656 ftepp_out(ftepp, "#", false);
1659 if (!ftepp_skipspace(ftepp))
1664 static bool ftepp_preprocess(ftepp_t *ftepp)
1667 bool newline = true;
1670 char *expand = NULL;
1672 ftepp->lex->flags.preprocessing = true;
1673 ftepp->lex->flags.mergelines = false;
1674 ftepp->lex->flags.noops = true;
1679 if (ftepp->token >= TOKEN_EOF)
1685 switch (ftepp->token) {
1688 case TOKEN_TYPENAME:
1689 /* is it a predef? */
1690 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1691 char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp));
1693 expand = predef(ftepp->lex);
1694 ftepp_out (ftepp, expand, false);
1702 if (ftepp->output_on)
1703 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1708 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1712 if (!ftepp_macro_call(ftepp, macro))
1713 ftepp->token = TOKEN_ERROR;
1717 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1721 ftepp->lex->flags.mergelines = true;
1722 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1723 ftepp_error(ftepp, "error in preprocessor directive");
1724 ftepp->token = TOKEN_ERROR;
1727 if (!ftepp_hash(ftepp))
1728 ftepp->token = TOKEN_ERROR;
1729 ftepp->lex->flags.mergelines = false;
1733 ftepp_out(ftepp, "\n", true);
1737 /* same as default but don't set newline=false */
1738 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1743 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1747 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1749 /* force a 0 at the end but don't count it as added to the output */
1750 vec_push(ftepp->output_string, 0);
1751 vec_shrinkby(ftepp->output_string, 1);
1753 return (ftepp->token == TOKEN_EOF);
1756 /* Like in parser.c - files keep the previous state so we have one global
1757 * preprocessor. Except here we will want to warn about dangling #ifs.
1759 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1762 if (vec_size(ftepp->conditions)) {
1763 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1766 lex_close(ftepp->lex);
1768 if (ftepp->itemname) {
1769 mem_d(ftepp->itemname);
1770 ftepp->itemname = NULL;
1775 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1777 ftepp->lex = lex_open(filename);
1778 ftepp->itemname = util_strdup(filename);
1780 con_out("failed to open file \"%s\"\n", filename);
1783 if (!ftepp_preprocess(ftepp))
1785 return ftepp_preprocess_done(ftepp);
1788 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1790 ftepp->lex = lex_open_string(str, strlen(str), name);
1791 ftepp->itemname = util_strdup(name);
1793 con_out("failed to create lexer for string \"%s\"\n", name);
1796 if (!ftepp_preprocess(ftepp))
1798 return ftepp_preprocess_done(ftepp);
1802 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1803 char *create = NULL;
1805 /* use saner path for empty macros */
1807 ftepp_add_define(ftepp, "__builtin__", name);
1811 vec_append(create, 8, "#define ");
1812 vec_append(create, strlen(name), name);
1813 vec_push (create, ' ');
1814 vec_append(create, strlen(value), value);
1815 vec_push (create, 0);
1817 ftepp_preprocess_string(ftepp, "__builtin__", create);
1821 ftepp_t *ftepp_create()
1827 ftepp = ftepp_new();
1831 memset(minor, 0, sizeof(minor));
1832 memset(major, 0, sizeof(major));
1834 /* set the right macro based on the selected standard */
1835 ftepp_add_define(ftepp, NULL, "GMQCC");
1836 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1837 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1846 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1847 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1848 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1849 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1850 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1851 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1852 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1853 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1854 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1855 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1866 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1867 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1870 * We're going to just make __NULL__ nil, which works for 60% of the
1871 * cases of __NULL_ for fteqcc.
1873 ftepp_add_macro(ftepp, "__NULL__", "nil");
1878 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1881 lex_ctx_t ctx = { "__builtin__", 0, 0 };
1883 macro = ppmacro_new(ctx, name);
1884 /*vec_push(ftepp->macros, macro);*/
1885 util_htset(ftepp->macros, name, macro);
1888 const char *ftepp_get(ftepp_t *ftepp)
1890 return ftepp->output_string;
1893 void ftepp_flush(ftepp_t *ftepp)
1895 ftepp_flush_do(ftepp);
1898 void ftepp_finish(ftepp_t *ftepp)
1902 ftepp_delete(ftepp);