/
etc.go
147 lines (129 loc) · 3.53 KB
/
etc.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
package loader
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"os"
"path/filepath"
"reflect"
"strings"
"golang.org/x/tools/go/packages"
)
func assert(b bool, msg string) {
if !b {
panic(msg)
}
}
func panicIf(err error) {
if err != nil {
panic(err)
}
}
func errLog(a ...any) {
_, _ = fmt.Fprintln(os.Stderr, a...)
}
func mustMkDir(dir string) string {
dir, err := filepath.Abs(dir)
panicIf(err)
err = os.MkdirAll(dir, os.ModePerm)
panicIf(err)
return dir
}
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ Clear Pos ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
func ClearPos(n ast.Node) {
posType := reflect.TypeOf(token.NoPos)
ast.Inspect(n, func(n ast.Node) bool {
if n != nil {
v := reflect.ValueOf(n).Elem()
fields := v.Type().NumField()
for i := 0; i < fields; i++ {
f := v.Field(i)
if f.Type() == posType {
if f.Interface() != token.NoPos {
f.Set(reflect.ValueOf(token.Pos(1)))
}
}
}
}
return true
})
}
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ Def & Uses ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
type (
Def = *ast.Ident
Use = *ast.Ident
)
func DefUses(l *Loader) map[Def][]Use {
obj2uses := map[types.Object][]*ast.Ident{}
def2Objs := map[*ast.Ident][]types.Object{} // test and non-test
l.VisitAllPackages(nil, func(pkg *packages.Package) {
info := pkg.TypesInfo
for id, obj := range info.Uses {
obj2uses[obj] = append(obj2uses[obj], id)
}
for id, obj := range info.Defs {
def2Objs[id] = append(def2Objs[id], obj)
}
})
useMap := map[Def][]Use{}
for def, objs := range def2Objs {
for _, obj := range objs {
for _, use := range obj2uses[obj] {
useMap[def] = append(useMap[def], use)
}
}
}
return useMap
}
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ Generator ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// copy from go1.21 ast.IsGenerated
// IsGenerated reports whether the file was generated by a program,
// not handwritten, by detecting the special comment described
// at https://go.dev/s/generatedcode.
//
// The syntax tree must have been parsed with the ParseComments flag.
// Example:
//
// f, err := parser.ParseFile(fset, filename, src, parser.ParseComments|parser.PackageClauseOnly)
// if err != nil { ... }
// gen := ast.IsGenerated(f)
func IsGenerated(file *ast.File) bool {
_, ok := Generator(file)
return ok
}
func Generator(file *ast.File) (by string, is bool) {
var (
cutPrefix = func(s, prefix string) (after string, found bool) {
if !strings.HasPrefix(s, prefix) {
return s, false
}
return s[len(prefix):], true
}
cutSuffix = func(s, suffix string) (before string, found bool) {
if !strings.HasSuffix(s, suffix) {
return s, false
}
return s[:len(s)-len(suffix)], true
}
)
for _, group := range file.Comments {
for _, comment := range group.List {
if comment.Pos() > file.Package {
break // after package declaration
}
// opt: check Contains first to avoid unnecessary array allocation in Split.
const prefix = "// Code generated "
if strings.Contains(comment.Text, prefix) {
for _, line := range strings.Split(comment.Text, "\n") {
if rest, ok := cutPrefix(line, prefix); ok {
if gen, ok := cutSuffix(rest, " DO NOT EDIT."); ok {
return gen, true
}
}
}
}
}
}
return "", false
}