Report full function extents from the stock observer

The observer emits an optional extent inside each proven definition
span from the body's last statement finish (the C++ frontend does not
set function_end_locus). The occurrence decoder binds it to the same
snapshot through the negotiated encoding and rejects malformed
extents; call hierarchy attributes nested calls to the enclosing
extent. A real stdio session reported both inner calls as outgoing
from outer, and the observer and occurrence corpora still pass.
This commit is contained in:
faumaray committed 2026-12-05 17:28:59 +03:00
1 parent a098e45dc9
commit ec86a79a1a
11 files changed
+239 -11

No files matched your search

+17 -1
View File
@@ -61,7 +61,9 @@ def compile(compiler, observer, work, main, flags=()):
def read_span(span, name, label):
check(isinstance(span, dict) and set(span) == {"path", "line", "startByte", "endByte"},
check(isinstance(span, dict)
and set(span) in ({"path", "line", "startByte", "endByte"},
{"path", "line", "startByte", "endByte", "extent"}),
f"invalid {label} span {span!r}")
check(all(type(span[key]) is int for key in ("line", "startByte", "endByte")),
f"non-integer {label} span {span!r}")
@@ -76,6 +78,20 @@ def read_span(span, name, label):
f"{label} {name!r} does not occupy {span}: {raw!r}")
raw[:start - 1].decode("utf-8")
raw[:end - 1].decode("utf-8")
if "extent" in span:
extent = span["extent"]
check(isinstance(extent, dict)
and set(extent) == {"startLine", "startByte", "endLine", "endByte"}
and all(type(value) is int for value in extent.values()),
f"invalid {label} extent {extent!r}")
check(extent["startLine"] == span["line"] and extent["startByte"] == start
and (extent["endLine"], extent["endByte"]) >= (extent["startLine"], start),
f"{label} extent does not start at its span: {extent} vs {span}")
check(1 <= extent["endLine"] <= len(lines),
f"{label} extent line outside {path}: {extent}")
last = lines[extent["endLine"] - 1]
check(1 <= extent["endByte"] <= len(last) + 1,
f"{label} extent bytes outside line: {extent}")
return (path, span["line"], start, end)
+11
View File
@@ -124,3 +124,14 @@ VOID_TEST(CallHierarchy, outgoing_skips_unresolved_callees_and_bounds_groups) {
require(outgoing(calls, known, known.front()).size() == 1);
expect(outgoing(calls, known, known.front(), Limits{.callers = 0}).empty());
}
VOID_TEST(CallHierarchy, a_compiler_extent_identifies_the_same_function) {
const auto target = definition("f", "/p/a.cpp", range(1));
const auto caller_name = definition("outer", "/p/a.cpp", range(0, 4, 5));
const std::vector<Definition> known{definition("outer", "/p/a.cpp", {{0, 4}, {9, 1}}), target};
const std::vector<Call> calls{call("f", target, "/p/a.cpp", range(5))};
require(incoming(calls, known, target).size() == 1);
const auto result = outgoing(calls, known, caller_name);
require(result.size() == 1);
expect(result.front().to.name == "f");
}
+21
View File
@@ -186,3 +186,24 @@ VOID_TEST(Occurrences, rejects_a_malformed_envelope) {
require(!decode(extra, entries).has_value());
require(!decode("{\"schema\":1}", entries).has_value());
}
VOID_TEST(Occurrences, binds_a_multiline_definition_extent) {
auto entries = capture(std::string{source});
const auto call = span(source, "f", 1), declaration = span(source, "f", 0);
const auto definition = span_json(path, declaration);
const auto with_extent = definition.substr(0, definition.size() - 1) +
R"(,"extent":{"startLine":1,"startByte":5,"endLine":1,"endByte":21}})";
auto decoded =
decode(report(occurrence("f", "int", span_json(path, call), with_extent)), entries);
require(decoded.has_value());
require(decoded->occurrences.size() == 1);
const auto &fact = decoded->occurrences.front();
require(fact.extent.has_value());
expect(fact.extent->start.line == 0 && fact.extent->start.character == 4);
expect(fact.extent->end.line == 0 && fact.extent->end.character == 20);
// A malformed extent rejects the report rather than being dropped.
const auto bad = definition.substr(0, definition.size() - 1) +
R"(,"extent":{"startLine":2,"startByte":5,"endLine":1,"endByte":21}})";
expect(
!decode(report(occurrence("f", "int", span_json(path, call), bad)), entries).has_value());
}