[hackers] [scc] Warn when a local symbol is defined but not used || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Sun, 19 Jul 2015 11:30:43 +0200 (CEST)

commit 195cc1977d51217abaf20467714bc78c320221cd
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Sun Jul 19 11:26:34 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Sun Jul 19 11:26:34 2015 +0200

    Warn when a local symbol is defined but not used

diff --git a/cc1/cc1.h b/cc1/cc1.h
index c44d3dc..988ad4a 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -34,7 +34,7 @@ struct symbol {
         unsigned char ctx;
         unsigned char ns;
         unsigned char token;
- char flags;
+ short flags;
         union {
                 int i;
                 char *s;
_AT_@ -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
index be3840f..2354771 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -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
index a23687c..a2bf282 100644
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
_AT_@ -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
index f609e13..183b552 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -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:
_AT_@ -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);
         }
Received on Sun Jul 19 2015 - 11:30:43 CEST

This archive was generated by hypermail 2.3.0 : Sun Jul 19 2015 - 11:36:11 CEST