-
Notifications
You must be signed in to change notification settings - Fork 39
/
unused.go
124 lines (112 loc) · 2.19 KB
/
unused.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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"sort"
)
func unused(filenames ...string) {
for _, src := range filenames {
fset := token.NewFileSet() // positions are relative to fset
// Parse src but stop after processing the imports.
f, err := parser.ParseFile(fset, src, nil, parser.ParseComments)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
return
}
// map
m := map[string][]string{}
// Print the imports from the file's AST.
for i := range f.Decls {
decls := f.Decls[i]
if fd, ok := decls.(*ast.FuncDecl); ok {
name := fd.Name.Name
var cs callSearcher
ast.Walk(&cs, fd)
m[name] = cs.used
}
}
// list of all function
used := map[string]bool{}
list := []string{"main"}
for iter := len(m) * 10; iter > 0; iter-- {
rem:
sort.Strings(list)
for i := range list {
if i == 0 {
continue
}
if list[i-1] == list[i] {
// remove from list
list = append(list[:i], list[i+1:]...)
goto rem
}
}
for i := range list {
_, ok := used[list[i]]
if ok {
// remove from list
list = append(list[:i], list[i+1:]...)
goto rem
}
}
var newlist []string
for k, v := range m {
for i := range list {
used[list[i]] = true
if k == list[i] {
used[k] = true
newlist = append(newlist, v...)
}
}
}
list = newlist
}
// full list
full := map[string]bool{}
for k, v := range m {
full[k] = true
for i := range v {
full[v[i]] = true
}
}
// unused
for k := range full {
_, ok := used[k]
if ok {
continue
}
fmt.Fprintf(os.Stdout, "%s\n", k)
}
}
}
type callSearcher struct {
used []string
}
var goFuncs = []string{
"len", "make", "append",
"string",
"float64", "float32",
"int", "int64", "int32", "int16",
"go", "close",
"panic",
}
// Visit for walking by node tree
func (c *callSearcher) Visit(node ast.Node) (w ast.Visitor) {
if call, ok := node.(*ast.CallExpr); ok {
if f, ok := call.Fun.(*ast.Ident); ok {
isFound := false
for i := range goFuncs {
if f.Name == goFuncs[i] {
isFound = true
}
}
if !isFound {
c.used = append(c.used, f.Name)
}
}
}
return c
}