-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.go
234 lines (199 loc) · 5.49 KB
/
module.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
package astrav
import (
"go/ast"
"go/build"
"go/token"
"io"
"io/fs"
"os"
"path"
"strings"
"github.com/pkg/errors"
"golang.org/x/tools/go/callgraph"
"golang.org/x/tools/go/callgraph/cha"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/go/ssa"
"golang.org/x/tools/go/ssa/ssautil"
)
// NewModule creates an analyzer for an entire module or application. The dir argument is
// the folder of the module or application within root. If root only contains the module,
// dir can be left empty.
// Modules are loaded from disc. Support for fs.FS is currently not implemented
func NewModule(dir string) *Module {
return &Module{
dir: dir,
FSet: token.NewFileSet(),
Pkgs: map[string]*Package{},
}
}
// Module represents a Go module or an application.
type Module struct {
dir string
FSet *token.FileSet
Pkgs map[string]*Package
RawFiles map[string]*RawFile
Packages []*packages.Package
SSAPkgs []*ssa.Package
Graph *callgraph.Graph
}
// Dir returns the directory of the module.
func (s *Module) Dir() string {
return s.dir
}
// Load will load the module. DefaultFilterGoPackages is used to filter only Go packages that contain Go code.
// Packages only containing test files are excluded by default. Custom filter functions can be passed instead.
// The filter function should return false to filter a file or directory.
func (s *Module) Load(filterFns ...func(d fs.DirEntry) bool) error {
if len(filterFns) == 0 {
filterFns = append(filterFns, DefaultFilterGoPackages)
}
paths, fileSources, err := s.loadFiles(filterFns...)
if err != nil {
return err
}
if r := s.loadPackages(s.dir, paths); r != nil {
return r
}
s.fillRawFiles(fileSources)
if r := s.processPackages(); r != nil {
return r
}
s.buildCallGraph()
return nil
}
func (s *Module) buildCallGraph() {
program, pkgs := ssautil.AllPackages(s.Packages, 0)
s.SSAPkgs = pkgs
program.Build()
s.Graph = cha.CallGraph(program)
}
func (s *Module) processPackages() error {
for _, pack := range s.Packages {
if len(pack.CompiledGoFiles) != len(pack.Syntax) {
return errors.Errorf("file without Go code? %v", pack.CompiledGoFiles)
}
files := make(map[string]*ast.File, len(pack.Syntax))
for i, fileName := range pack.CompiledGoFiles {
file := pack.Syntax[i]
files[fileName] = file
}
pkgNode := creator(baseNode{
node: &ast.Package{
Name: pack.PkgPath,
Files: files,
},
}).(*Package)
pkgNode.rawFiles = map[string]*RawFile{}
for fileName, file := range s.RawFiles {
dir, _ := path.Split(fileName)
relativePath := strings.TrimSuffix(strings.TrimPrefix(dir, s.dir), "/")
if !strings.HasSuffix(pack.PkgPath, relativePath) {
continue
}
pkgNode.rawFiles[fileName] = file
}
pkgNode.info = pack.TypesInfo
pkgNode.typesPkg = pack.Types
pkgNode.pack = pack
s.Pkgs[pack.PkgPath] = pkgNode
}
return nil
}
func (s *Module) loadPackages(repoRoot string, paths []string) error {
s.FSet = token.NewFileSet()
packs, err := packages.Load(&packages.Config{
Dir: repoRoot,
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports |
packages.NeedTypes | packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps,
BuildFlags: build.Default.BuildTags,
Fset: s.FSet,
// Overlay: fileSources,
}, paths...)
if err != nil {
return errors.WithMessagef(err, "failed loading %s", s.dir)
}
s.Packages = packs
return nil
}
// DefaultFilterGoPackages returns the default filter function for Go packages.
func DefaultFilterGoPackages(d fs.DirEntry) bool {
if d.IsDir() {
return false
}
if !strings.HasSuffix(d.Name(), ".go") || strings.HasSuffix(d.Name(), "_test.go") {
return false
}
return true
}
// GetRawFiles a map of file contents
func (s *Module) GetRawFiles() map[string][]byte {
files := map[string][]byte{}
for name, file := range s.RawFiles {
files[name] = file.Source()
}
return files
}
func (s *Module) fillRawFiles(fileSources map[string][]byte) {
s.RawFiles = map[string]*RawFile{}
s.FSet.Iterate(func(file *token.File) bool {
fileSrc, ok := fileSources[file.Name()]
if !ok {
return true
}
s.RawFiles[file.Name()] = &RawFile{
File: file,
source: fileSrc,
}
return true
})
}
// Package returns a package by name
func (s *Module) Package(name string) *Package {
return s.Pkgs[name]
}
// loadFiles creates a slice of paths which contain Go packages in given baseDir.
func (s *Module) loadFiles(filterFns ...func(d fs.DirEntry) bool) ([]string, map[string][]byte, error) {
var (
fileSRCs = map[string][]byte{}
seen = map[string]struct{}{}
paths []string
)
root := os.DirFS(s.dir)
err := fs.WalkDir(root, ".", func(filePath string, d fs.DirEntry, err error) error {
if err != nil {
return fs.SkipDir
}
for _, isValid := range filterFns {
if !isValid(d) {
return nil
}
}
bts, err := readFile(root, filePath)
if err != nil {
return err
}
fileSRCs[path.Join(s.dir, filePath)] = bts
pkgPath := strings.TrimSuffix(filePath, d.Name())
if _, ok := seen[pkgPath]; ok {
return nil
}
seen[pkgPath] = struct{}{}
paths = append(paths, path.Join(s.dir, pkgPath))
return nil
})
if err != nil {
return nil, nil, errors.WithMessage(err, "failed to gather go packages")
}
return paths, fileSRCs, nil
}
func readFile(root fs.FS, filePath string) ([]byte, error) {
file, err := root.Open(filePath)
if err != nil {
return nil, err
}
bts, err := io.ReadAll(file)
if err != nil {
return nil, err
}
return bts, nil
}