scc

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

commit 89bb136a4e5fc5a942f34ff2d0e284c783f8b771
parent 9d9760579d87a8a5b22de85cd966ac57bfbc7e4d
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 11 Jan 2017 16:51:57 +0100

[cc1] Remove allocinput()

This function was used only to avoid some code in addinput(),
but since we added the support for macro expansion using
push up buffers we could unify both functions in only one.

Diffstat:
Mcc1/cc1.h | 1-
Mcc1/cpp.c | 5+++--
Mcc1/lex.c | 40++++++++++++++++++----------------------
3 files changed, 21 insertions(+), 25 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -395,7 +395,6 @@ extern int moreinput(void); extern void expect(unsigned tok); extern void discard(void); extern int addinput(char *fname, Symbol *hide, char *buffer); -extern void allocinput(char *fname, FILE *fp, char *line); extern void delinput(void); extern void setsafe(int type); extern void ilex(void); diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -26,13 +26,14 @@ void defdefine(char *macro, char *val, char *source) { char *def, *fmt = "#define %s %s"; + Symbol dummy = {.flags = SDECLARED}; if (!val) val = ""; def = xmalloc(strlen(fmt) + strlen(macro) + strlen(val)); sprintf(def, fmt, macro, val); - allocinput(source, NULL, def); + addinput(source, &dummy, def); input->nline = ++ncmdlines; cpp(); delinput(); @@ -268,7 +269,7 @@ expand(char *begin, Symbol *sym) substitute: DBG("MACRO '%s' expanded to :'%s'", macroname, buffer); buffer[elen] = '\0'; - addinput(NULL, sym, xstrdup(buffer)); + addinput(input->fname, sym, xstrdup(buffer)); return 1; } diff --git a/cc1/lex.c b/cc1/lex.c @@ -24,23 +24,6 @@ static int safe, eof; Input *input; void -allocinput(char *fname, FILE *fp, char *line) -{ - Input *ip = xmalloc(sizeof(Input)); - - if (!line) { - line = xmalloc(INPUTSIZ); - line[0] = '\0'; - } - ip->p = ip->begin = ip->line = line; - ip->nline = 0; - ip->fname = xstrdup(fname); - ip->next = input; - ip->fp = fp; - input = ip; -} - -void ilex(void) { static struct keyword keys[] = { @@ -90,12 +73,13 @@ addinput(char *fname, Symbol *hide, char *buffer) { FILE *fp; unsigned nline = 0; + Input *ip; if (hide) { /* this is a macro expansion */ fp = NULL; - fname = xstrdup(input->fname); - nline = input->nline; + if (input) + nline = input->nline; if (hide->hide == UCHAR_MAX) die("Too many macro expansions"); ++hide->hide; @@ -108,9 +92,21 @@ addinput(char *fname, Symbol *hide, char *buffer) fp = stdin; fname = "<stdin>"; } - allocinput(fname, fp, buffer); - input->hide = hide; - input->nline = nline; + + ip = xmalloc(sizeof(*ip)); + + if (!buffer) { + buffer = xmalloc(INPUTSIZ); + buffer[0] = '\0'; + } + + ip->p = ip->begin = ip->line = buffer; + ip->fname = xstrdup(fname); + ip->next = input; + ip->fp = fp; + ip->hide = hide; + ip->nline = nline; + input = ip; return 1; }