Simon Krajewski пре 5 месеци
родитељ
комит
6e32ac424f
5 измењених фајлова са 75 додато и 17 уклоњено
  1. 2 0
      src/context/common.ml
  2. 27 0
      src/context/safeCom.ml
  3. 29 4
      src/filters/filters.ml
  4. 6 6
      src/filters/localStatic.ml
  5. 11 7
      src/typing/macroContext.ml

+ 2 - 0
src/context/common.ml

@@ -342,6 +342,8 @@ let to_safe_com com = {
 	is_macro_context = com.is_macro_context;
 	exceptions = ref [];
 	exceptions_mutex = Mutex.create ();
+	warnings = ref [];
+	warnings_mutex = Mutex.create ();
 	curclass = null_class;
 	curfield = null_field;
 }

+ 27 - 0
src/context/safeCom.ml

@@ -2,6 +2,14 @@ open Globals
 open Type
 open PlatformConfig
 
+type saved_warning = {
+	w_module : module_def;
+	w_warning : WarningList.warning;
+	w_options : Warning.warning_option list list;
+	w_msg : string;
+	w_pos : pos;
+}
+
 type t = {
 	basic : basic_types;
 	platform : platform;
@@ -11,6 +19,8 @@ type t = {
 	is_macro_context : bool;
 	exceptions : exn list ref;
 	exceptions_mutex : Mutex.t;
+	warnings : saved_warning list ref;
+	warnings_mutex : Mutex.t;
 	curclass : tclass;
 	curfield : tclass_field;
 }
@@ -18,6 +28,23 @@ type t = {
 let add_exn com exn =
 	Mutex.protect com.exceptions_mutex (fun () -> com.exceptions := exn :: !(com.exceptions))
 
+let add_warning com w msg p =
+	let options = (Warning.from_meta com.curfield.cf_meta) @ (Warning.from_meta com.curclass.cl_meta) in
+	match Warning.get_mode w options with
+	| WMEnable ->
+		Mutex.protect com.warnings_mutex (fun () ->
+			let warning = {
+				w_module = com.curclass.cl_module;
+				w_warning = w;
+				w_options = options;
+				w_msg = msg;
+				w_pos = p;
+			} in
+			com.warnings := warning :: !(com.warnings)
+		)
+	| WMDisable ->
+		()
+
 let run_expression_filters_safe (com : t) detail_times filters t =
 	let run com identifier e =
 		List.fold_left (fun e (filter_name,f) ->

+ 29 - 4
src/filters/filters.ml

@@ -32,7 +32,7 @@ let get_native_name = Native.get_native_name
 (* -------------------------------------------------------------------------- *)
 (* CHECK LOCAL VARS INIT *)
 
-let check_local_vars_init ctx e =
+let check_local_vars_init scom e =
 	let intersect vl1 vl2 =
 		PMap.mapi (fun v t -> t && PMap.find v vl2) vl1
 	in
@@ -57,8 +57,8 @@ let check_local_vars_init ctx e =
 			let init = (try PMap.find v.v_id !vars with Not_found -> true) in
 			if not init then begin
 				if IntMap.mem v.v_id !outside_vars then
-					if v.v_name = "this" then warning ctx WVarInit "this might be used before assigning a value to it" e.epos
-					else warning ctx WVarInit ("Local variable " ^ v.v_name ^ " might be used before being initialized") e.epos
+					if v.v_name = "this" then SafeCom.add_warning scom WVarInit "this might be used before assigning a value to it" e.epos
+					else SafeCom.add_warning scom WVarInit ("Local variable " ^ v.v_name ^ " might be used before being initialized") e.epos
 				else
 					if v.v_name = "this" then raise_typing_error "Missing this = value" e.epos
 					else raise_typing_error ("Local variable " ^ v.v_name ^ " used without being initialized") e.epos
@@ -609,6 +609,26 @@ let might_need_cf_unoptimized c cf =
 	| _ ->
 		has_class_field_flag cf CfGeneric
 
+let check_scom com scom =
+	let open SafeCom in
+	List.iter (fun warning ->
+		module_warning com warning.w_module warning.w_warning warning.w_options warning.w_msg warning.w_pos
+	) !(scom.warnings);
+	scom.warnings := [];
+	let exns = !(scom.exceptions) in
+	scom.exceptions := [];
+	match exns with
+	| x :: _ ->
+		raise x
+	| [] ->
+		()
+
+let run_parallel_safe com scom f =
+	Parallel.run_in_new_pool (fun pool ->
+		f pool
+	);
+	check_scom com scom
+
 let run tctx ectx main before_destruction =
 	let com = tctx.com in
 	let detail_times = (try int_of_string (Common.defined_value_safe com ~default:"0" Define.FilterTimes) with _ -> 0) in
@@ -659,6 +679,11 @@ let run tctx ectx main before_destruction =
 		"local_statics",LocalStatic.run;
 		"fix_return_dynamic_from_void_function",fix_return_dynamic_from_void_function;
 		"check_local_vars_init",check_local_vars_init;
+	] in
+	run_parallel_safe com safe_com (fun pool ->
+		Parallel.ParallelArray.iter pool (SafeCom.run_expression_filters_safe safe_com detail_times filters) new_types_array
+	);
+	let filters = [
 		"check_abstract_as_value",check_abstract_as_value;
 		"Tre",if defined com Define.AnalyzerOptimize then Tre.run else (fun _ e -> e);
 		"reduce_expression",Optimizer.reduce_expression;
@@ -680,7 +705,7 @@ let run tctx ectx main before_destruction =
 		| _ -> (fun scom e -> RenameVars.run scom.curclass.cl_path locals e));
 		"mark_switch_break_loops",(fun _ -> mark_switch_break_loops);
 	] in
-	Parallel.run_in_new_pool (fun pool ->
+	run_parallel_safe com safe_com (fun pool ->
 		Parallel.ParallelArray.iter pool (SafeCom.run_expression_filters_safe safe_com detail_times filters) new_types_array
 	);
 	with_timer detail_times "callbacks" None (fun () ->

+ 6 - 6
src/filters/localStatic.ml

@@ -3,14 +3,14 @@ open Typecore
 open Error
 
 type lscontext = {
-	ctx : typer;
+	scom : SafeCom.t;
 	lut : (int,tclass_field) Hashtbl.t;
 	mutable added_fields : tclass_field list;
 }
 
 let promote_local_static lsctx run v eo =
-	let name = Printf.sprintf "%s_%s" lsctx.ctx.f.curfield.cf_name v.v_name in
-	let c = lsctx.ctx.c.curclass in
+	let name = Printf.sprintf "%s_%s" lsctx.scom.curfield.cf_name v.v_name in
+	let c = lsctx.scom.curclass in
 	begin try
 		let cf = PMap.find name c.cl_statics in
 		raise_typing_error_ext (make_error (Custom (Printf.sprintf "The expanded name of this local (%s) conflicts with another static field" name)) ~sub:[
@@ -81,13 +81,13 @@ let promote_local_static lsctx run v eo =
 let find_local_static lut v =
 	Hashtbl.find lut v.v_id
 
-let run ctx e =
+let run scom e =
 	let lsctx = {
-		ctx = ctx;
+		scom = scom;
 		lut = Hashtbl.create 0;
 		added_fields = [];
 	} in
-	let c = ctx.c.curclass in
+	let c = scom.curclass in
 	let rec run e = match e.eexpr with
 		| TBlock el ->
 			let el = ExtList.List.filter_map (fun e -> match e.eexpr with

+ 11 - 7
src/typing/macroContext.ml

@@ -611,13 +611,7 @@ and flush_macro_context mint mctx =
 	mctx.com.types <- types;
 	mctx.com.Common.modules <- modules;
 	let ectx = Exceptions.create_exception_context mctx in
-	(* we should maybe ensure that all filters in Main are applied. Not urgent atm *)
-	let expr_filters = [
-		"handle_abstract_casts",AbstractCast.handle_abstract_casts;
-		"local_statics",LocalStatic.run;
-		"Exceptions",(fun _ -> Exceptions.filter ectx);
-		"captured_vars",(fun _ -> CapturedVars.captured_vars mctx.com);
-	] in
+
 	(*
 		some filters here might cause side effects that would break compilation server.
 		let's save the minimal amount of information we need
@@ -658,6 +652,16 @@ and flush_macro_context mint mctx =
 		in
 		if apply_native then Native.apply_native_paths t
 	in
+	let expr_filters = [
+		"handle_abstract_casts",AbstractCast.handle_abstract_casts;
+		"local_statics",(fun tctx ->
+			let scom = to_safe_com mctx.com in
+			let scom = {scom with curclass = tctx.c.curclass; curfield = tctx.f.curfield} in (* This isn't great *)
+			LocalStatic.run scom
+		);
+		"Exceptions",(fun _ -> Exceptions.filter ectx);
+		"captured_vars",(fun _ -> CapturedVars.captured_vars mctx.com);
+	] in
 	let type_filters = [
 		FiltersCommon.remove_generic_base;
 		Exceptions.patch_constructors mctx ectx;