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
36 /* a copy from the lexer */
50 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
62 ppcondition *conditions;
73 char *(*func)(lex_file *);
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_line(lex_file *context) {
86 util_asprintf(&value, "%d", (int)context->line);
90 char *ftepp_predef_file(lex_file *context) {
91 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
92 char *value = (char*)mem_a(length);
93 memset (value, 0, length);
94 sprintf(value, "\"%s\"", context->name);
98 /* __COUNTER_LAST__ */
99 char *ftepp_predef_counterlast(lex_file *context) {
101 util_asprintf(&value, "%u", ftepp_predef_countval);
107 char *ftepp_predef_counter(lex_file *context) {
109 ftepp_predef_countval ++;
110 util_asprintf(&value, "%u", ftepp_predef_countval);
116 char *ftepp_predef_random(lex_file *context) {
118 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
119 util_asprintf(&value, "%u", ftepp_predef_randval);
124 /* __RANDOM_LAST__ */
125 char *ftepp_predef_randomlast(lex_file *context) {
127 util_asprintf(&value, "%u", ftepp_predef_randval);
133 static const predef_t ftepp_predefs[] = {
134 { "__LINE__", &ftepp_predef_line },
135 { "__FILE__", &ftepp_predef_file },
136 { "__COUNTER__", &ftepp_predef_counter },
137 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
138 { "__RANDOM__", &ftepp_predef_random },
139 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
142 #define ftepp_tokval(f) ((f)->lex->tok.value)
143 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
145 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
152 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
156 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
163 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
167 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
173 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
178 static pptoken *pptoken_make(ftepp_t *ftepp)
180 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
181 token->token = ftepp->token;
183 if (token->token == TOKEN_WHITE)
184 token->value = util_strdup(" ");
187 token->value = util_strdup(ftepp_tokval(ftepp));
189 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
193 static void pptoken_delete(pptoken *self)
199 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
201 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
204 memset(macro, 0, sizeof(*macro));
205 macro->name = util_strdup(name);
209 static void ppmacro_delete(ppmacro *self)
212 for (i = 0; i < vec_size(self->params); ++i)
213 mem_d(self->params[i]);
214 vec_free(self->params);
215 for (i = 0; i < vec_size(self->output); ++i)
216 pptoken_delete(self->output[i]);
217 vec_free(self->output);
222 static ftepp_t* ftepp_new()
226 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
227 memset(ftepp, 0, sizeof(*ftepp));
229 ftepp->output_on = true;
234 static void ftepp_delete(ftepp_t *self)
239 mem_d(self->itemname);
240 if (self->includename)
241 vec_free(self->includename);
242 for (i = 0; i < vec_size(self->macros); ++i)
243 ppmacro_delete(self->macros[i]);
244 vec_free(self->macros);
245 vec_free(self->conditions);
247 lex_close(self->lex);
251 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
253 if (ignore_cond || ftepp->output_on)
258 data = vec_add(ftepp->output_string, len);
259 memcpy(data, str, len);
263 static void ftepp_update_output_condition(ftepp_t *ftepp)
266 ftepp->output_on = true;
267 for (i = 0; i < vec_size(ftepp->conditions); ++i)
268 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
271 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
274 for (i = 0; i < vec_size(ftepp->macros); ++i) {
275 if (!strcmp(name, ftepp->macros[i]->name))
276 return ftepp->macros[i];
281 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
284 for (i = 0; i < vec_size(ftepp->macros); ++i) {
285 if (!strcmp(name, ftepp->macros[i]->name)) {
286 vec_remove(ftepp->macros, i, 1);
292 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
294 return (ftepp->token = lex_do(ftepp->lex));
297 /* Important: this does not skip newlines! */
298 static bool ftepp_skipspace(ftepp_t *ftepp)
300 if (ftepp->token != TOKEN_WHITE)
302 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
303 if (ftepp->token >= TOKEN_EOF) {
304 ftepp_error(ftepp, "unexpected end of preprocessor directive");
310 /* this one skips EOLs as well */
311 static bool ftepp_skipallwhite(ftepp_t *ftepp)
313 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
317 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
318 if (ftepp->token >= TOKEN_EOF) {
319 ftepp_error(ftepp, "unexpected end of preprocessor directive");
326 * The huge macro parsing code...
328 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
332 if (!ftepp_skipspace(ftepp))
334 if (ftepp->token == ')')
336 switch (ftepp->token) {
342 ftepp_error(ftepp, "unexpected token in parameter list");
345 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
347 if (!ftepp_skipspace(ftepp))
349 } while (ftepp->token == ',');
350 if (ftepp->token != ')') {
351 ftepp_error(ftepp, "expected closing paren after macro parameter list");
355 /* skipspace happens in ftepp_define */
359 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
362 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
363 ptok = pptoken_make(ftepp);
364 vec_push(macro->output, ptok);
367 /* recursive expansion can cause EOFs here */
368 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
369 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
375 static bool ftepp_define(ftepp_t *ftepp)
378 size_t l = ftepp_ctx(ftepp).line;
380 (void)ftepp_next(ftepp);
381 if (!ftepp_skipspace(ftepp))
384 switch (ftepp->token) {
388 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
389 if (macro && ftepp->output_on) {
390 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
392 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
394 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
397 ftepp_error(ftepp, "expected macro name");
401 (void)ftepp_next(ftepp);
403 if (ftepp->token == '(') {
404 macro->has_params = true;
405 if (!ftepp_define_params(ftepp, macro))
409 if (!ftepp_skipspace(ftepp))
412 if (!ftepp_define_body(ftepp, macro))
415 if (ftepp->output_on)
416 vec_push(ftepp->macros, macro);
418 ppmacro_delete(macro);
421 for (; l < ftepp_ctx(ftepp).line; ++l)
422 ftepp_out(ftepp, "\n", true);
427 * When a macro is used we have to handle parameters as well
428 * as special-concatenation via ## or stringification via #
430 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
431 * this kind of parens. Curly braces or [] don't count towards the
438 static void macroparam_clean(macroparam *self)
441 for (i = 0; i < vec_size(self->tokens); ++i)
442 pptoken_delete(self->tokens[i]);
443 vec_free(self->tokens);
446 /* need to leave the last token up */
447 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
449 macroparam *params = NULL;
455 if (!ftepp_skipallwhite(ftepp))
457 while (ftepp->token != ')') {
459 if (!ftepp_skipallwhite(ftepp))
461 while (parens || ftepp->token != ',') {
462 if (ftepp->token == '(')
464 else if (ftepp->token == ')') {
469 ptok = pptoken_make(ftepp);
470 vec_push(mp.tokens, ptok);
471 if (ftepp_next(ftepp) >= TOKEN_EOF) {
472 ftepp_error(ftepp, "unexpected EOF in macro call");
476 vec_push(params, mp);
478 if (ftepp->token == ')')
480 if (ftepp->token != ',') {
481 ftepp_error(ftepp, "expected closing paren or comma in macro call");
484 if (ftepp_next(ftepp) >= TOKEN_EOF) {
485 ftepp_error(ftepp, "unexpected EOF in macro call");
489 /* need to leave that up
490 if (ftepp_next(ftepp) >= TOKEN_EOF) {
491 ftepp_error(ftepp, "unexpected EOF in macro call");
495 *out_params = params;
500 macroparam_clean(&mp);
501 for (i = 0; i < vec_size(params); ++i)
502 macroparam_clean(¶ms[i]);
507 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
510 for (i = 0; i < vec_size(macro->params); ++i) {
511 if (!strcmp(macro->params[i], name)) {
519 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
524 switch (token->token) {
525 case TOKEN_STRINGCONST:
528 /* in preprocessor mode strings already are string,
529 * so we don't get actual newline bytes here.
530 * Still need to escape backslashes and quotes.
533 case '\\': ftepp_out(ftepp, "\\\\", false); break;
534 case '"': ftepp_out(ftepp, "\\\"", false); break;
537 ftepp_out(ftepp, chs, false);
544 ftepp_out(ftepp, " ", false);
547 ftepp_out(ftepp, "\\n", false);
550 ftepp_out(ftepp, token->value, false);
555 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
558 ftepp_out(ftepp, "\"", false);
559 for (i = 0; i < vec_size(param->tokens); ++i)
560 ftepp_stringify_token(ftepp, param->tokens[i]);
561 ftepp_out(ftepp, "\"", false);
564 static void ftepp_recursion_header(ftepp_t *ftepp)
566 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
569 static void ftepp_recursion_footer(ftepp_t *ftepp)
571 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
574 static bool ftepp_preprocess(ftepp_t *ftepp);
575 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
577 char *old_string = ftepp->output_string;
578 lex_file *old_lexer = ftepp->lex;
587 if (!vec_size(macro->output))
590 ftepp->output_string = NULL;
591 for (o = 0; o < vec_size(macro->output); ++o) {
592 pptoken *out = macro->output[o];
593 switch (out->token) {
597 if (!macro_params_find(macro, out->value, &pi)) {
598 ftepp_out(ftepp, out->value, false);
601 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
602 out = params[pi].tokens[pv];
603 if (out->token == TOKEN_EOL)
604 ftepp_out(ftepp, "\n", false);
606 ftepp_out(ftepp, out->value, false);
611 if (o + 1 < vec_size(macro->output)) {
612 nextok = macro->output[o+1]->token;
614 /* raw concatenation */
618 if ( (nextok == TOKEN_IDENT ||
619 nextok == TOKEN_KEYWORD ||
620 nextok == TOKEN_TYPENAME) &&
621 macro_params_find(macro, macro->output[o+1]->value, &pi))
624 ftepp_stringify(ftepp, ¶ms[pi]);
628 ftepp_out(ftepp, "#", false);
631 ftepp_out(ftepp, "\n", false);
634 ftepp_out(ftepp, out->value, false);
638 vec_push(ftepp->output_string, 0);
639 /* Now run the preprocessor recursively on this string buffer */
641 printf("__________\n%s\n=========\n", ftepp->output_string);
643 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
645 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
649 ftepp->output_string = old_string;
650 inlex->line = ftepp->lex->line;
651 inlex->sline = ftepp->lex->sline;
653 ftepp_recursion_header(ftepp);
654 if (!ftepp_preprocess(ftepp)) {
655 vec_free(ftepp->lex->open_string);
656 old_string = ftepp->output_string;
657 lex_close(ftepp->lex);
661 vec_free(ftepp->lex->open_string);
662 ftepp_recursion_footer(ftepp);
663 old_string = ftepp->output_string;
666 ftepp->lex = old_lexer;
667 ftepp->output_string = old_string;
671 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
674 macroparam *params = NULL;
677 if (!macro->has_params) {
678 if (!ftepp_macro_expand(ftepp, macro, NULL))
685 if (!ftepp_skipallwhite(ftepp))
688 if (ftepp->token != '(') {
689 ftepp_error(ftepp, "expected macro parameters in parenthesis");
694 if (!ftepp_macro_call_params(ftepp, ¶ms))
697 if (vec_size(params) != vec_size(macro->params)) {
698 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
699 (unsigned int)vec_size(macro->params),
700 (unsigned int)vec_size(params));
705 if (!ftepp_macro_expand(ftepp, macro, params))
710 for (o = 0; o < vec_size(params); ++o)
711 macroparam_clean(¶ms[o]);
717 * #if - the FTEQCC way:
718 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
719 * <numbers> => True if the number is not 0
720 * !<factor> => True if the factor yields false
721 * !!<factor> => ERROR on 2 or more unary nots
722 * <macro> => becomes the macro's FIRST token regardless of parameters
723 * <e> && <e> => True if both expressions are true
724 * <e> || <e> => True if either expression is true
726 * <ident> => False (remember for macros the <macro> rule applies instead)
727 * Unary + and - are weird and wrong in fteqcc so we don't allow them
728 * parenthesis in expressions are allowed
729 * parameter lists on macros are errors
730 * No mathematical calculations are executed
732 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
733 static bool ftepp_if_op(ftepp_t *ftepp)
735 ftepp->lex->flags.noops = false;
737 if (!ftepp_skipspace(ftepp))
739 ftepp->lex->flags.noops = true;
742 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
747 if (!ftepp_skipspace(ftepp))
750 while (ftepp->token == '!') {
753 if (!ftepp_skipspace(ftepp))
757 switch (ftepp->token) {
761 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
763 if (!ftepp_skipspace(ftepp))
765 if (ftepp->token != '(') {
766 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
770 if (!ftepp_skipspace(ftepp))
772 if (ftepp->token != TOKEN_IDENT &&
773 ftepp->token != TOKEN_TYPENAME &&
774 ftepp->token != TOKEN_KEYWORD)
776 ftepp_error(ftepp, "defined() used on an unexpected token type");
779 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
782 if (!ftepp_skipspace(ftepp))
784 if (ftepp->token != ')') {
785 ftepp_error(ftepp, "expected closing paren");
791 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
792 if (!macro || !vec_size(macro->output)) {
796 /* This does not expand recursively! */
797 switch (macro->output[0]->token) {
799 *value_out = macro->output[0]->constval.i;
800 *out = !!(macro->output[0]->constval.i);
802 case TOKEN_FLOATCONST:
803 *value_out = macro->output[0]->constval.f;
804 *out = !!(macro->output[0]->constval.f);
812 case TOKEN_STRINGCONST:
816 *value_out = ftepp->lex->tok.constval.i;
817 *out = !!(ftepp->lex->tok.constval.i);
819 case TOKEN_FLOATCONST:
820 *value_out = ftepp->lex->tok.constval.f;
821 *out = !!(ftepp->lex->tok.constval.f);
826 if (!ftepp_if_expr(ftepp, out, value_out))
828 if (ftepp->token != ')') {
829 ftepp_error(ftepp, "expected closing paren in #if expression");
835 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
840 *value_out = (*out ? 1 : 0);
846 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
848 if (!ftepp_next(ftepp))
850 return ftepp_if_value(ftepp, out, value_out);
854 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
856 if (!ftepp_if_value(ftepp, out, value_out))
859 if (!ftepp_if_op(ftepp))
862 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
865 /* FTEQCC is all right-associative and no precedence here */
866 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
867 !strcmp(ftepp_tokval(ftepp), "||"))
870 char opc = ftepp_tokval(ftepp)[0];
874 if (!ftepp_next(ftepp))
876 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
884 *value_out = (*out ? 1 : 0);
887 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
888 !strcmp(ftepp_tokval(ftepp), "!=") ||
889 !strcmp(ftepp_tokval(ftepp), ">=") ||
890 !strcmp(ftepp_tokval(ftepp), "<=") ||
891 !strcmp(ftepp_tokval(ftepp), ">") ||
892 !strcmp(ftepp_tokval(ftepp), "<"))
895 const char opc0 = ftepp_tokval(ftepp)[0];
896 const char opc1 = ftepp_tokval(ftepp)[1];
899 if (!ftepp_next(ftepp))
901 if (!ftepp_if_expr(ftepp, &next, &other))
905 *out = (*value_out == other);
906 else if (opc0 == '!')
907 *out = (*value_out != other);
908 else if (opc0 == '>') {
909 if (opc1 == '=') *out = (*value_out >= other);
910 else *out = (*value_out > other);
912 else if (opc0 == '<') {
913 if (opc1 == '=') *out = (*value_out <= other);
914 else *out = (*value_out < other);
916 *value_out = (*out ? 1 : 0);
921 ftepp_error(ftepp, "junk after #if");
926 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
931 memset(cond, 0, sizeof(*cond));
932 (void)ftepp_next(ftepp);
934 if (!ftepp_skipspace(ftepp))
936 if (ftepp->token == TOKEN_EOL) {
937 ftepp_error(ftepp, "expected expression for #if-directive");
941 if (!ftepp_if_expr(ftepp, &result, &dummy))
949 * ifdef is rather simple
951 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
954 memset(cond, 0, sizeof(*cond));
955 (void)ftepp_next(ftepp);
956 if (!ftepp_skipspace(ftepp))
959 switch (ftepp->token) {
963 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
966 ftepp_error(ftepp, "expected macro name");
970 (void)ftepp_next(ftepp);
971 if (!ftepp_skipspace(ftepp))
973 /* relaxing this condition
974 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
975 ftepp_error(ftepp, "stray tokens after #ifdef");
984 * undef is also simple
986 static bool ftepp_undef(ftepp_t *ftepp)
988 (void)ftepp_next(ftepp);
989 if (!ftepp_skipspace(ftepp))
992 if (ftepp->output_on) {
993 switch (ftepp->token) {
997 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1000 ftepp_error(ftepp, "expected macro name");
1005 (void)ftepp_next(ftepp);
1006 if (!ftepp_skipspace(ftepp))
1008 /* relaxing this condition
1009 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1010 ftepp_error(ftepp, "stray tokens after #ifdef");
1017 /* Special unescape-string function which skips a leading quote
1018 * and stops at a quote, not just at \0
1020 static void unescape(const char *str, char *out) {
1022 while (*str && *str != '"') {
1026 case '\\': *out++ = *str; break;
1027 case '"': *out++ = *str; break;
1028 case 'a': *out++ = '\a'; break;
1029 case 'b': *out++ = '\b'; break;
1030 case 'r': *out++ = '\r'; break;
1031 case 'n': *out++ = '\n'; break;
1032 case 't': *out++ = '\t'; break;
1033 case 'f': *out++ = '\f'; break;
1034 case 'v': *out++ = '\v'; break;
1049 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1052 char *filename = NULL;
1053 const char *last_slash;
1059 last_slash = strrchr(pathfile, '/');
1062 len = last_slash - pathfile;
1063 memcpy(vec_add(filename, len), pathfile, len);
1064 vec_push(filename, '/');
1068 memcpy(vec_add(filename, len+1), file, len);
1069 vec_last(filename) = 0;
1071 fp = file_open(filename, "rb");
1080 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1082 char *filename = NULL;
1084 filename = ftepp_include_find_path(file, ftepp->includename);
1086 filename = ftepp_include_find_path(file, ftepp->itemname);
1090 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1091 char *message = NULL;
1093 if (!ftepp_skipspace(ftepp))
1096 /* handle the odd non string constant case so it works like C */
1097 if (ftepp->token != TOKEN_STRINGCONST) {
1099 vec_upload(message, "#warning", 8);
1101 while (ftepp->token != TOKEN_EOL) {
1102 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1105 vec_push(message, '\0');
1106 store = ftepp_warn(ftepp, WARN_CPP, message);
1111 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1112 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1115 static void ftepp_directive_error(ftepp_t *ftepp) {
1116 char *message = NULL;
1118 if (!ftepp_skipspace(ftepp))
1121 /* handle the odd non string constant case so it works like C */
1122 if (ftepp->token != TOKEN_STRINGCONST) {
1123 vec_upload(message, "#error", 6);
1125 while (ftepp->token != TOKEN_EOL) {
1126 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1129 vec_push(message, '\0');
1130 ftepp_error(ftepp, message);
1135 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1136 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1141 * FIXME: do we need/want a -I option?
1142 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1144 static bool ftepp_include(ftepp_t *ftepp)
1146 lex_file *old_lexer = ftepp->lex;
1151 char *old_includename;
1153 (void)ftepp_next(ftepp);
1154 if (!ftepp_skipspace(ftepp))
1157 if (ftepp->token != TOKEN_STRINGCONST) {
1158 ftepp_error(ftepp, "expected filename to include");
1162 ctx = ftepp_ctx(ftepp);
1164 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1166 ftepp_out(ftepp, "\n#pragma file(", false);
1167 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1168 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1170 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1172 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1175 inlex = lex_open(filename);
1177 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1182 old_includename = ftepp->includename;
1183 ftepp->includename = filename;
1184 if (!ftepp_preprocess(ftepp)) {
1185 vec_free(ftepp->includename);
1186 ftepp->includename = old_includename;
1187 lex_close(ftepp->lex);
1188 ftepp->lex = old_lexer;
1191 vec_free(ftepp->includename);
1192 ftepp->includename = old_includename;
1193 lex_close(ftepp->lex);
1194 ftepp->lex = old_lexer;
1196 ftepp_out(ftepp, "\n#pragma file(", false);
1197 ftepp_out(ftepp, ctx.file, false);
1198 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1199 ftepp_out(ftepp, lineno, false);
1202 (void)ftepp_next(ftepp);
1203 if (!ftepp_skipspace(ftepp))
1205 if (ftepp->token != TOKEN_EOL) {
1206 ftepp_error(ftepp, "stray tokens after #include");
1209 (void)ftepp_next(ftepp);
1214 /* Basic structure handlers */
1215 static bool ftepp_else_allowed(ftepp_t *ftepp)
1217 if (!vec_size(ftepp->conditions)) {
1218 ftepp_error(ftepp, "#else without #if");
1221 if (vec_last(ftepp->conditions).had_else) {
1222 ftepp_error(ftepp, "multiple #else for a single #if");
1228 static bool ftepp_hash(ftepp_t *ftepp)
1233 lex_ctx ctx = ftepp_ctx(ftepp);
1235 if (!ftepp_skipspace(ftepp))
1238 switch (ftepp->token) {
1241 case TOKEN_TYPENAME:
1242 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1243 return ftepp_define(ftepp);
1245 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1246 return ftepp_undef(ftepp);
1248 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1249 if (!ftepp_ifdef(ftepp, &cond))
1251 cond.was_on = cond.on;
1252 vec_push(ftepp->conditions, cond);
1253 ftepp->output_on = ftepp->output_on && cond.on;
1256 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1257 if (!ftepp_ifdef(ftepp, &cond))
1260 cond.was_on = cond.on;
1261 vec_push(ftepp->conditions, cond);
1262 ftepp->output_on = ftepp->output_on && cond.on;
1265 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1266 if (!ftepp_else_allowed(ftepp))
1268 if (!ftepp_ifdef(ftepp, &cond))
1270 pc = &vec_last(ftepp->conditions);
1271 pc->on = !pc->was_on && cond.on;
1272 pc->was_on = pc->was_on || pc->on;
1273 ftepp_update_output_condition(ftepp);
1276 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1277 if (!ftepp_else_allowed(ftepp))
1279 if (!ftepp_ifdef(ftepp, &cond))
1282 pc = &vec_last(ftepp->conditions);
1283 pc->on = !pc->was_on && cond.on;
1284 pc->was_on = pc->was_on || pc->on;
1285 ftepp_update_output_condition(ftepp);
1288 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1289 if (!ftepp_else_allowed(ftepp))
1291 if (!ftepp_if(ftepp, &cond))
1293 pc = &vec_last(ftepp->conditions);
1294 pc->on = !pc->was_on && cond.on;
1295 pc->was_on = pc->was_on || pc->on;
1296 ftepp_update_output_condition(ftepp);
1299 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1300 if (!ftepp_if(ftepp, &cond))
1302 cond.was_on = cond.on;
1303 vec_push(ftepp->conditions, cond);
1304 ftepp->output_on = ftepp->output_on && cond.on;
1307 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1308 if (!ftepp_else_allowed(ftepp))
1310 pc = &vec_last(ftepp->conditions);
1311 pc->on = !pc->was_on;
1312 pc->had_else = true;
1314 ftepp_update_output_condition(ftepp);
1317 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1318 if (!vec_size(ftepp->conditions)) {
1319 ftepp_error(ftepp, "#endif without #if");
1322 vec_pop(ftepp->conditions);
1324 ftepp_update_output_condition(ftepp);
1327 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1328 return ftepp_include(ftepp);
1330 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1331 ftepp_out(ftepp, "#", false);
1334 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1335 ftepp_directive_warning(ftepp);
1338 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1339 ftepp_directive_error(ftepp);
1343 if (ftepp->output_on) {
1344 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1351 /* break; never reached */
1353 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1356 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1359 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1362 /* Builtins! Don't forget the builtins! */
1363 case TOKEN_INTCONST:
1364 case TOKEN_FLOATCONST:
1365 ftepp_out(ftepp, "#", false);
1368 if (!ftepp_skipspace(ftepp))
1373 static bool ftepp_preprocess(ftepp_t *ftepp)
1376 bool newline = true;
1379 char *expand = NULL;
1382 ftepp->lex->flags.preprocessing = true;
1383 ftepp->lex->flags.mergelines = false;
1384 ftepp->lex->flags.noops = true;
1389 if (ftepp->token >= TOKEN_EOF)
1395 switch (ftepp->token) {
1398 case TOKEN_TYPENAME:
1399 /* is it a predef? */
1400 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1401 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1402 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1403 expand = ftepp_predefs[i].func(ftepp->lex);
1404 ftepp_out(ftepp, expand, false);
1405 ftepp_next(ftepp); /* skip */
1407 mem_d(expand); /* free memory */
1413 if (ftepp->output_on)
1414 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1419 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1423 if (!ftepp_macro_call(ftepp, macro))
1424 ftepp->token = TOKEN_ERROR;
1428 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1432 ftepp->lex->flags.mergelines = true;
1433 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1434 ftepp_error(ftepp, "error in preprocessor directive");
1435 ftepp->token = TOKEN_ERROR;
1438 if (!ftepp_hash(ftepp))
1439 ftepp->token = TOKEN_ERROR;
1440 ftepp->lex->flags.mergelines = false;
1444 ftepp_out(ftepp, "\n", true);
1448 /* same as default but don't set newline=false */
1449 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1454 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1458 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1460 /* force a 0 at the end but don't count it as added to the output */
1461 vec_push(ftepp->output_string, 0);
1462 vec_shrinkby(ftepp->output_string, 1);
1464 return (ftepp->token == TOKEN_EOF);
1467 /* Like in parser.c - files keep the previous state so we have one global
1468 * preprocessor. Except here we will want to warn about dangling #ifs.
1470 static ftepp_t *ftepp;
1472 static bool ftepp_preprocess_done()
1475 if (vec_size(ftepp->conditions)) {
1476 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1479 lex_close(ftepp->lex);
1481 if (ftepp->itemname) {
1482 mem_d(ftepp->itemname);
1483 ftepp->itemname = NULL;
1488 bool ftepp_preprocess_file(const char *filename)
1490 ftepp->lex = lex_open(filename);
1491 ftepp->itemname = util_strdup(filename);
1493 con_out("failed to open file \"%s\"\n", filename);
1496 if (!ftepp_preprocess(ftepp))
1498 return ftepp_preprocess_done();
1501 bool ftepp_preprocess_string(const char *name, const char *str)
1503 ftepp->lex = lex_open_string(str, strlen(str), name);
1504 ftepp->itemname = util_strdup(name);
1506 con_out("failed to create lexer for string \"%s\"\n", name);
1509 if (!ftepp_preprocess(ftepp))
1511 return ftepp_preprocess_done();
1515 void ftepp_add_macro(const char *name, const char *value) {
1516 char *create = NULL;
1518 /* use saner path for empty macros */
1520 ftepp_add_define("__builtin__", name);
1524 vec_upload(create, "#define ", 8);
1525 vec_upload(create, name, strlen(name));
1526 vec_push (create, ' ');
1527 vec_upload(create, value, strlen(value));
1528 vec_push (create, 0);
1530 ftepp_preprocess_string("__builtin__", create);
1539 ftepp = ftepp_new();
1543 memset(minor, 0, sizeof(minor));
1544 memset(major, 0, sizeof(major));
1546 /* set the right macro based on the selected standard */
1547 ftepp_add_define(NULL, "GMQCC");
1548 if (opts.standard == COMPILER_FTEQCC) {
1549 ftepp_add_define(NULL, "__STD_FTEQCC__");
1558 } else if (opts.standard == COMPILER_GMQCC) {
1559 ftepp_add_define(NULL, "__STD_GMQCC__");
1560 sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR);
1561 sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR);
1562 } else if (opts.standard == COMPILER_QCC) {
1563 ftepp_add_define(NULL, "__STD_QCC__");
1574 ftepp_add_macro("__STD_VERSION_MINOR__", minor);
1575 ftepp_add_macro("__STD_VERSION_MAJOR__", major);
1580 void ftepp_add_define(const char *source, const char *name)
1583 lex_ctx ctx = { "__builtin__", 0 };
1585 macro = ppmacro_new(ctx, name);
1586 vec_push(ftepp->macros, macro);
1589 const char *ftepp_get()
1591 return ftepp->output_string;
1596 vec_free(ftepp->output_string);
1603 ftepp_delete(ftepp);