diff --git a/fold.cm b/fold.cm index a686c844..40592713 100644 --- a/fold.cm +++ b/fold.cm @@ -4,6 +4,7 @@ var fold = function(ast) { var scopes = ast.scopes var nr_scopes = length(scopes) + ast._diagnostics = [] var type_tag_map = { array: "array", record: "record", text: "text", @@ -701,8 +702,30 @@ var fold = function(ast) { name = stmt.left.name if (name != null) { sv = scope_var(fn_nr, name) - if (sv != null && sv.nr_uses == 0 && is_pure(stmt.right)) { - stmt.dead = true + if (sv != null && sv.nr_uses == 0) { + 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) if (sv != null && sv.nr_uses == 0) { 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) @@ -1028,9 +1057,22 @@ var fold = function(ast) { // Remove dead top-level functions var live_fns = [] var fn = null + var fn_sv = null fi = 0 while (fi < length(ast.functions)) { 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) { push(live_fns, fn) } diff --git a/internal/engine.cm b/internal/engine.cm index ff384479..009172b6 100644 --- a/internal/engine.cm +++ b/internal/engine.cm @@ -158,6 +158,7 @@ function analyze(src, filename) { var line = null var col = null var has_errors = _ast.errors != null && length(_ast.errors) > 0 + var folded = null if (has_errors) { while (_i < length(_ast.errors)) { e = _ast.errors[_i] @@ -176,7 +177,17 @@ function analyze(src, filename) { } 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)