Selaa lähdekoodia

[hl/c] Run (and fix) unit tests for hl/c on linux (#11382)

* [hl/c] prefix reserved identifiers with _hx_

* [tests] add test for 11378

* [tests] enable hl/c unit tests; disable 9678 for hl

Technically 9678 should be disabled for cpp and hl/c but we don't have a hl/c specific define

* [hl] add hlc define when compiling hl/c

* [tests] hl/c tests

* [hl/c] run unit tests for hl/c (on linux)

* [hl/c] fix Int64 unsigned right shift overflow

* hlc define is reserved

* [hl/c] dodge issues until fix on hashlink side

* [tests] LD_LIBRARY_PATH isn't used on windows
Rudy Ges 1 vuosi sitten
vanhempi
commit
36418174ac

+ 7 - 0
src-json/define.json

@@ -272,6 +272,13 @@
 		"define": "haxe-next",
 		"doc": "Enable experimental features that are meant to be released on next Haxe version."
 	},
+	{
+		"name": "Hlc",
+		"define": "hlc",
+		"doc": "Defined by compiler when using hl/c target.",
+		"platforms": ["hl"],
+		"reserved": true
+	},
 	{
 		"name": "HlVer",
 		"define": "hl-ver",

+ 3 - 2
src/context/common.ml

@@ -462,7 +462,7 @@ let external_defined_value ctx k =
 	Define.raw_defined_value ctx.defines (convert_define k)
 
 let reserved_flags = [
-	"true";"false";"null";"cross";"js";"lua";"neko";"flash";"php";"cpp";"cs";"java";"python";
+	"true";"false";"null";"cross";"js";"lua";"neko";"flash";"php";"cpp";"cs";"java";"python";"hl";"hlc";
 	"swc";"macro";"sys";"static";"utf16";"haxe";"haxe_ver"
 	]
 
@@ -971,7 +971,8 @@ let set_platform com pf file =
 	if com.platform <> Cross then failwith "Multiple targets";
 	init_platform com pf;
 	com.file <- file;
-	if (pf = Flash) && Path.file_extension file = "swc" then define com Define.Swc;
+	if (pf = Flash) && Path.file_extension file = "swc" then define com Define.Swc
+	else if (pf = Hl) && Path.file_extension file = "c" then define com Define.Hlc;
 	(* Set the source header, unless the user has set one already or the platform sets a custom one *)
 	if not (defined com Define.SourceHeader) && (pf <> Hl) then
 		define_value com Define.SourceHeader ("Generated by Haxe " ^ s_version_full)

+ 7 - 5
src/generators/hl2c.ml

@@ -91,8 +91,7 @@ let keywords =
 	(* Values *)
 	"NULL";"true";"false";
 	(* MS specific *)
-	"__asm";"dllimport2";"__int8";"naked2";"__based1";"__except";"__int16";"__stdcall";"__cdecl";"__fastcall";"__int32";
-	"thread2";"__declspec";"__finally";"__int64";"__try";"dllexport2";"__inline";"__leave";"asm";
+	"asm";"dllimport2";"dllexport2";"naked2";"thread2";
 	(* reserved by HLC *)
 	"t";
 	(* GCC *)
@@ -105,7 +104,7 @@ let keywords =
 	List.iter (fun i -> Hashtbl.add h i ()) c_kwds;
 	h
 
-let ident i = if Hashtbl.mem keywords i then "_" ^ i else i
+let ident i = if (Hashtbl.mem keywords i) || (ExtString.String.starts_with "__" i) then "_hx_" ^ i else i
 
 let s_comp = function
 	| CLt -> "<"
@@ -122,7 +121,7 @@ let core_types =
 
 let tname str =
 	let n = String.concat "__" (ExtString.String.nsplit str ".") in
-	if Hashtbl.mem keywords ("_" ^ n) then "__" ^ n else n
+	ident n
 
 let is_gc_ptr = function
 	| HVoid | HUI8 | HUI16 | HI32 | HI64 | HF32 | HF64 | HBool | HType | HRef _ | HMethod _ | HPacked _ -> false
@@ -844,7 +843,10 @@ let generate_function ctx f =
 		| OSShr (r,a,b) ->
 			sexpr "%s = %s >> %s" (reg r) (reg a) (reg b)
 		| OUShr (r,a,b) ->
-			sexpr "%s = ((unsigned)%s) >> %s" (reg r) (reg a) (reg b)
+			(match rtype r with
+			| HI64 -> sexpr "%s = ((uint64_t)%s) >> %s" (reg r) (reg a) (reg b)
+			| _ -> sexpr "%s = ((unsigned)%s) >> %s" (reg r) (reg a) (reg b)
+			);
 		| OAnd (r,a,b) ->
 			sexpr "%s = %s & %s" (reg r) (reg a) (reg b)
 		| OOr (r,a,b) ->

+ 3 - 0
std/hl/_std/haxe/NativeStackTrace.hx

@@ -29,6 +29,9 @@ class NativeStackTrace {
 		var count = callStackRaw(null);
 		var arr = new NativeArray(count);
 		callStackRaw(arr);
+		// This will avoid errors when compiling hl/c on unix
+		// See https://github.com/HaxeFoundation/haxe/pull/11382 for long term fix
+		if (arr.length == 0) return arr;
 		return arr.sub(1, arr.length - 1);
 	}
 

+ 37 - 0
tests/misc/hl/reserved-keywords/Macro.macro.hx

@@ -0,0 +1,37 @@
+import haxe.macro.Context;
+
+using haxe.macro.Tools;
+
+class Macro {
+	public static function build() {
+		var fields = Context.getBuildFields();
+		var keywords = [
+			"auto", "bool", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto",
+			"if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",
+			"void", "volatile", "while",
+			// Values
+			"NULL", "true", "false",
+			// MS specific
+			"asm", "dllimport2", "dllexport2", "naked2", "thread2",
+			// reserved by HLC
+			"t",
+			// GCC
+			"typeof",
+			// C11
+			"_Alignas", "_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", "_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local", "_Pragma",
+			"inline", "restrict"
+		];
+
+		var pos = Context.currentPos();
+
+		for (k in keywords)
+			fields.push({
+				pos: pos,
+				name: "_test_" + k,
+				meta: [{pos: pos, name: ":native", params: [macro $v{k}]}],
+				kind: FVar(macro :String, null)
+			});
+
+		return fields;
+	}
+}

+ 4 - 0
tests/misc/hl/reserved-keywords/Main.hx

@@ -0,0 +1,4 @@
+@:build(Macro.build())
+class Main {
+	public static function main() {}
+}

+ 2 - 0
tests/misc/hl/reserved-keywords/compile.hxml

@@ -0,0 +1,2 @@
+-main Main
+-hl bin/test.c

+ 9 - 0
tests/runci/System.hx

@@ -133,6 +133,15 @@ class System {
 		}
 	}
 
+	static public function addToLIBPATH(path:String):Void {
+		infoMsg('Prepending $path to LD_LIBRARY_PATH.');
+		switch (systemName) {
+			case "Windows": // pass
+			case "Mac", "Linux":
+				Sys.putEnv("LD_LIBRARY_PATH", path + ":" + Sys.getEnv("LD_LIBRARY_PATH"));
+		}
+	}
+
 	static function isLibraryInstalled(library:String):Bool {
 		return new Process("haxelib", ["path", library]).exitCode() == 0;
 	}

+ 28 - 0
tests/runci/targets/Hl.hx

@@ -65,6 +65,7 @@ class Hl {
 
 		runCommand(hlBinary, ["--version"]);
 		addToPATH(hlBuildBinDir);
+		addToLIBPATH(hlBuildBinDir);
 
 		haxelibDev("hashlink", '$hlSrc/other/haxelib/');
 	}
@@ -75,6 +76,23 @@ class Hl {
 		runCommand("haxe", ["compile-hl.hxml"].concat(args));
 		runCommand(hlBinary, ["bin/unit.hl"]);
 
+		runCommand("haxe", ["compile-hlc.hxml"].concat(args));
+		switch (systemName) {
+			case "Linux" if (isCi()):
+				runCommand("gcc", [
+					"-o", "bin/hlc/main",
+					"bin/hlc/main.c",
+					"-Ibin/hlc/",
+					'-I$hlSrc/src',
+					'$hlBuildBinDir/fmt.hdll',
+					"-lm",
+					'-L$hlBuildBinDir', "-lhl"
+				]);
+				runCommand("bin/hlc/main", []);
+			case _:
+				// TODO hl/c for mac/windows
+		}
+
 		changeDirectory(threadsDir);
 		runCommand("haxe", ["build.hxml", "-hl", "export/threads.hl"]);
 		runCommand(hlBinary, ["export/threads.hl"]);
@@ -88,6 +106,16 @@ class Hl {
 		// TODO: check output like misc tests do
 		runCommand(hlBinary, ["eventLoop.hl"]);
 
+		changeDirectory(getMiscSubDir("hl/reserved-keywords"));
+		runCommand("haxe", ["compile.hxml"]);
+		switch (systemName) {
+			case "Linux" if (isCi()):
+				runCommand("gcc", ["-o", "bin/test", "bin/test.c", "-Ibin/", '-I$hlSrc/src', '-L$hlBuildBinDir', "-lhl"]);
+				runCommand("bin/test", []);
+			case _:
+				// TODO hl/c for mac/windows
+		}
+
 		changeDirectory(miscHlDir);
 		runCommand("haxe", ["run.hxml"]);
 	}

+ 4 - 0
tests/unit/src/unit/TestExceptions.hx

@@ -253,6 +253,10 @@ class TestExceptions extends Test {
 			var expected = null;
 			var lineShift = 0;
 			for(s in stacks) {
+				// This will avoid errors when compiling hl/c on unix
+				// See https://github.com/HaxeFoundation/haxe/pull/11382 for long term fix
+				#if hlc if (s.length == 0) continue; #end
+
 				if(expected == null) {
 					expected = stackItemData(s[0]);
 				} else {

+ 1 - 1
tests/unit/src/unit/TestMain.hx

@@ -79,7 +79,7 @@ function main() {
 		new TestNumericCasts(),
 		new TestHashMap(),
 		new TestRest(),
-		new TestHttp(),
+		#if !hlc new TestHttp(), #end
 		#if !no_pattern_matching
 		new TestMatch(),
 		#end

+ 2 - 1
tests/unit/src/unit/issues/Issue10109.hx

@@ -1,7 +1,8 @@
 package unit.issues;
 
 class Issue10109 extends Test {
-#if hl
+// Disabled for hl/c until https://github.com/HaxeFoundation/haxe/pull/11382 is implemented
+#if (hl && !hlc)
 	@:pure(false)
 	static function foo( o : String ) {
 		return o.length;

+ 1 - 1
tests/unit/src/unit/issues/Issue9678.hx

@@ -1,7 +1,7 @@
 package unit.issues;
 
 class Issue9678 extends unit.Test {
-	#if !cpp
+	#if (!cpp && !hlc)
 	function test() {
 		var called = 0;
 		function returnVoid() {