-
Notifications
You must be signed in to change notification settings - Fork 1
/
godepth.go
245 lines (212 loc) · 5.11 KB
/
godepth.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
// godepth calculates maximum depth of go methods in Go source code.
//
// This work was mainly inspired by github.com/fzipp/gocyclo
//
// Usage:
// godepth [<flag> ...] <Go file or directory> ...
//
// Flags:
// -over N show functions with depth > N only and
// return exit code 1 if the output is non-empty
// -top N show the top N most complex functions only
// -avg show the average depth
//
// The output fields for each line are:
// <depth> <package> <function> <file:row:column>
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"os"
"path/filepath"
"sort"
)
const usageDoc = `Calculate maximum depth of Go functions.
Usage:
godepth [flags...] <Go file or directory> ...
Flags:
-over N show functions with depth > N only and
return exit code 1 if the set is non-empty
-top N show the top N most complex functions only
-avg show the average depth over all functions,
not depending on whether -over or -top are set
The output fields for each line are:
<depth> <package> <function> <file:row:column>
`
func usage() {
fmt.Fprintf(os.Stderr, usageDoc)
os.Exit(2)
}
var (
over = flag.Int("over", 0, "show functions with depth > N only")
top = flag.Int("top", -1, "show the top N deepest functions only")
avg = flag.Bool("avg", false, "show the average deepness")
)
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
if len(args) == 0 {
usage()
}
stats := analyze(args)
sort.Sort(byDepth(stats))
written := writeStats(os.Stdout, stats)
if *avg {
showAverage(stats)
}
if *over > 0 && written > 0 {
os.Exit(1)
}
}
func analyze(paths []string) []stat {
stats := []stat{}
for _, path := range paths {
if isDir(path) {
stats = analyzeDir(path, stats)
} else {
stats = analyzeFile(path, stats)
}
}
return stats
}
func isDir(filename string) bool {
fi, err := os.Stat(filename)
return err == nil && fi.IsDir()
}
func analyzeFile(fname string, stats []stat) []stat {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, fname, nil, 0)
if err != nil {
exitError(err)
}
return buildStats(f, fset, stats)
}
func analyzeDir(dirname string, stats []stat) []stat {
files, _ := filepath.Glob(filepath.Join(dirname, "*.go"))
for _, file := range files {
stats = analyzeFile(file, stats)
}
return stats
}
func exitError(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
func writeStats(w io.Writer, sortedStats []stat) int {
for i, stat := range sortedStats {
if i == *top {
return i
}
if stat.Depth <= *over {
return i
}
fmt.Fprintln(w, stat)
}
return len(sortedStats)
}
func showAverage(stats []stat) {
fmt.Printf("Average: %.3g\n", average(stats))
}
func average(stats []stat) float64 {
total := 0
for _, s := range stats {
total += s.Depth
}
return float64(total) / float64(len(stats))
}
type stat struct {
PkgName string
FuncName string
Depth int
Pos token.Position
}
func (s stat) String() string {
return fmt.Sprintf("%d %s %s %s", s.Depth, s.PkgName, s.FuncName, s.Pos)
}
type byDepth []stat
func (s byDepth) Len() int { return len(s) }
func (s byDepth) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byDepth) Less(i, j int) bool {
return s[i].Depth >= s[j].Depth
}
func buildStats(f *ast.File, fset *token.FileSet, stats []stat) []stat {
for _, decl := range f.Decls {
if fn, ok := decl.(*ast.FuncDecl); ok {
stats = append(stats, stat{
PkgName: f.Name.Name,
FuncName: funcName(fn),
Depth: depth(fn),
Pos: fset.Position(fn.Pos()),
})
}
}
return stats
}
// funcName returns the name representation of a function or method:
// "(Type).Name" for methods or simply "Name" for functions.
func funcName(fn *ast.FuncDecl) string {
if fn.Recv != nil {
typ := fn.Recv.List[0].Type
return fmt.Sprintf("(%s).%s", recvString(typ), fn.Name)
}
return fn.Name.Name
}
// recvString returns a string representation of recv of the
// form "T", "*T", or "BADRECV" (if not a proper receiver type).
func recvString(recv ast.Expr) string {
switch t := recv.(type) {
case *ast.Ident:
return t.Name
case *ast.StarExpr:
return "*" + recvString(t.X)
}
return "BADRECV"
}
func max(s []int) (m int) {
for _, value := range s {
if value > m {
m = value
}
}
return
}
// depth calculates the depth of a function
func depth(fn *ast.FuncDecl) int {
allDepth := []int{}
for _, lvl := range fn.Body.List {
v := maxDepthVisitor{}
ast.Walk(&v, lvl)
allDepth = append(allDepth, max(v.NodeDepth))
}
return max(allDepth)
}
type maxDepthVisitor struct {
Depth int
NodeDepth []int
Lbrace token.Pos
Rbrace token.Pos
}
// Visit implements the ast.Visitor interface.
func (v *maxDepthVisitor) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.BlockStmt:
if v.Rbrace == 0 && v.Lbrace == 0 {
v.Lbrace = n.Lbrace
v.Rbrace = n.Rbrace
}
if n.Lbrace > v.Rbrace {
v.Depth--
} else {
v.Depth++
}
v.Lbrace = n.Lbrace
v.Rbrace = n.Rbrace
v.NodeDepth = append(v.NodeDepth, v.Depth)
}
return v
}