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

+69 -1
View File
@@ -59,7 +59,9 @@ struct RawSpan {
std::optional<RawSpan> span(const Json *value) {
const auto *object = value ? P::as_object(value) : nullptr;
if (!object || object->size() != 4)
if (!object || (object->size() != 4 && object->size() != 5))
return {};
if (object->size() == 5 && !P::find_member(*object, "extent"))
return {};
const auto *path = P::as_string(P::find_member(*object, "path"));
const auto line = integer(P::find_member(*object, "line"));
@@ -73,6 +75,31 @@ std::optional<RawSpan> span(const Json *value) {
static_cast<std::uint32_t>(*end)};
}
struct RawExtent {
std::uint32_t start_line{}, start_byte{}, end_line{}, end_byte{};
};
std::optional<RawExtent> extent(const Json *definition) {
const auto *object = definition ? P::as_object(definition) : nullptr;
const auto *value = object ? P::find_member(*object, "extent") : nullptr;
const auto *fields = value ? P::as_object(value) : nullptr;
if (!fields || fields->size() != 4)
return {};
const auto start_line = integer(P::find_member(*fields, "startLine"));
const auto start_byte = integer(P::find_member(*fields, "startByte"));
const auto end_line = integer(P::find_member(*fields, "endLine"));
const auto end_byte = integer(P::find_member(*fields, "endByte"));
constexpr auto maximum = std::numeric_limits<std::int32_t>::max();
if (!start_line || !start_byte || !end_line || !end_byte || *start_line < 1 ||
*start_line > maximum || *end_line < *start_line || *end_line > maximum ||
*start_byte < 1 || *start_byte > maximum || *end_byte < 1 || *end_byte > maximum ||
(*end_line == *start_line && *end_byte <= *start_byte))
return {};
return RawExtent{static_cast<std::uint32_t>(*start_line),
static_cast<std::uint32_t>(*start_byte), static_cast<std::uint32_t>(*end_line),
static_cast<std::uint32_t>(*end_byte)};
}
bool captured(std::span<const CapturedSource> capture, const std::string &path) {
return std::ranges::any_of(capture, [&](const auto &entry) {
return entry.snapshot && entry.logical_path.native() == path;
@@ -118,6 +145,39 @@ bind_span(const RawSpan &raw, std::string_view name, std::span<const CapturedSou
}
return {};
}
// Binds a multi-line physical byte extent to the snapshot's negotiated encoding.
std::optional<Documents::Range> bind_extent(const RawExtent &raw,
const Documents::DocumentSnapshot &snapshot) {
const std::string_view text = snapshot.text;
const auto offset = [&](std::uint32_t line, std::uint32_t byte) -> std::optional<std::size_t> {
std::size_t line_start = 0;
for (std::uint32_t current = 1; current != line; ++current) {
const auto newline = text.find('\n', line_start);
if (newline == std::string_view::npos)
return {};
line_start = newline + 1;
}
auto line_end = text.find('\n', line_start);
if (line_end == std::string_view::npos)
line_end = text.size();
if (byte - 1 > line_end - line_start)
return {};
const std::size_t result = line_start + byte - 1;
if (result < text.size() && (static_cast<unsigned char>(text[result]) & 0xC0) == 0x80)
return {};
return result;
};
const auto begin = offset(raw.start_line, raw.start_byte);
const auto end = offset(raw.end_line, raw.end_byte);
if (!begin || !end || *end < *begin)
return {};
const auto start = snapshot.offset_to_position(*begin);
const auto finish = snapshot.offset_to_position(*end);
if (!start || !finish || !(*start < *finish))
return {};
return Documents::Range{*start, *finish};
}
} // namespace
std::expected<OccurrenceReport, OccurrenceError>
@@ -186,6 +246,14 @@ decode_occurrences(std::string_view report, std::string_view nonce,
return std::unexpected{OccurrenceError::invalid_report};
occurrence.definition_file = bound->first;
occurrence.definition = bound->second;
if (P::find_member(*P::as_object(definition), "extent")) {
const auto raw_extent = extent(definition);
if (!raw_extent)
return std::unexpected{OccurrenceError::invalid_report};
occurrence.extent = bind_extent(*raw_extent, *bound->first);
if (!occurrence.extent)
return std::unexpected{OccurrenceError::invalid_report};
}
}
}
result.occurrences.push_back(std::move(occurrence));