]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go, cmd/vet, go/internal/gccgoimport: make vet work with gccgo
authorIan Lance Taylor <iant@golang.org>
Fri, 18 May 2018 13:18:34 +0000 (09:18 -0400)
committerIan Lance Taylor <iant@golang.org>
Thu, 24 May 2018 22:08:00 +0000 (22:08 +0000)
When using gccgo/GoLLVM, there is no package file for a standard
library package. Since it is impossible for the go tool to rebuild the
package, and since the package file exists only in the form of a .gox
file, this seems like the best choice. Unfortunately it was confusing
vet, which wanted to see a real file. This caused vet to report errors
about missing package files for standard library packages. The
gccgoimporter knows how to correctly handle this case. Fix this by

1) telling vet which packages are standard;
2) letting vet skip those packages;
3) letting the gccgoimporter handle this case.

As a separate required fix, gccgo/GoLLVM has no runtime/cgo package,
so don't try to depend on it (as it happens, this fixes #25324).

The result is that the cmd/go vet tests pass when using -compiler=gccgo.

Fixes #25324

Change-Id: Iba8f948fe944da5dc674f580bd3321929ee50fa0
Reviewed-on: https://go-review.googlesource.com/113716
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/work/exec.go
src/cmd/vet/main.go
src/go/internal/gccgoimporter/importer.go

index b81b4e98dc99be488518a591a54b0936de3e0a6e..5a26ca7892a23a2dbae7168ec2af4db7bf27e6e3 100644 (file)
@@ -999,7 +999,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
 
        // Cgo translation adds imports of "runtime/cgo" and "syscall",
        // except for certain packages, to avoid circular dependencies.
-       if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) {
+       if p.UsesCgo() && (!p.Standard || !cgoExclude[p.ImportPath]) && cfg.BuildContext.Compiler != "gccgo" {
                addImport("runtime/cgo")
        }
        if p.UsesCgo() && (!p.Standard || !cgoSyscallExclude[p.ImportPath]) {
@@ -1008,7 +1008,9 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
 
        // SWIG adds imports of some standard packages.
        if p.UsesSwig() {
-               addImport("runtime/cgo")
+               if cfg.BuildContext.Compiler != "gccgo" {
+                       addImport("runtime/cgo")
+               }
                addImport("syscall")
                addImport("sync")
 
@@ -1214,7 +1216,7 @@ func LinkerDeps(p *Package) []string {
        deps := []string{"runtime"}
 
        // External linking mode forces an import of runtime/cgo.
-       if externalLinkingForced(p) {
+       if externalLinkingForced(p) && cfg.BuildContext.Compiler != "gccgo" {
                deps = append(deps, "runtime/cgo")
        }
        // On ARM with GOARM=5, it forces an import of math, for soft floating point.
index 9cb568a18f9a509416578ed55e2490a6c8d3394e..072e2904c14672af75a1b3c42e06cc78a6f342da 100644 (file)
@@ -743,6 +743,7 @@ type vetConfig struct {
        GoFiles     []string
        ImportMap   map[string]string
        PackageFile map[string]string
+       Standard    map[string]bool
        ImportPath  string
 
        SucceedOnTypecheckFailure bool
@@ -760,6 +761,7 @@ func buildVetConfig(a *Action, gofiles []string) {
                ImportPath:  a.Package.ImportPath,
                ImportMap:   make(map[string]string),
                PackageFile: make(map[string]string),
+               Standard:    make(map[string]bool),
        }
        a.vetCfg = vcfg
        for i, raw := range a.Package.Internal.RawImports {
@@ -776,7 +778,7 @@ func buildVetConfig(a *Action, gofiles []string) {
 
        for _, a1 := range a.Deps {
                p1 := a1.Package
-               if p1 == nil || p1.ImportPath == "" || a1.built == "" {
+               if p1 == nil || p1.ImportPath == "" {
                        continue
                }
                // Add import mapping if needed
@@ -784,7 +786,12 @@ func buildVetConfig(a *Action, gofiles []string) {
                if !vcfgMapped[p1.ImportPath] {
                        vcfg.ImportMap[p1.ImportPath] = p1.ImportPath
                }
-               vcfg.PackageFile[p1.ImportPath] = a1.built
+               if a1.built != "" {
+                       vcfg.PackageFile[p1.ImportPath] = a1.built
+               }
+               if p1.Standard {
+                       vcfg.Standard[p1.ImportPath] = true
+               }
        }
 }
 
@@ -812,7 +819,10 @@ func (b *Builder) vet(a *Action) error {
        if vcfg.ImportMap["fmt"] == "" {
                a1 := a.Deps[1]
                vcfg.ImportMap["fmt"] = "fmt"
-               vcfg.PackageFile["fmt"] = a1.built
+               if a1.built != "" {
+                       vcfg.PackageFile["fmt"] = a1.built
+               }
+               vcfg.Standard["fmt"] = true
        }
 
        // During go test, ignore type-checking failures during vet.
index 7265aa6f5793f7ca6b8f0407414db5fd57547464..4422add72f24250c2b4ba2b722fcda179cd35e6e 100644 (file)
@@ -294,6 +294,7 @@ type vetConfig struct {
        GoFiles     []string
        ImportMap   map[string]string
        PackageFile map[string]string
+       Standard    map[string]bool
 
        SucceedOnTypecheckFailure bool
 
@@ -312,6 +313,11 @@ func (v *vetConfig) Import(path string) (*types.Package, error) {
                return nil, fmt.Errorf("unknown import path %q", path)
        }
        if v.PackageFile[p] == "" {
+               if v.Compiler == "gccgo" && v.Standard[path] {
+                       // gccgo doesn't have sources for standard library packages,
+                       // but the importer will do the right thing.
+                       return v.imp.Import(path)
+               }
                return nil, fmt.Errorf("unknown package file for import %q", path)
        }
        return v.imp.Import(p)
@@ -320,6 +326,10 @@ func (v *vetConfig) Import(path string) (*types.Package, error) {
 func (v *vetConfig) openPackageFile(path string) (io.ReadCloser, error) {
        file := v.PackageFile[path]
        if file == "" {
+               if v.Compiler == "gccgo" && v.Standard[path] {
+                       // The importer knows how to handle this.
+                       return nil, nil
+               }
                // Note that path here has been translated via v.ImportMap,
                // unlike in the error in Import above. We prefer the error in
                // Import, but it's worth diagnosing this one too, just in case.
index 46544233dd4551493319537cf9eb308f3ed17e2e..d4998cf2a2e7a6b7e4e5d77d72938533c507c69a 100644 (file)
@@ -151,14 +151,17 @@ func GetImporter(searchpaths []string, initmap map[*types.Package]InitData) Impo
 
                var reader io.ReadSeeker
                var fpath string
+               var rc io.ReadCloser
                if lookup != nil {
                        if p := imports[pkgpath]; p != nil && p.Complete() {
                                return p, nil
                        }
-                       rc, err := lookup(pkgpath)
+                       rc, err = lookup(pkgpath)
                        if err != nil {
                                return nil, err
                        }
+               }
+               if rc != nil {
                        defer rc.Close()
                        rs, ok := rc.(io.ReadSeeker)
                        if !ok {