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
32 #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 struct tm *itime = NULL;
90 char *value = (char*)mem_a(82);
91 /* 82 is enough for strftime but we also have " " in our string */
99 localtime_s(itime, &rtime);
101 itime = localtime(&rtime);
104 strftime(value, 82, "\"%b %d %Y\"", itime);
110 static char *ftepp_predef_time(lex_file *context) {
111 struct tm *itime = NULL;
113 char *value = (char*)mem_a(82);
114 /* 82 is enough for strftime but we also have " " in our string */
122 localtime_s(itime, &rtime);
124 itime = localtime(&rtime);
127 strftime(value, 82, "\"%X\"", itime);
133 static char *ftepp_predef_line(lex_file *context) {
135 util_asprintf(&value, "%d", (int)context->line);
139 static char *ftepp_predef_file(lex_file *context) {
140 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
141 char *value = (char*)mem_a(length);
142 util_snprintf(value, length, "\"%s\"", context->name);
146 /* __COUNTER_LAST__ */
147 static char *ftepp_predef_counterlast(lex_file *context) {
149 util_asprintf(&value, "%u", ftepp_predef_countval);
155 static char *ftepp_predef_counter(lex_file *context) {
157 ftepp_predef_countval ++;
158 util_asprintf(&value, "%u", ftepp_predef_countval);
164 static char *ftepp_predef_random(lex_file *context) {
166 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
167 util_asprintf(&value, "%u", ftepp_predef_randval);
172 /* __RANDOM_LAST__ */
173 static char *ftepp_predef_randomlast(lex_file *context) {
175 util_asprintf(&value, "%u", ftepp_predef_randval);
181 static char *ftepp_predef_timestamp(lex_file *context) {
186 if (stat(context->name, &finfo))
187 return util_strdup("\"<failed to determine timestamp>\"");
190 * ctime and its fucking annoying newline char, no worries, we're
191 * professionals here.
193 find = ctime(&finfo.st_mtime);
194 value = (char*)mem_a(strlen(find) + 1);
195 memcpy(&value[1], find, (size = strlen(find)) - 1);
205 char *(*func)(lex_file *);
208 static const ftepp_predef_t ftepp_predefs[] = {
209 { "__LINE__", &ftepp_predef_line },
210 { "__FILE__", &ftepp_predef_file },
211 { "__COUNTER__", &ftepp_predef_counter },
212 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
213 { "__RANDOM__", &ftepp_predef_random },
214 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
215 { "__DATE__", &ftepp_predef_date },
216 { "__TIME__", &ftepp_predef_time },
217 { "__TIME_STAMP__", &ftepp_predef_timestamp }
220 static GMQCC_INLINE int ftepp_predef_index(const char *name) {
221 /* no hashtable here, we simply check for one to exist the naive way */
223 for(i = 0; i < (int)(sizeof(ftepp_predefs)/sizeof(*ftepp_predefs)); i++)
224 if (!strcmp(ftepp_predefs[i].name, name))
229 bool ftepp_predef_exists(const char *name) {
230 return ftepp_predef_index(name) != -1;
233 /* singleton because we're allowed */
234 static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) {
235 int i = ftepp_predef_index(name);
236 return (i != -1) ? ftepp_predefs[i].func : NULL;
239 #define ftepp_tokval(f) ((f)->lex->tok.value)
240 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
242 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
249 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
253 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
260 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
264 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
270 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
275 static pptoken *pptoken_make(ftepp_t *ftepp)
277 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
278 token->token = ftepp->token;
280 if (token->token == TOKEN_WHITE)
281 token->value = util_strdup(" ");
284 token->value = util_strdup(ftepp_tokval(ftepp));
286 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
290 static GMQCC_INLINE void pptoken_delete(pptoken *self)
296 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
298 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
301 memset(macro, 0, sizeof(*macro));
302 macro->name = util_strdup(name);
306 static void ppmacro_delete(ppmacro *self)
309 for (i = 0; i < vec_size(self->params); ++i)
310 mem_d(self->params[i]);
311 vec_free(self->params);
312 for (i = 0; i < vec_size(self->output); ++i)
313 pptoken_delete(self->output[i]);
314 vec_free(self->output);
319 static ftepp_t* ftepp_new(void)
323 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
324 memset(ftepp, 0, sizeof(*ftepp));
326 ftepp->macros = util_htnew(HT_MACROS);
327 ftepp->output_on = true;
332 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
334 vec_free(self->output_string);
337 static void ftepp_delete(ftepp_t *self)
339 ftepp_flush_do(self);
341 mem_d(self->itemname);
342 if (self->includename)
343 vec_free(self->includename);
345 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
347 vec_free(self->conditions);
349 lex_close(self->lex);
353 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
355 if (ignore_cond || ftepp->output_on)
360 data = vec_add(ftepp->output_string, len);
361 memcpy(data, str, len);
365 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
368 ftepp->output_on = true;
369 for (i = 0; i < vec_size(ftepp->conditions); ++i)
370 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
373 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
375 return (ppmacro*)util_htget(ftepp->macros, name);
378 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
380 util_htrm(ftepp->macros, name, (void (*)(void*))&ppmacro_delete);
383 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
385 return (ftepp->token = lex_do(ftepp->lex));
388 /* Important: this does not skip newlines! */
389 static bool ftepp_skipspace(ftepp_t *ftepp)
391 if (ftepp->token != TOKEN_WHITE)
393 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
394 if (ftepp->token >= TOKEN_EOF) {
395 ftepp_error(ftepp, "unexpected end of preprocessor directive");
401 /* this one skips EOLs as well */
402 static bool ftepp_skipallwhite(ftepp_t *ftepp)
404 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
408 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
409 if (ftepp->token >= TOKEN_EOF) {
410 ftepp_error(ftepp, "unexpected end of preprocessor directive");
417 * The huge macro parsing code...
419 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
423 if (!ftepp_skipspace(ftepp))
425 if (ftepp->token == ')')
427 switch (ftepp->token) {
431 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
434 macro->variadic = true;
437 ftepp_error(ftepp, "unexpected token in parameter list");
441 if (!ftepp_skipspace(ftepp))
443 if (macro->variadic && ftepp->token != ')') {
444 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
447 } while (ftepp->token == ',');
449 if (ftepp->token != ')') {
450 ftepp_error(ftepp, "expected closing paren after macro parameter list");
454 /* skipspace happens in ftepp_define */
458 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
461 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
462 bool subscript = false;
464 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
465 subscript = !!(ftepp_next(ftepp) == '#');
467 if (subscript && ftepp_next(ftepp) != '#') {
468 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
470 } else if (subscript) {
471 if (ftepp_next(ftepp) == '[') {
472 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
473 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
477 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
479 if (ftepp_next(ftepp) != ']') {
480 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
485 * mark it as an array to be handled later as such and not
486 * as traditional __VA_ARGS__
488 ftepp->token = TOKEN_VA_ARGS_ARRAY;
489 ptok = pptoken_make(ftepp);
490 ptok->constval.i = index;
491 vec_push(macro->output, ptok);
494 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
498 int old = ftepp->token;
499 ftepp->token = TOKEN_VA_ARGS;
500 ptok = pptoken_make(ftepp);
501 vec_push(macro->output, ptok);
505 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
506 ftepp->token = TOKEN_VA_COUNT;
507 ptok = pptoken_make(ftepp);
508 vec_push(macro->output, ptok);
511 ptok = pptoken_make(ftepp);
512 vec_push(macro->output, ptok);
516 /* recursive expansion can cause EOFs here */
517 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
518 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
524 static bool ftepp_define(ftepp_t *ftepp)
526 ppmacro *macro = NULL;
527 size_t l = ftepp_ctx(ftepp).line;
529 (void)ftepp_next(ftepp);
530 if (!ftepp_skipspace(ftepp))
533 switch (ftepp->token) {
537 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
538 if (macro && ftepp->output_on) {
539 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
541 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
543 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
546 ftepp_error(ftepp, "expected macro name");
550 (void)ftepp_next(ftepp);
552 if (ftepp->token == '(') {
553 macro->has_params = true;
554 if (!ftepp_define_params(ftepp, macro)) {
555 ppmacro_delete(macro);
560 if (!ftepp_skipspace(ftepp)) {
561 ppmacro_delete(macro);
565 if (!ftepp_define_body(ftepp, macro)) {
566 ppmacro_delete(macro);
570 if (ftepp->output_on)
571 util_htset(ftepp->macros, macro->name, (void*)macro);
573 ppmacro_delete(macro);
576 for (; l < ftepp_ctx(ftepp).line; ++l)
577 ftepp_out(ftepp, "\n", true);
582 * When a macro is used we have to handle parameters as well
583 * as special-concatenation via ## or stringification via #
585 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
586 * this kind of parens. Curly braces or [] don't count towards the
593 static void macroparam_clean(macroparam *self)
596 for (i = 0; i < vec_size(self->tokens); ++i)
597 pptoken_delete(self->tokens[i]);
598 vec_free(self->tokens);
601 /* need to leave the last token up */
602 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
604 macroparam *params = NULL;
610 if (!ftepp_skipallwhite(ftepp))
612 while (ftepp->token != ')') {
614 if (!ftepp_skipallwhite(ftepp))
616 while (parens || ftepp->token != ',') {
617 if (ftepp->token == '(')
619 else if (ftepp->token == ')') {
624 ptok = pptoken_make(ftepp);
625 vec_push(mp.tokens, ptok);
626 if (ftepp_next(ftepp) >= TOKEN_EOF) {
627 ftepp_error(ftepp, "unexpected EOF in macro call");
631 vec_push(params, mp);
633 if (ftepp->token == ')')
635 if (ftepp->token != ',') {
636 ftepp_error(ftepp, "expected closing paren or comma in macro call");
639 if (ftepp_next(ftepp) >= TOKEN_EOF) {
640 ftepp_error(ftepp, "unexpected EOF in macro call");
644 /* need to leave that up
645 if (ftepp_next(ftepp) >= TOKEN_EOF) {
646 ftepp_error(ftepp, "unexpected EOF in macro call");
650 *out_params = params;
655 macroparam_clean(&mp);
656 for (i = 0; i < vec_size(params); ++i)
657 macroparam_clean(¶ms[i]);
662 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
665 for (i = 0; i < vec_size(macro->params); ++i) {
666 if (!strcmp(macro->params[i], name)) {
674 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
679 switch (token->token) {
680 case TOKEN_STRINGCONST:
683 /* in preprocessor mode strings already are string,
684 * so we don't get actual newline bytes here.
685 * Still need to escape backslashes and quotes.
688 case '\\': ftepp_out(ftepp, "\\\\", false); break;
689 case '"': ftepp_out(ftepp, "\\\"", false); break;
692 ftepp_out(ftepp, chs, false);
699 ftepp_out(ftepp, " ", false);
702 ftepp_out(ftepp, "\\n", false);
705 ftepp_out(ftepp, token->value, false);
710 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
713 ftepp_out(ftepp, "\"", false);
714 for (i = 0; i < vec_size(param->tokens); ++i)
715 ftepp_stringify_token(ftepp, param->tokens[i]);
716 ftepp_out(ftepp, "\"", false);
719 static void ftepp_recursion_header(ftepp_t *ftepp)
721 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
724 static void ftepp_recursion_footer(ftepp_t *ftepp)
726 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
729 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
733 for (i = 0; i < vec_size(param->tokens); ++i) {
734 out = param->tokens[i];
735 if (out->token == TOKEN_EOL)
736 ftepp_out(ftepp, "\n", false);
738 ftepp_out(ftepp, out->value, false);
742 static bool ftepp_preprocess(ftepp_t *ftepp);
743 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
746 char *old_string = ftepp->output_string;
748 lex_file *old_lexer = ftepp->lex;
749 size_t vararg_start = vec_size(macro->params);
761 if (vararg_start < vec_size(params))
762 varargs = vec_size(params) - vararg_start;
767 if (!vec_size(macro->output))
770 ftepp->output_string = NULL;
771 for (o = 0; o < vec_size(macro->output); ++o) {
772 pptoken *out = macro->output[o];
773 switch (out->token) {
775 if (!macro->variadic) {
776 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
777 vec_free(old_string);
784 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
785 for (++pi; pi < varargs; ++pi) {
786 ftepp_out(ftepp, ", ", false);
787 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
791 case TOKEN_VA_ARGS_ARRAY:
792 if ((size_t)out->constval.i >= varargs) {
793 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
794 vec_free(old_string);
798 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
802 util_asprintf(&buffer, "%d", varargs);
803 ftepp_out(ftepp, buffer, false);
810 if (!macro_params_find(macro, out->value, &pi)) {
811 ftepp_out(ftepp, out->value, false);
814 ftepp_param_out(ftepp, ¶ms[pi]);
817 if (o + 1 < vec_size(macro->output)) {
818 nextok = macro->output[o+1]->token;
820 /* raw concatenation */
824 if ( (nextok == TOKEN_IDENT ||
825 nextok == TOKEN_KEYWORD ||
826 nextok == TOKEN_TYPENAME) &&
827 macro_params_find(macro, macro->output[o+1]->value, &pi))
830 ftepp_stringify(ftepp, ¶ms[pi]);
834 ftepp_out(ftepp, "#", false);
837 ftepp_out(ftepp, "\n", false);
840 ftepp_out(ftepp, out->value, false);
844 vec_push(ftepp->output_string, 0);
845 /* Now run the preprocessor recursively on this string buffer */
847 printf("__________\n%s\n=========\n", ftepp->output_string);
849 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
851 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
856 inlex->line = ftepp->lex->line;
857 inlex->sline = ftepp->lex->sline;
860 old_inmacro = ftepp->in_macro;
861 ftepp->in_macro = true;
862 ftepp->output_string = NULL;
863 if (!ftepp_preprocess(ftepp)) {
864 ftepp->in_macro = old_inmacro;
865 vec_free(ftepp->lex->open_string);
866 vec_free(ftepp->output_string);
867 lex_close(ftepp->lex);
871 ftepp->in_macro = old_inmacro;
872 vec_free(ftepp->lex->open_string);
873 lex_close(ftepp->lex);
875 inner_string = ftepp->output_string;
876 ftepp->output_string = old_string;
878 has_newlines = (strchr(inner_string, '\n') != NULL);
880 if (has_newlines && !old_inmacro)
881 ftepp_recursion_header(ftepp);
883 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
884 vec_free(inner_string);
886 if (has_newlines && !old_inmacro)
887 ftepp_recursion_footer(ftepp);
889 if (resetline && !ftepp->in_macro) {
891 util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
892 ftepp_out(ftepp, lineno, false);
895 old_string = ftepp->output_string;
897 ftepp->lex = old_lexer;
898 ftepp->output_string = old_string;
902 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
905 macroparam *params = NULL;
909 if (!macro->has_params) {
910 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
917 if (!ftepp_skipallwhite(ftepp))
920 if (ftepp->token != '(') {
921 ftepp_error(ftepp, "expected macro parameters in parenthesis");
926 paramline = ftepp->lex->sline;
927 if (!ftepp_macro_call_params(ftepp, ¶ms))
930 if ( vec_size(params) < vec_size(macro->params) ||
931 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
933 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
934 (macro->variadic ? " at least" : ""),
935 (unsigned int)vec_size(macro->params),
936 (unsigned int)vec_size(params));
941 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
946 for (o = 0; o < vec_size(params); ++o)
947 macroparam_clean(¶ms[o]);
953 * #if - the FTEQCC way:
954 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
955 * <numbers> => True if the number is not 0
956 * !<factor> => True if the factor yields false
957 * !!<factor> => ERROR on 2 or more unary nots
958 * <macro> => becomes the macro's FIRST token regardless of parameters
959 * <e> && <e> => True if both expressions are true
960 * <e> || <e> => True if either expression is true
962 * <ident> => False (remember for macros the <macro> rule applies instead)
963 * Unary + and - are weird and wrong in fteqcc so we don't allow them
964 * parenthesis in expressions are allowed
965 * parameter lists on macros are errors
966 * No mathematical calculations are executed
968 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
969 static bool ftepp_if_op(ftepp_t *ftepp)
971 ftepp->lex->flags.noops = false;
973 if (!ftepp_skipspace(ftepp))
975 ftepp->lex->flags.noops = true;
978 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
984 if (!ftepp_skipspace(ftepp))
987 while (ftepp->token == '!') {
990 if (!ftepp_skipspace(ftepp))
994 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
998 if (!ftepp_skipspace(ftepp))
1002 switch (ftepp->token) {
1004 case TOKEN_TYPENAME:
1006 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
1008 if (!ftepp_skipspace(ftepp))
1010 if (ftepp->token != '(') {
1011 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
1015 if (!ftepp_skipspace(ftepp))
1017 if (ftepp->token != TOKEN_IDENT &&
1018 ftepp->token != TOKEN_TYPENAME &&
1019 ftepp->token != TOKEN_KEYWORD)
1021 ftepp_error(ftepp, "defined() used on an unexpected token type");
1024 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1027 if (!ftepp_skipspace(ftepp))
1029 if (ftepp->token != ')') {
1030 ftepp_error(ftepp, "expected closing paren");
1036 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1037 if (!macro || !vec_size(macro->output)) {
1041 /* This does not expand recursively! */
1042 switch (macro->output[0]->token) {
1043 case TOKEN_INTCONST:
1044 *value_out = macro->output[0]->constval.i;
1045 *out = !!(macro->output[0]->constval.i);
1047 case TOKEN_FLOATCONST:
1048 *value_out = macro->output[0]->constval.f;
1049 *out = !!(macro->output[0]->constval.f);
1057 case TOKEN_STRINGCONST:
1061 case TOKEN_INTCONST:
1062 *value_out = ftepp->lex->tok.constval.i;
1063 *out = !!(ftepp->lex->tok.constval.i);
1065 case TOKEN_FLOATCONST:
1066 *value_out = ftepp->lex->tok.constval.f;
1067 *out = !!(ftepp->lex->tok.constval.f);
1072 if (!ftepp_if_expr(ftepp, out, value_out))
1074 if (ftepp->token != ')') {
1075 ftepp_error(ftepp, "expected closing paren in #if expression");
1081 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1082 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1083 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1087 *value_out = -*value_out;
1090 *value_out = (*out ? 1 : 0);
1096 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1098 if (!ftepp_next(ftepp))
1100 return ftepp_if_value(ftepp, out, value_out);
1104 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1106 if (!ftepp_if_value(ftepp, out, value_out))
1109 if (!ftepp_if_op(ftepp))
1112 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1115 /* FTEQCC is all right-associative and no precedence here */
1116 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1117 !strcmp(ftepp_tokval(ftepp), "||"))
1120 char opc = ftepp_tokval(ftepp)[0];
1124 if (!ftepp_next(ftepp))
1126 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1130 *out = *out && next;
1132 *out = *out || next;
1134 *value_out = (*out ? 1 : 0);
1137 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1138 !strcmp(ftepp_tokval(ftepp), "!=") ||
1139 !strcmp(ftepp_tokval(ftepp), ">=") ||
1140 !strcmp(ftepp_tokval(ftepp), "<=") ||
1141 !strcmp(ftepp_tokval(ftepp), ">") ||
1142 !strcmp(ftepp_tokval(ftepp), "<"))
1145 const char opc0 = ftepp_tokval(ftepp)[0];
1146 const char opc1 = ftepp_tokval(ftepp)[1];
1149 if (!ftepp_next(ftepp))
1151 if (!ftepp_if_expr(ftepp, &next, &other))
1155 *out = (*value_out == other);
1156 else if (opc0 == '!')
1157 *out = (*value_out != other);
1158 else if (opc0 == '>') {
1159 if (opc1 == '=') *out = (*value_out >= other);
1160 else *out = (*value_out > other);
1162 else if (opc0 == '<') {
1163 if (opc1 == '=') *out = (*value_out <= other);
1164 else *out = (*value_out < other);
1166 *value_out = (*out ? 1 : 0);
1171 ftepp_error(ftepp, "junk after #if");
1176 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1178 bool result = false;
1181 memset(cond, 0, sizeof(*cond));
1182 (void)ftepp_next(ftepp);
1184 if (!ftepp_skipspace(ftepp))
1186 if (ftepp->token == TOKEN_EOL) {
1187 ftepp_error(ftepp, "expected expression for #if-directive");
1191 if (!ftepp_if_expr(ftepp, &result, &dummy))
1199 * ifdef is rather simple
1201 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1204 memset(cond, 0, sizeof(*cond));
1205 (void)ftepp_next(ftepp);
1206 if (!ftepp_skipspace(ftepp))
1209 switch (ftepp->token) {
1211 case TOKEN_TYPENAME:
1213 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1216 ftepp_error(ftepp, "expected macro name");
1220 (void)ftepp_next(ftepp);
1221 if (!ftepp_skipspace(ftepp))
1223 /* relaxing this condition
1224 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1225 ftepp_error(ftepp, "stray tokens after #ifdef");
1234 * undef is also simple
1236 static bool ftepp_undef(ftepp_t *ftepp)
1238 (void)ftepp_next(ftepp);
1239 if (!ftepp_skipspace(ftepp))
1242 if (ftepp->output_on) {
1243 switch (ftepp->token) {
1245 case TOKEN_TYPENAME:
1247 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1250 ftepp_error(ftepp, "expected macro name");
1255 (void)ftepp_next(ftepp);
1256 if (!ftepp_skipspace(ftepp))
1258 /* relaxing this condition
1259 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1260 ftepp_error(ftepp, "stray tokens after #ifdef");
1267 /* Special unescape-string function which skips a leading quote
1268 * and stops at a quote, not just at \0
1270 static void unescape(const char *str, char *out) {
1272 while (*str && *str != '"') {
1276 case '\\': *out++ = *str; break;
1277 case '"': *out++ = *str; break;
1278 case 'a': *out++ = '\a'; break;
1279 case 'b': *out++ = '\b'; break;
1280 case 'r': *out++ = '\r'; break;
1281 case 'n': *out++ = '\n'; break;
1282 case 't': *out++ = '\t'; break;
1283 case 'f': *out++ = '\f'; break;
1284 case 'v': *out++ = '\v'; break;
1299 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1302 char *filename = NULL;
1303 const char *last_slash;
1309 last_slash = strrchr(pathfile, '/');
1312 len = last_slash - pathfile;
1313 memcpy(vec_add(filename, len), pathfile, len);
1314 vec_push(filename, '/');
1318 memcpy(vec_add(filename, len+1), file, len);
1319 vec_last(filename) = 0;
1321 fp = fs_file_open(filename, "rb");
1330 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1332 char *filename = NULL;
1334 filename = ftepp_include_find_path(file, ftepp->includename);
1336 filename = ftepp_include_find_path(file, ftepp->itemname);
1340 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1341 char *message = NULL;
1343 if (!ftepp_skipspace(ftepp))
1346 /* handle the odd non string constant case so it works like C */
1347 if (ftepp->token != TOKEN_STRINGCONST) {
1349 vec_upload(message, "#warning", 8);
1351 while (ftepp->token != TOKEN_EOL) {
1352 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1355 vec_push(message, '\0');
1356 if (ftepp->output_on)
1357 store = ftepp_warn(ftepp, WARN_CPP, message);
1364 if (!ftepp->output_on)
1367 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1368 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1371 static void ftepp_directive_error(ftepp_t *ftepp) {
1372 char *message = NULL;
1374 if (!ftepp_skipspace(ftepp))
1377 /* handle the odd non string constant case so it works like C */
1378 if (ftepp->token != TOKEN_STRINGCONST) {
1379 vec_upload(message, "#error", 6);
1381 while (ftepp->token != TOKEN_EOL) {
1382 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1385 vec_push(message, '\0');
1386 if (ftepp->output_on)
1387 ftepp_error(ftepp, message);
1392 if (!ftepp->output_on)
1395 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1396 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1399 static void ftepp_directive_message(ftepp_t *ftepp) {
1400 char *message = NULL;
1402 if (!ftepp_skipspace(ftepp))
1405 /* handle the odd non string constant case so it works like C */
1406 if (ftepp->token != TOKEN_STRINGCONST) {
1407 vec_upload(message, "#message", 8);
1409 while (ftepp->token != TOKEN_EOL) {
1410 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1413 vec_push(message, '\0');
1414 if (ftepp->output_on)
1415 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1420 if (!ftepp->output_on)
1423 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1424 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1429 * FIXME: do we need/want a -I option?
1430 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1432 static bool ftepp_include(ftepp_t *ftepp)
1434 lex_file *old_lexer = ftepp->lex;
1439 char *old_includename;
1441 (void)ftepp_next(ftepp);
1442 if (!ftepp_skipspace(ftepp))
1445 if (ftepp->token != TOKEN_STRINGCONST) {
1446 ftepp_error(ftepp, "expected filename to include");
1450 if (!ftepp->output_on) {
1455 ctx = ftepp_ctx(ftepp);
1457 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1459 ftepp_out(ftepp, "\n#pragma file(", false);
1460 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1461 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1463 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1465 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1468 inlex = lex_open(filename);
1470 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1475 old_includename = ftepp->includename;
1476 ftepp->includename = filename;
1477 if (!ftepp_preprocess(ftepp)) {
1478 vec_free(ftepp->includename);
1479 ftepp->includename = old_includename;
1480 lex_close(ftepp->lex);
1481 ftepp->lex = old_lexer;
1484 vec_free(ftepp->includename);
1485 ftepp->includename = old_includename;
1486 lex_close(ftepp->lex);
1487 ftepp->lex = old_lexer;
1489 ftepp_out(ftepp, "\n#pragma file(", false);
1490 ftepp_out(ftepp, ctx.file, false);
1491 util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1492 ftepp_out(ftepp, lineno, false);
1495 (void)ftepp_next(ftepp);
1496 if (!ftepp_skipspace(ftepp))
1498 if (ftepp->token != TOKEN_EOL) {
1499 ftepp_error(ftepp, "stray tokens after #include");
1502 (void)ftepp_next(ftepp);
1507 /* Basic structure handlers */
1508 static bool ftepp_else_allowed(ftepp_t *ftepp)
1510 if (!vec_size(ftepp->conditions)) {
1511 ftepp_error(ftepp, "#else without #if");
1514 if (vec_last(ftepp->conditions).had_else) {
1515 ftepp_error(ftepp, "multiple #else for a single #if");
1521 static bool ftepp_hash(ftepp_t *ftepp)
1526 lex_ctx ctx = ftepp_ctx(ftepp);
1528 if (!ftepp_skipspace(ftepp))
1531 switch (ftepp->token) {
1534 case TOKEN_TYPENAME:
1535 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1536 return ftepp_define(ftepp);
1538 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1539 return ftepp_undef(ftepp);
1541 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1542 if (!ftepp_ifdef(ftepp, &cond))
1544 cond.was_on = cond.on;
1545 vec_push(ftepp->conditions, cond);
1546 ftepp->output_on = ftepp->output_on && cond.on;
1549 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1550 if (!ftepp_ifdef(ftepp, &cond))
1553 cond.was_on = cond.on;
1554 vec_push(ftepp->conditions, cond);
1555 ftepp->output_on = ftepp->output_on && cond.on;
1558 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1559 if (!ftepp_else_allowed(ftepp))
1561 if (!ftepp_ifdef(ftepp, &cond))
1563 pc = &vec_last(ftepp->conditions);
1564 pc->on = !pc->was_on && cond.on;
1565 pc->was_on = pc->was_on || pc->on;
1566 ftepp_update_output_condition(ftepp);
1569 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1570 if (!ftepp_else_allowed(ftepp))
1572 if (!ftepp_ifdef(ftepp, &cond))
1575 pc = &vec_last(ftepp->conditions);
1576 pc->on = !pc->was_on && cond.on;
1577 pc->was_on = pc->was_on || pc->on;
1578 ftepp_update_output_condition(ftepp);
1581 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1582 if (!ftepp_else_allowed(ftepp))
1584 if (!ftepp_if(ftepp, &cond))
1586 pc = &vec_last(ftepp->conditions);
1587 pc->on = !pc->was_on && cond.on;
1588 pc->was_on = pc->was_on || pc->on;
1589 ftepp_update_output_condition(ftepp);
1592 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1593 if (!ftepp_if(ftepp, &cond))
1595 cond.was_on = cond.on;
1596 vec_push(ftepp->conditions, cond);
1597 ftepp->output_on = ftepp->output_on && cond.on;
1600 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1601 if (!ftepp_else_allowed(ftepp))
1603 pc = &vec_last(ftepp->conditions);
1604 pc->on = !pc->was_on;
1605 pc->had_else = true;
1607 ftepp_update_output_condition(ftepp);
1610 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1611 if (!vec_size(ftepp->conditions)) {
1612 ftepp_error(ftepp, "#endif without #if");
1615 vec_pop(ftepp->conditions);
1617 ftepp_update_output_condition(ftepp);
1620 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1621 return ftepp_include(ftepp);
1623 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1624 ftepp_out(ftepp, "#", false);
1627 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1628 ftepp_directive_warning(ftepp);
1631 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1632 ftepp_directive_error(ftepp);
1635 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1636 ftepp_directive_message(ftepp);
1640 if (ftepp->output_on) {
1641 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1648 /* break; never reached */
1650 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1653 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1656 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1659 /* Builtins! Don't forget the builtins! */
1660 case TOKEN_INTCONST:
1661 case TOKEN_FLOATCONST:
1662 ftepp_out(ftepp, "#", false);
1665 if (!ftepp_skipspace(ftepp))
1670 static bool ftepp_preprocess(ftepp_t *ftepp)
1673 bool newline = true;
1676 char *expand = NULL;
1678 ftepp->lex->flags.preprocessing = true;
1679 ftepp->lex->flags.mergelines = false;
1680 ftepp->lex->flags.noops = true;
1685 if (ftepp->token >= TOKEN_EOF)
1691 switch (ftepp->token) {
1694 case TOKEN_TYPENAME:
1695 /* is it a predef? */
1696 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1697 char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp));
1699 expand = predef(ftepp->lex);
1700 ftepp_out (ftepp, expand, false);
1708 if (ftepp->output_on)
1709 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1714 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1718 if (!ftepp_macro_call(ftepp, macro))
1719 ftepp->token = TOKEN_ERROR;
1723 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1727 ftepp->lex->flags.mergelines = true;
1728 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1729 ftepp_error(ftepp, "error in preprocessor directive");
1730 ftepp->token = TOKEN_ERROR;
1733 if (!ftepp_hash(ftepp))
1734 ftepp->token = TOKEN_ERROR;
1735 ftepp->lex->flags.mergelines = false;
1739 ftepp_out(ftepp, "\n", true);
1743 /* same as default but don't set newline=false */
1744 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1749 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1753 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1755 /* force a 0 at the end but don't count it as added to the output */
1756 vec_push(ftepp->output_string, 0);
1757 vec_shrinkby(ftepp->output_string, 1);
1759 return (ftepp->token == TOKEN_EOF);
1762 /* Like in parser.c - files keep the previous state so we have one global
1763 * preprocessor. Except here we will want to warn about dangling #ifs.
1765 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1768 if (vec_size(ftepp->conditions)) {
1769 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1772 lex_close(ftepp->lex);
1774 if (ftepp->itemname) {
1775 mem_d(ftepp->itemname);
1776 ftepp->itemname = NULL;
1781 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1783 ftepp->lex = lex_open(filename);
1784 ftepp->itemname = util_strdup(filename);
1786 con_out("failed to open file \"%s\"\n", filename);
1789 if (!ftepp_preprocess(ftepp))
1791 return ftepp_preprocess_done(ftepp);
1794 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1796 ftepp->lex = lex_open_string(str, strlen(str), name);
1797 ftepp->itemname = util_strdup(name);
1799 con_out("failed to create lexer for string \"%s\"\n", name);
1802 if (!ftepp_preprocess(ftepp))
1804 return ftepp_preprocess_done(ftepp);
1808 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1809 char *create = NULL;
1811 /* use saner path for empty macros */
1813 ftepp_add_define(ftepp, "__builtin__", name);
1817 vec_upload(create, "#define ", 8);
1818 vec_upload(create, name, strlen(name));
1819 vec_push (create, ' ');
1820 vec_upload(create, value, strlen(value));
1821 vec_push (create, 0);
1823 ftepp_preprocess_string(ftepp, "__builtin__", create);
1827 ftepp_t *ftepp_create()
1833 ftepp = ftepp_new();
1837 memset(minor, 0, sizeof(minor));
1838 memset(major, 0, sizeof(major));
1840 /* set the right macro based on the selected standard */
1841 ftepp_add_define(ftepp, NULL, "GMQCC");
1842 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1843 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1852 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1853 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1854 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1855 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1856 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1857 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1858 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1859 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1860 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1861 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1872 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1873 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1876 * We're going to just make __NULL__ nil, which works for 60% of the
1877 * cases of __NULL_ for fteqcc.
1879 ftepp_add_macro(ftepp, "__NULL__", "nil");
1884 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1887 lex_ctx ctx = { "__builtin__", 0, 0 };
1889 macro = ppmacro_new(ctx, name);
1890 /*vec_push(ftepp->macros, macro);*/
1891 util_htset(ftepp->macros, name, macro);
1894 const char *ftepp_get(ftepp_t *ftepp)
1896 return ftepp->output_string;
1899 void ftepp_flush(ftepp_t *ftepp)
1901 ftepp_flush_do(ftepp);
1904 void ftepp_finish(ftepp_t *ftepp)
1908 ftepp_delete(ftepp);