This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
forked from alexkohler/prealloc
-
Notifications
You must be signed in to change notification settings - Fork 4
/
prealloc.go
403 lines (349 loc) · 9.47 KB
/
prealloc.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package prealloc
import (
"errors"
"flag"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"strings"
)
// Support: (in order of priority)
// * Full make suggestion with type?
// * Test flag
// * Embedded ifs?
// * Use an import rather than the duplcated import.go
const (
pwd = "./"
)
func usage() {
log.Printf("Usage of %s:\n", os.Args[0])
log.Printf("\nprealloc [flags] # runs on package in current directory\n")
log.Printf("\nprealloc [flags] [packages]\n")
log.Printf("Flags:\n")
flag.PrintDefaults()
}
type sliceDeclaration struct {
name string
// sType string
genD *ast.GenDecl
}
type returnsVisitor struct {
// flags
simple bool
includeRangeLoops bool
includeForLoops bool
// visitor fields
sliceDeclarations []*sliceDeclaration
preallocHints []Hint
returnsInsideOfLoop bool
arrayTypes []string
}
func NoMain() {
// Remove log timestamp
log.SetFlags(0)
simple := flag.Bool("simple", true, "Report preallocation suggestions only on simple loops that have no returns/breaks/continues/gotos in them")
includeRangeLoops := flag.Bool("rangeloops", true, "Report preallocation suggestions on range loops")
includeForLoops := flag.Bool("forloops", false, "Report preallocation suggestions on for loops")
setExitStatus := flag.Bool("set_exit_status", false, "Set exit status to 1 if any issues are found")
flag.Usage = usage
flag.Parse()
hints, err := checkForPreallocations(flag.Args(), simple, includeRangeLoops, includeForLoops)
if err != nil {
log.Println(err)
}
for _, hint := range hints {
log.Println(hint)
}
if *setExitStatus && len(hints) > 0 {
os.Exit(1)
}
}
func checkForPreallocations(args []string, simple, includeRangeLoops *bool, includeForLoops *bool) ([]Hint, error) {
fset := token.NewFileSet()
files, err := parseInput(args, fset)
if err != nil {
return nil, fmt.Errorf("could not parse input %v", err)
}
if simple == nil {
return nil, errors.New("simple nil")
}
if includeRangeLoops == nil {
return nil, errors.New("includeRangeLoops nil")
}
if includeForLoops == nil {
return nil, errors.New("includeForLoops nil")
}
hints := []Hint{}
for _, f := range files {
retVis := &returnsVisitor{
simple: *simple,
includeRangeLoops: *includeRangeLoops,
includeForLoops: *includeForLoops,
}
ast.Walk(retVis, f)
// if simple is true, then we actually have to check if we had returns
// inside of our loop. Otherwise, we can just report all messages.
if !retVis.simple || !retVis.returnsInsideOfLoop {
hints = append(hints, retVis.preallocHints...)
}
}
return hints, nil
}
func Check(files []*ast.File, simple, includeRangeLoops, includeForLoops bool) []Hint {
hints := []Hint{}
for _, f := range files {
retVis := &returnsVisitor{
simple: simple,
includeRangeLoops: includeRangeLoops,
includeForLoops: includeForLoops,
}
ast.Walk(retVis, f)
// if simple is true, then we actually have to check if we had returns
// inside of our loop. Otherwise, we can just report all messages.
if !retVis.simple || !retVis.returnsInsideOfLoop {
hints = append(hints, retVis.preallocHints...)
}
}
return hints
}
func parseInput(args []string, fset *token.FileSet) ([]*ast.File, error) {
var directoryList []string
var fileMode bool
files := make([]*ast.File, 0)
if len(args) == 0 {
directoryList = append(directoryList, pwd)
} else {
for _, arg := range args {
if strings.HasSuffix(arg, "/...") && isDir(arg[:len(arg)-len("/...")]) {
for _, dirname := range allPackagesInFS(arg) {
directoryList = append(directoryList, dirname)
}
} else if isDir(arg) {
directoryList = append(directoryList, arg)
} else if exists(arg) {
if strings.HasSuffix(arg, ".go") {
fileMode = true
f, err := parser.ParseFile(fset, arg, nil, 0)
if err != nil {
return nil, err
}
files = append(files, f)
} else {
return nil, fmt.Errorf("invalid file %v specified", arg)
}
} else {
//TODO clean this up a bit
imPaths := importPaths([]string{arg})
for _, importPath := range imPaths {
pkg, err := build.Import(importPath, ".", 0)
if err != nil {
return nil, err
}
var stringFiles []string
stringFiles = append(stringFiles, pkg.GoFiles...)
// files = append(files, pkg.CgoFiles...)
stringFiles = append(stringFiles, pkg.TestGoFiles...)
if pkg.Dir != "." {
for i, f := range stringFiles {
stringFiles[i] = filepath.Join(pkg.Dir, f)
}
}
fileMode = true
for _, stringFile := range stringFiles {
f, err := parser.ParseFile(fset, stringFile, nil, 0)
if err != nil {
return nil, err
}
files = append(files, f)
}
}
}
}
}
// if we're not in file mode, then we need to grab each and every package in each directory
// we can to grab all the files
if !fileMode {
for _, fpath := range directoryList {
pkgs, err := parser.ParseDir(fset, fpath, nil, 0)
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
for _, f := range pkg.Files {
files = append(files, f)
}
}
}
}
return files, nil
}
func isDir(filename string) bool {
fi, err := os.Stat(filename)
return err == nil && fi.IsDir()
}
func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func (v *returnsVisitor) Visit(node ast.Node) ast.Visitor {
v.sliceDeclarations = nil
v.returnsInsideOfLoop = false
switch n := node.(type) {
case *ast.TypeSpec:
if _, ok := n.Type.(*ast.ArrayType); ok {
if n.Name != nil {
v.arrayTypes = append(v.arrayTypes, n.Name.Name)
}
}
case *ast.FuncDecl:
if n.Body != nil {
for _, stmt := range n.Body.List {
switch s := stmt.(type) {
// Find non pre-allocated slices
case *ast.DeclStmt:
genD, ok := s.Decl.(*ast.GenDecl)
if !ok {
continue
}
if genD.Tok == token.TYPE {
for _, spec := range genD.Specs {
tSpec, ok := spec.(*ast.TypeSpec)
if !ok {
continue
}
if _, ok := tSpec.Type.(*ast.ArrayType); ok {
if tSpec.Name != nil {
v.arrayTypes = append(v.arrayTypes, tSpec.Name.Name)
}
}
}
} else if genD.Tok == token.VAR {
for _, spec := range genD.Specs {
vSpec, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
var isArrType bool
switch val := vSpec.Type.(type) {
case *ast.ArrayType:
isArrType = true
case *ast.Ident:
isArrType = contains(v.arrayTypes, val.Name)
}
if isArrType {
if vSpec.Names != nil {
/*atID, ok := arrayType.Elt.(*ast.Ident)
if !ok {
continue
}*/
// We should handle multiple slices declared on same line e.g. var mySlice1, mySlice2 []uint32
for _, vName := range vSpec.Names {
v.sliceDeclarations = append(v.sliceDeclarations, &sliceDeclaration{name: vName.Name /*sType: atID.Name,*/, genD: genD})
}
}
}
}
}
case *ast.RangeStmt:
if v.includeRangeLoops {
if len(v.sliceDeclarations) == 0 {
continue
}
if s.Body != nil {
v.handleLoops(s.Body)
}
}
case *ast.ForStmt:
if v.includeForLoops {
if len(v.sliceDeclarations) == 0 {
continue
}
if s.Body != nil {
v.handleLoops(s.Body)
}
}
default:
}
}
}
}
return v
}
// handleLoops is a helper function to share the logic required for both *ast.RangeLoops and *ast.ForLoops
func (v *returnsVisitor) handleLoops(blockStmt *ast.BlockStmt) {
for _, stmt := range blockStmt.List {
switch bodyStmt := stmt.(type) {
case *ast.AssignStmt:
asgnStmt := bodyStmt
for _, expr := range asgnStmt.Rhs {
callExpr, ok := expr.(*ast.CallExpr)
if !ok {
continue // should this be break? comes back to multi-call support I think
}
ident, ok := callExpr.Fun.(*ast.Ident)
if !ok {
continue
}
if ident.Name == "append" {
// see if this append is appending the slice we found
for _, lhsExpr := range asgnStmt.Lhs {
lhsIdent, ok := lhsExpr.(*ast.Ident)
if !ok {
continue
}
for _, sliceDecl := range v.sliceDeclarations {
if sliceDecl.name == lhsIdent.Name {
// This is a potential mark, we just need to make sure there are no returns/continues in the
// range loop.
// now we just need to grab whatever we're ranging over
/*sxIdent, ok := s.X.(*ast.Ident)
if !ok {
continue
}*/
v.preallocHints = append(v.preallocHints, Hint{
Pos: sliceDecl.genD.Pos(),
DeclaredSliceName: sliceDecl.name,
})
}
}
}
}
}
case *ast.IfStmt:
ifStmt := bodyStmt
if ifStmt.Body != nil {
for _, ifBodyStmt := range ifStmt.Body.List {
// TODO should probably handle embedded ifs here
switch /*ift :=*/ ifBodyStmt.(type) {
case *ast.BranchStmt, *ast.ReturnStmt:
v.returnsInsideOfLoop = true
default:
}
}
}
default:
}
}
}
// Hint stores the information about an occurence of a slice that could be
// preallocated.
type Hint struct {
Pos token.Pos
DeclaredSliceName string
}
func (h Hint) String() string {
return fmt.Sprintf("%v: Consider preallocating %v", h.Pos, h.DeclaredSliceName)
}