From b779c6f7d73e5f70b2d886ed75e6e65217ad1e85 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 19 May 2020 19:12:10 -0700 Subject: checkin: found a large bug associated to ident resetting the buffer vector --- sys/cmd/cc/pp.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'sys/cmd/cc/pp.c') diff --git a/sys/cmd/cc/pp.c b/sys/cmd/cc/pp.c index 2aa1291..264f11a 100644 --- a/sys/cmd/cc/pp.c +++ b/sys/cmd/cc/pp.c @@ -34,7 +34,7 @@ string identdots(Lexer *lx, int *dots) { int c; - string s; + byte *s; s = ident(lx); if (s != nil) @@ -62,7 +62,7 @@ defmacro(Lexer *lx, string name, string macro) { Sym *mac; - printf("DEFINING MACRO %s = %s, ON LINE %d\n", name, macro+2, lx->pos.line); + printf("DEFINING MACRO %s ON LINE %d, file %s\n", name, lx->pos.line, os·basename(lx->pos.path)); mac = define(&lx->sym, name, Smacro); mac->macro = macro; @@ -397,6 +397,7 @@ enum // ----------------------------------------------------------------------- // preprocessor functions +/* #endif */ static error ppend(Lexer *lx) @@ -426,10 +427,9 @@ ppund(Lexer *lx) printf("FORGETTING %s\n", s); err = forget(&lx->sym, s); - if (err) { - errorat(lx->pos, "attempting to undefine unrecognized symbol '%s'", s); - return err; - } + if (err) + warnat(lx->pos, "attempting to undefine unrecognized symbol '%s'", s); + ppend(lx); return 0; } @@ -442,7 +442,7 @@ ppdef(Lexer *lx) int b; Sym *sym; int i, j, f, n, dot; - string s, a, buf, args[PPnarg]; + string s, a, base, buf, args[PPnarg]; s = ident(lx); if (!s) { @@ -452,11 +452,8 @@ ppdef(Lexer *lx) intern(&s); sym = lookup(&lx->sym, s); - if (sym) { - // TODO: should be a warning... - errorat(lx->pos, "macro redefined: '%s'", sym->name); - // goto Bad; - } + if (sym) + warnat(lx->pos, "macro redefined: '%s'", sym->name); n = 0; dot = 0; @@ -494,10 +491,10 @@ ppdef(Lexer *lx) if (b != '\n') b = getnsbyte(lx); - buf = str·makef("%c%c", n, PPbeg); + base = lx->b; + buf = str·makef("%c%c", n, PPbeg); for (;;) { if (isalpha(b) || b == '_') { - lx->b = lx->buf; *lx->b++ = b; b = getbyte(lx); @@ -505,7 +502,7 @@ ppdef(Lexer *lx) *lx->b++ = b; b = getbyte(lx); } - *lx->b = '\0'; + *lx->b++ = '\0'; for (i = 0; i < n; i++) { if (strcmp(lx->buf, args[i]) == 0) { @@ -999,6 +996,10 @@ Skip: lx->pos.line++; b = 1; } + if (c == EOF) { + errorat(lx->pos, "EOF hit while skipping if block. Missing endif"); + goto Bad; + } continue; } if (!b) @@ -1007,7 +1008,7 @@ Skip: if (!s) continue; - if ((strcmp(s, "elif") == 0) && l == 0) + if (l == 0 && (strcmp(s, "elif") == 0)) ppif(lx, 0); if (strcmp(s, "endif") == 0) { @@ -1019,7 +1020,6 @@ Skip: return 0; } if (strcmp(s, "if") == 0 || - strcmp(s, "elif") == 0 || strcmp(s, "ifdef") == 0 || strcmp(s, "ifndef") == 0) { l++; -- cgit v1.2.1