Skip to content

Commit

Permalink
Tweak #include behavior, move pragma once test to driver
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhsnn committed Dec 29, 2024
1 parent d4ed26b commit 0e36613
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
8 changes: 4 additions & 4 deletions preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,9 @@ static char *read_include_filename(Token *tok, bool *is_dquote) {
}

static Token *include_file(Token *tok, char *path, Token *filename_tok, int *incl_no) {
if (!path)
error_tok(filename_tok, "file not found");

// Check for "#pragma once"
if (hashmap_get(&pragma_once, path))
return tok;
Expand Down Expand Up @@ -1191,7 +1194,7 @@ static Token *directives(Token **cur, Token *start, bool is_root) {
char *filename = read_include_filename(split_line(&tok, tok->next), &is_dquote);
int incl_no = -1;
char *path = search_include_paths2(filename, start, is_dquote, &incl_no);
return include_file(tok, path ? path : filename, start->next->next, &incl_no);
return include_file(tok, path, start->next->next, &incl_no);
}

if (equal(tok, "include_next")) {
Expand All @@ -1201,9 +1204,6 @@ static Token *directives(Token **cur, Token *start, bool is_root) {
int incl_no = tok->file->incl_no + 1;
char *filename = read_include_filename(split_line(&tok, tok->next), &(bool){0});
char *path = search_include_next(filename, &incl_no);
if (!path)
error_tok(start->next->next, "%s: cannot open file: %s", filename, strerror(errno));

return include_file(tok, path, start->next->next, &incl_no);
}

Expand Down
5 changes: 5 additions & 0 deletions test/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ check -I
echo "#include \"i-option-test\"" | $testcc -I $tmp/dir -E -xc - | grep -q foo
check -I

# pragma once
printf "#pragma once\n#ifdef A\n#error\n#endif\n#define A\n" > $tmp/inc.h
printf "#include \"inc.h\"\n#include \"inc.h\"" | $testcc -xc - -E -I$tmp -o/dev/null
check "pragma once"

# -D
echo foo | $testcc -Dfoo -E -xc - | grep -q 1
check -D
Expand Down
10 changes: 0 additions & 10 deletions test/pragma-once.c

This file was deleted.

0 comments on commit 0e36613

Please sign in to comment.