Просмотр исходного кода

Display Context.error and Context.info messages in diagnostics (#11021)

* Display Context.error() errors in diagnostics

* [diagnostics] also display Context.info messages

* [tests] add test for 11020

* fix test output

* [tests] add option to escape backslashes

* [tests] add option to escape backslashes

* Template function args are string
Rudy Ges 2 лет назад
Родитель
Сommit
7623a8cf99

+ 3 - 0
src/compiler/displayProcessing.ml

@@ -92,6 +92,9 @@ let process_display_arg ctx actx =
 let process_display_configuration ctx =
 	let com = ctx.com in
 	if is_diagnostics com then begin
+		com.info <- (fun ?depth s p ->
+			add_diagnostics_message com (located s p) DKCompilerMessage Information
+		);
 		com.warning <- (fun ?depth w options s p ->
 			match Warning.get_mode w (com.warning_options @ options) with
 			| WMEnable ->

+ 4 - 3
src/macro/macroApi.ml

@@ -61,10 +61,11 @@ type 'value compiler_api = {
 	encode_ctype : Ast.type_hint -> 'value;
 	decode_type : 'value -> t;
 	flush_context : (unit -> t) -> t;
+	info : ?depth:int -> string -> pos -> unit;
+	warning : ?depth:int -> Warning.warning -> string -> pos -> unit;
 	display_error : ?depth:int -> (string -> pos -> unit);
 	with_imports : 'a . import list -> placed_name list list -> (unit -> 'a) -> 'a;
 	with_options : 'a . compiler_options -> (unit -> 'a) -> 'a;
-	warning : ?depth:int -> Warning.warning -> string -> pos -> unit;
 }
 
 
@@ -1693,7 +1694,7 @@ let macro_api ccom get_api =
 			let msg = decode_string msg in
 			let p = decode_pos p in
 			let depth = decode_int depth in
-			(ccom()).error ~depth msg p;
+			(get_api()).display_error ~depth msg p;
 			raise Abort
 		);
 		"fatal_error", vfun3 (fun msg p depth ->
@@ -1720,7 +1721,7 @@ let macro_api ccom get_api =
 			let msg = decode_string msg in
 			let p = decode_pos p in
 			let depth = decode_int depth in
-			(ccom()).info ~depth msg p;
+			(get_api()).info ~depth msg p;
 			vnull
 		);
 		"get_messages", vfun0 (fun() ->

+ 3 - 0
src/typing/macroContext.ml

@@ -422,6 +422,9 @@ let make_macro_api ctx p =
 			in
 			Std.finally restore f ()
 		);
+		MacroApi.info = (fun ?(depth=0) msg p ->
+			ctx.com.info ~depth msg p
+		);
 		MacroApi.warning = (fun ?(depth=0) w msg p ->
 			warning ~depth ctx w msg p
 		);

+ 17 - 0
tests/misc/projects/Issue11020/Main.hx

@@ -0,0 +1,17 @@
+#if !macro @:build(Main.error()) #end
+class Main {
+	var field:Int = 0;
+	static function main() {}
+}
+
+#if macro
+function error() {
+	var fields = haxe.macro.Context.getBuildFields();
+	haxe.macro.Context.info("Context.info", fields[0].pos);
+	haxe.macro.Context.warning("Context.warning", fields[0].pos);
+	haxe.macro.Context.error("Context.error", fields[0].pos);
+
+	return null;
+}
+#end
+

+ 4 - 0
tests/misc/projects/Issue11020/compile.hxml

@@ -0,0 +1,4 @@
+-cp src
+-main Main
+--interp
+--display "Main.hx@0@diagnostics"

+ 1 - 0
tests/misc/projects/Issue11020/compile.hxml.stderr

@@ -0,0 +1 @@
+[{"file":"$$normPath(::cwd::/,true)Main.hx","diagnostics":[{"kind":2,"severity":3,"range":{"start":{"line":2,"character":1},"end":{"line":2,"character":19}},"args":"Context.info"},{"kind":2,"severity":2,"range":{"start":{"line":2,"character":1},"end":{"line":2,"character":19}},"args":"Context.warning"},{"kind":2,"severity":1,"range":{"start":{"line":2,"character":1},"end":{"line":2,"character":19}},"args":"Context.error"},{"kind":2,"severity":1,"range":{"start":{"line":0,"character":11},"end":{"line":0,"character":18}},"args":"Build failure"}]}]

+ 4 - 1
tests/misc/src/Main.hx

@@ -77,11 +77,14 @@ class Main {
 		return new haxe.Template(s).execute(context, macros);
 	}
 
-	static function normPath(_, p:String):String {
+	static function normPath(_, p:String, escape:String = "false"):String {
+		var escape = escape != "false";
+
 		if (Sys.systemName() == "Windows") {
 			// on windows, haxe returns lowercase paths with backslashes, drive letter uppercased
 			p = p.substr(0, 1).toUpperCase() + p.substr(1);
 			p = p.replace("/", "\\");
+			if (escape) p = p.replace("\\", "\\\\");
 		}
 		return p;
 	}