cmd.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // Package lintcmd implements the frontend of an analysis runner.
  2. // It serves as the entry-point for the staticcheck command, and can also be used to implement custom linters that behave like staticcheck.
  3. package lintcmd
  4. import (
  5. "bufio"
  6. "encoding/gob"
  7. "flag"
  8. "fmt"
  9. "go/token"
  10. "io"
  11. "log"
  12. "os"
  13. "path/filepath"
  14. "reflect"
  15. "runtime"
  16. "runtime/pprof"
  17. "runtime/trace"
  18. "sort"
  19. "strings"
  20. "sync"
  21. "time"
  22. "honnef.co/go/tools/analysis/lint"
  23. "honnef.co/go/tools/config"
  24. "honnef.co/go/tools/go/loader"
  25. "honnef.co/go/tools/lintcmd/version"
  26. "golang.org/x/tools/go/analysis"
  27. "golang.org/x/tools/go/buildutil"
  28. )
  29. type BuildConfig struct {
  30. Name string
  31. Envs []string
  32. Flags []string
  33. }
  34. // Command represents a linter command line tool.
  35. type Command struct {
  36. name string
  37. analyzers map[string]*lint.Analyzer
  38. version string
  39. machineVersion string
  40. flags struct {
  41. fs *flag.FlagSet
  42. tags string
  43. tests bool
  44. showIgnored bool
  45. formatter string
  46. // mutually exclusive mode flags
  47. explain string
  48. printVersion bool
  49. listChecks bool
  50. merge bool
  51. matrix bool
  52. debugCpuprofile string
  53. debugMemprofile string
  54. debugVersion bool
  55. debugNoCompileErrors bool
  56. debugMeasureAnalyzers string
  57. debugTrace string
  58. checks list
  59. fail list
  60. goVersion versionFlag
  61. }
  62. }
  63. // NewCommand returns a new Command.
  64. func NewCommand(name string) *Command {
  65. cmd := &Command{
  66. name: name,
  67. analyzers: map[string]*lint.Analyzer{},
  68. version: "devel",
  69. machineVersion: "devel",
  70. }
  71. cmd.initFlagSet(name)
  72. return cmd
  73. }
  74. // SetVersion sets the command's version.
  75. // It is divided into a human part and a machine part.
  76. // For example, Staticcheck 2020.2.1 had the human version "2020.2.1" and the machine version "v0.1.1".
  77. // If you only use Semver, you can set both parts to the same value.
  78. //
  79. // Calling this method is optional. Both versions default to "devel", and we'll attempt to deduce more version information from the Go module.
  80. func (cmd *Command) SetVersion(human, machine string) {
  81. cmd.version = human
  82. cmd.machineVersion = machine
  83. }
  84. // FlagSet returns the command's flag set.
  85. // This can be used to add additional command line arguments.
  86. func (cmd *Command) FlagSet() *flag.FlagSet {
  87. return cmd.flags.fs
  88. }
  89. // AddAnalyzers adds analyzers to the command.
  90. // These are lint.Analyzer analyzers, which wrap analysis.Analyzer analyzers, bundling them with structured documentation.
  91. //
  92. // To add analysis.Analyzer analyzers without providing structured documentation, use AddBareAnalyzers.
  93. func (cmd *Command) AddAnalyzers(as ...*lint.Analyzer) {
  94. for _, a := range as {
  95. cmd.analyzers[a.Analyzer.Name] = a
  96. }
  97. }
  98. // AddBareAnalyzers adds bare analyzers to the command.
  99. func (cmd *Command) AddBareAnalyzers(as ...*analysis.Analyzer) {
  100. for _, a := range as {
  101. var title, text string
  102. if idx := strings.Index(a.Doc, "\n\n"); idx > -1 {
  103. title = a.Doc[:idx]
  104. text = a.Doc[idx+2:]
  105. }
  106. doc := &lint.Documentation{
  107. Title: title,
  108. Text: text,
  109. Severity: lint.SeverityWarning,
  110. }
  111. cmd.analyzers[a.Name] = &lint.Analyzer{
  112. Doc: doc,
  113. Analyzer: a,
  114. }
  115. }
  116. }
  117. func (cmd *Command) initFlagSet(name string) {
  118. flags := flag.NewFlagSet("", flag.ExitOnError)
  119. cmd.flags.fs = flags
  120. flags.Usage = usage(name, flags)
  121. flags.StringVar(&cmd.flags.tags, "tags", "", "List of `build tags`")
  122. flags.BoolVar(&cmd.flags.tests, "tests", true, "Include tests")
  123. flags.BoolVar(&cmd.flags.printVersion, "version", false, "Print version and exit")
  124. flags.BoolVar(&cmd.flags.showIgnored, "show-ignored", false, "Don't filter ignored diagnostics")
  125. flags.StringVar(&cmd.flags.formatter, "f", "text", "Output `format` (valid choices are 'stylish', 'text' and 'json')")
  126. flags.StringVar(&cmd.flags.explain, "explain", "", "Print description of `check`")
  127. flags.BoolVar(&cmd.flags.listChecks, "list-checks", false, "List all available checks")
  128. flags.BoolVar(&cmd.flags.merge, "merge", false, "Merge results of multiple Staticcheck runs")
  129. flags.BoolVar(&cmd.flags.matrix, "matrix", false, "Read a build config matrix from stdin")
  130. flags.StringVar(&cmd.flags.debugCpuprofile, "debug.cpuprofile", "", "Write CPU profile to `file`")
  131. flags.StringVar(&cmd.flags.debugMemprofile, "debug.memprofile", "", "Write memory profile to `file`")
  132. flags.BoolVar(&cmd.flags.debugVersion, "debug.version", false, "Print detailed version information about this program")
  133. flags.BoolVar(&cmd.flags.debugNoCompileErrors, "debug.no-compile-errors", false, "Don't print compile errors")
  134. flags.StringVar(&cmd.flags.debugMeasureAnalyzers, "debug.measure-analyzers", "", "Write analysis measurements to `file`. `file` will be opened for appending if it already exists.")
  135. flags.StringVar(&cmd.flags.debugTrace, "debug.trace", "", "Write trace to `file`")
  136. cmd.flags.checks = list{"inherit"}
  137. cmd.flags.fail = list{"all"}
  138. cmd.flags.goVersion = versionFlag("module")
  139. flags.Var(&cmd.flags.checks, "checks", "Comma-separated list of `checks` to enable.")
  140. flags.Var(&cmd.flags.fail, "fail", "Comma-separated list of `checks` that can cause a non-zero exit status.")
  141. flags.Var(&cmd.flags.goVersion, "go", "Target Go `version` in the format '1.x', or the literal 'module' to use the module's Go version")
  142. }
  143. type list []string
  144. func (list *list) String() string {
  145. return `"` + strings.Join(*list, ",") + `"`
  146. }
  147. func (list *list) Set(s string) error {
  148. if s == "" {
  149. *list = nil
  150. return nil
  151. }
  152. elems := strings.Split(s, ",")
  153. for i, elem := range elems {
  154. elems[i] = strings.TrimSpace(elem)
  155. }
  156. *list = elems
  157. return nil
  158. }
  159. type versionFlag string
  160. func (v *versionFlag) String() string {
  161. return fmt.Sprintf("%q", string(*v))
  162. }
  163. func (v *versionFlag) Set(s string) error {
  164. if s == "module" {
  165. *v = "module"
  166. } else {
  167. var vf lint.VersionFlag
  168. if err := vf.Set(s); err != nil {
  169. return err
  170. }
  171. *v = versionFlag(s)
  172. }
  173. return nil
  174. }
  175. // ParseFlags parses command line flags.
  176. // It must be called before calling Run.
  177. // After calling ParseFlags, the values of flags can be accessed.
  178. //
  179. // Example:
  180. //
  181. // cmd.ParseFlags(os.Args[1:])
  182. func (cmd *Command) ParseFlags(args []string) {
  183. cmd.flags.fs.Parse(args)
  184. }
  185. // diagnosticDescriptor represents the uniquiely identifying information of diagnostics.
  186. type diagnosticDescriptor struct {
  187. Position token.Position
  188. End token.Position
  189. Category string
  190. Message string
  191. }
  192. func (diag diagnostic) descriptor() diagnosticDescriptor {
  193. return diagnosticDescriptor{
  194. Position: diag.Position,
  195. End: diag.End,
  196. Category: diag.Category,
  197. Message: diag.Message,
  198. }
  199. }
  200. type run struct {
  201. checkedFiles map[string]struct{}
  202. diagnostics map[diagnosticDescriptor]diagnostic
  203. }
  204. func runFromLintResult(res LintResult) run {
  205. out := run{
  206. checkedFiles: map[string]struct{}{},
  207. diagnostics: map[diagnosticDescriptor]diagnostic{},
  208. }
  209. for _, cf := range res.CheckedFiles {
  210. out.checkedFiles[cf] = struct{}{}
  211. }
  212. for _, diag := range res.Diagnostics {
  213. out.diagnostics[diag.descriptor()] = diag
  214. }
  215. return out
  216. }
  217. func decodeGob(br io.ByteReader) ([]run, error) {
  218. var runs []run
  219. for {
  220. var res LintResult
  221. if err := gob.NewDecoder(br.(io.Reader)).Decode(&res); err != nil {
  222. if err == io.EOF {
  223. break
  224. } else {
  225. return nil, err
  226. }
  227. }
  228. runs = append(runs, runFromLintResult(res))
  229. }
  230. return runs, nil
  231. }
  232. // Run runs all registered analyzers and reports their findings.
  233. // It always calls os.Exit and does not return.
  234. func (cmd *Command) Run() {
  235. var measureAnalyzers func(analysis *analysis.Analyzer, pkg *loader.PackageSpec, d time.Duration)
  236. if path := cmd.flags.debugMeasureAnalyzers; path != "" {
  237. f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  238. if err != nil {
  239. log.Fatal(err)
  240. }
  241. mu := &sync.Mutex{}
  242. measureAnalyzers = func(analysis *analysis.Analyzer, pkg *loader.PackageSpec, d time.Duration) {
  243. mu.Lock()
  244. defer mu.Unlock()
  245. // FIXME(dh): print pkg.ID
  246. if _, err := fmt.Fprintf(f, "%s\t%s\t%d\n", analysis.Name, pkg, d.Nanoseconds()); err != nil {
  247. log.Println("error writing analysis measurements:", err)
  248. }
  249. }
  250. }
  251. if path := cmd.flags.debugCpuprofile; path != "" {
  252. f, err := os.Create(path)
  253. if err != nil {
  254. log.Fatal(err)
  255. }
  256. pprof.StartCPUProfile(f)
  257. }
  258. if path := cmd.flags.debugTrace; path != "" {
  259. f, err := os.Create(path)
  260. if err != nil {
  261. log.Fatal(err)
  262. }
  263. trace.Start(f)
  264. }
  265. defaultChecks := []string{"all"}
  266. cs := make([]*lint.Analyzer, 0, len(cmd.analyzers))
  267. for _, a := range cmd.analyzers {
  268. cs = append(cs, a)
  269. if a.Doc.NonDefault {
  270. defaultChecks = append(defaultChecks, "-"+a.Analyzer.Name)
  271. }
  272. }
  273. config.DefaultConfig.Checks = defaultChecks
  274. switch {
  275. case cmd.flags.debugVersion:
  276. version.Verbose(cmd.version, cmd.machineVersion)
  277. cmd.exit(0)
  278. case cmd.flags.listChecks:
  279. sort.Slice(cs, func(i, j int) bool {
  280. return cs[i].Analyzer.Name < cs[j].Analyzer.Name
  281. })
  282. for _, c := range cs {
  283. var title string
  284. if c.Doc != nil {
  285. title = c.Doc.Title
  286. }
  287. fmt.Printf("%s %s\n", c.Analyzer.Name, title)
  288. }
  289. cmd.exit(0)
  290. case cmd.flags.printVersion:
  291. version.Print(cmd.version, cmd.machineVersion)
  292. cmd.exit(0)
  293. case cmd.flags.explain != "":
  294. explain := cmd.flags.explain
  295. check, ok := cmd.analyzers[explain]
  296. if !ok {
  297. fmt.Fprintln(os.Stderr, "Couldn't find check", explain)
  298. cmd.exit(1)
  299. }
  300. if check.Analyzer.Doc == "" {
  301. fmt.Fprintln(os.Stderr, explain, "has no documentation")
  302. cmd.exit(1)
  303. }
  304. fmt.Println(check.Doc)
  305. fmt.Println("Online documentation\n https://staticcheck.io/docs/checks#" + check.Analyzer.Name)
  306. cmd.exit(0)
  307. case cmd.flags.merge:
  308. var runs []run
  309. if len(cmd.flags.fs.Args()) == 0 {
  310. var err error
  311. runs, err = decodeGob(bufio.NewReader(os.Stdin))
  312. if err != nil {
  313. fmt.Fprintln(os.Stderr, fmt.Errorf("couldn't parse stdin: %s", err))
  314. cmd.exit(1)
  315. }
  316. } else {
  317. for _, path := range cmd.flags.fs.Args() {
  318. someRuns, err := func(path string) ([]run, error) {
  319. f, err := os.Open(path)
  320. if err != nil {
  321. return nil, err
  322. }
  323. defer f.Close()
  324. br := bufio.NewReader(f)
  325. return decodeGob(br)
  326. }(path)
  327. if err != nil {
  328. fmt.Fprintln(os.Stderr, fmt.Errorf("couldn't parse file %s: %s", path, err))
  329. cmd.exit(1)
  330. }
  331. runs = append(runs, someRuns...)
  332. }
  333. }
  334. relevantDiagnostics := mergeRuns(runs)
  335. cmd.printDiagnostics(cs, relevantDiagnostics)
  336. default:
  337. switch cmd.flags.formatter {
  338. case "text", "stylish", "json", "sarif", "binary", "null":
  339. default:
  340. fmt.Fprintf(os.Stderr, "unsupported output format %q\n", cmd.flags.formatter)
  341. cmd.exit(2)
  342. }
  343. var bconfs []BuildConfig
  344. if cmd.flags.matrix {
  345. if cmd.flags.tags != "" {
  346. fmt.Fprintln(os.Stderr, "cannot use -matrix and -tags together")
  347. cmd.exit(2)
  348. }
  349. var err error
  350. bconfs, err = parseBuildConfigs(os.Stdin)
  351. if err != nil {
  352. if perr, ok := err.(parseBuildConfigError); ok {
  353. fmt.Fprintf(os.Stderr, "<stdin>:%d couldn't parse build matrix: %s\n", perr.line, perr.err)
  354. } else {
  355. fmt.Fprintln(os.Stderr, err)
  356. }
  357. os.Exit(2)
  358. }
  359. } else {
  360. bc := BuildConfig{}
  361. if cmd.flags.tags != "" {
  362. // Validate that the tags argument is well-formed. go/packages
  363. // doesn't detect malformed build flags and returns unhelpful
  364. // errors.
  365. tf := buildutil.TagsFlag{}
  366. if err := tf.Set(cmd.flags.tags); err != nil {
  367. fmt.Fprintln(os.Stderr, fmt.Errorf("invalid value %q for flag -tags: %s", cmd.flags.tags, err))
  368. cmd.exit(1)
  369. }
  370. bc.Flags = []string{"-tags", cmd.flags.tags}
  371. }
  372. bconfs = append(bconfs, bc)
  373. }
  374. var runs []run
  375. for _, bconf := range bconfs {
  376. res, err := doLint(cs, cmd.flags.fs.Args(), &options{
  377. BuildConfig: bconf,
  378. LintTests: cmd.flags.tests,
  379. GoVersion: string(cmd.flags.goVersion),
  380. Config: config.Config{
  381. Checks: cmd.flags.checks,
  382. },
  383. PrintAnalyzerMeasurement: measureAnalyzers,
  384. })
  385. if err != nil {
  386. fmt.Fprintln(os.Stderr, err)
  387. cmd.exit(1)
  388. }
  389. for _, w := range res.Warnings {
  390. fmt.Fprintln(os.Stderr, "warning:", w)
  391. }
  392. cwd, err := os.Getwd()
  393. if err != nil {
  394. cwd = ""
  395. }
  396. relPath := func(s string) string {
  397. if cwd == "" {
  398. return filepath.ToSlash(s)
  399. }
  400. out, err := filepath.Rel(cwd, s)
  401. if err != nil {
  402. return filepath.ToSlash(s)
  403. }
  404. return filepath.ToSlash(out)
  405. }
  406. if cmd.flags.formatter == "binary" {
  407. for i, s := range res.CheckedFiles {
  408. res.CheckedFiles[i] = relPath(s)
  409. }
  410. for i := range res.Diagnostics {
  411. // We turn all paths into relative, /-separated paths. This is to make -merge work correctly when
  412. // merging runs from different OSs, with different absolute paths.
  413. //
  414. // We zero out Offset, because checkouts of code on different OSs may have different kinds of
  415. // newlines and thus different offsets. We don't ever make use of the Offset, anyway. Line and
  416. // column numbers are precomputed.
  417. d := &res.Diagnostics[i]
  418. d.Position.Filename = relPath(d.Position.Filename)
  419. d.Position.Offset = 0
  420. d.End.Filename = relPath(d.End.Filename)
  421. d.End.Offset = 0
  422. for j := range d.Related {
  423. r := &d.Related[j]
  424. r.Position.Filename = relPath(r.Position.Filename)
  425. r.Position.Offset = 0
  426. r.End.Filename = relPath(r.End.Filename)
  427. r.End.Offset = 0
  428. }
  429. }
  430. err := gob.NewEncoder(os.Stdout).Encode(res)
  431. if err != nil {
  432. fmt.Fprintf(os.Stderr, "failed writing output: %s\n", err)
  433. cmd.exit(2)
  434. }
  435. } else {
  436. runs = append(runs, runFromLintResult(res))
  437. }
  438. }
  439. if cmd.flags.formatter != "binary" {
  440. diags := mergeRuns(runs)
  441. cmd.printDiagnostics(cs, diags)
  442. }
  443. }
  444. }
  445. func mergeRuns(runs []run) []diagnostic {
  446. var relevantDiagnostics []diagnostic
  447. for _, r := range runs {
  448. for _, diag := range r.diagnostics {
  449. switch diag.MergeIf {
  450. case lint.MergeIfAny:
  451. relevantDiagnostics = append(relevantDiagnostics, diag)
  452. case lint.MergeIfAll:
  453. doPrint := true
  454. for _, r := range runs {
  455. if _, ok := r.checkedFiles[diag.Position.Filename]; ok {
  456. if _, ok := r.diagnostics[diag.descriptor()]; !ok {
  457. doPrint = false
  458. }
  459. }
  460. }
  461. if doPrint {
  462. relevantDiagnostics = append(relevantDiagnostics, diag)
  463. }
  464. }
  465. }
  466. }
  467. return relevantDiagnostics
  468. }
  469. func (cmd *Command) exit(code int) {
  470. if cmd.flags.debugCpuprofile != "" {
  471. pprof.StopCPUProfile()
  472. }
  473. if path := cmd.flags.debugMemprofile; path != "" {
  474. f, err := os.Create(path)
  475. if err != nil {
  476. panic(err)
  477. }
  478. runtime.GC()
  479. pprof.WriteHeapProfile(f)
  480. }
  481. if cmd.flags.debugTrace != "" {
  482. trace.Stop()
  483. }
  484. os.Exit(code)
  485. }
  486. func (cmd *Command) printDiagnostics(cs []*lint.Analyzer, diagnostics []diagnostic) {
  487. if len(diagnostics) > 1 {
  488. sort.Slice(diagnostics, func(i, j int) bool {
  489. di := diagnostics[i]
  490. dj := diagnostics[j]
  491. pi := di.Position
  492. pj := dj.Position
  493. if pi.Filename != pj.Filename {
  494. return pi.Filename < pj.Filename
  495. }
  496. if pi.Line != pj.Line {
  497. return pi.Line < pj.Line
  498. }
  499. if pi.Column != pj.Column {
  500. return pi.Column < pj.Column
  501. }
  502. if di.Message != dj.Message {
  503. return di.Message < dj.Message
  504. }
  505. if di.BuildName != dj.BuildName {
  506. return di.BuildName < dj.BuildName
  507. }
  508. return di.Category < dj.Category
  509. })
  510. filtered := []diagnostic{
  511. diagnostics[0],
  512. }
  513. builds := []map[string]struct{}{
  514. {diagnostics[0].BuildName: {}},
  515. }
  516. for _, diag := range diagnostics[1:] {
  517. // We may encounter duplicate diagnostics because one file
  518. // can be part of many packages, and because multiple
  519. // build configurations may check the same files.
  520. if !filtered[len(filtered)-1].equal(diag) {
  521. if filtered[len(filtered)-1].descriptor() == diag.descriptor() {
  522. // Diagnostics only differ in build name, track new name
  523. builds[len(filtered)-1][diag.BuildName] = struct{}{}
  524. } else {
  525. filtered = append(filtered, diag)
  526. builds = append(builds, map[string]struct{}{})
  527. builds[len(filtered)-1][diag.BuildName] = struct{}{}
  528. }
  529. }
  530. }
  531. var names []string
  532. for i := range filtered {
  533. names = names[:0]
  534. for k := range builds[i] {
  535. names = append(names, k)
  536. }
  537. sort.Strings(names)
  538. filtered[i].BuildName = strings.Join(names, ",")
  539. }
  540. diagnostics = filtered
  541. }
  542. var f formatter
  543. switch cmd.flags.formatter {
  544. case "text":
  545. f = textFormatter{W: os.Stdout}
  546. case "stylish":
  547. f = &stylishFormatter{W: os.Stdout}
  548. case "json":
  549. f = jsonFormatter{W: os.Stdout}
  550. case "sarif":
  551. f = &sarifFormatter{
  552. driverName: cmd.name,
  553. driverVersion: cmd.version,
  554. }
  555. if cmd.name == "staticcheck" {
  556. f.(*sarifFormatter).driverName = "Staticcheck"
  557. f.(*sarifFormatter).driverWebsite = "https://staticcheck.io"
  558. }
  559. case "binary":
  560. fmt.Fprintln(os.Stderr, "'-f binary' not supported in this context")
  561. cmd.exit(2)
  562. case "null":
  563. f = nullFormatter{}
  564. default:
  565. fmt.Fprintf(os.Stderr, "unsupported output format %q\n", cmd.flags.formatter)
  566. cmd.exit(2)
  567. }
  568. fail := cmd.flags.fail
  569. analyzerNames := make([]string, len(cs))
  570. for i, a := range cs {
  571. analyzerNames[i] = a.Analyzer.Name
  572. }
  573. shouldExit := filterAnalyzerNames(analyzerNames, fail)
  574. shouldExit["staticcheck"] = true
  575. shouldExit["compile"] = true
  576. var (
  577. numErrors int
  578. numWarnings int
  579. numIgnored int
  580. )
  581. notIgnored := make([]diagnostic, 0, len(diagnostics))
  582. for _, diag := range diagnostics {
  583. if diag.Category == "compile" && cmd.flags.debugNoCompileErrors {
  584. continue
  585. }
  586. if diag.Severity == severityIgnored && !cmd.flags.showIgnored {
  587. numIgnored++
  588. continue
  589. }
  590. if shouldExit[diag.Category] {
  591. numErrors++
  592. } else {
  593. diag.Severity = severityWarning
  594. numWarnings++
  595. }
  596. notIgnored = append(notIgnored, diag)
  597. }
  598. f.Format(cs, notIgnored)
  599. if f, ok := f.(statter); ok {
  600. f.Stats(len(diagnostics), numErrors, numWarnings, numIgnored)
  601. }
  602. if numErrors > 0 {
  603. if _, ok := f.(*sarifFormatter); ok {
  604. // When emitting SARIF, finding errors is considered success.
  605. cmd.exit(0)
  606. } else {
  607. cmd.exit(1)
  608. }
  609. }
  610. cmd.exit(0)
  611. }
  612. func usage(name string, fs *flag.FlagSet) func() {
  613. return func() {
  614. fmt.Fprintf(os.Stderr, "Usage: %s [flags] [packages]\n", name)
  615. fmt.Fprintln(os.Stderr)
  616. fmt.Fprintln(os.Stderr, "Flags:")
  617. printDefaults(fs)
  618. fmt.Fprintln(os.Stderr)
  619. fmt.Fprintln(os.Stderr, "For help about specifying packages, see 'go help packages'")
  620. }
  621. }
  622. // isZeroValue determines whether the string represents the zero
  623. // value for a flag.
  624. //
  625. // this function has been copied from the Go standard library's 'flag' package.
  626. func isZeroValue(f *flag.Flag, value string) bool {
  627. // Build a zero value of the flag's Value type, and see if the
  628. // result of calling its String method equals the value passed in.
  629. // This works unless the Value type is itself an interface type.
  630. typ := reflect.TypeOf(f.Value)
  631. var z reflect.Value
  632. if typ.Kind() == reflect.Ptr {
  633. z = reflect.New(typ.Elem())
  634. } else {
  635. z = reflect.Zero(typ)
  636. }
  637. return value == z.Interface().(flag.Value).String()
  638. }
  639. // this function has been copied from the Go standard library's 'flag' package and modified to skip debug flags.
  640. func printDefaults(fs *flag.FlagSet) {
  641. fs.VisitAll(func(f *flag.Flag) {
  642. // Don't print debug flags
  643. if strings.HasPrefix(f.Name, "debug.") {
  644. return
  645. }
  646. var b strings.Builder
  647. fmt.Fprintf(&b, " -%s", f.Name) // Two spaces before -; see next two comments.
  648. name, usage := flag.UnquoteUsage(f)
  649. if len(name) > 0 {
  650. b.WriteString(" ")
  651. b.WriteString(name)
  652. }
  653. // Boolean flags of one ASCII letter are so common we
  654. // treat them specially, putting their usage on the same line.
  655. if b.Len() <= 4 { // space, space, '-', 'x'.
  656. b.WriteString("\t")
  657. } else {
  658. // Four spaces before the tab triggers good alignment
  659. // for both 4- and 8-space tab stops.
  660. b.WriteString("\n \t")
  661. }
  662. b.WriteString(strings.ReplaceAll(usage, "\n", "\n \t"))
  663. if !isZeroValue(f, f.DefValue) {
  664. if T := reflect.TypeOf(f.Value); T.Name() == "*stringValue" && T.PkgPath() == "flag" {
  665. // put quotes on the value
  666. fmt.Fprintf(&b, " (default %q)", f.DefValue)
  667. } else {
  668. fmt.Fprintf(&b, " (default %v)", f.DefValue)
  669. }
  670. }
  671. fmt.Fprint(fs.Output(), b.String(), "\n")
  672. })
  673. }