Browse Source

[display] refactor diagnostics to support multiple files

see #9134
Simon Krajewski 5 years ago
parent
commit
450bd6ceec

+ 7 - 6
src/compiler/displayOutput.ml

@@ -230,11 +230,12 @@ let handle_display_argument com file_pos pre_compilation did_something =
 		(try Memory.display_memory com with e -> prerr_endline (Printexc.get_backtrace ()));
 	| "diagnostics" ->
 		Common.define com Define.NoCOpt;
-		com.display <- DisplayMode.create (DMDiagnostics true);
-		Parser.display_mode := DMDiagnostics true;
+		com.display <- DisplayMode.create (DMDiagnostics []);
+		Parser.display_mode := DMDiagnostics [];
 	| _ ->
 		let file, pos = try ExtString.String.split file_pos "@" with _ -> failwith ("Invalid format: " ^ file_pos) in
 		let file = unquote file in
+		let file_unique = Path.unique_full_path file in
 		let pos, smode = try ExtString.String.split pos "@" with _ -> pos,"" in
 		let mode = match smode with
 			| "position" ->
@@ -258,7 +259,7 @@ let handle_display_argument com file_pos pre_compilation did_something =
 				DMModuleSymbols None;
 			| "diagnostics" ->
 				Common.define com Define.NoCOpt;
-				DMDiagnostics false;
+				DMDiagnostics [file_unique];
 			| "statistics" ->
 				Common.define com Define.NoCOpt;
 				DMStatistics
@@ -283,7 +284,7 @@ let handle_display_argument com file_pos pre_compilation did_something =
 		Parser.display_mode := mode;
 		if not com.display.dms_full_typing then Common.define_value com Define.Display (if smode <> "" then smode else "1");
 		DisplayPosition.display_position#set {
-			pfile = Path.unique_full_path file;
+			pfile = file_unique;
 			pmin = pos;
 			pmax = pos;
 		}
@@ -416,8 +417,8 @@ let process_global_display_mode com tctx =
 		FindReferences.find_references tctx com with_definition
 	| DMImplementation ->
 		FindReferences.find_implementations tctx com
-	| DMDiagnostics global ->
-		Diagnostics.run com global
+	| DMDiagnostics _ ->
+		Diagnostics.run com
 	| DMStatistics ->
 		let stats = Statistics.collect_statistics tctx (SFFile (DisplayPosition.display_position#get).pfile) true in
 		raise_statistics (Statistics.Printer.print_statistics stats)

+ 6 - 6
src/compiler/haxe.ml

@@ -458,21 +458,21 @@ let process_display_configuration ctx =
 	end
 
 let run_or_diagnose com f arg =
-	let handle_diagnostics global msg p kind =
+	let handle_diagnostics msg p kind =
 		add_diagnostics_message com msg p kind DisplayTypes.DiagnosticsSeverity.Error;
-		Diagnostics.run com global;
+		Diagnostics.run com;
 	in
 	match com.display.dms_kind with
-	| DMDiagnostics global ->
+	| DMDiagnostics _ ->
 		begin try
 			f arg
 		with
 		| Error.Error(msg,p) ->
-			handle_diagnostics global (Error.error_msg msg) p DisplayTypes.DiagnosticsKind.DKCompilerError
+			handle_diagnostics (Error.error_msg msg) p DisplayTypes.DiagnosticsKind.DKCompilerError
 		| Parser.Error(msg,p) ->
-			handle_diagnostics global (Parser.error_msg msg) p DisplayTypes.DiagnosticsKind.DKParserError
+			handle_diagnostics (Parser.error_msg msg) p DisplayTypes.DiagnosticsKind.DKParserError
 		| Lexer.Error(msg,p) ->
-			handle_diagnostics global (Lexer.error_msg msg) p DisplayTypes.DiagnosticsKind.DKParserError
+			handle_diagnostics (Lexer.error_msg msg) p DisplayTypes.DiagnosticsKind.DKParserError
 		end
 	| _ ->
 		f arg

+ 2 - 2
src/compiler/server.ml

@@ -38,7 +38,7 @@ let s_version with_build =
 let check_display_flush ctx f_otherwise = match ctx.com.json_out with
 	| None ->
 		begin match ctx.com.display.dms_kind with
-		| DMDiagnostics global->
+		| DMDiagnostics _->
 			List.iter (fun msg ->
 				let msg,p,kind = match msg with
 					| CMInfo(msg,p) -> msg,p,DisplayTypes.DiagnosticsSeverity.Information
@@ -47,7 +47,7 @@ let check_display_flush ctx f_otherwise = match ctx.com.json_out with
 				in
 				add_diagnostics_message ctx.com msg p DisplayTypes.DiagnosticsKind.DKCompilerError kind
 			) (List.rev ctx.messages);
-			raise (Completion (Diagnostics.print ctx.com global))
+			raise (Completion (Diagnostics.print ctx.com))
 		| _ ->
 			f_otherwise ()
 		end

+ 8 - 12
src/context/display/diagnostics.ml

@@ -12,6 +12,8 @@ open DiagnosticsTypes
 let add_removable_code ctx s p prange =
 	ctx.removable_code <- (s,p,prange) :: ctx.removable_code
 
+let is_diagnostics_run p = DiagnosticsPrinter.is_diagnostics_file p.pfile
+
 let find_unused_variables com e =
 	let vars = Hashtbl.create 0 in
 	let pmin_map = Hashtbl.create 0 in
@@ -96,9 +98,8 @@ let prepare_field dctx com cf = match cf.cf_expr with
 		check_other_things com e;
 		DeprecationCheck.run_on_expr com e
 
-let prepare com global =
+let prepare com =
 	let dctx = {
-		global = global;
 		removable_code = [];
 		import_positions = PMap.empty;
 		dead_blocks = Hashtbl.create 0;
@@ -106,7 +107,7 @@ let prepare com global =
 		unresolved_identifiers = [];
 	} in
 	List.iter (function
-		| TClassDecl c when global || DisplayPosition.display_position#is_in_file c.cl_pos.pfile ->
+		| TClassDecl c when DiagnosticsPrinter.is_diagnostics_file c.cl_pos.pfile ->
 			List.iter (prepare_field dctx com) c.cl_ordered_fields;
 			List.iter (prepare_field dctx com) c.cl_ordered_statics;
 			(match c.cl_constructor with None -> () | Some cf -> prepare_field dctx com cf);
@@ -159,18 +160,13 @@ let prepare com global =
 	dctx.unresolved_identifiers <- com.display_information.unresolved_identifiers;
 	dctx
 
-let is_diagnostics_run p = match (!Parser.display_mode) with
-	| DMDiagnostics true -> true
-	| DMDiagnostics false -> DisplayPosition.display_position#is_in_file p.pfile
-	| _ -> false
-
 let secure_generated_code ctx e =
 	if is_diagnostics_run e.epos then mk (TMeta((Meta.Extern,[],e.epos),e)) e.etype e.epos else e
 
-let print com global =
-	let dctx = prepare com global in
+let print com =
+	let dctx = prepare com in
 	Json.string_of_json (DiagnosticsPrinter.json_of_diagnostics dctx)
 
-let run com global =
-	let dctx = prepare com global in
+let run com =
+	let dctx = prepare com in
 	DisplayException.raise_diagnostics dctx

+ 8 - 1
src/context/display/diagnosticsPrinter.ml

@@ -7,6 +7,13 @@ open DiagnosticsTypes
 
 type t = DiagnosticsKind.t * pos
 
+let is_diagnostics_file file =
+	let file = Path.unique_full_path file in
+	match (!Parser.display_mode) with
+	| DMDiagnostics [] -> true
+	| DMDiagnostics files -> List.exists (fun file' -> file = file') files
+	| _ -> false
+
 module UnresolvedIdentifierSuggestion = struct
 	type t =
 		| UISImport
@@ -36,7 +43,7 @@ let json_of_diagnostics dctx =
 			Hashtbl.add diag p (dk,p,sev,args)
 	in
 	let add dk p sev args =
-		if dctx.global || p = null_pos || DisplayPosition.display_position#is_in_file p.pfile then add dk p sev args
+		if p = null_pos || is_diagnostics_file p.pfile then add dk p sev args
 	in
 	List.iter (fun (s,p,suggestions) ->
 		let suggestions = ExtList.List.filter_map (fun (s,item,r) ->

+ 0 - 1
src/context/display/diagnosticsTypes.ml

@@ -2,7 +2,6 @@ open Globals
 open Ast
 
 type diagnostics_context = {
-	global : bool;
 	mutable removable_code : (string * pos * pos) list;
 	mutable import_positions : (pos,bool ref) PMap.t;
 	mutable dead_blocks : (string,(pos * expr) list) Hashtbl.t;

+ 5 - 5
src/core/displayTypes.ml

@@ -188,7 +188,7 @@ module DisplayMode = struct
 		| DMPackage
 		| DMHover
 		| DMModuleSymbols of string option
-		| DMDiagnostics of bool (* true = global, false = only in display file *)
+		| DMDiagnostics of string list
 		| DMStatistics
 		| DMSignature
 
@@ -258,10 +258,10 @@ module DisplayMode = struct
 				dms_force_macro_typing = false;
 				dms_per_file = true;
 			}
-		| DMDiagnostics global -> { default_compilation_settings with
-				dms_kind = DMDiagnostics global;
+		| DMDiagnostics files -> { default_compilation_settings with
+				dms_kind = DMDiagnostics files;
 				dms_error_policy = EPCollect;
-				dms_display_file_policy = if global then DFPNo else DFPAlso;
+				dms_display_file_policy = if files = [] then DFPNo else DFPAlso;
 				dms_per_file = true;
 			}
 		| DMStatistics -> { settings with
@@ -286,7 +286,7 @@ module DisplayMode = struct
 		| DMUsage false -> "references"
 		| DMModuleSymbols None -> "module-symbols"
 		| DMModuleSymbols (Some s) -> "workspace-symbols " ^ s
-		| DMDiagnostics b -> (if b then "global " else "") ^ "diagnostics"
+		| DMDiagnostics _ -> "diagnostics"
 		| DMStatistics -> "statistics"
 		| DMSignature -> "signature"
 end

+ 1 - 1
src/typing/typer.ml

@@ -1239,7 +1239,7 @@ and type_ident ctx i p mode =
 					match ctx.com.display.dms_kind with
 						| DMNone ->
 							raise (Error(err,p))
-						| DMDiagnostics b when b || ctx.is_display_file ->
+						| DMDiagnostics _ ->
 							DisplayToplevel.handle_unresolved_identifier ctx i p false;
 							let t = mk_mono() in
 							AKExpr (mk (TIdent i) t p)