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
29 #define HT_MACROS 1024
39 /* a copy from the lexer */
53 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
60 typedef struct ftepp_s {
66 ppcondition *conditions;
68 ht macros; /* hashtable<string, ppmacro*> */
78 * Implement the predef subsystem now. We can do this safely with the
79 * help of lexer contexts.
81 static uint32_t ftepp_predef_countval = 0;
82 static uint32_t ftepp_predef_randval = 0;
85 char *ftepp_predef_date(lex_file *context) {
86 struct tm *itime = NULL;
88 char *value = (char*)mem_a(82);
89 /* 82 is enough for strftime but we also have " " in our string */
97 localtime_s(itime, &rtime);
99 itime = localtime(&rtime);
102 strftime(value, 82, "\"%b %d %Y\"", itime);
108 char *ftepp_predef_time(lex_file *context) {
109 struct tm *itime = NULL;
111 char *value = (char*)mem_a(82);
112 /* 82 is enough for strftime but we also have " " in our string */
120 localtime_s(itime, &rtime);
122 itime = localtime(&rtime);
125 strftime(value, 82, "\"%X\"", itime);
131 char *ftepp_predef_line(lex_file *context) {
133 util_asprintf(&value, "%d", (int)context->line);
137 char *ftepp_predef_file(lex_file *context) {
138 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
139 char *value = (char*)mem_a(length);
140 util_snprintf(value, length, "\"%s\"", context->name);
144 /* __COUNTER_LAST__ */
145 char *ftepp_predef_counterlast(lex_file *context) {
147 util_asprintf(&value, "%u", ftepp_predef_countval);
153 char *ftepp_predef_counter(lex_file *context) {
155 ftepp_predef_countval ++;
156 util_asprintf(&value, "%u", ftepp_predef_countval);
162 char *ftepp_predef_random(lex_file *context) {
164 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
165 util_asprintf(&value, "%u", ftepp_predef_randval);
170 /* __RANDOM_LAST__ */
171 char *ftepp_predef_randomlast(lex_file *context) {
173 util_asprintf(&value, "%u", ftepp_predef_randval);
179 char *ftepp_predef_timestamp(lex_file *context) {
184 if (stat(context->name, &finfo))
185 return util_strdup("\"<failed to determine timestamp>\"");
188 * ctime and it's fucking annoying newline char, no worries, we're
189 * professionals here.
191 find = ctime(&finfo.st_mtime);
192 value = (char*)mem_a(strlen(find) + 1);
193 memcpy(&value[1], find, (size = strlen(find)) - 1);
201 const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = {
202 { "__LINE__", &ftepp_predef_line },
203 { "__FILE__", &ftepp_predef_file },
204 { "__COUNTER__", &ftepp_predef_counter },
205 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
206 { "__RANDOM__", &ftepp_predef_random },
207 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
208 { "__DATE__", &ftepp_predef_date },
209 { "__TIME__", &ftepp_predef_time },
210 { "__TIME_STAMP__", &ftepp_predef_timestamp }
213 #define ftepp_tokval(f) ((f)->lex->tok.value)
214 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
216 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
223 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
227 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
234 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
238 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
244 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
249 static pptoken *pptoken_make(ftepp_t *ftepp)
251 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
252 token->token = ftepp->token;
254 if (token->token == TOKEN_WHITE)
255 token->value = util_strdup(" ");
258 token->value = util_strdup(ftepp_tokval(ftepp));
260 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
264 static GMQCC_INLINE void pptoken_delete(pptoken *self)
270 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
272 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
275 memset(macro, 0, sizeof(*macro));
276 macro->name = util_strdup(name);
280 static void ppmacro_delete(ppmacro *self)
283 for (i = 0; i < vec_size(self->params); ++i)
284 mem_d(self->params[i]);
285 vec_free(self->params);
286 for (i = 0; i < vec_size(self->output); ++i)
287 pptoken_delete(self->output[i]);
288 vec_free(self->output);
293 static ftepp_t* ftepp_new()
297 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
298 memset(ftepp, 0, sizeof(*ftepp));
300 ftepp->macros = util_htnew(HT_MACROS);
301 ftepp->output_on = true;
306 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
308 vec_free(self->output_string);
311 static void ftepp_delete(ftepp_t *self)
313 ftepp_flush_do(self);
315 mem_d(self->itemname);
316 if (self->includename)
317 vec_free(self->includename);
319 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
321 vec_free(self->conditions);
323 lex_close(self->lex);
327 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
329 if (ignore_cond || ftepp->output_on)
334 data = vec_add(ftepp->output_string, len);
335 memcpy(data, str, len);
339 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
342 ftepp->output_on = true;
343 for (i = 0; i < vec_size(ftepp->conditions); ++i)
344 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
347 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
349 return (ppmacro*)util_htget(ftepp->macros, name);
352 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
354 util_htrm(ftepp->macros, name, NULL);
357 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
359 return (ftepp->token = lex_do(ftepp->lex));
362 /* Important: this does not skip newlines! */
363 static bool ftepp_skipspace(ftepp_t *ftepp)
365 if (ftepp->token != TOKEN_WHITE)
367 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
368 if (ftepp->token >= TOKEN_EOF) {
369 ftepp_error(ftepp, "unexpected end of preprocessor directive");
375 /* this one skips EOLs as well */
376 static bool ftepp_skipallwhite(ftepp_t *ftepp)
378 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
382 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
383 if (ftepp->token >= TOKEN_EOF) {
384 ftepp_error(ftepp, "unexpected end of preprocessor directive");
391 * The huge macro parsing code...
393 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
397 if (!ftepp_skipspace(ftepp))
399 if (ftepp->token == ')')
401 switch (ftepp->token) {
405 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
408 macro->variadic = true;
411 ftepp_error(ftepp, "unexpected token in parameter list");
415 if (!ftepp_skipspace(ftepp))
417 if (macro->variadic && ftepp->token != ')') {
418 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
421 } while (ftepp->token == ',');
423 if (ftepp->token != ')') {
424 ftepp_error(ftepp, "expected closing paren after macro parameter list");
428 /* skipspace happens in ftepp_define */
432 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
435 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
436 bool subscript = false;
438 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
439 subscript = !!(ftepp_next(ftepp) == '#');
441 if (subscript && ftepp_next(ftepp) != '#') {
442 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
444 } else if (subscript) {
445 if (ftepp_next(ftepp) == '[') {
446 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
447 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
451 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
453 if (ftepp_next(ftepp) != ']') {
454 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
459 * mark it as an array to be handled later as such and not
460 * as traditional __VA_ARGS__
462 ftepp->token = TOKEN_VA_ARGS_ARRAY;
463 ptok = pptoken_make(ftepp);
464 ptok->constval.i = index;
465 vec_push(macro->output, ptok);
468 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
472 int old = ftepp->token;
473 ftepp->token = TOKEN_VA_ARGS;
474 ptok = pptoken_make(ftepp);
475 vec_push(macro->output, ptok);
479 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
480 ftepp->token = TOKEN_VA_COUNT;
481 ptok = pptoken_make(ftepp);
482 vec_push(macro->output, ptok);
485 ptok = pptoken_make(ftepp);
486 vec_push(macro->output, ptok);
490 /* recursive expansion can cause EOFs here */
491 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
492 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
498 static bool ftepp_define(ftepp_t *ftepp)
500 ppmacro *macro = NULL;
501 size_t l = ftepp_ctx(ftepp).line;
503 (void)ftepp_next(ftepp);
504 if (!ftepp_skipspace(ftepp))
507 switch (ftepp->token) {
511 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
512 if (macro && ftepp->output_on) {
513 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
515 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
517 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
520 ftepp_error(ftepp, "expected macro name");
524 (void)ftepp_next(ftepp);
526 if (ftepp->token == '(') {
527 macro->has_params = true;
528 if (!ftepp_define_params(ftepp, macro)) {
529 ppmacro_delete(macro);
534 if (!ftepp_skipspace(ftepp)) {
535 ppmacro_delete(macro);
539 if (!ftepp_define_body(ftepp, macro)) {
540 ppmacro_delete(macro);
545 if (ftepp->output_on)
546 vec_push(ftepp->macros, macro);
548 if (ftepp->output_on)
549 util_htset(ftepp->macros, macro->name, (void*)macro);
551 ppmacro_delete(macro);
554 for (; l < ftepp_ctx(ftepp).line; ++l)
555 ftepp_out(ftepp, "\n", true);
560 * When a macro is used we have to handle parameters as well
561 * as special-concatenation via ## or stringification via #
563 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
564 * this kind of parens. Curly braces or [] don't count towards the
571 static void macroparam_clean(macroparam *self)
574 for (i = 0; i < vec_size(self->tokens); ++i)
575 pptoken_delete(self->tokens[i]);
576 vec_free(self->tokens);
579 /* need to leave the last token up */
580 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
582 macroparam *params = NULL;
588 if (!ftepp_skipallwhite(ftepp))
590 while (ftepp->token != ')') {
592 if (!ftepp_skipallwhite(ftepp))
594 while (parens || ftepp->token != ',') {
595 if (ftepp->token == '(')
597 else if (ftepp->token == ')') {
602 ptok = pptoken_make(ftepp);
603 vec_push(mp.tokens, ptok);
604 if (ftepp_next(ftepp) >= TOKEN_EOF) {
605 ftepp_error(ftepp, "unexpected EOF in macro call");
609 vec_push(params, mp);
611 if (ftepp->token == ')')
613 if (ftepp->token != ',') {
614 ftepp_error(ftepp, "expected closing paren or comma in macro call");
617 if (ftepp_next(ftepp) >= TOKEN_EOF) {
618 ftepp_error(ftepp, "unexpected EOF in macro call");
622 /* need to leave that up
623 if (ftepp_next(ftepp) >= TOKEN_EOF) {
624 ftepp_error(ftepp, "unexpected EOF in macro call");
628 *out_params = params;
633 macroparam_clean(&mp);
634 for (i = 0; i < vec_size(params); ++i)
635 macroparam_clean(¶ms[i]);
640 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
643 for (i = 0; i < vec_size(macro->params); ++i) {
644 if (!strcmp(macro->params[i], name)) {
652 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
657 switch (token->token) {
658 case TOKEN_STRINGCONST:
661 /* in preprocessor mode strings already are string,
662 * so we don't get actual newline bytes here.
663 * Still need to escape backslashes and quotes.
666 case '\\': ftepp_out(ftepp, "\\\\", false); break;
667 case '"': ftepp_out(ftepp, "\\\"", false); break;
670 ftepp_out(ftepp, chs, false);
677 ftepp_out(ftepp, " ", false);
680 ftepp_out(ftepp, "\\n", false);
683 ftepp_out(ftepp, token->value, false);
688 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
691 ftepp_out(ftepp, "\"", false);
692 for (i = 0; i < vec_size(param->tokens); ++i)
693 ftepp_stringify_token(ftepp, param->tokens[i]);
694 ftepp_out(ftepp, "\"", false);
697 static void ftepp_recursion_header(ftepp_t *ftepp)
699 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
702 static void ftepp_recursion_footer(ftepp_t *ftepp)
704 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
707 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
711 for (i = 0; i < vec_size(param->tokens); ++i) {
712 out = param->tokens[i];
713 if (out->token == TOKEN_EOL)
714 ftepp_out(ftepp, "\n", false);
716 ftepp_out(ftepp, out->value, false);
720 static bool ftepp_preprocess(ftepp_t *ftepp);
721 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
724 char *old_string = ftepp->output_string;
726 lex_file *old_lexer = ftepp->lex;
727 size_t vararg_start = vec_size(macro->params);
739 if (vararg_start < vec_size(params))
740 varargs = vec_size(params) - vararg_start;
745 if (!vec_size(macro->output))
748 ftepp->output_string = NULL;
749 for (o = 0; o < vec_size(macro->output); ++o) {
750 pptoken *out = macro->output[o];
751 switch (out->token) {
753 if (!macro->variadic) {
754 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
755 vec_free(old_string);
762 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
763 for (++pi; pi < varargs; ++pi) {
764 ftepp_out(ftepp, ", ", false);
765 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
769 case TOKEN_VA_ARGS_ARRAY:
770 if ((size_t)out->constval.i >= varargs) {
771 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
772 vec_free(old_string);
776 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
780 util_asprintf(&buffer, "%d", varargs);
781 ftepp_out(ftepp, buffer, false);
788 if (!macro_params_find(macro, out->value, &pi)) {
789 ftepp_out(ftepp, out->value, false);
792 ftepp_param_out(ftepp, ¶ms[pi]);
795 if (o + 1 < vec_size(macro->output)) {
796 nextok = macro->output[o+1]->token;
798 /* raw concatenation */
802 if ( (nextok == TOKEN_IDENT ||
803 nextok == TOKEN_KEYWORD ||
804 nextok == TOKEN_TYPENAME) &&
805 macro_params_find(macro, macro->output[o+1]->value, &pi))
808 ftepp_stringify(ftepp, ¶ms[pi]);
812 ftepp_out(ftepp, "#", false);
815 ftepp_out(ftepp, "\n", false);
818 ftepp_out(ftepp, out->value, false);
822 vec_push(ftepp->output_string, 0);
823 /* Now run the preprocessor recursively on this string buffer */
825 printf("__________\n%s\n=========\n", ftepp->output_string);
827 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
829 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
834 inlex->line = ftepp->lex->line;
835 inlex->sline = ftepp->lex->sline;
838 old_inmacro = ftepp->in_macro;
839 ftepp->in_macro = true;
840 ftepp->output_string = NULL;
841 if (!ftepp_preprocess(ftepp)) {
842 ftepp->in_macro = old_inmacro;
843 vec_free(ftepp->lex->open_string);
844 vec_free(ftepp->output_string);
845 lex_close(ftepp->lex);
849 ftepp->in_macro = old_inmacro;
850 vec_free(ftepp->lex->open_string);
851 lex_close(ftepp->lex);
853 inner_string = ftepp->output_string;
854 ftepp->output_string = old_string;
856 has_newlines = (strchr(inner_string, '\n') != NULL);
858 if (has_newlines && !old_inmacro)
859 ftepp_recursion_header(ftepp);
861 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
862 vec_free(inner_string);
864 if (has_newlines && !old_inmacro)
865 ftepp_recursion_footer(ftepp);
867 if (resetline && !ftepp->in_macro) {
869 util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
870 ftepp_out(ftepp, lineno, false);
873 old_string = ftepp->output_string;
875 ftepp->lex = old_lexer;
876 ftepp->output_string = old_string;
880 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
883 macroparam *params = NULL;
887 if (!macro->has_params) {
888 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
895 if (!ftepp_skipallwhite(ftepp))
898 if (ftepp->token != '(') {
899 ftepp_error(ftepp, "expected macro parameters in parenthesis");
904 paramline = ftepp->lex->sline;
905 if (!ftepp_macro_call_params(ftepp, ¶ms))
908 if ( vec_size(params) < vec_size(macro->params) ||
909 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
911 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
912 (macro->variadic ? " at least" : ""),
913 (unsigned int)vec_size(macro->params),
914 (unsigned int)vec_size(params));
919 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
924 for (o = 0; o < vec_size(params); ++o)
925 macroparam_clean(¶ms[o]);
931 * #if - the FTEQCC way:
932 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
933 * <numbers> => True if the number is not 0
934 * !<factor> => True if the factor yields false
935 * !!<factor> => ERROR on 2 or more unary nots
936 * <macro> => becomes the macro's FIRST token regardless of parameters
937 * <e> && <e> => True if both expressions are true
938 * <e> || <e> => True if either expression is true
940 * <ident> => False (remember for macros the <macro> rule applies instead)
941 * Unary + and - are weird and wrong in fteqcc so we don't allow them
942 * parenthesis in expressions are allowed
943 * parameter lists on macros are errors
944 * No mathematical calculations are executed
946 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
947 static bool ftepp_if_op(ftepp_t *ftepp)
949 ftepp->lex->flags.noops = false;
951 if (!ftepp_skipspace(ftepp))
953 ftepp->lex->flags.noops = true;
956 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
962 if (!ftepp_skipspace(ftepp))
965 while (ftepp->token == '!') {
968 if (!ftepp_skipspace(ftepp))
972 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
976 if (!ftepp_skipspace(ftepp))
980 switch (ftepp->token) {
984 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
986 if (!ftepp_skipspace(ftepp))
988 if (ftepp->token != '(') {
989 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
993 if (!ftepp_skipspace(ftepp))
995 if (ftepp->token != TOKEN_IDENT &&
996 ftepp->token != TOKEN_TYPENAME &&
997 ftepp->token != TOKEN_KEYWORD)
999 ftepp_error(ftepp, "defined() used on an unexpected token type");
1002 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1005 if (!ftepp_skipspace(ftepp))
1007 if (ftepp->token != ')') {
1008 ftepp_error(ftepp, "expected closing paren");
1014 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1015 if (!macro || !vec_size(macro->output)) {
1019 /* This does not expand recursively! */
1020 switch (macro->output[0]->token) {
1021 case TOKEN_INTCONST:
1022 *value_out = macro->output[0]->constval.i;
1023 *out = !!(macro->output[0]->constval.i);
1025 case TOKEN_FLOATCONST:
1026 *value_out = macro->output[0]->constval.f;
1027 *out = !!(macro->output[0]->constval.f);
1035 case TOKEN_STRINGCONST:
1039 case TOKEN_INTCONST:
1040 *value_out = ftepp->lex->tok.constval.i;
1041 *out = !!(ftepp->lex->tok.constval.i);
1043 case TOKEN_FLOATCONST:
1044 *value_out = ftepp->lex->tok.constval.f;
1045 *out = !!(ftepp->lex->tok.constval.f);
1050 if (!ftepp_if_expr(ftepp, out, value_out))
1052 if (ftepp->token != ')') {
1053 ftepp_error(ftepp, "expected closing paren in #if expression");
1059 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1060 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1061 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1065 *value_out = -*value_out;
1068 *value_out = (*out ? 1 : 0);
1074 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1076 if (!ftepp_next(ftepp))
1078 return ftepp_if_value(ftepp, out, value_out);
1082 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1084 if (!ftepp_if_value(ftepp, out, value_out))
1087 if (!ftepp_if_op(ftepp))
1090 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1093 /* FTEQCC is all right-associative and no precedence here */
1094 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1095 !strcmp(ftepp_tokval(ftepp), "||"))
1098 char opc = ftepp_tokval(ftepp)[0];
1102 if (!ftepp_next(ftepp))
1104 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1108 *out = *out && next;
1110 *out = *out || next;
1112 *value_out = (*out ? 1 : 0);
1115 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1116 !strcmp(ftepp_tokval(ftepp), "!=") ||
1117 !strcmp(ftepp_tokval(ftepp), ">=") ||
1118 !strcmp(ftepp_tokval(ftepp), "<=") ||
1119 !strcmp(ftepp_tokval(ftepp), ">") ||
1120 !strcmp(ftepp_tokval(ftepp), "<"))
1123 const char opc0 = ftepp_tokval(ftepp)[0];
1124 const char opc1 = ftepp_tokval(ftepp)[1];
1127 if (!ftepp_next(ftepp))
1129 if (!ftepp_if_expr(ftepp, &next, &other))
1133 *out = (*value_out == other);
1134 else if (opc0 == '!')
1135 *out = (*value_out != other);
1136 else if (opc0 == '>') {
1137 if (opc1 == '=') *out = (*value_out >= other);
1138 else *out = (*value_out > other);
1140 else if (opc0 == '<') {
1141 if (opc1 == '=') *out = (*value_out <= other);
1142 else *out = (*value_out < other);
1144 *value_out = (*out ? 1 : 0);
1149 ftepp_error(ftepp, "junk after #if");
1154 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1156 bool result = false;
1159 memset(cond, 0, sizeof(*cond));
1160 (void)ftepp_next(ftepp);
1162 if (!ftepp_skipspace(ftepp))
1164 if (ftepp->token == TOKEN_EOL) {
1165 ftepp_error(ftepp, "expected expression for #if-directive");
1169 if (!ftepp_if_expr(ftepp, &result, &dummy))
1177 * ifdef is rather simple
1179 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1182 memset(cond, 0, sizeof(*cond));
1183 (void)ftepp_next(ftepp);
1184 if (!ftepp_skipspace(ftepp))
1187 switch (ftepp->token) {
1189 case TOKEN_TYPENAME:
1191 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1194 ftepp_error(ftepp, "expected macro name");
1198 (void)ftepp_next(ftepp);
1199 if (!ftepp_skipspace(ftepp))
1201 /* relaxing this condition
1202 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1203 ftepp_error(ftepp, "stray tokens after #ifdef");
1212 * undef is also simple
1214 static bool ftepp_undef(ftepp_t *ftepp)
1216 (void)ftepp_next(ftepp);
1217 if (!ftepp_skipspace(ftepp))
1220 if (ftepp->output_on) {
1221 switch (ftepp->token) {
1223 case TOKEN_TYPENAME:
1225 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1228 ftepp_error(ftepp, "expected macro name");
1233 (void)ftepp_next(ftepp);
1234 if (!ftepp_skipspace(ftepp))
1236 /* relaxing this condition
1237 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1238 ftepp_error(ftepp, "stray tokens after #ifdef");
1245 /* Special unescape-string function which skips a leading quote
1246 * and stops at a quote, not just at \0
1248 static void unescape(const char *str, char *out) {
1250 while (*str && *str != '"') {
1254 case '\\': *out++ = *str; break;
1255 case '"': *out++ = *str; break;
1256 case 'a': *out++ = '\a'; break;
1257 case 'b': *out++ = '\b'; break;
1258 case 'r': *out++ = '\r'; break;
1259 case 'n': *out++ = '\n'; break;
1260 case 't': *out++ = '\t'; break;
1261 case 'f': *out++ = '\f'; break;
1262 case 'v': *out++ = '\v'; break;
1277 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1280 char *filename = NULL;
1281 const char *last_slash;
1287 last_slash = strrchr(pathfile, '/');
1290 len = last_slash - pathfile;
1291 memcpy(vec_add(filename, len), pathfile, len);
1292 vec_push(filename, '/');
1296 memcpy(vec_add(filename, len+1), file, len);
1297 vec_last(filename) = 0;
1299 fp = fs_file_open(filename, "rb");
1308 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1310 char *filename = NULL;
1312 filename = ftepp_include_find_path(file, ftepp->includename);
1314 filename = ftepp_include_find_path(file, ftepp->itemname);
1318 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1319 char *message = NULL;
1321 if (!ftepp_skipspace(ftepp))
1324 /* handle the odd non string constant case so it works like C */
1325 if (ftepp->token != TOKEN_STRINGCONST) {
1327 vec_upload(message, "#warning", 8);
1329 while (ftepp->token != TOKEN_EOL) {
1330 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1333 vec_push(message, '\0');
1334 if (ftepp->output_on)
1335 store = ftepp_warn(ftepp, WARN_CPP, message);
1342 if (!ftepp->output_on)
1345 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1346 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1349 static void ftepp_directive_error(ftepp_t *ftepp) {
1350 char *message = NULL;
1352 if (!ftepp_skipspace(ftepp))
1355 /* handle the odd non string constant case so it works like C */
1356 if (ftepp->token != TOKEN_STRINGCONST) {
1357 vec_upload(message, "#error", 6);
1359 while (ftepp->token != TOKEN_EOL) {
1360 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1363 vec_push(message, '\0');
1364 if (ftepp->output_on)
1365 ftepp_error(ftepp, message);
1370 if (!ftepp->output_on)
1373 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1374 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1377 static void ftepp_directive_message(ftepp_t *ftepp) {
1378 char *message = NULL;
1380 if (!ftepp_skipspace(ftepp))
1383 /* handle the odd non string constant case so it works like C */
1384 if (ftepp->token != TOKEN_STRINGCONST) {
1385 vec_upload(message, "#message", 8);
1387 while (ftepp->token != TOKEN_EOL) {
1388 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1391 vec_push(message, '\0');
1392 if (ftepp->output_on)
1393 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1398 if (!ftepp->output_on)
1401 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1402 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1407 * FIXME: do we need/want a -I option?
1408 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1410 static bool ftepp_include(ftepp_t *ftepp)
1412 lex_file *old_lexer = ftepp->lex;
1417 char *old_includename;
1419 (void)ftepp_next(ftepp);
1420 if (!ftepp_skipspace(ftepp))
1423 if (ftepp->token != TOKEN_STRINGCONST) {
1424 ftepp_error(ftepp, "expected filename to include");
1428 if (!ftepp->output_on) {
1433 ctx = ftepp_ctx(ftepp);
1435 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1437 ftepp_out(ftepp, "\n#pragma file(", false);
1438 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1439 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1441 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1443 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1446 inlex = lex_open(filename);
1448 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1453 old_includename = ftepp->includename;
1454 ftepp->includename = filename;
1455 if (!ftepp_preprocess(ftepp)) {
1456 vec_free(ftepp->includename);
1457 ftepp->includename = old_includename;
1458 lex_close(ftepp->lex);
1459 ftepp->lex = old_lexer;
1462 vec_free(ftepp->includename);
1463 ftepp->includename = old_includename;
1464 lex_close(ftepp->lex);
1465 ftepp->lex = old_lexer;
1467 ftepp_out(ftepp, "\n#pragma file(", false);
1468 ftepp_out(ftepp, ctx.file, false);
1469 util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1470 ftepp_out(ftepp, lineno, false);
1473 (void)ftepp_next(ftepp);
1474 if (!ftepp_skipspace(ftepp))
1476 if (ftepp->token != TOKEN_EOL) {
1477 ftepp_error(ftepp, "stray tokens after #include");
1480 (void)ftepp_next(ftepp);
1485 /* Basic structure handlers */
1486 static bool ftepp_else_allowed(ftepp_t *ftepp)
1488 if (!vec_size(ftepp->conditions)) {
1489 ftepp_error(ftepp, "#else without #if");
1492 if (vec_last(ftepp->conditions).had_else) {
1493 ftepp_error(ftepp, "multiple #else for a single #if");
1499 static bool ftepp_hash(ftepp_t *ftepp)
1504 lex_ctx ctx = ftepp_ctx(ftepp);
1506 if (!ftepp_skipspace(ftepp))
1509 switch (ftepp->token) {
1512 case TOKEN_TYPENAME:
1513 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1514 return ftepp_define(ftepp);
1516 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1517 return ftepp_undef(ftepp);
1519 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1520 if (!ftepp_ifdef(ftepp, &cond))
1522 cond.was_on = cond.on;
1523 vec_push(ftepp->conditions, cond);
1524 ftepp->output_on = ftepp->output_on && cond.on;
1527 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1528 if (!ftepp_ifdef(ftepp, &cond))
1531 cond.was_on = cond.on;
1532 vec_push(ftepp->conditions, cond);
1533 ftepp->output_on = ftepp->output_on && cond.on;
1536 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1537 if (!ftepp_else_allowed(ftepp))
1539 if (!ftepp_ifdef(ftepp, &cond))
1541 pc = &vec_last(ftepp->conditions);
1542 pc->on = !pc->was_on && cond.on;
1543 pc->was_on = pc->was_on || pc->on;
1544 ftepp_update_output_condition(ftepp);
1547 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1548 if (!ftepp_else_allowed(ftepp))
1550 if (!ftepp_ifdef(ftepp, &cond))
1553 pc = &vec_last(ftepp->conditions);
1554 pc->on = !pc->was_on && cond.on;
1555 pc->was_on = pc->was_on || pc->on;
1556 ftepp_update_output_condition(ftepp);
1559 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1560 if (!ftepp_else_allowed(ftepp))
1562 if (!ftepp_if(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), "if")) {
1571 if (!ftepp_if(ftepp, &cond))
1573 cond.was_on = cond.on;
1574 vec_push(ftepp->conditions, cond);
1575 ftepp->output_on = ftepp->output_on && cond.on;
1578 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1579 if (!ftepp_else_allowed(ftepp))
1581 pc = &vec_last(ftepp->conditions);
1582 pc->on = !pc->was_on;
1583 pc->had_else = true;
1585 ftepp_update_output_condition(ftepp);
1588 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1589 if (!vec_size(ftepp->conditions)) {
1590 ftepp_error(ftepp, "#endif without #if");
1593 vec_pop(ftepp->conditions);
1595 ftepp_update_output_condition(ftepp);
1598 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1599 return ftepp_include(ftepp);
1601 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1602 ftepp_out(ftepp, "#", false);
1605 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1606 ftepp_directive_warning(ftepp);
1609 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1610 ftepp_directive_error(ftepp);
1613 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1614 ftepp_directive_message(ftepp);
1618 if (ftepp->output_on) {
1619 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1626 /* break; never reached */
1628 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1631 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1634 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1637 /* Builtins! Don't forget the builtins! */
1638 case TOKEN_INTCONST:
1639 case TOKEN_FLOATCONST:
1640 ftepp_out(ftepp, "#", false);
1643 if (!ftepp_skipspace(ftepp))
1648 static bool ftepp_preprocess(ftepp_t *ftepp)
1651 bool newline = true;
1654 char *expand = NULL;
1657 ftepp->lex->flags.preprocessing = true;
1658 ftepp->lex->flags.mergelines = false;
1659 ftepp->lex->flags.noops = true;
1664 if (ftepp->token >= TOKEN_EOF)
1670 switch (ftepp->token) {
1673 case TOKEN_TYPENAME:
1674 /* is it a predef? */
1675 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1676 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1677 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1678 expand = ftepp_predefs[i].func(ftepp->lex);
1679 ftepp_out(ftepp, expand, false);
1680 ftepp_next(ftepp); /* skip */
1682 mem_d(expand); /* free memory */
1688 if (ftepp->output_on)
1689 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1694 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1698 if (!ftepp_macro_call(ftepp, macro))
1699 ftepp->token = TOKEN_ERROR;
1703 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1707 ftepp->lex->flags.mergelines = true;
1708 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1709 ftepp_error(ftepp, "error in preprocessor directive");
1710 ftepp->token = TOKEN_ERROR;
1713 if (!ftepp_hash(ftepp))
1714 ftepp->token = TOKEN_ERROR;
1715 ftepp->lex->flags.mergelines = false;
1719 ftepp_out(ftepp, "\n", true);
1723 /* same as default but don't set newline=false */
1724 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1729 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1733 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1735 /* force a 0 at the end but don't count it as added to the output */
1736 vec_push(ftepp->output_string, 0);
1737 vec_shrinkby(ftepp->output_string, 1);
1739 return (ftepp->token == TOKEN_EOF);
1742 /* Like in parser.c - files keep the previous state so we have one global
1743 * preprocessor. Except here we will want to warn about dangling #ifs.
1745 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1748 if (vec_size(ftepp->conditions)) {
1749 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1752 lex_close(ftepp->lex);
1754 if (ftepp->itemname) {
1755 mem_d(ftepp->itemname);
1756 ftepp->itemname = NULL;
1761 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1763 ftepp->lex = lex_open(filename);
1764 ftepp->itemname = util_strdup(filename);
1766 con_out("failed to open file \"%s\"\n", filename);
1769 if (!ftepp_preprocess(ftepp))
1771 return ftepp_preprocess_done(ftepp);
1774 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1776 ftepp->lex = lex_open_string(str, strlen(str), name);
1777 ftepp->itemname = util_strdup(name);
1779 con_out("failed to create lexer for string \"%s\"\n", name);
1782 if (!ftepp_preprocess(ftepp))
1784 return ftepp_preprocess_done(ftepp);
1788 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1789 char *create = NULL;
1791 /* use saner path for empty macros */
1793 ftepp_add_define(ftepp, "__builtin__", name);
1797 vec_upload(create, "#define ", 8);
1798 vec_upload(create, name, strlen(name));
1799 vec_push (create, ' ');
1800 vec_upload(create, value, strlen(value));
1801 vec_push (create, 0);
1803 ftepp_preprocess_string(ftepp, "__builtin__", create);
1807 ftepp_t *ftepp_create()
1813 ftepp = ftepp_new();
1817 memset(minor, 0, sizeof(minor));
1818 memset(major, 0, sizeof(major));
1820 /* set the right macro based on the selected standard */
1821 ftepp_add_define(ftepp, NULL, "GMQCC");
1822 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1823 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1832 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1833 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1834 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1835 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1836 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1837 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1838 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1839 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1840 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1841 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1852 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1853 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1856 * We're going to just make __NULL__ nil, which works for 60% of the
1857 * cases of __NULL_ for fteqcc.
1859 ftepp_add_macro(ftepp, "__NULL__", "nil");
1864 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1867 lex_ctx ctx = { "__builtin__", 0 };
1869 macro = ppmacro_new(ctx, name);
1870 /*vec_push(ftepp->macros, macro);*/
1871 util_htset(ftepp->macros, name, macro);
1874 const char *ftepp_get(ftepp_t *ftepp)
1876 return ftepp->output_string;
1879 void ftepp_flush(ftepp_t *ftepp)
1881 ftepp_flush_do(ftepp);
1884 void ftepp_finish(ftepp_t *ftepp)
1888 ftepp_delete(ftepp);