featmap 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- featmap-0.1.1.dist-info/METADATA +95 -0
- featmap-0.1.1.dist-info/RECORD +130 -0
- featmap-0.1.1.dist-info/WHEEL +5 -0
- featmap-0.1.1.dist-info/entry_points.txt +2 -0
- featmap-0.1.1.dist-info/licenses/LICENSE +24 -0
- featmap-0.1.1.dist-info/licenses/fmap/vendor/typescript/LICENSE.txt +55 -0
- featmap-0.1.1.dist-info/licenses/fmap/vendor/typescript/ThirdPartyNoticeText.txt +193 -0
- featmap-0.1.1.dist-info/top_level.txt +1 -0
- fmap/__init__.py +1 -0
- fmap/__main__.py +3 -0
- fmap/axum.py +485 -0
- fmap/cache.py +25 -0
- fmap/cli.py +907 -0
- fmap/goext/go.mod +3 -0
- fmap/goext/main.go +547 -0
- fmap/goext.py +43 -0
- fmap/graph.py +920 -0
- fmap/impact.py +171 -0
- fmap/index.py +66 -0
- fmap/pyx.py +1133 -0
- fmap/routes.py +163 -0
- fmap/rsx.py +381 -0
- fmap/scip.py +80 -0
- fmap/trace_site.py +63 -0
- fmap/tsx.cjs +1258 -0
- fmap/tsx.py +98 -0
- fmap/vendor/typescript/LICENSE.txt +55 -0
- fmap/vendor/typescript/ThirdPartyNoticeText.txt +193 -0
- fmap/vendor/typescript/lib/lib.d.ts +22 -0
- fmap/vendor/typescript/lib/lib.decorators.d.ts +384 -0
- fmap/vendor/typescript/lib/lib.decorators.legacy.d.ts +22 -0
- fmap/vendor/typescript/lib/lib.dom.asynciterable.d.ts +41 -0
- fmap/vendor/typescript/lib/lib.dom.d.ts +39429 -0
- fmap/vendor/typescript/lib/lib.dom.iterable.d.ts +571 -0
- fmap/vendor/typescript/lib/lib.es2015.collection.d.ts +147 -0
- fmap/vendor/typescript/lib/lib.es2015.core.d.ts +597 -0
- fmap/vendor/typescript/lib/lib.es2015.d.ts +28 -0
- fmap/vendor/typescript/lib/lib.es2015.generator.d.ts +77 -0
- fmap/vendor/typescript/lib/lib.es2015.iterable.d.ts +605 -0
- fmap/vendor/typescript/lib/lib.es2015.promise.d.ts +81 -0
- fmap/vendor/typescript/lib/lib.es2015.proxy.d.ts +128 -0
- fmap/vendor/typescript/lib/lib.es2015.reflect.d.ts +144 -0
- fmap/vendor/typescript/lib/lib.es2015.symbol.d.ts +46 -0
- fmap/vendor/typescript/lib/lib.es2015.symbol.wellknown.d.ts +326 -0
- fmap/vendor/typescript/lib/lib.es2016.array.include.d.ts +116 -0
- fmap/vendor/typescript/lib/lib.es2016.d.ts +21 -0
- fmap/vendor/typescript/lib/lib.es2016.full.d.ts +23 -0
- fmap/vendor/typescript/lib/lib.es2016.intl.d.ts +31 -0
- fmap/vendor/typescript/lib/lib.es2017.arraybuffer.d.ts +21 -0
- fmap/vendor/typescript/lib/lib.es2017.d.ts +26 -0
- fmap/vendor/typescript/lib/lib.es2017.date.d.ts +31 -0
- fmap/vendor/typescript/lib/lib.es2017.full.d.ts +23 -0
- fmap/vendor/typescript/lib/lib.es2017.intl.d.ts +44 -0
- fmap/vendor/typescript/lib/lib.es2017.object.d.ts +49 -0
- fmap/vendor/typescript/lib/lib.es2017.sharedmemory.d.ts +135 -0
- fmap/vendor/typescript/lib/lib.es2017.string.d.ts +45 -0
- fmap/vendor/typescript/lib/lib.es2017.typedarrays.d.ts +53 -0
- fmap/vendor/typescript/lib/lib.es2018.asyncgenerator.d.ts +77 -0
- fmap/vendor/typescript/lib/lib.es2018.asynciterable.d.ts +53 -0
- fmap/vendor/typescript/lib/lib.es2018.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2018.full.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2018.intl.d.ts +83 -0
- fmap/vendor/typescript/lib/lib.es2018.promise.d.ts +30 -0
- fmap/vendor/typescript/lib/lib.es2018.regexp.d.ts +37 -0
- fmap/vendor/typescript/lib/lib.es2019.array.d.ts +79 -0
- fmap/vendor/typescript/lib/lib.es2019.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2019.full.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2019.intl.d.ts +23 -0
- fmap/vendor/typescript/lib/lib.es2019.object.d.ts +33 -0
- fmap/vendor/typescript/lib/lib.es2019.string.d.ts +37 -0
- fmap/vendor/typescript/lib/lib.es2019.symbol.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2020.bigint.d.ts +765 -0
- fmap/vendor/typescript/lib/lib.es2020.d.ts +27 -0
- fmap/vendor/typescript/lib/lib.es2020.date.d.ts +42 -0
- fmap/vendor/typescript/lib/lib.es2020.full.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2020.intl.d.ts +474 -0
- fmap/vendor/typescript/lib/lib.es2020.number.d.ts +28 -0
- fmap/vendor/typescript/lib/lib.es2020.promise.d.ts +47 -0
- fmap/vendor/typescript/lib/lib.es2020.sharedmemory.d.ts +99 -0
- fmap/vendor/typescript/lib/lib.es2020.string.d.ts +44 -0
- fmap/vendor/typescript/lib/lib.es2020.symbol.wellknown.d.ts +41 -0
- fmap/vendor/typescript/lib/lib.es2021.d.ts +23 -0
- fmap/vendor/typescript/lib/lib.es2021.full.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2021.intl.d.ts +166 -0
- fmap/vendor/typescript/lib/lib.es2021.promise.d.ts +48 -0
- fmap/vendor/typescript/lib/lib.es2021.string.d.ts +33 -0
- fmap/vendor/typescript/lib/lib.es2021.weakref.d.ts +78 -0
- fmap/vendor/typescript/lib/lib.es2022.array.d.ts +121 -0
- fmap/vendor/typescript/lib/lib.es2022.d.ts +25 -0
- fmap/vendor/typescript/lib/lib.es2022.error.d.ts +75 -0
- fmap/vendor/typescript/lib/lib.es2022.full.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2022.intl.d.ts +145 -0
- fmap/vendor/typescript/lib/lib.es2022.object.d.ts +26 -0
- fmap/vendor/typescript/lib/lib.es2022.regexp.d.ts +39 -0
- fmap/vendor/typescript/lib/lib.es2022.string.d.ts +25 -0
- fmap/vendor/typescript/lib/lib.es2023.array.d.ts +924 -0
- fmap/vendor/typescript/lib/lib.es2023.collection.d.ts +21 -0
- fmap/vendor/typescript/lib/lib.es2023.d.ts +22 -0
- fmap/vendor/typescript/lib/lib.es2023.full.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2023.intl.d.ts +56 -0
- fmap/vendor/typescript/lib/lib.es2024.arraybuffer.d.ts +65 -0
- fmap/vendor/typescript/lib/lib.es2024.collection.d.ts +29 -0
- fmap/vendor/typescript/lib/lib.es2024.d.ts +26 -0
- fmap/vendor/typescript/lib/lib.es2024.full.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.es2024.object.d.ts +29 -0
- fmap/vendor/typescript/lib/lib.es2024.promise.d.ts +35 -0
- fmap/vendor/typescript/lib/lib.es2024.regexp.d.ts +25 -0
- fmap/vendor/typescript/lib/lib.es2024.sharedmemory.d.ts +68 -0
- fmap/vendor/typescript/lib/lib.es2024.string.d.ts +29 -0
- fmap/vendor/typescript/lib/lib.es5.d.ts +4601 -0
- fmap/vendor/typescript/lib/lib.es6.d.ts +23 -0
- fmap/vendor/typescript/lib/lib.esnext.array.d.ts +35 -0
- fmap/vendor/typescript/lib/lib.esnext.collection.d.ts +96 -0
- fmap/vendor/typescript/lib/lib.esnext.d.ts +29 -0
- fmap/vendor/typescript/lib/lib.esnext.decorators.d.ts +28 -0
- fmap/vendor/typescript/lib/lib.esnext.disposable.d.ts +193 -0
- fmap/vendor/typescript/lib/lib.esnext.error.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.esnext.float16.d.ts +445 -0
- fmap/vendor/typescript/lib/lib.esnext.full.d.ts +24 -0
- fmap/vendor/typescript/lib/lib.esnext.intl.d.ts +21 -0
- fmap/vendor/typescript/lib/lib.esnext.iterator.d.ts +148 -0
- fmap/vendor/typescript/lib/lib.esnext.promise.d.ts +34 -0
- fmap/vendor/typescript/lib/lib.esnext.sharedmemory.d.ts +25 -0
- fmap/vendor/typescript/lib/lib.scripthost.d.ts +322 -0
- fmap/vendor/typescript/lib/lib.webworker.asynciterable.d.ts +41 -0
- fmap/vendor/typescript/lib/lib.webworker.d.ts +13150 -0
- fmap/vendor/typescript/lib/lib.webworker.importscripts.d.ts +23 -0
- fmap/vendor/typescript/lib/lib.webworker.iterable.d.ts +340 -0
- fmap/vendor/typescript/lib/typescript.js +200276 -0
- fmap/vendor/typescript/package.json +120 -0
fmap/goext/go.mod
ADDED
fmap/goext/main.go
ADDED
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
// fmap Go extractor (spec §12 P5): go list -export -deps for the package set, go/types for
|
|
2
|
+
// project packages, export data for everything else. Emits the same JSONL records as tsx.cjs.
|
|
3
|
+
package main
|
|
4
|
+
|
|
5
|
+
import (
|
|
6
|
+
"bytes"
|
|
7
|
+
"encoding/json"
|
|
8
|
+
"fmt"
|
|
9
|
+
"go/ast"
|
|
10
|
+
"go/importer"
|
|
11
|
+
"go/parser"
|
|
12
|
+
"go/token"
|
|
13
|
+
"go/types"
|
|
14
|
+
"io"
|
|
15
|
+
"os"
|
|
16
|
+
"os/exec"
|
|
17
|
+
"path/filepath"
|
|
18
|
+
"regexp"
|
|
19
|
+
"runtime"
|
|
20
|
+
"sort"
|
|
21
|
+
"strings"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
type input struct {
|
|
25
|
+
Excluded []string `json:"excluded"`
|
|
26
|
+
Root string `json:"root"`
|
|
27
|
+
Tracked []string `json:"tracked"`
|
|
28
|
+
Config map[string][]rule `json:"config"`
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type rule struct {
|
|
32
|
+
At string `json:"at"`
|
|
33
|
+
Expr string `json:"expr"`
|
|
34
|
+
Targets []string `json:"targets"`
|
|
35
|
+
Target string `json:"target"`
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type pkgInfo struct {
|
|
39
|
+
ImportPath string
|
|
40
|
+
Dir string
|
|
41
|
+
GoFiles []string
|
|
42
|
+
CgoFiles []string
|
|
43
|
+
Export string
|
|
44
|
+
Standard bool
|
|
45
|
+
ImportMap map[string]string
|
|
46
|
+
Module *struct{ Main bool }
|
|
47
|
+
Error *struct{ Err string }
|
|
48
|
+
DepsErrors []*struct{ Err string }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const UNLOADED_PATH_LIMIT = 3
|
|
52
|
+
|
|
53
|
+
var out = json.NewEncoder(os.Stdout)
|
|
54
|
+
|
|
55
|
+
func emit(m map[string]any) { out.Encode(m) }
|
|
56
|
+
|
|
57
|
+
func main() {
|
|
58
|
+
var in input
|
|
59
|
+
data, _ := io.ReadAll(os.Stdin)
|
|
60
|
+
if err := json.Unmarshal(data, &in); err != nil {
|
|
61
|
+
fmt.Fprintln(os.Stderr, err)
|
|
62
|
+
os.Exit(2)
|
|
63
|
+
}
|
|
64
|
+
root, _ := filepath.Abs(in.Root)
|
|
65
|
+
excluded := map[string]bool{}
|
|
66
|
+
for _, f := range in.Excluded {
|
|
67
|
+
excluded[f] = true
|
|
68
|
+
}
|
|
69
|
+
tracked := map[string]bool{}
|
|
70
|
+
for _, f := range in.Tracked {
|
|
71
|
+
tracked[f] = true
|
|
72
|
+
}
|
|
73
|
+
cmd := exec.Command("go", "list", "-e", "-export", "-json", "-deps", "./...")
|
|
74
|
+
cmd.Dir = root
|
|
75
|
+
var stderr bytes.Buffer
|
|
76
|
+
cmd.Stderr = &stderr
|
|
77
|
+
raw, _ := cmd.Output()
|
|
78
|
+
dec := json.NewDecoder(bytes.NewReader(raw))
|
|
79
|
+
var pkgs []*pkgInfo
|
|
80
|
+
byPath := map[string]*pkgInfo{}
|
|
81
|
+
for dec.More() {
|
|
82
|
+
p := &pkgInfo{}
|
|
83
|
+
if err := dec.Decode(p); err != nil {
|
|
84
|
+
break
|
|
85
|
+
}
|
|
86
|
+
pkgs = append(pkgs, p)
|
|
87
|
+
byPath[p.ImportPath] = p
|
|
88
|
+
}
|
|
89
|
+
rel := func(f string) string { r, _ := filepath.Rel(root, f); return filepath.ToSlash(r) }
|
|
90
|
+
var missing []string
|
|
91
|
+
failed := "" // a failing `go list` (no packages, or the pattern itself errs) is not a missing import (spec §6.4)
|
|
92
|
+
for _, p := range pkgs {
|
|
93
|
+
if p.ImportPath == "./..." && p.Error != nil {
|
|
94
|
+
failed = "go list failed in .: " + strings.Split(p.Error.Err, "\n")[0]
|
|
95
|
+
} else if p.Error != nil && !(p.Module != nil && p.Module.Main) {
|
|
96
|
+
missing = append(missing, p.ImportPath+": "+strings.Split(p.Error.Err, "\n")[0])
|
|
97
|
+
}
|
|
98
|
+
for _, e := range p.DepsErrors {
|
|
99
|
+
missing = append(missing, strings.Split(e.Err, "\n")[0])
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if len(pkgs) == 0 {
|
|
103
|
+
// Warning-only stderr is harmless; tracked files still need a listed package below.
|
|
104
|
+
for _, l := range strings.Split(strings.TrimSpace(stderr.String()), "\n") {
|
|
105
|
+
if l != "" && !strings.HasPrefix(l, "go: warning:") {
|
|
106
|
+
failed = "go list failed in .: " + l
|
|
107
|
+
break
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
covered := map[string]bool{}
|
|
112
|
+
for _, p := range pkgs {
|
|
113
|
+
if p.Module != nil && p.Module.Main {
|
|
114
|
+
covered[rel(p.Dir)] = true
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if failed == "" {
|
|
118
|
+
unloaded, err := unloadedGoFiles(root, tracked, excluded, covered)
|
|
119
|
+
if err != nil {
|
|
120
|
+
failed = err.Error()
|
|
121
|
+
} else if len(unloaded) > 0 {
|
|
122
|
+
failed = "go program does not load tracked files: " + strings.Join(first(unloaded, UNLOADED_PATH_LIMIT), ", ")
|
|
123
|
+
if len(unloaded) > UNLOADED_PATH_LIMIT {
|
|
124
|
+
failed += fmt.Sprintf(", ... (%d more)", len(unloaded)-UNLOADED_PATH_LIMIT)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
sort.Strings(missing)
|
|
130
|
+
missing = uniq(missing)
|
|
131
|
+
|
|
132
|
+
fset := token.NewFileSet()
|
|
133
|
+
gc := importer.ForCompiler(fset, "gc", func(path string) (io.ReadCloser, error) {
|
|
134
|
+
if p := byPath[path]; p != nil && p.Export != "" {
|
|
135
|
+
return os.Open(p.Export)
|
|
136
|
+
}
|
|
137
|
+
return nil, fmt.Errorf("no export data for %s", path)
|
|
138
|
+
})
|
|
139
|
+
checked := map[string]*types.Package{}
|
|
140
|
+
type unit struct {
|
|
141
|
+
pkg *types.Package
|
|
142
|
+
info *types.Info
|
|
143
|
+
files []*ast.File
|
|
144
|
+
}
|
|
145
|
+
var units []unit
|
|
146
|
+
var outside []string
|
|
147
|
+
loaded := []string{}
|
|
148
|
+
for _, p := range pkgs {
|
|
149
|
+
if p.Module == nil || !p.Module.Main || p.Error != nil && len(p.GoFiles) == 0 {
|
|
150
|
+
continue
|
|
151
|
+
}
|
|
152
|
+
var files []*ast.File
|
|
153
|
+
for _, name := range append(append([]string{}, p.GoFiles...), p.CgoFiles...) {
|
|
154
|
+
full := filepath.Join(p.Dir, name)
|
|
155
|
+
r := rel(full)
|
|
156
|
+
loaded = append(loaded, r)
|
|
157
|
+
if !tracked[r] && !excluded[r] {
|
|
158
|
+
outside = append(outside, r)
|
|
159
|
+
}
|
|
160
|
+
f, err := parser.ParseFile(fset, full, nil, parser.ParseComments|parser.SkipObjectResolution)
|
|
161
|
+
if err == nil {
|
|
162
|
+
files = append(files, f)
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
info := &types.Info{Uses: map[*ast.Ident]types.Object{}, Defs: map[*ast.Ident]types.Object{},
|
|
166
|
+
Selections: map[*ast.SelectorExpr]*types.Selection{}, Types: map[ast.Expr]types.TypeAndValue{}}
|
|
167
|
+
imp := importerFunc(func(path, dir string, mode types.ImportMode) (*types.Package, error) {
|
|
168
|
+
if m, ok := p.ImportMap[path]; ok {
|
|
169
|
+
path = m
|
|
170
|
+
}
|
|
171
|
+
if t := checked[path]; t != nil {
|
|
172
|
+
return t, nil
|
|
173
|
+
}
|
|
174
|
+
return gc.(types.ImporterFrom).ImportFrom(path, dir, mode)
|
|
175
|
+
})
|
|
176
|
+
conf := types.Config{Importer: imp, Error: func(error) {}, Sizes: types.SizesFor("gc", runtime.GOARCH)}
|
|
177
|
+
tp, _ := conf.Check(p.ImportPath, fset, files, info)
|
|
178
|
+
checked[p.ImportPath] = tp
|
|
179
|
+
units = append(units, unit{tp, info, files})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ---- nodes ----
|
|
183
|
+
project := func(o types.Object) bool {
|
|
184
|
+
return o != nil && o.Pkg() != nil && checked[o.Pkg().Path()] != nil && !excluded[rel(fset.Position(o.Pos()).Filename)]
|
|
185
|
+
}
|
|
186
|
+
idOf := map[types.Object]string{}
|
|
187
|
+
posLine := func(p token.Pos) int { return fset.Position(p).Line }
|
|
188
|
+
for _, u := range units {
|
|
189
|
+
for _, f := range u.files {
|
|
190
|
+
file := rel(fset.Position(f.Pos()).Filename)
|
|
191
|
+
if excluded[file] {
|
|
192
|
+
continue
|
|
193
|
+
}
|
|
194
|
+
emit(map[string]any{"t": "node", "id": file + ":<module>", "kind": "module", "line": 1, "end_line": posLine(f.End())})
|
|
195
|
+
for _, d := range f.Decls {
|
|
196
|
+
switch d := d.(type) {
|
|
197
|
+
case *ast.FuncDecl:
|
|
198
|
+
name := d.Name.Name
|
|
199
|
+
kind := "function"
|
|
200
|
+
if d.Recv != nil && len(d.Recv.List) == 1 {
|
|
201
|
+
name = recvName(d.Recv.List[0].Type) + "." + name
|
|
202
|
+
kind = "method"
|
|
203
|
+
}
|
|
204
|
+
id := file + ":" + name
|
|
205
|
+
if o := u.info.Defs[d.Name]; o != nil && !(d.Recv == nil && (d.Name.Name == "init" || d.Name.Name == "_")) {
|
|
206
|
+
idOf[o] = id
|
|
207
|
+
} else if d.Name.Name == "init" {
|
|
208
|
+
id = fmt.Sprintf("%s:init#%d", file, posLine(d.Pos()))
|
|
209
|
+
}
|
|
210
|
+
emit(map[string]any{"t": "node", "id": id, "kind": kind, "line": posLine(d.Pos()), "end_line": posLine(d.End())})
|
|
211
|
+
case *ast.GenDecl:
|
|
212
|
+
for _, s := range d.Specs {
|
|
213
|
+
ts, ok := s.(*ast.TypeSpec)
|
|
214
|
+
if !ok {
|
|
215
|
+
continue
|
|
216
|
+
}
|
|
217
|
+
o := u.info.Defs[ts.Name]
|
|
218
|
+
id := file + ":" + ts.Name.Name
|
|
219
|
+
idOf[o] = id
|
|
220
|
+
emit(map[string]any{"t": "node", "id": id, "kind": "class", "line": posLine(ts.Pos()), "end_line": posLine(ts.End())})
|
|
221
|
+
if it, ok := ts.Type.(*ast.InterfaceType); ok {
|
|
222
|
+
for _, m := range it.Methods.List {
|
|
223
|
+
for _, n := range m.Names {
|
|
224
|
+
mid := id + "." + n.Name
|
|
225
|
+
idOf[u.info.Defs[n]] = mid
|
|
226
|
+
emit(map[string]any{"t": "node", "id": mid, "kind": "method", "line": posLine(m.Pos()), "end_line": posLine(m.End())})
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ---- impl: interface method -> concrete method (types.Implements is exact in Go) ----
|
|
237
|
+
var ifaces, concretes []*types.TypeName
|
|
238
|
+
for _, u := range units {
|
|
239
|
+
if u.pkg == nil {
|
|
240
|
+
continue
|
|
241
|
+
}
|
|
242
|
+
sc := u.pkg.Scope()
|
|
243
|
+
for _, n := range sc.Names() {
|
|
244
|
+
tn, ok := sc.Lookup(n).(*types.TypeName)
|
|
245
|
+
if !ok || tn.IsAlias() {
|
|
246
|
+
continue
|
|
247
|
+
}
|
|
248
|
+
if types.IsInterface(tn.Type()) {
|
|
249
|
+
ifaces = append(ifaces, tn)
|
|
250
|
+
} else {
|
|
251
|
+
concretes = append(concretes, tn)
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
for _, it := range ifaces {
|
|
256
|
+
iface := it.Type().Underlying().(*types.Interface)
|
|
257
|
+
if iface.NumMethods() == 0 {
|
|
258
|
+
continue
|
|
259
|
+
}
|
|
260
|
+
for _, c := range concretes {
|
|
261
|
+
var t types.Type = c.Type()
|
|
262
|
+
if !types.Implements(t, iface) {
|
|
263
|
+
t = types.NewPointer(t)
|
|
264
|
+
if !types.Implements(t, iface) {
|
|
265
|
+
continue
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
for i := 0; i < iface.NumExplicitMethods(); i++ {
|
|
269
|
+
im := iface.ExplicitMethod(i)
|
|
270
|
+
obj, _, _ := types.LookupFieldOrMethod(t, true, c.Pkg(), im.Name())
|
|
271
|
+
from, to := idOf[im], idOf[obj]
|
|
272
|
+
if from != "" && to != "" {
|
|
273
|
+
emit(map[string]any{"t": "edge", "from": from, "to": to, "kind": "impl", "at": rel(fset.Position(obj.Pos()).Filename) + fmt.Sprint(":", posLine(obj.Pos()))})
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// ---- edges ----
|
|
280
|
+
for _, u := range units {
|
|
281
|
+
for _, f := range u.files {
|
|
282
|
+
file := rel(fset.Position(f.Pos()).Filename)
|
|
283
|
+
if excluded[file] {
|
|
284
|
+
continue
|
|
285
|
+
}
|
|
286
|
+
at := func(n ast.Node) string { return fmt.Sprintf("%s:%d", file, posLine(n.Pos())) }
|
|
287
|
+
var dispatch, procs []rule
|
|
288
|
+
for _, r := range in.Config["dispatch"] {
|
|
289
|
+
if pathMatch(r.At, file) {
|
|
290
|
+
dispatch = append(dispatch, r)
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
for _, r := range in.Config["process"] {
|
|
294
|
+
if r.At != "" && pathMatch(r.At, file) {
|
|
295
|
+
procs = append(procs, r)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
src, _ := os.ReadFile(fset.Position(f.Pos()).Filename)
|
|
299
|
+
text := func(n ast.Node) string {
|
|
300
|
+
return strings.Join(strings.Fields(string(src[fset.Position(n.Pos()).Offset:fset.Position(n.End()).Offset])), " ")
|
|
301
|
+
}
|
|
302
|
+
for _, d := range f.Decls {
|
|
303
|
+
me := file + ":<module>"
|
|
304
|
+
if fd, ok := d.(*ast.FuncDecl); ok {
|
|
305
|
+
if o := u.info.Defs[fd.Name]; o != nil && idOf[o] != "" {
|
|
306
|
+
me = idOf[o]
|
|
307
|
+
} else if fd.Name.Name == "init" {
|
|
308
|
+
me = fmt.Sprintf("%s:init#%d", file, posLine(fd.Pos()))
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
callees := map[ast.Expr]bool{}
|
|
312
|
+
ast.Inspect(d, func(n ast.Node) bool {
|
|
313
|
+
call, ok := n.(*ast.CallExpr)
|
|
314
|
+
if !ok {
|
|
315
|
+
if id, ok := n.(*ast.Ident); ok && !callees[id] {
|
|
316
|
+
if o, ok := u.info.Uses[id].(*types.Func); ok && project(o) && idOf[o] != "" && idOf[o] != me {
|
|
317
|
+
emit(map[string]any{"t": "edge", "from": me, "to": idOf[o], "kind": "ref", "at": at(id)})
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return true
|
|
321
|
+
}
|
|
322
|
+
fun := ast.Unparen(call.Fun)
|
|
323
|
+
callees[fun] = true
|
|
324
|
+
if sel, ok := fun.(*ast.SelectorExpr); ok {
|
|
325
|
+
callees[sel.Sel] = true
|
|
326
|
+
}
|
|
327
|
+
calleeText, full := text(fun), text(call)
|
|
328
|
+
for _, r := range dispatch {
|
|
329
|
+
if exprMatch(r.Expr, calleeText, full) {
|
|
330
|
+
for _, t := range r.Targets {
|
|
331
|
+
emit(map[string]any{"t": "edge", "from": me, "to": t, "kind": "dispatch", "at": at(call), "src": "rule"})
|
|
332
|
+
}
|
|
333
|
+
return true
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
if tv, ok := u.info.Types[fun]; ok && (tv.IsType() || tv.IsBuiltin()) {
|
|
337
|
+
return true // conversion or builtin
|
|
338
|
+
}
|
|
339
|
+
var obj types.Object
|
|
340
|
+
switch fn := fun.(type) {
|
|
341
|
+
case *ast.Ident:
|
|
342
|
+
obj = u.info.Uses[fn]
|
|
343
|
+
case *ast.SelectorExpr:
|
|
344
|
+
if s := u.info.Selections[fn]; s != nil {
|
|
345
|
+
obj = s.Obj()
|
|
346
|
+
} else {
|
|
347
|
+
obj = u.info.Uses[fn.Sel]
|
|
348
|
+
}
|
|
349
|
+
case *ast.IndexExpr, *ast.IndexListExpr:
|
|
350
|
+
if inner := indexBase(fn); inner != nil {
|
|
351
|
+
if o, ok := u.info.Uses[inner].(*types.Func); ok {
|
|
352
|
+
obj = o // generic instantiation f[T](...)
|
|
353
|
+
break
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
emit(map[string]any{"t": "edge", "from": me, "to": "?", "kind": "unresolved", "at": at(call), "expr": calleeText, "full": full})
|
|
357
|
+
return true
|
|
358
|
+
case *ast.FuncLit:
|
|
359
|
+
return true // shortcut: closure bodies are attributed to the enclosing func
|
|
360
|
+
}
|
|
361
|
+
switch o := obj.(type) {
|
|
362
|
+
case *types.Func:
|
|
363
|
+
if o.Pkg() != nil && o.Pkg().Path() == "os/exec" && (o.Name() == "Command" || o.Name() == "CommandContext") {
|
|
364
|
+
for _, r := range procs {
|
|
365
|
+
if exprMatch(r.Expr, calleeText, full) {
|
|
366
|
+
emit(map[string]any{"t": "edge", "from": me, "to": r.Target + ":<module>", "kind": "process", "at": at(call), "src": "rule", "target_file": r.Target})
|
|
367
|
+
return true
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
emit(map[string]any{"t": "edge", "from": me, "to": "?", "kind": "unresolved", "at": at(call), "expr": calleeText, "full": full})
|
|
371
|
+
return true
|
|
372
|
+
}
|
|
373
|
+
if !project(o) {
|
|
374
|
+
return true // provably external
|
|
375
|
+
}
|
|
376
|
+
o = o.Origin()
|
|
377
|
+
if id := idOf[o]; id != "" {
|
|
378
|
+
kind := "call"
|
|
379
|
+
if sig, ok := o.Type().(*types.Signature); ok && sig.Recv() != nil && types.IsInterface(sig.Recv().Type()) {
|
|
380
|
+
kind = "sigcall"
|
|
381
|
+
}
|
|
382
|
+
emit(map[string]any{"t": "edge", "from": me, "to": id, "kind": kind, "at": at(call), "expr": calleeText})
|
|
383
|
+
} else {
|
|
384
|
+
emit(map[string]any{"t": "edge", "from": me, "to": "?", "kind": "?callback", "at": at(call), "expr": calleeText})
|
|
385
|
+
}
|
|
386
|
+
case *types.Var:
|
|
387
|
+
if o.Pkg() != nil && !project(o) {
|
|
388
|
+
return true
|
|
389
|
+
}
|
|
390
|
+
emit(map[string]any{"t": "edge", "from": me, "to": "?", "kind": "?callback", "at": at(call), "expr": calleeText})
|
|
391
|
+
case nil:
|
|
392
|
+
emit(map[string]any{"t": "edge", "from": me, "to": "?", "kind": "?callback", "at": at(call), "expr": calleeText})
|
|
393
|
+
}
|
|
394
|
+
return true
|
|
395
|
+
})
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
deps := "ok"
|
|
400
|
+
if len(missing) > 0 {
|
|
401
|
+
deps = "missing"
|
|
402
|
+
}
|
|
403
|
+
if failed != "" {
|
|
404
|
+
deps = "failed"
|
|
405
|
+
}
|
|
406
|
+
sort.Strings(outside)
|
|
407
|
+
sort.Strings(loaded)
|
|
408
|
+
emit(map[string]any{"t": "program", "name": "go", "deps": deps, "missing": first(missing, 3), "missing_count": len(missing),
|
|
409
|
+
"outside": nonNil(outside), "loaded": loaded, "tool": "go " + strings.TrimPrefix(runtime.Version(), "go"), "failed": failed})
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
type importerFunc func(path, dir string, mode types.ImportMode) (*types.Package, error)
|
|
413
|
+
|
|
414
|
+
func (f importerFunc) Import(path string) (*types.Package, error) { return f(path, "", 0) }
|
|
415
|
+
func (f importerFunc) ImportFrom(path, dir string, mode types.ImportMode) (*types.Package, error) {
|
|
416
|
+
return f(path, dir, mode)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
func recvName(e ast.Expr) string {
|
|
420
|
+
switch t := e.(type) {
|
|
421
|
+
case *ast.StarExpr:
|
|
422
|
+
return recvName(t.X)
|
|
423
|
+
case *ast.IndexExpr:
|
|
424
|
+
return recvName(t.X)
|
|
425
|
+
case *ast.IndexListExpr:
|
|
426
|
+
return recvName(t.X)
|
|
427
|
+
case *ast.Ident:
|
|
428
|
+
return t.Name
|
|
429
|
+
}
|
|
430
|
+
return "?"
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
func indexBase(e ast.Expr) *ast.Ident {
|
|
434
|
+
var x ast.Expr
|
|
435
|
+
switch t := e.(type) {
|
|
436
|
+
case *ast.IndexExpr:
|
|
437
|
+
x = t.X
|
|
438
|
+
case *ast.IndexListExpr:
|
|
439
|
+
x = t.X
|
|
440
|
+
}
|
|
441
|
+
switch t := x.(type) {
|
|
442
|
+
case *ast.Ident:
|
|
443
|
+
return t
|
|
444
|
+
case *ast.SelectorExpr:
|
|
445
|
+
return t.Sel
|
|
446
|
+
}
|
|
447
|
+
return nil
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// fnmatch-like (`*` crosses '/'), like pyx.path_match
|
|
451
|
+
func pathMatch(pat, file string) bool {
|
|
452
|
+
if pat == "" || pat == file {
|
|
453
|
+
return true
|
|
454
|
+
}
|
|
455
|
+
return wild(pat).MatchString(file)
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
func wild(pat string) *regexp.Regexp {
|
|
459
|
+
q := regexp.QuoteMeta(pat)
|
|
460
|
+
q = strings.ReplaceAll(q, `\*`, ".*")
|
|
461
|
+
q = strings.ReplaceAll(q, `\?`, ".")
|
|
462
|
+
return regexp.MustCompile(`(?s)^` + q + `$`)
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// same semantics as pyx.expr_match: `*` / `(*)` are wildcards, matched against callee or full call text
|
|
466
|
+
func exprMatch(pat, callee, full string) bool {
|
|
467
|
+
if pat == "" {
|
|
468
|
+
return false
|
|
469
|
+
}
|
|
470
|
+
q := strings.ReplaceAll(regexp.QuoteMeta(strings.Join(strings.Fields(pat), " ")), `\*`, ".*")
|
|
471
|
+
rx := regexp.MustCompile(`(?s)^` + q + `$`)
|
|
472
|
+
return rx.MatchString(callee) || rx.MatchString(full)
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
func uniq(s []string) []string {
|
|
476
|
+
var r []string
|
|
477
|
+
for i, x := range s {
|
|
478
|
+
if i == 0 || x != s[i-1] {
|
|
479
|
+
r = append(r, x)
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return r
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
func isIgnoredGoPath(file string) bool {
|
|
486
|
+
for _, part := range strings.Split(filepath.ToSlash(file), "/") {
|
|
487
|
+
if part == "vendor" || part == "testdata" || strings.HasPrefix(part, ".") || strings.HasPrefix(part, "_") {
|
|
488
|
+
return true
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return false
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
func isMainModulePackage(root, directory string) (bool, error) {
|
|
495
|
+
cmd := exec.Command("go", "list", "-e", "-json", "./"+directory)
|
|
496
|
+
cmd.Dir = root
|
|
497
|
+
var stderr bytes.Buffer
|
|
498
|
+
cmd.Stderr = &stderr
|
|
499
|
+
raw, err := cmd.Output()
|
|
500
|
+
if err != nil {
|
|
501
|
+
return false, fmt.Errorf("go list failed in %s: %w: %s", directory, err, strings.Split(strings.TrimSpace(stderr.String()), "\n")[0])
|
|
502
|
+
}
|
|
503
|
+
var info pkgInfo
|
|
504
|
+
if err := json.Unmarshal(raw, &info); err != nil {
|
|
505
|
+
return false, fmt.Errorf("go list output invalid in %s: %w", directory, err)
|
|
506
|
+
}
|
|
507
|
+
return info.Module != nil && info.Module.Main, nil
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
func unloadedGoFiles(root string, tracked, excluded, covered map[string]bool) ([]string, error) {
|
|
511
|
+
var files, unloaded []string
|
|
512
|
+
for file := range tracked {
|
|
513
|
+
files = append(files, file)
|
|
514
|
+
}
|
|
515
|
+
sort.Strings(files)
|
|
516
|
+
for _, file := range files {
|
|
517
|
+
if !strings.HasSuffix(file, ".go") || excluded[file] || isIgnoredGoPath(file) {
|
|
518
|
+
continue
|
|
519
|
+
}
|
|
520
|
+
directory := filepath.ToSlash(filepath.Dir(file))
|
|
521
|
+
if _, known := covered[directory]; !known {
|
|
522
|
+
inside, err := isMainModulePackage(root, directory)
|
|
523
|
+
if err != nil {
|
|
524
|
+
return nil, err
|
|
525
|
+
}
|
|
526
|
+
covered[directory] = inside
|
|
527
|
+
}
|
|
528
|
+
if !covered[directory] {
|
|
529
|
+
unloaded = append(unloaded, file)
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return unloaded, nil
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
func first(s []string, n int) []string {
|
|
536
|
+
if len(s) > n {
|
|
537
|
+
return s[:n]
|
|
538
|
+
}
|
|
539
|
+
return nonNil(s)
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
func nonNil(s []string) []string {
|
|
543
|
+
if s == nil {
|
|
544
|
+
return []string{}
|
|
545
|
+
}
|
|
546
|
+
return s
|
|
547
|
+
}
|
fmap/goext.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Runs the Go extractor (goext/main.go, built once per source hash) on the repo (spec §12 P5)."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import shutil
|
|
6
|
+
import subprocess
|
|
7
|
+
|
|
8
|
+
from . import cache
|
|
9
|
+
|
|
10
|
+
HERE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "goext")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def go_bin():
|
|
14
|
+
return shutil.which("go")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def binary(root):
|
|
18
|
+
h = hashlib.sha256()
|
|
19
|
+
for name in ("main.go", "go.mod"):
|
|
20
|
+
with open(os.path.join(HERE, name), "rb") as fh:
|
|
21
|
+
h.update(fh.read())
|
|
22
|
+
ver = subprocess.run([go_bin(), "env", "GOVERSION"], capture_output=True, text=True).stdout.strip()
|
|
23
|
+
h.update(ver.encode())
|
|
24
|
+
exe = os.path.join(cache.cache_dir(root), f"fmap-go-{h.hexdigest()[:16]}")
|
|
25
|
+
if not os.path.exists(exe):
|
|
26
|
+
r = subprocess.run([go_bin(), "build", "-o", exe + ".tmp", "."], cwd=HERE, capture_output=True, text=True,
|
|
27
|
+
env=dict(os.environ, GOFLAGS="-mod=mod", GOWORK="off"))
|
|
28
|
+
if r.returncode != 0:
|
|
29
|
+
raise SystemExit(f"fmap: Go extractor build failed:\n{r.stderr[-4000:]}")
|
|
30
|
+
os.replace(exe + ".tmp", exe)
|
|
31
|
+
return exe
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def extract(root, files, config, excluded=()):
|
|
35
|
+
# shortcut: no per-package result cache (type-checking from export data is fast), add one if a big Go repo appears
|
|
36
|
+
if not any(f.endswith(".go") for f in files) or not go_bin():
|
|
37
|
+
return []
|
|
38
|
+
payload = {"root": root, "tracked": files, "excluded": sorted(excluded), "config": {k: config.get(k, []) for k in ("dispatch", "process")}}
|
|
39
|
+
r = subprocess.run([binary(root)], input=json.dumps(payload), capture_output=True, text=True, cwd=root,
|
|
40
|
+
env=dict(os.environ, GOPROXY="off")) # never download: uninstalled deps are "deps missing"
|
|
41
|
+
if r.returncode != 0:
|
|
42
|
+
raise SystemExit(f"fmap: Go extractor failed:\n{r.stderr[-4000:]}")
|
|
43
|
+
return [json.loads(line) for line in r.stdout.splitlines() if line]
|