660a739fbbd4d3b9d17a0bd9ea2633971e102130
[gcc.git] / libgo / go / cmd / go / internal / vet / vet.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package vet implements the ``go vet'' command.
6 package vet
7
8 import (
9 "cmd/go/internal/base"
10 "cmd/go/internal/load"
11 "cmd/go/internal/modload"
12 "cmd/go/internal/work"
13 "path/filepath"
14 )
15
16 var CmdVet = &base.Command{
17 Run: runVet,
18 CustomFlags: true,
19 UsageLine: "go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages]",
20 Short: "report likely mistakes in packages",
21 Long: `
22 Vet runs the Go vet command on the packages named by the import paths.
23
24 For more about vet and its flags, see 'go doc cmd/vet'.
25 For more about specifying packages, see 'go help packages'.
26 For a list of checkers and their flags, see 'go tool vet help'.
27 For details of a specific checker such as 'printf', see 'go tool vet help printf'.
28
29 The -n flag prints commands that would be executed.
30 The -x flag prints commands as they are executed.
31
32 The -vettool=prog flag selects a different analysis tool with alternative
33 or additional checks.
34 For example, the 'shadow' analyzer can be built and run using these commands:
35
36 go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
37 go vet -vettool=$(which shadow)
38
39 The build flags supported by go vet are those that control package resolution
40 and execution, such as -n, -x, -v, -tags, and -toolexec.
41 For more about these flags, see 'go help build'.
42
43 See also: go fmt, go fix.
44 `,
45 }
46
47 func runVet(cmd *base.Command, args []string) {
48 modload.LoadTests = true
49
50 vetFlags, pkgArgs := vetFlags(vetUsage, args)
51
52 work.BuildInit()
53 work.VetFlags = vetFlags
54 work.VetExplicit = true
55 if vetTool != "" {
56 var err error
57 work.VetTool, err = filepath.Abs(vetTool)
58 if err != nil {
59 base.Fatalf("%v", err)
60 }
61 }
62
63 pkgs := load.PackagesForBuild(pkgArgs)
64 if len(pkgs) == 0 {
65 base.Fatalf("no packages to vet")
66 }
67
68 var b work.Builder
69 b.Init()
70
71 root := &work.Action{Mode: "go vet"}
72 for _, p := range pkgs {
73 _, ptest, pxtest, err := load.TestPackagesFor(p, nil)
74 if err != nil {
75 base.Errorf("%v", err)
76 continue
77 }
78 if len(ptest.GoFiles) == 0 && len(ptest.CgoFiles) == 0 && pxtest == nil {
79 base.Errorf("go vet %s: no Go files in %s", p.ImportPath, p.Dir)
80 continue
81 }
82 if len(ptest.GoFiles) > 0 || len(ptest.CgoFiles) > 0 {
83 root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, ptest))
84 }
85 if pxtest != nil {
86 root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest))
87 }
88 }
89 b.Do(root)
90 }