From c1c7a93884e148b6ffdbae8002ee64b9f975d64e Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Mon, 17 Dec 2012 14:58:40 +0000 Subject: [PATCH] Implemented -D for preprocessor --- ftepp.c | 52 +++++++++++++++++++++++++++++++--------------------- gmqcc.h | 1 + main.c | 25 ++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 22 deletions(-) diff --git a/ftepp.c b/ftepp.c index d28b19a..6fff6e0 100644 --- a/ftepp.c +++ b/ftepp.c @@ -1366,12 +1366,23 @@ bool ftepp_preprocess_string(const char *name, const char *str) return ftepp_preprocess_done(); } + +void ftepp_add_macro(const char *name, const char *value) { + char *create = NULL; + vec_upload(create, "#define ", 8); + vec_upload(create, name, strlen(name)); + vec_push (create, ' '); + vec_upload(create, value, strlen(value)); + vec_push (create, 0); + + ftepp_preprocess_string("__builtin__", create); + vec_free (create); +} + bool ftepp_init() { char minor[32]; char major[32]; - char *verminor = NULL; - char *vermajor = NULL; ftepp = ftepp_new(); if (!ftepp) @@ -1385,33 +1396,32 @@ bool ftepp_init() if (opts.standard == COMPILER_FTEQCC) { ftepp_add_define(NULL, "__STD_FTEQCC__"); /* 1.00 */ - major[0] = '1'; - minor[0] = '0'; + major[0] = '"'; + major[1] = '1'; + major[2] = '"'; + + minor[0] = '"'; + minor[1] = '0'; + minor[2] = '"'; } else if (opts.standard == COMPILER_GMQCC) { ftepp_add_define(NULL, "__STD_GMQCC__"); - sprintf(major, "%d", GMQCC_VERSION_MAJOR); - sprintf(minor, "%d", GMQCC_VERSION_MINOR); + sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR); + sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR); } else if (opts.standard == COMPILER_QCC) { ftepp_add_define(NULL, "__STD_QCC__"); /* 1.0 */ - major[0] = '1'; - minor[0] = '0'; - } + major[0] = '"'; + major[1] = '1'; + major[2] = '"'; - vec_upload(verminor, "#define __STD_VERSION_MINOR__ \"", 31); - vec_upload(vermajor, "#define __STD_VERSION_MAJOR__ \"", 31); - vec_upload(verminor, minor, strlen(minor)); - vec_upload(vermajor, major, strlen(major)); - vec_push (verminor, '"'); - vec_push (vermajor, '"'); - vec_push (verminor, 0); - vec_push (vermajor, 0); + minor[0] = '"'; + minor[1] = '0'; + minor[2] = '"'; + } - ftepp_preprocess_string("__builtin__", verminor); - ftepp_preprocess_string("__builtin__", vermajor); + ftepp_add_macro("__STD_VERSION_MINOR__", minor); + ftepp_add_macro("__STD_VERSION_MAJOR__", major); - vec_free(verminor); - vec_free(vermajor); return true; } diff --git a/gmqcc.h b/gmqcc.h index 2ccaafe..9a07d7a 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -845,6 +845,7 @@ void ftepp_finish (); const char *ftepp_get (); void ftepp_flush (); void ftepp_add_define (const char *source, const char *name); +void ftepp_add_macro (const char *name, const char *value); /*===================================================================*/ /*======================= main.c commandline ========================*/ diff --git a/main.c b/main.c index 0aea1c7..9e2051b 100644 --- a/main.c +++ b/main.c @@ -34,8 +34,10 @@ cmd_options opts; const oper_info *operators = NULL; size_t operator_count = 0; -typedef struct { char *filename; int type; } argitem; +typedef struct { char *filename; int type; } argitem; +typedef struct { char *name; char *value; } ppitem; static argitem *items = NULL; +static ppitem *ppems = NULL; #define TYPE_QC 0 #define TYPE_ASM 1 @@ -187,6 +189,7 @@ static bool options_parse(int argc, char **argv) { while (!argend && argc > 1) { char *argarg; argitem item; + ppitem macro; ++argv; --argc; @@ -294,6 +297,17 @@ static bool options_parse(int argc, char **argv) { options_setflag("LNO", true); break; + case 'D': + if (!(argarg = strchr(argv[0] + 2, '='))) { + con_out("missing = in -D\n"); + exit(0); + } + *argarg='\0'; /* terminate for name */ + macro.name = util_strdup(argarg); + macro.value = util_strdup(argv[0]+2); + vec_push(ppems, macro); + break; + /* handle all -fflags */ case 'f': util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1); @@ -587,11 +601,19 @@ int main(int argc, char **argv) { goto cleanup; } } + if (opts.pp_only || OPTS_FLAG(FTEPP)) { if (!ftepp_init()) { con_err("failed to initialize parser\n"); retval = 1; goto cleanup; + } else { + size_t i; + for (i = 0; i < vec_size(ppems); ++i) { + ftepp_add_macro(ppems[i].name, ppems[i].value); + mem_d(ppems[i].name); + mem_d(ppems[i].value); + } } } @@ -720,6 +742,7 @@ cleanup: ftepp_finish(); con_close(); vec_free(items); + vec_free(ppems); if (!opts.pp_only) parser_cleanup(); -- 2.39.2