#include #include #include #include #include #include #include #include #include #include #include #include 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 chunk; while (stream.read(chunk.data(), chunk.size()) || stream.gcount()) { const auto count = static_cast(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 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 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(result->compiler_runs)}}, {"lookups", P::Json{static_cast(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 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 args{argv + 1, static_cast(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(); 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 { 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(status), true, report}); if (!batch) { std::cerr << "diagnostic adapter rejected report (" << static_cast(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; } }