-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
173 lines (147 loc) · 4.07 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"flag"
"fmt"
"go/ast"
"go/build"
"go/parser"
"go/token"
"log"
"os/exec"
"path/filepath"
"sort"
"strings"
)
var (
Words = make(map[string]struct{}, 8000)
// https://golang.org/ref/spec#Keywords
Keywords = []string{
"break", "default", "func", "interface", "select",
"case", "defer", "go", "map", "struct",
"chan", "else", "goto", "package", "switch",
"const", "fallthrough", "if", "range", "type",
"continue", "for", "import", "return", "var",
}
// https://golang.org/pkg/builtin/
Builtins = []string{
"true", "false", "iota", "nil",
"append", "cap", "clear", "close", "complex", "copy", "delete", "imag",
"len", "make", "max", "min", "new", "panic", "print", "println", "real", "recover",
"bool", "byte", "complex128", "complex64", "error", "float32", "float64",
"int", "int16", "int32", "int64", "int8",
"rune", "string",
"uint", "uint16", "uint32", "uint64", "uint8", "uintptr",
"any", "comparable",
}
Extra = []string{
"golang",
"gopkg", // gopkg.in
"omitempty", // popular tag value in std
"gopath", "goroot",
"goroutine", "goroutines",
// https://github.com/golang/go/blob/master/src/cmd/dist/build.go
"386", "amd64", "arm", "arm64", "loong64", "mips", "mipsle", "mips64", "mips64le", "ppc64", "ppc64le", "riscv64", "s390x", "sparc64", "wasm", // GOARCH
"darwin", "dragonfly", "illumos", "ios", "js", "wasip1", "linux", "android", "solaris", "freebsd", "nacl", "netbsd", "openbsd", "plan9", "windows", "aix", // GOOS
"gc", "gccgo", "gcc", "cgo",
"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9", "go1.10",
"go1.11", "go1.12", "go1.13", "go1.14", "go1.15", "go1.16", "go1.17", "go1.18", "go1.19", "go1.20",
"go1.21", "go1.22", "go1.23", "go1.24",
}
)
var DebugF = flag.Bool("debug", false, "Enable debug output")
func debugf(format string, v ...interface{}) {
if *DebugF {
log.Printf(format, v...)
}
}
// addWords adds words to Words map.
func addWords(words ...string) {
for _, w := range words {
Words[w] = struct{}{}
}
}
// processIdent extracts words from ident and adds them to Words.
func processIdent(ident *ast.Ident) {
if ident == nil || ident.Name == "." {
return
}
if strings.Contains(ident.Name, ".") {
log.Fatalf("unhandled ident %q", ident.Name)
}
if ast.IsExported(ident.Name) {
addWords(ident.Name)
}
}
// processAST extracts words from f.
func processAST(f *ast.File) {
for _, decl := range f.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
for _, spec := range decl.Specs {
switch spec := spec.(type) {
case *ast.ImportSpec:
processIdent(spec.Name)
case *ast.ValueSpec:
for _, n := range spec.Names {
processIdent(n)
}
case *ast.TypeSpec:
processIdent(spec.Name)
default:
log.Fatalf("unhandled spec %#v", spec)
}
}
case *ast.FuncDecl:
processIdent(decl.Name)
default:
log.Fatalf("unhandled decl %#v", decl)
}
}
}
func main() {
flag.Parse()
log.SetFlags(log.Lshortfile)
// add keywords, builtins and some extra words
addWords(Keywords...)
addWords(Builtins...)
addWords(Extra...)
// get std packages
cmd := exec.Command("go", "list", "std")
b, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
packages := strings.Split(strings.TrimSuffix(string(b), "\n"), "\n")
// process packages
for _, p := range packages {
if strings.HasPrefix(p, "vendor/") || strings.Contains(p, "/internal/") {
debugf("skipping package %q", p)
continue
}
pack, err := build.Import(p, "", 0)
if err != nil {
log.Fatal(err)
}
debugf("processing package %q", pack.ImportPath)
addWords(strings.Split(pack.ImportPath, "/")...)
addWords(pack.Name)
fset := token.NewFileSet()
for _, f := range pack.GoFiles {
ast, err := parser.ParseFile(fset, filepath.Join(pack.Dir, f), nil, 0)
if err != nil {
log.Fatal(err)
}
processAST(ast)
}
}
// sort and print result
log.Printf("found %d words", len(Words))
res := make([]string, 0, len(Words))
for w := range Words {
res = append(res, w)
}
sort.Strings(res)
for _, w := range res {
fmt.Println(w)
}
}