-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmd_escape.go
89 lines (73 loc) · 1.88 KB
/
cmd_escape.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"flag"
"fmt"
"os/exec"
"regexp"
)
type escapeAnalysisRunner struct {
flagMod string
asJSON bool
messageRE *regexp.Regexp
}
func (r *escapeAnalysisRunner) ExecCommand(_ context.Context, args []string) error {
fset := flag.NewFlagSet("escapeAnalysis", flag.ContinueOnError)
fset.StringVar(&r.flagMod, "mod", "", `-mod compiler flag(readonly|vendor)`)
fset.BoolVar(&r.asJSON, "json", false, `return result as JSON`)
if err := fset.Parse(args); err != nil {
return err
}
args = fset.Args()
if len(args) == 0 {
args = []string{"."}
}
const location = `(.*:\d+:\d+)`
const variable = `(.*)`
const pat = location + `: ` + variable + `escapes to heap`
r.messageRE = regexp.MustCompile(pat)
for _, pkg := range args {
if err := r.process(pkg); err != nil {
fmt.Printf("%s: %v", pkg, err)
}
}
return nil
}
func (r *escapeAnalysisRunner) Init() {}
func (r *escapeAnalysisRunner) Run(pkg string) error { return nil }
func (r *escapeAnalysisRunner) process(pkg string) error {
cmd := exec.Command("go", r.getCmdArgs(pkg)...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%v: %s", err, out)
}
type escapeAnalysisResult struct {
Loc string `json:"loc"`
Variable string `json:"var"`
}
results := []escapeAnalysisResult{}
for _, submatches := range r.messageRE.FindAllStringSubmatch(string(out), -1) {
loc := submatches[1]
variable := submatches[2]
results = append(results, escapeAnalysisResult{
Loc: loc,
Variable: variable,
})
}
if r.asJSON {
marshalJSON(results)
return nil
}
for _, r := range results {
fmt.Printf("%s: %s\n", r.Loc, r.Variable)
}
return nil
}
func (r *escapeAnalysisRunner) getCmdArgs(pkg string) []string {
args := []string{"build", "-gcflags", "-m -m"}
if r.flagMod != "" {
args = append(args, "-mod", r.flagMod)
}
args = append(args, pkg)
return args
}