-
Notifications
You must be signed in to change notification settings - Fork 195
/
main.go
150 lines (135 loc) · 2.78 KB
/
main.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
// parser parses the go programs in the given paths and prints
// the top five most common names of local variables and variables
// defined at package level.
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"sort"
)
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "usage:\n\t%s [files]\n", os.Args[0])
os.Exit(1)
}
fs := token.NewFileSet()
locals, globals := make(map[string]int), make(map[string]int)
for _, arg := range os.Args[1:] {
f, err := parser.ParseFile(fs, arg, nil, parser.AllErrors)
if err != nil {
log.Printf("could not parse %s: %v", arg, err)
continue
}
v := newVisitor(f)
ast.Walk(v, f)
for k, v := range v.locals {
locals[k] += v
}
for k, v := range v.globals {
globals[k] += v
}
}
fmt.Println("most common local variable names")
printTopFive(locals)
fmt.Println("most common global variable names")
printTopFive(globals)
}
func printTopFive(counts map[string]int) {
type pair struct {
s string
n int
}
pairs := make([]pair, 0, len(counts))
for s, n := range counts {
pairs = append(pairs, pair{s, n})
}
sort.Slice(pairs, func(i, j int) bool { return pairs[i].n > pairs[j].n })
for i := 0; i < len(pairs) && i < 5; i++ {
fmt.Printf("%6d %s\n", pairs[i].n, pairs[i].s)
}
}
type visitor struct {
pkgDecl map[*ast.GenDecl]bool
locals map[string]int
globals map[string]int
}
func newVisitor(f *ast.File) visitor {
decls := make(map[*ast.GenDecl]bool)
for _, decl := range f.Decls {
if v, ok := decl.(*ast.GenDecl); ok {
decls[v] = true
}
}
return visitor{
decls,
make(map[string]int),
make(map[string]int),
}
}
func (v visitor) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
switch d := n.(type) {
case *ast.AssignStmt:
if d.Tok != token.DEFINE {
return v
}
for _, name := range d.Lhs {
v.local(name)
}
case *ast.RangeStmt:
v.local(d.Key)
v.local(d.Value)
case *ast.FuncDecl:
if d.Recv != nil {
v.localList(d.Recv.List)
}
v.localList(d.Type.Params.List)
if d.Type.Results != nil {
v.localList(d.Type.Results.List)
}
case *ast.GenDecl:
if d.Tok != token.VAR {
return v
}
for _, spec := range d.Specs {
if value, ok := spec.(*ast.ValueSpec); ok {
for _, name := range value.Names {
if name.Name == "_" {
continue
}
if v.pkgDecl[d] {
v.globals[name.Name]++
} else {
v.locals[name.Name]++
}
}
}
}
}
return v
}
func (v visitor) local(n ast.Node) {
ident, ok := n.(*ast.Ident)
if !ok {
return
}
if ident.Name == "_" || ident.Name == "" {
return
}
if ident.Obj != nil && ident.Obj.Pos() == ident.Pos() {
v.locals[ident.Name]++
}
}
func (v visitor) localList(fs []*ast.Field) {
for _, f := range fs {
for _, name := range f.Names {
v.local(name)
}
}
}