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,335 @@
|
||||
#include <vls/semantic/Diagnostics.hpp>
|
||||
|
||||
#include <Void/LanguageServer/Protocol/Json.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
namespace {
|
||||
using namespace vls::semantic;
|
||||
namespace D = Void::LanguageServer::Documents;
|
||||
namespace P = Void::LanguageServer::Protocol;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::require;
|
||||
using namespace std::string_view_literals;
|
||||
|
||||
DiagnosticSnapshot snapshot(std::string uri, std::string text,
|
||||
D::PositionEncoding encoding = D::PositionEncoding::utf16) {
|
||||
auto doc = std::make_shared<D::DocumentSnapshot>();
|
||||
doc->identity.canonical_uri = doc->identity.display_uri = std::move(uri);
|
||||
doc->text = std::move(text);
|
||||
doc->line_index = D::LineIndex(doc->text);
|
||||
doc->encoding = encoding;
|
||||
doc->version = 7;
|
||||
return doc;
|
||||
}
|
||||
|
||||
std::string quote(std::string_view s) {
|
||||
auto out = P::serialize_json(P::Json{std::string{s}});
|
||||
require(out.has_value());
|
||||
return *out;
|
||||
}
|
||||
|
||||
constexpr auto primary = R"({"uri":"main.cpp","uriBaseId":"PWD"})"sv;
|
||||
constexpr auto header = R"({"uri":"a.hpp","uriBaseId":"PWD"})"sv;
|
||||
constexpr auto first_error = R"({"startLine":1,"startColumn":5,"endColumn":12})"sv;
|
||||
|
||||
std::string artifact(std::string_view name, bool owner,
|
||||
std::optional<std::string_view> bytes = {}) {
|
||||
std::string record = R"({"location":)" + std::string{name};
|
||||
if (owner)
|
||||
record += R"(,"roles":["analysisTarget"])";
|
||||
if (bytes)
|
||||
record += R"(,"contents":{"text":)" + quote(*bytes) + "}";
|
||||
return record + "}";
|
||||
}
|
||||
std::string location(std::string_view name = primary, std::string_view region = first_error) {
|
||||
return R"({"physicalLocation":{"artifactLocation":)" + std::string{name} + R"(,"region":)" +
|
||||
std::string{region} + "}}";
|
||||
}
|
||||
std::string issue(std::string where = location(), std::string_view level = "error",
|
||||
std::string_view rule = "error", std::string_view related = {}) {
|
||||
return R"({"level":)" + quote(level) + R"(,"ruleId":)" + quote(rule) +
|
||||
R"(,"message":{"text":"the compiler message"},"locations":[)" + where +
|
||||
R"(],"relatedLocations":[)" + std::string{related} + "]}";
|
||||
}
|
||||
std::string report(bool success, std::string findings = {},
|
||||
std::string files = artifact(primary, true),
|
||||
std::string_view notifications = {}) {
|
||||
return R"({"version":"2.1.0","runs":[{"tool":{"driver":{"name":"GNU C++26","version":"16.2.0"}},)"
|
||||
R"("originalUriBaseIds":{"PWD":{"uri":"file:///work/"}},"artifacts":[)" +
|
||||
files + R"(],"invocations":[{"executionSuccessful":)" + (success ? "true" : "false") +
|
||||
R"(,"toolExecutionNotifications":[)" + std::string{notifications} +
|
||||
R"(]}],"results":[)" + findings + "]}]}";
|
||||
}
|
||||
|
||||
struct Capture {
|
||||
DiagnosticSnapshot owner;
|
||||
DiagnosticJob job;
|
||||
|
||||
explicit Capture(std::string bytes = "int missing;\n",
|
||||
D::PositionEncoding encoding = D::PositionEncoding::utf16)
|
||||
: owner(snapshot("file:///work/main.cpp", std::move(bytes), encoding)),
|
||||
job{"run-17", "view-3",
|
||||
owner, {{{"main.cpp", "file:///work/"}, owner}},
|
||||
true, [](const LocationRequest &) { return CoordinatePolicy{}; }} {}
|
||||
|
||||
auto decode(const std::string &bytes, int status = 1) const {
|
||||
return decode_gcc_diagnostics(job,
|
||||
{job.invocation_id, job.input_view_id, status, true, bytes});
|
||||
}
|
||||
DiagnosticBatch batch(const std::string &bytes, int status = 1) const {
|
||||
auto result = decode(bytes, status);
|
||||
require(result.has_value());
|
||||
return std::move(*result);
|
||||
}
|
||||
};
|
||||
|
||||
DiagnosticLocation primary_location(const Capture &capture, std::string_view region = first_error) {
|
||||
auto batch = capture.batch(report(false, issue(location(primary, region))));
|
||||
require(batch.diagnostics.size() == 1);
|
||||
return batch.diagnostics[0].location;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
VOID_TEST(DiagnosticTests, only_the_matched_owner_and_capture_can_clear) {
|
||||
Capture f;
|
||||
const auto wire = report(true);
|
||||
const auto clean = f.batch(wire, 0);
|
||||
expect(clean.complete_for_owner && clean.diagnostics.empty() && clean.owner == f.owner);
|
||||
expect(clean.invocation_id == "run-17" && clean.input_view_id == "view-3");
|
||||
expect(!decode_gcc_diagnostics(f.job, {"run-18", "view-3", 0, true, wire}));
|
||||
expect(!decode_gcc_diagnostics(f.job, {"run-17", "view-4", 0, true, wire}));
|
||||
const auto other = snapshot("file:///work/other.cpp", f.owner->text);
|
||||
f.job.artifacts.push_back({{"other.cpp", "file:///work/"}, other});
|
||||
const auto incorrect = artifact(R"({"uri":"other.cpp","uriBaseId":"PWD"})", true);
|
||||
expect(!f.decode(report(true, {}, incorrect), 0));
|
||||
expect(!f.decode(report(true, {}, artifact(primary, true) + "," + incorrect), 0));
|
||||
expect(!f.decode(report(true, {}, artifact(primary, false)), 0));
|
||||
expect(!f.decode(report(true, {}, artifact(primary, true, "other content")), 0));
|
||||
f.job.inputs_complete = false;
|
||||
expect(!f.batch(wire, 0).complete_for_owner);
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, artifact_self_indices_must_match_the_table) {
|
||||
Capture f;
|
||||
auto indexed = [](std::string_view name, std::string_view index, bool owner) {
|
||||
return artifact(R"({"uri":")" + std::string{name} + R"(","uriBaseId":"PWD","index":)" +
|
||||
std::string{index} + "}",
|
||||
owner);
|
||||
};
|
||||
const auto clean = f.batch(
|
||||
report(true, {}, indexed("a.hpp", "0", false) + "," + indexed("main.cpp", "1", true)), 0);
|
||||
expect(clean.complete_for_owner && clean.diagnostics.empty());
|
||||
const auto second = indexed("a.hpp", "1", false);
|
||||
for (std::string_view index : {"999", R"("0")", "1", "0.0", "-1", "null"}) {
|
||||
const auto rejected =
|
||||
f.decode(report(true, {}, indexed("main.cpp", index, true) + "," + second), 0);
|
||||
expect(!rejected && rejected.error() == DiagnosticError::malformed_report);
|
||||
}
|
||||
const auto rejected = f.decode(
|
||||
report(true, {}, indexed("main.cpp", "0", true) + "," + indexed("a.hpp", "0", false)), 0);
|
||||
expect(!rejected && rejected.error() == DiagnosticError::malformed_report);
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, source_error_and_warning_keep_message_rule_and_related_evidence) {
|
||||
Capture f;
|
||||
const auto note = snapshot("file:///work/a.hpp", "int missing;\n");
|
||||
f.job.artifacts.push_back({{"a.hpp", "file:///work/"}, note});
|
||||
const auto files =
|
||||
artifact(primary, true, f.owner->text) + "," + artifact(header, false, note->text);
|
||||
auto related = location(R"({"index":1})") + "," + location(header, R"({"startLine":1})");
|
||||
const auto batch = f.batch(report(false, issue(location(), "error", "error", related), files));
|
||||
require(batch.diagnostics.size() == 1);
|
||||
const auto &d = batch.diagnostics.front();
|
||||
expect(batch.complete_for_owner && d.severity == DiagnosticSeverity::error &&
|
||||
d.rule == "error" && d.message == "the compiler message");
|
||||
expect(d.location.snapshot == f.owner && d.location.range == D::Range{{0, 4}, {0, 11}});
|
||||
require(d.related.size() == 2);
|
||||
expect(d.related[0].location.snapshot == note &&
|
||||
d.related[0].location.range == d.location.range);
|
||||
expect(d.related[1].message.empty() &&
|
||||
d.related[1].location.gap == LocationGap::incomplete_region);
|
||||
const auto warning =
|
||||
f.batch(report(true, issue(location(), "warning", "-Wdeprecated-declarations")), 0);
|
||||
expect(warning.complete_for_owner &&
|
||||
warning.diagnostics[0].severity == DiagnosticSeverity::warning);
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, source_text_alone_cannot_map_a_remapped_occurrence) {
|
||||
Capture f{"#if 0\nint missing;\n#endif\n#include \"alias.hpp\"\n"};
|
||||
const auto alias = snapshot("file:///work/alias.hpp", "#line 2 \"main.cpp\"\nint missing;\n");
|
||||
f.job.artifacts.push_back({{"alias.hpp", "file:///work/"}, alias});
|
||||
f.job.physical_origin = {};
|
||||
const auto d = f.batch(
|
||||
report(false, issue(location(primary, R"({"startLine":2,"startColumn":5,"endColumn":12})")),
|
||||
artifact(primary, true, f.owner->text)));
|
||||
expect(!d.complete_for_owner && d.diagnostics[0].message == "the compiler message" &&
|
||||
d.diagnostics[0].location.gap == LocationGap::unproven_origin);
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, literal_artifact_names_and_indices_bind_exactly) {
|
||||
Capture f;
|
||||
const auto space = snapshot("file:///work/a%20b.cpp", "int missing;\n");
|
||||
const auto percent = snapshot("file:///work/a%2520b.cpp", space->text);
|
||||
f.job.artifacts.push_back({{"a b.cpp", ""}, space});
|
||||
f.job.artifacts.push_back({{"a%20b.cpp", ""}, percent});
|
||||
const auto files = artifact(primary, true) + "," + artifact(R"({"uri":"a b.cpp"})", false) +
|
||||
"," + artifact(R"({"uri":"a%20b.cpp"})", false);
|
||||
auto locate = [&](std::string_view name) {
|
||||
return f.batch(report(false, issue(location(name)), files)).diagnostics.front().location;
|
||||
};
|
||||
expect(locate(R"({"uri":"a%20b.cpp","index":2})").snapshot == percent);
|
||||
expect(locate(R"({"index":1})").snapshot == space);
|
||||
expect(locate(R"({"uri":"a%20b.cpp","index":1})").gap == LocationGap::unknown_artifact);
|
||||
expect(locate(R"({"uri":"a b.cpp","uriBaseId":"wrong"})").gap == LocationGap::unknown_artifact);
|
||||
expect(locate(R"({"index":1.00000000000000001})").gap == LocationGap::unknown_artifact);
|
||||
const auto stale =
|
||||
artifact(primary, true) + "," + artifact(R"({"uri":"a b.cpp"})", false, "old");
|
||||
expect(f.batch(report(false, issue(location(R"({"index":1})")), stale))
|
||||
.diagnostics[0]
|
||||
.location.gap == LocationGap::artifact_mismatch);
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, certified_byte_positions_use_the_negotiated_encoding) {
|
||||
for (auto encoding : {D::PositionEncoding::utf8, D::PositionEncoding::utf16}) {
|
||||
Capture f{"\t😀é missing;\r\n", encoding};
|
||||
f.job.physical_origin = [](const LocationRequest &) {
|
||||
return CoordinatePolicy{ColumnUnit::utf8_bytes};
|
||||
};
|
||||
const auto loc = primary_location(f, R"({"startLine":1,"startColumn":10,"endColumn":17})");
|
||||
const std::uint32_t start = encoding == D::PositionEncoding::utf8 ? 9 : 6;
|
||||
expect(loc.range == D::Range{{0, start}, {0, start + 7}});
|
||||
expect(primary_location(f, R"({"startLine":1,"startColumn":3,"endColumn":17})").gap ==
|
||||
LocationGap::ambiguous_column);
|
||||
f.job.physical_origin = [](const LocationRequest &) {
|
||||
return CoordinatePolicy{ColumnUnit::gcc_display};
|
||||
};
|
||||
expect(primary_location(f, R"({"startLine":1,"startColumn":16,"endColumn":23})").gap ==
|
||||
LocationGap::ambiguous_column);
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, display_columns_withhold_ambiguous_zero_width_boundaries) {
|
||||
const auto wire = report(
|
||||
false, issue(location(primary, R"({"startLine":1,"startColumn":16,"endColumn":17})")));
|
||||
for (const auto unit : {ColumnUnit::ascii_consensus, ColumnUnit::gcc_display}) {
|
||||
const auto policy = [unit](const LocationRequest &) { return CoordinatePolicy{unit}; };
|
||||
for (const char *source : {"int f(){return e\u0301;}\n", "int f(){return y\u200d;}\n",
|
||||
"int f(){return e\u0301\u200d;}\n"}) {
|
||||
Capture f{source};
|
||||
f.job.physical_origin = policy;
|
||||
const auto batch = f.batch(wire);
|
||||
require(batch.diagnostics.size() == 1);
|
||||
const auto &diagnostic = batch.diagnostics.front();
|
||||
expect(!batch.complete_for_owner && diagnostic.message == "the compiler message");
|
||||
expect(diagnostic.location.gap == LocationGap::ambiguous_column &&
|
||||
!diagnostic.location.range);
|
||||
}
|
||||
Capture leading{"\u0301x\nz\n"};
|
||||
leading.job.physical_origin = policy;
|
||||
expect(primary_location(leading,
|
||||
R"({"startLine":1,"startColumn":1,"endLine":2,"endColumn":2})")
|
||||
.gap == LocationGap::ambiguous_column);
|
||||
|
||||
Capture ascii{"int f(){return yy;} // \u0301\n"};
|
||||
ascii.job.physical_origin = policy;
|
||||
expect(
|
||||
primary_location(ascii, R"({"startLine":1,"startColumn":16,"endColumn":18})").range ==
|
||||
D::Range{{0, 15}, {0, 17}});
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, certified_byte_columns_distinguish_zero_width_boundaries) {
|
||||
for (const auto encoding : {D::PositionEncoding::utf8, D::PositionEncoding::utf16}) {
|
||||
Capture f{"e\u0301;\n", encoding};
|
||||
f.job.physical_origin = [](const LocationRequest &) {
|
||||
return CoordinatePolicy{ColumnUnit::utf8_bytes};
|
||||
};
|
||||
expect(primary_location(f, R"({"startLine":1,"startColumn":1,"endColumn":2})").range ==
|
||||
D::Range{{0, 0}, {0, 1}});
|
||||
const std::uint32_t end = encoding == D::PositionEncoding::utf8 ? 3 : 2;
|
||||
expect(primary_location(f, R"({"startLine":1,"startColumn":1,"endColumn":4})").range ==
|
||||
D::Range{{0, 0}, {0, end}});
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, tabstop_and_first_line_bom_require_known_producer_units) {
|
||||
Capture tab{"\tmissing;\n"};
|
||||
for (const unsigned width : {4u, 8u}) {
|
||||
tab.job.physical_origin = [width](const LocationRequest &) {
|
||||
return CoordinatePolicy{ColumnUnit::gcc_display, width};
|
||||
};
|
||||
const auto region = R"({"startLine":1,"startColumn":)" + std::to_string(width + 1) +
|
||||
R"(,"endColumn":)" + std::to_string(width + 8) + "}";
|
||||
expect(primary_location(tab, region).range == D::Range{{0, 1}, {0, 8}});
|
||||
expect(primary_location(tab, R"({"startLine":1,"startColumn":2,"endColumn":3})").gap ==
|
||||
LocationGap::ambiguous_column);
|
||||
}
|
||||
Capture bom{"\xef\xbb\xbfmissing;\n"};
|
||||
const auto region = R"({"startLine":1,"startColumn":1,"endColumn":8})";
|
||||
expect(primary_location(bom, region).gap == LocationGap::ambiguous_column);
|
||||
bom.job.physical_origin = [](const LocationRequest &) {
|
||||
return CoordinatePolicy{ColumnUnit::utf8_bytes, 8, FirstLineBom::skipped};
|
||||
};
|
||||
expect(primary_location(bom, region).range == D::Range{{0, 1}, {0, 8}});
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, invalid_lines_offsets_and_numeric_variants_never_clamp) {
|
||||
Capture f;
|
||||
for (std::string_view r : {R"({"startLine":0,"startColumn":1,"endColumn":2})",
|
||||
R"({"startLine":1,"startColumn":1.00000000000000001,"endColumn":2})",
|
||||
R"({"startLine":1,"startColumn":3,"endColumn":2})",
|
||||
R"({"startLine":1,"startColumn":1,"endColumn":2147483648})"})
|
||||
expect(primary_location(f, r).gap == LocationGap::incomplete_region);
|
||||
for (std::string_view r : {R"({"startLine":1,"startColumn":1,"endColumn":14})",
|
||||
R"({"startLine":3,"startColumn":1,"endColumn":2})"})
|
||||
expect(primary_location(f, r).gap == LocationGap::ambiguous_column);
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, early_failures_preserve_the_problem_without_clearing_diagnostics) {
|
||||
Capture f;
|
||||
auto fatal = f.batch(report(false, issue(location(), "error", "fatal error")));
|
||||
expect(!fatal.complete_for_owner && fatal.diagnostics.front().location.range.has_value());
|
||||
auto before_open = f.batch(report(false, issue("", "error", "fatal error"), {}));
|
||||
expect(!before_open.complete_for_owner &&
|
||||
before_open.diagnostics.front().message == "the compiler message");
|
||||
auto unknown = f.batch(report(false, issue(), artifact(primary, false)));
|
||||
expect(!unknown.complete_for_owner &&
|
||||
unknown.diagnostics[0].location.gap == LocationGap::unknown_artifact);
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, incomplete_process_and_malformed_reports_cannot_clear) {
|
||||
Capture f;
|
||||
const auto wire = report(true);
|
||||
for (const auto &out : {DiagnosticOutput{"run-17", "view-3", {}, true, wire},
|
||||
DiagnosticOutput{"run-17", "view-3", 126, true, wire},
|
||||
DiagnosticOutput{"run-17", "view-3", 0, false, wire},
|
||||
DiagnosticOutput{"run-17", "view-3", 1, true, {}}})
|
||||
expect(!decode_gcc_diagnostics(f.job, out));
|
||||
expect(!f.decode(wire));
|
||||
expect(!f.decode(report(false)));
|
||||
expect(!f.decode(report(true, issue()), 0));
|
||||
expect(!f.decode(report(false, issue(location(), "warning"))));
|
||||
expect(!f.decode(report(true, {}, artifact(primary, true), "{}"), 0));
|
||||
expect(!f.decode(wire.substr(0, wire.size() - 1), 0));
|
||||
}
|
||||
|
||||
VOID_TEST(DiagnosticTests, report_structure_and_output_sizes_are_bounded) {
|
||||
Capture f;
|
||||
expect(f.decode(std::string(4 * 1024 * 1024 + 1, ' '), 0).error() ==
|
||||
DiagnosticError::resource_limit);
|
||||
std::string repeated = "[0";
|
||||
for (unsigned i = 0; i < 150000; ++i)
|
||||
repeated += ",0";
|
||||
expect(f.decode(repeated + "]").error() == DiagnosticError::resource_limit);
|
||||
auto findings = issue();
|
||||
const auto pos = findings.find("the compiler message");
|
||||
require(pos != findings.npos);
|
||||
findings.replace(pos, 20, 65537, 'x');
|
||||
expect(f.decode(report(false, findings)).error() == DiagnosticError::resource_limit);
|
||||
const auto quoted_text = std::string(150000, ',');
|
||||
expect(f.decode(report(true, {},
|
||||
artifact(primary, true) + "," + artifact(header, false, quoted_text)),
|
||||
0)
|
||||
.has_value());
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
#include <vls/WorkspaceSession.hpp>
|
||||
#include <vls/semantic/Diagnostics.hpp>
|
||||
#include <vls/semantic/SourceCatalog.hpp>
|
||||
#include <vls/semantic/StockCompiler.hpp>
|
||||
|
||||
#include <Void/LanguageServer/Protocol/Json.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <charconv>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
namespace P = Void::LanguageServer::Protocol;
|
||||
namespace D = Void::LanguageServer::Documents;
|
||||
using namespace vls::semantic;
|
||||
|
||||
std::string bounded_read(std::istream &stream) {
|
||||
constexpr std::size_t limit = 4 * 1024 * 1024;
|
||||
std::string bytes;
|
||||
std::array<char, 8192> chunk;
|
||||
while (stream.read(chunk.data(), chunk.size()) || stream.gcount()) {
|
||||
const auto count = static_cast<std::size_t>(stream.gcount());
|
||||
if (count > limit - bytes.size())
|
||||
throw std::runtime_error("fixture stream too large");
|
||||
bytes.append(chunk.data(), count);
|
||||
}
|
||||
if (!stream.eof() || stream.bad())
|
||||
throw std::runtime_error("incomplete fixture stream");
|
||||
return bytes;
|
||||
}
|
||||
|
||||
unsigned number(std::string_view input) {
|
||||
unsigned result{};
|
||||
const auto [end, error] = std::from_chars(input.data(), input.data() + input.size(), result);
|
||||
if (error != std::errc{} || end != input.data() + input.size())
|
||||
throw std::runtime_error("invalid fixture number");
|
||||
return result;
|
||||
}
|
||||
|
||||
P::Json position(const D::Position &p) {
|
||||
return P::Json{
|
||||
P::Json::Array{P::Json{std::int64_t{p.line}}, P::Json{std::int64_t{p.character}}}};
|
||||
}
|
||||
|
||||
P::Json describe_command(std::span<char *> args) {
|
||||
namespace W = Void::LanguageServer::Workspace;
|
||||
const std::string_view mode{args.front()};
|
||||
const bool analyze = mode == "--analyze";
|
||||
const bool capture = mode == "--capture-disk" || mode == "--capture-overlay";
|
||||
const std::size_t required = capture ? 5 : 4;
|
||||
if (args.size() != required && args.size() != required + 1)
|
||||
throw std::runtime_error("expected WORKSPACE SOURCE GCC, a capture file if selected, and "
|
||||
"optional COMPILE_COMMANDS");
|
||||
const std::filesystem::path root{args[1]}, source{args[2]};
|
||||
std::filesystem::path driver{args[3]};
|
||||
std::shared_ptr<const GccProfile> profile;
|
||||
if (analyze) {
|
||||
auto loaded = GccProfile::load(driver);
|
||||
if (!loaded)
|
||||
throw std::runtime_error(loaded.error());
|
||||
profile = std::move(*loaded);
|
||||
driver = profile->driver;
|
||||
}
|
||||
if (!root.is_absolute() || !source.is_absolute() || !driver.is_absolute())
|
||||
throw std::runtime_error("expected absolute context fixture paths");
|
||||
W::SettingsOverrides overrides;
|
||||
overrides.provider =
|
||||
args.size() == required ? W::ProviderPreference::vbs : W::ProviderPreference::compdb;
|
||||
if (args.size() > required)
|
||||
overrides.compdb_path = args[required];
|
||||
overrides.tool_approvals = {{root, W::AuthorityLevel::compiler_use, driver}};
|
||||
vls::WorkspaceSession session(
|
||||
{.root = root, .cli_settings = W::settings_from_cli(std::move(overrides), root)});
|
||||
const auto root_uri = D::path_to_file_uri(root);
|
||||
const auto doc_uri = D::path_to_file_uri(source);
|
||||
const P::Json folder{P::Json::Object{{"uri", P::Json{root_uri}}}};
|
||||
if (auto initialized = session.initialize(
|
||||
P::Json{P::Json::Object{{"workspaceFolders", P::Json{P::Json::Array{folder}}}}});
|
||||
!initialized)
|
||||
throw std::runtime_error(initialized.error());
|
||||
auto lease =
|
||||
session.acquire_semantic_context(doc_uri, root_uri, {driver, W::CompilerDriver::gcc});
|
||||
if (!lease)
|
||||
throw std::runtime_error(lease.error().message);
|
||||
const auto &selection = lease->selection();
|
||||
const auto *row = selection->selected;
|
||||
if (!row || !row->semantic_arguments || !row->original || !row->working_directory ||
|
||||
row->source_language != "c++" || row->key.owning_tu != source)
|
||||
throw std::runtime_error("the selected row has no checked C++ command");
|
||||
if (analyze) {
|
||||
D::DocumentStore documents;
|
||||
if (!documents.open(doc_uri, "cpp", 1, bounded_read(std::cin)))
|
||||
throw std::runtime_error("cannot open compiler fixture document");
|
||||
SourceCatalog catalog{documents};
|
||||
const SourceScope scope{selection->generation->identity().workspace,
|
||||
lease->configuration_generation(),
|
||||
lease->driver().tool.trust_generation};
|
||||
auto grant = catalog.install(scope, std::array{root});
|
||||
if (!grant)
|
||||
throw std::runtime_error("could not grant selected source root");
|
||||
auto result = analyze_with_gcc(*profile, *lease, documents, catalog, *grant, 1, {}, {});
|
||||
if (!result)
|
||||
throw std::runtime_error(result.error());
|
||||
P::Json::Array diagnostics;
|
||||
for (auto &d : result->batch.diagnostics) {
|
||||
P::Json range{nullptr};
|
||||
if (d.location.range)
|
||||
range = P::Json{P::Json::Array{position(d.location.range->start),
|
||||
position(d.location.range->end)}};
|
||||
diagnostics.emplace_back(
|
||||
P::Json::Object{{"message", P::Json{d.message}}, {"range", std::move(range)}});
|
||||
}
|
||||
return P::Json{P::Json::Object{
|
||||
{"complete", P::Json{result->batch.complete_for_owner}},
|
||||
{"compilerRuns", P::Json{static_cast<std::int64_t>(result->compiler_runs)}},
|
||||
{"lookups", P::Json{static_cast<std::int64_t>(result->lookups.size())}},
|
||||
{"unprovenOrigin", result->unproven_location_origin
|
||||
? P::Json{result->unproven_location_origin->native()}
|
||||
: P::Json{nullptr}},
|
||||
{"diagnostics", P::Json{std::move(diagnostics)}}}};
|
||||
}
|
||||
if (capture) {
|
||||
const std::filesystem::path file{args[4]};
|
||||
const auto uri = D::path_to_file_uri(file);
|
||||
D::DocumentStore documents;
|
||||
if (mode == "--capture-overlay") {
|
||||
if (auto opened = documents.open(uri, "cpp", 1, bounded_read(std::cin)); !opened)
|
||||
throw std::runtime_error(opened.error().message);
|
||||
}
|
||||
SourceCatalog catalog{documents};
|
||||
const SourceScope job{selection->generation->identity().workspace,
|
||||
lease->configuration_generation(),
|
||||
lease->driver().tool.trust_generation};
|
||||
// The fixture explicitly grants its workspace, including its prepared library generation.
|
||||
const std::array roots{root};
|
||||
auto grant = catalog.install(job, roots);
|
||||
auto census = documents.file_overlay_inventory();
|
||||
if (!grant || !census)
|
||||
throw std::runtime_error("could not bind source authority for the selected fixture");
|
||||
std::vector<SourceRequest> names{{source}};
|
||||
if (file != source)
|
||||
names.push_back({file});
|
||||
auto captured =
|
||||
catalog.capture(job, *grant, *census, names,
|
||||
{.files = 2, .file_bytes = 512 * 1024, .total_bytes = 1024 * 1024});
|
||||
if (!captured)
|
||||
throw std::runtime_error("fixture source was unavailable for capture");
|
||||
auto current = session.validate_semantic_context(*lease);
|
||||
if (!current || !catalog.validate(job, **captured))
|
||||
throw std::runtime_error("the selected command or source changed during capture");
|
||||
const auto &doc = (*captured)->entries().back().snapshot;
|
||||
return P::Json{
|
||||
P::Json::Object{{"uri", P::Json{doc->identity.canonical_uri}},
|
||||
{"text", P::Json{doc->text}},
|
||||
{"overlay", P::Json{doc->origin == D::DocumentOrigin::overlay}}}};
|
||||
}
|
||||
P::Json::Array arguments;
|
||||
for (const auto &arg : *row->semantic_arguments)
|
||||
arguments.emplace_back(arg);
|
||||
if (auto valid = session.validate_semantic_context(*lease); !valid)
|
||||
throw std::runtime_error(valid.error().message);
|
||||
return P::Json{
|
||||
P::Json::Object{{"directory", P::Json{row->working_directory->native()}},
|
||||
{"file", P::Json{row->key.owning_tu.native()}},
|
||||
{"database", P::Json{row->original->metadata_file.native()}},
|
||||
{"provider", P::Json{selection->generation->identity().provider}},
|
||||
{"driverSha256", P::Json{lease->driver().tool.identity.content_hash_hex}},
|
||||
{"arguments", P::Json{std::move(arguments)}}}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
try {
|
||||
std::span<char *> args{argv + 1, static_cast<std::size_t>(argc - 1)};
|
||||
const std::string_view mode =
|
||||
args.empty() ? std::string_view{} : std::string_view{args.front()};
|
||||
if (mode == "--command" || mode == "--capture-disk" || mode == "--capture-overlay" ||
|
||||
mode == "--analyze") {
|
||||
std::cout << P::serialize_json(describe_command(args)).value() << '\n';
|
||||
return 0;
|
||||
}
|
||||
std::filesystem::path logical_source;
|
||||
if (args.size() >= 2 && std::string_view{args[0]} == "--logical-file") {
|
||||
logical_source = args[1];
|
||||
if (!logical_source.is_absolute() ||
|
||||
logical_source != logical_source.lexically_normal())
|
||||
throw std::runtime_error("expected a normalized absolute compiler input name");
|
||||
args = args.subspan(2);
|
||||
}
|
||||
if (args.size() != 2 && args.size() != 5)
|
||||
throw std::runtime_error(
|
||||
"expected source and status, optionally a witnessed line/start/end");
|
||||
const std::filesystem::path source{args[0]};
|
||||
const unsigned status = number(args[1]);
|
||||
if (source != source.filename() || source.empty() || status > 1)
|
||||
throw std::runtime_error("expected a local fixture filename and compiler exit 0 or 1");
|
||||
std::ifstream input(source, std::ios::binary);
|
||||
if (!input)
|
||||
throw std::runtime_error("cannot open the supplied source snapshot");
|
||||
auto doc = std::make_shared<D::DocumentSnapshot>();
|
||||
const auto logical =
|
||||
logical_source.empty() ? std::filesystem::absolute(source) : logical_source;
|
||||
const auto uri = D::path_to_file_uri(logical);
|
||||
doc->identity = {uri, uri, logical, true};
|
||||
doc->text = bounded_read(input);
|
||||
doc->line_index = D::LineIndex(doc->text);
|
||||
doc->version = 1;
|
||||
// GCC writes the producer's literal working directory in originalUriBaseIds.
|
||||
auto base = "file://" + std::filesystem::current_path().native();
|
||||
if (!base.ends_with('/'))
|
||||
base += '/';
|
||||
ArtifactName artifact = logical_source.empty() ? ArtifactName{source.native(), base}
|
||||
: ArtifactName{logical_source.native(), {}};
|
||||
DiagnosticJob job{
|
||||
"fixture-run", "fixture-view", doc, {{std::move(artifact), doc}}, true, {}};
|
||||
if (args.size() == 5) {
|
||||
// The test runner supplies the single physical occurrence witnessed by its edit.
|
||||
const unsigned line = number(args[2]), first = number(args[3]), end = number(args[4]);
|
||||
job.physical_origin =
|
||||
[=](const LocationRequest &request) -> std::optional<CoordinatePolicy> {
|
||||
const auto &r = request.region;
|
||||
if (!request.related && r.first_line == line && r.last_line == line &&
|
||||
r.first_column == first && r.end_column == end)
|
||||
return CoordinatePolicy{};
|
||||
return std::nullopt;
|
||||
};
|
||||
}
|
||||
const auto report = bounded_read(std::cin);
|
||||
auto batch = decode_gcc_diagnostics(
|
||||
job, {job.invocation_id, job.input_view_id, static_cast<int>(status), true, report});
|
||||
if (!batch) {
|
||||
std::cerr << "diagnostic adapter rejected report (" << static_cast<int>(batch.error())
|
||||
<< ")\n";
|
||||
return 1;
|
||||
}
|
||||
P::Json::Array diagnostics;
|
||||
for (const auto &d : batch->diagnostics) {
|
||||
P::Json range{nullptr};
|
||||
if (d.location.range)
|
||||
range = P::Json{P::Json::Array{position(d.location.range->start),
|
||||
position(d.location.range->end)}};
|
||||
diagnostics.emplace_back(P::Json::Object{{"message", P::Json{d.message}},
|
||||
{"rule", P::Json{d.rule}},
|
||||
{"range", std::move(range)}});
|
||||
}
|
||||
auto bytes = P::serialize_json(
|
||||
P::Json{P::Json::Object{{"complete", P::Json{batch->complete_for_owner}},
|
||||
{"diagnostics", P::Json{std::move(diagnostics)}}}});
|
||||
if (!bytes)
|
||||
throw std::runtime_error(bytes.error());
|
||||
std::cout << *bytes << '\n';
|
||||
} catch (const std::exception &error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
#include <vls/semantic/Occurrences.hpp>
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
namespace D = Void::LanguageServer::Documents;
|
||||
using namespace vls::semantic;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::require;
|
||||
|
||||
constexpr std::string_view nonce = "0123456789abcdef0123456789abcdef";
|
||||
constexpr std::string_view path = "/workspace/unit.cpp";
|
||||
|
||||
std::shared_ptr<const D::DocumentSnapshot>
|
||||
snapshot(std::string text, D::PositionEncoding encoding = D::PositionEncoding::utf16) {
|
||||
auto value = std::make_shared<D::DocumentSnapshot>();
|
||||
value->text = std::move(text);
|
||||
value->line_index = D::LineIndex(value->text);
|
||||
value->encoding = encoding;
|
||||
return value;
|
||||
}
|
||||
|
||||
std::vector<CapturedSource> capture(std::string text,
|
||||
D::PositionEncoding encoding = D::PositionEncoding::utf16) {
|
||||
std::vector<CapturedSource> entries;
|
||||
entries.push_back({path, snapshot(std::move(text), encoding), SourceNode::regular, {}});
|
||||
return entries;
|
||||
}
|
||||
|
||||
struct Span {
|
||||
std::uint32_t line, start, end;
|
||||
};
|
||||
|
||||
Span span(std::string_view text, std::string_view name, std::size_t ordinal = 0) {
|
||||
std::size_t offset = 0;
|
||||
for (std::size_t found = 0; found <= ordinal; ++found) {
|
||||
offset = text.find(name, offset);
|
||||
require(offset != std::string_view::npos);
|
||||
if (found != ordinal)
|
||||
offset += name.size();
|
||||
}
|
||||
std::uint32_t line = 1;
|
||||
std::size_t line_start = 0;
|
||||
for (std::size_t i = 0; i != offset; ++i)
|
||||
if (text[i] == '\n') {
|
||||
++line;
|
||||
line_start = i + 1;
|
||||
}
|
||||
return {line, static_cast<std::uint32_t>(offset - line_start + 1),
|
||||
static_cast<std::uint32_t>(offset - line_start + 1 + name.size())};
|
||||
}
|
||||
|
||||
std::string span_json(std::string_view where, const Span &value) {
|
||||
return R"({"path":")" + std::string{where} + R"(","line":)" + std::to_string(value.line) +
|
||||
R"(,"startByte":)" + std::to_string(value.start) + R"(,"endByte":)" +
|
||||
std::to_string(value.end) + "}";
|
||||
}
|
||||
|
||||
std::string occurrence(std::string_view name, std::string_view type, const std::string &use,
|
||||
const std::string &definition) {
|
||||
return R"({"name":")" + std::string{name} + R"(","type":")" + std::string{type} +
|
||||
R"(","canonicalType":")" + std::string{type} +
|
||||
R"sig(","signature":"int f(int)","use":)sig" + use + R"(,"definition":)" + definition +
|
||||
"}";
|
||||
}
|
||||
|
||||
std::string report(std::string_view occurrences, bool complete = true, bool partial = false,
|
||||
std::string_view reason = "", std::string_view selected = nonce) {
|
||||
return R"({"schema":1,"nonce":")" + std::string{selected} + R"(","complete":)" +
|
||||
(complete ? "true" : "false") + R"(,"partial":)" + (partial ? "true" : "false") +
|
||||
R"(,"reason":")" + std::string{reason} + R"(","occurrences":[)" +
|
||||
std::string{occurrences} + "]}";
|
||||
}
|
||||
|
||||
auto decode(std::string_view text, std::span<const CapturedSource> entries) {
|
||||
return decode_occurrences(text, nonce, entries);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
constexpr std::string_view source = "int f(int v) { return v; }\nint q() { return f(1); }\n";
|
||||
|
||||
VOID_TEST(Occurrences, binds_use_and_definition) {
|
||||
auto entries = capture(std::string{source});
|
||||
const auto call = span(source, "f", 1), declaration = span(source, "f", 0);
|
||||
auto decoded =
|
||||
decode(report(occurrence("f", "int", span_json(path, call), span_json(path, declaration))),
|
||||
entries);
|
||||
require(decoded.has_value());
|
||||
require(decoded->complete && !decoded->partial);
|
||||
require(decoded->occurrences.size() == 1);
|
||||
const auto &fact = decoded->occurrences.front();
|
||||
expect(fact.name == "f" && fact.type == "int" && fact.canonical_type == "int");
|
||||
expect(fact.signature == "int f(int)");
|
||||
require(fact.has_definition());
|
||||
const auto offset = entries.front().snapshot->position_to_offset(fact.use.start);
|
||||
require(offset.has_value());
|
||||
expect(entries.front().snapshot->text.substr(*offset, 1) == "f");
|
||||
}
|
||||
|
||||
VOID_TEST(Occurrences, rejects_wrong_nonce) {
|
||||
auto entries = capture(std::string{source});
|
||||
const auto call = span(source, "f", 1);
|
||||
auto decoded = decode(report(occurrence("f", "int", span_json(path, call), "null"), true, false,
|
||||
"", "ffffffffffffffffffffffffffffffff"),
|
||||
entries);
|
||||
require(!decoded.has_value());
|
||||
expect(decoded.error() == OccurrenceError::wrong_nonce);
|
||||
}
|
||||
|
||||
VOID_TEST(Occurrences, rejects_unknown_path) {
|
||||
auto entries = capture(std::string{source});
|
||||
const auto call = span(source, "f", 1);
|
||||
auto decoded = decode(
|
||||
report(occurrence("f", "int", span_json("/workspace/other.cpp", call), "null")), entries);
|
||||
require(!decoded.has_value());
|
||||
expect(decoded.error() == OccurrenceError::invalid_report);
|
||||
}
|
||||
|
||||
VOID_TEST(Occurrences, rejects_mismatched_bytes) {
|
||||
auto entries = capture(std::string{source});
|
||||
auto call = span(source, "f", 1);
|
||||
++call.start;
|
||||
auto decoded = decode(report(occurrence("f", "int", span_json(path, call), "null")), entries);
|
||||
require(!decoded.has_value());
|
||||
expect(decoded.error() == OccurrenceError::invalid_report);
|
||||
}
|
||||
|
||||
VOID_TEST(Occurrences, incomplete_publishes_no_facts) {
|
||||
auto entries = capture(std::string{source});
|
||||
const auto call = span(source, "f", 1);
|
||||
auto decoded = decode(
|
||||
report(occurrence("f", "int", span_json(path, call), "null"), false, false, "capacity"),
|
||||
entries);
|
||||
require(decoded.has_value());
|
||||
expect(!decoded->complete && decoded->reason == "capacity" && decoded->occurrences.empty());
|
||||
}
|
||||
|
||||
VOID_TEST(Occurrences, converts_through_the_negotiated_encoding) {
|
||||
const std::string text = "int q(){ /*\xF0\x9F\x99\x82*/ return f(1); }\n";
|
||||
for (const auto encoding : {D::PositionEncoding::utf16, D::PositionEncoding::utf8}) {
|
||||
auto entries = capture(text, encoding);
|
||||
const auto call = span(text, "f");
|
||||
auto decoded =
|
||||
decode(report(occurrence("f", "int", span_json(path, call), "null")), entries);
|
||||
require(decoded.has_value());
|
||||
const auto &fact = decoded->occurrences.front();
|
||||
const auto prefix = text.substr(0, text.find('f'));
|
||||
const auto expected = encoding == D::PositionEncoding::utf16
|
||||
? prefix.size() - 4 + 2 // emoji: 4 bytes, 2 UTF-16 units
|
||||
: prefix.size();
|
||||
expect(fact.use.start.character == expected);
|
||||
}
|
||||
}
|
||||
|
||||
VOID_TEST(Occurrences, rejects_duplicate_uses) {
|
||||
auto entries = capture(std::string{source});
|
||||
const auto call = span(source, "f", 1);
|
||||
const auto item = occurrence("f", "int", span_json(path, call), "null");
|
||||
auto decoded = decode(report(item + "," + item), entries);
|
||||
require(!decoded.has_value());
|
||||
expect(decoded.error() == OccurrenceError::invalid_report);
|
||||
}
|
||||
|
||||
VOID_TEST(Occurrences, withholds_a_definition_outside_the_capture) {
|
||||
auto entries = capture(std::string{source});
|
||||
const auto call = span(source, "f", 1);
|
||||
auto decoded = decode(report(occurrence("f", "int", span_json(path, call),
|
||||
span_json("/workspace/header.hpp", call))),
|
||||
entries);
|
||||
require(decoded.has_value());
|
||||
require(decoded->occurrences.size() == 1);
|
||||
expect(!decoded->occurrences.front().has_definition());
|
||||
}
|
||||
|
||||
VOID_TEST(Occurrences, rejects_a_malformed_envelope) {
|
||||
auto entries = capture(std::string{source});
|
||||
const auto call = span(source, "f", 1);
|
||||
auto extra = report(occurrence("f", "int", span_json(path, call), "null"));
|
||||
extra.insert(extra.size() - 1, R"(,"extra":1)");
|
||||
require(!decode(extra, entries).has_value());
|
||||
require(!decode("{\"schema\":1}", entries).has_value());
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
#include <vls/semantic/SourceCatalog.hpp>
|
||||
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <fstream>
|
||||
#include <sys/inotify.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace {
|
||||
namespace fs = std::filesystem;
|
||||
namespace D = Void::LanguageServer::Documents;
|
||||
using namespace vls::semantic;
|
||||
using Void::Testing::expect;
|
||||
using Void::Testing::require;
|
||||
|
||||
struct Files {
|
||||
Void::FileSystem::TempDir temp{"vls_source_catalog_"};
|
||||
fs::path root = fs::canonical(temp.path()) / "workspace";
|
||||
fs::path external = root.parent_path() / "external";
|
||||
SourceScope scope{{root, "workspace-1"}, 3, 5};
|
||||
D::DocumentStore documents;
|
||||
SourceCatalog catalog;
|
||||
InstalledSourceGrant grant;
|
||||
|
||||
explicit Files(std::function<void()> hook = {}, D::DocumentLimits limits = {})
|
||||
: documents(D::PositionEncoding::utf16, limits), catalog(documents, std::move(hook)) {
|
||||
fs::create_directories(root / "nested");
|
||||
fs::create_directory(external);
|
||||
grant = install({root});
|
||||
}
|
||||
InstalledSourceGrant install(std::initializer_list<fs::path> roots) {
|
||||
auto result = catalog.install(scope, roots);
|
||||
require(result.has_value());
|
||||
return *result;
|
||||
}
|
||||
D::FileOverlayInventory census() const {
|
||||
auto result = documents.file_overlay_inventory();
|
||||
require(result.has_value());
|
||||
return std::move(*result);
|
||||
}
|
||||
auto take(std::initializer_list<SourceRequest> requests, SourceLimits limits = {}) {
|
||||
return catalog.capture(scope, grant, census(), requests, limits);
|
||||
}
|
||||
static void write(const fs::path &path, std::string_view text) {
|
||||
std::ofstream out(path, std::ios::binary);
|
||||
out << text;
|
||||
out.close();
|
||||
require(out.good());
|
||||
}
|
||||
auto open(const fs::path &path, std::string text) {
|
||||
return documents.open(D::path_to_file_uri(path), "cpp", 1, std::move(text));
|
||||
}
|
||||
};
|
||||
|
||||
void failed(const auto &result, SourceIssue issue) {
|
||||
require(!result.has_value());
|
||||
expect(result.error().issue == issue);
|
||||
}
|
||||
|
||||
class AccessWatch {
|
||||
Void::System::OwnedFd events_{::inotify_init1(IN_NONBLOCK | IN_CLOEXEC)};
|
||||
|
||||
public:
|
||||
explicit AccessWatch(const fs::path &path) {
|
||||
require(events_ && ::inotify_add_watch(events_.get(), path.c_str(), IN_ACCESS) >= 0);
|
||||
}
|
||||
bool accessed() const {
|
||||
alignas(inotify_event) std::array<char, 4096> buffer;
|
||||
auto n = ::read(events_.get(), buffer.data(), buffer.size());
|
||||
require(n > 0 || (n < 0 && errno == EAGAIN));
|
||||
return n > 0;
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
VOID_TEST(SourceCatalogTests, lookup_captures_directories_and_regular_presence_without_reading) {
|
||||
Files f;
|
||||
const auto dir = f.root / "nested", file = dir / "input.hpp";
|
||||
Files::write(file, "one");
|
||||
AccessWatch watch(file);
|
||||
auto capture = f.take({{f.root, true, true}, {dir, true, true}, {file, true, true}});
|
||||
require(capture.has_value());
|
||||
const auto entries = (*capture)->entries();
|
||||
expect(entries[0].node == SourceNode::directory && entries[1].node == SourceNode::directory);
|
||||
expect(entries[2].node == SourceNode::regular && !entries[2].snapshot);
|
||||
expect(!watch.accessed());
|
||||
Files::write(dir / "unrelated.hpp", "");
|
||||
expect(f.catalog.validate(f.scope, **capture).has_value());
|
||||
Files::write(file, "two");
|
||||
failed(f.catalog.validate(f.scope, **capture), SourceIssue::changed);
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, first_missing_directory_invalidates_deeper_optional_lookup) {
|
||||
Files f;
|
||||
const auto dir = f.root / "future", file = dir / "deeper" / "optional.hpp";
|
||||
auto capture = f.take({{file, true, true}});
|
||||
require(capture.has_value());
|
||||
expect((*capture)->entries().front().node == SourceNode::missing);
|
||||
fs::create_directory(dir);
|
||||
failed(f.catalog.validate(f.scope, **capture), SourceIssue::changed);
|
||||
failed(f.take({{file, true}}), SourceIssue::missing_parent);
|
||||
require(f.open(file, "unsaved").has_value());
|
||||
failed(f.take({{file, true, true}}), SourceIssue::missing_parent);
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, captures_disk_and_unsaved_source_in_request_order) {
|
||||
Files f;
|
||||
const auto source = f.root / "source.cpp", header = f.root / "nested" / "α.hpp";
|
||||
Files::write(source, "saved");
|
||||
Files::write(header, "disk header");
|
||||
auto overlay = f.open(header, "");
|
||||
require(overlay.has_value());
|
||||
AccessWatch watch(header);
|
||||
auto cap = f.take({{header}, {source}, {header}});
|
||||
require(cap.has_value());
|
||||
auto entries = (*cap)->entries();
|
||||
require(entries.size() == 3);
|
||||
expect(entries[0].snapshot == *overlay && entries[2].snapshot == *overlay);
|
||||
expect(entries[1].snapshot->text == "saved");
|
||||
expect(entries[1].snapshot->origin == D::DocumentOrigin::disk);
|
||||
expect(entries[0].logical_path == header &&
|
||||
entries[0].snapshot->identity.canonical_path == header);
|
||||
expect(!watch.accessed());
|
||||
expect(f.catalog.validate(f.scope, **cap).has_value());
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, absent_optional_leaf_becomes_an_unsaved_then_a_saved_header) {
|
||||
Files f;
|
||||
auto header = f.root / "absent.hpp";
|
||||
failed(f.take({{header}}), SourceIssue::missing);
|
||||
auto old = f.take({{header, true}});
|
||||
require(old.has_value());
|
||||
expect(!(*old)->entries()[0].snapshot);
|
||||
require(f.open(header, "new overlay").has_value());
|
||||
failed(f.catalog.validate(f.scope, **old), SourceIssue::changed);
|
||||
auto unsaved = f.take({{header}});
|
||||
require(unsaved.has_value());
|
||||
expect((*unsaved)->entries()[0].snapshot->text == "new overlay");
|
||||
require(f.documents.close(D::path_to_file_uri(header)).has_value());
|
||||
auto absent = f.take({{header, true}});
|
||||
require(absent.has_value());
|
||||
Files::write(header, "new disk");
|
||||
failed(f.catalog.validate(f.scope, **absent), SourceIssue::changed);
|
||||
fs::remove(header);
|
||||
failed(f.catalog.validate(f.scope, **absent), SourceIssue::changed);
|
||||
expect((*unsaved)->entries()[0].snapshot->text == "new overlay");
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, binds_job_and_installed_grants_before_reading_shared_files) {
|
||||
Files f;
|
||||
const auto path = f.external / "shared.hpp";
|
||||
Files::write(path, "shared");
|
||||
const auto first = f.install({f.root, f.external});
|
||||
SourceScope other{{f.root, "another-incarnation"}, f.scope.configuration, f.scope.trust};
|
||||
const auto requests = std::array{SourceRequest{path}};
|
||||
AccessWatch watch(path);
|
||||
auto attempt = [&](const SourceScope &scope, const InstalledSourceGrant &grant) {
|
||||
return f.catalog.capture(scope, grant, f.census(), requests);
|
||||
};
|
||||
failed(attempt(other, first), SourceIssue::wrong_authority);
|
||||
auto old_settings = f.scope;
|
||||
++old_settings.configuration;
|
||||
failed(attempt(old_settings, first), SourceIssue::wrong_authority);
|
||||
auto old_trust = f.scope;
|
||||
++old_trust.trust;
|
||||
failed(attempt(old_trust, first), SourceIssue::wrong_authority);
|
||||
f.catalog.revoke();
|
||||
const auto renewed = f.install({f.root, f.external});
|
||||
failed(attempt(f.scope, first), SourceIssue::wrong_authority);
|
||||
expect(!watch.accessed());
|
||||
require(attempt(f.scope, renewed).has_value());
|
||||
expect(watch.accessed());
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, retains_the_callers_empty_census_and_only_the_selected_text) {
|
||||
Files f;
|
||||
const auto tu = f.root / "tu.cpp", header = f.root / "new.hpp";
|
||||
Files::write(tu, "tu");
|
||||
const auto before = f.census();
|
||||
auto overlay = f.open(header, "unsaved");
|
||||
require(overlay.has_value());
|
||||
std::weak_ptr<const D::DocumentSnapshot> unrelated = *overlay;
|
||||
overlay->reset();
|
||||
auto requests = std::array{SourceRequest{tu}};
|
||||
AccessWatch watch(tu);
|
||||
failed(f.catalog.capture(f.scope, f.grant, before, requests), SourceIssue::changed);
|
||||
expect(!watch.accessed());
|
||||
auto selected = f.take({{tu}});
|
||||
require(selected.has_value());
|
||||
require(f.documents.close(D::path_to_file_uri(header)).has_value());
|
||||
expect(unrelated.expired());
|
||||
failed(f.catalog.validate(f.scope, **selected), SourceIssue::changed);
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, refuses_a_desynchronized_or_refused_overlay_without_reading_disk) {
|
||||
D::DocumentLimits limits;
|
||||
limits.max_open_documents = 1;
|
||||
Files f{{}, limits};
|
||||
const auto a = f.root / "a.cpp", b = f.root / "b.cpp";
|
||||
Files::write(a, "disk-a");
|
||||
Files::write(b, "disk-b");
|
||||
require(f.open(a, "open").has_value());
|
||||
require(!f.open(b, "refused").has_value());
|
||||
AccessWatch first(a), second(b);
|
||||
require(f.documents
|
||||
.mark_desynchronized(D::path_to_file_uri(a), 2,
|
||||
{D::DocumentError::Kind::malformed_change, "bad edit"})
|
||||
.has_value());
|
||||
failed(f.take({{a}}), SourceIssue::unavailable_overlay);
|
||||
failed(f.take({{b}}), SourceIssue::unavailable_overlay);
|
||||
expect(!first.accessed() && !second.accessed());
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, rejects_another_editor_stores_census_before_disk_access) {
|
||||
Files f;
|
||||
const auto source = f.root / "edited.cpp";
|
||||
Files::write(source, "disk");
|
||||
require(f.open(source, "editor").has_value());
|
||||
D::DocumentStore different_editor;
|
||||
auto empty = different_editor.file_overlay_inventory();
|
||||
require(empty.has_value());
|
||||
AccessWatch watch(source);
|
||||
const std::array requests{SourceRequest{source}};
|
||||
failed(f.catalog.capture(f.scope, f.grant, *empty, requests), SourceIssue::changed);
|
||||
auto correct = f.take({{source}});
|
||||
require(correct.has_value());
|
||||
expect((*correct)->entries()[0].snapshot->text == "editor");
|
||||
expect(!watch.accessed());
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, refuses_two_open_uri_aliases_for_one_physical_source) {
|
||||
Files f;
|
||||
auto path = f.root / "target.cpp", alias = f.root / "alias.cpp";
|
||||
Files::write(path, "disk");
|
||||
fs::create_symlink(path, alias);
|
||||
require(f.open(path, "one").has_value());
|
||||
require(f.open(alias, "two").has_value());
|
||||
failed(f.take({{path}}), SourceIssue::unavailable_overlay);
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, invalidates_identical_bytes_on_leaf_and_directory_replacement) {
|
||||
Files f;
|
||||
auto parent = f.root / "nested", path = parent / "a.cpp";
|
||||
Files::write(path, "stable");
|
||||
f.grant = f.install({f.root, parent, f.external});
|
||||
auto old = f.take({{path}});
|
||||
require(old.has_value());
|
||||
Files::write(parent / "new", "stable");
|
||||
fs::rename(parent / "new", path);
|
||||
failed(f.catalog.validate(f.scope, **old), SourceIssue::changed);
|
||||
auto fresh = f.take({{path}});
|
||||
require(fresh.has_value());
|
||||
fs::rename(parent, f.root / "old");
|
||||
fs::create_directory(parent);
|
||||
Files::write(path, "new");
|
||||
failed(f.catalog.validate(f.scope, **fresh), SourceIssue::changed);
|
||||
failed(f.take({{path}}), SourceIssue::changed);
|
||||
expect((*old)->entries()[0].snapshot->text == "stable");
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, replacement_of_the_workspace_invalidates_an_external_only_capture) {
|
||||
Files f;
|
||||
f.grant = f.install({f.external});
|
||||
const auto path = f.external / "header.hpp";
|
||||
Files::write(path, "header");
|
||||
auto old = f.take({{path}});
|
||||
require(old.has_value());
|
||||
fs::rename(f.root, f.external.parent_path() / "old-workspace");
|
||||
fs::create_directory(f.root);
|
||||
failed(f.catalog.validate(f.scope, **old), SourceIssue::changed);
|
||||
failed(f.take({{path}}), SourceIssue::changed);
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, refuses_hard_links_and_special_files_before_reading) {
|
||||
Files f;
|
||||
const auto primary = f.root / "public.hpp", other = f.root / "impl.hpp";
|
||||
Files::write(primary, "stale");
|
||||
fs::create_hard_link(primary, other);
|
||||
require(f.open(other, "").has_value());
|
||||
AccessWatch watch(primary);
|
||||
failed(f.take({{primary}}), SourceIssue::hard_link);
|
||||
expect(!watch.accessed());
|
||||
const auto fifo = f.root / "fifo";
|
||||
require(::mkfifo(fifo.c_str(), 0600) == 0);
|
||||
failed(f.take({{fifo}}), SourceIssue::not_regular);
|
||||
failed(f.take({{f.root / "nested"}}), SourceIssue::not_regular);
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, refuses_unsupported_and_outside_routes_before_source_reads) {
|
||||
Files f;
|
||||
auto outside = f.external / "secret.hpp", link = f.root / "leaf", dir = f.root / "dir";
|
||||
Files::write(outside, "benign external fixture");
|
||||
fs::create_symlink(outside, link);
|
||||
fs::create_directory_symlink(f.external, dir);
|
||||
AccessWatch watch(outside);
|
||||
failed(f.take({{outside}}), SourceIssue::outside_scope);
|
||||
failed(f.take({{link, true}}), SourceIssue::unsupported_route);
|
||||
failed(f.take({{dir / "secret.hpp", true}}), SourceIssue::unsupported_route);
|
||||
failed(f.take({{f.root / "nested" / ".." / "leaf"}}), SourceIssue::invalid_path);
|
||||
failed(f.take({{f.root / "missing" / "a.hpp", true}}), SourceIssue::missing_parent);
|
||||
expect(!watch.accessed());
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests,
|
||||
rejects_conflicting_ancestor_and_descendant_file_names_in_either_order) {
|
||||
Files f;
|
||||
auto parent = f.root / "generated", child = parent / "config.hpp";
|
||||
require(f.open(child, "unsaved").has_value());
|
||||
failed(f.take({{parent, true}, {child}}), SourceIssue::conflicting_names);
|
||||
failed(f.take({{child}, {parent, true}}), SourceIssue::conflicting_names);
|
||||
require(f.open(parent, "separate file").has_value());
|
||||
failed(f.take({{parent}, {child}}), SourceIssue::conflicting_names);
|
||||
failed(f.take({{child}, {parent}}), SourceIssue::conflicting_names);
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, validates_authority_editor_and_disk_again_after_capture) {
|
||||
std::function<void()> change;
|
||||
Files f{[&] {
|
||||
if (change)
|
||||
change();
|
||||
}};
|
||||
const auto path = f.root / "a.cpp";
|
||||
Files::write(path, "before");
|
||||
change = [&] { Files::write(path, "after"); };
|
||||
failed(f.take({{path}}), SourceIssue::changed);
|
||||
change = [&] { require(f.open(path, "now unsaved").has_value()); };
|
||||
failed(f.take({{path}}), SourceIssue::changed);
|
||||
change = [&] {
|
||||
std::array edits{D::TextEdit{std::nullopt, "later"}};
|
||||
require(f.documents.apply_changes(D::path_to_file_uri(path), 2, edits).has_value());
|
||||
};
|
||||
failed(f.take({{path}}), SourceIssue::changed);
|
||||
change = [&] { f.catalog.revoke(); };
|
||||
failed(f.take({{path}}), SourceIssue::wrong_authority);
|
||||
}
|
||||
|
||||
VOID_TEST(SourceCatalogTests, enforces_count_bytes_descriptors_and_retained_capture_limits) {
|
||||
Files f;
|
||||
auto a = f.root / "a.cpp", b = f.root / "nested" / "b.hpp";
|
||||
Files::write(a, "abc");
|
||||
Files::write(b, "defg");
|
||||
failed(f.take({{a}, {b}}, {.files = 1}), SourceIssue::resource_limit);
|
||||
failed(f.take({{a}}, {.name_bytes = 1}), SourceIssue::resource_limit);
|
||||
failed(f.take({{a}}, {.file_bytes = 2}), SourceIssue::resource_limit);
|
||||
failed(f.take({{a}, {b}}, {.total_bytes = 6}), SourceIssue::resource_limit);
|
||||
failed(f.take({{b}}, {.descriptors = 1}), SourceIssue::resource_limit);
|
||||
require(f.take({{a}, {b}}, {.total_bytes = 7}).has_value());
|
||||
std::vector<std::shared_ptr<const SourceCapture>> retained;
|
||||
for (unsigned i = 0; i < 63; ++i) {
|
||||
auto cap = f.take({{a}});
|
||||
require(cap.has_value());
|
||||
retained.push_back(std::move(*cap));
|
||||
}
|
||||
failed(f.take({{a}}), SourceIssue::resource_limit);
|
||||
retained.pop_back();
|
||||
expect(f.take({{a}}).has_value());
|
||||
}
|
||||
Reference in new issue
Block a user