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.
189 lines
7.7 KiB
C++
189 lines
7.7 KiB
C++
#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());
|
|
}
|