ref: 195cc1977d51217abaf20467714bc78c320221cd
parent: a73e9f76d9fd0691add07e42337884ee4a57cbef
author: Roberto E. Vargas Caballero <[email protected]>
date: Sun Jul 19 07:26:34 EDT 2015
Warn when a local symbol is defined but not used
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -34,7 +34,7 @@
unsigned char ctx;
unsigned char ns;
unsigned char token;
- char flags;
+ short flags;
union {
int i;
char *s;
@@ -111,14 +111,15 @@
/* 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
};
--- a/cc1/expr.c
+++ b/cc1/expr.c
@@ -473,6 +473,7 @@
yylval.sym->flags |= ISDEFINED;
error("'%s' undeclared", yytext);
}
+ yylval.sym->flags |= ISUSED;
np = varnode(yylval.sym);
next();
break;
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
@@ -201,6 +201,7 @@
next();
if (yytoken != IDEN)
unexpected();
+ yylval.sym->flags |= ISUSED;
emit(OJUMP, yylval.sym);
next();
expect(';');
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -71,7 +71,6 @@
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 @@
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);
}