github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/test/internal/genflags/vetflag.go (about) 1 // Copyright 2019 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 genflags 6 7 import ( 8 "bytes" 9 "encoding/json" 10 "fmt" 11 "os/exec" 12 "regexp" 13 "sort" 14 15 "github.com/go-asm/go/cmd/go/base" 16 ) 17 18 // VetAnalyzers computes analyzers and their aliases supported by vet. 19 func VetAnalyzers() ([]string, error) { 20 // get supported vet flag information 21 tool := base.Tool("vet") 22 vetcmd := exec.Command(tool, "-flags") 23 out := new(bytes.Buffer) 24 vetcmd.Stdout = out 25 if err := vetcmd.Run(); err != nil { 26 return nil, fmt.Errorf("go vet: can't execute %s -flags: %v\n", tool, err) 27 } 28 var analysisFlags []struct { 29 Name string 30 Bool bool 31 Usage string 32 } 33 if err := json.Unmarshal(out.Bytes(), &analysisFlags); err != nil { 34 return nil, fmt.Errorf("go vet: can't unmarshal JSON from %s -flags: %v", tool, err) 35 } 36 37 // parse the flags to figure out which ones stand for analyses 38 analyzerSet := make(map[string]bool) 39 rEnable := regexp.MustCompile("^enable .+ analysis$") 40 for _, flag := range analysisFlags { 41 if rEnable.MatchString(flag.Usage) { 42 analyzerSet[flag.Name] = true 43 } 44 } 45 46 rDeprecated := regexp.MustCompile("^deprecated alias for -(?P<analyzer>(.+))$") 47 // Returns the original value matched by rDeprecated on input value. 48 // If there is no match, "" is returned. 49 originalValue := func(value string) string { 50 match := rDeprecated.FindStringSubmatch(value) 51 if len(match) < 2 { 52 return "" 53 } 54 return match[1] 55 } 56 // extract deprecated aliases for existing analyses 57 for _, flag := range analysisFlags { 58 if o := originalValue(flag.Usage); analyzerSet[o] { 59 analyzerSet[flag.Name] = true 60 } 61 } 62 63 var analyzers []string 64 for a := range analyzerSet { 65 analyzers = append(analyzers, a) 66 } 67 sort.Strings(analyzers) 68 return analyzers, nil 69 }