Skip to content

Commit

Permalink
Add -include option
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent b377284 commit 8f5ff07
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
2 changes: 2 additions & 0 deletions chibicc.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ Token *tokenize_file(char *filename);
// preprocess.c
//

char *search_include_paths(char *filename);
bool file_exists(char *path);
void init_macros(void);
void define_macro(char *name, char *buf);
void undef_macro(char *name);
Expand Down
51 changes: 46 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
StringArray include_paths;
bool opt_fcommon = true;

static StringArray opt_include;
static bool opt_E;
static bool opt_S;
static bool opt_c;
Expand All @@ -22,7 +23,7 @@ static void usage(int status) {
}

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

for (int i = 0; i < sizeof(x) / sizeof(*x); i++)
if (!strcmp(arg, x[i]))
Expand Down Expand Up @@ -133,6 +134,11 @@ static void parse_args(int argc, char **argv) {
continue;
}

if (!strcmp(argv[i], "-include")) {
strarray_push(&opt_include, argv[++i]);
continue;
}

if (!strcmp(argv[i], "-cc1-input")) {
base_file = argv[++i];
continue;
Expand Down Expand Up @@ -274,12 +280,47 @@ static void print_tokens(Token *tok) {
fprintf(out, "\n");
}

static void cc1(void) {
// Tokenize and parse.
Token *tok = tokenize_file(base_file);
static Token *must_tokenize_file(char *path) {
Token *tok = tokenize_file(path);
if (!tok)
error("%s: %s", base_file, strerror(errno));
error("%s: %s", path, strerror(errno));
return tok;
}

static Token *append_tokens(Token *tok1, Token *tok2) {
if (!tok1 || tok1->kind == TK_EOF)
return tok2;

Token *t = tok1;
while (t->next->kind != TK_EOF)
t = t->next;
t->next = tok2;
return tok1;
}

static void cc1(void) {
Token *tok = NULL;

// Process -include option
for (int i = 0; i < opt_include.len; i++) {
char *incl = opt_include.data[i];

char *path;
if (file_exists(incl)) {
path = incl;
} else {
path = search_include_paths(incl);
if (!path)
error("-include: %s: %s", incl, strerror(errno));
}

Token *tok2 = must_tokenize_file(path);
tok = append_tokens(tok, tok2);
}

// Tokenize and parse.
Token *tok2 = must_tokenize_file(base_file);
tok = append_tokens(tok, tok2);
tok = preprocess(tok);

// If -E is given, print out preprocessed C code as a result.
Expand Down
2 changes: 1 addition & 1 deletion preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ static bool expand_macro(Token **rest, Token *tok) {
return true;
}

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

Expand Down
7 changes: 7 additions & 0 deletions test/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,11 @@ check '-fcommon'
echo 'int foo;' | $chibicc -fno-common -S -o- - | grep -q '^foo:'
check '-fno-common'

# -include
echo foo > $tmp/out.h
echo bar | $chibicc -include $tmp/out.h -E -o- - | grep -q -z 'foo.*bar'
check -include
echo NULL | $chibicc -Iinclude -include stdio.h -E -o- - | grep -q 0
check -include

echo OK

0 comments on commit 8f5ff07

Please sign in to comment.