Open epatrizio opened 1 month ago
I've tried to implement this by adding a usage counter to the variables in the environment :
type ('a, 'b) t =
{
(* ... *)
; globals : (string * int) SMap.t
; locals : (string * int) SMap.t
(* ... *)
}
let add_global n env =
(* ... *)
let globals = SMap.add n (fresh_name, 0) env.globals in
(* ... *)
let add_local n env =
(* ... *)
let locals = SMap.add n (fresh_name, 0) env.locals in
(* ... *)
let get_name n env =
(* ... *)
| Some (name, nb) ->
let locals = SMap.add n (name, nb + 1) env.locals in
(* ... *)
| None -> (
(* ... *)
| Some (name, nb) ->
let globals = SMap.add n (name, nb + 1) env.globals in
(* ... *)
Then, during the scope analysis, detecting null counters is very tricky for example with local scopes and nested blocks :
(* .mll file *)
let z = false;
let y = 0 in
let x = 42 in
if z then 0 else (x+y)
Warning: unused local variable y
Warning: unused global variable z
Set up a static analysis pass before compilation. Example: unused variables. Experimental.