2 * Copyright (C) 2012, 2013
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is furnished to do
11 * so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #define HT_MACROS 1024
42 /* a copy from the lexer */
56 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
63 typedef struct ftepp_s {
69 ppcondition *conditions;
71 ht macros; /* hashtable<string, ppmacro*> */
80 * Implement the predef subsystem now. We can do this safely with the
81 * help of lexer contexts.
83 static uint32_t ftepp_predef_countval = 0;
84 static uint32_t ftepp_predef_randval = 0;
87 static char *ftepp_predef_date(lex_file *context) {
88 struct tm *itime = NULL;
90 char *value = (char*)mem_a(82);
91 /* 82 is enough for strftime but we also have " " in our string */
99 localtime_s(itime, &rtime);
101 itime = localtime(&rtime);
104 strftime(value, 82, "\"%b %d %Y\"", itime);
110 static char *ftepp_predef_time(lex_file *context) {
111 struct tm *itime = NULL;
113 char *value = (char*)mem_a(82);
114 /* 82 is enough for strftime but we also have " " in our string */
122 localtime_s(itime, &rtime);
124 itime = localtime(&rtime);
127 strftime(value, 82, "\"%X\"", itime);
133 static char *ftepp_predef_line(lex_file *context) {
135 util_asprintf(&value, "%d", (int)context->line);
139 static char *ftepp_predef_file(lex_file *context) {
140 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
141 char *value = (char*)mem_a(length);
142 util_snprintf(value, length, "\"%s\"", context->name);
146 /* __COUNTER_LAST__ */
147 static char *ftepp_predef_counterlast(lex_file *context) {
149 util_asprintf(&value, "%u", ftepp_predef_countval);
155 static char *ftepp_predef_counter(lex_file *context) {
157 ftepp_predef_countval ++;
158 util_asprintf(&value, "%u", ftepp_predef_countval);
164 static char *ftepp_predef_random(lex_file *context) {
166 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
167 util_asprintf(&value, "%u", ftepp_predef_randval);
172 /* __RANDOM_LAST__ */
173 static char *ftepp_predef_randomlast(lex_file *context) {
175 util_asprintf(&value, "%u", ftepp_predef_randval);
181 static char *ftepp_predef_timestamp(lex_file *context) {
189 if (stat(context->name, &finfo))
190 return util_strdup("\"<failed to determine timestamp>\"");
193 * ctime and its fucking annoying newline char, no worries, we're
194 * professionals here.
198 find = ctime(&finfo.st_mtime);
200 ctime_s(buffer, sizeof(buffer), &finfo.st_mtime);
204 value = (char*)mem_a(strlen(find) + 1);
205 memcpy(&value[1], find, (size = strlen(find)) - 1);
215 char *(*func)(lex_file *);
218 static const ftepp_predef_t ftepp_predefs[] = {
219 { "__LINE__", &ftepp_predef_line },
220 { "__FILE__", &ftepp_predef_file },
221 { "__COUNTER__", &ftepp_predef_counter },
222 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
223 { "__RANDOM__", &ftepp_predef_random },
224 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
225 { "__DATE__", &ftepp_predef_date },
226 { "__TIME__", &ftepp_predef_time },
227 { "__TIME_STAMP__", &ftepp_predef_timestamp }
230 static GMQCC_INLINE size_t ftepp_predef_index(const char *name) {
231 /* no hashtable here, we simply check for one to exist the naive way */
233 for(i = 1; i < GMQCC_ARRAY_COUNT(ftepp_predefs) + 1; i++)
234 if (!strcmp(ftepp_predefs[i-1].name, name))
239 bool ftepp_predef_exists(const char *name);
240 bool ftepp_predef_exists(const char *name) {
241 return ftepp_predef_index(name) != 0;
244 /* singleton because we're allowed */
245 static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) {
246 size_t i = ftepp_predef_index(name);
247 return (i != 0) ? ftepp_predefs[i-1].func : NULL;
250 #define ftepp_tokval(f) ((f)->lex->tok.value)
251 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
253 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx_t ctx, const char *fmt, ...)
260 con_cvprintmsg(ctx, LVL_ERROR, "error", fmt, ap);
264 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
271 con_cvprintmsg(ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
275 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
281 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
286 static pptoken *pptoken_make(ftepp_t *ftepp)
288 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
289 token->token = ftepp->token;
291 if (token->token == TOKEN_WHITE)
292 token->value = util_strdup(" ");
295 token->value = util_strdup(ftepp_tokval(ftepp));
297 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
301 static GMQCC_INLINE void pptoken_delete(pptoken *self)
307 static ppmacro *ppmacro_new(lex_ctx_t ctx, const char *name)
309 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
312 memset(macro, 0, sizeof(*macro));
313 macro->name = util_strdup(name);
317 static void ppmacro_delete(ppmacro *self)
320 for (i = 0; i < vec_size(self->params); ++i)
321 mem_d(self->params[i]);
322 vec_free(self->params);
323 for (i = 0; i < vec_size(self->output); ++i)
324 pptoken_delete(self->output[i]);
325 vec_free(self->output);
330 static ftepp_t* ftepp_new(void)
334 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
335 memset(ftepp, 0, sizeof(*ftepp));
337 ftepp->macros = util_htnew(HT_MACROS);
338 ftepp->output_on = true;
343 static GMQCC_INLINE void ftepp_flush_do(ftepp_t *self)
345 vec_free(self->output_string);
348 static void ftepp_delete(ftepp_t *self)
350 ftepp_flush_do(self);
352 mem_d(self->itemname);
353 if (self->includename)
354 vec_free(self->includename);
356 util_htrem(self->macros, (void (*)(void*))&ppmacro_delete);
358 vec_free(self->conditions);
360 lex_close(self->lex);
364 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
366 if (ignore_cond || ftepp->output_on)
371 data = vec_add(ftepp->output_string, len);
372 memcpy(data, str, len);
376 static GMQCC_INLINE void ftepp_update_output_condition(ftepp_t *ftepp)
379 ftepp->output_on = true;
380 for (i = 0; i < vec_size(ftepp->conditions); ++i)
381 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
384 static GMQCC_INLINE ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
386 return (ppmacro*)util_htget(ftepp->macros, name);
389 static GMQCC_INLINE void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
391 util_htrm(ftepp->macros, name, (void (*)(void*))&ppmacro_delete);
394 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
396 return (ftepp->token = lex_do(ftepp->lex));
399 /* Important: this does not skip newlines! */
400 static bool ftepp_skipspace(ftepp_t *ftepp)
402 if (ftepp->token != TOKEN_WHITE)
404 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
405 if (ftepp->token >= TOKEN_EOF) {
406 ftepp_error(ftepp, "unexpected end of preprocessor directive");
412 /* this one skips EOLs as well */
413 static bool ftepp_skipallwhite(ftepp_t *ftepp)
415 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
419 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
420 if (ftepp->token >= TOKEN_EOF) {
421 ftepp_error(ftepp, "unexpected end of preprocessor directive");
428 * The huge macro parsing code...
430 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
434 if (!ftepp_skipspace(ftepp))
436 if (ftepp->token == ')')
438 switch (ftepp->token) {
442 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
445 macro->variadic = true;
448 ftepp_error(ftepp, "unexpected token in parameter list");
452 if (!ftepp_skipspace(ftepp))
454 if (macro->variadic && ftepp->token != ')') {
455 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
458 } while (ftepp->token == ',');
460 if (ftepp->token != ')') {
461 ftepp_error(ftepp, "expected closing paren after macro parameter list");
465 /* skipspace happens in ftepp_define */
469 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
472 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
473 bool subscript = false;
475 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__")) {
476 subscript = !!(ftepp_next(ftepp) == '#');
478 if (subscript && ftepp_next(ftepp) != '#') {
479 ftepp_error(ftepp, "expected `##` in __VA_ARGS__ for subscripting");
481 } else if (subscript) {
482 if (ftepp_next(ftepp) == '[') {
483 if (ftepp_next(ftepp) != TOKEN_INTCONST) {
484 ftepp_error(ftepp, "expected index for __VA_ARGS__ subscript");
488 index = (int)strtol(ftepp_tokval(ftepp), NULL, 10);
490 if (ftepp_next(ftepp) != ']') {
491 ftepp_error(ftepp, "expected `]` in __VA_ARGS__ subscript");
496 * mark it as an array to be handled later as such and not
497 * as traditional __VA_ARGS__
499 ftepp->token = TOKEN_VA_ARGS_ARRAY;
500 ptok = pptoken_make(ftepp);
501 ptok->constval.i = index;
502 vec_push(macro->output, ptok);
505 ftepp_error(ftepp, "expected `[` for subscripting of __VA_ARGS__");
509 int old = ftepp->token;
510 ftepp->token = TOKEN_VA_ARGS;
511 ptok = pptoken_make(ftepp);
512 vec_push(macro->output, ptok);
516 else if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_COUNT__")) {
517 ftepp->token = TOKEN_VA_COUNT;
518 ptok = pptoken_make(ftepp);
519 vec_push(macro->output, ptok);
522 ptok = pptoken_make(ftepp);
523 vec_push(macro->output, ptok);
527 /* recursive expansion can cause EOFs here */
528 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
529 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
535 static bool ftepp_define(ftepp_t *ftepp)
537 ppmacro *macro = NULL;
538 size_t l = ftepp_ctx(ftepp).line;
540 (void)ftepp_next(ftepp);
541 if (!ftepp_skipspace(ftepp))
544 switch (ftepp->token) {
548 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
549 if (macro && ftepp->output_on) {
550 if (ftepp_warn(ftepp, WARN_CPP, "redefining `%s`", ftepp_tokval(ftepp)))
552 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
554 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
557 ftepp_error(ftepp, "expected macro name");
561 (void)ftepp_next(ftepp);
563 if (ftepp->token == '(') {
564 macro->has_params = true;
565 if (!ftepp_define_params(ftepp, macro)) {
566 ppmacro_delete(macro);
571 if (!ftepp_skipspace(ftepp)) {
572 ppmacro_delete(macro);
576 if (!ftepp_define_body(ftepp, macro)) {
577 ppmacro_delete(macro);
581 if (ftepp->output_on)
582 util_htset(ftepp->macros, macro->name, (void*)macro);
584 ppmacro_delete(macro);
587 for (; l < ftepp_ctx(ftepp).line; ++l)
588 ftepp_out(ftepp, "\n", true);
593 * When a macro is used we have to handle parameters as well
594 * as special-concatenation via ## or stringification via #
596 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
597 * this kind of parens. Curly braces or [] don't count towards the
604 static void macroparam_clean(macroparam *self)
607 for (i = 0; i < vec_size(self->tokens); ++i)
608 pptoken_delete(self->tokens[i]);
609 vec_free(self->tokens);
612 /* need to leave the last token up */
613 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
615 macroparam *params = NULL;
621 if (!ftepp_skipallwhite(ftepp))
623 while (ftepp->token != ')') {
625 if (!ftepp_skipallwhite(ftepp))
627 while (parens || ftepp->token != ',') {
628 if (ftepp->token == '(')
630 else if (ftepp->token == ')') {
635 ptok = pptoken_make(ftepp);
636 vec_push(mp.tokens, ptok);
637 if (ftepp_next(ftepp) >= TOKEN_EOF) {
638 ftepp_error(ftepp, "unexpected end of file in macro call");
642 vec_push(params, mp);
644 if (ftepp->token == ')')
646 if (ftepp->token != ',') {
647 ftepp_error(ftepp, "expected closing paren or comma in macro call");
650 if (ftepp_next(ftepp) >= TOKEN_EOF) {
651 ftepp_error(ftepp, "unexpected end of file in macro call");
655 *out_params = params;
660 macroparam_clean(&mp);
661 for (i = 0; i < vec_size(params); ++i)
662 macroparam_clean(¶ms[i]);
667 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
670 for (i = 0; i < vec_size(macro->params); ++i) {
671 if (!strcmp(macro->params[i], name)) {
679 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
684 switch (token->token) {
685 case TOKEN_STRINGCONST:
688 /* in preprocessor mode strings already are string,
689 * so we don't get actual newline bytes here.
690 * Still need to escape backslashes and quotes.
693 case '\\': ftepp_out(ftepp, "\\\\", false); break;
694 case '"': ftepp_out(ftepp, "\\\"", false); break;
697 ftepp_out(ftepp, chs, false);
704 ftepp_out(ftepp, " ", false);
707 ftepp_out(ftepp, "\\n", false);
710 ftepp_out(ftepp, token->value, false);
715 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
718 ftepp_out(ftepp, "\"", false);
719 for (i = 0; i < vec_size(param->tokens); ++i)
720 ftepp_stringify_token(ftepp, param->tokens[i]);
721 ftepp_out(ftepp, "\"", false);
724 static void ftepp_recursion_header(ftepp_t *ftepp)
726 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
729 static void ftepp_recursion_footer(ftepp_t *ftepp)
731 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
734 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
738 for (i = 0; i < vec_size(param->tokens); ++i) {
739 out = param->tokens[i];
740 if (out->token == TOKEN_EOL)
741 ftepp_out(ftepp, "\n", false);
743 ftepp_out(ftepp, out->value, false);
747 static bool ftepp_preprocess(ftepp_t *ftepp);
748 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params, bool resetline)
751 char *old_string = ftepp->output_string;
753 lex_file *old_lexer = ftepp->lex;
754 size_t vararg_start = vec_size(macro->params);
766 if (vararg_start < vec_size(params))
767 varargs = vec_size(params) - vararg_start;
772 if (!vec_size(macro->output))
775 ftepp->output_string = NULL;
776 for (o = 0; o < vec_size(macro->output); ++o) {
777 pptoken *out = macro->output[o];
778 switch (out->token) {
780 if (!macro->variadic) {
781 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
782 vec_free(old_string);
789 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
790 for (++pi; pi < varargs; ++pi) {
791 ftepp_out(ftepp, ", ", false);
792 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
796 case TOKEN_VA_ARGS_ARRAY:
797 if ((size_t)out->constval.i >= varargs) {
798 ftepp_error(ftepp, "subscript of `[%u]` is out of bounds for `__VA_ARGS__`", out->constval.i);
799 vec_free(old_string);
803 ftepp_param_out(ftepp, ¶ms[out->constval.i + vararg_start]);
807 util_asprintf(&buffer, "%d", varargs);
808 ftepp_out(ftepp, buffer, false);
815 if (!macro_params_find(macro, out->value, &pi)) {
816 ftepp_out(ftepp, out->value, false);
819 ftepp_param_out(ftepp, ¶ms[pi]);
822 if (o + 1 < vec_size(macro->output)) {
823 nextok = macro->output[o+1]->token;
825 /* raw concatenation */
829 if ( (nextok == TOKEN_IDENT ||
830 nextok == TOKEN_KEYWORD ||
831 nextok == TOKEN_TYPENAME) &&
832 macro_params_find(macro, macro->output[o+1]->value, &pi))
835 ftepp_stringify(ftepp, ¶ms[pi]);
839 ftepp_out(ftepp, "#", false);
842 ftepp_out(ftepp, "\n", false);
845 ftepp_out(ftepp, out->value, false);
849 vec_push(ftepp->output_string, 0);
850 /* Now run the preprocessor recursively on this string buffer */
852 printf("__________\n%s\n=========\n", ftepp->output_string);
854 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
856 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
861 inlex->line = ftepp->lex->line;
862 inlex->sline = ftepp->lex->sline;
865 old_inmacro = ftepp->in_macro;
866 ftepp->in_macro = true;
867 ftepp->output_string = NULL;
868 if (!ftepp_preprocess(ftepp)) {
869 ftepp->in_macro = old_inmacro;
870 vec_free(ftepp->lex->open_string);
871 vec_free(ftepp->output_string);
872 lex_close(ftepp->lex);
876 ftepp->in_macro = old_inmacro;
877 vec_free(ftepp->lex->open_string);
878 lex_close(ftepp->lex);
880 inner_string = ftepp->output_string;
881 ftepp->output_string = old_string;
883 has_newlines = (strchr(inner_string, '\n') != NULL);
885 if (has_newlines && !old_inmacro)
886 ftepp_recursion_header(ftepp);
888 vec_append(ftepp->output_string, vec_size(inner_string), inner_string);
889 vec_free(inner_string);
891 if (has_newlines && !old_inmacro)
892 ftepp_recursion_footer(ftepp);
894 if (resetline && !ftepp->in_macro) {
896 util_snprintf(lineno, 128, "\n#pragma line(%lu)\n", (unsigned long)(old_lexer->sline));
897 ftepp_out(ftepp, lineno, false);
900 old_string = ftepp->output_string;
902 ftepp->lex = old_lexer;
903 ftepp->output_string = old_string;
907 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
910 macroparam *params = NULL;
914 if (!macro->has_params) {
915 if (!ftepp_macro_expand(ftepp, macro, NULL, false))
922 if (!ftepp_skipallwhite(ftepp))
925 if (ftepp->token != '(') {
926 ftepp_error(ftepp, "expected macro parameters in parenthesis");
931 paramline = ftepp->lex->sline;
932 if (!ftepp_macro_call_params(ftepp, ¶ms))
935 if ( vec_size(params) < vec_size(macro->params) ||
936 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
938 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
939 (macro->variadic ? " at least" : ""),
940 (unsigned int)vec_size(macro->params),
941 (unsigned int)vec_size(params));
946 if (!ftepp_macro_expand(ftepp, macro, params, (paramline != ftepp->lex->sline)))
951 for (o = 0; o < vec_size(params); ++o)
952 macroparam_clean(¶ms[o]);
958 * #if - the FTEQCC way:
959 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
960 * <numbers> => True if the number is not 0
961 * !<factor> => True if the factor yields false
962 * !!<factor> => ERROR on 2 or more unary nots
963 * <macro> => becomes the macro's FIRST token regardless of parameters
964 * <e> && <e> => True if both expressions are true
965 * <e> || <e> => True if either expression is true
967 * <ident> => False (remember for macros the <macro> rule applies instead)
968 * Unary + and - are weird and wrong in fteqcc so we don't allow them
969 * parenthesis in expressions are allowed
970 * parameter lists on macros are errors
971 * No mathematical calculations are executed
973 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
974 static bool ftepp_if_op(ftepp_t *ftepp)
976 ftepp->lex->flags.noops = false;
978 if (!ftepp_skipspace(ftepp))
980 ftepp->lex->flags.noops = true;
983 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
989 if (!ftepp_skipspace(ftepp))
992 while (ftepp->token == '!') {
995 if (!ftepp_skipspace(ftepp))
999 if (ftepp->token == TOKEN_OPERATOR && !strcmp(ftepp_tokval(ftepp), "-"))
1003 if (!ftepp_skipspace(ftepp))
1007 switch (ftepp->token) {
1009 case TOKEN_TYPENAME:
1011 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
1013 if (!ftepp_skipspace(ftepp))
1015 if (ftepp->token != '(') {
1016 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
1020 if (!ftepp_skipspace(ftepp))
1022 if (ftepp->token != TOKEN_IDENT &&
1023 ftepp->token != TOKEN_TYPENAME &&
1024 ftepp->token != TOKEN_KEYWORD)
1026 ftepp_error(ftepp, "defined() used on an unexpected token type");
1029 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1032 if (!ftepp_skipspace(ftepp))
1034 if (ftepp->token != ')') {
1035 ftepp_error(ftepp, "expected closing paren");
1041 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1042 if (!macro || !vec_size(macro->output)) {
1046 /* This does not expand recursively! */
1047 switch (macro->output[0]->token) {
1048 case TOKEN_INTCONST:
1049 *value_out = macro->output[0]->constval.i;
1050 *out = !!(macro->output[0]->constval.i);
1052 case TOKEN_FLOATCONST:
1053 *value_out = macro->output[0]->constval.f;
1054 *out = !!(macro->output[0]->constval.f);
1062 case TOKEN_STRINGCONST:
1066 case TOKEN_INTCONST:
1067 *value_out = ftepp->lex->tok.constval.i;
1068 *out = !!(ftepp->lex->tok.constval.i);
1070 case TOKEN_FLOATCONST:
1071 *value_out = ftepp->lex->tok.constval.f;
1072 *out = !!(ftepp->lex->tok.constval.f);
1077 if (!ftepp_if_expr(ftepp, out, value_out))
1079 if (ftepp->token != ')') {
1080 ftepp_error(ftepp, "expected closing paren in #if expression");
1086 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
1087 if (OPTS_OPTION_BOOL(OPTION_DEBUG))
1088 ftepp_error(ftepp, "internal: token %i\n", ftepp->token);
1092 *value_out = -*value_out;
1095 *value_out = (*out ? 1 : 0);
1101 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
1103 if (!ftepp_next(ftepp))
1105 return ftepp_if_value(ftepp, out, value_out);
1109 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
1111 if (!ftepp_if_value(ftepp, out, value_out))
1114 if (!ftepp_if_op(ftepp))
1117 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
1120 /* FTEQCC is all right-associative and no precedence here */
1121 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
1122 !strcmp(ftepp_tokval(ftepp), "||"))
1125 char opc = ftepp_tokval(ftepp)[0];
1129 if (!ftepp_next(ftepp))
1131 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
1135 *out = *out && next;
1137 *out = *out || next;
1139 *value_out = (*out ? 1 : 0);
1142 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
1143 !strcmp(ftepp_tokval(ftepp), "!=") ||
1144 !strcmp(ftepp_tokval(ftepp), ">=") ||
1145 !strcmp(ftepp_tokval(ftepp), "<=") ||
1146 !strcmp(ftepp_tokval(ftepp), ">") ||
1147 !strcmp(ftepp_tokval(ftepp), "<"))
1150 const char opc0 = ftepp_tokval(ftepp)[0];
1151 const char opc1 = ftepp_tokval(ftepp)[1];
1154 if (!ftepp_next(ftepp))
1156 if (!ftepp_if_expr(ftepp, &next, &other))
1160 *out = (*value_out == other);
1161 else if (opc0 == '!')
1162 *out = (*value_out != other);
1163 else if (opc0 == '>') {
1164 if (opc1 == '=') *out = (*value_out >= other);
1165 else *out = (*value_out > other);
1167 else if (opc0 == '<') {
1168 if (opc1 == '=') *out = (*value_out <= other);
1169 else *out = (*value_out < other);
1171 *value_out = (*out ? 1 : 0);
1176 ftepp_error(ftepp, "junk after #if");
1181 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1183 bool result = false;
1186 memset(cond, 0, sizeof(*cond));
1187 (void)ftepp_next(ftepp);
1189 if (!ftepp_skipspace(ftepp))
1191 if (ftepp->token == TOKEN_EOL) {
1192 ftepp_error(ftepp, "expected expression for #if-directive");
1196 if (!ftepp_if_expr(ftepp, &result, &dummy))
1204 * ifdef is rather simple
1206 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1209 memset(cond, 0, sizeof(*cond));
1210 (void)ftepp_next(ftepp);
1211 if (!ftepp_skipspace(ftepp))
1214 switch (ftepp->token) {
1216 case TOKEN_TYPENAME:
1218 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1221 ftepp_error(ftepp, "expected macro name");
1225 (void)ftepp_next(ftepp);
1226 if (!ftepp_skipspace(ftepp))
1228 /* relaxing this condition
1229 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1230 ftepp_error(ftepp, "stray tokens after #ifdef");
1239 * undef is also simple
1241 static bool ftepp_undef(ftepp_t *ftepp)
1243 (void)ftepp_next(ftepp);
1244 if (!ftepp_skipspace(ftepp))
1247 if (ftepp->output_on) {
1248 switch (ftepp->token) {
1250 case TOKEN_TYPENAME:
1252 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1255 ftepp_error(ftepp, "expected macro name");
1260 (void)ftepp_next(ftepp);
1261 if (!ftepp_skipspace(ftepp))
1263 /* relaxing this condition
1264 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1265 ftepp_error(ftepp, "stray tokens after #ifdef");
1272 /* Special unescape-string function which skips a leading quote
1273 * and stops at a quote, not just at \0
1275 static void unescape(const char *str, char *out) {
1277 while (*str && *str != '"') {
1281 case '\\': *out++ = *str; break;
1282 case '"': *out++ = *str; break;
1283 case 'a': *out++ = '\a'; break;
1284 case 'b': *out++ = '\b'; break;
1285 case 'r': *out++ = '\r'; break;
1286 case 'n': *out++ = '\n'; break;
1287 case 't': *out++ = '\t'; break;
1288 case 'f': *out++ = '\f'; break;
1289 case 'v': *out++ = '\v'; break;
1304 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1307 char *filename = NULL;
1308 const char *last_slash;
1314 last_slash = strrchr(pathfile, '/');
1317 len = last_slash - pathfile;
1318 memcpy(vec_add(filename, len), pathfile, len);
1319 vec_push(filename, '/');
1323 memcpy(vec_add(filename, len+1), file, len);
1324 vec_last(filename) = 0;
1326 fp = fs_file_open(filename, "rb");
1335 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1337 char *filename = NULL;
1339 filename = ftepp_include_find_path(file, ftepp->includename);
1341 filename = ftepp_include_find_path(file, ftepp->itemname);
1345 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1346 char *message = NULL;
1348 if (!ftepp_skipspace(ftepp))
1351 /* handle the odd non string constant case so it works like C */
1352 if (ftepp->token != TOKEN_STRINGCONST) {
1354 vec_append(message, 8, "#warning");
1356 while (ftepp->token != TOKEN_EOL) {
1357 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1360 vec_push(message, '\0');
1361 if (ftepp->output_on)
1362 store = ftepp_warn(ftepp, WARN_CPP, message);
1369 if (!ftepp->output_on)
1372 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1373 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1376 static void ftepp_directive_error(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, 6, "#error");
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 ftepp_error(ftepp, message);
1397 if (!ftepp->output_on)
1400 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1401 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1404 static void ftepp_directive_message(ftepp_t *ftepp) {
1405 char *message = NULL;
1407 if (!ftepp_skipspace(ftepp))
1410 /* handle the odd non string constant case so it works like C */
1411 if (ftepp->token != TOKEN_STRINGCONST) {
1412 vec_append(message, 8, "#message");
1414 while (ftepp->token != TOKEN_EOL) {
1415 vec_append(message, strlen(ftepp_tokval(ftepp)), ftepp_tokval(ftepp));
1418 vec_push(message, '\0');
1419 if (ftepp->output_on)
1420 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", message);
1425 if (!ftepp->output_on)
1428 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1429 con_cprintmsg(ftepp->lex->tok.ctx, LVL_MSG, "message", ftepp_tokval(ftepp));
1434 * FIXME: do we need/want a -I option?
1435 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1437 static bool ftepp_include(ftepp_t *ftepp)
1439 lex_file *old_lexer = ftepp->lex;
1444 char *old_includename;
1446 (void)ftepp_next(ftepp);
1447 if (!ftepp_skipspace(ftepp))
1450 if (ftepp->token != TOKEN_STRINGCONST) {
1451 ftepp_error(ftepp, "expected filename to include");
1455 if (!ftepp->output_on) {
1460 ctx = ftepp_ctx(ftepp);
1462 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1464 ftepp_out(ftepp, "\n#pragma file(", false);
1465 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1466 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1468 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1470 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1473 inlex = lex_open(filename);
1475 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1480 old_includename = ftepp->includename;
1481 ftepp->includename = filename;
1482 if (!ftepp_preprocess(ftepp)) {
1483 vec_free(ftepp->includename);
1484 ftepp->includename = old_includename;
1485 lex_close(ftepp->lex);
1486 ftepp->lex = old_lexer;
1489 vec_free(ftepp->includename);
1490 ftepp->includename = old_includename;
1491 lex_close(ftepp->lex);
1492 ftepp->lex = old_lexer;
1494 ftepp_out(ftepp, "\n#pragma file(", false);
1495 ftepp_out(ftepp, ctx.file, false);
1496 util_snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1497 ftepp_out(ftepp, lineno, false);
1500 (void)ftepp_next(ftepp);
1501 if (!ftepp_skipspace(ftepp))
1503 if (ftepp->token != TOKEN_EOL) {
1504 ftepp_error(ftepp, "stray tokens after #include");
1507 (void)ftepp_next(ftepp);
1512 /* Basic structure handlers */
1513 static bool ftepp_else_allowed(ftepp_t *ftepp)
1515 if (!vec_size(ftepp->conditions)) {
1516 ftepp_error(ftepp, "#else without #if");
1519 if (vec_last(ftepp->conditions).had_else) {
1520 ftepp_error(ftepp, "multiple #else for a single #if");
1526 static bool ftepp_hash(ftepp_t *ftepp)
1531 lex_ctx_t ctx = ftepp_ctx(ftepp);
1533 if (!ftepp_skipspace(ftepp))
1536 switch (ftepp->token) {
1539 case TOKEN_TYPENAME:
1540 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1541 return ftepp_define(ftepp);
1543 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1544 return ftepp_undef(ftepp);
1546 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1547 if (!ftepp_ifdef(ftepp, &cond))
1549 cond.was_on = cond.on;
1550 vec_push(ftepp->conditions, cond);
1551 ftepp->output_on = ftepp->output_on && cond.on;
1554 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1555 if (!ftepp_ifdef(ftepp, &cond))
1558 cond.was_on = cond.on;
1559 vec_push(ftepp->conditions, cond);
1560 ftepp->output_on = ftepp->output_on && cond.on;
1563 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1564 if (!ftepp_else_allowed(ftepp))
1566 if (!ftepp_ifdef(ftepp, &cond))
1568 pc = &vec_last(ftepp->conditions);
1569 pc->on = !pc->was_on && cond.on;
1570 pc->was_on = pc->was_on || pc->on;
1571 ftepp_update_output_condition(ftepp);
1574 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1575 if (!ftepp_else_allowed(ftepp))
1577 if (!ftepp_ifdef(ftepp, &cond))
1580 pc = &vec_last(ftepp->conditions);
1581 pc->on = !pc->was_on && cond.on;
1582 pc->was_on = pc->was_on || pc->on;
1583 ftepp_update_output_condition(ftepp);
1586 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1587 if (!ftepp_else_allowed(ftepp))
1589 if (!ftepp_if(ftepp, &cond))
1591 pc = &vec_last(ftepp->conditions);
1592 pc->on = !pc->was_on && cond.on;
1593 pc->was_on = pc->was_on || pc->on;
1594 ftepp_update_output_condition(ftepp);
1597 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1598 if (!ftepp_if(ftepp, &cond))
1600 cond.was_on = cond.on;
1601 vec_push(ftepp->conditions, cond);
1602 ftepp->output_on = ftepp->output_on && cond.on;
1605 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1606 if (!ftepp_else_allowed(ftepp))
1608 pc = &vec_last(ftepp->conditions);
1609 pc->on = !pc->was_on;
1610 pc->had_else = true;
1612 ftepp_update_output_condition(ftepp);
1615 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1616 if (!vec_size(ftepp->conditions)) {
1617 ftepp_error(ftepp, "#endif without #if");
1620 vec_pop(ftepp->conditions);
1622 ftepp_update_output_condition(ftepp);
1625 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1626 return ftepp_include(ftepp);
1628 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1629 ftepp_out(ftepp, "#", false);
1632 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1633 ftepp_directive_warning(ftepp);
1636 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1637 ftepp_directive_error(ftepp);
1640 else if (!strcmp(ftepp_tokval(ftepp), "message")) {
1641 ftepp_directive_message(ftepp);
1645 if (ftepp->output_on) {
1646 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1653 /* break; never reached */
1655 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1658 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1661 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1664 /* Builtins! Don't forget the builtins! */
1665 case TOKEN_INTCONST:
1666 case TOKEN_FLOATCONST:
1667 ftepp_out(ftepp, "#", false);
1670 if (!ftepp_skipspace(ftepp))
1675 static bool ftepp_preprocess(ftepp_t *ftepp)
1678 bool newline = true;
1681 char *expand = NULL;
1683 ftepp->lex->flags.preprocessing = true;
1684 ftepp->lex->flags.mergelines = false;
1685 ftepp->lex->flags.noops = true;
1690 if (ftepp->token >= TOKEN_EOF)
1696 switch (ftepp->token) {
1699 case TOKEN_TYPENAME:
1700 /* is it a predef? */
1701 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1702 char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp));
1704 expand = predef(ftepp->lex);
1705 ftepp_out (ftepp, expand, false);
1713 if (ftepp->output_on)
1714 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1719 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1723 if (!ftepp_macro_call(ftepp, macro))
1724 ftepp->token = TOKEN_ERROR;
1728 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1732 ftepp->lex->flags.mergelines = true;
1733 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1734 ftepp_error(ftepp, "error in preprocessor directive");
1735 ftepp->token = TOKEN_ERROR;
1738 if (!ftepp_hash(ftepp))
1739 ftepp->token = TOKEN_ERROR;
1740 ftepp->lex->flags.mergelines = false;
1744 ftepp_out(ftepp, "\n", true);
1748 /* same as default but don't set newline=false */
1749 ftepp_out(ftepp, ftepp_tokval(ftepp), true);
1754 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1758 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1760 /* force a 0 at the end but don't count it as added to the output */
1761 vec_push(ftepp->output_string, 0);
1762 vec_shrinkby(ftepp->output_string, 1);
1764 return (ftepp->token == TOKEN_EOF);
1767 /* Like in parser.c - files keep the previous state so we have one global
1768 * preprocessor. Except here we will want to warn about dangling #ifs.
1770 static bool ftepp_preprocess_done(ftepp_t *ftepp)
1773 if (vec_size(ftepp->conditions)) {
1774 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1777 lex_close(ftepp->lex);
1779 if (ftepp->itemname) {
1780 mem_d(ftepp->itemname);
1781 ftepp->itemname = NULL;
1786 bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
1788 ftepp->lex = lex_open(filename);
1789 ftepp->itemname = util_strdup(filename);
1791 con_out("failed to open file \"%s\"\n", filename);
1794 if (!ftepp_preprocess(ftepp))
1796 return ftepp_preprocess_done(ftepp);
1799 bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
1801 ftepp->lex = lex_open_string(str, strlen(str), name);
1802 ftepp->itemname = util_strdup(name);
1804 con_out("failed to create lexer for string \"%s\"\n", name);
1807 if (!ftepp_preprocess(ftepp))
1809 return ftepp_preprocess_done(ftepp);
1813 void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
1814 char *create = NULL;
1816 /* use saner path for empty macros */
1818 ftepp_add_define(ftepp, "__builtin__", name);
1822 vec_append(create, 8, "#define ");
1823 vec_append(create, strlen(name), name);
1824 vec_push (create, ' ');
1825 vec_append(create, strlen(value), value);
1826 vec_push (create, 0);
1828 ftepp_preprocess_string(ftepp, "__builtin__", create);
1832 ftepp_t *ftepp_create()
1838 ftepp = ftepp_new();
1842 memset(minor, 0, sizeof(minor));
1843 memset(major, 0, sizeof(major));
1845 /* set the right macro based on the selected standard */
1846 ftepp_add_define(ftepp, NULL, "GMQCC");
1847 if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
1848 ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
1857 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
1858 ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
1859 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1860 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1861 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
1862 ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
1863 util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
1864 util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
1865 } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
1866 ftepp_add_define(ftepp, NULL, "__STD_QCC__");
1877 ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
1878 ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
1881 * We're going to just make __NULL__ nil, which works for 60% of the
1882 * cases of __NULL_ for fteqcc.
1884 ftepp_add_macro(ftepp, "__NULL__", "nil");
1889 void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
1892 lex_ctx_t ctx = { "__builtin__", 0, 0 };
1894 macro = ppmacro_new(ctx, name);
1895 /*vec_push(ftepp->macros, macro);*/
1896 util_htset(ftepp->macros, name, macro);
1899 const char *ftepp_get(ftepp_t *ftepp)
1901 return ftepp->output_string;
1904 void ftepp_flush(ftepp_t *ftepp)
1906 ftepp_flush_do(ftepp);
1909 void ftepp_finish(ftepp_t *ftepp)
1913 ftepp_delete(ftepp);