2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #define HT_MACROS 1024
38 /* a copy from the lexer */
52 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
59 typedef struct ftepp_s {
65 ppcondition *conditions;
67 ht macros; /* hashtable<string, ppmacro*> */
77 * Implement the predef subsystem now. We can do this safely with the
78 * help of lexer contexts.
80 static uint32_t ftepp_predef_countval = 0;
81 static uint32_t ftepp_predef_randval = 0;
84 char *ftepp_predef_date(lex_file *context) {
87 char *value = (char*)mem_a(82);
88 /* 82 is enough for strftime but we also have " " in our string */
94 itime = localtime(&rtime);
96 strftime(value, 82, "\"%b %d %Y\"", itime);
102 char *ftepp_predef_time(lex_file *context) {
105 char *value = (char*)mem_a(82);
106 /* 82 is enough for strftime but we also have " " in our string */
112 itime = localtime(&rtime);
114 strftime(value, 82, "\"%X\"", itime);
120 char *ftepp_predef_line(lex_file *context) {
122 util_asprintf(&value, "%d", (int)context->line);
126 char *ftepp_predef_file(lex_file *context) {
127 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
128 char *value = (char*)mem_a(length);
129 snprintf(value, length, "\"%s\"", context->name);
133 /* __COUNTER_LAST__ */
134 char *ftepp_predef_counterlast(lex_file *context) {
136 util_asprintf(&value, "%u", ftepp_predef_countval);
142 char *ftepp_predef_counter(lex_file *context) {
144 ftepp_predef_countval ++;
145 util_asprintf(&value, "%u", ftepp_predef_countval);
151 char *ftepp_predef_random(lex_file *context) {
153 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
154 util_asprintf(&value, "%u", ftepp_predef_randval);
159 /* __RANDOM_LAST__ */
160 char *ftepp_predef_randomlast(lex_file *context) {
162 util_asprintf(&value, "%u", ftepp_predef_randval);
168 const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = {
169 { "__LINE__", &ftepp_predef_line },
170 { "__FILE__", &ftepp_predef_file },
171 { "__COUNTER__", &ftepp_predef_counter },
172 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
173 { "__RANDOM__", &ftepp_predef_random },
174 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
175 { "__DATE__", &ftepp_predef_date },
176 { "__TIME__", &ftepp_predef_time }
179 #define ftepp_tokval(f) ((f)->lex->tok.value)
180 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
182 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
189 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
193 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
200 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
204 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
210 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
215 static pptoken *pptoken_make(ftepp_t *ftepp)
217 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
218 token->token = ftepp->token;
220 if (token->token == TOKEN_WHITE)
221 token->value = util_strdup(" ");
224 token->value = util_strdup(ftepp_tokval(ftepp));
226 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
230 static void pptoken_delete(pptoken *self)
236 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
238 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
241 memset(macro, 0, sizeof(*macro));
242 macro->name = util_strdup(name);
246 static void ppmacro_delete(ppmacro *self)
249 for (i = 0; i < vec_size(self->params); ++i)
250 mem_d(self->params[i]);
251 vec_free(self->params);
252 for (i = 0; i < vec_size(self->output); ++i)
253 pptoken_delete(self->output[i]);
254 vec_free(self->output);
259 static ftepp_t* ftepp_new()
263 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
264 memset(ftepp, 0, sizeof(*ftepp));
266 ftepp->macros = util_htnew(HT_MACROS);
267 ftepp->output_on = true;
272 static void ftepp_flush_do(ftepp_t *self)
274 vec_free(self->output_string);
277 static void ftepp_delete(ftepp_t *self)
279 ftepp_flush_do(self);
281 mem_d(self->itemname);
282 if (self->includename)
283 vec_free(self->includename);
286 for (i = 0; i < vec_size(self->macros); ++i)
287 ppmacro_delete(self->macros[i]);
289 vec_free(self->macros);
292 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
294 vec_free(self->conditions);
296 lex_close(self->lex);
300 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
302 if (ignore_cond || ftepp->output_on)
307 data = vec_add(ftepp->output_string, len);
308 memcpy(data, str, len);
312 static void ftepp_update_output_condition(ftepp_t *ftepp)
315 ftepp->output_on = true;
316 for (i = 0; i < vec_size(ftepp->conditions); ++i)
317 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
320 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
322 return util_htget(ftepp->macros, name);
325 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
327 util_htrm(ftepp->macros, name, NULL);
330 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
332 return (ftepp->token = lex_do(ftepp->lex));
335 /* Important: this does not skip newlines! */
336 static bool ftepp_skipspace(ftepp_t *ftepp)
338 if (ftepp->token != TOKEN_WHITE)
340 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
341 if (ftepp->token >= TOKEN_EOF) {
342 ftepp_error(ftepp, "unexpected end of preprocessor directive");
348 /* this one skips EOLs as well */
349 static bool ftepp_skipallwhite(ftepp_t *ftepp)
351 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
355 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
356 if (ftepp->token >= TOKEN_EOF) {
357 ftepp_error(ftepp, "unexpected end of preprocessor directive");
364 * The huge macro parsing code...
366 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
370 if (!ftepp_skipspace(ftepp))
372 if (ftepp->token == ')')
374 switch (ftepp->token) {
378 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
381 macro->variadic = true;
384 ftepp_error(ftepp, "unexpected token in parameter list");
388 if (!ftepp_skipspace(ftepp))
390 if (macro->variadic && ftepp->token != ')') {
391 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
394 } while (ftepp->token == ',');
396 if (ftepp->token != ')') {
397 ftepp_error(ftepp, "expected closing paren after macro parameter list");
401 /* skipspace happens in ftepp_define */
405 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
408 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
409 bool subscript = false;
411 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
412 subscript = !!(ftepp_next(ftepp) == '#');
414 if (subscript && ftepp_next(ftepp) != '#') {
415 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
417 } else if (subscript) {
418 if (ftepp_next(ftepp) == '[') {
419 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
420 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
424 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
426 if (ftepp_next(ftepp) != ']') {
427 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
432 * mark it as an array to be handled later as such and not
433 * as traditional __VA_ARGS__
435 ftepp->token = TOKEN_VA_ARGS_ARRAY;
436 ptok = pptoken_make(ftepp);
437 ptok->constval.i = index;
438 vec_push(macro->output, ptok);
441 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
445 int old = ftepp->token;
446 ftepp->token = TOKEN_VA_ARGS;
447 ptok = pptoken_make(ftepp);
448 vec_push(macro->output, ptok);
452 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
453 ftepp->token = TOKEN_VA_COUNT;
454 ptok = pptoken_make(ftepp);
455 vec_push(macro->output, ptok);
458 ptok = pptoken_make(ftepp);
459 vec_push(macro->output, ptok);
463 /* recursive expansion can cause EOFs here */
464 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
465 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
471 static bool ftepp_define(ftepp_t *ftepp)
473 ppmacro *macro = NULL;
474 size_t l = ftepp_ctx(ftepp).line;
476 (void)ftepp_next(ftepp);
477 if (!ftepp_skipspace(ftepp))
480 switch (ftepp->token) {
484 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
485 if (macro && ftepp->output_on) {
486 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
488 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
490 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
493 ftepp_error(ftepp, "expected macro name");
497 (void)ftepp_next(ftepp);
499 if (ftepp->token == '(') {
500 macro->has_params = true;
501 if (!ftepp_define_params(ftepp, macro)) {
502 ppmacro_delete(macro);
507 if (!ftepp_skipspace(ftepp)) {
508 ppmacro_delete(macro);
512 if (!ftepp_define_body(ftepp, macro)) {
513 ppmacro_delete(macro);
518 if (ftepp->output_on)
519 vec_push(ftepp->macros, macro);
521 if (ftepp->output_on)
522 util_htset(ftepp->macros, macro->name, (void*)macro);
524 ppmacro_delete(macro);
527 for (; l < ftepp_ctx(ftepp).line; ++l)
528 ftepp_out(ftepp, "\n", true);
533 * When a macro is used we have to handle parameters as well
534 * as special-concatenation via ## or stringification via #
536 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
537 * this kind of parens. Curly braces or [] don't count towards the
544 static void macroparam_clean(macroparam *self)
547 for (i = 0; i < vec_size(self->tokens); ++i)
548 pptoken_delete(self->tokens[i]);
549 vec_free(self->tokens);
552 /* need to leave the last token up */
553 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
555 macroparam *params = NULL;
561 if (!ftepp_skipallwhite(ftepp))
563 while (ftepp->token != ')') {
565 if (!ftepp_skipallwhite(ftepp))
567 while (parens || ftepp->token != ',') {
568 if (ftepp->token == '(')
570 else if (ftepp->token == ')') {
575 ptok = pptoken_make(ftepp);
576 vec_push(mp.tokens, ptok);
577 if (ftepp_next(ftepp) >= TOKEN_EOF) {
578 ftepp_error(ftepp, "unexpected EOF in macro call");
582 vec_push(params, mp);
584 if (ftepp->token == ')')
586 if (ftepp->token != ',') {
587 ftepp_error(ftepp, "expected closing paren or comma in macro call");
590 if (ftepp_next(ftepp) >= TOKEN_EOF) {
591 ftepp_error(ftepp, "unexpected EOF in macro call");
595 /* need to leave that up
596 if (ftepp_next(ftepp) >= TOKEN_EOF) {
597 ftepp_error(ftepp, "unexpected EOF in macro call");
601 *out_params = params;
606 macroparam_clean(&mp);
607 for (i = 0; i < vec_size(params); ++i)
608 macroparam_clean(¶ms[i]);
613 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
616 for (i = 0; i < vec_size(macro->params); ++i) {
617 if (!strcmp(macro->params[i], name)) {
625 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
630 switch (token->token) {
631 case TOKEN_STRINGCONST:
634 /* in preprocessor mode strings already are string,
635 * so we don't get actual newline bytes here.
636 * Still need to escape backslashes and quotes.
639 case '\\': ftepp_out(ftepp, "\\\\", false); break;
640 case '"': ftepp_out(ftepp, "\\\"", false); break;
643 ftepp_out(ftepp, chs, false);
650 ftepp_out(ftepp, " ", false);
653 ftepp_out(ftepp, "\\n", false);
656 ftepp_out(ftepp, token->value, false);
661 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
664 ftepp_out(ftepp, "\"", false);
665 for (i = 0; i < vec_size(param->tokens); ++i)
666 ftepp_stringify_token(ftepp, param->tokens[i]);
667 ftepp_out(ftepp, "\"", false);
670 static void ftepp_recursion_header(ftepp_t *ftepp)
672 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
675 static void ftepp_recursion_footer(ftepp_t *ftepp)
677 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
680 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
684 for (i = 0; i < vec_size(param->tokens); ++i) {
685 out = param->tokens[i];
686 if (out->token == TOKEN_EOL)
687 ftepp_out(ftepp, "\n", false);
689 ftepp_out(ftepp, out->value, false);
693 static bool ftepp_preprocess(ftepp_t *ftepp);
694 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
697 char *old_string = ftepp->output_string;
699 lex_file *old_lexer = ftepp->lex;
700 size_t vararg_start = vec_size(macro->params);
712 if (vararg_start < vec_size(params))
713 varargs = vec_size(params) - vararg_start;
718 if (!vec_size(macro->output))
721 ftepp->output_string = NULL;
722 for (o = 0; o < vec_size(macro->output); ++o) {
723 pptoken *out = macro->output[o];
724 switch (out->token) {
726 if (!macro->variadic) {
727 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
728 vec_free(old_string);
735 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
736 for (++pi; pi < varargs; ++pi) {
737 ftepp_out(ftepp, ", ", false);
738 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
742 case TOKEN_VA_ARGS_ARRAY:
743 if ((size_t)out->constval.i >= varargs) {
744 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
745 vec_free(old_string);
749 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
753 util_asprintf(&buffer, "%d", varargs);
754 ftepp_out(ftepp, buffer, false);
761 if (!macro_params_find(macro, out->value, &pi)) {
762 ftepp_out(ftepp, out->value, false);
765 ftepp_param_out(ftepp, ¶ms[pi]);
768 if (o + 1 < vec_size(macro->output)) {
769 nextok = macro->output[o+1]->token;
771 /* raw concatenation */
775 if ( (nextok == TOKEN_IDENT ||
776 nextok == TOKEN_KEYWORD ||
777 nextok == TOKEN_TYPENAME) &&
778 macro_params_find(macro, macro->output[o+1]->value, &pi))
781 ftepp_stringify(ftepp, ¶ms[pi]);
785 ftepp_out(ftepp, "#", false);
788 ftepp_out(ftepp, "\n", false);
791 ftepp_out(ftepp, out->value, false);
795 vec_push(ftepp->output_string, 0);
796 /* Now run the preprocessor recursively on this string buffer */
798 printf("__________\n%s\n=========\n", ftepp->output_string);
800 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
802 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
807 inlex->line = ftepp->lex->line;
808 inlex->sline = ftepp->lex->sline;
811 old_inmacro = ftepp->in_macro;
812 ftepp->in_macro = true;
813 ftepp->output_string = NULL;
814 if (!ftepp_preprocess(ftepp)) {
815 ftepp->in_macro = old_inmacro;
816 vec_free(ftepp->lex->open_string);
817 vec_free(ftepp->output_string);
818 lex_close(ftepp->lex);
822 ftepp->in_macro = old_inmacro;
823 vec_free(ftepp->lex->open_string);
824 lex_close(ftepp->lex);
826 inner_string = ftepp->output_string;
827 ftepp->output_string = old_string;
829 has_newlines = (strchr(inner_string, '\n') != NULL);
831 if (has_newlines && !old_inmacro)
832 ftepp_recursion_header(ftepp);
834 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
835 vec_free(inner_string);
837 if (has_newlines && !old_inmacro)
838 ftepp_recursion_footer(ftepp);
840 if (resetline && !ftepp->in_macro) {
842 snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
843 ftepp_out(ftepp, lineno, false);
846 old_string = ftepp->output_string;
848 ftepp->lex = old_lexer;
849 ftepp->output_string = old_string;
853 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
856 macroparam *params = NULL;
860 if (!macro->has_params) {
861 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
868 if (!ftepp_skipallwhite(ftepp))
871 if (ftepp->token != '(') {
872 ftepp_error(ftepp, "expected macro parameters in parenthesis");
877 paramline = ftepp->lex->sline;
878 if (!ftepp_macro_call_params(ftepp, ¶ms))
881 if ( vec_size(params) < vec_size(macro->params) ||
882 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
884 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
885 (macro->variadic ? " at least" : ""),
886 (unsigned int)vec_size(macro->params),
887 (unsigned int)vec_size(params));
892 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
897 for (o = 0; o < vec_size(params); ++o)
898 macroparam_clean(¶ms[o]);
904 * #if - the FTEQCC way:
905 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
906 * <numbers> => True if the number is not 0
907 * !<factor> => True if the factor yields false
908 * !!<factor> => ERROR on 2 or more unary nots
909 * <macro> => becomes the macro's FIRST token regardless of parameters
910 * <e> && <e> => True if both expressions are true
911 * <e> || <e> => True if either expression is true
913 * <ident> => False (remember for macros the <macro> rule applies instead)
914 * Unary + and - are weird and wrong in fteqcc so we don't allow them
915 * parenthesis in expressions are allowed
916 * parameter lists on macros are errors
917 * No mathematical calculations are executed
919 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
920 static bool ftepp_if_op(ftepp_t *ftepp)
922 ftepp->lex->flags.noops = false;
924 if (!ftepp_skipspace(ftepp))
926 ftepp->lex->flags.noops = true;
929 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
935 if (!ftepp_skipspace(ftepp))
938 while (ftepp->token == '!') {
941 if (!ftepp_skipspace(ftepp))
945 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
949 if (!ftepp_skipspace(ftepp))
953 switch (ftepp->token) {
957 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
959 if (!ftepp_skipspace(ftepp))
961 if (ftepp->token != '(') {
962 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
966 if (!ftepp_skipspace(ftepp))
968 if (ftepp->token != TOKEN_IDENT &&
969 ftepp->token != TOKEN_TYPENAME &&
970 ftepp->token != TOKEN_KEYWORD)
972 ftepp_error(ftepp, "defined() used on an unexpected token type");
975 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
978 if (!ftepp_skipspace(ftepp))
980 if (ftepp->token != ')') {
981 ftepp_error(ftepp, "expected closing paren");
987 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
988 if (!macro || !vec_size(macro->output)) {
992 /* This does not expand recursively! */
993 switch (macro->output[0]->token) {
995 *value_out = macro->output[0]->constval.i;
996 *out = !!(macro->output[0]->constval.i);
998 case TOKEN_FLOATCONST:
999 *value_out = macro->output[0]->constval.f;
1000 *out = !!(macro->output[0]->constval.f);
1008 case TOKEN_STRINGCONST:
1012 case TOKEN_INTCONST:
1013 *value_out = ftepp->lex->tok.constval.i;
1014 *out = !!(ftepp->lex->tok.constval.i);
1016 case TOKEN_FLOATCONST:
1017 *value_out = ftepp->lex->tok.constval.f;
1018 *out = !!(ftepp->lex->tok.constval.f);
1023 if (!ftepp_if_expr(ftepp, out, value_out))
1025 if (ftepp->token != ')') {
1026 ftepp_error(ftepp, "expected closing paren in #if expression");
1032 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1033 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1034 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1038 *value_out = -*value_out;
1041 *value_out = (*out ? 1 : 0);
1047 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1049 if (!ftepp_next(ftepp))
1051 return ftepp_if_value(ftepp, out, value_out);
1055 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1057 if (!ftepp_if_value(ftepp, out, value_out))
1060 if (!ftepp_if_op(ftepp))
1063 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1066 /* FTEQCC is all right-associative and no precedence here */
1067 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1068 !strcmp(ftepp_tokval(ftepp), "||"))
1071 char opc = ftepp_tokval(ftepp)[0];
1075 if (!ftepp_next(ftepp))
1077 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1081 *out = *out && next;
1083 *out = *out || next;
1085 *value_out = (*out ? 1 : 0);
1088 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1089 !strcmp(ftepp_tokval(ftepp), "!=") ||
1090 !strcmp(ftepp_tokval(ftepp), ">=") ||
1091 !strcmp(ftepp_tokval(ftepp), "<=") ||
1092 !strcmp(ftepp_tokval(ftepp), ">") ||
1093 !strcmp(ftepp_tokval(ftepp), "<"))
1096 const char opc0 = ftepp_tokval(ftepp)[0];
1097 const char opc1 = ftepp_tokval(ftepp)[1];
1100 if (!ftepp_next(ftepp))
1102 if (!ftepp_if_expr(ftepp, &next, &other))
1106 *out = (*value_out == other);
1107 else if (opc0 == '!')
1108 *out = (*value_out != other);
1109 else if (opc0 == '>') {
1110 if (opc1 == '=') *out = (*value_out >= other);
1111 else *out = (*value_out > other);
1113 else if (opc0 == '<') {
1114 if (opc1 == '=') *out = (*value_out <= other);
1115 else *out = (*value_out < other);
1117 *value_out = (*out ? 1 : 0);
1122 ftepp_error(ftepp, "junk after #if");
1127 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1129 bool result = false;
1132 memset(cond, 0, sizeof(*cond));
1133 (void)ftepp_next(ftepp);
1135 if (!ftepp_skipspace(ftepp))
1137 if (ftepp->token == TOKEN_EOL) {
1138 ftepp_error(ftepp, "expected expression for #if-directive");
1142 if (!ftepp_if_expr(ftepp, &result, &dummy))
1150 * ifdef is rather simple
1152 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1155 memset(cond, 0, sizeof(*cond));
1156 (void)ftepp_next(ftepp);
1157 if (!ftepp_skipspace(ftepp))
1160 switch (ftepp->token) {
1162 case TOKEN_TYPENAME:
1164 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1167 ftepp_error(ftepp, "expected macro name");
1171 (void)ftepp_next(ftepp);
1172 if (!ftepp_skipspace(ftepp))
1174 /* relaxing this condition
1175 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1176 ftepp_error(ftepp, "stray tokens after #ifdef");
1185 * undef is also simple
1187 static bool ftepp_undef(ftepp_t *ftepp)
1189 (void)ftepp_next(ftepp);
1190 if (!ftepp_skipspace(ftepp))
1193 if (ftepp->output_on) {
1194 switch (ftepp->token) {
1196 case TOKEN_TYPENAME:
1198 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1201 ftepp_error(ftepp, "expected macro name");
1206 (void)ftepp_next(ftepp);
1207 if (!ftepp_skipspace(ftepp))
1209 /* relaxing this condition
1210 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1211 ftepp_error(ftepp, "stray tokens after #ifdef");
1218 /* Special unescape-string function which skips a leading quote
1219 * and stops at a quote, not just at \0
1221 static void unescape(const char *str, char *out) {
1223 while (*str && *str != '"') {
1227 case '\\': *out++ = *str; break;
1228 case '"': *out++ = *str; break;
1229 case 'a': *out++ = '\a'; break;
1230 case 'b': *out++ = '\b'; break;
1231 case 'r': *out++ = '\r'; break;
1232 case 'n': *out++ = '\n'; break;
1233 case 't': *out++ = '\t'; break;
1234 case 'f': *out++ = '\f'; break;
1235 case 'v': *out++ = '\v'; break;
1250 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1253 char *filename = NULL;
1254 const char *last_slash;
1260 last_slash = strrchr(pathfile, '/');
1263 len = last_slash - pathfile;
1264 memcpy(vec_add(filename, len), pathfile, len);
1265 vec_push(filename, '/');
1269 memcpy(vec_add(filename, len+1), file, len);
1270 vec_last(filename) = 0;
1272 fp = fs_file_open(filename, "rb");
1281 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1283 char *filename = NULL;
1285 filename = ftepp_include_find_path(file, ftepp->includename);
1287 filename = ftepp_include_find_path(file, ftepp->itemname);
1291 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1292 char *message = NULL;
1294 if (!ftepp_skipspace(ftepp))
1297 /* handle the odd non string constant case so it works like C */
1298 if (ftepp->token != TOKEN_STRINGCONST) {
1300 vec_upload(message, "#warning", 8);
1302 while (ftepp->token != TOKEN_EOL) {
1303 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1306 vec_push(message, '\0');
1307 if (ftepp->output_on)
1308 store = ftepp_warn(ftepp, WARN_CPP, message);
1315 if (!ftepp->output_on)
1318 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1319 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1322 static void ftepp_directive_error(ftepp_t *ftepp) {
1323 char *message = NULL;
1325 if (!ftepp_skipspace(ftepp))
1328 /* handle the odd non string constant case so it works like C */
1329 if (ftepp->token != TOKEN_STRINGCONST) {
1330 vec_upload(message, "#error", 6);
1332 while (ftepp->token != TOKEN_EOL) {
1333 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1336 vec_push(message, '\0');
1337 if (ftepp->output_on)
1338 ftepp_error(ftepp, message);
1343 if (!ftepp->output_on)
1346 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1347 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1350 static void ftepp_directive_message(ftepp_t *ftepp) {
1351 char *message = NULL;
1353 if (!ftepp_skipspace(ftepp))
1356 /* handle the odd non string constant case so it works like C */
1357 if (ftepp->token != TOKEN_STRINGCONST) {
1358 vec_upload(message, "#message", 8);
1360 while (ftepp->token != TOKEN_EOL) {
1361 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1364 vec_push(message, '\0');
1365 if (ftepp->output_on)
1366 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1371 if (!ftepp->output_on)
1374 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1375 con_cprintmsg(&ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1380 * FIXME: do we need/want a -I option?
1381 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1383 static bool ftepp_include(ftepp_t *ftepp)
1385 lex_file *old_lexer = ftepp->lex;
1390 char *old_includename;
1392 (void)ftepp_next(ftepp);
1393 if (!ftepp_skipspace(ftepp))
1396 if (ftepp->token != TOKEN_STRINGCONST) {
1397 ftepp_error(ftepp, "expected filename to include");
1401 if (!ftepp->output_on) {
1406 ctx = ftepp_ctx(ftepp);
1408 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1410 ftepp_out(ftepp, "\n#pragma file(", false);
1411 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1412 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1414 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1416 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1419 inlex = lex_open(filename);
1421 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1426 old_includename = ftepp->includename;
1427 ftepp->includename = filename;
1428 if (!ftepp_preprocess(ftepp)) {
1429 vec_free(ftepp->includename);
1430 ftepp->includename = old_includename;
1431 lex_close(ftepp->lex);
1432 ftepp->lex = old_lexer;
1435 vec_free(ftepp->includename);
1436 ftepp->includename = old_includename;
1437 lex_close(ftepp->lex);
1438 ftepp->lex = old_lexer;
1440 ftepp_out(ftepp, "\n#pragma file(", false);
1441 ftepp_out(ftepp, ctx.file, false);
1442 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1443 ftepp_out(ftepp, lineno, false);
1446 (void)ftepp_next(ftepp);
1447 if (!ftepp_skipspace(ftepp))
1449 if (ftepp->token != TOKEN_EOL) {
1450 ftepp_error(ftepp, "stray tokens after #include");
1453 (void)ftepp_next(ftepp);
1458 /* Basic structure handlers */
1459 static bool ftepp_else_allowed(ftepp_t *ftepp)
1461 if (!vec_size(ftepp->conditions)) {
1462 ftepp_error(ftepp, "#else without #if");
1465 if (vec_last(ftepp->conditions).had_else) {
1466 ftepp_error(ftepp, "multiple #else for a single #if");
1472 static bool ftepp_hash(ftepp_t *ftepp)
1477 lex_ctx ctx = ftepp_ctx(ftepp);
1479 if (!ftepp_skipspace(ftepp))
1482 switch (ftepp->token) {
1485 case TOKEN_TYPENAME:
1486 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1487 return ftepp_define(ftepp);
1489 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1490 return ftepp_undef(ftepp);
1492 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1493 if (!ftepp_ifdef(ftepp, &cond))
1495 cond.was_on = cond.on;
1496 vec_push(ftepp->conditions, cond);
1497 ftepp->output_on = ftepp->output_on && cond.on;
1500 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1501 if (!ftepp_ifdef(ftepp, &cond))
1504 cond.was_on = cond.on;
1505 vec_push(ftepp->conditions, cond);
1506 ftepp->output_on = ftepp->output_on && cond.on;
1509 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1510 if (!ftepp_else_allowed(ftepp))
1512 if (!ftepp_ifdef(ftepp, &cond))
1514 pc = &vec_last(ftepp->conditions);
1515 pc->on = !pc->was_on && cond.on;
1516 pc->was_on = pc->was_on || pc->on;
1517 ftepp_update_output_condition(ftepp);
1520 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1521 if (!ftepp_else_allowed(ftepp))
1523 if (!ftepp_ifdef(ftepp, &cond))
1526 pc = &vec_last(ftepp->conditions);
1527 pc->on = !pc->was_on && cond.on;
1528 pc->was_on = pc->was_on || pc->on;
1529 ftepp_update_output_condition(ftepp);
1532 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1533 if (!ftepp_else_allowed(ftepp))
1535 if (!ftepp_if(ftepp, &cond))
1537 pc = &vec_last(ftepp->conditions);
1538 pc->on = !pc->was_on && cond.on;
1539 pc->was_on = pc->was_on || pc->on;
1540 ftepp_update_output_condition(ftepp);
1543 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1544 if (!ftepp_if(ftepp, &cond))
1546 cond.was_on = cond.on;
1547 vec_push(ftepp->conditions, cond);
1548 ftepp->output_on = ftepp->output_on && cond.on;
1551 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1552 if (!ftepp_else_allowed(ftepp))
1554 pc = &vec_last(ftepp->conditions);
1555 pc->on = !pc->was_on;
1556 pc->had_else = true;
1558 ftepp_update_output_condition(ftepp);
1561 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1562 if (!vec_size(ftepp->conditions)) {
1563 ftepp_error(ftepp, "#endif without #if");
1566 vec_pop(ftepp->conditions);
1568 ftepp_update_output_condition(ftepp);
1571 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1572 return ftepp_include(ftepp);
1574 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1575 ftepp_out(ftepp, "#", false);
1578 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1579 ftepp_directive_warning(ftepp);
1582 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1583 ftepp_directive_error(ftepp);
1586 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1587 ftepp_directive_message(ftepp);
1591 if (ftepp->output_on) {
1592 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1599 /* break; never reached */
1601 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1604 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1607 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1610 /* Builtins! Don't forget the builtins! */
1611 case TOKEN_INTCONST:
1612 case TOKEN_FLOATCONST:
1613 ftepp_out(ftepp, "#", false);
1616 if (!ftepp_skipspace(ftepp))
1621 static bool ftepp_preprocess(ftepp_t *ftepp)
1624 bool newline = true;
1627 char *expand = NULL;
1630 ftepp->lex->flags.preprocessing = true;
1631 ftepp->lex->flags.mergelines = false;
1632 ftepp->lex->flags.noops = true;
1637 if (ftepp->token >= TOKEN_EOF)
1643 switch (ftepp->token) {
1646 case TOKEN_TYPENAME:
1647 /* is it a predef? */
1648 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1649 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1650 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1651 expand = ftepp_predefs[i].func(ftepp->lex);
1652 ftepp_out(ftepp, expand, false);
1653 ftepp_next(ftepp); /* skip */
1655 mem_d(expand); /* free memory */
1661 if (ftepp->output_on)
1662 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1667 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1671 if (!ftepp_macro_call(ftepp, macro))
1672 ftepp->token = TOKEN_ERROR;
1676 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1680 ftepp->lex->flags.mergelines = true;
1681 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1682 ftepp_error(ftepp, "error in preprocessor directive");
1683 ftepp->token = TOKEN_ERROR;
1686 if (!ftepp_hash(ftepp))
1687 ftepp->token = TOKEN_ERROR;
1688 ftepp->lex->flags.mergelines = false;
1692 ftepp_out(ftepp, "\n", true);
1696 /* same as default but don't set newline=false */
1697 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1702 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1706 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1708 /* force a 0 at the end but don't count it as added to the output */
1709 vec_push(ftepp->output_string, 0);
1710 vec_shrinkby(ftepp->output_string, 1);
1712 return (ftepp->token == TOKEN_EOF);
1715 /* Like in parser.c - files keep the previous state so we have one global
1716 * preprocessor. Except here we will want to warn about dangling #ifs.
1718 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1721 if (vec_size(ftepp->conditions)) {
1722 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1725 lex_close(ftepp->lex);
1727 if (ftepp->itemname) {
1728 mem_d(ftepp->itemname);
1729 ftepp->itemname = NULL;
1734 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1736 ftepp->lex = lex_open(filename);
1737 ftepp->itemname = util_strdup(filename);
1739 con_out("failed to open file \"%s\"\n", filename);
1742 if (!ftepp_preprocess(ftepp))
1744 return ftepp_preprocess_done(ftepp);
1747 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1749 ftepp->lex = lex_open_string(str, strlen(str), name);
1750 ftepp->itemname = util_strdup(name);
1752 con_out("failed to create lexer for string \"%s\"\n", name);
1755 if (!ftepp_preprocess(ftepp))
1757 return ftepp_preprocess_done(ftepp);
1761 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1762 char *create = NULL;
1764 /* use saner path for empty macros */
1766 ftepp_add_define(ftepp, "__builtin__", name);
1770 vec_upload(create, "#define ", 8);
1771 vec_upload(create, name, strlen(name));
1772 vec_push (create, ' ');
1773 vec_upload(create, value, strlen(value));
1774 vec_push (create, 0);
1776 ftepp_preprocess_string(ftepp, "__builtin__", create);
1780 ftepp_t *ftepp_create()
1786 ftepp = ftepp_new();
1790 memset(minor, 0, sizeof(minor));
1791 memset(major, 0, sizeof(major));
1793 /* set the right macro based on the selected standard */
1794 ftepp_add_define(ftepp, NULL, "GMQCC");
1795 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1796 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1805 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1806 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1807 snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1808 snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1809 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1810 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1811 snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1812 snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1813 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1814 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1825 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1826 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1831 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1834 lex_ctx ctx = { "__builtin__", 0 };
1836 macro = ppmacro_new(ctx, name);
1837 /*vec_push(ftepp->macros, macro);*/
1838 util_htset(ftepp->macros, name, macro);
1841 const char *ftepp_get(ftepp_t *ftepp)
1843 return ftepp->output_string;
1846 void ftepp_flush(ftepp_t *ftepp)
1848 ftepp_flush_do(ftepp);
1851 void ftepp_finish(ftepp_t *ftepp)
1855 ftepp_delete(ftepp);