Skip to content

Commit

Permalink
Merge pull request #1209 from xushiwei/q
Browse files Browse the repository at this point in the history
testdata/helloc2go passed
  • Loading branch information
xushiwei committed May 21, 2022
2 parents 2d8a1c2 + 20268cb commit bd26658
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 11 deletions.
22 changes: 15 additions & 7 deletions cl/builtin_test.go
Expand Up @@ -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())
})
}

Expand Down
9 changes: 6 additions & 3 deletions cl/c.go
Expand Up @@ -17,9 +17,9 @@
package cl

import (
"log"
"strings"

"github.com/goplus/gop/ast"
"github.com/goplus/gox"
)

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion cl/compile.go
Expand Up @@ -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))
}
Expand Down
21 changes: 21 additions & 0 deletions 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"
)
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions go.sum
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
11 changes: 11 additions & 0 deletions load.go
Expand Up @@ -21,6 +21,7 @@ import (
"go/token"
"go/types"
"io/fs"
"path/filepath"
"strings"
"syscall"

Expand Down Expand Up @@ -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") {
Expand Down
3 changes: 3 additions & 0 deletions testdata/helloc2go/hello.gop
@@ -0,0 +1,3 @@
import "C"

C.printf C"Hello, c2go!\n"

0 comments on commit bd26658

Please sign in to comment.