unused local variable detection

This commit is contained in:
Bill Currie 2001-09-22 23:41:47 +00:00
parent bb6796247c
commit 7aec6e253a
4 changed files with 45 additions and 14 deletions

View file

@ -290,6 +290,10 @@ typedef struct def_s {
int initialized;// 1 when a declaration included "= immediate"
statref_t *refs; // for relocations
int used; // unused local detection
string_t file; // source file
int line; // source line
int users; // ref counted temps
struct expr_s *expr; // temp expr using this def
@ -464,7 +468,7 @@ void PR_FreeLocation (def_t *def);
def_t *PR_GetTempDef (type_t *type, def_t *scope);
void PR_FreeTempDefs ();
void PR_ResetTempDefs ();
void PR_FlushScope (def_t *scope);
void PR_FlushScope (def_t *scope, int force_used);
void PR_PrintDefs (void);
void PR_PrintFunction (def_t *def);

View file

@ -50,12 +50,18 @@ emit_statement (int sline, opcode_t *op, def_t *var_a, def_t *var_b, def_t *var_
statement->c = var_c->ofs;
ret = var_c;
}
if (var_a)
if (var_a) {
var_a->users--;
if (var_b)
var_a->used = 1;
}
if (var_b) {
var_b->users--;
if (var_c)
var_a->used = 1;
}
if (var_c) {
var_c->users--;
var_a->used = 1;
}
PR_AddStatementRef (var_a, statement, 0);
PR_AddStatementRef (var_b, statement, 1);
PR_AddStatementRef (var_c, statement, 2);

View file

@ -91,14 +91,19 @@ PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate)
.origin can be accessed as .origin_x, .origin_y, and .origin_z
*/
if (type->type == ev_vector) {
def_t *d;
sprintf (element, "%s_x", name);
PR_GetDef (&type_float, element, scope, allocate);
d = PR_GetDef (&type_float, element, scope, allocate);
d->used = 1;
sprintf (element, "%s_y", name);
PR_GetDef (&type_float, element, scope, allocate);
d = PR_GetDef (&type_float, element, scope, allocate);
d->used = 1;
sprintf (element, "%s_z", name);
PR_GetDef (&type_float, element, scope, allocate);
d = PR_GetDef (&type_float, element, scope, allocate);
d->used = 1;
} else {
*allocate += type_size[type->type];
}
@ -107,14 +112,19 @@ PR_GetDef (type_t *type, const char *name, def_t *scope, int *allocate)
*(int *) &pr_globals[def->ofs] = pr.size_fields;
if (type->aux_type->type == ev_vector) {
def_t *d;
sprintf (element, "%s_x", name);
PR_GetDef (&type_floatfield, element, scope, allocate);
d = PR_GetDef (&type_floatfield, element, scope, allocate);
d->used = 1; // always `used'
sprintf (element, "%s_y", name);
PR_GetDef (&type_floatfield, element, scope, allocate);
d = PR_GetDef (&type_floatfield, element, scope, allocate);
d->used = 1; // always `used'
sprintf (element, "%s_z", name);
PR_GetDef (&type_floatfield, element, scope, allocate);
d = PR_GetDef (&type_floatfield, element, scope, allocate);
d->used = 1; // always `used'
} else {
pr.size_fields += type_size[type->aux_type->type];
}
@ -145,6 +155,9 @@ PR_NewDef (type_t *type, const char *name, def_t *scope)
def->scope = scope;
def->file = s_file;
def->line = pr_source_line;
return def;
}
@ -246,12 +259,19 @@ PR_ResetTempDefs (void)
}
void
PR_FlushScope (def_t *scope)
PR_FlushScope (def_t *scope, int force_used)
{
def_t *def;
for (def = scope->scope_next; def; def = def->scope_next) {
if (def->name)
if (def->name) {
if (!force_used && !def->used) {
expr_t e;
e.line = def->line;
e.file = def->file;
warning (&e, "variable %s unused", def->name);
}
Hash_Del (defs_by_name, def->name);
}
}
}

View file

@ -143,7 +143,7 @@ maybe_func
}
param_list
{
PR_FlushScope (&param_scope);
PR_FlushScope (&param_scope, 1);
current_type = $<scope>2.type;
param_scope.scope_next = $<scope>2.pscope;
pr_scope = $<scope>2.scope;
@ -580,6 +580,7 @@ build_scope (function_t *f, def_t *func)
f->parm_ofs[i] = def->ofs;
if (i > 0 && f->parm_ofs[i] < f->parm_ofs[i - 1])
Error ("bad parm order");
def->used = 1; // don't warn for unused params
}
}
@ -660,7 +661,7 @@ emit_function (function_t *f, expr_t *e)
e = e->next;
}
emit_statement (pr_source_line, op_done, 0, 0, 0);
PR_FlushScope (pr_scope);
PR_FlushScope (pr_scope, 0);
pr_scope = 0;
PR_ResetTempDefs ();