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
37 /* a copy from the lexer */
51 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
64 ppcondition *conditions;
75 char *(*func)(lex_file *);
79 * Implement the predef subsystem now. We can do this safely with the
80 * help of lexer contexts.
82 static uint32_t ftepp_predef_countval = 0;
83 static uint32_t ftepp_predef_randval = 0;
86 char *ftepp_predef_date(lex_file *context) {
89 char *value = mem_a(82);
90 /* 82 is enough for strftime but we also have " " in our string */
96 itime = localtime(&rtime);
98 strftime(value, 82, "\"%b %d %Y\"", itime);
104 char *ftepp_predef_time(lex_file *context) {
107 char *value = mem_a(82);
108 /* 82 is enough for strftime but we also have " " in our string */
114 itime = localtime(&rtime);
116 strftime(value, 82, "\"%X\"", itime);
122 char *ftepp_predef_line(lex_file *context) {
124 util_asprintf(&value, "%d", (int)context->line);
128 char *ftepp_predef_file(lex_file *context) {
129 size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
130 char *value = (char*)mem_a(length);
131 memset (value, 0, length);
132 sprintf(value, "\"%s\"", context->name);
136 /* __COUNTER_LAST__ */
137 char *ftepp_predef_counterlast(lex_file *context) {
139 util_asprintf(&value, "%u", ftepp_predef_countval);
145 char *ftepp_predef_counter(lex_file *context) {
147 ftepp_predef_countval ++;
148 util_asprintf(&value, "%u", ftepp_predef_countval);
154 char *ftepp_predef_random(lex_file *context) {
156 ftepp_predef_randval = (util_rand() % 0xFF) + 1;
157 util_asprintf(&value, "%u", ftepp_predef_randval);
162 /* __RANDOM_LAST__ */
163 char *ftepp_predef_randomlast(lex_file *context) {
165 util_asprintf(&value, "%u", ftepp_predef_randval);
171 static const predef_t ftepp_predefs[] = {
172 { "__LINE__", &ftepp_predef_line },
173 { "__FILE__", &ftepp_predef_file },
174 { "__COUNTER__", &ftepp_predef_counter },
175 { "__COUNTER_LAST__", &ftepp_predef_counterlast },
176 { "__RANDOM__", &ftepp_predef_random },
177 { "__RANDOM_LAST__", &ftepp_predef_randomlast },
178 { "__DATE__", &ftepp_predef_date },
179 { "__TIME__", &ftepp_predef_time }
182 #define ftepp_tokval(f) ((f)->lex->tok.value)
183 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
185 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
192 con_cvprintmsg((void*)&ctx, LVL_ERROR, "error", fmt, ap);
196 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
203 con_cvprintmsg((void*)&ftepp->lex->tok.ctx, LVL_ERROR, "error", fmt, ap);
207 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
213 r = vcompile_warning(ftepp->lex->tok.ctx, warntype, fmt, ap);
218 static pptoken *pptoken_make(ftepp_t *ftepp)
220 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
221 token->token = ftepp->token;
223 if (token->token == TOKEN_WHITE)
224 token->value = util_strdup(" ");
227 token->value = util_strdup(ftepp_tokval(ftepp));
229 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
233 static void pptoken_delete(pptoken *self)
239 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
241 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
244 memset(macro, 0, sizeof(*macro));
245 macro->name = util_strdup(name);
249 static void ppmacro_delete(ppmacro *self)
252 for (i = 0; i < vec_size(self->params); ++i)
253 mem_d(self->params[i]);
254 vec_free(self->params);
255 for (i = 0; i < vec_size(self->output); ++i)
256 pptoken_delete(self->output[i]);
257 vec_free(self->output);
262 static ftepp_t* ftepp_new()
266 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
267 memset(ftepp, 0, sizeof(*ftepp));
269 ftepp->output_on = true;
274 static void ftepp_flush_do(ftepp_t *self)
276 vec_free(self->output_string);
279 static void ftepp_delete(ftepp_t *self)
282 ftepp_flush_do(self);
284 mem_d(self->itemname);
285 if (self->includename)
286 vec_free(self->includename);
287 for (i = 0; i < vec_size(self->macros); ++i)
288 ppmacro_delete(self->macros[i]);
289 vec_free(self->macros);
290 vec_free(self->conditions);
292 lex_close(self->lex);
296 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
298 if (ignore_cond || ftepp->output_on)
303 data = vec_add(ftepp->output_string, len);
304 memcpy(data, str, len);
308 static void ftepp_update_output_condition(ftepp_t *ftepp)
311 ftepp->output_on = true;
312 for (i = 0; i < vec_size(ftepp->conditions); ++i)
313 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
316 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
319 for (i = 0; i < vec_size(ftepp->macros); ++i) {
320 if (!strcmp(name, ftepp->macros[i]->name))
321 return ftepp->macros[i];
326 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
329 for (i = 0; i < vec_size(ftepp->macros); ++i) {
330 if (!strcmp(name, ftepp->macros[i]->name)) {
331 vec_remove(ftepp->macros, i, 1);
337 static GMQCC_INLINE int ftepp_next(ftepp_t *ftepp)
339 return (ftepp->token = lex_do(ftepp->lex));
342 /* Important: this does not skip newlines! */
343 static bool ftepp_skipspace(ftepp_t *ftepp)
345 if (ftepp->token != TOKEN_WHITE)
347 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
348 if (ftepp->token >= TOKEN_EOF) {
349 ftepp_error(ftepp, "unexpected end of preprocessor directive");
355 /* this one skips EOLs as well */
356 static bool ftepp_skipallwhite(ftepp_t *ftepp)
358 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
362 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
363 if (ftepp->token >= TOKEN_EOF) {
364 ftepp_error(ftepp, "unexpected end of preprocessor directive");
371 * The huge macro parsing code...
373 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
377 if (!ftepp_skipspace(ftepp))
379 if (ftepp->token == ')')
381 switch (ftepp->token) {
385 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
388 macro->variadic = true;
391 ftepp_error(ftepp, "unexpected token in parameter list");
395 if (!ftepp_skipspace(ftepp))
397 if (macro->variadic && ftepp->token != ')') {
398 ftepp_error(ftepp, "cannot have parameters after the variadic parameters");
401 } while (ftepp->token == ',');
402 if (ftepp->token != ')') {
403 ftepp_error(ftepp, "expected closing paren after macro parameter list");
407 /* skipspace happens in ftepp_define */
411 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
414 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
415 if (macro->variadic && !strcmp(ftepp_tokval(ftepp), "__VA_ARGS__"))
416 ftepp->token = TOKEN_VA_ARGS;
417 ptok = pptoken_make(ftepp);
418 vec_push(macro->output, ptok);
421 /* recursive expansion can cause EOFs here */
422 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
423 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
429 static bool ftepp_define(ftepp_t *ftepp)
432 size_t l = ftepp_ctx(ftepp).line;
434 (void)ftepp_next(ftepp);
435 if (!ftepp_skipspace(ftepp))
438 switch (ftepp->token) {
442 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
443 if (macro && ftepp->output_on) {
444 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
446 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
448 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
451 ftepp_error(ftepp, "expected macro name");
455 (void)ftepp_next(ftepp);
457 if (ftepp->token == '(') {
458 macro->has_params = true;
459 if (!ftepp_define_params(ftepp, macro))
463 if (!ftepp_skipspace(ftepp))
466 if (!ftepp_define_body(ftepp, macro))
469 if (ftepp->output_on)
470 vec_push(ftepp->macros, macro);
472 ppmacro_delete(macro);
475 for (; l < ftepp_ctx(ftepp).line; ++l)
476 ftepp_out(ftepp, "\n", true);
481 * When a macro is used we have to handle parameters as well
482 * as special-concatenation via ## or stringification via #
484 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
485 * this kind of parens. Curly braces or [] don't count towards the
492 static void macroparam_clean(macroparam *self)
495 for (i = 0; i < vec_size(self->tokens); ++i)
496 pptoken_delete(self->tokens[i]);
497 vec_free(self->tokens);
500 /* need to leave the last token up */
501 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
503 macroparam *params = NULL;
509 if (!ftepp_skipallwhite(ftepp))
511 while (ftepp->token != ')') {
513 if (!ftepp_skipallwhite(ftepp))
515 while (parens || ftepp->token != ',') {
516 if (ftepp->token == '(')
518 else if (ftepp->token == ')') {
523 ptok = pptoken_make(ftepp);
524 vec_push(mp.tokens, ptok);
525 if (ftepp_next(ftepp) >= TOKEN_EOF) {
526 ftepp_error(ftepp, "unexpected EOF in macro call");
530 vec_push(params, mp);
532 if (ftepp->token == ')')
534 if (ftepp->token != ',') {
535 ftepp_error(ftepp, "expected closing paren or comma in macro call");
538 if (ftepp_next(ftepp) >= TOKEN_EOF) {
539 ftepp_error(ftepp, "unexpected EOF in macro call");
543 /* need to leave that up
544 if (ftepp_next(ftepp) >= TOKEN_EOF) {
545 ftepp_error(ftepp, "unexpected EOF in macro call");
549 *out_params = params;
554 macroparam_clean(&mp);
555 for (i = 0; i < vec_size(params); ++i)
556 macroparam_clean(¶ms[i]);
561 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
564 for (i = 0; i < vec_size(macro->params); ++i) {
565 if (!strcmp(macro->params[i], name)) {
573 static void ftepp_stringify_token(ftepp_t *ftepp, pptoken *token)
578 switch (token->token) {
579 case TOKEN_STRINGCONST:
582 /* in preprocessor mode strings already are string,
583 * so we don't get actual newline bytes here.
584 * Still need to escape backslashes and quotes.
587 case '\\': ftepp_out(ftepp, "\\\\", false); break;
588 case '"': ftepp_out(ftepp, "\\\"", false); break;
591 ftepp_out(ftepp, chs, false);
598 ftepp_out(ftepp, " ", false);
601 ftepp_out(ftepp, "\\n", false);
604 ftepp_out(ftepp, token->value, false);
609 static void ftepp_stringify(ftepp_t *ftepp, macroparam *param)
612 ftepp_out(ftepp, "\"", false);
613 for (i = 0; i < vec_size(param->tokens); ++i)
614 ftepp_stringify_token(ftepp, param->tokens[i]);
615 ftepp_out(ftepp, "\"", false);
618 static void ftepp_recursion_header(ftepp_t *ftepp)
620 ftepp_out(ftepp, "\n#pragma push(line)\n", false);
623 static void ftepp_recursion_footer(ftepp_t *ftepp)
625 ftepp_out(ftepp, "\n#pragma pop(line)\n", false);
628 static void ftepp_param_out(ftepp_t *ftepp, macroparam *param)
632 for (i = 0; i < vec_size(param->tokens); ++i) {
633 out = param->tokens[i];
634 if (out->token == TOKEN_EOL)
635 ftepp_out(ftepp, "\n", false);
637 ftepp_out(ftepp, out->value, false);
641 static bool ftepp_preprocess(ftepp_t *ftepp);
642 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
644 char *old_string = ftepp->output_string;
645 lex_file *old_lexer = ftepp->lex;
646 size_t vararg_start = vec_size(macro->params);
655 if (vararg_start < vec_size(params))
656 varargs = vec_size(params) - vararg_start;
661 if (!vec_size(macro->output))
664 ftepp->output_string = NULL;
665 for (o = 0; o < vec_size(macro->output); ++o) {
666 pptoken *out = macro->output[o];
667 switch (out->token) {
669 if (!macro->variadic) {
670 ftepp_error(ftepp, "internal preprocessor error: TOKEN_VA_ARGS in non-variadic macro");
676 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
677 for (++pi; pi < varargs; ++pi) {
678 ftepp_out(ftepp, ", ", false);
679 ftepp_param_out(ftepp, ¶ms[pi + vararg_start]);
685 if (!macro_params_find(macro, out->value, &pi)) {
686 ftepp_out(ftepp, out->value, false);
689 ftepp_param_out(ftepp, ¶ms[pi]);
692 if (o + 1 < vec_size(macro->output)) {
693 nextok = macro->output[o+1]->token;
695 /* raw concatenation */
699 if ( (nextok == TOKEN_IDENT ||
700 nextok == TOKEN_KEYWORD ||
701 nextok == TOKEN_TYPENAME) &&
702 macro_params_find(macro, macro->output[o+1]->value, &pi))
705 ftepp_stringify(ftepp, ¶ms[pi]);
709 ftepp_out(ftepp, "#", false);
712 ftepp_out(ftepp, "\n", false);
715 ftepp_out(ftepp, out->value, false);
719 vec_push(ftepp->output_string, 0);
720 /* Now run the preprocessor recursively on this string buffer */
722 printf("__________\n%s\n=========\n", ftepp->output_string);
724 inlex = lex_open_string(ftepp->output_string, vec_size(ftepp->output_string)-1, ftepp->lex->name);
726 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
730 ftepp->output_string = old_string;
731 inlex->line = ftepp->lex->line;
732 inlex->sline = ftepp->lex->sline;
734 ftepp_recursion_header(ftepp);
735 if (!ftepp_preprocess(ftepp)) {
736 vec_free(ftepp->lex->open_string);
737 old_string = ftepp->output_string;
738 lex_close(ftepp->lex);
742 vec_free(ftepp->lex->open_string);
743 ftepp_recursion_footer(ftepp);
744 old_string = ftepp->output_string;
747 ftepp->lex = old_lexer;
748 ftepp->output_string = old_string;
752 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
755 macroparam *params = NULL;
758 if (!macro->has_params) {
759 if (!ftepp_macro_expand(ftepp, macro, NULL))
766 if (!ftepp_skipallwhite(ftepp))
769 if (ftepp->token != '(') {
770 ftepp_error(ftepp, "expected macro parameters in parenthesis");
775 if (!ftepp_macro_call_params(ftepp, ¶ms))
778 if ( vec_size(params) < vec_size(macro->params) ||
779 (vec_size(params) > vec_size(macro->params) && !macro->variadic) )
781 ftepp_error(ftepp, "macro %s expects%s %u paramteters, %u provided", macro->name,
782 (macro->variadic ? " at least" : ""),
783 (unsigned int)vec_size(macro->params),
784 (unsigned int)vec_size(params));
789 if (!ftepp_macro_expand(ftepp, macro, params))
794 for (o = 0; o < vec_size(params); ++o)
795 macroparam_clean(¶ms[o]);
801 * #if - the FTEQCC way:
802 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
803 * <numbers> => True if the number is not 0
804 * !<factor> => True if the factor yields false
805 * !!<factor> => ERROR on 2 or more unary nots
806 * <macro> => becomes the macro's FIRST token regardless of parameters
807 * <e> && <e> => True if both expressions are true
808 * <e> || <e> => True if either expression is true
810 * <ident> => False (remember for macros the <macro> rule applies instead)
811 * Unary + and - are weird and wrong in fteqcc so we don't allow them
812 * parenthesis in expressions are allowed
813 * parameter lists on macros are errors
814 * No mathematical calculations are executed
816 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out);
817 static bool ftepp_if_op(ftepp_t *ftepp)
819 ftepp->lex->flags.noops = false;
821 if (!ftepp_skipspace(ftepp))
823 ftepp->lex->flags.noops = true;
826 static bool ftepp_if_value(ftepp_t *ftepp, bool *out, double *value_out)
831 if (!ftepp_skipspace(ftepp))
834 while (ftepp->token == '!') {
837 if (!ftepp_skipspace(ftepp))
841 switch (ftepp->token) {
845 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
847 if (!ftepp_skipspace(ftepp))
849 if (ftepp->token != '(') {
850 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
854 if (!ftepp_skipspace(ftepp))
856 if (ftepp->token != TOKEN_IDENT &&
857 ftepp->token != TOKEN_TYPENAME &&
858 ftepp->token != TOKEN_KEYWORD)
860 ftepp_error(ftepp, "defined() used on an unexpected token type");
863 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
866 if (!ftepp_skipspace(ftepp))
868 if (ftepp->token != ')') {
869 ftepp_error(ftepp, "expected closing paren");
875 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
876 if (!macro || !vec_size(macro->output)) {
880 /* This does not expand recursively! */
881 switch (macro->output[0]->token) {
883 *value_out = macro->output[0]->constval.i;
884 *out = !!(macro->output[0]->constval.i);
886 case TOKEN_FLOATCONST:
887 *value_out = macro->output[0]->constval.f;
888 *out = !!(macro->output[0]->constval.f);
896 case TOKEN_STRINGCONST:
900 *value_out = ftepp->lex->tok.constval.i;
901 *out = !!(ftepp->lex->tok.constval.i);
903 case TOKEN_FLOATCONST:
904 *value_out = ftepp->lex->tok.constval.f;
905 *out = !!(ftepp->lex->tok.constval.f);
910 if (!ftepp_if_expr(ftepp, out, value_out))
912 if (ftepp->token != ')') {
913 ftepp_error(ftepp, "expected closing paren in #if expression");
919 ftepp_error(ftepp, "junk in #if: `%s` ...", ftepp_tokval(ftepp));
924 *value_out = (*out ? 1 : 0);
930 static bool ftepp_if_nextvalue(ftepp_t *ftepp, bool *out, double *value_out)
932 if (!ftepp_next(ftepp))
934 return ftepp_if_value(ftepp, out, value_out);
938 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out, double *value_out)
940 if (!ftepp_if_value(ftepp, out, value_out))
943 if (!ftepp_if_op(ftepp))
946 if (ftepp->token == ')' || ftepp->token != TOKEN_OPERATOR)
949 /* FTEQCC is all right-associative and no precedence here */
950 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
951 !strcmp(ftepp_tokval(ftepp), "||"))
954 char opc = ftepp_tokval(ftepp)[0];
958 if (!ftepp_next(ftepp))
960 if (!ftepp_if_expr(ftepp, &next, &nextvalue))
968 *value_out = (*out ? 1 : 0);
971 else if (!strcmp(ftepp_tokval(ftepp), "==") ||
972 !strcmp(ftepp_tokval(ftepp), "!=") ||
973 !strcmp(ftepp_tokval(ftepp), ">=") ||
974 !strcmp(ftepp_tokval(ftepp), "<=") ||
975 !strcmp(ftepp_tokval(ftepp), ">") ||
976 !strcmp(ftepp_tokval(ftepp), "<"))
979 const char opc0 = ftepp_tokval(ftepp)[0];
980 const char opc1 = ftepp_tokval(ftepp)[1];
983 if (!ftepp_next(ftepp))
985 if (!ftepp_if_expr(ftepp, &next, &other))
989 *out = (*value_out == other);
990 else if (opc0 == '!')
991 *out = (*value_out != other);
992 else if (opc0 == '>') {
993 if (opc1 == '=') *out = (*value_out >= other);
994 else *out = (*value_out > other);
996 else if (opc0 == '<') {
997 if (opc1 == '=') *out = (*value_out <= other);
998 else *out = (*value_out < other);
1000 *value_out = (*out ? 1 : 0);
1005 ftepp_error(ftepp, "junk after #if");
1010 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
1012 bool result = false;
1015 memset(cond, 0, sizeof(*cond));
1016 (void)ftepp_next(ftepp);
1018 if (!ftepp_skipspace(ftepp))
1020 if (ftepp->token == TOKEN_EOL) {
1021 ftepp_error(ftepp, "expected expression for #if-directive");
1025 if (!ftepp_if_expr(ftepp, &result, &dummy))
1033 * ifdef is rather simple
1035 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
1038 memset(cond, 0, sizeof(*cond));
1039 (void)ftepp_next(ftepp);
1040 if (!ftepp_skipspace(ftepp))
1043 switch (ftepp->token) {
1045 case TOKEN_TYPENAME:
1047 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1050 ftepp_error(ftepp, "expected macro name");
1054 (void)ftepp_next(ftepp);
1055 if (!ftepp_skipspace(ftepp))
1057 /* relaxing this condition
1058 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1059 ftepp_error(ftepp, "stray tokens after #ifdef");
1068 * undef is also simple
1070 static bool ftepp_undef(ftepp_t *ftepp)
1072 (void)ftepp_next(ftepp);
1073 if (!ftepp_skipspace(ftepp))
1076 if (ftepp->output_on) {
1077 switch (ftepp->token) {
1079 case TOKEN_TYPENAME:
1081 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
1084 ftepp_error(ftepp, "expected macro name");
1089 (void)ftepp_next(ftepp);
1090 if (!ftepp_skipspace(ftepp))
1092 /* relaxing this condition
1093 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
1094 ftepp_error(ftepp, "stray tokens after #ifdef");
1101 /* Special unescape-string function which skips a leading quote
1102 * and stops at a quote, not just at \0
1104 static void unescape(const char *str, char *out) {
1106 while (*str && *str != '"') {
1110 case '\\': *out++ = *str; break;
1111 case '"': *out++ = *str; break;
1112 case 'a': *out++ = '\a'; break;
1113 case 'b': *out++ = '\b'; break;
1114 case 'r': *out++ = '\r'; break;
1115 case 'n': *out++ = '\n'; break;
1116 case 't': *out++ = '\t'; break;
1117 case 'f': *out++ = '\f'; break;
1118 case 'v': *out++ = '\v'; break;
1133 static char *ftepp_include_find_path(const char *file, const char *pathfile)
1136 char *filename = NULL;
1137 const char *last_slash;
1143 last_slash = strrchr(pathfile, '/');
1146 len = last_slash - pathfile;
1147 memcpy(vec_add(filename, len), pathfile, len);
1148 vec_push(filename, '/');
1152 memcpy(vec_add(filename, len+1), file, len);
1153 vec_last(filename) = 0;
1155 fp = file_open(filename, "rb");
1164 static char *ftepp_include_find(ftepp_t *ftepp, const char *file)
1166 char *filename = NULL;
1168 filename = ftepp_include_find_path(file, ftepp->includename);
1170 filename = ftepp_include_find_path(file, ftepp->itemname);
1174 static bool ftepp_directive_warning(ftepp_t *ftepp) {
1175 char *message = NULL;
1177 if (!ftepp_skipspace(ftepp))
1180 /* handle the odd non string constant case so it works like C */
1181 if (ftepp->token != TOKEN_STRINGCONST) {
1183 vec_upload(message, "#warning", 8);
1185 while (ftepp->token != TOKEN_EOL) {
1186 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1189 vec_push(message, '\0');
1190 store = ftepp_warn(ftepp, WARN_CPP, message);
1195 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1196 return ftepp_warn(ftepp, WARN_CPP, "#warning %s", ftepp_tokval(ftepp));
1199 static void ftepp_directive_error(ftepp_t *ftepp) {
1200 char *message = NULL;
1202 if (!ftepp_skipspace(ftepp))
1205 /* handle the odd non string constant case so it works like C */
1206 if (ftepp->token != TOKEN_STRINGCONST) {
1207 vec_upload(message, "#error", 6);
1209 while (ftepp->token != TOKEN_EOL) {
1210 vec_upload(message, ftepp_tokval(ftepp), strlen(ftepp_tokval(ftepp)));
1213 vec_push(message, '\0');
1214 ftepp_error(ftepp, message);
1219 unescape (ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1220 ftepp_error(ftepp, "#error %s", ftepp_tokval(ftepp));
1225 * FIXME: do we need/want a -I option?
1226 * FIXME: what about when dealing with files in subdirectories coming from a progs.src?
1228 static bool ftepp_include(ftepp_t *ftepp)
1230 lex_file *old_lexer = ftepp->lex;
1235 char *old_includename;
1237 (void)ftepp_next(ftepp);
1238 if (!ftepp_skipspace(ftepp))
1241 if (ftepp->token != TOKEN_STRINGCONST) {
1242 ftepp_error(ftepp, "expected filename to include");
1246 ctx = ftepp_ctx(ftepp);
1248 unescape(ftepp_tokval(ftepp), ftepp_tokval(ftepp));
1250 ftepp_out(ftepp, "\n#pragma file(", false);
1251 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1252 ftepp_out(ftepp, ")\n#pragma line(1)\n", false);
1254 filename = ftepp_include_find(ftepp, ftepp_tokval(ftepp));
1256 ftepp_error(ftepp, "failed to open include file `%s`", ftepp_tokval(ftepp));
1259 inlex = lex_open(filename);
1261 ftepp_error(ftepp, "open failed on include file `%s`", filename);
1266 old_includename = ftepp->includename;
1267 ftepp->includename = filename;
1268 if (!ftepp_preprocess(ftepp)) {
1269 vec_free(ftepp->includename);
1270 ftepp->includename = old_includename;
1271 lex_close(ftepp->lex);
1272 ftepp->lex = old_lexer;
1275 vec_free(ftepp->includename);
1276 ftepp->includename = old_includename;
1277 lex_close(ftepp->lex);
1278 ftepp->lex = old_lexer;
1280 ftepp_out(ftepp, "\n#pragma file(", false);
1281 ftepp_out(ftepp, ctx.file, false);
1282 snprintf(lineno, sizeof(lineno), ")\n#pragma line(%lu)\n", (unsigned long)(ctx.line+1));
1283 ftepp_out(ftepp, lineno, false);
1286 (void)ftepp_next(ftepp);
1287 if (!ftepp_skipspace(ftepp))
1289 if (ftepp->token != TOKEN_EOL) {
1290 ftepp_error(ftepp, "stray tokens after #include");
1293 (void)ftepp_next(ftepp);
1298 /* Basic structure handlers */
1299 static bool ftepp_else_allowed(ftepp_t *ftepp)
1301 if (!vec_size(ftepp->conditions)) {
1302 ftepp_error(ftepp, "#else without #if");
1305 if (vec_last(ftepp->conditions).had_else) {
1306 ftepp_error(ftepp, "multiple #else for a single #if");
1312 static bool ftepp_hash(ftepp_t *ftepp)
1317 lex_ctx ctx = ftepp_ctx(ftepp);
1319 if (!ftepp_skipspace(ftepp))
1322 switch (ftepp->token) {
1325 case TOKEN_TYPENAME:
1326 if (!strcmp(ftepp_tokval(ftepp), "define")) {
1327 return ftepp_define(ftepp);
1329 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
1330 return ftepp_undef(ftepp);
1332 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
1333 if (!ftepp_ifdef(ftepp, &cond))
1335 cond.was_on = cond.on;
1336 vec_push(ftepp->conditions, cond);
1337 ftepp->output_on = ftepp->output_on && cond.on;
1340 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
1341 if (!ftepp_ifdef(ftepp, &cond))
1344 cond.was_on = cond.on;
1345 vec_push(ftepp->conditions, cond);
1346 ftepp->output_on = ftepp->output_on && cond.on;
1349 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
1350 if (!ftepp_else_allowed(ftepp))
1352 if (!ftepp_ifdef(ftepp, &cond))
1354 pc = &vec_last(ftepp->conditions);
1355 pc->on = !pc->was_on && cond.on;
1356 pc->was_on = pc->was_on || pc->on;
1357 ftepp_update_output_condition(ftepp);
1360 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
1361 if (!ftepp_else_allowed(ftepp))
1363 if (!ftepp_ifdef(ftepp, &cond))
1366 pc = &vec_last(ftepp->conditions);
1367 pc->on = !pc->was_on && cond.on;
1368 pc->was_on = pc->was_on || pc->on;
1369 ftepp_update_output_condition(ftepp);
1372 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
1373 if (!ftepp_else_allowed(ftepp))
1375 if (!ftepp_if(ftepp, &cond))
1377 pc = &vec_last(ftepp->conditions);
1378 pc->on = !pc->was_on && cond.on;
1379 pc->was_on = pc->was_on || pc->on;
1380 ftepp_update_output_condition(ftepp);
1383 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
1384 if (!ftepp_if(ftepp, &cond))
1386 cond.was_on = cond.on;
1387 vec_push(ftepp->conditions, cond);
1388 ftepp->output_on = ftepp->output_on && cond.on;
1391 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
1392 if (!ftepp_else_allowed(ftepp))
1394 pc = &vec_last(ftepp->conditions);
1395 pc->on = !pc->was_on;
1396 pc->had_else = true;
1398 ftepp_update_output_condition(ftepp);
1401 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
1402 if (!vec_size(ftepp->conditions)) {
1403 ftepp_error(ftepp, "#endif without #if");
1406 vec_pop(ftepp->conditions);
1408 ftepp_update_output_condition(ftepp);
1411 else if (!strcmp(ftepp_tokval(ftepp), "include")) {
1412 return ftepp_include(ftepp);
1414 else if (!strcmp(ftepp_tokval(ftepp), "pragma")) {
1415 ftepp_out(ftepp, "#", false);
1418 else if (!strcmp(ftepp_tokval(ftepp), "warning")) {
1419 ftepp_directive_warning(ftepp);
1422 else if (!strcmp(ftepp_tokval(ftepp), "error")) {
1423 ftepp_directive_error(ftepp);
1427 if (ftepp->output_on) {
1428 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
1435 /* break; never reached */
1437 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
1440 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
1443 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
1446 /* Builtins! Don't forget the builtins! */
1447 case TOKEN_INTCONST:
1448 case TOKEN_FLOATCONST:
1449 ftepp_out(ftepp, "#", false);
1452 if (!ftepp_skipspace(ftepp))
1457 static bool ftepp_preprocess(ftepp_t *ftepp)
1460 bool newline = true;
1463 char *expand = NULL;
1466 ftepp->lex->flags.preprocessing = true;
1467 ftepp->lex->flags.mergelines = false;
1468 ftepp->lex->flags.noops = true;
1473 if (ftepp->token >= TOKEN_EOF)
1479 switch (ftepp->token) {
1482 case TOKEN_TYPENAME:
1483 /* is it a predef? */
1484 if (OPTS_FLAG(FTEPP_PREDEFS)) {
1485 for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
1486 if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
1487 expand = ftepp_predefs[i].func(ftepp->lex);
1488 ftepp_out(ftepp, expand, false);
1489 ftepp_next(ftepp); /* skip */
1491 mem_d(expand); /* free memory */
1497 if (ftepp->output_on)
1498 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
1503 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1507 if (!ftepp_macro_call(ftepp, macro))
1508 ftepp->token = TOKEN_ERROR;
1512 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1516 ftepp->lex->flags.mergelines = true;
1517 if (ftepp_next(ftepp) >= TOKEN_EOF) {
1518 ftepp_error(ftepp, "error in preprocessor directive");
1519 ftepp->token = TOKEN_ERROR;
1522 if (!ftepp_hash(ftepp))
1523 ftepp->token = TOKEN_ERROR;
1524 ftepp->lex->flags.mergelines = false;
1528 ftepp_out(ftepp, "\n", true);
1532 /* same as default but don't set newline=false */
1533 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1538 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1542 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1544 /* force a 0 at the end but don't count it as added to the output */
1545 vec_push(ftepp->output_string, 0);
1546 vec_shrinkby(ftepp->output_string, 1);
1548 return (ftepp->token == TOKEN_EOF);
1551 /* Like in parser.c - files keep the previous state so we have one global
1552 * preprocessor. Except here we will want to warn about dangling #ifs.
1554 static ftepp_t *ftepp;
1556 static bool ftepp_preprocess_done()
1559 if (vec_size(ftepp->conditions)) {
1560 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1563 lex_close(ftepp->lex);
1565 if (ftepp->itemname) {
1566 mem_d(ftepp->itemname);
1567 ftepp->itemname = NULL;
1572 bool ftepp_preprocess_file(const char *filename)
1574 ftepp->lex = lex_open(filename);
1575 ftepp->itemname = util_strdup(filename);
1577 con_out("failed to open file \"%s\"\n", filename);
1580 if (!ftepp_preprocess(ftepp))
1582 return ftepp_preprocess_done();
1585 bool ftepp_preprocess_string(const char *name, const char *str)
1587 ftepp->lex = lex_open_string(str, strlen(str), name);
1588 ftepp->itemname = util_strdup(name);
1590 con_out("failed to create lexer for string \"%s\"\n", name);
1593 if (!ftepp_preprocess(ftepp))
1595 return ftepp_preprocess_done();
1599 void ftepp_add_macro(const char *name, const char *value) {
1600 char *create = NULL;
1602 /* use saner path for empty macros */
1604 ftepp_add_define("__builtin__", name);
1608 vec_upload(create, "#define ", 8);
1609 vec_upload(create, name, strlen(name));
1610 vec_push (create, ' ');
1611 vec_upload(create, value, strlen(value));
1612 vec_push (create, 0);
1614 ftepp_preprocess_string("__builtin__", create);
1623 ftepp = ftepp_new();
1627 memset(minor, 0, sizeof(minor));
1628 memset(major, 0, sizeof(major));
1630 /* set the right macro based on the selected standard */
1631 ftepp_add_define(NULL, "GMQCC");
1632 if (opts.standard == COMPILER_FTEQCC) {
1633 ftepp_add_define(NULL, "__STD_FTEQCC__");
1642 } else if (opts.standard == COMPILER_GMQCC) {
1643 ftepp_add_define(NULL, "__STD_GMQCC__");
1644 sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR);
1645 sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR);
1646 } else if (opts.standard == COMPILER_QCC) {
1647 ftepp_add_define(NULL, "__STD_QCC__");
1658 ftepp_add_macro("__STD_VERSION_MINOR__", minor);
1659 ftepp_add_macro("__STD_VERSION_MAJOR__", major);
1664 void ftepp_add_define(const char *source, const char *name)
1667 lex_ctx ctx = { "__builtin__", 0 };
1669 macro = ppmacro_new(ctx, name);
1670 vec_push(ftepp->macros, macro);
1673 const char *ftepp_get()
1675 return ftepp->output_string;
1680 ftepp_flush_do(ftepp);
1687 ftepp_delete(ftepp);