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
43 /* a copy from the lexer */
57 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
64 typedef struct ftepp_s {
70 ppcondition *conditions;
72 ht macros; /* hashtable<string, ppmacro*> */
81 * Implement the predef subsystem now. We can do this safely with the
82 * help of lexer contexts.
84 static uint32_t ftepp_predef_countval = 0;
85 static uint32_t ftepp_predef_randval = 0;
88 static char *ftepp_predef_date(lex_file *context) {
89 const struct tm *itime = NULL;
90 char *value = (char*)mem_a(82);
96 itime = util_localtime(&rtime);
97 strftime(value, 82, "\"%b %d %Y\"", itime);
103 static char *ftepp_predef_time(lex_file *context) {
104 const struct tm *itime = NULL;
105 char *value = (char*)mem_a(82);
111 itime = util_localtime(&rtime);
112 strftime(value, 82, "\"%X\"", itime);
118 static char *ftepp_predef_line(lex_file *context) {
120 util_asprintf(&value, "%d", (int)context->line);
124 static char *ftepp_predef_file(lex_file *context) {
125 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
126 char *value = (char*)mem_a(length);
127 util_snprintf(value, length, "\"%s\"", context->name);
131 /* __COUNTER_LAST__ */
132 static char *ftepp_predef_counterlast(lex_file *context) {
134 util_asprintf(&value, "%u", ftepp_predef_countval);
140 static char *ftepp_predef_counter(lex_file *context) {
142 ftepp_predef_countval ++;
143 util_asprintf(&value, "%u", ftepp_predef_countval);
149 static char *ftepp_predef_random(lex_file *context) {
151 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
152 util_asprintf(&value, "%u", ftepp_predef_randval);
157 /* __RANDOM_LAST__ */
158 static char *ftepp_predef_randomlast(lex_file *context) {
160 util_asprintf(&value, "%u", ftepp_predef_randval);
166 static char *ftepp_predef_timestamp(lex_file *context) {
172 if (stat(context->name, &finfo))
173 return util_strdup("\"<failed to determine timestamp>\"");
175 find = util_ctime(&finfo.st_mtime);
176 value = (char*)mem_a(strlen(find) + 1);
177 memcpy(&value[1], find, (size = strlen(find)) - 1);
187 char *(*func)(lex_file *);
190 static const ftepp_predef_t ftepp_predefs[] = {
191 { "__LINE__", &ftepp_predef_line },
192 { "__FILE__", &ftepp_predef_file },
193 { "__COUNTER__", &ftepp_predef_counter },
194 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
195 { "__RANDOM__", &ftepp_predef_random },
196 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
197 { "__DATE__", &ftepp_predef_date },
198 { "__TIME__", &ftepp_predef_time },
199 { "__TIME_STAMP__", &ftepp_predef_timestamp }
202 static GMQCC_INLINE size_t ftepp_predef_index(const char *name) {
203 /* no hashtable here, we simply check for one to exist the naive way */
205 for(i = 1; i < GMQCC_ARRAY_COUNT(ftepp_predefs) + 1; i++)
206 if (!strcmp(ftepp_predefs[i-1].name, name))
211 bool ftepp_predef_exists(const char *name);
212 bool ftepp_predef_exists(const char *name) {
213 return ftepp_predef_index(name) != 0;
216 /* singleton because we're allowed */
217 static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) {
218 size_t i = ftepp_predef_index(name);
219 return (i != 0) ? ftepp_predefs[i-1].func : NULL;
222 #define ftepp_tokval(f) ((f)->lex->tok.value)
223 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
225 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx_t ctx, const char *fmt, ...)
232 con_cvprintmsg(ctx, LVL_ERROR, "error", fmt, ap);
236 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
243 con_cvprintmsg(ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
247 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
253 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
258 static pptoken *pptoken_make(ftepp_t *ftepp)
260 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
261 token->token = ftepp->token;
263 if (token->token == TOKEN_WHITE)
264 token->value = util_strdup(" ");
267 token->value = util_strdup(ftepp_tokval(ftepp));
269 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
273 static GMQCC_INLINE void pptoken_delete(pptoken *self)
279 static ppmacro *ppmacro_new(lex_ctx_t ctx, const char *name)
281 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
284 memset(macro, 0, sizeof(*macro));
285 macro->name = util_strdup(name);
289 static void ppmacro_delete(ppmacro *self)
292 for (i = 0; i < vec_size(self->params); ++i)
293 mem_d(self->params[i]);
294 vec_free(self->params);
295 for (i = 0; i < vec_size(self->output); ++i)
296 pptoken_delete(self->output[i]);
297 vec_free(self->output);
302 static ftepp_t* ftepp_new(void)
306 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
307 memset(ftepp, 0, sizeof(*ftepp));
309 ftepp->macros = util_htnew(HT_MACROS);
310 ftepp->output_on = true;
315 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
317 vec_free(self->output_string);
320 static void ftepp_delete(ftepp_t *self)
322 ftepp_flush_do(self);
324 mem_d(self->itemname);
325 if (self->includename)
326 vec_free(self->includename);
328 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
330 vec_free(self->conditions);
332 lex_close(self->lex);
336 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
338 if (ignore_cond || ftepp->output_on)
343 data = vec_add(ftepp->output_string, len);
344 memcpy(data, str, len);
348 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
351 ftepp->output_on = true;
352 for (i = 0; i < vec_size(ftepp->conditions); ++i)
353 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
356 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
358 return (ppmacro*)util_htget(ftepp->macros, name);
361 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
363 util_htrm(ftepp->macros, name, (void (*)(void*))&ppmacro_delete);
366 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
368 return (ftepp->token = lex_do(ftepp->lex));
371 /* Important: this does not skip newlines! */
372 static bool ftepp_skipspace(ftepp_t *ftepp)
374 if (ftepp->token != TOKEN_WHITE)
376 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
377 if (ftepp->token >= TOKEN_EOF) {
378 ftepp_error(ftepp, "unexpected end of preprocessor directive");
384 /* this one skips EOLs as well */
385 static bool ftepp_skipallwhite(ftepp_t *ftepp)
387 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
391 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
392 if (ftepp->token >= TOKEN_EOF) {
393 ftepp_error(ftepp, "unexpected end of preprocessor directive");
400 * The huge macro parsing code...
402 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
406 if (!ftepp_skipspace(ftepp))
408 if (ftepp->token == ')')
410 switch (ftepp->token) {
414 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
417 macro->variadic = true;
420 ftepp_error(ftepp, "unexpected token in parameter list");
424 if (!ftepp_skipspace(ftepp))
426 if (macro->variadic && ftepp->token != ')') {
427 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
430 } while (ftepp->token == ',');
432 if (ftepp->token != ')') {
433 ftepp_error(ftepp, "expected closing paren after macro parameter list");
437 /* skipspace happens in ftepp_define */
441 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
444 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
445 bool subscript = false;
447 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
448 subscript = !!(ftepp_next(ftepp) == '#');
450 if (subscript && ftepp_next(ftepp) != '#') {
451 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
453 } else if (subscript) {
454 if (ftepp_next(ftepp) == '[') {
455 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
456 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
460 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
462 if (ftepp_next(ftepp) != ']') {
463 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
468 * mark it as an array to be handled later as such and not
469 * as traditional __VA_ARGS__
471 ftepp->token = TOKEN_VA_ARGS_ARRAY;
472 ptok = pptoken_make(ftepp);
473 ptok->constval.i = index;
474 vec_push(macro->output, ptok);
477 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
481 int old = ftepp->token;
482 ftepp->token = TOKEN_VA_ARGS;
483 ptok = pptoken_make(ftepp);
484 vec_push(macro->output, ptok);
488 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
489 ftepp->token = TOKEN_VA_COUNT;
490 ptok = pptoken_make(ftepp);
491 vec_push(macro->output, ptok);
494 ptok = pptoken_make(ftepp);
495 vec_push(macro->output, ptok);
499 /* recursive expansion can cause EOFs here */
500 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
501 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
507 static bool ftepp_define(ftepp_t *ftepp)
509 ppmacro *macro = NULL;
510 size_t l = ftepp_ctx(ftepp).line;
512 (void)ftepp_next(ftepp);
513 if (!ftepp_skipspace(ftepp))
516 switch (ftepp->token) {
520 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
521 if (macro && ftepp->output_on) {
522 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
524 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
526 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
529 ftepp_error(ftepp, "expected macro name");
533 (void)ftepp_next(ftepp);
535 if (ftepp->token == '(') {
536 macro->has_params = true;
537 if (!ftepp_define_params(ftepp, macro)) {
538 ppmacro_delete(macro);
543 if (!ftepp_skipspace(ftepp)) {
544 ppmacro_delete(macro);
548 if (!ftepp_define_body(ftepp, macro)) {
549 ppmacro_delete(macro);
553 if (ftepp->output_on)
554 util_htset(ftepp->macros, macro->name, (void*)macro);
556 ppmacro_delete(macro);
559 for (; l < ftepp_ctx(ftepp).line; ++l)
560 ftepp_out(ftepp, "\n", true);
565 * When a macro is used we have to handle parameters as well
566 * as special-concatenation via ## or stringification via #
568 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
569 * this kind of parens. Curly braces or [] don't count towards the
576 static void macroparam_clean(macroparam *self)
579 for (i = 0; i < vec_size(self->tokens); ++i)
580 pptoken_delete(self->tokens[i]);
581 vec_free(self->tokens);
584 /* need to leave the last token up */
585 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
587 macroparam *params = NULL;
593 if (!ftepp_skipallwhite(ftepp))
595 while (ftepp->token != ')') {
597 if (!ftepp_skipallwhite(ftepp))
599 while (parens || ftepp->token != ',') {
600 if (ftepp->token == '(')
602 else if (ftepp->token == ')') {
607 ptok = pptoken_make(ftepp);
608 vec_push(mp.tokens, ptok);
609 if (ftepp_next(ftepp) >= TOKEN_EOF) {
610 ftepp_error(ftepp, "unexpected end of file in macro call");
614 vec_push(params, mp);
616 if (ftepp->token == ')')
618 if (ftepp->token != ',') {
619 ftepp_error(ftepp, "expected closing paren or comma in macro call");
622 if (ftepp_next(ftepp) >= TOKEN_EOF) {
623 ftepp_error(ftepp, "unexpected end of file in macro call");
627 *out_params = params;
632 macroparam_clean(&mp);
633 for (i = 0; i < vec_size(params); ++i)
634 macroparam_clean(¶ms[i]);
639 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
642 for (i = 0; i < vec_size(macro->params); ++i) {
643 if (!strcmp(macro->params[i], name)) {
651 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
656 switch (token->token) {
657 case TOKEN_STRINGCONST:
660 /* in preprocessor mode strings already are string,
661 * so we don't get actual newline bytes here.
662 * Still need to escape backslashes and quotes.
665 case '\\': ftepp_out(ftepp, "\\\\", false); break;
666 case '"': ftepp_out(ftepp, "\\\"", false); break;
669 ftepp_out(ftepp, chs, false);
676 ftepp_out(ftepp, " ", false);
679 ftepp_out(ftepp, "\\n", false);
682 ftepp_out(ftepp, token->value, false);
687 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
690 ftepp_out(ftepp, "\"", false);
691 for (i = 0; i < vec_size(param->tokens); ++i)
692 ftepp_stringify_token(ftepp, param->tokens[i]);
693 ftepp_out(ftepp, "\"", false);
696 static void ftepp_recursion_header(ftepp_t *ftepp)
698 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
701 static void ftepp_recursion_footer(ftepp_t *ftepp)
703 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
706 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
710 for (i = 0; i < vec_size(param->tokens); ++i) {
711 out = param->tokens[i];
712 if (out->token == TOKEN_EOL)
713 ftepp_out(ftepp, "\n", false);
715 ftepp_out(ftepp, out->value, false);
719 static bool ftepp_preprocess(ftepp_t *ftepp);
720 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
723 char *old_string = ftepp->output_string;
725 lex_file *old_lexer = ftepp->lex;
726 size_t vararg_start = vec_size(macro->params);
738 if (vararg_start < vec_size(params))
739 varargs = vec_size(params) - vararg_start;
744 if (!vec_size(macro->output))
747 ftepp->output_string = NULL;
748 for (o = 0; o < vec_size(macro->output); ++o) {
749 pptoken *out = macro->output[o];
750 switch (out->token) {
752 if (!macro->variadic) {
753 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
754 vec_free(old_string);
761 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
762 for (++pi; pi < varargs; ++pi) {
763 ftepp_out(ftepp, ", ", false);
764 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
768 case TOKEN_VA_ARGS_ARRAY:
769 if ((size_t)out->constval.i >= varargs) {
770 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
771 vec_free(old_string);
775 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
779 util_asprintf(&buffer, "%d", varargs);
780 ftepp_out(ftepp, buffer, false);
787 if (!macro_params_find(macro, out->value, &pi)) {
788 ftepp_out(ftepp, out->value, false);
791 ftepp_param_out(ftepp, ¶ms[pi]);
794 if (o + 1 < vec_size(macro->output)) {
795 nextok = macro->output[o+1]->token;
797 /* raw concatenation */
801 if ( (nextok == TOKEN_IDENT ||
802 nextok == TOKEN_KEYWORD ||
803 nextok == TOKEN_TYPENAME) &&
804 macro_params_find(macro, macro->output[o+1]->value, &pi))
807 ftepp_stringify(ftepp, ¶ms[pi]);
811 ftepp_out(ftepp, "#", false);
814 ftepp_out(ftepp, "\n", false);
817 ftepp_out(ftepp, out->value, false);
821 vec_push(ftepp->output_string, 0);
822 /* Now run the preprocessor recursively on this string buffer */
824 printf("__________\n%s\n=========\n", ftepp->output_string);
826 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
828 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
833 inlex->line = ftepp->lex->line;
834 inlex->sline = ftepp->lex->sline;
837 old_inmacro = ftepp->in_macro;
838 ftepp->in_macro = true;
839 ftepp->output_string = NULL;
840 if (!ftepp_preprocess(ftepp)) {
841 ftepp->in_macro = old_inmacro;
842 vec_free(ftepp->lex->open_string);
843 vec_free(ftepp->output_string);
844 lex_close(ftepp->lex);
848 ftepp->in_macro = old_inmacro;
849 vec_free(ftepp->lex->open_string);
850 lex_close(ftepp->lex);
852 inner_string = ftepp->output_string;
853 ftepp->output_string = old_string;
855 has_newlines = (strchr(inner_string, '\n') != NULL);
857 if (has_newlines && !old_inmacro)
858 ftepp_recursion_header(ftepp);
860 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
861 vec_free(inner_string);
863 if (has_newlines && !old_inmacro)
864 ftepp_recursion_footer(ftepp);
866 if (resetline && !ftepp->in_macro) {
868 util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
869 ftepp_out(ftepp, lineno, false);
872 old_string = ftepp->output_string;
874 ftepp->lex = old_lexer;
875 ftepp->output_string = old_string;
879 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
882 macroparam *params = NULL;
886 if (!macro->has_params) {
887 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
894 if (!ftepp_skipallwhite(ftepp))
897 if (ftepp->token != '(') {
898 ftepp_error(ftepp, "expected macro parameters in parenthesis");
903 paramline = ftepp->lex->sline;
904 if (!ftepp_macro_call_params(ftepp, ¶ms))
907 if ( vec_size(params) < vec_size(macro->params) ||
908 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
910 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
911 (macro->variadic ? " at least" : ""),
912 (unsigned int)vec_size(macro->params),
913 (unsigned int)vec_size(params));
918 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
923 for (o = 0; o < vec_size(params); ++o)
924 macroparam_clean(¶ms[o]);
930 * #if - the FTEQCC way:
931 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
932 * <numbers> => True if the number is not 0
933 * !<factor> => True if the factor yields false
934 * !!<factor> => ERROR on 2 or more unary nots
935 * <macro> => becomes the macro's FIRST token regardless of parameters
936 * <e> && <e> => True if both expressions are true
937 * <e> || <e> => True if either expression is true
939 * <ident> => False (remember for macros the <macro> rule applies instead)
940 * Unary + and - are weird and wrong in fteqcc so we don't allow them
941 * parenthesis in expressions are allowed
942 * parameter lists on macros are errors
943 * No mathematical calculations are executed
945 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
946 static bool ftepp_if_op(ftepp_t *ftepp)
948 ftepp->lex->flags.noops = false;
950 if (!ftepp_skipspace(ftepp))
952 ftepp->lex->flags.noops = true;
955 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
961 if (!ftepp_skipspace(ftepp))
964 while (ftepp->token == '!') {
967 if (!ftepp_skipspace(ftepp))
971 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
975 if (!ftepp_skipspace(ftepp))
979 switch (ftepp->token) {
983 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
985 if (!ftepp_skipspace(ftepp))
987 if (ftepp->token != '(') {
988 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
992 if (!ftepp_skipspace(ftepp))
994 if (ftepp->token != TOKEN_IDENT &&
995 ftepp->token != TOKEN_TYPENAME &&
996 ftepp->token != TOKEN_KEYWORD)
998 ftepp_error(ftepp, "defined() used on an unexpected token type");
1001 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1004 if (!ftepp_skipspace(ftepp))
1006 if (ftepp->token != ')') {
1007 ftepp_error(ftepp, "expected closing paren");
1013 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1014 if (!macro || !vec_size(macro->output)) {
1018 /* This does not expand recursively! */
1019 switch (macro->output[0]->token) {
1020 case TOKEN_INTCONST:
1021 *value_out = macro->output[0]->constval.i;
1022 *out = !!(macro->output[0]->constval.i);
1024 case TOKEN_FLOATCONST:
1025 *value_out = macro->output[0]->constval.f;
1026 *out = !!(macro->output[0]->constval.f);
1034 case TOKEN_STRINGCONST:
1038 case TOKEN_INTCONST:
1039 *value_out = ftepp->lex->tok.constval.i;
1040 *out = !!(ftepp->lex->tok.constval.i);
1042 case TOKEN_FLOATCONST:
1043 *value_out = ftepp->lex->tok.constval.f;
1044 *out = !!(ftepp->lex->tok.constval.f);
1049 if (!ftepp_if_expr(ftepp, out, value_out))
1051 if (ftepp->token != ')') {
1052 ftepp_error(ftepp, "expected closing paren in #if expression");
1058 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1059 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1060 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1064 *value_out = -*value_out;
1067 *value_out = (*out ? 1 : 0);
1073 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1075 if (!ftepp_next(ftepp))
1077 return ftepp_if_value(ftepp, out, value_out);
1081 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1083 if (!ftepp_if_value(ftepp, out, value_out))
1086 if (!ftepp_if_op(ftepp))
1089 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1092 /* FTEQCC is all right-associative and no precedence here */
1093 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1094 !strcmp(ftepp_tokval(ftepp), "||"))
1097 char opc = ftepp_tokval(ftepp)[0];
1101 if (!ftepp_next(ftepp))
1103 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1107 *out = *out && next;
1109 *out = *out || next;
1111 *value_out = (*out ? 1 : 0);
1114 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1115 !strcmp(ftepp_tokval(ftepp), "!=") ||
1116 !strcmp(ftepp_tokval(ftepp), ">=") ||
1117 !strcmp(ftepp_tokval(ftepp), "<=") ||
1118 !strcmp(ftepp_tokval(ftepp), ">") ||
1119 !strcmp(ftepp_tokval(ftepp), "<"))
1122 const char opc0 = ftepp_tokval(ftepp)[0];
1123 const char opc1 = ftepp_tokval(ftepp)[1];
1126 if (!ftepp_next(ftepp))
1128 if (!ftepp_if_expr(ftepp, &next, &other))
1132 *out = (*value_out == other);
1133 else if (opc0 == '!')
1134 *out = (*value_out != other);
1135 else if (opc0 == '>') {
1136 if (opc1 == '=') *out = (*value_out >= other);
1137 else *out = (*value_out > other);
1139 else if (opc0 == '<') {
1140 if (opc1 == '=') *out = (*value_out <= other);
1141 else *out = (*value_out < other);
1143 *value_out = (*out ? 1 : 0);
1148 ftepp_error(ftepp, "junk after #if");
1153 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1155 bool result = false;
1158 memset(cond, 0, sizeof(*cond));
1159 (void)ftepp_next(ftepp);
1161 if (!ftepp_skipspace(ftepp))
1163 if (ftepp->token == TOKEN_EOL) {
1164 ftepp_error(ftepp, "expected expression for #if-directive");
1168 if (!ftepp_if_expr(ftepp, &result, &dummy))
1176 * ifdef is rather simple
1178 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1181 memset(cond, 0, sizeof(*cond));
1182 (void)ftepp_next(ftepp);
1183 if (!ftepp_skipspace(ftepp))
1186 switch (ftepp->token) {
1188 case TOKEN_TYPENAME:
1190 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1193 ftepp_error(ftepp, "expected macro name");
1197 (void)ftepp_next(ftepp);
1198 if (!ftepp_skipspace(ftepp))
1200 /* relaxing this condition
1201 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1202 ftepp_error(ftepp, "stray tokens after #ifdef");
1211 * undef is also simple
1213 static bool ftepp_undef(ftepp_t *ftepp)
1215 (void)ftepp_next(ftepp);
1216 if (!ftepp_skipspace(ftepp))
1219 if (ftepp->output_on) {
1220 switch (ftepp->token) {
1222 case TOKEN_TYPENAME:
1224 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1227 ftepp_error(ftepp, "expected macro name");
1232 (void)ftepp_next(ftepp);
1233 if (!ftepp_skipspace(ftepp))
1235 /* relaxing this condition
1236 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1237 ftepp_error(ftepp, "stray tokens after #ifdef");
1244 /* Special unescape-string function which skips a leading quote
1245 * and stops at a quote, not just at \0
1247 static void unescape(const char *str, char *out) {
1249 while (*str && *str != '"') {
1253 case '\\': *out++ = *str; break;
1254 case '"': *out++ = *str; break;
1255 case 'a': *out++ = '\a'; break;
1256 case 'b': *out++ = '\b'; break;
1257 case 'r': *out++ = '\r'; break;
1258 case 'n': *out++ = '\n'; break;
1259 case 't': *out++ = '\t'; break;
1260 case 'f': *out++ = '\f'; break;
1261 case 'v': *out++ = '\v'; break;
1276 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1279 char *filename = NULL;
1280 const char *last_slash;
1286 last_slash = strrchr(pathfile, '/');
1289 len = last_slash - pathfile;
1290 memcpy(vec_add(filename, len), pathfile, len);
1291 vec_push(filename, '/');
1295 memcpy(vec_add(filename, len+1), file, len);
1296 vec_last(filename) = 0;
1298 fp = fs_file_open(filename, "rb");
1307 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1309 char *filename = NULL;
1311 filename = ftepp_include_find_path(file, ftepp->includename);
1313 filename = ftepp_include_find_path(file, ftepp->itemname);
1317 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1318 char *message = NULL;
1320 if (!ftepp_skipspace(ftepp))
1323 /* handle the odd non string constant case so it works like C */
1324 if (ftepp->token != TOKEN_STRINGCONST) {
1326 vec_append(message, 8, "#warning");
1328 while (ftepp->token != TOKEN_EOL) {
1329 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1332 vec_push(message, '\0');
1333 if (ftepp->output_on)
1334 store = ftepp_warn(ftepp, WARN_CPP, message);
1341 if (!ftepp->output_on)
1344 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1345 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1348 static void ftepp_directive_error(ftepp_t *ftepp) {
1349 char *message = NULL;
1351 if (!ftepp_skipspace(ftepp))
1354 /* handle the odd non string constant case so it works like C */
1355 if (ftepp->token != TOKEN_STRINGCONST) {
1356 vec_append(message, 6, "#error");
1358 while (ftepp->token != TOKEN_EOL) {
1359 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1362 vec_push(message, '\0');
1363 if (ftepp->output_on)
1364 ftepp_error(ftepp, message);
1369 if (!ftepp->output_on)
1372 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1373 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1376 static void ftepp_directive_message(ftepp_t *ftepp) {
1377 char *message = NULL;
1379 if (!ftepp_skipspace(ftepp))
1382 /* handle the odd non string constant case so it works like C */
1383 if (ftepp->token != TOKEN_STRINGCONST) {
1384 vec_append(message, 8, "#message");
1386 while (ftepp->token != TOKEN_EOL) {
1387 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1390 vec_push(message, '\0');
1391 if (ftepp->output_on)
1392 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1397 if (!ftepp->output_on)
1400 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1401 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1406 * FIXME: do we need/want a -I option?
1407 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1409 static bool ftepp_include(ftepp_t *ftepp)
1411 lex_file *old_lexer = ftepp->lex;
1416 char *old_includename;
1418 (void)ftepp_next(ftepp);
1419 if (!ftepp_skipspace(ftepp))
1422 if (ftepp->token != TOKEN_STRINGCONST) {
1423 ftepp_error(ftepp, "expected filename to include");
1427 if (!ftepp->output_on) {
1432 ctx = ftepp_ctx(ftepp);
1434 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1436 ftepp_out(ftepp, "\n#pragma file(", false);
1437 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1438 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1440 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1442 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1445 inlex = lex_open(filename);
1447 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1452 old_includename = ftepp->includename;
1453 ftepp->includename = filename;
1454 if (!ftepp_preprocess(ftepp)) {
1455 vec_free(ftepp->includename);
1456 ftepp->includename = old_includename;
1457 lex_close(ftepp->lex);
1458 ftepp->lex = old_lexer;
1461 vec_free(ftepp->includename);
1462 ftepp->includename = old_includename;
1463 lex_close(ftepp->lex);
1464 ftepp->lex = old_lexer;
1466 ftepp_out(ftepp, "\n#pragma file(", false);
1467 ftepp_out(ftepp, ctx.file, false);
1468 util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1469 ftepp_out(ftepp, lineno, false);
1472 (void)ftepp_next(ftepp);
1473 if (!ftepp_skipspace(ftepp))
1475 if (ftepp->token != TOKEN_EOL) {
1476 ftepp_error(ftepp, "stray tokens after #include");
1479 (void)ftepp_next(ftepp);
1484 /* Basic structure handlers */
1485 static bool ftepp_else_allowed(ftepp_t *ftepp)
1487 if (!vec_size(ftepp->conditions)) {
1488 ftepp_error(ftepp, "#else without #if");
1491 if (vec_last(ftepp->conditions).had_else) {
1492 ftepp_error(ftepp, "multiple #else for a single #if");
1498 static GMQCC_INLINE void ftepp_inmacro(ftepp_t *ftepp, const char *hash) {
1499 if (ftepp->in_macro)
1500 (void)!ftepp_warn(ftepp, WARN_DIRECTIVE_INMACRO, "`#%s` directive in macro", hash);
1503 static bool ftepp_hash(ftepp_t *ftepp)
1508 lex_ctx_t ctx = ftepp_ctx(ftepp);
1510 if (!ftepp_skipspace(ftepp))
1513 switch (ftepp->token) {
1516 case TOKEN_TYPENAME:
1517 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1518 ftepp_inmacro(ftepp, "define");
1519 return ftepp_define(ftepp);
1521 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1522 ftepp_inmacro(ftepp, "undef");
1523 return ftepp_undef(ftepp);
1525 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1526 ftepp_inmacro(ftepp, "ifdef");
1527 if (!ftepp_ifdef(ftepp, &cond))
1529 cond.was_on = cond.on;
1530 vec_push(ftepp->conditions, cond);
1531 ftepp->output_on = ftepp->output_on && cond.on;
1534 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1535 ftepp_inmacro(ftepp, "ifndef");
1536 if (!ftepp_ifdef(ftepp, &cond))
1539 cond.was_on = cond.on;
1540 vec_push(ftepp->conditions, cond);
1541 ftepp->output_on = ftepp->output_on && cond.on;
1544 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1545 ftepp_inmacro(ftepp, "elifdef");
1546 if (!ftepp_else_allowed(ftepp))
1548 if (!ftepp_ifdef(ftepp, &cond))
1550 pc = &vec_last(ftepp->conditions);
1551 pc->on = !pc->was_on && cond.on;
1552 pc->was_on = pc->was_on || pc->on;
1553 ftepp_update_output_condition(ftepp);
1556 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1557 ftepp_inmacro(ftepp, "elifndef");
1558 if (!ftepp_else_allowed(ftepp))
1560 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), "elif")) {
1570 ftepp_inmacro(ftepp, "elif");
1571 if (!ftepp_else_allowed(ftepp))
1573 if (!ftepp_if(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), "if")) {
1582 ftepp_inmacro(ftepp, "if");
1583 if (!ftepp_if(ftepp, &cond))
1585 cond.was_on = cond.on;
1586 vec_push(ftepp->conditions, cond);
1587 ftepp->output_on = ftepp->output_on && cond.on;
1590 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1591 ftepp_inmacro(ftepp, "else");
1592 if (!ftepp_else_allowed(ftepp))
1594 pc = &vec_last(ftepp->conditions);
1595 pc->on = !pc->was_on;
1596 pc->had_else = true;
1598 ftepp_update_output_condition(ftepp);
1601 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1602 ftepp_inmacro(ftepp, "endif");
1603 if (!vec_size(ftepp->conditions)) {
1604 ftepp_error(ftepp, "#endif without #if");
1607 vec_pop(ftepp->conditions);
1609 ftepp_update_output_condition(ftepp);
1612 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1613 ftepp_inmacro(ftepp, "include");
1614 return ftepp_include(ftepp);
1616 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1617 ftepp_out(ftepp, "#", false);
1620 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1621 ftepp_directive_warning(ftepp);
1624 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1625 ftepp_directive_error(ftepp);
1628 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1629 ftepp_directive_message(ftepp);
1633 if (ftepp->output_on) {
1634 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1641 /* break; never reached */
1643 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1646 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1649 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1652 /* Builtins! Don't forget the builtins! */
1653 case TOKEN_INTCONST:
1654 case TOKEN_FLOATCONST:
1655 ftepp_out(ftepp, "#", false);
1658 if (!ftepp_skipspace(ftepp))
1663 static bool ftepp_preprocess(ftepp_t *ftepp)
1666 bool newline = true;
1669 char *expand = NULL;
1671 ftepp->lex->flags.preprocessing = true;
1672 ftepp->lex->flags.mergelines = false;
1673 ftepp->lex->flags.noops = true;
1678 if (ftepp->token >= TOKEN_EOF)
1684 switch (ftepp->token) {
1687 case TOKEN_TYPENAME:
1688 /* is it a predef? */
1689 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1690 char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp));
1692 expand = predef(ftepp->lex);
1693 ftepp_out (ftepp, expand, false);
1701 if (ftepp->output_on)
1702 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1707 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1711 if (!ftepp_macro_call(ftepp, macro))
1712 ftepp->token = TOKEN_ERROR;
1716 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1720 ftepp->lex->flags.mergelines = true;
1721 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1722 ftepp_error(ftepp, "error in preprocessor directive");
1723 ftepp->token = TOKEN_ERROR;
1726 if (!ftepp_hash(ftepp))
1727 ftepp->token = TOKEN_ERROR;
1728 ftepp->lex->flags.mergelines = false;
1732 ftepp_out(ftepp, "\n", true);
1736 /* same as default but don't set newline=false */
1737 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1742 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1746 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1748 /* force a 0 at the end but don't count it as added to the output */
1749 vec_push(ftepp->output_string, 0);
1750 vec_shrinkby(ftepp->output_string, 1);
1752 return (ftepp->token == TOKEN_EOF);
1755 /* Like in parser.c - files keep the previous state so we have one global
1756 * preprocessor. Except here we will want to warn about dangling #ifs.
1758 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1761 if (vec_size(ftepp->conditions)) {
1762 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1765 lex_close(ftepp->lex);
1767 if (ftepp->itemname) {
1768 mem_d(ftepp->itemname);
1769 ftepp->itemname = NULL;
1774 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1776 ftepp->lex = lex_open(filename);
1777 ftepp->itemname = util_strdup(filename);
1779 con_out("failed to open file \"%s\"\n", filename);
1782 if (!ftepp_preprocess(ftepp))
1784 return ftepp_preprocess_done(ftepp);
1787 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1789 ftepp->lex = lex_open_string(str, strlen(str), name);
1790 ftepp->itemname = util_strdup(name);
1792 con_out("failed to create lexer for string \"%s\"\n", name);
1795 if (!ftepp_preprocess(ftepp))
1797 return ftepp_preprocess_done(ftepp);
1801 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1802 char *create = NULL;
1804 /* use saner path for empty macros */
1806 ftepp_add_define(ftepp, "__builtin__", name);
1810 vec_append(create, 8, "#define ");
1811 vec_append(create, strlen(name), name);
1812 vec_push (create, ' ');
1813 vec_append(create, strlen(value), value);
1814 vec_push (create, 0);
1816 ftepp_preprocess_string(ftepp, "__builtin__", create);
1820 ftepp_t *ftepp_create()
1826 ftepp = ftepp_new();
1830 memset(minor, 0, sizeof(minor));
1831 memset(major, 0, sizeof(major));
1833 /* set the right macro based on the selected standard */
1834 ftepp_add_define(ftepp, NULL, "GMQCC");
1835 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1836 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1845 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1846 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1847 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1848 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1849 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1850 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1851 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1852 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1853 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1854 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1865 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1866 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1869 * We're going to just make __NULL__ nil, which works for 60% of the
1870 * cases of __NULL_ for fteqcc.
1872 ftepp_add_macro(ftepp, "__NULL__", "nil");
1877 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1880 lex_ctx_t ctx = { "__builtin__", 0, 0 };
1882 macro = ppmacro_new(ctx, name);
1883 /*vec_push(ftepp->macros, macro);*/
1884 util_htset(ftepp->macros, name, macro);
1887 const char *ftepp_get(ftepp_t *ftepp)
1889 return ftepp->output_string;
1892 void ftepp_flush(ftepp_t *ftepp)
1894 ftepp_flush_do(ftepp);
1897 void ftepp_finish(ftepp_t *ftepp)
1901 ftepp_delete(ftepp);