Repository: Konstantin8105/c4go
Pull request #489 (2021-04-19 11:56:38)

CSTD: add


Patch:
diff --git a/README.md b/README.md
index 8a680d6..eabb093 100644
--- a/README.md
+++ b/README.md
@@ -468,13 +468,13 @@ less vi.go
             iso646.h	          	    undefined
             limits.h	          	    undefined
             locale.h	       3/3	         100%
-              math.h	     22/23	        95.7%
+              math.h	     22/22	         100%
             setjmp.h	       0/3	           0%
             signal.h	       3/3	         100%
             stdarg.h	       4/4	         100%
-            stddef.h	       3/4	          75%
+            stddef.h	       4/4	         100%
              stdio.h	     37/41	        90.2%
-            stdlib.h	     31/37	        83.8%
+            stdlib.h	     34/37	        91.9%
             string.h	     21/24	        87.5%
               time.h	     14/15	        93.3%
              wchar.h	      3/60	           5%
diff --git a/transpiler/bind.go b/transpiler/bind.go
index 83278b8..a92ceef 100644
--- a/transpiler/bind.go
+++ b/transpiler/bind.go
@@ -92,6 +92,9 @@ func getBindFunction(p *program.Program, d program.DefinitionFunction) (code str
 	var fl goast.FieldList
 	var argResolvedType []string
 	for i := range d.ArgumentTypes {
+		if d.ArgumentTypes[i] == "void"{
+			continue
+		}
 		if i == len(d.ArgumentTypes)-1 && d.ArgumentTypes[i] == "..." {
 			argResolvedType[len(argResolvedType)-1] =
 				"..." + argResolvedType[len(argResolvedType)-1]


Test patch:
diff --git a/cstd_test.go b/cstd_test.go
index be4da46..9df9160 100644
--- a/cstd_test.go
+++ b/cstd_test.go
@@ -91,7 +91,7 @@ var cstd = map[string][]string{
 		"localeconv",
 	},
 	"math.h": {
-		"abs",
+		// "abs",
 		"acos",
 		// c++ "acosh",
 		"asin",
diff --git a/main_test.go b/main_test.go
index 5bbc5c8..edcd0fc 100644
--- a/main_test.go
+++ b/main_test.go
@@ -544,6 +544,114 @@ func TestMultifileTranspilation(t *testing.T) {
 	}
 }
 
+func TestBind(t *testing.T) {
+	cProgram := programOut{}
+
+	{
+		// create C object file
+		// gcc -c test.c
+		cmd := exec.Command("clang",
+			"-o", "./testdata/bind/test.o",
+			"-c", "./tests/bind/test.c",
+		)
+		cmd.Stdout = &cProgram.stdout
+		cmd.Stderr = &cProgram.stderr
+		err := cmd.Run()
+		if err != nil {
+			fmt.Fprintf(os.Stdout, "%v. %v\n", cProgram.stderr.String(), err)
+			return
+		}
+	}
+	{
+		// ar rc libtest.a test.o
+		cmd := exec.Command("ar", "rc",
+			"./testdata/bind/libtest.a",
+			"./testdata/bind/test.o",
+		)
+		cmd.Stdout = &cProgram.stdout
+		cmd.Stderr = &cProgram.stderr
+		err := cmd.Run()
+		if err != nil {
+			fmt.Fprintf(os.Stdout, "%v. %v\n", cProgram.stderr.String(), err)
+			return
+		}
+	}
+	{
+		// ranlib libtest.a
+		cmd := exec.Command("ranlib",
+			"./testdata/bind/libtest.a",
+		)
+		cmd.Stdout = &cProgram.stdout
+		cmd.Stderr = &cProgram.stderr
+		err := cmd.Run()
+		if err != nil {
+			fmt.Fprintf(os.Stdout, "%v. %v\n", cProgram.stderr.String(), err)
+			return
+		}
+	}
+	{
+		// gcc bind.c libtest.a
+		cmd := exec.Command("clang",
+			"-o", "./testdata/bind/a.out",
+			"./tests/bind/bind.c",
+			"./testdata/bind/libtest.a",
+		)
+		cmd.Stdout = &cProgram.stdout
+		cmd.Stderr = &cProgram.stderr
+		err := cmd.Run()
+		if err != nil {
+			fmt.Fprintf(os.Stdout, "%v. %v\n", cProgram.stderr.String(), err)
+			return
+		}
+	}
+	{
+		// output
+		cmd := exec.Command("./testdata/bind/a.out")
+		var buf bytes.Buffer
+		cmd.Stdout = &buf
+		cmd.Stderr = &cProgram.stderr
+		err := cmd.Run()
+		if err != nil {
+			fmt.Fprintf(os.Stdout, "%v. %v\n", cProgram.stderr.String(), err)
+			return
+		}
+		fmt.Println("::::::::", buf.String())
+	}
+
+	// create subfolders for test
+	subFolder := buildFolder + separator +
+		"bind" + separator
+
+	// Create build folder
+	err := os.MkdirAll(subFolder, os.ModePerm)
+	if err != nil {
+		t.Fatalf("error: %v", err)
+	}
+
+	var args = DefaultProgramArgs()
+	args.inputFiles = []string{"./tests/bind/bind.c"}
+	args.outputFile = path.Join(subFolder, "main.go")
+	args.clangFlags = []string{"-Itests/bind", "./testdata/bind/libtest.a"}
+	args.packageName = "main"
+	args.verbose = true // Added for checking verbose mode
+
+	for _, state := range []ProgramState{StateAst, StateTranspile} {
+		args.state = state
+		err = Start(args)
+		if err != nil {
+			t.Errorf(err.Error())
+		}
+	}
+
+	// Run Go program
+	out, err := args.runGoTest("", []string{""})
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	fmt.Println(out)
+}
+
 func TestTrigraph(t *testing.T) {
 	// create subfolders for test
 	subFolder := buildFolder + separator +
diff --git a/tests/bind/bind.c b/tests/bind/bind.c
new file mode 100644
index 0000000..4c458f0
--- /dev/null
+++ b/tests/bind/bind.c
@@ -0,0 +1,7 @@
+#include "test.h"
+
+int main(void) 
+{
+	view();
+	return 0;
+}
diff --git a/tests/bind/test.c b/tests/bind/test.c
new file mode 100644
index 0000000..0b60952
--- /dev/null
+++ b/tests/bind/test.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+void view()
+{
+	printf("view");
+}
diff --git a/tests/bind/test.h b/tests/bind/test.h
new file mode 100644
index 0000000..45c567a
--- /dev/null
+++ b/tests/bind/test.h
@@ -0,0 +1,1 @@
+void view();
diff --git a/tests/code_quality/define.c b/tests/code_quality/define.c
new file mode 100644
index 0000000..be0fa5b
--- /dev/null
+++ b/tests/code_quality/define.c
@@ -0,0 +1,11 @@
+#define A 1
+#define B "hello world"
+#define C a + 1
+
+void t()
+{
+    int a = A;
+    printf(B);
+    int c = C;
+    printf("%d", c);
+}
diff --git a/tests/code_quality/define.go.expected b/tests/code_quality/define.go.expected
new file mode 100644
index 0000000..5297b47
--- /dev/null
+++ b/tests/code_quality/define.go.expected
@@ -0,0 +1,16 @@
+//
+//	Package - transpiled by c4go
+//
+//	If you have found any issues, please raise an issue at:
+//	https://github.com/Konstantin8105/c4go/
+//
+
+package code_quality
+
+// t - transpiled function from  C4GO/tests/code_quality/define.c:5
+func t() {
+	var a int32 = 1
+	printf([]byte("hello world\x00"))
+	var c int32 = a + 1
+	printf([]byte("%d\x00"), c)
+}
diff --git a/tests/stddef.c b/tests/stddef.c
index 30cf79c..da7b821 100644
--- a/tests/stddef.c
+++ b/tests/stddef.c
@@ -43,12 +43,24 @@ void test_ptrdiff_t()
     }
 }
 
+void test_sizet()
+{
+    diag("size_t");
+    const size_t N = 100;
+    int numbers[N];
+    for (size_t ndx = 0; ndx < N; ++ndx)
+        numbers[ndx] = ndx;
+    size_t size = sizeof numbers;
+    is_eq(size, 400);
+}
+
 int main()
 {
-    plan(7);
+    plan(8);
 
     test_offset();
     test_ptrdiff_t();
+    test_sizet();
 
     done_testing();
 }
diff --git a/tests/stdlib.c b/tests/stdlib.c
index 4d2711d..1c638df 100644
--- a/tests/stdlib.c
+++ b/tests/stdlib.c
@@ -397,7 +397,7 @@ void test_atoi_post()
 
 int main()
 {
-    plan(767);
+    plan(771);
 
     struct_with_define();
 
@@ -697,5 +697,17 @@ int main()
     // diag("my_memrchr");
     // test_my_memrchr();
 
+    diag("EXIT_FAILURE");
+    is_eq(EXIT_FAILURE, 1);
+
+    diag("EXIT_SUCCESS");
+    is_eq(EXIT_SUCCESS, 0);
+
+    diag("MB_CUR_MAX");
+    is_eq(MB_CUR_MAX, 1);
+
+    diag("RAND_MAX");
+    is_eq(RAND_MAX, 2147483647);
+
     done_testing();
 }