Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unused variable warning #443

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/aoc2023/day05/part2.jou
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class Map:
for i = 0; i < self->ntriples; i++:
dest_start = self->triples[i][0]
source_start = self->triples[i][1]
range_length = self->triples[i][2]

# Solve equation: output = n - source_start + dest_start
n = output + source_start - dest_start
Expand Down
1 change: 1 addition & 0 deletions self_hosted/runs_wrong.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ tests/should_succeed/if_WINDOWS_at_runtime.jou
tests/should_succeed/return_none.jou
tests/syntax_error/assign_to_None.jou
tests/syntax_error/None_as_value.jou
tests/should_succeed/unused_variable.jou
23 changes: 19 additions & 4 deletions src/simplify_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,25 +435,40 @@ static void remove_unreachable_blocks(CfGraph *cfg)

static void remove_unused_variables(CfGraph *cfg)
{
enum { READ=1, WRITE=2 };
char *used = calloc(1, cfg->locals.len);
Location *write_locations = malloc(sizeof(write_locations[0]) * cfg->locals.len);

for (CfBlock **b = cfg->all_blocks.ptr; b < End(cfg->all_blocks); b++) {
for (CfInstruction *ins = (*b)->instructions.ptr; ins < End((*b)->instructions); ins++) {
if (ins->destvar)
used[find_var_index(cfg, ins->destvar)] = true;
if (ins->destvar) {
used[find_var_index(cfg, ins->destvar)] |= WRITE;
write_locations[find_var_index(cfg, ins->destvar)] = ins->location;
}
for (int i = 0; i < ins->noperands; i++)
used[find_var_index(cfg, ins->operands[i])] = true;
used[find_var_index(cfg, ins->operands[i])] |= READ;
}
}

for (int i = 0; i < cfg->locals.len; i++) {
if (
used[i] == WRITE
&& cfg->locals.ptr[i]->name[0] != '\0'
&& strcmp(cfg->locals.ptr[i]->name, "return") != 0
) {
show_warning(write_locations[i], "variable '%s' is never used", cfg->locals.ptr[i]->name);
}
}

for (int i = cfg->locals.len - 1; i>=0; i--) {
if (!used[i] && !cfg->locals.ptr[i]->is_argument) {
if (used[i] == 0 && !cfg->locals.ptr[i]->is_argument) {
free(cfg->locals.ptr[i]);
cfg->locals.ptr[i] = Pop(&cfg->locals);
}
}

free(used);
free(write_locations);
}

static void warn_about_undefined_variables(CfGraph *cfg)
Expand Down
3 changes: 3 additions & 0 deletions tests/should_succeed/and_or_not.jou
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,7 @@ def main() -> int:
result = not side_effect('b', False)
printf("\n")

# prevent unused variable warnings
assert result

return 0
6 changes: 6 additions & 0 deletions tests/should_succeed/unused_import.jou
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ def main() -> int:
x = stdout
y = strcmp("foo", "bar")
z = Point{x=1, y=2}

# prevent unused variable warnings
assert x != NULL
assert y != 0
assert z.x != 0

return 0
3 changes: 3 additions & 0 deletions tests/should_succeed/unused_variable.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def main() -> int:
x = 4 # Warning: variable 'x' is never used
return 0