Skip to content

Commit

Permalink
Don't repeatedly try to expand #include IDENT
Browse files Browse the repository at this point in the history
Fixes #16.
  • Loading branch information
sgraham committed May 3, 2024
1 parent 15c5eb1 commit d86212b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ static char* search_include_next(char* filename) {
}

// Read an #include argument.
static char* read_include_filename(Token** rest, Token* tok, bool* is_dquote) {
static char* read_include_filename(Token** rest, Token* tok, bool* is_dquote, bool can_expand) {
// Pattern 1: #include "foo.h"
if (tok->kind == TK_STR) {
// A double-quoted filename for #include is a special kind of
Expand Down Expand Up @@ -765,9 +765,9 @@ static char* read_include_filename(Token** rest, Token* tok, bool* is_dquote) {
// Pattern 3: #include FOO
// In this case FOO must be macro-expanded to either
// a single string token or a sequence of "<" ... ">".
if (tok->kind == TK_IDENT) {
if (can_expand && tok->kind == TK_IDENT) {
Token* tok2 = preprocess2(copy_line(rest, tok));
return read_include_filename(&tok2, tok2, is_dquote);
return read_include_filename(&tok2, tok2, is_dquote, /*can_expand=*/false);
}

error_tok(tok, "expected a filename");
Expand Down Expand Up @@ -884,7 +884,7 @@ static Token* preprocess2(Token* tok) {

if (equal(tok, "include")) {
bool is_dquote;
char* filename = read_include_filename(&tok, tok->next, &is_dquote);
char* filename = read_include_filename(&tok, tok->next, &is_dquote, true);

if (filename[0] != '/' && is_dquote) {
char* path = format(AL_Compile, "%s/%s", dirname(bumpstrdup(start->file->name, AL_Compile)),
Expand All @@ -902,7 +902,7 @@ static Token* preprocess2(Token* tok) {

if (equal(tok, "include_next")) {
bool ignore;
char* filename = read_include_filename(&tok, tok->next, &ignore);
char* filename = read_include_filename(&tok, tok->next, &ignore, true);
char* path = search_include_next(filename);
tok = include_file(tok, path ? path : filename, start->next->next);
continue;
Expand Down
5 changes: 5 additions & 0 deletions test/err_identinclude.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: {self}
// RET: 255
// TXT: {self}:5: #include a
// TXT: ^ error: expected a filename
#include a

0 comments on commit d86212b

Please sign in to comment.