scc

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

commit 195cc1977d51217abaf20467714bc78c320221cd
parent a73e9f76d9fd0691add07e42337884ee4a57cbef
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 19 Jul 2015 11:26:34 +0200

Warn when a local symbol is defined but not used

Diffstat:
Mcc1/cc1.h | 19++++++++++---------
Mcc1/expr.c | 1+
Mcc1/stmt.c | 1+
Mcc1/symbol.c | 6++++--
4 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -34,7 +34,7 @@ struct symbol { unsigned char ctx; unsigned char ns; unsigned char token; - char flags; + short flags; union { int i; char *s; @@ -111,14 +111,15 @@ enum { /* symbol flags */ enum { - ISGLOBAL = 1, - ISSTATIC = 2, - ISAUTO = 4, - ISREGISTER = 8, - ISDEFINED = 16, - ISFIELD = 32, - ISPARAM = 64, - ISEXTERN =128 + ISGLOBAL = 1, + ISSTATIC = 2, + ISAUTO = 4, + ISREGISTER = 8, + ISDEFINED = 16, + ISFIELD = 32, + ISPARAM = 64, + ISEXTERN = 128, + ISUSED = 256 }; diff --git a/cc1/expr.c b/cc1/expr.c @@ -473,6 +473,7 @@ primary(void) yylval.sym->flags |= ISDEFINED; error("'%s' undeclared", yytext); } + yylval.sym->flags |= ISUSED; np = varnode(yylval.sym); next(); break; diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -201,6 +201,7 @@ Goto(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) next(); if (yytoken != IDEN) unexpected(); + yylval.sym->flags |= ISUSED; emit(OJUMP, yylval.sym); next(); expect(';'); diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -71,7 +71,6 @@ popctx(void) goto save_symbol; if (sym->flags & ISDEFINED) break; - /* TODO: check if the label was used */ printerr("label '%s' is not defined", sym->name); break; case NS_CPP: @@ -87,8 +86,11 @@ popctx(void) sym->type->defined = 0; break; } - if (sym->name) + if (sym->name) { htab[hash(sym->name)] = sym->hash; + if ((sym->flags & (ISUSED|ISGLOBAL)) == 0) + warn("'%s' defined but not used", sym->name); + } free(sym->name); free(sym); }