30 lines
1.4 KiB
Plaintext
30 lines
1.4 KiB
Plaintext
|
|
using selfhost.shared.common.string_helpers as Str
|
||
|
|
|
||
|
|
static box RuleDeadMethodsBox {
|
||
|
|
// IR expects: methods(Array<String>), calls(Array<Map{from,to}>), entrypoints(Array<String>)
|
||
|
|
apply_ir(ir, path, out) {
|
||
|
|
local methods = ir.get("methods"); if methods == null { return }
|
||
|
|
local calls = ir.get("calls"); if calls == null { return }
|
||
|
|
local eps = ir.get("entrypoints"); if eps == null { eps = new ArrayBox() }
|
||
|
|
// build graph
|
||
|
|
local adj = new MapBox()
|
||
|
|
local i = 0; while i < methods.size() { adj.set(methods.get(i), new ArrayBox()); i = i + 1 }
|
||
|
|
i = 0; while i < calls.size() { local c=calls.get(i); local f=c.get("from"); local t=c.get("to"); if adj.has(f)==1 { adj.get(f).push(t) }; i = i + 1 }
|
||
|
|
// DFS from entrypoints
|
||
|
|
local seen = new MapBox();
|
||
|
|
local j = 0; while j < eps.size() { me._dfs(adj, eps.get(j), seen); j = j + 1 }
|
||
|
|
// report dead = methods not seen
|
||
|
|
i = 0; while i < methods.size() { local m=methods.get(i); if seen.has(m)==0 { out.push("[HC011] unreachable method (dead code): " + path + " :: " + m) }; i = i + 1 }
|
||
|
|
}
|
||
|
|
_dfs(adj, node, seen) {
|
||
|
|
if node == null { return }
|
||
|
|
if seen.has(node) == 1 { return }
|
||
|
|
seen.set(node, 1)
|
||
|
|
if adj.has(node) == 0 { return }
|
||
|
|
local arr = adj.get(node)
|
||
|
|
local k = 0; while k < arr.size() { me._dfs(adj, arr.get(k), seen); k = k + 1 }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
static box RuleDeadMethodsMain { method main(args) { return 0 } }
|