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.
264 lines
12 KiB
C++
264 lines
12 KiB
C++
#include <vls/semantic/Occurrences.hpp>
|
|
|
|
#include <Void/LanguageServer/Protocol/Json.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <optional>
|
|
#include <set>
|
|
#include <tuple>
|
|
|
|
namespace vls::semantic {
|
|
namespace P = Void::LanguageServer::Protocol;
|
|
using Json = P::Json;
|
|
|
|
namespace {
|
|
constexpr std::size_t max_report = 1024 * 1024;
|
|
constexpr std::size_t max_occurrences = 256;
|
|
constexpr std::size_t max_name = 64;
|
|
constexpr std::size_t max_type = 160;
|
|
constexpr std::size_t max_signature = 300;
|
|
constexpr std::size_t max_reason = 128;
|
|
constexpr std::size_t max_path = 4095;
|
|
|
|
bool plain(std::string_view text, std::size_t limit) {
|
|
return text.size() <= limit &&
|
|
std::ranges::none_of(text, [](unsigned char c) { return c < 32 || c == 127; });
|
|
}
|
|
|
|
bool identifier(std::string_view name) {
|
|
if (name.empty() || name.size() > max_name)
|
|
return false;
|
|
for (std::size_t i = 0; i != name.size(); ++i) {
|
|
const char c = name[i];
|
|
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' ||
|
|
(i && c >= '0' && c <= '9')))
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
const Json *member(const Json &value, std::string_view key) {
|
|
const auto *object = P::as_object(&value);
|
|
return object ? P::find_member(*object, key) : nullptr;
|
|
}
|
|
|
|
std::optional<std::int64_t> integer(const Json *value) {
|
|
if (!value)
|
|
return {};
|
|
if (const auto *number = std::get_if<std::int64_t>(&value->value))
|
|
return *number;
|
|
return {};
|
|
}
|
|
|
|
struct RawSpan {
|
|
std::string path;
|
|
std::uint32_t line, start, end;
|
|
};
|
|
|
|
std::optional<RawSpan> span(const Json *value) {
|
|
const auto *object = value ? P::as_object(value) : nullptr;
|
|
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"));
|
|
const auto start = integer(P::find_member(*object, "startByte"));
|
|
const auto end = integer(P::find_member(*object, "endByte"));
|
|
constexpr auto maximum = std::numeric_limits<std::int32_t>::max();
|
|
if (!path || !plain(*path, max_path) || path->find('\0') != path->npos || !line || !start ||
|
|
!end || *line < 1 || *line > maximum || *start < 1 || *end <= *start || *end > maximum)
|
|
return {};
|
|
return RawSpan{*path, static_cast<std::uint32_t>(*line), static_cast<std::uint32_t>(*start),
|
|
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;
|
|
});
|
|
}
|
|
|
|
// Bind a physical byte span to the captured snapshot and convert it through the
|
|
// snapshot's negotiated encoding. The sliced bytes must be exactly the declared
|
|
// name and both endpoints must fall on UTF-8 boundaries.
|
|
std::optional<std::pair<DiagnosticSnapshot, Documents::Range>>
|
|
bind_span(const RawSpan &raw, std::string_view name, std::span<const CapturedSource> capture) {
|
|
for (const auto &entry : capture) {
|
|
if (!entry.snapshot || entry.logical_path.native() != raw.path)
|
|
continue;
|
|
const auto &snapshot = entry.snapshot;
|
|
const std::string_view text = snapshot->text;
|
|
std::size_t line_start = 0;
|
|
for (std::uint32_t line = 1; line != raw.line; ++line) {
|
|
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();
|
|
const std::size_t width = line_end - line_start;
|
|
if (raw.start - 1 > width || raw.end - 1 > width)
|
|
return {};
|
|
const std::size_t begin = line_start + raw.start - 1;
|
|
const std::size_t end = line_start + raw.end - 1;
|
|
if (text.substr(begin, name.size()) != name)
|
|
return {};
|
|
if (begin && (static_cast<unsigned char>(text[begin - 1]) & 0xC0) == 0x80)
|
|
return {};
|
|
if (end < text.size() && (static_cast<unsigned char>(text[end]) & 0xC0) == 0x80)
|
|
return {};
|
|
const auto start = snapshot->offset_to_position(begin);
|
|
const auto finish = snapshot->offset_to_position(end);
|
|
if (!start || !finish || !(*start < *finish))
|
|
return {};
|
|
return std::pair{snapshot, Documents::Range{*start, *finish}};
|
|
}
|
|
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>
|
|
decode_occurrences(std::string_view report, std::string_view nonce,
|
|
std::span<const CapturedSource> capture) {
|
|
if (report.empty() || report.size() > max_report)
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
auto parsed = P::parse_json(report, 8);
|
|
if (!parsed)
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
const auto *root = P::as_object(&*parsed);
|
|
if (!root || root->size() != 6)
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
const auto schema = integer(P::find_member(*root, "schema"));
|
|
const auto *nonce_text = P::as_string(P::find_member(*root, "nonce"));
|
|
const auto complete = P::as_bool(P::find_member(*root, "complete"));
|
|
const auto partial = P::as_bool(P::find_member(*root, "partial"));
|
|
const auto *reason = P::as_string(P::find_member(*root, "reason"));
|
|
const auto *items = P::as_array(P::find_member(*root, "occurrences"));
|
|
if (!schema || *schema != 1 || !nonce_text || !complete || !partial || !reason ||
|
|
!plain(*reason, max_reason) || !items || items->size() > max_occurrences)
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
if (*nonce_text != nonce)
|
|
return std::unexpected{OccurrenceError::wrong_nonce};
|
|
OccurrenceReport result{{}, *complete, *partial, *reason};
|
|
if (!*complete)
|
|
return result;
|
|
std::set<std::tuple<const void *, std::uint32_t, std::uint32_t>> uses;
|
|
for (const auto &item : *items) {
|
|
const auto *fields = P::as_object(&item);
|
|
if (!fields || (fields->size() != 5 && fields->size() != 6))
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
const auto *name = P::as_string(P::find_member(*fields, "name"));
|
|
const auto *type = P::as_string(P::find_member(*fields, "type"));
|
|
const auto *canonical = P::as_string(P::find_member(*fields, "canonicalType"));
|
|
const auto *signature = P::as_string(P::find_member(*fields, "signature"));
|
|
if (!name || !identifier(*name) || !type || !plain(*type, max_type) || type->empty() ||
|
|
!canonical || !plain(*canonical, max_type) || canonical->empty() ||
|
|
(signature && (!plain(*signature, max_signature) || signature->empty())))
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
const auto use_span = span(P::find_member(*fields, "use"));
|
|
if (!use_span)
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
auto use = bind_span(*use_span, *name, capture);
|
|
if (!use)
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
if (!uses.emplace(use->first.get(), use->second.start.line, use->second.start.character)
|
|
.second)
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
Occurrence occurrence;
|
|
occurrence.name = *name;
|
|
occurrence.type = *type;
|
|
occurrence.canonical_type = *canonical;
|
|
if (signature)
|
|
occurrence.signature = *signature;
|
|
occurrence.use_file = use->first;
|
|
occurrence.use = use->second;
|
|
const auto *definition = P::find_member(*fields, "definition");
|
|
if (definition && !std::holds_alternative<std::nullptr_t>(definition->value)) {
|
|
const auto definition_span = span(definition);
|
|
if (!definition_span)
|
|
return std::unexpected{OccurrenceError::invalid_report};
|
|
if (captured(capture, definition_span->path)) {
|
|
auto bound = bind_span(*definition_span, *name, capture);
|
|
if (!bound)
|
|
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));
|
|
}
|
|
return result;
|
|
}
|
|
} // namespace vls::semantic
|