cl: _testcgo/strlen - todo

This commit is contained in:
xushiwei
2024-04-26 02:05:49 +08:00
parent 28dd34a136
commit 43ae7a23b2
8 changed files with 95 additions and 18 deletions

17
cl/_testcgo/strlen/in.go Normal file
View File

@@ -0,0 +1,17 @@
package main
import "C"
import _ "unsafe"
//go:linkname printf printf
func printf(format *int8, __llgo_va_list ...any)
//go:linkname strlen strlen
func strlen(str *int8) C.int
var format = [...]int8{'H', 'e', 'l', 'l', 'o', ' ', '%', 'd', '\n', 0}
func main() {
sfmt := &format[0]
printf(sfmt, strlen(sfmt))
}

View File

View File

@@ -29,6 +29,7 @@ import (
"github.com/goplus/gogen/packages"
"github.com/goplus/llgo/cl"
"github.com/goplus/llgo/internal/llgen"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
@@ -41,7 +42,7 @@ func init() {
llssa.SetDebug(llssa.DbgFlagAll)
}
func FromDir(t *testing.T, sel, relDir string) {
func FromDir(t *testing.T, sel, relDir string, byLLGen bool) {
dir, err := os.Getwd()
if err != nil {
t.Fatal("Getwd failed:", err)
@@ -57,23 +58,30 @@ func FromDir(t *testing.T, sel, relDir string) {
continue
}
t.Run(name, func(t *testing.T) {
testFrom(t, dir+"/"+name, sel)
testFrom(t, dir+"/"+name, sel, byLLGen)
})
}
}
func testFrom(t *testing.T, pkgDir, sel string) {
func testFrom(t *testing.T, pkgDir, sel string, byLLGen bool) {
if sel != "" && !strings.Contains(pkgDir, sel) {
return
}
log.Println("Parsing", pkgDir)
in := pkgDir + "/in.go"
out := pkgDir + "/out.ll"
expected, err := os.ReadFile(out)
b, err := os.ReadFile(out)
if err != nil {
t.Fatal("ReadFile failed:", err)
}
TestCompileEx(t, nil, in, string(expected))
expected := string(b)
if byLLGen {
if v := llgen.GenFromFile(in); v != expected {
t.Fatalf("\n==> got:\n%s\n==> expected:\n%s\n", v, expected)
}
} else {
TestCompileEx(t, nil, in, expected)
}
}
func TestCompileEx(t *testing.T, src any, fname, expected string) {

View File

@@ -28,7 +28,7 @@ func testCompile(t *testing.T, src, expected string) {
}
func TestFromTestdata(t *testing.T) {
cltest.FromDir(t, "", "./_testdata")
cltest.FromDir(t, "apkg", "./_testdata", false)
}
func TestVar(t *testing.T) {