Debian Codegraph @

go/analysis/singlechecker/singlechecker.go

raw symbols SCIP
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 singlechecker defines the main function for an analysis
6 // driver with only a single analysis.
7 // This package makes it easy for a provider of an analysis package to
8 // also provide a standalone tool that runs just that analysis.
9 //
10 // For example, if example.org/findbadness is an analysis package,
11 // all that is needed to define a standalone tool is a file,
12 // example.org/findbadness/cmd/findbadness/main.go, containing:
13 //
14 // // The findbadness command runs an analysis.
15 // package main
16 //
17 // import (
18 // "example.org/findbadness"
19 // "golang.org/x/tools/go/analysis/singlechecker"
20 // )
21 //
22 // func main() { singlechecker.Main(findbadness.Analyzer) }
23 package singlechecker
24
25 import (
26 "flag"
27 "fmt"
28 "log"
29 "os"
30 "strings"
31
32 "golang.org/x/tools/go/analysis"
33 "golang.org/x/tools/go/analysis/internal/analysisflags"
34 "golang.org/x/tools/go/analysis/internal/checker"
35 "golang.org/x/tools/go/analysis/unitchecker"
36 )
37
38 // Main is the main function for a checker command for a single analysis.
39 func Main(a *analysis.Analyzer) {
40 log.SetFlags(0)
41 log.SetPrefix(a.Name + ": ")
42
43 analyzers := []*analysis.Analyzer{a}
44
45 if err := analysis.Validate(analyzers); err != nil {
46 log.Fatal(err)
47 }
48
49 checker.RegisterFlags()
50
51 flag.Usage = func() {
52 paras := strings.Split(a.Doc, "\n\n")
53 fmt.Fprintf(os.Stderr, "%s: %s\n\n", a.Name, paras[0])
54 fmt.Fprintf(os.Stderr, "Usage: %s [-flag] [package]\n\n", a.Name)
55 if len(paras) > 1 {
56 fmt.Fprintln(os.Stderr, strings.Join(paras[1:], "\n\n"))
57 }
58 fmt.Fprintln(os.Stderr, "\nFlags:")
59 flag.PrintDefaults()
60 }
61
62 analyzers = analysisflags.Parse(analyzers, false)
63
64 args := flag.Args()
65 if len(args) == 0 {
66 flag.Usage()
67 os.Exit(1)
68 }
69
70 if len(args) == 1 && strings.HasSuffix(args[0], ".cfg") {
71 unitchecker.Run(args[0], analyzers)
72 panic("unreachable")
73 }
74
75 os.Exit(checker.Run(args, analyzers))
76 }