2
0
Эх сурвалжийг харах

generalize removable code diagnostics and use it for dead variables

see vshaxe/vshaxe#35
Simon Krajewski 9 жил өмнө
parent
commit
ba74901470

+ 18 - 4
src/display/display.ml

@@ -266,30 +266,44 @@ module Diagnostics = struct
 			| DKUnusedImport
 			| DKUnusedImport
 			| DKUnresolvedIdentifier
 			| DKUnresolvedIdentifier
 			| DKCompilerError
 			| DKCompilerError
+			| DKRemovableCode
 
 
 		let to_int = function
 		let to_int = function
 			| DKUnusedImport -> 0
 			| DKUnusedImport -> 0
 			| DKUnresolvedIdentifier -> 1
 			| DKUnresolvedIdentifier -> 1
 			| DKCompilerError -> 2
 			| DKCompilerError -> 2
+			| DKRemovableCode -> 3
 	end
 	end
 
 
 	open DiagnosticsKind
 	open DiagnosticsKind
 	open DisplayTypes
 	open DisplayTypes
 
 
+	let add_removable_code com s p prange =
+		let di = com.shared.shared_display_information in
+		di.removable_code <- (s,p,prange) :: di.removable_code
+
 	let find_unused_variables com e =
 	let find_unused_variables com e =
 		let vars = Hashtbl.create 0 in
 		let vars = Hashtbl.create 0 in
+		let pmin_map = Hashtbl.create 0 in
 		let rec loop e = match e.eexpr with
 		let rec loop e = match e.eexpr with
 			| TVar(v,eo) when Meta.has Meta.UserVariable v.v_meta ->
 			| TVar(v,eo) when Meta.has Meta.UserVariable v.v_meta ->
-				Hashtbl.replace vars v.v_id v;
-				(match eo with None -> () | Some e -> loop e)
+				Hashtbl.add pmin_map e.epos.pmin v;
+				let p = match eo with
+					| None -> e.epos
+					| Some e1 ->
+						loop e1;
+						{ e.epos with pmax = e1.epos.pmin }
+				in
+				Hashtbl.replace vars v.v_id (v,p);
 			| TLocal v when Meta.has Meta.UserVariable v.v_meta ->
 			| TLocal v when Meta.has Meta.UserVariable v.v_meta ->
 				Hashtbl.remove vars v.v_id;
 				Hashtbl.remove vars v.v_id;
 			| _ ->
 			| _ ->
 				Type.iter loop e
 				Type.iter loop e
 		in
 		in
 		loop e;
 		loop e;
-		Hashtbl.iter (fun _ v ->
-			add_diagnostics_message com "Unused variable" v.v_pos DiagnosticsSeverity.Warning
+		Hashtbl.iter (fun _ (v,p) ->
+			let p = match (Hashtbl.find_all pmin_map p.pmin) with [_] -> p | _ -> null_pos in
+			add_removable_code com "Unused variable" v.v_pos p
 		) vars
 		) vars
 
 
 	let check_other_things com e =
 	let check_other_things com e =

+ 8 - 5
src/display/displayOutput.ml

@@ -540,21 +540,24 @@ module DiagnosticsPrinter = struct
 					"name",JString s
 					"name",JString s
 				]
 				]
 			) suggestions in
 			) suggestions in
-			add DKUnresolvedIdentifier p DiagnosticsSeverity.Error (suggestions @ (find_type s));
+			add DKUnresolvedIdentifier p DiagnosticsSeverity.Error (JArray (suggestions @ (find_type s)));
 		) com.display_information.unresolved_identifiers;
 		) com.display_information.unresolved_identifiers;
 		PMap.iter (fun p (r,_) ->
 		PMap.iter (fun p (r,_) ->
-			if not !r then add DKUnusedImport p DiagnosticsSeverity.Warning []
+			if not !r then add DKUnusedImport p DiagnosticsSeverity.Warning (JArray [])
 		) com.shared.shared_display_information.import_positions;
 		) com.shared.shared_display_information.import_positions;
 		List.iter (fun (s,p,sev) ->
 		List.iter (fun (s,p,sev) ->
-			add DKCompilerError p sev [JString s]
+			add DKCompilerError p sev (JString s)
 		) com.shared.shared_display_information.diagnostics_messages;
 		) com.shared.shared_display_information.diagnostics_messages;
+		List.iter (fun (s,p,prange) ->
+			add DKRemovableCode p DiagnosticsSeverity.Warning (JObject ["description",JString s;"range",if prange = null_pos then JNull else pos_to_json_range prange])
+		) com.shared.shared_display_information.removable_code;
 		let jl = Hashtbl.fold (fun file diag acc ->
 		let jl = Hashtbl.fold (fun file diag acc ->
-			let jl = DynArray.fold_left (fun acc (dk,p,sev,args) ->
+			let jl = DynArray.fold_left (fun acc (dk,p,sev,jargs) ->
 				(JObject [
 				(JObject [
 					"kind",JInt (to_int dk);
 					"kind",JInt (to_int dk);
 					"severity",JInt (DiagnosticsSeverity.to_int sev);
 					"severity",JInt (DiagnosticsSeverity.to_int sev);
 					"range",pos_to_json_range p;
 					"range",pos_to_json_range p;
-					"args",JArray args
+					"args",jargs
 				]) :: acc
 				]) :: acc
 			) [] diag in
 			) [] diag in
 			(JObject [
 			(JObject [

+ 2 - 0
src/typing/common.ml

@@ -234,6 +234,7 @@ type shared_display_information = {
 	mutable diagnostics_messages : (string * pos * DisplayTypes.DiagnosticsSeverity.t) list;
 	mutable diagnostics_messages : (string * pos * DisplayTypes.DiagnosticsSeverity.t) list;
 	mutable type_hints : (pos,Type.t) Hashtbl.t;
 	mutable type_hints : (pos,Type.t) Hashtbl.t;
 	mutable document_symbols : (string * DisplayTypes.SymbolInformation.t DynArray.t) list;
 	mutable document_symbols : (string * DisplayTypes.SymbolInformation.t DynArray.t) list;
+	mutable removable_code : (string * pos * pos) list;
 }
 }
 
 
 type display_information = {
 type display_information = {
@@ -906,6 +907,7 @@ let create version s_version args =
 				diagnostics_messages = [];
 				diagnostics_messages = [];
 				type_hints = Hashtbl.create 0;
 				type_hints = Hashtbl.create 0;
 				document_symbols = [];
 				document_symbols = [];
+				removable_code = [];
 			}
 			}
 		};
 		};
 		display_information = {
 		display_information = {