Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite: protocol and document sync, VBS/compdb providers, stock GCC diagnostics (including in-process default profile preparation), the stock occurrence observer and hover/definition, the bounded Scheduler, C++ module graph, scan and artifact cache with real CMI production, the context-aware symbol index with schema-2 snapshot migration, navigation and guarded rename, reflection provenance with splice-origin extraction, index-backed completion and signature help, live GCC worker control with a registered DAP suite, and the Neovim and VS Code clients. VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced from the versioned patches and verified stepping reflection/template code with projected values 17/34 and 41/82.
This commit is contained in:
1 parent
ca04975d54
commit
da8003f6d4
310 files changed
+47552
-480
No files matched your search
@@ -0,0 +1,399 @@
|
||||
#include <vls/semantic/Diagnostics.hpp>
|
||||
|
||||
#include <Void/LanguageServer/Protocol/Json.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
|
||||
namespace vls::semantic {
|
||||
namespace {
|
||||
namespace P = Void::LanguageServer::Protocol;
|
||||
using Json = P::Json;
|
||||
using Object = Json::Object;
|
||||
using Error = DiagnosticError;
|
||||
template <class T> using Result = std::expected<T, Error>;
|
||||
|
||||
constexpr std::size_t max_report_bytes = 4 * 1024 * 1024;
|
||||
constexpr std::size_t max_artifacts = 2048, max_results = 4096, max_locations = 16384;
|
||||
constexpr std::size_t max_name = 16384, max_message = 65536;
|
||||
constexpr std::string_view bom = "\xef\xbb\xbf";
|
||||
|
||||
// Cap container allocation in Void's JSON value tree before parsing. Brackets
|
||||
// and commas in embedded source/diagnostic strings are ordinary content.
|
||||
bool within_structure_limit(std::string_view bytes) {
|
||||
std::size_t remaining = 128 * 1024;
|
||||
bool quoted = false, escaped = false;
|
||||
for (const char c : bytes) {
|
||||
if (quoted) {
|
||||
if (escaped)
|
||||
escaped = false;
|
||||
else if (c == '\\')
|
||||
escaped = true;
|
||||
else if (c == '"')
|
||||
quoted = false;
|
||||
} else if (c == '"') {
|
||||
quoted = true;
|
||||
} else if (c == '[' || c == '{' || c == ',' || c == ':') {
|
||||
if (remaining == 0)
|
||||
return false;
|
||||
--remaining;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const Json *member(const Object *obj, std::string_view key) {
|
||||
return obj ? P::find_member(*obj, key) : nullptr;
|
||||
}
|
||||
const std::string *string(const Object *obj, std::string_view key) {
|
||||
return P::as_string(member(obj, key));
|
||||
}
|
||||
const Object *object(const Object *obj, std::string_view key) {
|
||||
return P::as_object(member(obj, key));
|
||||
}
|
||||
const Object *only_object(const Object *obj, std::string_view key) {
|
||||
const auto *list = P::as_array(member(obj, key));
|
||||
return list && list->size() == 1 ? P::as_object(&list->front()) : nullptr;
|
||||
}
|
||||
bool equals(const Object *obj, std::string_view key, std::string_view value) {
|
||||
const auto *text = string(obj, key);
|
||||
return text && *text == value;
|
||||
}
|
||||
std::optional<std::uint32_t> natural(const Json *value) {
|
||||
if (!value)
|
||||
return {};
|
||||
const auto *integer = std::get_if<std::int64_t>(&value->value);
|
||||
if (!integer || *integer < 0 || *integer > std::numeric_limits<std::int32_t>::max())
|
||||
return {};
|
||||
return static_cast<std::uint32_t>(*integer);
|
||||
}
|
||||
|
||||
std::optional<ArtifactName> artifact_name(const Object *record, const Object *bases) {
|
||||
const auto *uri = string(record, "uri");
|
||||
if (!uri || uri->empty() || uri->size() > max_name)
|
||||
return {};
|
||||
const auto *base_id = member(record, "uriBaseId");
|
||||
if (!base_id)
|
||||
return ArtifactName{*uri, {}};
|
||||
const auto *id = P::as_string(base_id);
|
||||
const auto *base = id ? object(bases, *id) : nullptr;
|
||||
const auto *base_uri = string(base, "uri");
|
||||
if (!base_uri || base_uri->empty() || base_uri->size() > max_name || member(base, "uriBaseId"))
|
||||
return {};
|
||||
return ArtifactName{*uri, *base_uri};
|
||||
}
|
||||
|
||||
struct BoundArtifact {
|
||||
const DiagnosticArtifact *file{};
|
||||
bool contents_match{};
|
||||
};
|
||||
|
||||
struct Reader {
|
||||
explicit Reader(const DiagnosticJob &source_job) : job(source_job) {}
|
||||
const DiagnosticJob &job;
|
||||
const Object *bases{};
|
||||
std::map<ArtifactName, const DiagnosticArtifact *> bindings;
|
||||
std::map<ArtifactName, std::size_t> listed;
|
||||
std::vector<BoundArtifact> by_index;
|
||||
std::size_t positions_left{max_locations}, column_work{32 * 1024 * 1024};
|
||||
|
||||
Result<bool> bind(const Object *run) {
|
||||
if (job.artifacts.size() > max_artifacts)
|
||||
return std::unexpected(Error::resource_limit);
|
||||
std::size_t names_left = 2 * 1024 * 1024;
|
||||
for (const auto &file : job.artifacts) {
|
||||
if (!file.snapshot || file.name.uri.empty() || file.name.uri.size() > max_name ||
|
||||
file.name.base_uri.size() > max_name)
|
||||
return std::unexpected(Error::invalid_job);
|
||||
const auto cost = file.name.uri.size() + file.name.base_uri.size();
|
||||
if (cost > names_left)
|
||||
return std::unexpected(Error::resource_limit);
|
||||
names_left -= cost;
|
||||
if (!bindings.emplace(file.name, &file).second)
|
||||
return std::unexpected(Error::invalid_job);
|
||||
}
|
||||
bases = object(run, "originalUriBaseIds");
|
||||
if (member(run, "originalUriBaseIds") && !bases)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
const auto *files = P::as_array(member(run, "artifacts"));
|
||||
if (!files) {
|
||||
if (member(run, "artifacts"))
|
||||
return std::unexpected(Error::malformed_report);
|
||||
return false;
|
||||
}
|
||||
if (files->size() > max_artifacts)
|
||||
return std::unexpected(Error::resource_limit);
|
||||
bool found_owner = false;
|
||||
for (const auto &item : *files) {
|
||||
const auto *record = P::as_object(&item);
|
||||
const auto *location = object(record, "location");
|
||||
if (const auto *self_index = member(location, "index")) {
|
||||
const auto index = natural(self_index);
|
||||
if (!index || *index != by_index.size())
|
||||
return std::unexpected(Error::malformed_report);
|
||||
}
|
||||
const auto name = artifact_name(location, bases);
|
||||
if (!name || !listed.emplace(*name, by_index.size()).second)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
auto binding = bindings.find(*name);
|
||||
auto *file = binding == bindings.end() ? nullptr : binding->second;
|
||||
bool matches = true;
|
||||
if (member(record, "contents")) {
|
||||
const auto *bytes = string(object(record, "contents"), "text");
|
||||
if (file) {
|
||||
const std::string_view input = file->snapshot->text;
|
||||
matches = bytes && (*bytes == input ||
|
||||
(input.starts_with(bom) && *bytes == input.substr(3)));
|
||||
}
|
||||
}
|
||||
by_index.push_back({file, matches});
|
||||
if (!member(record, "roles"))
|
||||
continue;
|
||||
const auto *roles = P::as_array(member(record, "roles"));
|
||||
if (!roles)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
for (const auto &role : *roles) {
|
||||
const auto *text = P::as_string(&role);
|
||||
if (!text)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
if (*text == "analysisTarget") {
|
||||
if (found_owner || !file || file->snapshot != job.owner || !matches)
|
||||
return std::unexpected(Error::wrong_owner);
|
||||
found_owner = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return found_owner;
|
||||
}
|
||||
|
||||
std::optional<BoundArtifact> binding(const Object *location) const {
|
||||
const auto *index = member(location, "index");
|
||||
std::optional<std::uint32_t> reference;
|
||||
if (index) {
|
||||
reference = natural(index);
|
||||
if (!reference || *reference >= by_index.size())
|
||||
return {};
|
||||
}
|
||||
if (member(location, "uri")) {
|
||||
auto name = artifact_name(location, bases);
|
||||
if (!name)
|
||||
return {};
|
||||
const auto found = listed.find(*name);
|
||||
if (found == listed.end() || (reference && *reference != found->second))
|
||||
return {};
|
||||
return by_index[found->second];
|
||||
}
|
||||
if (member(location, "uriBaseId") || !reference)
|
||||
return {};
|
||||
return by_index[*reference];
|
||||
}
|
||||
|
||||
Result<std::optional<Documents::Position>> position(const Documents::DocumentSnapshot &snapshot,
|
||||
std::uint32_t line, std::uint32_t column,
|
||||
CoordinatePolicy policy) {
|
||||
const auto start = snapshot.line_index.line_start_offset(line - 1);
|
||||
const auto end = snapshot.line_index.line_content_end_offset(snapshot.text, line - 1);
|
||||
if (!start || !end || *end < *start || *end > snapshot.text.size())
|
||||
return {};
|
||||
auto text = std::string_view{snapshot.text}.substr(*start, *end - *start);
|
||||
std::size_t offset = 0;
|
||||
if (line == 1 && text.starts_with(bom)) {
|
||||
if (policy.bom == FirstLineBom::unknown)
|
||||
return {};
|
||||
if (policy.bom == FirstLineBom::skipped)
|
||||
offset = bom.size();
|
||||
}
|
||||
const std::size_t target = column - 1;
|
||||
// Also charge the subsequent UTF-16 conversion, which can scan the line prefix.
|
||||
const auto charge = 2 * (std::min(target, text.size()) + offset + 1);
|
||||
if (charge > column_work)
|
||||
return std::unexpected(Error::resource_limit);
|
||||
column_work -= charge;
|
||||
if (policy.unit == ColumnUnit::utf8_bytes) {
|
||||
if (target > text.size() - offset)
|
||||
return {};
|
||||
offset += target;
|
||||
} else {
|
||||
std::size_t display = 0;
|
||||
while (display < target && offset < text.size()) {
|
||||
const auto ch = static_cast<unsigned char>(text[offset++]);
|
||||
if (ch == '\t' && policy.unit == ColumnUnit::gcc_display && policy.tabstop > 0 &&
|
||||
policy.tabstop <= 100) {
|
||||
display += policy.tabstop - display % policy.tabstop;
|
||||
} else if (ch >= 0x20 && ch <= 0x7e) {
|
||||
++display;
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
if (display != target)
|
||||
return {};
|
||||
// The prefix advanced strictly, but a following non-ASCII scalar
|
||||
// can share this display column. Require a provable boundary.
|
||||
if (offset < text.size() && static_cast<unsigned char>(text[offset]) >= 0x80)
|
||||
return {};
|
||||
}
|
||||
auto pos = snapshot.offset_to_position(*start + offset);
|
||||
if (!pos)
|
||||
return {};
|
||||
return *pos;
|
||||
}
|
||||
|
||||
Result<DiagnosticLocation> locate(const Object *loc, std::size_t result_index,
|
||||
std::size_t location_index, bool related) {
|
||||
if (positions_left == 0)
|
||||
return std::unexpected(Error::resource_limit);
|
||||
--positions_left;
|
||||
const auto *physical = object(loc, "physicalLocation");
|
||||
if (!physical)
|
||||
return DiagnosticLocation{};
|
||||
const auto file = binding(object(physical, "artifactLocation"));
|
||||
if (!file || !file->file)
|
||||
return DiagnosticLocation{LocationGap::unknown_artifact};
|
||||
if (!file->contents_match)
|
||||
return DiagnosticLocation{LocationGap::artifact_mismatch};
|
||||
const auto *region = object(physical, "region");
|
||||
auto first = natural(member(region, "startLine")),
|
||||
left = natural(member(region, "startColumn"));
|
||||
auto last = member(region, "endLine") ? natural(member(region, "endLine")) : first;
|
||||
auto right = natural(member(region, "endColumn"));
|
||||
if (!first || !left || !last || !right || !*first || !*left || !*last || !*right ||
|
||||
*last < *first || (*last == *first && *right < *left))
|
||||
return DiagnosticLocation{LocationGap::incomplete_region};
|
||||
LocationRequest request{
|
||||
*file->file, {*first, *left, *last, *right}, result_index, location_index, related};
|
||||
const auto policy = job.physical_origin ? job.physical_origin(request) : std::nullopt;
|
||||
if (!policy)
|
||||
return DiagnosticLocation{LocationGap::unproven_origin};
|
||||
const auto &snapshot = file->file->snapshot;
|
||||
auto start = position(*snapshot, *first, *left, *policy);
|
||||
if (!start)
|
||||
return std::unexpected(start.error());
|
||||
auto end = position(*snapshot, *last, *right, *policy);
|
||||
if (!end)
|
||||
return std::unexpected(end.error());
|
||||
if (!*start || !*end || **end < **start)
|
||||
return DiagnosticLocation{LocationGap::ambiguous_column};
|
||||
return DiagnosticLocation{LocationGap::none, snapshot, Documents::Range{**start, **end}};
|
||||
}
|
||||
};
|
||||
|
||||
std::optional<DiagnosticSeverity> severity(const Object *record) {
|
||||
if (equals(record, "level", "error"))
|
||||
return DiagnosticSeverity::error;
|
||||
if (equals(record, "level", "warning"))
|
||||
return DiagnosticSeverity::warning;
|
||||
if (equals(record, "level", "note"))
|
||||
return DiagnosticSeverity::information;
|
||||
if (equals(record, "level", "none"))
|
||||
return DiagnosticSeverity::hint;
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> append_locations(Reader &reader, const Json *value, std::size_t index, bool related,
|
||||
CompilerDiagnostic &diagnostic) {
|
||||
if (!value)
|
||||
return {};
|
||||
const auto *locations = P::as_array(value);
|
||||
if (!locations)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
for (std::size_t i = 0; i != locations->size(); ++i) {
|
||||
const auto *record = P::as_object(&(*locations)[i]);
|
||||
if (!record)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
auto location = reader.locate(record, index, i, related);
|
||||
if (!location)
|
||||
return std::unexpected(location.error());
|
||||
if (!related && i == 0) {
|
||||
diagnostic.location = std::move(*location);
|
||||
} else {
|
||||
const auto *message = string(object(record, "message"), "text");
|
||||
if (message && message->size() > max_message)
|
||||
return std::unexpected(Error::resource_limit);
|
||||
diagnostic.related.push_back(
|
||||
{message ? *message : std::string{}, std::move(*location)});
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<DiagnosticBatch, DiagnosticError>
|
||||
decode_gcc_diagnostics(const DiagnosticJob &job, const DiagnosticOutput &output) {
|
||||
if (!job.owner || job.invocation_id.empty() || job.input_view_id.empty() ||
|
||||
job.invocation_id.size() > max_name || job.input_view_id.size() > max_name)
|
||||
return std::unexpected(Error::invalid_job);
|
||||
if (job.invocation_id != output.invocation_id || job.input_view_id != output.input_view_id)
|
||||
return std::unexpected(Error::wrong_capture);
|
||||
if (!output.stream_complete || output.sarif.empty())
|
||||
return std::unexpected(Error::incomplete_stream);
|
||||
if (!output.exit_code || (*output.exit_code != 0 && *output.exit_code != 1))
|
||||
return std::unexpected(Error::failed_process);
|
||||
if (output.sarif.size() > max_report_bytes || !within_structure_limit(output.sarif))
|
||||
return std::unexpected(Error::resource_limit);
|
||||
auto report = P::parse_json(output.sarif, 40);
|
||||
if (!report)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
const auto *root = P::as_object(&*report);
|
||||
const auto *run = only_object(root, "runs");
|
||||
const auto *driver = object(object(run, "tool"), "driver");
|
||||
if (!equals(root, "version", "2.1.0") || !equals(driver, "name", "GNU C++26") ||
|
||||
!equals(driver, "version", "16.2.0") || member(run, "columnKind"))
|
||||
return std::unexpected(Error::unsupported_report);
|
||||
const auto *invocation = only_object(run, "invocations");
|
||||
const auto success = P::as_bool(member(invocation, "executionSuccessful"));
|
||||
const auto *notifications = P::as_array(member(invocation, "toolExecutionNotifications"));
|
||||
if (!success || *success != (*output.exit_code == 0) || !notifications)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
if (!notifications->empty())
|
||||
return std::unexpected(Error::tool_failure);
|
||||
Reader reader{job};
|
||||
const auto owner_matched = reader.bind(run);
|
||||
if (!owner_matched)
|
||||
return std::unexpected(owner_matched.error());
|
||||
if (*success && !*owner_matched)
|
||||
return std::unexpected(Error::wrong_owner);
|
||||
// GCC can fail before opening its main file. Keep the invocation's message
|
||||
// but do not bind positions to an unconfirmed translation unit.
|
||||
if (!*owner_matched) {
|
||||
reader.by_index.clear();
|
||||
reader.listed.clear();
|
||||
}
|
||||
const auto *results = P::as_array(member(run, "results"));
|
||||
if (!results)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
if (results->size() > max_results)
|
||||
return std::unexpected(Error::resource_limit);
|
||||
DiagnosticBatch batch{
|
||||
job.invocation_id, job.input_view_id, job.owner, {}, job.inputs_complete && *owner_matched};
|
||||
bool source_error = false;
|
||||
for (std::size_t i = 0; i != results->size(); ++i) {
|
||||
const auto *record = P::as_object(&(*results)[i]);
|
||||
const auto level = severity(record);
|
||||
const auto *message = string(object(record, "message"), "text");
|
||||
const auto *rule = string(record, "ruleId");
|
||||
if (!level || !message || !rule || message->empty())
|
||||
return std::unexpected(Error::malformed_report);
|
||||
if (message->size() > max_message || rule->size() > max_name)
|
||||
return std::unexpected(Error::resource_limit);
|
||||
CompilerDiagnostic diagnostic{*level, *message, *rule, {}, {}};
|
||||
if (auto primary =
|
||||
append_locations(reader, member(record, "locations"), i, false, diagnostic);
|
||||
!primary)
|
||||
return std::unexpected(primary.error());
|
||||
if (auto related =
|
||||
append_locations(reader, member(record, "relatedLocations"), i, true, diagnostic);
|
||||
!related)
|
||||
return std::unexpected(related.error());
|
||||
source_error |= *level == DiagnosticSeverity::error;
|
||||
if (!diagnostic.location.range || *rule == "fatal error" ||
|
||||
*rule == "internal compiler error")
|
||||
batch.complete_for_owner = false;
|
||||
batch.diagnostics.push_back(std::move(diagnostic));
|
||||
}
|
||||
if (source_error == *success)
|
||||
return std::unexpected(Error::malformed_report);
|
||||
return batch;
|
||||
}
|
||||
} // namespace vls::semantic
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,195 @@
|
||||
#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)
|
||||
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)};
|
||||
}
|
||||
|
||||
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 {};
|
||||
}
|
||||
} // 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;
|
||||
}
|
||||
}
|
||||
result.occurrences.push_back(std::move(occurrence));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} // namespace vls::semantic
|
||||
@@ -0,0 +1,483 @@
|
||||
#include <vls/semantic/SourceCatalog.hpp>
|
||||
|
||||
#include <Void/LanguageServer/Support/RegularFile.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include <linux/openat2.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
namespace vls::semantic {
|
||||
namespace fs = std::filesystem;
|
||||
namespace File = Void::LanguageServer::Detail;
|
||||
using Void::System::OwnedFd;
|
||||
using Issue = SourceIssue;
|
||||
|
||||
// Shared by old and new grant generations. Historical readers keep their own reservations.
|
||||
struct SourceAccounting {
|
||||
std::mutex mutex;
|
||||
std::size_t handles{}, descriptors{}, bytes{};
|
||||
};
|
||||
|
||||
namespace {
|
||||
auto fail(Issue issue, const fs::path &path = {}) {
|
||||
return std::unexpected(SourceError{issue, path.native().size() <= 4095 ? path : fs::path{}});
|
||||
}
|
||||
|
||||
struct Reservation {
|
||||
std::shared_ptr<SourceAccounting> account;
|
||||
std::size_t descriptors{}, bytes{};
|
||||
|
||||
explicit Reservation(std::shared_ptr<SourceAccounting> account) : account(std::move(account)) {}
|
||||
Reservation(const Reservation &) = delete;
|
||||
~Reservation() {
|
||||
std::lock_guard lock{account->mutex};
|
||||
--account->handles;
|
||||
account->descriptors -= descriptors;
|
||||
account->bytes -= bytes;
|
||||
}
|
||||
bool claim(std::size_t fds, std::size_t size) {
|
||||
std::lock_guard lock{account->mutex};
|
||||
if (fds > 512 - account->descriptors || size > 192 * 1024 * 1024 - account->bytes)
|
||||
return false;
|
||||
account->descriptors += fds;
|
||||
account->bytes += size;
|
||||
descriptors += fds;
|
||||
bytes += size;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
std::shared_ptr<Reservation> reserve(const std::shared_ptr<SourceAccounting> &account) {
|
||||
std::lock_guard lock{account->mutex};
|
||||
if (account->handles >= 64)
|
||||
return {};
|
||||
auto reservation = std::make_shared<Reservation>(account);
|
||||
++account->handles;
|
||||
return reservation;
|
||||
}
|
||||
|
||||
bool direct_name(const fs::path &path) {
|
||||
return File::valid_path(path) && path.is_absolute() &&
|
||||
path.native() == path.lexically_normal().native() &&
|
||||
(path == path.root_path() || path.has_filename()) &&
|
||||
std::distance(path.begin(), path.end()) <= 64;
|
||||
}
|
||||
|
||||
bool below(const fs::path &path, const fs::path &root) {
|
||||
auto [r, p] = std::mismatch(root.begin(), root.end(), path.begin(), path.end());
|
||||
return r == root.end() && p != path.end();
|
||||
}
|
||||
|
||||
struct Pin {
|
||||
OwnedFd fd;
|
||||
struct stat state;
|
||||
};
|
||||
using Directory = std::shared_ptr<const Pin>;
|
||||
|
||||
bool same_object(const struct stat &a, const struct stat &b) {
|
||||
return a.st_dev == b.st_dev && a.st_ino == b.st_ino;
|
||||
}
|
||||
|
||||
OwnedFd open_directory(const fs::path &path) {
|
||||
open_how how{};
|
||||
how.flags = O_PATH | O_CLOEXEC | O_DIRECTORY;
|
||||
how.resolve = RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS;
|
||||
return OwnedFd{
|
||||
static_cast<int>(::syscall(SYS_openat2, AT_FDCWD, path.c_str(), &how, sizeof how))};
|
||||
}
|
||||
|
||||
std::expected<Directory, SourceError> pin_directory(const fs::path &path,
|
||||
Reservation &reservation) {
|
||||
auto fd = open_directory(path);
|
||||
struct stat state{};
|
||||
if (!fd)
|
||||
return fail(errno == ELOOP || errno == ENOTDIR ? Issue::unsupported_route
|
||||
: Issue::unavailable,
|
||||
path);
|
||||
if (::fstat(fd.get(), &state) != 0)
|
||||
return fail(Issue::unavailable, path);
|
||||
if (!reservation.claim(1, path.native().size()))
|
||||
return fail(Issue::resource_limit, path);
|
||||
return std::make_shared<Pin>(std::move(fd), state);
|
||||
}
|
||||
|
||||
bool directory_current(const fs::path &path, const Pin &pin) {
|
||||
auto fd = open_directory(path);
|
||||
struct stat state{};
|
||||
return fd && ::fstat(fd.get(), &state) == 0 && same_object(pin.state, state);
|
||||
}
|
||||
|
||||
SourceIssue from_document(const Documents::DocumentError &error) {
|
||||
using K = Documents::DocumentError::Kind;
|
||||
switch (error.kind) {
|
||||
case K::resource_limit:
|
||||
return Issue::resource_limit;
|
||||
case K::stale_read:
|
||||
return Issue::changed;
|
||||
case K::invalid_utf8:
|
||||
return Issue::invalid_text;
|
||||
case K::invalid_uri:
|
||||
return Issue::invalid_path;
|
||||
default:
|
||||
return Issue::unavailable_overlay;
|
||||
}
|
||||
}
|
||||
|
||||
struct Leaf {
|
||||
Directory parent;
|
||||
fs::path name;
|
||||
OwnedFd object;
|
||||
struct stat state{};
|
||||
};
|
||||
|
||||
bool leaf_current(const Leaf &leaf) {
|
||||
struct stat state{};
|
||||
if (::fstatat(leaf.parent->fd.get(), leaf.name.c_str(), &state, AT_SYMLINK_NOFOLLOW) != 0)
|
||||
return !leaf.object && errno == ENOENT;
|
||||
return leaf.object && File::same_file_state(leaf.state, state);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
struct SourceGrant {
|
||||
SourceScope scope;
|
||||
std::shared_ptr<Reservation> reservation;
|
||||
Directory workspace;
|
||||
std::map<fs::path, Directory> roots;
|
||||
};
|
||||
|
||||
struct SourceEvidence {
|
||||
InstalledSourceGrant grant;
|
||||
std::shared_ptr<Reservation> reservation;
|
||||
Documents::FileOverlayGeneration membership;
|
||||
std::map<fs::path, Directory> directories;
|
||||
std::vector<std::pair<fs::path, Leaf>> leaves;
|
||||
std::vector<Documents::BoundSourceObservation> observations;
|
||||
std::atomic_bool expired{};
|
||||
|
||||
SourceEvidence(InstalledSourceGrant grant, std::shared_ptr<Reservation> reservation,
|
||||
Documents::FileOverlayGeneration membership)
|
||||
: grant(std::move(grant)), reservation(std::move(reservation)),
|
||||
membership(std::move(membership)) {}
|
||||
};
|
||||
|
||||
SourceCapture::SourceCapture(std::shared_ptr<SourceEvidence> evidence)
|
||||
: evidence_(std::move(evidence)) {}
|
||||
|
||||
SourceCatalog::SourceCatalog(const Documents::DocumentStore &documents,
|
||||
std::function<void()> before_capture_validation)
|
||||
: documents_(documents), accounting_(std::make_shared<SourceAccounting>()),
|
||||
before_capture_validation_(std::move(before_capture_validation)) {}
|
||||
|
||||
std::expected<InstalledSourceGrant, SourceError>
|
||||
SourceCatalog::install(const SourceScope &scope, std::span<const fs::path> directories) {
|
||||
std::lock_guard lock{installations_};
|
||||
installed_.store({});
|
||||
if (!direct_name(scope.workspace.root) || scope.workspace.session.empty() ||
|
||||
scope.workspace.session.size() > 4096 || scope.workspace.session.contains('\0'))
|
||||
return fail(Issue::invalid_path, scope.workspace.root);
|
||||
if (directories.empty() || directories.size() > 32)
|
||||
return fail(Issue::resource_limit);
|
||||
auto reservation = reserve(accounting_);
|
||||
if (!reservation)
|
||||
return fail(Issue::resource_limit);
|
||||
auto workspace = pin_directory(scope.workspace.root, *reservation);
|
||||
if (!workspace)
|
||||
return std::unexpected(workspace.error());
|
||||
auto grant = std::make_shared<SourceGrant>(scope, std::move(reservation), *workspace,
|
||||
std::map<fs::path, Directory>{});
|
||||
for (const auto &name : directories) {
|
||||
if (!direct_name(name))
|
||||
return fail(Issue::invalid_path, name);
|
||||
if (grant->roots.contains(name))
|
||||
continue;
|
||||
auto pin =
|
||||
name == scope.workspace.root ? workspace : pin_directory(name, *grant->reservation);
|
||||
if (!pin)
|
||||
return std::unexpected(pin.error());
|
||||
grant->roots.emplace(name, std::move(*pin));
|
||||
}
|
||||
installed_.store(grant);
|
||||
return grant;
|
||||
}
|
||||
|
||||
void SourceCatalog::revoke() {
|
||||
std::lock_guard lock{installations_};
|
||||
installed_.store({});
|
||||
}
|
||||
|
||||
namespace {
|
||||
using Roots = std::map<fs::path, Directory>;
|
||||
Roots::const_iterator owning_root(const Roots &roots, const fs::path &path) {
|
||||
auto owner = roots.end();
|
||||
for (auto root = roots.begin(); root != roots.end(); ++root)
|
||||
if (below(path, root->first) &&
|
||||
(owner == roots.end() || root->first.native().size() > owner->first.native().size()))
|
||||
owner = root;
|
||||
return owner;
|
||||
}
|
||||
|
||||
std::expected<void, SourceError> check_requests(const SourceGrant &grant,
|
||||
std::span<const SourceRequest> requests,
|
||||
const SourceLimits &limits) {
|
||||
if (requests.size() > std::min(limits.files, SourceLimits{}.files))
|
||||
return fail(Issue::resource_limit);
|
||||
auto remaining = std::min(limits.name_bytes, SourceLimits{}.name_bytes);
|
||||
std::set<fs::path> file_names;
|
||||
for (const auto &[path, allow_missing, lookup_only] : requests) {
|
||||
if (!direct_name(path) || !path.has_filename())
|
||||
return fail(Issue::invalid_path, path);
|
||||
if (path.native().size() > remaining)
|
||||
return fail(Issue::resource_limit, path);
|
||||
remaining -= path.native().size();
|
||||
if (owning_root(grant.roots, path) == grant.roots.end() &&
|
||||
!(lookup_only && grant.roots.contains(path)))
|
||||
return fail(Issue::outside_scope, path);
|
||||
if (!lookup_only)
|
||||
file_names.insert(path);
|
||||
}
|
||||
for (const auto &request : requests)
|
||||
for (auto p = request.path.parent_path(); p != p.root_path(); p = p.parent_path())
|
||||
if (file_names.contains(p))
|
||||
return fail(Issue::conflicting_names, request.path);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<Directory, SourceError> parent_directory(SourceEvidence &evidence,
|
||||
const fs::path &path, std::size_t max_fds) {
|
||||
const auto root = owning_root(evidence.grant->roots, path);
|
||||
auto name = root->first;
|
||||
auto current = root->second;
|
||||
if (!directory_current(name, *current))
|
||||
return fail(Issue::changed, name);
|
||||
evidence.directories.try_emplace(name, current);
|
||||
const auto relative = path.lexically_relative(name);
|
||||
for (const auto &part : relative.parent_path()) {
|
||||
name /= part;
|
||||
if (auto i = evidence.directories.find(name); i != evidence.directories.end()) {
|
||||
current = i->second;
|
||||
continue;
|
||||
}
|
||||
OwnedFd fd{::openat(current->fd.get(), part.c_str(), O_PATH | O_NOFOLLOW | O_CLOEXEC)};
|
||||
if (!fd)
|
||||
return fail(errno == ENOENT ? Issue::missing_parent : Issue::unavailable, name);
|
||||
struct stat state{};
|
||||
if (::fstat(fd.get(), &state) != 0)
|
||||
return fail(Issue::unavailable, name);
|
||||
if (!S_ISDIR(state.st_mode))
|
||||
return fail(Issue::unsupported_route, name);
|
||||
if (evidence.reservation->descriptors >= max_fds ||
|
||||
!evidence.reservation->claim(1, name.native().size()))
|
||||
return fail(Issue::resource_limit, name);
|
||||
current = std::make_shared<Pin>(std::move(fd), state);
|
||||
evidence.directories.emplace(name, current);
|
||||
}
|
||||
// A cached intermediate directory may have moved since the previous request.
|
||||
if (!directory_current(name, *current))
|
||||
return fail(Issue::changed, name);
|
||||
return current;
|
||||
}
|
||||
|
||||
std::expected<Leaf, SourceError> bind_leaf(Directory parent, const fs::path &path,
|
||||
Reservation &budget, std::size_t max_fds,
|
||||
bool allow_directory = false) {
|
||||
Leaf leaf{std::move(parent), path.filename(), {}};
|
||||
leaf.object = OwnedFd{
|
||||
::openat(leaf.parent->fd.get(), leaf.name.c_str(), O_PATH | O_NOFOLLOW | O_CLOEXEC)};
|
||||
if (!leaf.object) {
|
||||
if (errno == ENOENT)
|
||||
return leaf;
|
||||
return fail(Issue::unavailable, path);
|
||||
}
|
||||
if (::fstat(leaf.object.get(), &leaf.state) != 0)
|
||||
return fail(Issue::unavailable, path);
|
||||
if (S_ISLNK(leaf.state.st_mode))
|
||||
return fail(Issue::unsupported_route, path);
|
||||
if (!S_ISREG(leaf.state.st_mode) && !(allow_directory && S_ISDIR(leaf.state.st_mode)))
|
||||
return fail(Issue::not_regular, path);
|
||||
if (S_ISREG(leaf.state.st_mode) && leaf.state.st_nlink != 1)
|
||||
return fail(Issue::hard_link, path);
|
||||
if (budget.descriptors >= max_fds || !budget.claim(1, 0))
|
||||
return fail(Issue::resource_limit, path);
|
||||
return leaf;
|
||||
}
|
||||
|
||||
using Snapshot = std::shared_ptr<const Documents::DocumentSnapshot>;
|
||||
std::expected<Snapshot, SourceError>
|
||||
read_source(const Documents::DocumentStore &documents,
|
||||
const Documents::BoundSourceObservation &observation, const Leaf &leaf,
|
||||
const SourceRequest &request, Reservation &budget, std::size_t limit) {
|
||||
const auto &overlay = observation.overlay();
|
||||
if (!overlay && !leaf.object) {
|
||||
if (!request.allow_missing)
|
||||
return fail(Issue::missing, request.path);
|
||||
return Snapshot{};
|
||||
}
|
||||
if (!overlay && leaf.state.st_size < 0)
|
||||
return fail(Issue::unavailable, request.path);
|
||||
const auto size =
|
||||
overlay ? overlay->text.size() : static_cast<std::uintmax_t>(leaf.state.st_size);
|
||||
if (size > limit || !budget.claim(0, size))
|
||||
return fail(Issue::resource_limit, request.path);
|
||||
if (overlay)
|
||||
return overlay;
|
||||
std::string bytes;
|
||||
bytes.reserve(size);
|
||||
auto result = File::stream_regular_fd(
|
||||
leaf.object, leaf.state, limit, false, [&](std::span<const std::byte> chunk) {
|
||||
bytes.append(reinterpret_cast<const char *>(chunk.data()), chunk.size());
|
||||
return true;
|
||||
});
|
||||
if (!result)
|
||||
return fail(result.error() == File::FileError::changed ? Issue::changed
|
||||
: Issue::unavailable,
|
||||
request.path);
|
||||
auto snapshot = documents.materialize_bound_disk(observation, std::move(bytes));
|
||||
if (!snapshot)
|
||||
return fail(from_document(snapshot.error()), request.path);
|
||||
return *snapshot;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<std::shared_ptr<const SourceCapture>, SourceError>
|
||||
SourceCatalog::capture(const SourceScope &job, const InstalledSourceGrant &grant,
|
||||
const Documents::FileOverlayInventory &census,
|
||||
std::span<const SourceRequest> requests, SourceLimits limits) const {
|
||||
if (!grant || installed_.load() != grant || job != grant->scope)
|
||||
return fail(Issue::wrong_authority);
|
||||
if (!directory_current(job.workspace.root, *grant->workspace))
|
||||
return fail(Issue::changed, job.workspace.root);
|
||||
if (auto checked = check_requests(*grant, requests, limits); !checked)
|
||||
return std::unexpected(checked.error());
|
||||
if (auto checked = documents_.validate_bound_sources(census); !checked)
|
||||
return fail(from_document(checked.error()));
|
||||
auto reservation = reserve(accounting_);
|
||||
if (!reservation)
|
||||
return fail(Issue::resource_limit);
|
||||
auto evidence =
|
||||
std::make_shared<SourceEvidence>(grant, std::move(reservation), census.generation());
|
||||
std::shared_ptr<SourceCapture> result{new SourceCapture(evidence)};
|
||||
auto remaining = std::min(limits.total_bytes, SourceLimits{}.total_bytes);
|
||||
const auto file_limit = std::min(limits.file_bytes, SourceLimits{}.file_bytes);
|
||||
const auto max_fds = std::min(limits.descriptors, SourceLimits{}.descriptors);
|
||||
std::map<std::pair<dev_t, ino_t>, fs::path> regular_names;
|
||||
std::set<fs::path> open_names;
|
||||
for (const auto &entry : census.entries())
|
||||
open_names.insert(entry.identity.canonical_path);
|
||||
for (const auto &request : requests) {
|
||||
const auto &path = request.path;
|
||||
for (auto p = path.parent_path(); p != p.root_path(); p = p.parent_path())
|
||||
if (open_names.contains(p))
|
||||
return fail(Issue::conflicting_names, p);
|
||||
auto observed =
|
||||
documents_.observe_bound_source(census, Documents::path_to_file_uri(path), path);
|
||||
if (!observed)
|
||||
return fail(from_document(observed.error()), path);
|
||||
if (!evidence->reservation->claim(0, path.native().size()))
|
||||
return fail(Issue::resource_limit, path);
|
||||
if (auto root = grant->roots.find(path);
|
||||
request.lookup_only && root != grant->roots.end()) {
|
||||
if (observed->overlay())
|
||||
return fail(Issue::conflicting_names, path);
|
||||
evidence->directories.try_emplace(path, root->second);
|
||||
evidence->observations.push_back(std::move(*observed));
|
||||
result->entries_.push_back({path, {}, SourceNode::directory});
|
||||
continue;
|
||||
}
|
||||
auto parent = parent_directory(*evidence, path, max_fds);
|
||||
if (!parent && request.lookup_only && parent.error().issue == Issue::missing_parent) {
|
||||
const auto &first_missing = parent.error().path;
|
||||
auto it = open_names.lower_bound(first_missing);
|
||||
if (it != open_names.end() && (*it == first_missing || below(*it, first_missing)))
|
||||
return fail(Issue::missing_parent, first_missing);
|
||||
auto anchor = evidence->directories.find(first_missing.parent_path());
|
||||
if (anchor == evidence->directories.end())
|
||||
return fail(Issue::changed, first_missing);
|
||||
Leaf missing{anchor->second, first_missing.filename(), {}};
|
||||
evidence->leaves.emplace_back(first_missing, std::move(missing));
|
||||
evidence->observations.push_back(std::move(*observed));
|
||||
result->entries_.push_back({path, {}, SourceNode::missing});
|
||||
continue;
|
||||
}
|
||||
if (!parent)
|
||||
return std::unexpected(parent.error());
|
||||
auto leaf = bind_leaf(*parent, path, *evidence->reservation, max_fds, request.lookup_only);
|
||||
if (!leaf)
|
||||
return std::unexpected(leaf.error());
|
||||
if (leaf->object && S_ISDIR(leaf->state.st_mode)) {
|
||||
if (observed->overlay())
|
||||
return fail(Issue::conflicting_names, path);
|
||||
evidence->directories.try_emplace(
|
||||
path, std::make_shared<Pin>(std::move(leaf->object), leaf->state));
|
||||
evidence->observations.push_back(std::move(*observed));
|
||||
result->entries_.push_back({path, {}, SourceNode::directory});
|
||||
continue;
|
||||
}
|
||||
if (leaf->object) {
|
||||
auto [it, added] =
|
||||
regular_names.try_emplace({leaf->state.st_dev, leaf->state.st_ino}, path);
|
||||
if (!added && it->second != path)
|
||||
return fail(Issue::conflicting_names, path);
|
||||
}
|
||||
if (installed_.load() != grant)
|
||||
return fail(Issue::wrong_authority);
|
||||
const auto node =
|
||||
leaf->object || observed->overlay() ? SourceNode::regular : SourceNode::missing;
|
||||
if (node == SourceNode::missing) {
|
||||
const auto it = open_names.upper_bound(path);
|
||||
if (it != open_names.end() && below(*it, path))
|
||||
return fail(Issue::missing_parent, path);
|
||||
}
|
||||
std::expected<Snapshot, SourceError> snapshot{Snapshot{}};
|
||||
if (!request.lookup_only)
|
||||
snapshot = read_source(documents_, *observed, *leaf, request, *evidence->reservation,
|
||||
std::min(remaining, file_limit));
|
||||
if (!snapshot)
|
||||
return std::unexpected(snapshot.error());
|
||||
if (*snapshot)
|
||||
remaining -= (*snapshot)->text.size();
|
||||
std::optional<std::time_t> modified;
|
||||
if (*snapshot)
|
||||
modified = leaf->object ? leaf->state.st_mtim.tv_sec : (*snapshot)->created_at;
|
||||
result->entries_.push_back({path, std::move(*snapshot), node, modified});
|
||||
evidence->leaves.emplace_back(path, std::move(*leaf));
|
||||
evidence->observations.push_back(std::move(*observed));
|
||||
}
|
||||
if (before_capture_validation_)
|
||||
before_capture_validation_();
|
||||
if (auto checked = validate(job, *result); !checked)
|
||||
return std::unexpected(checked.error());
|
||||
return result;
|
||||
}
|
||||
|
||||
std::expected<void, SourceError> SourceCatalog::validate(const SourceScope &job,
|
||||
const SourceCapture &capture) const {
|
||||
auto &e = *capture.evidence_;
|
||||
if (e.grant != installed_.load() || e.grant->scope != job)
|
||||
return fail(Issue::wrong_authority);
|
||||
auto expire = [&](const fs::path &path) {
|
||||
e.expired = true;
|
||||
return fail(Issue::changed, path);
|
||||
};
|
||||
if (e.expired || !directory_current(job.workspace.root, *e.grant->workspace))
|
||||
return expire(job.workspace.root);
|
||||
for (const auto &[path, dir] : e.directories)
|
||||
if (!directory_current(path, *dir))
|
||||
return expire(path);
|
||||
for (const auto &[path, leaf] : e.leaves)
|
||||
if (!leaf_current(leaf))
|
||||
return expire(path);
|
||||
if (auto current = documents_.validate_bound_sources(e.membership, e.observations); !current)
|
||||
return expire({});
|
||||
if (e.grant != installed_.load())
|
||||
return fail(Issue::wrong_authority);
|
||||
if (e.expired)
|
||||
return fail(Issue::changed);
|
||||
return {};
|
||||
}
|
||||
} // namespace vls::semantic
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user