Skip to content

Commit e787b13

Browse files
committed
cmd/go: vet: pass non-.go files to vet tool
The "gofiles" cache entry has been renamed "srcfiles", and it includes non-Go files (.s, .c, .cxx) that belong to the package. It does not include raw cgo files. Added regression test. Fixes #27665 Change-Id: I4884fe9b4f823f50705f8c2d357a04a8e567734f Reviewed-on: https://go-review.googlesource.com/c/148904 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent b075dfb commit e787b13

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

src/cmd/go/internal/work/exec.go

+36-15
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ func (b *Builder) build(a *Action) (err error) {
377377
if b.NeedExport {
378378
p.Export = a.built
379379
}
380-
if need&needCompiledGoFiles != 0 && b.loadCachedGoFiles(a) {
380+
if need&needCompiledGoFiles != 0 && b.loadCachedSrcFiles(a) {
381381
need &^= needCompiledGoFiles
382382
}
383383
// Otherwise, we need to write files to a.Objdir (needVet, needCgoHdr).
@@ -575,7 +575,13 @@ func (b *Builder) build(a *Action) (err error) {
575575
b.cacheCgoHdr(a)
576576
}
577577
}
578-
b.cacheGofiles(a, gofiles)
578+
579+
var srcfiles []string // .go and non-.go
580+
srcfiles = append(srcfiles, gofiles...)
581+
srcfiles = append(srcfiles, sfiles...)
582+
srcfiles = append(srcfiles, cfiles...)
583+
srcfiles = append(srcfiles, cxxfiles...)
584+
b.cacheSrcFiles(a, srcfiles)
579585

580586
// Running cgo generated the cgo header.
581587
need &^= needCgoHdr
@@ -587,11 +593,11 @@ func (b *Builder) build(a *Action) (err error) {
587593

588594
// Prepare Go vet config if needed.
589595
if need&needVet != 0 {
590-
buildVetConfig(a, gofiles)
596+
buildVetConfig(a, srcfiles)
591597
need &^= needVet
592598
}
593599
if need&needCompiledGoFiles != 0 {
594-
if !b.loadCachedGoFiles(a) {
600+
if !b.loadCachedSrcFiles(a) {
595601
return fmt.Errorf("failed to cache compiled Go files")
596602
}
597603
need &^= needCompiledGoFiles
@@ -794,13 +800,13 @@ func (b *Builder) loadCachedCgoHdr(a *Action) bool {
794800
return err == nil
795801
}
796802

797-
func (b *Builder) cacheGofiles(a *Action, gofiles []string) {
803+
func (b *Builder) cacheSrcFiles(a *Action, srcfiles []string) {
798804
c := cache.Default()
799805
if c == nil {
800806
return
801807
}
802808
var buf bytes.Buffer
803-
for _, file := range gofiles {
809+
for _, file := range srcfiles {
804810
if !strings.HasPrefix(file, a.Objdir) {
805811
// not generated
806812
buf.WriteString("./")
@@ -815,42 +821,42 @@ func (b *Builder) cacheGofiles(a *Action, gofiles []string) {
815821
return
816822
}
817823
}
818-
c.PutBytes(cache.Subkey(a.actionID, "gofiles"), buf.Bytes())
824+
c.PutBytes(cache.Subkey(a.actionID, "srcfiles"), buf.Bytes())
819825
}
820826

821827
func (b *Builder) loadCachedVet(a *Action) bool {
822828
c := cache.Default()
823829
if c == nil {
824830
return false
825831
}
826-
list, _, err := c.GetBytes(cache.Subkey(a.actionID, "gofiles"))
832+
list, _, err := c.GetBytes(cache.Subkey(a.actionID, "srcfiles"))
827833
if err != nil {
828834
return false
829835
}
830-
var gofiles []string
836+
var srcfiles []string
831837
for _, name := range strings.Split(string(list), "\n") {
832838
if name == "" { // end of list
833839
continue
834840
}
835841
if strings.HasPrefix(name, "./") {
836-
gofiles = append(gofiles, name[2:])
842+
srcfiles = append(srcfiles, name[2:])
837843
continue
838844
}
839845
if err := b.loadCachedObjdirFile(a, c, name); err != nil {
840846
return false
841847
}
842-
gofiles = append(gofiles, a.Objdir+name)
848+
srcfiles = append(srcfiles, a.Objdir+name)
843849
}
844-
buildVetConfig(a, gofiles)
850+
buildVetConfig(a, srcfiles)
845851
return true
846852
}
847853

848-
func (b *Builder) loadCachedGoFiles(a *Action) bool {
854+
func (b *Builder) loadCachedSrcFiles(a *Action) bool {
849855
c := cache.Default()
850856
if c == nil {
851857
return false
852858
}
853-
list, _, err := c.GetBytes(cache.Subkey(a.actionID, "gofiles"))
859+
list, _, err := c.GetBytes(cache.Subkey(a.actionID, "srcfiles"))
854860
if err != nil {
855861
return false
856862
}
@@ -879,6 +885,7 @@ type vetConfig struct {
879885
Dir string // directory containing package
880886
ImportPath string // canonical import path ("package path")
881887
GoFiles []string // absolute paths to package source files
888+
NonGoFiles []string // absolute paths to package non-Go files
882889

883890
ImportMap map[string]string // map import path in source code to package path
884891
PackageFile map[string]string // map package path to .a file with export data
@@ -890,7 +897,18 @@ type vetConfig struct {
890897
SucceedOnTypecheckFailure bool // awful hack; see #18395 and below
891898
}
892899

893-
func buildVetConfig(a *Action, gofiles []string) {
900+
func buildVetConfig(a *Action, srcfiles []string) {
901+
// Classify files based on .go extension.
902+
// srcfiles does not include raw cgo files.
903+
var gofiles, nongofiles []string
904+
for _, name := range srcfiles {
905+
if strings.HasSuffix(name, ".go") {
906+
gofiles = append(gofiles, name)
907+
} else {
908+
nongofiles = append(nongofiles, name)
909+
}
910+
}
911+
894912
// Pass list of absolute paths to vet,
895913
// so that vet's error messages will use absolute paths,
896914
// so that we can reformat them relative to the directory
@@ -899,6 +917,7 @@ func buildVetConfig(a *Action, gofiles []string) {
899917
Compiler: cfg.BuildToolchainName,
900918
Dir: a.Package.Dir,
901919
GoFiles: mkAbsFiles(a.Package.Dir, gofiles),
920+
NonGoFiles: mkAbsFiles(a.Package.Dir, nongofiles),
902921
ImportPath: a.Package.ImportPath,
903922
ImportMap: make(map[string]string),
904923
PackageFile: make(map[string]string),
@@ -995,6 +1014,8 @@ func (b *Builder) vet(a *Action) error {
9951014
}
9961015
}
9971016

1017+
// TODO(adonovan): delete this when we use the new vet printf checker.
1018+
// https://github.com/golang/go/issues/28756
9981019
if vcfg.ImportMap["fmt"] == "" {
9991020
a1 := a.Deps[1]
10001021
vcfg.ImportMap["fmt"] = "fmt"
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Issue 27665. Verify that "go vet" analyzes non-Go files.
2+
3+
env GOARCH=amd64
4+
! go vet -asmdecl a
5+
stderr 'f: invalid MOVW of x'
6+
7+
-- a/a.go --
8+
package a
9+
10+
func f(x int8)
11+
12+
-- a/asm.s --
13+
TEXT ·f(SB),0,$0-1
14+
MOVW x+0(FP), AX
15+
RET

src/cmd/vet/main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ type vetConfig struct {
365365
Dir string
366366
ImportPath string
367367
GoFiles []string
368+
NonGoFiles []string
368369
ImportMap map[string]string
369370
PackageFile map[string]string
370371
Standard map[string]bool
@@ -430,7 +431,12 @@ func doPackageCfg(cfgFile string) {
430431
stdImporter = &vcfg
431432
inittypes()
432433
mustTypecheck = true
433-
doPackage(vcfg.GoFiles, nil)
434+
435+
var allFiles []string
436+
allFiles = append(allFiles, vcfg.GoFiles...)
437+
allFiles = append(allFiles, vcfg.NonGoFiles...)
438+
439+
doPackage(allFiles, nil)
434440
if vcfg.VetxOutput != "" {
435441
out := make([]vetxExport, 0, len(exporters))
436442
for name, fn := range exporters {

0 commit comments

Comments
 (0)