| 1 | // Copyright 2018 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 multichecker defines the main function for an analysis driver |
| 6 | // with several analyzers. This package makes it easy for anyone to build |
| 7 | // an analysis tool containing just the analyzers they need. |
| 8 | package multichecker |
| 9 | |
| 10 | import ( |
| 11 | "flag" |
| 12 | "fmt" |
| 13 | "log" |
| 14 | "os" |
| 15 | "path/filepath" |
| 16 | "strings" |
| 17 | |
| 18 | "golang.org/x/tools/go/analysis" |
| 19 | "golang.org/x/tools/go/analysis/internal/analysisflags" |
| 20 | "golang.org/x/tools/go/analysis/internal/checker" |
| 21 | "golang.org/x/tools/go/analysis/unitchecker" |
| 22 | ) |
| 23 | |
| 24 | func Main(analyzers ...*analysis.Analyzer) { |
| 25 | progname := filepath.Base(os.Args[0]) |
| 26 | log.SetFlags(0) |
| 27 | log.SetPrefix(progname + ": ") // e.g. "vet: " |
| 28 | |
| 29 | if err := analysis.Validate(analyzers); err != nil { |
| 30 | log.Fatal(err) |
| 31 | } |
| 32 | |
| 33 | checker.RegisterFlags() |
| 34 | |
| 35 | analyzers = analysisflags.Parse(analyzers, true) |
| 36 | |
| 37 | args := flag.Args() |
| 38 | if len(args) == 0 { |
| 39 | fmt.Fprintf(os.Stderr, `%[1]s is a tool for static analysis of Go programs. |
| 40 | |
| 41 | Usage: %[1]s [-flag] [package] |
| 42 | |
| 43 | Run '%[1]s help' for more detail, |
| 44 | or '%[1]s help name' for details and flags of a specific analyzer. |
| 45 | `, progname) |
| 46 | os.Exit(1) |
| 47 | } |
| 48 | |
| 49 | if args[0] == "help" { |
| 50 | analysisflags.Help(progname, analyzers, args[1:]) |
| 51 | os.Exit(0) |
| 52 | } |
| 53 | |
| 54 | if len(args) == 1 && strings.HasSuffix(args[0], ".cfg") { |
| 55 | unitchecker.Run(args[0], analyzers) |
| 56 | panic("unreachable") |
| 57 | } |
| 58 | |
| 59 | os.Exit(checker.Run(args, analyzers)) |
| 60 | } |
| 61 |
Members