From d8fb878d7af78a13ea399d2a90ee3187add25796 Mon Sep 17 00:00:00 2001 From: Jeremiah Orians Date: Wed, 27 Oct 2021 22:22:49 -0400 Subject: [PATCH] Correct #define behavior inside of #if/#elif/#else/#endif blocks --- cc_macro.c | 16 +++++++++++++++- test/test.answers | 12 ++++++------ test/test1000/proof.answer | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/cc_macro.c b/cc_macro.c index 3ceb548..addfca3 100644 --- a/cc_macro.c +++ b/cc_macro.c @@ -362,6 +362,16 @@ void handle_define() struct token_list* expansion_end = NULL; struct token_list* line_start = macro_token; + /* don't use #define statements from non-included blocks */ + int conditional_define = TRUE; + if(NULL != conditional_inclusion_top) + { + if(FALSE == conditional_inclusion_top->include) + { + conditional_define = FALSE; + } + } + eat_current_token(); require(NULL != macro_token, "got an EOF terminated #define\n"); @@ -371,7 +381,8 @@ void handle_define() hold = calloc(1, sizeof(struct macro_list)); hold->symbol = macro_token->s; hold->next = macro_env; - macro_env = hold; + /* provided it isn't in a non-included block */ + if(conditional_define) macro_env = hold; /* discard the macro name */ eat_current_token(); @@ -401,6 +412,9 @@ void handle_define() hold->expansion = macro_token; } + /* throw away if not used */ + if(!conditional_define) free(hold); + eat_current_token(); } diff --git a/test/test.answers b/test/test.answers index 29dfc58..00b25be 100644 --- a/test/test.answers +++ b/test/test.answers @@ -212,9 +212,9 @@ eb1f6aab09a1a4966f9f49a518d8aa03ca97aa7c46e443871d758a306671a85e test/results/t a2cbfd5f76d3be8049c6737e3fa02f191caf29b6885db006fa23f86e6eacc186 test/results/test0106-knight-posix-binary d75e450e2fcdf19df63f9d6a3fe5e032933e57b33f6f06b39a8ed2f3dc759f17 test/results/test0106-riscv64-binary 473cc504d6cba9eaf648abcf15c83a317ff3f4a7f08f5e8936e3b35b2cc4fbc6 test/results/test0106-x86-binary -1071ba48b488d819219d499df231dbfa8c563651418810371bdc43e37ca9e5ce test/results/test1000-aarch64-binary -e2e37c4d5d15ca647699b2bd32858b842ce9718e780d7e5a7771087948d4e36a test/results/test1000-amd64-binary -4b6560f40038a37beaa6e02018a8058e42e48ae60f2e6cec6536fc8cf74aca1e test/results/test1000-armv7l-binary -e49004d44844da98f993af2f8536048c6b389e653d611c8dc27bde70ba026118 test/results/test1000-knight-posix-binary -c55cf7ffc36b8d81fa06b65a362f680b0d43d3527672b87e4150ebecd63d92b9 test/results/test1000-riscv64-binary -610050fdc4005e36c13d881e6eedc08c7a4710bd3ad9a1cef007e29958fdbf04 test/results/test1000-x86-binary +d120d662f78dbc3f0a81ba777847d3aaa2363b0f13f039066d2cb45e4a89477d test/results/test1000-aarch64-binary +3a5a71519bb54514d832a51f464329fe1e171f2c9a49e4251ba2198466b0f8e3 test/results/test1000-amd64-binary +74941c9a1ec9ab8c9534cda69c562a5da2a50d33e549c970b49860805ac26bc5 test/results/test1000-armv7l-binary +bebcc13cb938bea42bf018a6bcf3e69e8a7365b2ce8202805bab3091df1ddb82 test/results/test1000-knight-posix-binary +0867008fddffe0115ade51505f25a688d27c9bedff1b7b9658c388bbccd233c7 test/results/test1000-riscv64-binary +0fc90e4f9401ab0c93a6e76b9e9fd0cbe780588223bf970641b5fe47809c76dc test/results/test1000-x86-binary diff --git a/test/test1000/proof.answer b/test/test1000/proof.answer index 90ca619..13c50a5 100644 --- a/test/test1000/proof.answer +++ b/test/test1000/proof.answer @@ -1 +1 @@ -9ddace917535db9ad89c8aa035e3e90145de84cefb2f80b3c18cf36389b02633 test/test1000/proof +abcd0057124ccc0bf88a48e33d4de259684409080be258ce90a78e96b2af9371 test/test1000/proof