Skip to content

Commit

Permalink
Fix include path.
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Apr 8, 2012
1 parent 658c028 commit b38faf8
Show file tree
Hide file tree
Showing 29 changed files with 68 additions and 66 deletions.
6 changes: 3 additions & 3 deletions 8cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ extern Token *read_cpp_token(void);
extern void set_input_buffer(List *tokens);
extern List *get_input_buffer(void);
extern void skip_cond_incl(void);
extern bool read_header_file_name(char **name, bool *std);
extern void push_input_file(char *filename, FILE *input);
extern void set_input_file(char *filename, FILE *input);
extern char *read_header_file_name(bool *std);
extern void push_input_file(char *displayname, char *realname, FILE *input);
extern void set_input_file(char *displayname, char *realname, FILE *input);
extern char *input_position(void);
extern char *get_current_file(void);
extern int get_current_line(void);
Expand Down
39 changes: 20 additions & 19 deletions cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _POSIX_C_SOURCE 200809L

#include <ctype.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Expand Down Expand Up @@ -40,7 +41,7 @@ static void read_print(void);

static void eval(char *buf) {
FILE *fp = fmemopen(buf, strlen(buf), "r");
set_input_file("(eval)", fp);
set_input_file("(eval)", NULL, fp);
List *toplevels = read_toplevels();
for (Iter *i = list_iter(toplevels); !iter_end(i);)
emit_toplevel(iter_next(i));
Expand Down Expand Up @@ -581,18 +582,18 @@ static void read_error(void) {
* #include
*/

static void read_cpp_header_name(char **name, bool *std) {
static char *read_cpp_header_name(bool *std) {
if (!get_input_buffer()) {
if (read_header_file_name(name, std))
return;
char *r = read_header_file_name(std);
if (r)
return r;
}
Token *tok = read_expand();
if (!tok || tok->type == TTYPE_NEWLINE)
error("expected file name, but got %s", t2s(tok));
if (tok->type == TTYPE_STRING) {
*name = tok->sval;
*std = false;
return;
return tok->sval;
}
if (!is_punct(tok, '<'))
error("'<' expected, but got %s", t2s(tok));
Expand All @@ -605,28 +606,28 @@ static void read_cpp_header_name(char **name, bool *std) {
break;
list_push(tokens, tok);
}
*name = join_tokens(tokens, false);
*std = true;
return;
}

static char *construct_path(char *path1, char *path2) {
if (path1[0] == '\0')
return path2;
return format("%s/%s", path1, path2);
return join_tokens(tokens, false);
}

static void read_include(void) {
char *name;
bool std;
read_cpp_header_name(&name, &std);
char *name = read_cpp_header_name(&std);
expect_newline();
List *paths = std ? std_include_path : make_list1("");
List *paths;
if (std) {
paths = std_include_path;
} else if (get_current_file()) {
char *buf = format("%s", get_current_file());
paths = make_list1(dirname(buf));
} else {
paths = make_list1(".");
}
for (Iter *i = list_iter(paths); !iter_end(i);) {
char *path = construct_path(iter_next(i), name);
char *path = format("%s/%s", iter_next(i), name);
FILE *fp = fopen(path, "r");
if (fp) {
push_input_file(path, fp);
push_input_file(path, path, fp);
return;
}
}
Expand Down
33 changes: 17 additions & 16 deletions lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
static bool at_bol = true;

typedef struct {
char *name;
char *displayname;
char *realname;
int line;
int column;
FILE *fp;
Expand All @@ -24,9 +25,10 @@ static Token *space_token = &(Token){ .type = TTYPE_SPACE, .space = false };

static void skip_block_comment(void);

static File *make_file(char *name, FILE *fp) {
static File *make_file(char *displayname, char *realname, FILE *fp) {
File *r = malloc(sizeof(File));
r->name = name;
r->displayname = displayname;
r->realname = realname;
r->line = 1;
r->column = 0;
r->fp = fp;
Expand All @@ -35,7 +37,7 @@ static File *make_file(char *name, FILE *fp) {

void lex_init(char *filename) {
if (!strcmp(filename, "-")) {
set_input_file("(stdin)", stdin);
set_input_file("(stdin)", NULL, stdin);
return;
}
FILE *fp = fopen(filename, "r");
Expand All @@ -44,14 +46,14 @@ void lex_init(char *filename) {
strerror_r(errno, buf, sizeof(buf));
error("Cannot open %s: %s", filename, buf);
}
set_input_file(filename, fp);
set_input_file(filename, filename, fp);
}

static Token *make_token(Token *tmpl) {
Token *r = malloc(sizeof(Token));
*r = *tmpl;
r->hideset = make_dict(NULL);
r->file = file->name;
r->file = file->displayname;
r->line = file->line;
r->column = file->column;
return r;
Expand All @@ -77,23 +79,23 @@ static Token *make_char(char c) {
return make_token(&(Token){ TTYPE_CHAR, .c = c });
}

void push_input_file(char *filename, FILE *fp) {
void push_input_file(char *displayname, char *realname, FILE *fp) {
list_push(file_stack, file);
file = make_file(filename, fp);
file = make_file(displayname, realname, fp);
at_bol = true;
}

void set_input_file(char *filename, FILE *fp) {
file = make_file(filename, fp);
void set_input_file(char *displayname, char *realname, FILE *fp) {
file = make_file(displayname, realname, fp);
at_bol = true;
}

char *input_position(void) {
return format("%s:%d:%d", file->name, file->line, file->column);
return format("%s:%d:%d", file->displayname, file->line, file->column);
}

char *get_current_file(void) {
return file->name;
return file->realname;
}

int get_current_line(void) {
Expand Down Expand Up @@ -441,7 +443,7 @@ static Token *read_token_int(void) {
}
}

bool read_header_file_name(char **name, bool *std) {
char *read_header_file_name(bool *std) {
skip_space();
char close;
int c = get();
Expand All @@ -453,7 +455,7 @@ bool read_header_file_name(char **name, bool *std) {
close = '>';
} else {
unget(c);
return false;
return NULL;
}
String *s = make_string();
for (;;) {
Expand All @@ -466,8 +468,7 @@ bool read_header_file_name(char **name, bool *std) {
}
if (get_cstring(s)[0] == '\0')
error("header name should not be empty");
*name = get_cstring(s);
return true;
return get_cstring(s);
}

bool is_punct(Token *tok, int c) {
Expand Down
2 changes: 1 addition & 1 deletion test/arith.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void test_basic(void) {
expect(0, 0);
Expand Down
2 changes: 1 addition & 1 deletion test/array.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void t1(void) {
int a[2][3];
Expand Down
2 changes: 1 addition & 1 deletion test/assign.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void testmain(void) {
print("compound assignment");
Expand Down
2 changes: 1 addition & 1 deletion test/bitop.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void test_or(void) {
expect(3, 1 | 2);
Expand Down
2 changes: 1 addition & 1 deletion test/cast.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void expectf(float a, float b);
void expectd(double a, double b);
Expand Down
2 changes: 1 addition & 1 deletion test/comp.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void testmain(void) {
print("comparison operators");
Expand Down
2 changes: 1 addition & 1 deletion test/control.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

int test_if1(void) { if (1) { return 'a';} return 0; }
int test_if2(void) { if (0) { return 0;} return 'b'; }
Expand Down
2 changes: 1 addition & 1 deletion test/decl.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void t1(void) {
int a = 1;
Expand Down
2 changes: 1 addition & 1 deletion test/enum.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

enum { g1, g2, g3 } global1;

Expand Down
2 changes: 1 addition & 1 deletion test/extern.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

extern int expect(int, int);
extern int externvar1;
Expand Down
2 changes: 1 addition & 1 deletion test/float.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

float tf1(float a) { return a; }
float tf2(double a) { return a; }
Expand Down
2 changes: 1 addition & 1 deletion test/function.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

int t1(void) {
return 77;
Expand Down
2 changes: 1 addition & 1 deletion test/global.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

int val = 21;
int a1[3];
Expand Down
2 changes: 1 addition & 1 deletion test/initializer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void verify(int *expected, int *got, int len) {
for (int i = 0; i < len; i++)
Expand Down
2 changes: 1 addition & 1 deletion test/int.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void expects(short a, short b) {
if (!(a == b)) {
Expand Down
2 changes: 1 addition & 1 deletion test/literal.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

static void test_char(void) {
expect(65, 'A');
Expand Down
6 changes: 3 additions & 3 deletions test/macro.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void special(void) {
expect_string("test/macro.c", __FILE__);
Expand All @@ -8,10 +8,10 @@ void special(void) {
}

void include(void) {
#include "test/macro1.h"
#include "macro1.h"
expect_string("macro1", MACRO_1);

#define MACRO_2_FILE "test/macro2.h"
#define MACRO_2_FILE "macro2.h"
#include MACRO_2_FILE
expect_string("macro2", MACRO_2);

Expand Down
2 changes: 1 addition & 1 deletion test/number.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void testmain(void) {
print("numeric constants");
Expand Down
2 changes: 1 addition & 1 deletion test/pointer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void t1(void) {
int a = 61;
Expand Down
2 changes: 1 addition & 1 deletion test/scope.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void testmain(void) {
print("scope");
Expand Down
2 changes: 1 addition & 1 deletion test/sizeof.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"
#include <stdbool.h>

void testmain(void) {
Expand Down
2 changes: 1 addition & 1 deletion test/struct.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void t1(void) {
struct { int a; } x;
Expand Down
2 changes: 1 addition & 1 deletion test/test.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdio.h>

void exit(int);
int strlen(char *);
size_t strlen(const char *);

extern int externvar1;
extern int externvar2;
Expand Down
2 changes: 1 addition & 1 deletion test/type.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"
#include <stdbool.h>

void test_type(void) {
Expand Down
2 changes: 1 addition & 1 deletion test/union.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "test/test.h"
#include "test.h"

void t1(void) {
union { int a; int b; } x;
Expand Down
2 changes: 1 addition & 1 deletion test/varargs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <stdarg.h>
#include "test/test.h"
#include "test.h"

char buf[100];

Expand Down

0 comments on commit b38faf8

Please sign in to comment.