Browse Source

added macro_depth to typer context to check macro recursion (fixed issue #948)

Simon Krajewski 13 năm trước cách đây
mục cha
commit
ae96208dfa
3 tập tin đã thay đổi với 9 bổ sung2 xóa
  1. 1 0
      typecore.ml
  2. 1 0
      typeload.ml
  3. 7 2
      typer.ml

+ 1 - 0
typecore.ml

@@ -62,6 +62,7 @@ and typer = {
 	mutable t : basic_types;
 	g : typer_globals;
 	mutable in_macro : bool;
+	mutable macro_depth : int;
 	(* per-module *)
 	current : module_def;
 	mutable local_types : module_type list;

+ 1 - 0
typeload.ml

@@ -1342,6 +1342,7 @@ let type_module ctx m file tdecls loadp =
 		com = ctx.com;
 		g = ctx.g;
 		t = ctx.t;
+		macro_depth = ctx.macro_depth;
 		curclass = ctx.curclass;
 		tthis = ctx.tthis;
 		ret = ctx.ret;

+ 7 - 2
typer.ml

@@ -2182,7 +2182,9 @@ and build_call ctx acc el twith p =
 			| _ -> assert false)
 		| _ -> assert false)
 	| AKMacro (ethis,f) ->
-		(match ethis.eexpr with
+		if ctx.macro_depth > 300 then error "Stack overflow" p;
+		ctx.macro_depth <- ctx.macro_depth + 1;
+		let e = (match ethis.eexpr with
 		| TTypeExpr (TClassDecl c) ->
 			(match ctx.g.do_macro ctx MExpr c.cl_path f.cf_name el p with
 			| None -> type_expr ctx (EConst (Ident "null"),p)
@@ -2202,7 +2204,9 @@ and build_call ctx acc el twith p =
 						| Some (csup,_) -> loop csup
 				in
 				loop c
-			| _ -> assert false))
+			| _ -> assert false)) in
+		ctx.macro_depth <- ctx.macro_depth - 1;
+		e;
 	| AKNo _ | AKSet _ ->
 		ignore(acc_get ctx acc p);
 		assert false
@@ -2983,6 +2987,7 @@ let rec create com is_macro_ctx =
 			do_optimize = Optimizer.reduce_expression;
 			do_build_instance = Codegen.build_instance;
 		};
+		macro_depth = 0;
 		untyped = false;
 		curfun = FStatic;
 		in_loop = false;