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" int initialized;// 1 when a declaration included "= immediate"
statref_t *refs; // for relocations statref_t *refs; // for relocations
int used; // unused local detection
string_t file; // source file
int line; // source line
int users; // ref counted temps int users; // ref counted temps
struct expr_s *expr; // temp expr using this def 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); def_t *PR_GetTempDef (type_t *type, def_t *scope);
void PR_FreeTempDefs (); void PR_FreeTempDefs ();
void PR_ResetTempDefs (); void PR_ResetTempDefs ();
void PR_FlushScope (def_t *scope); void PR_FlushScope (def_t *scope, int force_used);
void PR_PrintDefs (void); void PR_PrintDefs (void);
void PR_PrintFunction (def_t *def); 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; statement->c = var_c->ofs;
ret = var_c; ret = var_c;
} }
if (var_a) if (var_a) {
var_a->users--; var_a->users--;
if (var_b) var_a->used = 1;
}
if (var_b) {
var_b->users--; var_b->users--;
if (var_c) var_a->used = 1;
}
if (var_c) {
var_c->users--; var_c->users--;
var_a->used = 1;
}
PR_AddStatementRef (var_a, statement, 0); PR_AddStatementRef (var_a, statement, 0);
PR_AddStatementRef (var_b, statement, 1); PR_AddStatementRef (var_b, statement, 1);
PR_AddStatementRef (var_c, statement, 2); 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 .origin can be accessed as .origin_x, .origin_y, and .origin_z
*/ */
if (type->type == ev_vector) { if (type->type == ev_vector) {
def_t *d;
sprintf (element, "%s_x", name); 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); 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); sprintf (element, "%s_z", name);
PR_GetDef (&type_float, element, scope, allocate); d = PR_GetDef (&type_float, element, scope, allocate);
d->used = 1;
} else { } else {
*allocate += type_size[type->type]; *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; *(int *) &pr_globals[def->ofs] = pr.size_fields;
if (type->aux_type->type == ev_vector) { if (type->aux_type->type == ev_vector) {
def_t *d;
sprintf (element, "%s_x", name); 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); 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); 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 { } else {
pr.size_fields += type_size[type->aux_type->type]; 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->scope = scope;
def->file = s_file;
def->line = pr_source_line;
return def; return def;
} }
@ -246,12 +259,19 @@ PR_ResetTempDefs (void)
} }
void void
PR_FlushScope (def_t *scope) PR_FlushScope (def_t *scope, int force_used)
{ {
def_t *def; def_t *def;
for (def = scope->scope_next; def; def = def->scope_next) { 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); Hash_Del (defs_by_name, def->name);
}
} }
} }

View file

@ -143,7 +143,7 @@ maybe_func
} }
param_list param_list
{ {
PR_FlushScope (&param_scope); PR_FlushScope (&param_scope, 1);
current_type = $<scope>2.type; current_type = $<scope>2.type;
param_scope.scope_next = $<scope>2.pscope; param_scope.scope_next = $<scope>2.pscope;
pr_scope = $<scope>2.scope; pr_scope = $<scope>2.scope;
@ -580,6 +580,7 @@ build_scope (function_t *f, def_t *func)
f->parm_ofs[i] = def->ofs; f->parm_ofs[i] = def->ofs;
if (i > 0 && f->parm_ofs[i] < f->parm_ofs[i - 1]) if (i > 0 && f->parm_ofs[i] < f->parm_ofs[i - 1])
Error ("bad parm order"); 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; e = e->next;
} }
emit_statement (pr_source_line, op_done, 0, 0, 0); emit_statement (pr_source_line, op_done, 0, 0, 0);
PR_FlushScope (pr_scope); PR_FlushScope (pr_scope, 0);
pr_scope = 0; pr_scope = 0;
PR_ResetTempDefs (); PR_ResetTempDefs ();