-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathpkg.go
More file actions
308 lines (282 loc) · 8.37 KB
/
pkg.go
File metadata and controls
308 lines (282 loc) · 8.37 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2025 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parser
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
. "github.com/cloudwego/abcoder/lang/uniast"
"golang.org/x/tools/go/packages"
)
func (p *GoParser) parseImports(fset *token.FileSet, file []byte, mod *Module, impts []*ast.ImportSpec) (*importInfo, error) {
thirdPartyImports := make(map[string][2]string)
projectImports := make(map[string]string)
sysImports := make(map[string]string)
ret := &importInfo{}
for _, imp := range impts {
importPath, _ := strconv.Unquote(imp.Path.Value) // remove the quotes
// skip CGO import path
if importPath == "C" {
continue
}
importAlias := ""
// Check if user has defined an alias for current import
if imp.Name != nil {
importAlias = imp.Name.Name // update the alias
ret.Origins = append(ret.Origins, Import{Path: imp.Path.Value, Alias: &importAlias})
} else {
importAlias = getPackageAlias(importPath)
ret.Origins = append(ret.Origins, Import{Path: imp.Path.Value})
}
// Fix: module name may also be like this?
if isSysPkg(importPath) {
// Ignoring golang standard libraries(like net/http)
sysImports[importAlias] = importPath
} else {
match, path := matchMod(importPath, mod.Dependencies)
if match == "" {
if !strings.HasPrefix(importPath, mod.Name) {
fmt.Fprintf(os.Stderr, "package %s not found mod", importPath)
}
projectImports[importAlias] = importPath
} else {
thirdPartyImports[importAlias] = [2]string{path, importPath}
}
}
}
ret.SysImports = sysImports
ret.ProjectImports = projectImports
ret.ThirdPartyImports = thirdPartyImports
return ret, nil
}
func matchMod(impt string, modules map[string]string) (name string, path string) {
matches := [][2]string{}
for name, path := range modules {
if strings.HasPrefix(impt, name) {
matches = append(matches, [2]string{name, path})
}
}
if len(matches) > 0 {
sort.Slice(matches, func(i, j int) bool {
return len(matches[i][0]) > len(matches[j][0])
})
name = matches[0][0]
path = matches[0][1]
}
return
}
func (p *GoParser) ParseNode(pkgPath string, name string) (Repository, error) {
out := NewRepository(p.repo.Name)
if pkgPath == "" {
//search mode
idss, err := p.searchName(name)
if err != nil {
return Repository{}, fmt.Errorf("Error search %v:%v", name, err)
}
for _, id := range idss {
if err := loadNode(p, id.PkgPath, id.Name, &out); err != nil {
return out, err
}
}
} else {
// parse entity
pkgPath, name := pkgPath, name
if err := loadNode(p, pkgPath, name, &out); err != nil {
return out, err
}
}
return out, nil
}
func (p *GoParser) associateImplements() {
for typ, tid := range p.types {
for iface, iid := range p.interfaces {
if types.Implements(typ, iface) {
tobj := p.getRepo().GetType(tid)
tobj.Implements = Append(tobj.Implements, iid)
}
// 另外检查 typ 的指针类型是否实现了 iface
if types.Implements(types.NewPointer(typ), iface) {
tobj := p.getRepo().GetType(tid)
tobj.Implements = Append(tobj.Implements, iid)
}
}
}
}
func (p *GoParser) ParsePackage(pkgPath PkgPath) (Repository, error) {
if err := p.parsePackage(pkgPath); err != nil {
return Repository{}, err
}
repo := p.getRepo()
k, _ := p.getModuleFromPkg(pkgPath)
var out = NewRepository(repo.Name)
out.Modules[k] = newModule(repo.Modules[k].Name, repo.Modules[k].Dir)
out.Modules[k].Packages[pkgPath] = repo.Modules[k].Packages[pkgPath]
return out, nil
}
func (p *GoParser) parsePackage(pkgPath PkgPath) (err error) {
mod, _ := p.getModuleFromPkg(pkgPath)
if mod == "" {
return fmt.Errorf("not found module for package %s", pkgPath)
}
// fast-path: check cache first
if p.visited[pkgPath] {
return nil
}
p.visited[pkgPath] = true
lib := p.repo.Modules[mod]
if lib == nil {
return fmt.Errorf("module not load: %s", mod)
}
// fmt.Println("[parsePackage] mod:", mod, "dir:", dir, "pkgPath:", pkgPath, p.opts.ReferCodeDepth)
return p.loadPackages(lib, filepath.Join(p.homePageDir, lib.Dir), pkgPath)
}
var loadCount = 0
func (p *GoParser) loadPackages(mod *Module, dir string, pkgPath PkgPath) (err error) {
if mm := p.repo.Modules[mod.Name]; mm != nil && (*mm).Packages[pkgPath] != nil {
return nil
}
fmt.Fprintf(os.Stderr, "[loadPackages] mod: %s, dir: %s, pkgPath: %s\n", mod.Name, dir, pkgPath)
fset := token.NewFileSet()
loadCount++
baseOpts := packages.NeedFiles | packages.NeedSyntax | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedImports
if p.opts.ReferCodeDepth != 0 {
baseOpts |= packages.NeedDeps
}
if p.opts.NeedTest {
baseOpts |= packages.NeedForTest
}
cfg := &packages.Config{
Mode: baseOpts,
Fset: fset,
Dir: dir,
Env: append(os.Environ(), "GOSUMDB=off"),
BuildFlags: p.opts.BuildFlags,
}
if p.opts.NeedTest {
cfg.Tests = true
}
hasCGO := false
if len(p.cgoPkgs) > 0 {
hasCGO = true
}
var pkgs []*packages.Package
if hasCGO {
baseOpts |= packages.NeedCompiledGoFiles
cfg.Mode = baseOpts
pkgs, err = packages.Load(cfg, pkgPath)
if err != nil {
return fmt.Errorf("load path '%s' with CGO failed: %v", dir, err)
}
} else {
pkgs, err = packages.Load(cfg, pkgPath)
if err != nil {
return fmt.Errorf("load path '%s' failed: %v", dir, err)
}
}
fmt.Fprintf(os.Stderr, "[loadPackages] mod: %s, dir: %s, pkgPath: %s, hasCGO: %v\n", mod.Name, dir, pkgPath, hasCGO)
for _, pkg := range pkgs {
if mm := p.repo.Modules[mod.Name]; mm != nil && (*mm).Packages[pkg.ID] != nil {
continue
}
if pp, ok := mod.Packages[pkg.ID]; ok && pp != nil {
continue
}
for idx, file := range pkg.Syntax {
var filePath string
if hasCGO {
// Cgo file path is tmp file path, like: /Users/bytedance/Library/Caches/go-build/61/6150fdadd44b9dca151737e261abf95697ba13b799e8dbdd464c0c27b443792a-d.
// We should get it through CompiledGoFiles
if idx >= len(pkg.CompiledGoFiles) {
fmt.Fprintf(os.Stderr, "skip file %s by loader\n", file.Name)
continue
}
filePath = pkg.CompiledGoFiles[idx]
} else {
filePath = fset.Position(file.Pos()).Filename
if filePath == "" {
fmt.Fprintf(os.Stderr, "filename is empty, pkg: %s\n", pkg.ID)
continue
}
}
var skip bool
for _, exclude := range p.exclues {
if exclude.MatchString(filePath) {
fmt.Fprintf(os.Stderr, "skip file %s\n", filePath)
skip = true
break
}
}
if skip {
continue
}
bs := p.getFileBytes(filePath)
ctx := &fileContext{
repoDir: p.homePageDir,
filePath: filePath,
module: mod,
pkgPath: pkg.ID,
bs: bs,
fset: fset,
pkgTypeInfo: pkg.TypesInfo,
deps: pkg.Imports,
collectComment: p.opts.CollectComment,
}
imports, err := p.parseImports(ctx.fset, ctx.bs, mod, file.Imports)
if err != nil {
return err
}
ctx.imports = imports
relpath, _ := filepath.Rel(p.homePageDir, filePath)
f := mod.Files[relpath]
if f == nil {
f = NewFile(relpath)
mod.Files[relpath] = f
}
if f.Package == "" {
f.Package = pkg.ID
f.Imports = imports.Origins
}
if err := p.parseFile(ctx, file); err != nil {
return err
}
}
if obj := mod.Packages[pkg.ID]; obj != nil {
// obj.Dependencies = make([]PkgPath, 0, len(pkg.Imports))
// for _, imp := range pkg.Imports {
// if isSysPkg(imp.ID) {
// continue
// }
// obj.Dependencies = append(obj.Dependencies, imp.ID)
// }
obj.PkgPath = pkg.ID
if strings.HasSuffix(obj.PkgPath, ".test]") {
obj.IsTest = true
}
if strings.HasSuffix(obj.PkgPath, ".test") {
delete(mod.Packages, obj.PkgPath)
}
}
mod.LoadErrors = append(mod.LoadErrors, pkg.Errors...)
}
return
}
func IsTestPackage(pkgPath string) bool {
return strings.HasSuffix(pkgPath, ".test") || strings.HasSuffix(pkgPath, ".test]")
}