Browse Source

[server] move dead_blocks to cfile

see #8949
Simon Krajewski 5 years ago
parent
commit
1ffaa6909d

+ 1 - 1
src/compiler/displayOutput.ml

@@ -391,7 +391,7 @@ let load_display_content_standalone ctx input =
 	let file = file_input_marker in
 	let p = {pfile = file; pmin = 0; pmax = 0} in
 	let parsed = TypeloadParse.parse_file_from_string com file p input in
-	let pack,decls = TypeloadParse.handle_parser_result com file p parsed in
+	let pack,decls = TypeloadParse.handle_parser_result com p parsed in
 	ignore(TypeloadModule.type_module ctx (pack,"?DISPLAY") file ~dont_check_path:true decls p)
 
 let promote_type_hints tctx =

+ 10 - 3
src/compiler/server.ml

@@ -147,9 +147,16 @@ let parse_file cs com file p =
 				let parse_result = TypeloadParse.parse_file com file p in
 				let info,is_unusual = match parse_result with
 					| ParseError(_,_,_) -> "not cached, has parse error",true
-					| ParseSuccess(_,true,_) -> "not cached, is display file",true
-					| ParseSuccess(data,_,pdi) ->
-						begin try
+					| ParseSuccess(data,is_display_file,pdi) ->
+						if is_display_file then begin
+							if pdi.pd_errors <> [] then
+								"not cached, is display file with parse errors",true
+							else if com.display.dms_per_file then begin
+								cc#cache_file ffile ftime data pdi;
+								"cached, is intact display file",true
+							end else
+								"not cached, is display file",true
+						end else begin try
 							(* We assume that when not in display mode it's okay to cache stuff that has #if display
 							checks. The reasoning is that non-display mode has more information than display mode. *)
 							if not com.display.dms_display then raise Not_found;

+ 0 - 2
src/context/common.ml

@@ -154,7 +154,6 @@ end
 
 type shared_display_information = {
 	mutable diagnostics_messages : (string * pos * DisplayTypes.DiagnosticsKind.t * DisplayTypes.DiagnosticsSeverity.t) list;
-	mutable dead_blocks : (string,(pos * expr) list) Hashtbl.t;
 }
 
 type display_information = {
@@ -434,7 +433,6 @@ let create version s_version args =
 		shared = {
 			shared_display_information = {
 				diagnostics_messages = [];
-				dead_blocks = Hashtbl.create 0;
 			}
 		};
 		display_information = {

+ 27 - 1
src/context/display/diagnostics.ml

@@ -10,6 +10,7 @@ type diagnostics_context = {
 	com : Common.context;
 	mutable removable_code : (string * pos * pos) list;
 	mutable import_positions : (pos,bool ref) PMap.t;
+	mutable dead_blocks : (string,(pos * expr) list) Hashtbl.t;
 }
 
 open DisplayTypes
@@ -106,6 +107,7 @@ let prepare com global =
 		removable_code = [];
 		com = com;
 		import_positions = PMap.empty;
+		dead_blocks = Hashtbl.create 0;
 	} in
 	List.iter (function
 		| TClassDecl c when global || DisplayPosition.display_position#is_in_file c.cl_pos.pfile ->
@@ -115,6 +117,30 @@ let prepare com global =
 		| _ ->
 			()
 	) com.types;
+	let handle_dead_blocks com = match com.cache with
+		| Some cc ->
+			let macro_defines = adapt_defines_to_macro_context com.defines in
+			let display_defines = {macro_defines with values = PMap.add "display" "1" macro_defines.values} in
+			let is_true defines e =
+				ParserEntry.is_true (ParserEntry.eval defines e)
+			in
+			Hashtbl.iter (fun file cfile ->
+				if DisplayPosition.display_position#is_in_file file then begin
+					let dead_blocks = cfile.CompilationServer.c_pdi.pd_dead_blocks in
+					let dead_blocks = List.filter (fun (_,e) -> not (is_true display_defines e)) dead_blocks in
+					try
+						let dead_blocks2 = Hashtbl.find dctx.dead_blocks file in
+						(* Intersect *)
+						let dead_blocks2 = List.filter (fun (p,_) -> List.mem_assoc p dead_blocks) dead_blocks2 in
+						Hashtbl.replace dctx.dead_blocks file dead_blocks2
+					with Not_found ->
+						Hashtbl.add dctx.dead_blocks file dead_blocks
+				end
+			) cc#get_files
+		| None ->
+			()
+	in
+	handle_dead_blocks com;
 	let process_modules com =
 		List.iter (fun m ->
 			PMap.iter (fun p b ->
@@ -221,7 +247,7 @@ module Printer = struct
 				] in
 				add DKInactiveBlock p DiagnosticsSeverity.Hint jo
 			) ranges
-		) com.shared.shared_display_information.dead_blocks;
+		) dctx.dead_blocks;
 		let jl = Hashtbl.fold (fun file diag acc ->
 			let jl = Hashtbl.fold (fun _ (dk,p,sev,jargs) acc ->
 				(JObject [

+ 2 - 8
src/context/display/displayTexpr.ml

@@ -137,14 +137,10 @@ let check_display_file ctx cs =
 	match ctx.com.cache with
 	| Some cc ->
 		begin try
-			(* TODO: diagnostics currently relies on information collected during typing. *)
-			begin match ctx.com.display.dms_kind with
-				| DMDiagnostics _ -> raise Not_found
-				| _ -> ()
-			end;
 			let p = DisplayPosition.display_position#get in
 			let cfile = cc#find_file (Path.unique_full_path p.pfile) in
 			let path = (cfile.c_package,get_module_name_of_cfile p.pfile cfile) in
+			TypeloadParse.PdiHandler.handle_pdi ctx.com cfile.c_pdi;
 			(* We have to go through type_module_hook because one of the module's dependencies could be
 			   invalid (issue #8991). *)
 			begin match !TypeloadModule.type_module_hook ctx path null_pos with
@@ -152,9 +148,7 @@ let check_display_file ctx cs =
 			| Some m -> check_display_module ctx cc cfile m
 			end
 		with Not_found ->
-			(* Special case for diagnostics: It's not treated as a display mode, but we still want to invalidate the
-				current file in order to run diagnostics on it again. *)
-			if ctx.com.display.dms_display || (match ctx.com.display.dms_kind with DMDiagnostics _ -> true | _ -> false) then begin
+			if ctx.com.display.dms_display then begin
 				let file = (DisplayPosition.display_position#get).pfile in
 				(* force parsing again : if the completion point have been changed *)
 				cs#remove_files file;

+ 6 - 0
src/core/displayTypes.ml

@@ -213,6 +213,7 @@ module DisplayMode = struct
 		dms_inline : bool;
 		dms_display_file_policy : display_file_policy;
 		dms_exit_during_typing : bool;
+		dms_per_file : bool;
 	}
 
 	let default_display_settings = {
@@ -226,6 +227,7 @@ module DisplayMode = struct
 		dms_inline = false;
 		dms_display_file_policy = DFPOnly;
 		dms_exit_during_typing = true;
+		dms_per_file = false;
 	}
 
 	let default_compilation_settings = {
@@ -239,6 +241,7 @@ module DisplayMode = struct
 		dms_inline = true;
 		dms_display_file_policy = DFPNo;
 		dms_exit_during_typing = false;
+		dms_per_file = false;
 	}
 
 	let create dm =
@@ -257,12 +260,14 @@ module DisplayMode = struct
 				dms_display_file_policy = if filter = None then DFPOnly else DFPNo;
 				dms_exit_during_typing = false;
 				dms_force_macro_typing = false;
+				dms_per_file = true;
 			}
 		| DMDiagnostics global -> { default_compilation_settings with
 				dms_kind = DMDiagnostics global;
 				dms_error_policy = EPCollect;
 				dms_collect_data = true;
 				dms_display_file_policy = if global then DFPNo else DFPAlso;
+				dms_per_file = true;
 			}
 		| DMStatistics -> { settings with
 				dms_full_typing = true;
@@ -271,6 +276,7 @@ module DisplayMode = struct
 				dms_display_file_policy = DFPAlso;
 				dms_exit_during_typing = false;
 				dms_force_macro_typing = true;
+				dms_per_file = true;
 			}
 
 	let to_string = function

+ 1 - 26
src/syntax/parserEntry.ml

@@ -119,30 +119,7 @@ and eval_binop_exprs ctx e1 e2 =
 	| TString s, (TVersion _ as v2) -> (parse_version s (snd e1), v2)
 	| v1, v2 -> (v1, v2)
 
-class condition_handler_nop = object(self)
-	val null = EConst(Ident "null"),null_pos
-
-	method cond_if (e : expr) =
-		()
-
-	method cond_else =
-		()
-
-	method cond_elseif (e : expr) =
-		()
-
-	method cond_end =
-		()
-
-	method get_current_condition : expr =
-		null
-
-	method get_conditions : expr list =
-		[]
-end
-
 class condition_Handler = object(self)
-	inherit condition_handler_nop
 	val mutable conditional_expressions = []
 	val mutable conditional_stack = []
 	val mutable depths = []
@@ -226,8 +203,6 @@ class dead_block_collector conds = object(self)
 		DynArray.to_list dead_blocks
 end
 
-let nop_handler = new condition_handler_nop
-
 (* parse main *)
 let parse ctx code file =
 	let old = Lexer.save() in
@@ -259,7 +234,7 @@ let parse ctx code file =
 		error (Custom line) p
 	in
 
-	let conds = if !in_display_file then new condition_Handler else nop_handler in
+	let conds = new condition_Handler in
 	let dbc = new dead_block_collector conds in
 	let sraw = Stream.from (fun _ -> Some (Lexer.sharp_token code)) in
 	let rec next_token() = process_token (Lexer.token code)

+ 1 - 1
src/typing/macroContext.ml

@@ -129,7 +129,7 @@ let make_macro_api ctx p =
 			try
 				begin match ParserEntry.parse_expr_string ctx.com.defines s p error inl with
 					| ParseSuccess(data,true,_) when inl -> data (* ignore errors when inline-parsing in display file *)
-					| ParseSuccess(data,false,_) -> data
+					| ParseSuccess(data,_,_) -> data
 					| ParseError _ -> raise MacroApi.Invalid_expr
 				end
 			with Exit ->

+ 5 - 15
src/typing/typeloadParse.ml

@@ -235,7 +235,7 @@ module PdiHandler = struct
 	let is_true defines e =
 		ParserEntry.is_true (ParserEntry.eval defines e)
 
-	let handle_pdi com file pdi =
+	let handle_pdi com pdi =
 		let macro_defines = adapt_defines_to_macro_context com.defines in
 		let check = (if com.display.dms_kind = DMHover then
 			encloses_position_gt
@@ -262,20 +262,10 @@ module PdiHandler = struct
 		| _ ->
 			()
 		end;
-		let display_defines = {macro_defines with values = PMap.add "display" "1" macro_defines.values} in
-		let dead_blocks = List.filter (fun (_,e) -> not (is_true display_defines e)) pdi.pd_dead_blocks in
-		let sdi = com.shared.shared_display_information in
-		begin try
-			let dead_blocks2 = Hashtbl.find sdi.dead_blocks file in
-			(* Intersect *)
-			let dead_blocks2 = List.filter (fun (p,_) -> List.mem_assoc p dead_blocks) dead_blocks2 in
-			Hashtbl.replace sdi.dead_blocks file dead_blocks2
-		with Not_found ->
-			Hashtbl.add sdi.dead_blocks file dead_blocks
-		end;
+		()
 end
 
-let handle_parser_result com file p result =
+let handle_parser_result com p result =
 	let handle_parser_error msg p =
 		let msg = Parser.error_msg msg in
 		match com.display.dms_error_policy with
@@ -290,7 +280,7 @@ let handle_parser_result com file p result =
 				| (msg,p) :: _ -> handle_parser_error msg p
 				| [] -> ()
 				end;
-				PdiHandler.handle_pdi com file pdi;
+				PdiHandler.handle_pdi com pdi;
 			end;
 			data
 		| ParseError(data,(msg,p),_) ->
@@ -298,7 +288,7 @@ let handle_parser_result com file p result =
 			data
 
 let parse_module_file com file p =
-	handle_parser_result com file p ((!parse_hook) com file p)
+	handle_parser_result com p ((!parse_hook) com file p)
 
 let parse_module' com m p =
 	let remap = ref (fst m) in