-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess.go
207 lines (167 loc) · 4.99 KB
/
process.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package tracegen
import (
"bytes"
"go/token"
"os"
"path/filepath"
"github.com/dave/dst"
"github.com/dave/dst/decorator"
"github.com/dave/dst/decorator/resolver"
"github.com/pkg/errors"
)
var (
writer func(name string, data []byte, perm os.FileMode) error = os.WriteFile
)
// Process applies the specified update function to relevant functions discovered
// within packages matching the passed-in package patterns. The supplied resolver
// must be capable of matching any pre-existing import within the loaded packages
// as well as any introduced by the update function.
func Process(settings Settings, packages []string, update func(fn *dst.FuncDecl, shouldSkip bool) (imports []string), getResolver func(pkg *decorator.Package, file *dst.File) resolver.RestorerResolver) (err error) {
pkgs, err := LoadPackages(packages)
if err != nil {
return
}
return ProcessPackages(settings, pkgs, update, getResolver)
}
func LoadPackages(packages []string) (pkgs []*decorator.Package, err error) {
pkgs, err = decorator.Load(nil, packages...)
if err != nil {
return nil, errors.Wrap(err, "failed to load packages")
}
return pkgs, nil
}
func ProcessPackages(settings Settings, pkgs []*decorator.Package, update func(fn *dst.FuncDecl, shouldSkip bool) (imports []string), getResolver func(pkg *decorator.Package, file *dst.File) resolver.RestorerResolver) (err error) {
for _, pkg := range pkgs {
var excluded bool
for _, pattern := range settings.excludePatterns {
if pattern.MatchString(filepath.Join(pkg.Dir, pkg.Name)) {
excluded = true
break
}
}
if excluded {
continue
}
// Types to skip, based on trace:skip tags
skipTypes := make(map[string]struct{})
// Types to include, based on trace:enable tags
enableTypes := make(map[string]struct{})
for _, file := range pkg.Syntax {
// Iterate through types first to build the skipTypes map
dst.Inspect(file, func(n dst.Node) bool {
switch node := n.(type) {
case *dst.GenDecl:
// Only process types
if node.Tok != token.TYPE {
return true
}
typeName := node.Specs[0].(*dst.TypeSpec).Name.Name
if explicitInclude(node.Decs.Start) {
enableTypes[typeName] = struct{}{}
break
}
if skipByName(settings, typeName) {
skipTypes[typeName] = struct{}{}
} else if skipByComments(settings, node.Decs.Start) {
skipTypes[typeName] = struct{}{}
} else if settings.Tagged {
skipTypes[typeName] = struct{}{}
}
}
return true
})
}
changed := make(map[string][]byte)
for _, file := range pkg.Syntax {
resolver := getResolver(pkg, file)
pre, err := fileContents(pkg, file, resolver)
if err != nil {
return err
}
var skipped bool
imports := make(map[string]struct{})
// Iterate through functions next
dst.Inspect(file, func(n dst.Node) bool {
switch node := n.(type) {
case *dst.FuncDecl:
// Whether this function should be skipped
var shouldSkip bool
defer func() {
if shouldSkip {
skipped = true
}
}()
// Whether this function should explicitly be included
shouldInclude := !settings.Tagged
if skipByName(settings, node.Name.Name) {
shouldSkip = true
}
if skipByComments(settings, node.Decs.Start) {
shouldSkip = true
}
// Check for a struct-level skip tag
if node.Recv != nil {
for _, field := range node.Recv.List {
typeName := typeNameFromFieldExpr(field.Type)
if typeName != "" {
if _, include := enableTypes[typeName]; include {
shouldInclude = true
} else if _, skip := skipTypes[typeName]; skip {
shouldSkip = true
}
}
}
} else if settings.Methods {
shouldSkip = true
}
if explicitInclude(node.Decs.Start) {
shouldSkip = false
} else if settings.Tagged && !shouldSkip {
shouldSkip = !shouldInclude
}
for _, imp := range update(node, shouldSkip) {
imports[imp] = struct{}{}
}
}
return true
})
if !skipped {
for imp := range imports {
addImport(pkg, file, imp)
}
}
post, err := fileContents(pkg, file, resolver)
if err != nil {
return err
}
if !bytes.Equal(pre, post) {
changed[pkg.Decorator.Filenames[file]] = post
}
}
for filename, data := range changed {
if err := writer(filename, data, 0666); err != nil {
return errors.Wrapf(err, "failed to save file %s", filename)
}
}
}
return nil
}
func fileContents(p *decorator.Package, file *dst.File, resolver resolver.RestorerResolver) (data []byte, err error) {
buf := &bytes.Buffer{}
r := decorator.NewRestorerWithImports(p.PkgPath, resolver)
if err := r.Fprint(buf, file); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func typeNameFromFieldExpr(expr dst.Expr) string {
switch expr := expr.(type) {
case *dst.Ident:
return expr.Name
case *dst.IndexExpr:
return typeNameFromFieldExpr(expr.X)
case *dst.StarExpr:
return typeNameFromFieldExpr(expr.X)
}
return ""
}