featmap 0.1.0__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.
Potentially problematic release.
This version of featmap might be problematic. Click here for more details.
- featmap-0.1.0.dist-info/METADATA +129 -0
- featmap-0.1.0.dist-info/RECORD +130 -0
- featmap-0.1.0.dist-info/WHEEL +5 -0
- featmap-0.1.0.dist-info/entry_points.txt +2 -0
- featmap-0.1.0.dist-info/licenses/LICENSE +24 -0
- featmap-0.1.0.dist-info/licenses/fmap/vendor/typescript/LICENSE.txt +55 -0
- featmap-0.1.0.dist-info/licenses/fmap/vendor/typescript/ThirdPartyNoticeText.txt +193 -0
- featmap-0.1.0.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/routes.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""External route sources (spec §10.1): OpenAPI files, OWASP Noir, and an opt-in runtime command.
|
|
2
|
+
|
|
3
|
+
Each gives (method, path, file, line) or an OpenAPI operationId; routes are tied to the handler node
|
|
4
|
+
the location points at. Routes the built-in extractors already proved win; nothing here guesses a handler.
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import re
|
|
9
|
+
import shutil
|
|
10
|
+
import subprocess
|
|
11
|
+
import tempfile
|
|
12
|
+
|
|
13
|
+
HTTP_METHODS = {"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
|
|
14
|
+
OPENAPI_FILE = re.compile(r"(^|/)(openapi|swagger)[^/]*\.json$", re.I)
|
|
15
|
+
FUNCS = ("function", "method")
|
|
16
|
+
# shortcut: a decorator stack longer than this many lines is not tied to the def below it; raise if real code needs more
|
|
17
|
+
DECORATOR_SPAN = 40
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def handler_at(nodes_by_file, file, line):
|
|
21
|
+
"""The function the route location names: the first def at/after a decorator line, else the innermost enclosing one."""
|
|
22
|
+
after = [(n["line"], nid) for nid, n in nodes_by_file.get(file, []) if n["line"] >= line and n["line"] - line <= DECORATOR_SPAN]
|
|
23
|
+
if after:
|
|
24
|
+
return min(after)[1]
|
|
25
|
+
around = [(n["end_line"] - n["line"], nid) for nid, n in nodes_by_file.get(file, []) if n["line"] <= line <= n["end_line"]]
|
|
26
|
+
return min(around)[1] if around else None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def from_locations(items, nodes_by_file, source, marks=()):
|
|
30
|
+
by_site = {m["at"]: m["handler"] for m in marks if m.get("handler")} # registration call names its handler
|
|
31
|
+
out, unmapped = [], []
|
|
32
|
+
for method, path, file, line in items:
|
|
33
|
+
method = (method or "").upper()
|
|
34
|
+
if method not in HTTP_METHODS or not isinstance(path, str) or not path.startswith("/"):
|
|
35
|
+
continue
|
|
36
|
+
nid = by_site.get(f"{file}:{line}") or (handler_at(nodes_by_file, file, line) if file and line else None)
|
|
37
|
+
if nid is None:
|
|
38
|
+
unmapped.append(f"{method} {path}")
|
|
39
|
+
continue
|
|
40
|
+
out.append({"kind": "route", "value": f"{method} {path}", "node": nid, "at": f"{file}:{line}", "source": source})
|
|
41
|
+
return out, unmapped
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def openapi_routes(root, files, nodes, marks=()):
|
|
45
|
+
"""Paths of tracked OpenAPI JSON files. The handler is the one route-shaped decorator (a safety-net `?route`
|
|
46
|
+
with the same method and a path the spec path ends with), else the one function the operationId starts with."""
|
|
47
|
+
by_name = {}
|
|
48
|
+
for nid, n in nodes.items():
|
|
49
|
+
if n["kind"] in FUNCS:
|
|
50
|
+
by_name.setdefault(nid.rsplit(":", 1)[1].rsplit(".", 1)[-1], []).append(nid)
|
|
51
|
+
out, unmapped, used = [], [], []
|
|
52
|
+
for p in files:
|
|
53
|
+
if not OPENAPI_FILE.search(p):
|
|
54
|
+
continue
|
|
55
|
+
try:
|
|
56
|
+
with open(os.path.join(root, p), encoding="utf-8") as fh:
|
|
57
|
+
doc = json.load(fh)
|
|
58
|
+
except (OSError, ValueError):
|
|
59
|
+
continue
|
|
60
|
+
if not isinstance(doc, dict) or not isinstance(doc.get("paths"), dict) or not ("openapi" in doc or "swagger" in doc):
|
|
61
|
+
continue
|
|
62
|
+
used.append(p)
|
|
63
|
+
for path, ops in doc["paths"].items():
|
|
64
|
+
for method, op in (ops or {}).items():
|
|
65
|
+
if method.upper() not in HTTP_METHODS or not isinstance(op, dict):
|
|
66
|
+
continue
|
|
67
|
+
decorated = {m["handler"] for m in marks if m.get("handler") and m.get("method") == method.upper()
|
|
68
|
+
and path.endswith(m["path"])}
|
|
69
|
+
if len(decorated) == 1:
|
|
70
|
+
nid = decorated.pop()
|
|
71
|
+
out.append({"kind": "route", "value": f"{method.upper()} {path}", "node": nid,
|
|
72
|
+
"at": f"{nodes[nid]['file']}:{nodes[nid]['line']}", "source": f"openapi:{p}"})
|
|
73
|
+
continue
|
|
74
|
+
op_id = op.get("operationId") or ""
|
|
75
|
+
# FastAPI default ids are "<function>_<path>_<method>"; other generators use the bare name
|
|
76
|
+
names = [n for n in by_name if op_id == n or op_id.startswith(n + "_")]
|
|
77
|
+
longest = max(names, key=len) if names else None
|
|
78
|
+
hits = by_name.get(longest, []) if longest else []
|
|
79
|
+
if len(hits) != 1:
|
|
80
|
+
unmapped.append(f"{method.upper()} {path}")
|
|
81
|
+
continue
|
|
82
|
+
n = nodes[hits[0]]
|
|
83
|
+
out.append({"kind": "route", "value": f"{method.upper()} {path}", "node": hits[0],
|
|
84
|
+
"at": f"{n['file']}:{n['line']}", "source": f"openapi:{p}"})
|
|
85
|
+
return out, unmapped, used
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def noir_bin():
|
|
89
|
+
env = os.environ.get("FMAP_NOIR")
|
|
90
|
+
if env == "0":
|
|
91
|
+
return None
|
|
92
|
+
return env or shutil.which("noir")
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def run_json(cmd, root, timeout):
|
|
96
|
+
with tempfile.TemporaryDirectory(prefix="fmap-routes-") as tmp:
|
|
97
|
+
out = os.path.join(tmp, "routes.json")
|
|
98
|
+
r = subprocess.run(cmd(out) if callable(cmd) else cmd, cwd=root, shell=not callable(cmd),
|
|
99
|
+
capture_output=True, text=True, timeout=timeout)
|
|
100
|
+
if r.returncode != 0:
|
|
101
|
+
raise RuntimeError((r.stderr or r.stdout).strip()[-300:])
|
|
102
|
+
if callable(cmd):
|
|
103
|
+
with open(out, encoding="utf-8") as fh:
|
|
104
|
+
text = fh.read()
|
|
105
|
+
else:
|
|
106
|
+
text = r.stdout
|
|
107
|
+
return json.loads(text)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def locations(doc, root):
|
|
111
|
+
"""Noir report ({"endpoints": [...]}) or a plain list of {method, path, file, line}."""
|
|
112
|
+
rel = lambda f: os.path.normpath(os.path.relpath(f, root) if os.path.isabs(f) else f) if f else f # noir prints "./x.py"
|
|
113
|
+
if isinstance(doc, dict) and "endpoints" in doc:
|
|
114
|
+
for e in doc["endpoints"]:
|
|
115
|
+
code = (e.get("details") or {}).get("code_paths") or [{}]
|
|
116
|
+
yield e.get("method"), e.get("url"), rel(code[0].get("path")), code[0].get("line")
|
|
117
|
+
elif isinstance(doc, list):
|
|
118
|
+
for e in doc:
|
|
119
|
+
yield e.get("method"), e.get("path"), rel(e.get("file")), e.get("line")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def external_routes(root, files, nodes, config, known, marks=()):
|
|
123
|
+
"""Returns (route entries not already in `known`, programs, errors, notes)."""
|
|
124
|
+
nodes_by_file = {}
|
|
125
|
+
for nid, n in nodes.items():
|
|
126
|
+
if n["kind"] in FUNCS:
|
|
127
|
+
nodes_by_file.setdefault(n["file"], []).append((nid, n))
|
|
128
|
+
found, programs, errors, notes = [], [], [], []
|
|
129
|
+
for rule in config.get("routes", []): # opt-in runtime command: runs project code, so never automatic
|
|
130
|
+
if not rule.get("cmd") or not rule.get("reason"):
|
|
131
|
+
errors.append("routes: `cmd` and `reason` are required")
|
|
132
|
+
continue
|
|
133
|
+
try:
|
|
134
|
+
items, unmapped = from_locations(locations(run_json(rule["cmd"], root, 300), root), nodes_by_file, "runtime", marks)
|
|
135
|
+
except (OSError, ValueError, RuntimeError, subprocess.TimeoutExpired) as error:
|
|
136
|
+
errors.append(f"routes cmd `{rule['cmd']}` failed: {error}")
|
|
137
|
+
continue
|
|
138
|
+
found += items
|
|
139
|
+
programs.append({"name": "routes", "deps": "ok", "tool": "runtime"})
|
|
140
|
+
notes += [f"not: routes cmd: {len(unmapped)} route has no handler in the graph ({', '.join(unmapped[:3])})"] if unmapped else []
|
|
141
|
+
items, unmapped, used = openapi_routes(root, files, nodes, marks)
|
|
142
|
+
found += items
|
|
143
|
+
programs += [{"name": "routes", "deps": "ok", "tool": f"openapi:{p}"} for p in used]
|
|
144
|
+
if unmapped:
|
|
145
|
+
notes.append(f"not: openapi: {len(unmapped)} operation has no unique handler ({', '.join(unmapped[:3])})")
|
|
146
|
+
binary = noir_bin()
|
|
147
|
+
if binary:
|
|
148
|
+
try:
|
|
149
|
+
version = subprocess.run([binary, "--version"], capture_output=True, text=True, timeout=30).stdout.strip()
|
|
150
|
+
doc = run_json(lambda out: [binary, "-b", root, "-f", "json", "--no-color", "-o", out], root, 300)
|
|
151
|
+
items, unmapped = from_locations(locations(doc, root), nodes_by_file, "noir", marks)
|
|
152
|
+
found += items
|
|
153
|
+
programs.append({"name": "routes", "deps": "ok", "tool": f"noir@{version}"})
|
|
154
|
+
notes += [f"not: noir: {len(unmapped)} route has no handler in the graph ({', '.join(unmapped[:3])})"] if unmapped else []
|
|
155
|
+
except (OSError, ValueError, RuntimeError, subprocess.TimeoutExpired) as error:
|
|
156
|
+
notes.append(f"not: noir failed, its routes are missing: {error}")
|
|
157
|
+
seen = {e["value"] for e in known if e["kind"] == "route"}
|
|
158
|
+
out = []
|
|
159
|
+
for e in found: # order = priority: runtime, OpenAPI, Noir
|
|
160
|
+
if e["value"] not in seen:
|
|
161
|
+
seen.add(e["value"])
|
|
162
|
+
out.append(e)
|
|
163
|
+
return out, programs, errors, notes
|
fmap/rsx.py
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
"""Rust extractor: rust-analyzer SCIP index -> nodes, call/ref/impl edges (spec §12 P4)."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import re
|
|
6
|
+
import shutil
|
|
7
|
+
import subprocess
|
|
8
|
+
|
|
9
|
+
from . import axum, scip
|
|
10
|
+
from . import cache as repository_cache
|
|
11
|
+
|
|
12
|
+
with open(os.path.abspath(__file__), "rb") as _fh:
|
|
13
|
+
with open(axum.__file__, "rb") as _axum_fh:
|
|
14
|
+
SCRIPT_HASH = hashlib.sha256(_fh.read() + _axum_fh.read()).hexdigest()
|
|
15
|
+
DESC = re.compile(r"impl#\[([^\]]*)\](?:\[([^\]]*)\])?|`([^`]*)`(.)|([\w$-]+)(\(\)\.|[/#.!])")
|
|
16
|
+
DIAG = re.compile(r"file (\S+?): \w+ \w+\(\"(E0425|E0433|E0599)\"\) from LineCol \{ line: (\d+), col: (\d+) \} "
|
|
17
|
+
r"to LineCol \{ line: (\d+), col: (\d+) \}")
|
|
18
|
+
CLOSURE_AFTER = re.compile(r"\s*(?::[^=;]*)?=\s*(?:async\s+)?(?:move\s+)?\|")
|
|
19
|
+
CALL_AFTER = re.compile(r"\s*(?:::\s*<[^()]*>\s*)?\(")
|
|
20
|
+
# std wrapper semantics for derived impls: does W::default() / W::clone() run the inner type's impl?
|
|
21
|
+
EMPTY_DEFAULT = {"Option", "Vec", "VecDeque", "HashMap", "HashSet", "BTreeMap", "BTreeSet", "BinaryHeap", "PhantomData", "Weak"}
|
|
22
|
+
INNER_DEFAULT = {"Box", "Arc", "Rc", "Mutex", "RwLock", "Cell", "RefCell"}
|
|
23
|
+
SHARED_CLONE = {"Arc", "Rc", "Weak", "PhantomData"}
|
|
24
|
+
INNER_CLONE = {"Option", "Vec", "VecDeque", "HashMap", "HashSet", "BTreeMap", "BTreeSet", "BinaryHeap", "Box"}
|
|
25
|
+
DERIVED_CALL = re.compile(r"impl#\[([^\]]*)\]\[(Default|Clone)\](default|clone)\(\)\.$")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def ra_bin():
|
|
29
|
+
"""rust-analyzer from rustup (a mise shim may resolve back to itself); None if unusable."""
|
|
30
|
+
cands = []
|
|
31
|
+
r = subprocess.run(["rustup", "which", "rust-analyzer"], capture_output=True, text=True) if shutil.which("rustup") else None
|
|
32
|
+
if r and r.returncode == 0:
|
|
33
|
+
cands.append(r.stdout.strip())
|
|
34
|
+
if shutil.which("rust-analyzer"):
|
|
35
|
+
cands.append(shutil.which("rust-analyzer"))
|
|
36
|
+
for c in cands:
|
|
37
|
+
v = subprocess.run([c, "--version"], capture_output=True, text=True)
|
|
38
|
+
if v.returncode == 0 and v.stdout.startswith("rust-analyzer"):
|
|
39
|
+
return c, v.stdout.strip()
|
|
40
|
+
return None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def crate_roots(root, files):
|
|
44
|
+
tomls = sorted(os.path.dirname(f) for f in files if os.path.basename(f) == "Cargo.toml")
|
|
45
|
+
ws = [d for d in tomls if "[workspace]" in tsx_read(root, os.path.join(d, "Cargo.toml"))]
|
|
46
|
+
out = []
|
|
47
|
+
for d in tomls:
|
|
48
|
+
if any(w != d and (w == "" or d.startswith(w + "/")) for w in ws):
|
|
49
|
+
continue # a member of a workspace indexed from its root
|
|
50
|
+
out.append(d)
|
|
51
|
+
return out
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def tsx_read(root, rel):
|
|
55
|
+
with open(os.path.join(root, rel), encoding="utf-8", errors="replace") as fh:
|
|
56
|
+
return fh.read()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def parse_desc(desc):
|
|
60
|
+
"""`store/impl#[Mem][Store]get().` -> (["store", "Mem", "get"], kinds, trait)."""
|
|
61
|
+
parts, kinds, trait = [], [], None
|
|
62
|
+
for m in DESC.finditer(desc):
|
|
63
|
+
if m.group(1) is not None:
|
|
64
|
+
parts.append(m.group(1).strip("`").split("<")[0])
|
|
65
|
+
kinds.append("#")
|
|
66
|
+
trait = (m.group(2) or "").strip("`").split("<")[0] or trait
|
|
67
|
+
elif m.group(3) is not None:
|
|
68
|
+
parts.append(m.group(3))
|
|
69
|
+
kinds.append(m.group(4))
|
|
70
|
+
else:
|
|
71
|
+
parts.append(m.group(5))
|
|
72
|
+
kinds.append(m.group(6))
|
|
73
|
+
return parts, kinds, trait
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def extract(root, files, config, excluded=(), use_cache=True):
|
|
77
|
+
rs = [f for f in files if f.endswith(".rs")]
|
|
78
|
+
ra = ra_bin() if rs else None
|
|
79
|
+
if not ra:
|
|
80
|
+
return [], bool(rs)
|
|
81
|
+
tracked = set(files)
|
|
82
|
+
records = []
|
|
83
|
+
crates = crate_roots(root, files)
|
|
84
|
+
for d in crates:
|
|
85
|
+
records += extract_crate(root, d, tracked, ra, excluded, use_cache)
|
|
86
|
+
# Files outside Cargo roots have no Rust cache or SCIP documents.
|
|
87
|
+
orphaned = [p for p in rs if not any(not d or p.startswith(d + "/") for d in crates)]
|
|
88
|
+
known = axum.frameworks({path: tsx_read(root, path) for path in orphaned}) if orphaned else set()
|
|
89
|
+
records += [{"t": "fw", "name": axum.FRAMEWORKS[name]} for name in sorted(known)]
|
|
90
|
+
if any(r["t"] == "entry" and r["kind"] == "route" for r in records):
|
|
91
|
+
records = [r for r in records if r["t"] != "fw" or r["name"] != "axum"]
|
|
92
|
+
return records, False
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def extract_crate(root, crate, tracked, ra, excluded=(), use_cache=True):
|
|
96
|
+
cdir = os.path.join(root, crate)
|
|
97
|
+
name = os.path.join(crate, "Cargo.toml") if crate else "Cargo.toml"
|
|
98
|
+
meta = subprocess.run(["cargo", "metadata", "--offline", "--format-version", "1"], cwd=cdir,
|
|
99
|
+
capture_output=True, text=True)
|
|
100
|
+
if meta.returncode != 0:
|
|
101
|
+
err = [l for l in meta.stderr.splitlines() if l.strip()]
|
|
102
|
+
known = axum.frameworks({p: tsx_read(root, p) for p in sorted(tracked)
|
|
103
|
+
if p.endswith(".rs") and (not crate or p.startswith(crate + "/"))})
|
|
104
|
+
return [{"t": "fw", "name": axum.FRAMEWORKS[n]} for n in sorted(known)] + [{"t": "program", "name": name, "deps": "failed", "missing_count": 0,
|
|
105
|
+
"missing": [], "outside": [], "loaded": [], "tool": ra[1],
|
|
106
|
+
"failed": f"cargo metadata --offline failed in {crate or '.'}: {err[0] if err else f'exit {meta.returncode}'}"}]
|
|
107
|
+
members = {p["name"].replace("-", "_") for p in json.loads(meta.stdout)["packages"] if p.get("source") is None}
|
|
108
|
+
members |= {p["name"] for p in json.loads(meta.stdout)["packages"] if p.get("source") is None}
|
|
109
|
+
srcs = sorted(f for f in tracked if f.startswith(crate + "/" if crate else "") and
|
|
110
|
+
(f.endswith(".rs") or os.path.basename(f) in ("Cargo.toml", "Cargo.lock")))
|
|
111
|
+
h = hashlib.sha256(json.dumps([SCRIPT_HASH, ra[1], name, sorted(excluded)]).encode())
|
|
112
|
+
for f in srcs:
|
|
113
|
+
h.update(f.encode() + b"\0" + repository_cache.file_sig(root, f).encode())
|
|
114
|
+
# keyed by checkout root like the TS cache: the revision slot and the working tree keep separate entries
|
|
115
|
+
stem = os.path.join(repository_cache.cache_dir(root), "rs-" + hashlib.sha1((os.path.realpath(root) + "\0" + name).encode()).hexdigest())
|
|
116
|
+
cache = stem + ".json"
|
|
117
|
+
if use_cache and os.path.exists(cache):
|
|
118
|
+
with open(cache) as fh:
|
|
119
|
+
c = json.load(fh)
|
|
120
|
+
if c.get("key") == h.hexdigest():
|
|
121
|
+
return c["records"]
|
|
122
|
+
out = os.path.abspath(stem + ".scip")
|
|
123
|
+
r = subprocess.run([ra[0], "scip", ".", "--output", out], cwd=cdir, capture_output=True, text=True)
|
|
124
|
+
if r.returncode != 0:
|
|
125
|
+
raise SystemExit(f"fmap: rust-analyzer scip failed in {crate or '.'}:\n{r.stderr[-4000:]}")
|
|
126
|
+
# SCIP omits what rust-analyzer could not resolve; its diagnostics name those sites
|
|
127
|
+
dg = subprocess.run([ra[0], "diagnostics", "."], cwd=cdir, capture_output=True, text=True)
|
|
128
|
+
# rust-analyzer prints real paths; a root reached through a symlink must be resolved the same way
|
|
129
|
+
diags = [(os.path.relpath(os.path.realpath(m.group(1)), os.path.realpath(root)).replace(os.sep, "/"), m.group(2), int(m.group(3)), int(m.group(4)),
|
|
130
|
+
int(m.group(6))) for m in DIAG.finditer(dg.stdout.replace("\r", "\n"))]
|
|
131
|
+
recs = from_index(root, crate, scip.read(out), tracked, members, diags, excluded, [p for p in srcs if p.endswith(".rs")])
|
|
132
|
+
outside = recs.pop()["files"]
|
|
133
|
+
recs.append({"t": "program", "name": name, "deps": "ok", "missing_count": 0, "missing": [], "outside": outside,
|
|
134
|
+
"loaded": [], "tool": ra[1]})
|
|
135
|
+
os.remove(out)
|
|
136
|
+
if use_cache:
|
|
137
|
+
with open(cache + ".tmp", "w") as fh:
|
|
138
|
+
json.dump({"key": h.hexdigest(), "records": recs}, fh)
|
|
139
|
+
os.replace(cache + ".tmp", cache)
|
|
140
|
+
return recs
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def from_index(root, crate, docs, tracked, members, diags=(), excluded=(), framework_files=()):
|
|
144
|
+
pre = crate + "/" if crate else ""
|
|
145
|
+
|
|
146
|
+
def project_desc(sym):
|
|
147
|
+
p = sym.split(" ")
|
|
148
|
+
return " ".join(p[4:]) if len(p) >= 5 and p[0] == "rust-analyzer" and p[2] in members else None
|
|
149
|
+
|
|
150
|
+
# pass 1: definitions -> node ids
|
|
151
|
+
defs, recs, outside = {}, [], []
|
|
152
|
+
excluded_symbols = {o["symbol"] for d in docs if pre + d["path"] in excluded
|
|
153
|
+
for o in d["occ"] if o["roles"] & 1 and not o["symbol"].startswith("local ")}
|
|
154
|
+
for d in docs:
|
|
155
|
+
path = pre + d["path"]
|
|
156
|
+
if path in excluded:
|
|
157
|
+
continue
|
|
158
|
+
if path not in tracked:
|
|
159
|
+
outside.append(path) # spec §5.4: an untracked/generated file in the program blocks check
|
|
160
|
+
continue
|
|
161
|
+
text = tsx_read(root, path)
|
|
162
|
+
d["file"], d["lines"] = path, text.split("\n")
|
|
163
|
+
mod = next((project_desc(o["symbol"]) for o in d["occ"]
|
|
164
|
+
if o["roles"] & 1 and o["range"][:2] == (0, 0) and o["symbol"].endswith("/")), "") or ""
|
|
165
|
+
mod_parts = parse_desc(mod)[0] if mod and mod != "crate/" else []
|
|
166
|
+
recs.append({"t": "node", "id": f"{path}:<module>", "kind": "module", "line": 1, "end_line": len(d["lines"])})
|
|
167
|
+
d["defs"] = []
|
|
168
|
+
seen = {}
|
|
169
|
+
for o in d["occ"]:
|
|
170
|
+
desc = project_desc(o["symbol"])
|
|
171
|
+
if not (o["roles"] & 1) or not desc or o["enclosing"] is None:
|
|
172
|
+
continue
|
|
173
|
+
parts, kinds, trait = parse_desc(desc)
|
|
174
|
+
if not kinds or kinds[-1] not in ("().", "#"):
|
|
175
|
+
continue
|
|
176
|
+
if parts[:len(mod_parts)] == mod_parts:
|
|
177
|
+
parts = parts[len(mod_parts):]
|
|
178
|
+
qual = ".".join(p for p in parts if p != "crate")
|
|
179
|
+
nid = f"{path}:{qual}"
|
|
180
|
+
seen[nid] = seen.get(nid, 0) + 1
|
|
181
|
+
if seen[nid] > 1:
|
|
182
|
+
nid = f"{nid}#{seen[nid]}"
|
|
183
|
+
kind = "class" if kinds[-1] == "#" else ("method" if len(kinds) > 1 and kinds[-2] == "#" else "function")
|
|
184
|
+
# rust-analyzer repeats symbols across a package's crates (tests/*.rs, bins): keep every definition
|
|
185
|
+
defs.setdefault(o["symbol"], []).append((nid, kinds, trait, d, o))
|
|
186
|
+
enc = o["enclosing"]
|
|
187
|
+
d["defs"].append((enc, nid, kind))
|
|
188
|
+
recs.append({"t": "node", "id": nid, "kind": kind, "line": enc[0] + 1, "end_line": enc[2] + 1})
|
|
189
|
+
|
|
190
|
+
def within(r, enc):
|
|
191
|
+
return (r[0], r[1]) >= (enc[0], enc[1]) and (r[2], r[3]) <= (enc[2], enc[3])
|
|
192
|
+
|
|
193
|
+
derived_cache = {}
|
|
194
|
+
project_types = {parse_desc(project_desc(sym))[0][-1] for sym in defs if sym.endswith("#") and project_desc(sym)}
|
|
195
|
+
|
|
196
|
+
def derived_type(tsym, trait, method, seen):
|
|
197
|
+
"""Project fns a `#[derive(Default|Clone)]` impl of tsym runs, or None when that is not provable:
|
|
198
|
+
a field is followed only when its type is a bare project type (tuple/array parts included);
|
|
199
|
+
`Vec<P>`, `Rc<P>`, `&P`, generics and unknown impls stay `?macro`."""
|
|
200
|
+
key = (tsym, trait)
|
|
201
|
+
if key in derived_cache:
|
|
202
|
+
return derived_cache[key]
|
|
203
|
+
if tsym in seen:
|
|
204
|
+
return set() # a cycle adds no new callee: the ancestor on the stack collects its own fields
|
|
205
|
+
top = not seen # only the outermost result is complete; a nested one may sit under a cycle cut
|
|
206
|
+
seen = seen | {tsym}
|
|
207
|
+
lst = defs.get(tsym, [])
|
|
208
|
+
if len({c[0] for c in lst}) != 1:
|
|
209
|
+
return None
|
|
210
|
+
d, enc = lst[0][3], lst[0][4]["enclosing"]
|
|
211
|
+
inside = [x for x in d["occ"] if within(x["range"], enc)]
|
|
212
|
+
refs = [x for x in inside if not x["roles"] & 1]
|
|
213
|
+
if not any(x["symbol"].endswith(f"/{trait}:") for x in refs) or any(
|
|
214
|
+
x["symbol"].startswith((tsym + "[", "local ")) for x in inside):
|
|
215
|
+
return None # not derived here, or generic (rust-analyzer emits type params as `local N`)
|
|
216
|
+
if trait == "Default" and any(x["roles"] & 1 and x["symbol"].endswith("#") and x["symbol"] != tsym for x in inside):
|
|
217
|
+
derived_cache[key] = set() # enum: `#[default]` is a unit variant, no field default runs
|
|
218
|
+
return set()
|
|
219
|
+
text = "\n".join(d["lines"][enc[0]:enc[2] + 1])
|
|
220
|
+
if re.search(r"\bfn\b", text[enc[1]:]):
|
|
221
|
+
return None # a fn pointer field: std's default stub never runs the return type's impl
|
|
222
|
+
out = set()
|
|
223
|
+
for x in refs:
|
|
224
|
+
if not x["symbol"].endswith("#") or x["symbol"] == tsym or not project_desc(x["symbol"]):
|
|
225
|
+
continue
|
|
226
|
+
off = sum(len(l) + 1 for l in d["lines"][enc[0]:x["range"][0]]) + x["range"][1]
|
|
227
|
+
before = text[enc[1]:off].replace("->", "")
|
|
228
|
+
if re.search(r"(&\s*(?:'\w+\s+)?(?:mut\s+)?|\*(?:const|mut)\s+|\bdyn\s+|\bimpl\s+)$", before):
|
|
229
|
+
return None # behind a reference/pointer, or a trait object type
|
|
230
|
+
stack = [] # generic wrappers around this mention, outermost first
|
|
231
|
+
for w in re.finditer(r"(\w*)\s*(<)|>", before):
|
|
232
|
+
if w[2]:
|
|
233
|
+
stack.append(w[1])
|
|
234
|
+
elif stack:
|
|
235
|
+
stack.pop()
|
|
236
|
+
if any(w in project_types for w in stack):
|
|
237
|
+
return None # a project type shadows a std wrapper name
|
|
238
|
+
if trait == "Default":
|
|
239
|
+
if any(w in EMPTY_DEFAULT for w in stack):
|
|
240
|
+
continue # an empty container never builds the inner value
|
|
241
|
+
if not all(w in INNER_DEFAULT for w in stack):
|
|
242
|
+
return None
|
|
243
|
+
else:
|
|
244
|
+
if any(w in SHARED_CLONE for w in stack):
|
|
245
|
+
continue # cloning a shared pointer bumps a count, the inner value is not cloned
|
|
246
|
+
if not all(w in INNER_CLONE for w in stack):
|
|
247
|
+
return None
|
|
248
|
+
got = impl_of(x["symbol"], trait, method, seen)
|
|
249
|
+
if got is None:
|
|
250
|
+
return None
|
|
251
|
+
out |= got
|
|
252
|
+
if top:
|
|
253
|
+
derived_cache[key] = out
|
|
254
|
+
return out
|
|
255
|
+
|
|
256
|
+
def impl_of(tsym, trait, method, seen):
|
|
257
|
+
if len({c[0] for c in defs.get(tsym, [])}) != 1:
|
|
258
|
+
return None # rust-analyzer gives a fn-local type the outer type's symbol: ambiguous
|
|
259
|
+
name = parse_desc(project_desc(tsym))[0][-1]
|
|
260
|
+
# the impl symbol carries the type's own module path; a same-named type elsewhere is another type
|
|
261
|
+
# shortcut: an impl written in another module of the type is missed (stays `?macro`), never a wrong edge
|
|
262
|
+
here = tsym[:max(tsym.rfind("/"), tsym.rfind(" ")) + 1]
|
|
263
|
+
manual = {c[0] for c in defs.get(f"{here}impl#[{name}][{trait}]{method}().", [])}
|
|
264
|
+
if manual:
|
|
265
|
+
return manual if len(manual) == 1 else None
|
|
266
|
+
return derived_type(tsym, trait, method, seen)
|
|
267
|
+
|
|
268
|
+
def inner(d, pos):
|
|
269
|
+
best = None
|
|
270
|
+
for enc, nid, kind in d["defs"]:
|
|
271
|
+
if kind != "class" and (enc[0], enc[1]) <= pos <= (enc[2], enc[3]):
|
|
272
|
+
if best is None or (enc[0], enc[1]) >= (best[0][0], best[0][1]):
|
|
273
|
+
best = (enc, nid)
|
|
274
|
+
return best[1] if best else f"{d['file']}:<module>"
|
|
275
|
+
|
|
276
|
+
# pass 2: references
|
|
277
|
+
for d in docs:
|
|
278
|
+
if "file" not in d:
|
|
279
|
+
continue
|
|
280
|
+
local_defs, closures = set(), set()
|
|
281
|
+
for o in d["occ"]:
|
|
282
|
+
if o["symbol"].startswith("local ") and o["roles"] & 1:
|
|
283
|
+
local_defs.add(o["symbol"])
|
|
284
|
+
ln, c = o["range"][2], o["range"][3]
|
|
285
|
+
# `let f = |x| ...` / `let f = move |x| ...`: its body sits in this frame and is already mapped
|
|
286
|
+
if ln < len(d["lines"]) and CLOSURE_AFTER.match(d["lines"][ln][c:]):
|
|
287
|
+
closures.add(o["symbol"])
|
|
288
|
+
for o in d["occ"]:
|
|
289
|
+
if o["roles"] & 1:
|
|
290
|
+
continue
|
|
291
|
+
sl, sc, el, ec = o["range"]
|
|
292
|
+
line = d["lines"][el] if el < len(d["lines"]) else ""
|
|
293
|
+
is_call = CALL_AFTER.match(line[ec:]) is not None
|
|
294
|
+
at = f"{d['file']}:{sl + 1}"
|
|
295
|
+
sym = o["symbol"]
|
|
296
|
+
if sym in excluded_symbols:
|
|
297
|
+
continue
|
|
298
|
+
if sym.startswith("local "):
|
|
299
|
+
if is_call and sym in local_defs and sym not in closures:
|
|
300
|
+
recs.append({"t": "edge", "from": inner(d, (sl, sc)), "to": "?", "kind": "?callback", "at": at,
|
|
301
|
+
"expr": line[sc:ec]})
|
|
302
|
+
continue
|
|
303
|
+
if sym.endswith("#") or not project_desc(sym):
|
|
304
|
+
continue # a type mention, or external (rust-analyzer resolved it outside the workspace)
|
|
305
|
+
me = inner(d, (sl, sc))
|
|
306
|
+
target, kinds = pick(defs.get(sym, []), d)
|
|
307
|
+
if target is None:
|
|
308
|
+
if is_call and sym.endswith(")."):
|
|
309
|
+
# a project symbol with no source definition: macro/derive generated (bitflags!, #[derive])
|
|
310
|
+
m = DERIVED_CALL.search(sym) if not defs.get(sym) else None
|
|
311
|
+
got = derived_type(sym[:m.start()] + m[1].strip("`").split("<")[0] + "#", m[2], m[3], frozenset()) if m else None
|
|
312
|
+
if got is not None:
|
|
313
|
+
recs += [{"t": "edge", "from": me, "to": t, "kind": "call", "at": at} for t in sorted(got) if t != me]
|
|
314
|
+
continue
|
|
315
|
+
recs.append({"t": "edge", "from": me, "to": "?", "kind": "?macro" if not defs.get(sym) else "?callback",
|
|
316
|
+
"at": at, "expr": line[sc:ec]})
|
|
317
|
+
continue
|
|
318
|
+
if me == target:
|
|
319
|
+
continue
|
|
320
|
+
if is_call:
|
|
321
|
+
trait_member = len(kinds) > 1 and kinds[-2] == "#" and "impl#" not in sym
|
|
322
|
+
recs.append({"t": "edge", "from": me, "to": target, "kind": "sigcall" if trait_member else "call",
|
|
323
|
+
"at": at, **({"expr": line[sc:ec]} if trait_member else {})})
|
|
324
|
+
else:
|
|
325
|
+
recs.append({"t": "edge", "from": me, "to": target, "kind": "ref", "at": at})
|
|
326
|
+
|
|
327
|
+
# pass 2b: unresolved names (E0425/E0433) block; unresolved methods (E0599) are method? (rust-analyzer's
|
|
328
|
+
# method inference has false positives on macro-heavy code, e.g. sqlx, so it must not block)
|
|
329
|
+
by_file = {d["file"]: d for d in docs if "file" in d}
|
|
330
|
+
for path, code, line, c0, c1 in sorted(set(diags)):
|
|
331
|
+
d = by_file.get(path)
|
|
332
|
+
if not d or line >= len(d["lines"]):
|
|
333
|
+
continue
|
|
334
|
+
text = d["lines"][line]
|
|
335
|
+
if not CALL_AFTER.match(text[c1:]):
|
|
336
|
+
continue
|
|
337
|
+
name, me, at = text[c0:c1], inner(d, (line, c0)), f"{path}:{line + 1}"
|
|
338
|
+
if code == "E0599":
|
|
339
|
+
recs.append({"t": "edge", "from": me, "to": "?", "kind": "method?", "at": at, "expr": name, "method": name})
|
|
340
|
+
else:
|
|
341
|
+
recs.append({"t": "edge", "from": me, "to": "?", "kind": "unresolved", "at": at, "expr": name,
|
|
342
|
+
"full": text.strip()})
|
|
343
|
+
|
|
344
|
+
# pass 3: impl edges, trait method -> impl method
|
|
345
|
+
for sym, lst in sorted(defs.items()):
|
|
346
|
+
for nid, kinds, trait, d, o in lst:
|
|
347
|
+
if not trait or not sym.endswith("()."):
|
|
348
|
+
continue
|
|
349
|
+
# shortcut: the trait is the nearest preceding reference named like the impl's trait in this file,
|
|
350
|
+
# upgrade if two traits with one name meet in a file
|
|
351
|
+
pos = o["range"][:2]
|
|
352
|
+
cands = [x for x in d["occ"] if not (x["roles"] & 1) and re.search(rf"[ /]{re.escape(trait)}#$", x["symbol"])
|
|
353
|
+
and x["range"][:2] < pos]
|
|
354
|
+
if not cands:
|
|
355
|
+
continue
|
|
356
|
+
tsym = max(cands, key=lambda x: x["range"][:2])["symbol"]
|
|
357
|
+
method = parse_desc(project_desc(sym) or "")[0][-1]
|
|
358
|
+
tm = pick(defs.get(f"{tsym}{method}().", []), d)[0]
|
|
359
|
+
if tm:
|
|
360
|
+
recs.append({"t": "edge", "from": tm, "to": nid, "kind": "impl", "at": f"{d['file']}:{o['range'][0] + 1}"})
|
|
361
|
+
sources, references = {}, {}
|
|
362
|
+
for d in docs:
|
|
363
|
+
if "file" not in d:
|
|
364
|
+
continue
|
|
365
|
+
sources[d["file"]] = "\n".join(d["lines"])
|
|
366
|
+
references[d["file"]] = {o["range"][:2]: pick(defs.get(o["symbol"], []), d)[0]
|
|
367
|
+
for o in d["occ"] if not o["roles"] & 1}
|
|
368
|
+
references[d["file"]] = {(line + 1, col): nid for (line, col), nid in references[d["file"]].items()}
|
|
369
|
+
framework_sources = dict(sources)
|
|
370
|
+
framework_sources.update({p: tsx_read(root, p) for p in framework_files if p not in sources})
|
|
371
|
+
recs += axum.extract(sources, {r["id"]: r for r in recs if r["t"] == "node"}, references, framework_sources)
|
|
372
|
+
recs.append({"t": "outside", "files": sorted(set(outside))})
|
|
373
|
+
return recs
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def pick(cands, doc):
|
|
377
|
+
"""The one definition of a symbol, or the one in the referencing file when the symbol is duplicated."""
|
|
378
|
+
if len(cands) == 1:
|
|
379
|
+
return cands[0][0], cands[0][1]
|
|
380
|
+
same = [c for c in cands if c[3] is doc]
|
|
381
|
+
return (same[0][0], same[0][1]) if len(same) == 1 else (None, None)
|
fmap/scip.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""Minimal stdlib reader for SCIP indexes (protobuf wire format), only the fields fmap uses."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def _varint(b, i):
|
|
5
|
+
r = s = 0
|
|
6
|
+
while True:
|
|
7
|
+
c = b[i]
|
|
8
|
+
i += 1
|
|
9
|
+
r |= (c & 0x7F) << s
|
|
10
|
+
if c < 0x80:
|
|
11
|
+
return r, i
|
|
12
|
+
s += 7
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def fields(b):
|
|
16
|
+
"""Yields (field number, wire type, value): ints for varints, bytes for length-delimited."""
|
|
17
|
+
i, n = 0, len(b)
|
|
18
|
+
while i < n:
|
|
19
|
+
key, i = _varint(b, i)
|
|
20
|
+
f, w = key >> 3, key & 7
|
|
21
|
+
if w == 0:
|
|
22
|
+
v, i = _varint(b, i)
|
|
23
|
+
elif w == 2:
|
|
24
|
+
ln, i = _varint(b, i)
|
|
25
|
+
v, i = b[i:i + ln], i + ln
|
|
26
|
+
elif w == 1:
|
|
27
|
+
v, i = b[i:i + 8], i + 8
|
|
28
|
+
elif w == 5:
|
|
29
|
+
v, i = b[i:i + 4], i + 4
|
|
30
|
+
else:
|
|
31
|
+
raise ValueError(f"scip: unsupported wire type {w}")
|
|
32
|
+
yield f, w, v
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _ints(w, v):
|
|
36
|
+
if w == 0:
|
|
37
|
+
return [v]
|
|
38
|
+
out, i = [], 0
|
|
39
|
+
while i < len(v):
|
|
40
|
+
x, i = _varint(v, i)
|
|
41
|
+
out.append(x)
|
|
42
|
+
return out
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _range(r):
|
|
46
|
+
# [startLine, startChar, endLine, endChar] or [line, startChar, endChar]; 0-based
|
|
47
|
+
return (r[0], r[1], r[2], r[3]) if len(r) == 4 else (r[0], r[1], r[0], r[2])
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def occurrence(b):
|
|
51
|
+
o = {"range": [], "symbol": "", "roles": 0, "enclosing": None}
|
|
52
|
+
for f, w, v in fields(b):
|
|
53
|
+
if f == 1:
|
|
54
|
+
o["range"] += _ints(w, v)
|
|
55
|
+
elif f == 2:
|
|
56
|
+
o["symbol"] = v.decode()
|
|
57
|
+
elif f == 3:
|
|
58
|
+
o["roles"] = v
|
|
59
|
+
elif f == 7:
|
|
60
|
+
o.setdefault("_enc", []).extend(_ints(w, v))
|
|
61
|
+
o["range"] = _range(o["range"])
|
|
62
|
+
if o.get("_enc"):
|
|
63
|
+
o["enclosing"] = _range(o.pop("_enc"))
|
|
64
|
+
return o
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def read(path):
|
|
68
|
+
with open(path, "rb") as fh:
|
|
69
|
+
b = fh.read()
|
|
70
|
+
docs = []
|
|
71
|
+
for f, _, v in fields(b):
|
|
72
|
+
if f == 2:
|
|
73
|
+
d = {"path": "", "occ": []}
|
|
74
|
+
for df, _, dv in fields(v):
|
|
75
|
+
if df == 1:
|
|
76
|
+
d["path"] = dv.decode()
|
|
77
|
+
elif df == 2:
|
|
78
|
+
d["occ"].append(occurrence(dv))
|
|
79
|
+
docs.append(d)
|
|
80
|
+
return docs
|