diff --git a/cc_reader.c b/cc_reader.c index e4dc3f7..a201f88 100644 --- a/cc_reader.c +++ b/cc_reader.c @@ -99,12 +99,13 @@ int preserve_keyword(int c, char* S) void reset_hold_string() { - int i = string_index + 2; - while(0 != i) + int i = MAX_STRING; + while(0 <= i) { hold_string[i] = 0; i = i - 1; } + string_index = 0; } /* note if this is the first token in the list, head needs fixing up */ @@ -210,6 +211,22 @@ struct token_list* remove_preprocessor_directives(struct token_list* head) return first; } +void new_token(char* s, int size) +{ + struct token_list* current = calloc(1, sizeof(struct token_list)); + require(NULL != current, "Exhausted memory while getting token\n"); + + /* More efficiently allocate memory for string */ + current->s = calloc(size, sizeof(char)); + require(NULL != current->s, "Exhausted memory while trying to copy a token\n"); + copy_string(current->s, s, MAX_STRING); + + current->prev = token; + current->next = token; + current->linenumber = line; + current->filename = file; + token = current; +} int get_token(int c) { @@ -321,19 +338,44 @@ reset: c = consume_byte(c); } - /* More efficiently allocate memory for string */ - current->s = calloc(string_index + 2, sizeof(char)); - require(NULL != current->s, "Exhausted memory while trying to copy a token\n"); - copy_string(current->s, hold_string, MAX_STRING); - - current->prev = token; - current->next = token; - current->linenumber = line; - current->filename = file; - token = current; + new_token(hold_string, string_index + 2); return c; } + +int consume_filename(int c) +{ + reset_hold_string(); + int done = FALSE; + + while(!done) + { + if(c == EOF) + { + fputs("we don't support EOF as a filename in #FILENAME statements\n", stderr); + exit(EXIT_FAILURE); + } + else if((32 == c) || (9 == c) || (c == '\n')) + { + c = grab_byte(); + } + else + { + do + { + c = consume_byte(c); + require(EOF != c, "Unterminated filename in #FILENAME\n"); + } while((32 != c) && (9 != c) && ('\n' != c)); + done = TRUE; + } + } + + /* with just a little extra to put in the matching at the end */ + new_token(hold_string, string_index + 3); + return c; +} + + int change_filename(int ch) { require(EOF != ch, "#FILENAME failed to receive filename\n"); @@ -341,7 +383,7 @@ int change_filename(int ch) token = token->next; /* Get new filename */ - ch = get_token(ch); + ch = consume_filename(ch); file = token->s; /* Remove it from the processing list */ token = token->next; diff --git a/test/test.answers b/test/test.answers index 7ddafcf..1f6669f 100644 --- a/test/test.answers +++ b/test/test.answers @@ -265,10 +265,10 @@ c23c7615009ab8824ce7c2c60051a0e86bae85a6c1b62f3d0f34fd4d4c691aed test/results/t 922a7955d68afda9781a70707dac2446d51d729eb3434a8abc0dc1de3a3519bb test/results/test0106-riscv32-binary 55ad174b3664907997e44dbba37a3293af685c7147e21c61f5aab1aed4139644 test/results/test0106-riscv64-binary 4fdc00c91b376ac7ec04bebdd264cf0ac1b7024f33f9a4cdcc3d43788e1278a7 test/results/test0106-x86-binary -f00f62e879d92e451b1054704edd72f26513468301606ed9bb9a02a49833a1f2 test/results/test1000-aarch64-binary -db36c559594b72e63853576c31231bcb8ad60292be387c4621ef396658777c9b test/results/test1000-amd64-binary -dc3c9827c23c837a2d406051471713926dabfdb3ccbb9d58c364d2af8bc7dd59 test/results/test1000-armv7l-binary -b558c9a219673736beeaf817dedcaa7488bb9f3fc4e23e05b72012b703827342 test/results/test1000-knight-posix-binary -07221b2469e4ce669527c8ebc35a96b8ad0cb4aa1f64adf62b2877d1a506aa20 test/results/test1000-riscv32-binary -8eeabd3625d5557c3477c2a78cf25af6e066a29e079a0e69a5a05a510f67dee0 test/results/test1000-riscv64-binary -7b382cb3e9dce4a3053f31960a103e6f2b60d987cdd612dd5af7d1a4c3546507 test/results/test1000-x86-binary +69e1f1ac6a9afce3fbb0e4d4be72170d218becbfdbd9ec202072a5bbd765d4c8 test/results/test1000-aarch64-binary +84d58fb5a76b21d682c05ae977db5b2c7ddb19fb4ed375d9e8fc71c5fbf51e86 test/results/test1000-amd64-binary +15e28a94b3c6fc1bba3abc16673cec48a392efcfb59413d8653385605366a256 test/results/test1000-armv7l-binary +8455b1e2f8deae4c2affe8f3587eee6c1e4e60bd3e1442e874f2dda46ef6e10f test/results/test1000-knight-posix-binary +798cfab8cdea8c0cdea6bbc973f3317cb9ed5a1eb1a5cc4c033df12128b4f1e8 test/results/test1000-riscv32-binary +d296114fdffde13517d91794658fa0a03f81cfdc9e7ad6b32e588a9c821802bb test/results/test1000-riscv64-binary +b4d233590567e9f41b43a4f35b4bb79703a6c3e2ecb26e2dc7cb56ad20505e94 test/results/test1000-x86-binary diff --git a/test/test1000/proof.answer b/test/test1000/proof.answer index 459dc08..c86a6e4 100644 --- a/test/test1000/proof.answer +++ b/test/test1000/proof.answer @@ -1 +1 @@ -872956d4b64c6eac289dcb77bb2657de741436ce75223c2d1fc64d0eea8459c5 test/test1000/proof +1abeaaceb20f78169d3645f3609ac2d78d7eaabcfffc9e030928a86ceeb63a75 test/test1000/proof