5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 /* a copy from the lexer */
49 /* yes we need an extra flag since `#define FOO x` is not the same as `#define FOO() x` */
62 ppcondition *conditions;
69 #define ftepp_tokval(f) ((f)->lex->tok.value)
70 #define ftepp_ctx(f) ((f)->lex->tok.ctx)
72 static void ftepp_errorat(ftepp_t *ftepp, lex_ctx ctx, const char *fmt, ...)
79 con_vprintmsg(LVL_ERROR, ctx.file, ctx.line, "error", fmt, ap);
83 static void ftepp_error(ftepp_t *ftepp, const char *fmt, ...)
90 con_vprintmsg(LVL_ERROR, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
94 static bool GMQCC_WARN ftepp_warn(ftepp_t *ftepp, int warntype, const char *fmt, ...)
97 int lvl = LVL_WARNING;
99 if (!OPTS_WARN(warntype))
108 con_vprintmsg(lvl, ftepp->lex->tok.ctx.file, ftepp->lex->tok.ctx.line, "error", fmt, ap);
113 static pptoken *pptoken_make(ftepp_t *ftepp)
115 pptoken *token = (pptoken*)mem_a(sizeof(pptoken));
116 token->token = ftepp->token;
118 if (token->token == TOKEN_WHITE)
119 token->value = util_strdup(" ");
122 token->value = util_strdup(ftepp_tokval(ftepp));
124 memcpy(&token->constval, &ftepp->lex->tok.constval, sizeof(token->constval));
128 static void pptoken_delete(pptoken *self)
134 static ppmacro *ppmacro_new(lex_ctx ctx, const char *name)
136 ppmacro *macro = (ppmacro*)mem_a(sizeof(ppmacro));
137 memset(macro, 0, sizeof(*macro));
138 macro->name = util_strdup(name);
142 static void ppmacro_delete(ppmacro *self)
145 for (i = 0; i < vec_size(self->params); ++i)
146 mem_d(self->params[i]);
147 vec_free(self->params);
148 for (i = 0; i < vec_size(self->output); ++i)
149 pptoken_delete(self->output[i]);
150 vec_free(self->output);
155 static ftepp_t* ftepp_new()
159 ftepp = (ftepp_t*)mem_a(sizeof(*ftepp));
160 memset(ftepp, 0, sizeof(*ftepp));
162 ftepp->output_on = true;
167 static void ftepp_delete(ftepp_t *self)
170 for (i = 0; i < vec_size(self->macros); ++i)
171 ppmacro_delete(self->macros[i]);
172 vec_free(self->macros);
173 vec_free(self->conditions);
174 lex_close(self->lex);
178 static void ftepp_out(ftepp_t *ftepp, const char *str, bool ignore_cond)
180 if (ignore_cond || ftepp->output_on)
184 if (!ftepp->output_string) {
189 data = vec_add(ftepp->output, len);
190 memcpy(data, str, len);
194 static void ftepp_update_output_condition(ftepp_t *ftepp)
197 ftepp->output_on = true;
198 for (i = 0; i < vec_size(ftepp->conditions); ++i)
199 ftepp->output_on = ftepp->output_on && ftepp->conditions[i].on;
202 static ppmacro* ftepp_macro_find(ftepp_t *ftepp, const char *name)
205 for (i = 0; i < vec_size(ftepp->macros); ++i) {
206 if (!strcmp(name, ftepp->macros[i]->name))
207 return ftepp->macros[i];
212 static void ftepp_macro_delete(ftepp_t *ftepp, const char *name)
215 for (i = 0; i < vec_size(ftepp->macros); ++i) {
216 if (!strcmp(name, ftepp->macros[i]->name)) {
217 vec_remove(ftepp->macros, i, 1);
223 static inline int ftepp_next(ftepp_t *ftepp)
225 return (ftepp->token = lex_do(ftepp->lex));
228 /* Important: this does not skip newlines! */
229 static bool ftepp_skipspace(ftepp_t *ftepp)
231 if (ftepp->token != TOKEN_WHITE)
233 while (ftepp_next(ftepp) == TOKEN_WHITE) {}
234 if (ftepp->token >= TOKEN_EOF) {
235 ftepp_error(ftepp, "unexpected end of preprocessor directive");
241 /* this one skips EOLs as well */
242 static bool ftepp_skipallwhite(ftepp_t *ftepp)
244 if (ftepp->token != TOKEN_WHITE && ftepp->token != TOKEN_EOL)
248 } while (ftepp->token == TOKEN_WHITE || ftepp->token == TOKEN_EOL);
249 if (ftepp->token >= TOKEN_EOF) {
250 ftepp_error(ftepp, "unexpected end of preprocessor directive");
257 * The huge macro parsing code...
259 static bool ftepp_define_params(ftepp_t *ftepp, ppmacro *macro)
263 if (!ftepp_skipspace(ftepp))
265 if (ftepp->token == ')')
267 switch (ftepp->token) {
273 ftepp_error(ftepp, "unexpected token in parameter list");
276 vec_push(macro->params, util_strdup(ftepp_tokval(ftepp)));
278 if (!ftepp_skipspace(ftepp))
280 } while (ftepp->token == ',');
281 if (ftepp->token != ')') {
282 ftepp_error(ftepp, "expected closing paren after macro parameter list");
286 /* skipspace happens in ftepp_define */
290 static bool ftepp_define_body(ftepp_t *ftepp, ppmacro *macro)
293 while (ftepp->token != TOKEN_EOL && ftepp->token < TOKEN_EOF) {
294 ptok = pptoken_make(ftepp);
295 vec_push(macro->output, ptok);
298 /* recursive expansion can cause EOFs here */
299 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
300 ftepp_error(ftepp, "unexpected junk after macro or unexpected end of file");
306 static bool ftepp_define(ftepp_t *ftepp)
309 (void)ftepp_next(ftepp);
310 if (!ftepp_skipspace(ftepp))
313 switch (ftepp->token) {
317 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
318 if (macro && ftepp->output_on) {
319 if (ftepp_warn(ftepp, WARN_PREPROCESSOR, "redefining `%s`", ftepp_tokval(ftepp)))
321 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
323 macro = ppmacro_new(ftepp_ctx(ftepp), ftepp_tokval(ftepp));
326 ftepp_error(ftepp, "expected macro name");
330 (void)ftepp_next(ftepp);
332 if (ftepp->token == '(') {
333 macro->has_params = true;
334 if (!ftepp_define_params(ftepp, macro))
338 if (!ftepp_skipspace(ftepp))
341 if (!ftepp_define_body(ftepp, macro))
344 if (ftepp->output_on)
345 vec_push(ftepp->macros, macro);
347 ppmacro_delete(macro);
353 * When a macro is used we have to handle parameters as well
354 * as special-concatenation via ## or stringification via #
356 * Note: parenthesis can nest, so FOO((a),b) is valid, but only
357 * this kind of parens. Curly braces or [] don't count towards the
364 static void macroparam_clean(macroparam *self)
367 for (i = 0; i < vec_size(self->tokens); ++i)
368 pptoken_delete(self->tokens[i]);
369 vec_free(self->tokens);
372 /* need to leave the last token up */
373 static bool ftepp_macro_call_params(ftepp_t *ftepp, macroparam **out_params)
375 macroparam *params = NULL;
381 if (!ftepp_skipallwhite(ftepp))
383 while (ftepp->token != ')') {
385 if (!ftepp_skipallwhite(ftepp))
387 while (parens || ftepp->token != ',') {
388 if (ftepp->token == '(')
390 else if (ftepp->token == ')') {
395 ptok = pptoken_make(ftepp);
396 vec_push(mp.tokens, ptok);
397 if (ftepp_next(ftepp) >= TOKEN_EOF) {
398 ftepp_error(ftepp, "unexpected EOF in macro call");
402 vec_push(params, mp);
404 if (ftepp->token == ')')
406 if (ftepp->token != ',') {
407 ftepp_error(ftepp, "expected closing paren or comma in macro call");
410 if (ftepp_next(ftepp) >= TOKEN_EOF) {
411 ftepp_error(ftepp, "unexpected EOF in macro call");
415 /* need to leave that up
416 if (ftepp_next(ftepp) >= TOKEN_EOF) {
417 ftepp_error(ftepp, "unexpected EOF in macro call");
421 *out_params = params;
426 macroparam_clean(&mp);
427 for (i = 0; i < vec_size(params); ++i)
428 macroparam_clean(¶ms[i]);
433 static bool macro_params_find(ppmacro *macro, const char *name, size_t *idx)
436 for (i = 0; i < vec_size(macro->params); ++i) {
437 if (!strcmp(macro->params[i], name)) {
445 static bool ftepp_preprocess(ftepp_t *ftepp);
446 static bool ftepp_macro_expand(ftepp_t *ftepp, ppmacro *macro, macroparam *params)
448 char *old_string = ftepp->output;
449 bool old_string_flag = ftepp->output_string;
450 lex_file *old_lexer = ftepp->lex;
457 if (!vec_size(macro->output))
460 ftepp->output = NULL;
461 ftepp->output_string = true;
462 for (o = 0; o < vec_size(macro->output); ++o) {
463 pptoken *out = macro->output[o];
464 switch (out->token) {
468 if (!macro_params_find(macro, out->value, &pi)) {
469 ftepp_out(ftepp, out->value, false);
472 for (pv = 0; pv < vec_size(params[pi].tokens); ++pv) {
473 out = params[pi].tokens[pv];
474 if (out->token == TOKEN_EOL)
475 ftepp_out(ftepp, "\n", false);
477 ftepp_out(ftepp, out->value, false);
482 if (o + 1 < vec_size(macro->output) && macro->output[o+1]->token == '#') {
483 /* raw concatenation */
487 ftepp_out(ftepp, "#", false);
490 ftepp_out(ftepp, "\n", false);
493 ftepp_out(ftepp, out->value, false);
497 vec_push(ftepp->output, 0);
498 /* Now run the preprocessor recursively on this string buffer */
500 printf("__________\n%s\n=========\n", ftepp->output);
502 inlex = lex_open_string(ftepp->output, vec_size(ftepp->output)-1, ftepp->lex->name);
504 ftepp_error(ftepp, "internal error: failed to instantiate lexer");
508 ftepp->output = old_string;
509 ftepp->output_string = old_string_flag;
511 if (!ftepp_preprocess(ftepp)) {
512 lex_close(ftepp->lex);
518 ftepp->lex = old_lexer;
519 ftepp->output = old_string;
520 ftepp->output_string = old_string_flag;
524 static bool ftepp_macro_call(ftepp_t *ftepp, ppmacro *macro)
527 macroparam *params = NULL;
530 if (!macro->has_params) {
531 if (!ftepp_macro_expand(ftepp, macro, NULL))
538 if (!ftepp_skipallwhite(ftepp))
541 if (ftepp->token != '(') {
542 ftepp_error(ftepp, "expected macro parameters in parenthesis");
547 if (!ftepp_macro_call_params(ftepp, ¶ms))
550 if (vec_size(params) != vec_size(macro->params)) {
551 ftepp_error(ftepp, "macro %s expects %u paramteters, %u provided", macro->name,
552 (unsigned int)vec_size(macro->params),
553 (unsigned int)vec_size(params));
558 if (!ftepp_macro_expand(ftepp, macro, params))
563 for (o = 0; o < vec_size(params); ++o)
564 macroparam_clean(¶ms[o]);
570 * #if - the FTEQCC way:
571 * defined(FOO) => true if FOO was #defined regardless of parameters or contents
572 * <numbers> => True if the number is not 0
573 * !<factor> => True if the factor yields false
574 * !!<factor> => ERROR on 2 or more unary nots
575 * <macro> => becomes the macro's FIRST token regardless of parameters
576 * <e> && <e> => True if both expressions are true
577 * <e> || <e> => True if either expression is true
579 * <ident> => False (remember for macros the <macro> rule applies instead)
580 * Unary + and - are weird and wrong in fteqcc so we don't allow them
581 * parenthesis in expressions are allowed
582 * parameter lists on macros are errors
583 * No mathematical calculations are executed
585 static bool ftepp_if_expr(ftepp_t *ftepp, bool *out)
590 if (!ftepp_skipspace(ftepp))
593 while (ftepp->token == '!') {
596 if (!ftepp_skipspace(ftepp))
600 switch (ftepp->token) {
604 if (!strcmp(ftepp_tokval(ftepp), "defined")) {
606 if (!ftepp_skipspace(ftepp))
608 if (ftepp->token != '(') {
609 ftepp_error(ftepp, "`defined` keyword in #if requires a macro name in parenthesis");
613 if (!ftepp_skipspace(ftepp))
615 if (ftepp->token != TOKEN_IDENT &&
616 ftepp->token != TOKEN_TYPENAME &&
617 ftepp->token != TOKEN_KEYWORD)
619 ftepp_error(ftepp, "defined() used on an unexpected token type");
622 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
625 if (!ftepp_skipspace(ftepp))
627 if (ftepp->token != ')') {
628 ftepp_error(ftepp, "expected closing paren");
634 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
635 if (!macro || !vec_size(macro->output)) {
638 /* This does not expand recursively! */
639 switch (macro->output[0]->token) {
641 *out = !!(macro->output[0]->constval.f);
643 case TOKEN_FLOATCONST:
644 *out = !!(macro->output[0]->constval.f);
652 case TOKEN_STRINGCONST:
656 *out = !!(ftepp->lex->tok.constval.i);
658 case TOKEN_FLOATCONST:
659 *out = !!(ftepp->lex->tok.constval.f);
664 if (!ftepp_if_expr(ftepp, out))
666 if (ftepp->token != ')') {
667 ftepp_error(ftepp, "expected closing paren in #if expression");
673 ftepp_error(ftepp, "junk in #if");
679 ftepp->lex->flags.noops = false;
681 if (!ftepp_skipspace(ftepp))
683 ftepp->lex->flags.noops = true;
685 if (ftepp->token == ')')
688 if (ftepp->token != TOKEN_OPERATOR)
691 if (!strcmp(ftepp_tokval(ftepp), "&&") ||
692 !strcmp(ftepp_tokval(ftepp), "||"))
695 char opc = ftepp_tokval(ftepp)[0];
698 if (!ftepp_if_expr(ftepp, &next))
708 ftepp_error(ftepp, "junk after #if");
713 static bool ftepp_if(ftepp_t *ftepp, ppcondition *cond)
717 memset(cond, 0, sizeof(*cond));
718 (void)ftepp_next(ftepp);
720 if (!ftepp_skipspace(ftepp))
722 if (ftepp->token == TOKEN_EOL) {
723 ftepp_error(ftepp, "expected expression for #if-directive");
727 if (!ftepp_if_expr(ftepp, &result))
735 * ifdef is rather simple
737 static bool ftepp_ifdef(ftepp_t *ftepp, ppcondition *cond)
740 memset(cond, 0, sizeof(*cond));
741 (void)ftepp_next(ftepp);
742 if (!ftepp_skipspace(ftepp))
745 switch (ftepp->token) {
749 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
752 ftepp_error(ftepp, "expected macro name");
756 (void)ftepp_next(ftepp);
757 if (!ftepp_skipspace(ftepp))
759 /* relaxing this condition
760 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
761 ftepp_error(ftepp, "stray tokens after #ifdef");
770 * undef is also simple
772 static bool ftepp_undef(ftepp_t *ftepp)
774 (void)ftepp_next(ftepp);
775 if (!ftepp_skipspace(ftepp))
778 if (ftepp->output_on) {
779 switch (ftepp->token) {
783 ftepp_macro_delete(ftepp, ftepp_tokval(ftepp));
786 ftepp_error(ftepp, "expected macro name");
791 (void)ftepp_next(ftepp);
792 if (!ftepp_skipspace(ftepp))
794 /* relaxing this condition
795 if (ftepp->token != TOKEN_EOL && ftepp->token != TOKEN_EOF) {
796 ftepp_error(ftepp, "stray tokens after #ifdef");
803 /* Basic structure handlers */
804 static bool ftepp_else_allowed(ftepp_t *ftepp)
806 if (!vec_size(ftepp->conditions)) {
807 ftepp_error(ftepp, "#else without #if");
810 if (vec_last(ftepp->conditions).had_else) {
811 ftepp_error(ftepp, "multiple #else for a single #if");
817 static bool ftepp_hash(ftepp_t *ftepp)
822 lex_ctx ctx = ftepp_ctx(ftepp);
824 if (!ftepp_skipspace(ftepp))
827 switch (ftepp->token) {
831 if (!strcmp(ftepp_tokval(ftepp), "define")) {
832 return ftepp_define(ftepp);
834 else if (!strcmp(ftepp_tokval(ftepp), "undef")) {
835 return ftepp_undef(ftepp);
837 else if (!strcmp(ftepp_tokval(ftepp), "ifdef")) {
838 if (!ftepp_ifdef(ftepp, &cond))
840 cond.was_on = cond.on;
841 vec_push(ftepp->conditions, cond);
842 ftepp->output_on = ftepp->output_on && cond.on;
845 else if (!strcmp(ftepp_tokval(ftepp), "ifndef")) {
846 if (!ftepp_ifdef(ftepp, &cond))
849 cond.was_on = cond.on;
850 vec_push(ftepp->conditions, cond);
851 ftepp->output_on = ftepp->output_on && cond.on;
854 else if (!strcmp(ftepp_tokval(ftepp), "elifdef")) {
855 if (!ftepp_else_allowed(ftepp))
857 if (!ftepp_ifdef(ftepp, &cond))
859 pc = &vec_last(ftepp->conditions);
860 pc->on = !pc->was_on && cond.on;
861 pc->was_on = pc->was_on || pc->on;
862 ftepp_update_output_condition(ftepp);
865 else if (!strcmp(ftepp_tokval(ftepp), "elifndef")) {
866 if (!ftepp_else_allowed(ftepp))
868 if (!ftepp_ifdef(ftepp, &cond))
871 pc = &vec_last(ftepp->conditions);
872 pc->on = !pc->was_on && cond.on;
873 pc->was_on = pc->was_on || pc->on;
874 ftepp_update_output_condition(ftepp);
877 else if (!strcmp(ftepp_tokval(ftepp), "elif")) {
878 if (!ftepp_else_allowed(ftepp))
880 if (!ftepp_if(ftepp, &cond))
882 pc = &vec_last(ftepp->conditions);
883 pc->on = !pc->was_on && cond.on;
884 pc->was_on = pc->was_on || pc->on;
885 ftepp_update_output_condition(ftepp);
888 else if (!strcmp(ftepp_tokval(ftepp), "if")) {
889 if (!ftepp_if(ftepp, &cond))
891 cond.was_on = cond.on;
892 vec_push(ftepp->conditions, cond);
893 ftepp->output_on = ftepp->output_on && cond.on;
896 else if (!strcmp(ftepp_tokval(ftepp), "else")) {
897 if (!ftepp_else_allowed(ftepp))
899 pc = &vec_last(ftepp->conditions);
900 pc->on = !pc->was_on;
903 ftepp_update_output_condition(ftepp);
906 else if (!strcmp(ftepp_tokval(ftepp), "endif")) {
907 if (!vec_size(ftepp->conditions)) {
908 ftepp_error(ftepp, "#endif without #if");
911 vec_pop(ftepp->conditions);
913 ftepp_update_output_condition(ftepp);
917 ftepp_error(ftepp, "unrecognized preprocessor directive: `%s`", ftepp_tokval(ftepp));
922 ftepp_error(ftepp, "unexpected preprocessor token: `%s`", ftepp_tokval(ftepp));
925 ftepp_errorat(ftepp, ctx, "empty preprocessor directive");
928 ftepp_error(ftepp, "missing newline at end of file", ftepp_tokval(ftepp));
931 /* Builtins! Don't forget the builtins! */
933 case TOKEN_FLOATCONST:
934 ftepp_out(ftepp, "#", false);
937 if (!ftepp_skipspace(ftepp))
942 static bool ftepp_preprocess(ftepp_t *ftepp)
947 ftepp->lex->flags.preprocessing = true;
948 ftepp->lex->flags.mergelines = false;
949 ftepp->lex->flags.noops = true;
954 if (ftepp->token >= TOKEN_EOF)
957 ftepp->newline = newline;
960 /* For the sake of FTE compatibility... FU, really */
961 ftepp->newline = newline = true;
964 switch (ftepp->token) {
968 macro = ftepp_macro_find(ftepp, ftepp_tokval(ftepp));
970 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
974 if (!ftepp_macro_call(ftepp, macro))
975 ftepp->token = TOKEN_ERROR;
978 if (!ftepp->newline) {
979 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
983 ftepp->lex->flags.mergelines = true;
984 if (ftepp_next(ftepp) >= TOKEN_EOF) {
985 ftepp_error(ftepp, "error in preprocessor directive");
986 ftepp->token = TOKEN_ERROR;
989 if (!ftepp_hash(ftepp))
990 ftepp->token = TOKEN_ERROR;
991 ftepp->lex->flags.mergelines = false;
995 ftepp_out(ftepp, "\n", true);
999 ftepp_out(ftepp, ftepp_tokval(ftepp), false);
1003 } while (!ftepp->errors && ftepp->token < TOKEN_EOF);
1005 newline = ftepp->token == TOKEN_EOF;
1009 /* Like in parser.c - files keep the previous state so we have one global
1010 * preprocessor. Except here we will want to warn about dangling #ifs.
1012 static ftepp_t *ftepp;
1014 static bool ftepp_preprocess_done()
1017 lex_close(ftepp->lex);
1019 if (vec_size(ftepp->conditions)) {
1020 if (ftepp_warn(ftepp, WARN_MULTIFILE_IF, "#if spanning multiple files, is this intended?"))
1026 bool ftepp_preprocess_file(const char *filename)
1028 ftepp->lex = lex_open(filename);
1030 con_out("failed to open file \"%s\"\n", filename);
1033 if (!ftepp_preprocess(ftepp)) {
1034 ftepp_delete(ftepp);
1037 return ftepp_preprocess_done();
1040 bool ftepp_preprocess_string(const char *name, const char *str)
1042 ftepp_t *ftepp = ftepp_new();
1043 ftepp->lex = lex_open_string(str, strlen(str), name);
1045 con_out("failed to create lexer for string \"%s\"\n", name);
1048 if (!ftepp_preprocess(ftepp)) {
1049 ftepp_delete(ftepp);
1052 return ftepp_preprocess_done();
1057 ftepp = ftepp_new();
1063 ftepp_delete(ftepp);