scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit b3f2f478c75b4649c5bdb99c0d23b681ec2f18fe
parent 43dd1d697192c9831ef353f9932884f88c0e6f4c
Author: Quentin Rameau <quinq@fifth.space>
Date:   Mon, 27 Jun 2016 15:22:30 +0200

[cc1] move file open handling from ilex to addinput

Diffstat:
Mcc1/cc1.h | 2+-
Mcc1/lex.c | 23++++++++---------------
Mcc1/main.c | 6+++++-
3 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -383,7 +383,7 @@ extern void expect(unsigned tok); extern void discard(void); extern int addinput(char *fname); extern void setsafe(int type); -extern void ilex(char *fname); +extern void ilex(void); #define accept(t) ((yytoken == (t)) ? next() : 0) /* code.c */ diff --git a/cc1/lex.c b/cc1/lex.c @@ -38,7 +38,7 @@ allocinput(char *fname, FILE *fp) } void -ilex(char *fname) +ilex(void) { static struct keyword keys[] = { {"auto", SCLASS, AUTO}, @@ -78,18 +78,6 @@ ilex(char *fname) {"while", WHILE, WHILE}, {NULL, 0, 0}, }; - FILE *fp; - - if (!fname) { - fp = stdin; - fname = "<stdin>"; - } else { - if ((fp = fopen(fname, "r")) == NULL) { - die("error: failed to open input file '%s': %s", - fname, strerror(errno)); - } - } - allocinput(fname, fp); keywords(keys, NS_KEYWORD); } @@ -98,8 +86,13 @@ addinput(char *fname) { FILE *fp; - if ((fp = fopen(fname, "r")) == NULL) - return 0; + if (fname) { + if ((fp = fopen(fname, "r")) == NULL) + return 0; + } else { + fp = stdin; + fname = "<stdin>"; + } allocinput(fname, fp); return 1; } diff --git a/cc1/main.c b/cc1/main.c @@ -46,6 +46,7 @@ main(int argc, char *argv[]) atexit(clean); icpp(); + ilex(); /* if run as cpp, only run the preprocessor */ name = (cp = strrchr(*argv, '/')) ? cp + 1 : *argv; @@ -88,7 +89,10 @@ main(int argc, char *argv[]) for (i = 0; i < uflags.n; ++i) undefmacro(uflags.s[i]); - ilex(*argv); + if (!addinput(*argv)) { + die("error: failed to open input file '%s': %s", + *argv, strerror(errno)); + } if (onlycpp) { outcpp(); } else {