-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
102 lines (84 loc) · 2.17 KB
/
run.go
File metadata and controls
102 lines (84 loc) · 2.17 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
package play
import (
"go/ast"
"go/parser"
"go/token"
"go/types"
"io/fs"
"path"
"golang.org/x/tools/go/analysis"
)
type Result struct {
Pass *analysis.Pass
Diagnostics []analysis.Diagnostic
Facts map[types.Object][]analysis.Fact
Result any
Err error
}
func Run(testdata fs.FS, a *analysis.Analyzer, pkgs ...string) ([]*Result, error) {
if len(pkgs) == 0 {
return nil, nil
}
var results []*Result
for _, pkg := range pkgs {
rs, err := run(testdata, a, pkg)
if err != nil {
return nil, err
}
results = append(results, rs...)
}
return results, nil
}
func run(testdata fs.FS, a *analysis.Analyzer, pkg string) (results []*Result, rerr error) {
defer derr(&rerr, "Run")
fsys, err := fs.Sub(testdata, path.Join("testdata", "src", pkg))
if err != nil {
return nil, err
}
config := &Config{
Fset: token.NewFileSet(),
Context: Context(fsys),
Fsys: fsys,
}
pkgs, err := ParseDir(config, ".", parser.ParseComments)
if err != nil {
return nil, err
}
for name, files := range pkgs {
typesConfig := &types.Config{
FakeImportC: true,
Importer: Importer(config),
}
info := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Instances: make(map[*ast.Ident]types.Instance),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Implicits: make(map[ast.Node]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
Scopes: make(map[ast.Node]*types.Scope),
}
typesPkg, err := typesConfig.Check(name, config.Fset, files, info)
if err != nil {
return nil, err
}
var result Result
result.Pass = &analysis.Pass{
Analyzer: a,
Fset: config.Fset,
Files: files,
Pkg: typesPkg,
TypesInfo: info,
TypesSizes: types.SizesFor(config.Context.Compiler, config.Context.GOARCH),
Report: func(d analysis.Diagnostic) {
result.Diagnostics = append(result.Diagnostics, d)
},
// FIXME: dependency
ResultOf: make(map[*analysis.Analyzer]any),
// FIXME: for fact
}
result.Result, result.Err = a.Run(result.Pass)
results = append(results, &result)
}
return results, nil
}