emit warnings for unused vars

This commit is contained in:
2026-02-20 20:03:12 -06:00
parent bb8536f6c9
commit 3e7b3e9994
2 changed files with 56 additions and 3 deletions

46
fold.cm
View File

@@ -4,6 +4,7 @@
var fold = function(ast) { var fold = function(ast) {
var scopes = ast.scopes var scopes = ast.scopes
var nr_scopes = length(scopes) var nr_scopes = length(scopes)
ast._diagnostics = []
var type_tag_map = { var type_tag_map = {
array: "array", record: "record", text: "text", array: "array", record: "record", text: "text",
@@ -701,8 +702,30 @@ var fold = function(ast) {
name = stmt.left.name name = stmt.left.name
if (name != null) { if (name != null) {
sv = scope_var(fn_nr, name) sv = scope_var(fn_nr, name)
if (sv != null && sv.nr_uses == 0 && is_pure(stmt.right)) { if (sv != null && sv.nr_uses == 0) {
stmt.dead = true if (is_pure(stmt.right)) stmt.dead = true
if (stmt.right != null && stmt.right.kind == "(" && stmt.right.expression != null && stmt.right.expression.name == "use") {
push(ast._diagnostics, {
severity: "warning",
line: stmt.left.from_row + 1,
col: stmt.left.from_column + 1,
message: `unused import '${name}'`
})
} else if (stmt.kind == "def") {
push(ast._diagnostics, {
severity: "warning",
line: stmt.left.from_row + 1,
col: stmt.left.from_column + 1,
message: `unused constant '${name}'`
})
} else {
push(ast._diagnostics, {
severity: "warning",
line: stmt.left.from_row + 1,
col: stmt.left.from_column + 1,
message: `unused variable '${name}'`
})
}
} }
} }
} }
@@ -715,6 +738,12 @@ var fold = function(ast) {
sv = scope_var(fn_nr, stmt.name) sv = scope_var(fn_nr, stmt.name)
if (sv != null && sv.nr_uses == 0) { if (sv != null && sv.nr_uses == 0) {
stmt.dead = true stmt.dead = true
push(ast._diagnostics, {
severity: "warning",
line: stmt.from_row + 1,
col: stmt.from_column + 1,
message: `unused function '${stmt.name}'`
})
} }
} }
if (stmt.dead != true) push(out, stmt) if (stmt.dead != true) push(out, stmt)
@@ -1028,9 +1057,22 @@ var fold = function(ast) {
// Remove dead top-level functions // Remove dead top-level functions
var live_fns = [] var live_fns = []
var fn = null var fn = null
var fn_sv = null
fi = 0 fi = 0
while (fi < length(ast.functions)) { while (fi < length(ast.functions)) {
fn = ast.functions[fi] fn = ast.functions[fi]
if (fn.name != null) {
fn_sv = scope_var(0, fn.name)
if (fn_sv != null && fn_sv.nr_uses == 0) {
fn.dead = true
push(ast._diagnostics, {
severity: "warning",
line: fn.from_row + 1,
col: fn.from_column + 1,
message: `unused function '${fn.name}'`
})
}
}
if (fn.dead != true) { if (fn.dead != true) {
push(live_fns, fn) push(live_fns, fn)
} }

View File

@@ -158,6 +158,7 @@ function analyze(src, filename) {
var line = null var line = null
var col = null var col = null
var has_errors = _ast.errors != null && length(_ast.errors) > 0 var has_errors = _ast.errors != null && length(_ast.errors) > 0
var folded = null
if (has_errors) { if (has_errors) {
while (_i < length(_ast.errors)) { while (_i < length(_ast.errors)) {
e = _ast.errors[_i] e = _ast.errors[_i]
@@ -176,7 +177,17 @@ function analyze(src, filename) {
} }
disrupt disrupt
} }
return fold_mod(_ast) folded = fold_mod(_ast)
if (!_no_warn && folded._diagnostics != null && length(folded._diagnostics) > 0) {
_i = 0
while (_i < length(folded._diagnostics)) {
e = folded._diagnostics[_i]
os.print(`${filename}:${text(e.line)}:${text(e.col)}: ${e.severity}: ${e.message}\n`)
_i = _i + 1
}
}
folded._diagnostics = null
return folded
} }
// Lazy-loaded verify_ir module (loaded on first use) // Lazy-loaded verify_ir module (loaded on first use)