Skip to content

Commit

Permalink
Add -I<dir> option
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent d85fc4f commit a1dd621
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ chibicc: $(OBJS)
$(OBJS): chibicc.h

test/%.exe: chibicc test/%.c
./chibicc -c -o test/$*.o test/$*.c
./chibicc -Itest -c -o test/$*.o test/$*.c
$(CC) -o $@ test/$*.o -xc test/common

test: $(TESTS)
Expand All @@ -35,7 +35,7 @@ stage2/%.o: chibicc self.py %.c

stage2/test/%.exe: stage2/chibicc test/%.c
mkdir -p stage2/test
./stage2/chibicc -c -o stage2/test/$*.o test/$*.c
./stage2/chibicc -Itest -c -o stage2/test/$*.o test/$*.c
$(CC) -o $@ stage2/test/$*.o -xc test/common

test-stage2: $(TESTS:test/%=stage2/test/%)
Expand Down
1 change: 1 addition & 0 deletions chibicc.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,4 +348,5 @@ int align_to(int n, int align);

bool file_exists(char *path);

extern StringArray include_paths;
extern char *base_file;
14 changes: 13 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "chibicc.h"

StringArray include_paths;

static bool opt_E;
static bool opt_S;
static bool opt_c;
Expand All @@ -19,7 +21,12 @@ static void usage(int status) {
}

static bool take_arg(char *arg) {
return !strcmp(arg, "-o");
char *x[] = {"-o", "-I"};

for (int i = 0; i < sizeof(x) / sizeof(*x); i++)
if (!strcmp(arg, x[i]))
return true;
return false;
}

static void parse_args(int argc, char **argv) {
Expand Down Expand Up @@ -69,6 +76,11 @@ static void parse_args(int argc, char **argv) {
continue;
}

if (!strncmp(argv[i], "-I", 2)) {
strarray_push(&include_paths, argv[i] + 2);
continue;
}

if (!strcmp(argv[i], "-cc1-input")) {
base_file = argv[++i];
continue;
Expand Down
19 changes: 16 additions & 3 deletions preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,19 @@ static bool expand_macro(Token **rest, Token *tok) {
return true;
}

static char *search_include_paths(char *filename) {
if (filename[0] == '/')
return filename;

// Search a file from the include paths.
for (int i = 0; i < include_paths.len; i++) {
char *path = format("%s/%s", include_paths.data[i], filename);
if (file_exists(path))
return path;
}
return NULL;
}

// Read an #include argument.
static char *read_include_filename(Token **rest, Token *tok, bool *is_dquote) {
// Pattern 1: #include "foo.h"
Expand Down Expand Up @@ -669,16 +682,16 @@ static Token *preprocess2(Token *tok) {
bool is_dquote;
char *filename = read_include_filename(&tok, tok->next, &is_dquote);

if (filename[0] != '/') {
if (filename[0] != '/' && is_dquote) {
char *path = format("%s/%s", dirname(strdup(start->file->name)), filename);
if (file_exists(path)) {
tok = include_file(tok, path, start->next->next);
continue;
}
}

// TODO: Search a file from the include paths.
tok = include_file(tok, filename, start->next->next);
char *path = search_include_paths(filename);
tok = include_file(tok, path ? path : filename, start->next->next);
continue;
}

Expand Down
2 changes: 2 additions & 0 deletions self.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
char *dirname(char *path);
char *strncpy(char *dest, char *src, long n);
int stat(char *pathname, struct stat *statbuf);
int stat(char *pathname, struct stat *statbuf);
char *dirname(char *path);
""")

for path in sys.argv[1:]:
Expand Down
6 changes: 6 additions & 0 deletions test/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,10 @@ echo "#include \"$tmp/out1\"" | $chibicc -E -o $tmp/out2 -
cat $tmp/out2 | grep -q foo
check '-E and -o'

# -I
mkdir $tmp/dir
echo foo > $tmp/dir/i-option-test
echo "#include \"i-option-test\"" | $chibicc -I$tmp/dir -E - | grep -q foo
check -I

echo OK

0 comments on commit a1dd621

Please sign in to comment.