Repository: goplus/gop
Pull request #1209 (2022-05-21 13:42:20)

testdata/helloc2go passed


Patch:
diff --git a/all.bat b/all.bat
index 0812ec4b..5395a855 100644
--- a/all.bat
+++ b/all.bat
@@ -1,1 +1,1 @@
-go run cmd\make.go --install --autoproxy
+go run cmd/make.go --install --autoproxy
diff --git a/cl/c.go b/cl/c.go
index 4c520634..39b4c989 100644
--- a/cl/c.go
+++ b/cl/c.go
@@ -17,9 +17,9 @@
 package cl
 
 import (
-	"log"
 	"strings"
 
+	"github.com/goplus/gop/ast"
 	"github.com/goplus/gox"
 )
 
@@ -57,10 +57,13 @@ func c2goBase(base string) string {
 
 // -----------------------------------------------------------------------------
 
-func loadC2goPkg(ctx *blockCtx, realPath string) *gox.PkgRef {
+func loadC2goPkg(ctx *blockCtx, realPath string, src *ast.BasicLit) *gox.PkgRef {
 	cpkg, err := ctx.cpkgs.Import(realPath)
 	if err != nil {
-		log.Panicln("loadC2goPkg failed:", err)
+		pos := ctx.Position(src.Pos())
+		ctx.handleCodeErrorf(&pos,
+			"%v not found or not a valid C package (c2go.a.pub file not found).\n", realPath)
+		return nil
 	}
 	ctx.clookups = append(ctx.clookups, cpkg)
 	return cpkg.Pkg()
diff --git a/cl/compile.go b/cl/compile.go
index 7b673060..099f3ad8 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -978,7 +978,9 @@ func loadImport(ctx *blockCtx, spec *ast.ImportSpec) {
 		if kind == c2goStandard {
 			realPath = ctx.c2goBase + realPath
 		}
-		pkg = loadC2goPkg(ctx, realPath)
+		if pkg = loadC2goPkg(ctx, realPath, spec.Path); pkg == nil {
+			return
+		}
 	} else {
 		pkg = ctx.pkg.Import(simplifyGopPackage(pkgPath))
 	}
diff --git a/cl/internal/dummy/dummy.go b/cl/internal/dummy/dummy.go
new file mode 100644
index 00000000..418051ca
--- /dev/null
+++ b/cl/internal/dummy/dummy.go
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package dummy
+
+import (
+	_ "github.com/goplus/libc"
+)
diff --git a/go.mod b/go.mod
index fd7cc70e..6dfe4272 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.16
 require (
 	github.com/goplus/c2go v0.7.1
 	github.com/goplus/gox v1.11.7
+	github.com/goplus/libc v0.3.5
 	github.com/goplus/mod v0.9.3
 	github.com/qiniu/x v1.11.5
 )
diff --git a/go.sum b/go.sum
index bcc028a4..2bb851d5 100644
--- a/go.sum
+++ b/go.sum
@@ -6,6 +6,8 @@ github.com/goplus/c2go v0.7.1 h1:F/ZofTnR9hONoIpTHXhNVNX0m1UBD/jDp5xRbROKq+s=
 github.com/goplus/c2go v0.7.1/go.mod h1:1om0h0m9wOzUqgxtLvaPOd2B1raurJmZ3ghGP576zZA=
 github.com/goplus/gox v1.11.7 h1:YbTrET69TPdZFwT7ESKeDmwq7wpXRb3DUZ474bCC9Og=
 github.com/goplus/gox v1.11.7/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
+github.com/goplus/libc v0.3.5 h1:dWyOxT2Smdz93daPXbgbEr6QuyFClXsu9XMWBZ2LZSM=
+github.com/goplus/libc v0.3.5/go.mod h1:Tylp4skmMW4TZsGKpajx1TARMtExegAqRX0bzAEGZW8=
 github.com/goplus/mod v0.9.3 h1:kXN2kavdUacqy39Lh/pIdT0zvBTKevb4u96ZbapPbcg=
 github.com/goplus/mod v0.9.3/go.mod h1:NHU13OjeNV3ez1f+R9FLJIlIun0KNSuZb0jnmP3N3o8=
 github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@@ -14,6 +16,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
 github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
+github.com/petermattis/goid v0.0.0-20220331194723-8ee3e6ded87a h1:VXRRto5GMJPNfB7MNbUVoFhtxwoYjBEsIt/NpWg42U0=
+github.com/petermattis/goid v0.0.0-20220331194723-8ee3e6ded87a/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/qiniu/x v1.11.5 h1:TYr5cl4g2yoHAZeDK4MTjKF6CMoG+IHlCDvvM5qym6U=
diff --git a/load.go b/load.go
index 93bc61fd..141618c6 100644
--- a/load.go
+++ b/load.go
@@ -21,6 +21,7 @@ import (
 	"go/token"
 	"go/types"
 	"io/fs"
+	"path/filepath"
 	"strings"
 	"syscall"
 
@@ -93,6 +94,16 @@ func LoadDir(dir string, conf *Config) (out, test *gox.Package, err error) {
 		Fset:        fset,
 		Importer:    conf.Importer,
 		LookupClass: mod.LookupClass,
+		LookupPub: func(pkgPath string) (pubfile string, err error) {
+			if mod.File == nil { // no go.mod/gop.mod file
+				return "", syscall.ENOENT
+			}
+			pkg, err := mod.Lookup(pkgPath)
+			if err == nil {
+				pubfile = filepath.Join(pkg.Dir, "c2go.a.pub")
+			}
+			return
+		},
 	}
 	for name, pkg := range pkgs {
 		if strings.HasSuffix(name, "_test") {


Test patch:
diff --git a/cl/builtin_test.go b/cl/builtin_test.go
index f8ab325b..2c5b9c6f 100644
--- a/cl/builtin_test.go
+++ b/cl/builtin_test.go
@@ -173,14 +173,22 @@ func TestErrDeclFunc(t *testing.T) {
 	})
 }
 
-func TestErrLoadC2goPkg(t *testing.T) {
-	testPanic(t, "loadC2goPkg failed: not found\n", func() {
-		pkg := &pkgCtx{cpkgs: cpackages.NewImporter(
-			&cpackages.Config{LookupPub: func(pkgPath string) (pubfile string, err error) {
-				return "", errors.New("not found")
-			}})}
+func TestErrLoadImport(t *testing.T) {
+	testPanic(t, ".: unknownpkg not found or not a valid C package (c2go.a.pub file not found).\n", func() {
+		pkg := &pkgCtx{
+			nodeInterp: &nodeInterp{
+				fset: token.NewFileSet(),
+			},
+			cpkgs: cpackages.NewImporter(
+				&cpackages.Config{LookupPub: func(pkgPath string) (pubfile string, err error) {
+					return "", errors.New("not found")
+				}})}
 		ctx := &blockCtx{pkgCtx: pkg}
-		loadC2goPkg(ctx, "notfound")
+		spec := &ast.ImportSpec{
+			Path: &ast.BasicLit{Kind: token.STRING, Value: `"C/unknownpkg"`},
+		}
+		loadImport(ctx, spec)
+		panic(ctx.errs[0].Error())
 	})
 }
 
diff --git a/testdata/helloc2go/hello.gop b/testdata/helloc2go/hello.gop
new file mode 100644
index 00000000..b08f5278
--- /dev/null
+++ b/testdata/helloc2go/hello.gop
@@ -0,0 +1,3 @@
+import "C"
+
+C.printf C"Hello, c2go!\n"