Files
Language-Server/tests/reflection/ProvenanceTests.cpp
T
faumaray da8003f6d4 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.
2026-12-05 14:06:50 +03:00

497 lines
23 KiB
C++

#include <vls/reflection/Provenance.hpp>
#include <Void/LanguageServer/Protocol/Json.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <algorithm>
#include <string>
#include <vector>
namespace {
namespace P = Void::LanguageServer::Protocol;
using namespace vls::reflection;
using Void::Testing::expect;
using Void::Testing::require;
Range range(std::string path, std::uint32_t line = 1) {
return {std::move(path), line, 0, line, 4};
}
Value integral(std::int64_t value, std::string type = "int") {
return Value{.kind = ValueKind::integral, .integral = value, .type = std::move(type)};
}
Value reference(std::string entity, std::string type = "State") {
return Value{.kind = ValueKind::entity, .type = std::move(type), .entity = std::move(entity)};
}
Member member(std::string name, Value value) { return {std::move(name), std::move(value)}; }
Entity entity(std::string id, std::vector<Member> members = {}) {
return Entity{
.id = std::move(id), .name = "entity", .type = "int", .members = std::move(members)};
}
Provenance chain(std::vector<Origin> origins, bool gap = false, std::string context = "debug:a") {
return Provenance{.origins = std::move(origins), .gap = gap, .context = std::move(context)};
}
const Expansion::Entry *entry(const Expansion &expansion, std::string_view path) {
const auto found = std::ranges::find_if(
expansion.entries, [&](const Expansion::Entry &item) { return item.path == path; });
return found == expansion.entries.end() ? nullptr : &*found;
}
} // namespace
VOID_TEST(Reflection, spelling_chain_is_editable) {
const auto provenance =
chain({Origin{.kind = OriginKind::spelling, .range = range("/project/a.cpp")}});
expect(provenance.navigable());
expect(provenance.editable());
}
VOID_TEST(Reflection, generated_chain_is_navigable_but_not_editable) {
const auto synthesized = chain(
{Origin{.kind = OriginKind::splice, .range = range("/project/a.cpp")},
Origin{.kind = OriginKind::synthesized},
Origin{.kind = OriginKind::reflected_declaration, .range = range("/project/a.cpp", 9)}});
expect(synthesized.navigable());
expect(!synthesized.editable());
const auto gapped =
chain({Origin{.kind = OriginKind::spelling, .range = range("/project/a.cpp")}}, true);
expect(gapped.navigable() && !gapped.editable());
expect(!Provenance{}.editable());
expect(!Provenance{}.navigable());
}
VOID_TEST(Reflection, expansion_projects_scalars_with_types) {
Snapshot snapshot;
require(snapshot.add(
entity("root",
{member("x", integral(17)),
member("flag", Value{.kind = ValueKind::boolean, .boolean = true, .type = "bool"}),
member("label", Value{.kind = ValueKind::text, .text = "hi", .type = "string"})})));
const auto expansion = snapshot.expand("root");
require(expansion.has_value());
expect(expansion->entries.size() == 3);
const auto *x = entry(*expansion, "x");
require(x != nullptr);
expect(x->kind == ValueKind::integral && x->text == "17" && x->type == "int" && x->depth == 1);
const auto *flag = entry(*expansion, "flag");
require(flag != nullptr);
expect(flag->text == "true");
const auto *label = entry(*expansion, "label");
require(label != nullptr);
expect(label->text == "hi" && label->type == "string");
expect(expansion->complete());
}
VOID_TEST(Reflection, expansion_follows_entity_members) {
Snapshot snapshot;
require(snapshot.add(entity("inner", {member("value", integral(34))})));
require(snapshot.add(entity("outer", {member("child", reference("inner"))})));
const auto expansion = snapshot.expand("outer");
require(expansion.has_value());
const auto *child = entry(*expansion, "child");
require(child != nullptr);
expect(child->kind == ValueKind::entity && child->entity == "inner" && child->depth == 1);
const auto *value = entry(*expansion, "child.value");
require(value != nullptr);
expect(value->text == "34" && value->depth == 2);
}
VOID_TEST(Reflection, expansion_marks_cycles) {
Snapshot snapshot;
require(
snapshot.add(entity("a", {member("self", reference("a")), member("b", reference("b"))})));
require(
snapshot.add(entity("b", {member("back", reference("a")), member("value", integral(1))})));
const auto expansion = snapshot.expand("a");
require(expansion.has_value());
expect(std::ranges::find(expansion->cycles, std::string{"self"}) != expansion->cycles.end());
expect(std::ranges::find(expansion->cycles, std::string{"b.back"}) != expansion->cycles.end());
expect(!expansion->complete());
require(entry(*expansion, "b.value") != nullptr);
}
VOID_TEST(Reflection, expansion_bounds_depth) {
Snapshot snapshot{Limits{.expansionDepth = 1}};
require(snapshot.add(entity("a", {member("next", reference("b"))})));
require(snapshot.add(entity("b", {member("value", integral(2))})));
const auto shallow = snapshot.expand("a");
require(shallow.has_value());
require(entry(*shallow, "next") != nullptr);
expect(entry(*shallow, "next.value") == nullptr);
expect(std::ranges::find(shallow->truncated, std::string{"next"}) != shallow->truncated.end());
const auto deep = snapshot.expand("a", 2);
require(deep.has_value());
require(entry(*deep, "next.value") != nullptr);
expect(deep->truncated.empty());
}
VOID_TEST(Reflection, expansion_bounds_total_values) {
Snapshot snapshot{Limits{.expansionValues = 2}};
require(snapshot.add(entity(
"root", {member("a", integral(1)), member("b", integral(2)), member("c", integral(3))})));
const auto expansion = snapshot.expand("root");
require(expansion.has_value());
expect(expansion->entries.size() == 2);
expect(std::ranges::find(expansion->truncated, std::string{"c"}) != expansion->truncated.end());
}
VOID_TEST(Reflection, expansion_preserves_opaque_and_unavailable_values) {
Snapshot snapshot;
require(snapshot.add(
entity("root", {member("opaque", Value{.kind = ValueKind::opaque,
.text = "the compiler cannot render this value",
.type = "widget"}),
member("missing", Value{.kind = ValueKind::unavailable,
.text = "not evaluated in this context"})})));
const auto expansion = snapshot.expand("root");
require(expansion.has_value());
const auto *opaque = entry(*expansion, "opaque");
require(opaque != nullptr);
expect(opaque->kind == ValueKind::opaque &&
opaque->text == "the compiler cannot render this value");
const auto *missing = entry(*expansion, "missing");
require(missing != nullptr);
expect(missing->kind == ValueKind::unavailable &&
missing->text == "not evaluated in this context");
}
VOID_TEST(Reflection, expansion_reports_dangling_and_unknown_entities) {
Snapshot snapshot;
require(snapshot.add(entity("root", {member("dangling", reference("absent"))})));
const auto expansion = snapshot.expand("root");
require(expansion.has_value());
const auto *dangling = entry(*expansion, "dangling");
require(dangling != nullptr);
expect(dangling->kind == ValueKind::unavailable);
expect(dangling->text == "entity is not in this snapshot");
expect(!snapshot.expand("absent").has_value());
}
VOID_TEST(Reflection, snapshot_rejects_duplicate_and_oversized_entities) {
Snapshot snapshot{Limits{.entities = 2, .membersPerEntity = 1}};
expect(snapshot.add(entity("a")));
expect(!snapshot.add(entity("a")));
expect(!snapshot.add(entity("b", {member("x", integral(1)), member("y", integral(2))})));
expect(!snapshot.add(Entity{}));
expect(snapshot.add(entity("c")));
expect(!snapshot.add(entity("d")));
}
VOID_TEST(Reflection, serialization_round_trip) {
Snapshot snapshot;
Entity node{.id = "node", .name = "member", .type = "int"};
node.members = {member("next", reference("other")), member("value", integral(41))};
node.provenance =
chain({Origin{.kind = OriginKind::spelling, .range = range("/project/a.cpp", 3)},
Origin{.kind = OriginKind::reflected_declaration,
.range = range("/project/a.cpp", 9),
.entity = "other",
.note = "P2996"}},
false, "debug:unit");
node.synthesized = true;
require(snapshot.add(std::move(node)));
require(snapshot.add(entity("other", {member("value", integral(82))})));
const auto encoded = snapshot.serialize();
require(encoded.has_value());
const auto parsed = Snapshot::parse(*encoded);
require(parsed.has_value());
require(parsed->size() == snapshot.size());
const auto *original = snapshot.find("node");
const auto *restored = parsed->find("node");
require(original != nullptr && restored != nullptr);
expect(*original == *restored);
const auto before = snapshot.expand("node");
const auto after = parsed->expand("node");
require(before.has_value() && after.has_value());
expect(before->entries == after->entries);
expect(after->cycles.empty());
}
VOID_TEST(Reflection, serialization_is_canonical_across_insertion_order) {
Entity a = entity("a", {member("x", integral(1))});
Entity b = entity("b", {member("y", integral(2))});
Snapshot first;
require(first.add(a));
require(first.add(b));
Snapshot second;
require(second.add(b));
require(second.add(a));
const auto left = first.serialize();
const auto right = second.serialize();
require(left.has_value() && right.has_value());
expect(*left == *right);
}
VOID_TEST(Reflection, parse_rejects_unknown_schema_and_corruption) {
Snapshot snapshot;
require(snapshot.add(entity("a")));
auto encoded = snapshot.serialize();
require(encoded.has_value());
auto future = *encoded;
const auto marker = future.find("\"schema\":1");
require(marker != std::string::npos);
future.replace(marker, 10, "\"schema\":2");
expect(!Snapshot::parse(future).has_value());
expect(!Snapshot::parse("not json").has_value());
expect(!Snapshot::parse("{}").has_value());
}
VOID_TEST(Reflection, parse_rejects_oversized_and_duplicate_input) {
Snapshot snapshot;
require(snapshot.add(entity("a")));
require(snapshot.add(entity("b")));
const auto encoded = snapshot.serialize();
require(encoded.has_value());
expect(!Snapshot::parse(*encoded, Limits{.snapshotBytes = 16}).has_value());
auto duplicate = *encoded;
const auto second = duplicate.find("\"id\":\"b\"");
require(second != std::string::npos);
duplicate.replace(second, 8, "\"id\":\"a\"");
expect(!Snapshot::parse(duplicate).has_value());
}
VOID_TEST(Reflection, capabilities_report_known_support) {
Capabilities capabilities;
require(capabilities.add(
Capability{.proposal = "P2996", .facility = "splices", .supported = true}));
const auto splices = capabilities.query("P2996", "splices");
require(splices.has_value());
expect(splices->supported);
expect(capabilities.size() == 1);
expect(!capabilities.add(Capability{
.proposal = "P2996", .facility = "splices", .supported = false, .reason = "duplicate"}));
}
VOID_TEST(Reflection, capabilities_unknown_is_an_explicit_error) {
Capabilities capabilities;
require(capabilities.add(Capability{.proposal = "P2996",
.facility = "annotations",
.supported = false,
.reason = "the accepted backend lacks annotations"}));
const auto known = capabilities.query("P2996", "annotations");
require(known.has_value());
expect(!known->supported && !known->reason.empty());
const auto unknown = capabilities.query("P2996", "expansionStatements");
require(!unknown.has_value());
expect(unknown.error().find("unknown reflection capability") != std::string::npos);
expect(!capabilities.add(
Capability{.proposal = "P2996", .facility = "staticAssert", .supported = false}));
}
VOID_TEST(Reflection, virtual_view_renders_entity_and_members) {
Snapshot snapshot;
require(snapshot.add(
entity("root", {member("x", integral(17)), member("child", reference("inner"))})));
require(snapshot.add(entity("inner", {member("value", integral(34))})));
const auto expansion = snapshot.expand("root");
require(expansion.has_value());
const auto *root = snapshot.find("root");
require(root != nullptr);
const auto view = render(*root, *expansion);
require(!view.lines.empty());
expect(view.lines.front().text == "entity : int");
expect(!view.compilable());
const auto member_line = std::ranges::find_if(view.lines, [](const VirtualLine &line) {
return line.text.find("x : int = 17") != std::string::npos;
});
expect(member_line != view.lines.end());
const auto entity_line = std::ranges::find_if(view.lines, [](const VirtualLine &line) {
return line.text.find("<entity inner>") != std::string::npos;
});
expect(entity_line != view.lines.end());
expect(entity_line->text.starts_with(" "));
}
VOID_TEST(Reflection, virtual_view_links_source_ranges) {
Snapshot snapshot;
Entity inner = entity("inner");
inner.provenance =
chain({Origin{.kind = OriginKind::spelling, .range = range("/p/inner.cpp", 5)}});
Entity outer = entity("outer", {member("child", reference("inner"))});
outer.provenance =
chain({Origin{.kind = OriginKind::spelling, .range = range("/p/outer.cpp", 9)}});
require(snapshot.add(std::move(inner)));
require(snapshot.add(std::move(outer)));
const auto expansion = snapshot.expand("outer");
require(expansion.has_value());
const auto *root = snapshot.find("outer");
require(root != nullptr);
const auto view = render(*root, *expansion);
require(!view.lines.empty());
require(view.lines.front().link.has_value());
expect(view.lines.front().link->path == "/p/outer.cpp" && view.lines.front().link->line == 9);
const auto child = std::ranges::find_if(view.lines, [](const VirtualLine &line) {
return line.text.find("<entity inner>") != std::string::npos;
});
require(child != view.lines.end());
require(child->link.has_value());
expect(child->link->path == "/p/inner.cpp" && child->link->line == 5);
}
VOID_TEST(Reflection, virtual_view_marks_cycles_and_truncation_explicitly) {
Snapshot snapshot;
require(snapshot.add(
entity("a", {member("self", reference("a")), member("next", reference("b"))})));
require(snapshot.add(entity("b", {member("value", integral(1))})));
const auto expansion = snapshot.expand("a", 1);
require(expansion.has_value());
const auto *root = snapshot.find("a");
require(root != nullptr);
const auto view = render(*root, *expansion);
const auto cycle = std::ranges::find_if(
view.lines, [](const VirtualLine &line) { return line.text.starts_with("! cycle: self"); });
expect(cycle != view.lines.end());
const auto truncated = std::ranges::find_if(view.lines, [](const VirtualLine &line) {
return line.text.starts_with("! truncated: next");
});
expect(truncated != view.lines.end());
}
VOID_TEST(Reflection, virtual_view_preserves_reasons_and_bounds_lines) {
Snapshot snapshot{Limits{.expansionValues = 2}};
require(snapshot.add(entity(
"root", {member("opaque", Value{.kind = ValueKind::opaque, .text = "cannot render"}),
member("missing", Value{.kind = ValueKind::unavailable, .text = "not evaluated"}),
member("extra", integral(3))})));
const auto expansion = snapshot.expand("root");
require(expansion.has_value());
const auto *root = snapshot.find("root");
require(root != nullptr);
const auto view = render(*root, *expansion, Limits{.expansionValues = 2});
expect(view.lines.size() == 3);
expect(view.truncated);
const auto opaque = std::ranges::find_if(view.lines, [](const VirtualLine &line) {
return line.text.find("cannot render") != std::string::npos;
});
expect(opaque != view.lines.end());
const auto missing = std::ranges::find_if(view.lines, [](const VirtualLine &line) {
return line.text.find("not evaluated") != std::string::npos;
});
expect(missing != view.lines.end());
}
VOID_TEST(Reflection, real_compiler_splice_origin_builds_a_provenance_chain) {
// Captured verbatim from `vls_gcc_probe replay` of a sealed session that
// compiled `enum class State { Ready = 17 }; ... [:e:] ...` with the
// reproduced private GCC. The compiler reports a start coordinate only.
constexpr std::string_view recorded =
R"([{"source":"input.cpp","line":4,"byteColumn":31,"targetKind":"declaration","targetName":"Ready","targetSource":"input.cpp","targetLine":1,"targetByteColumn":20}])";
auto parsed = P::parse_json(recorded, 8);
require(parsed.has_value());
const auto *array = P::as_array(&*parsed);
require(array != nullptr);
std::vector<SpliceOrigin> origins;
for (const auto &item : *array) {
const auto *fields = P::as_object(&item);
require(fields != nullptr);
const auto *source = P::as_string(P::find_member(*fields, "source"));
const auto line = P::as_uint32(P::find_member(*fields, "line"));
const auto column = P::as_uint32(P::find_member(*fields, "byteColumn"));
const auto *kind = P::as_string(P::find_member(*fields, "targetKind"));
const auto *name = P::as_string(P::find_member(*fields, "targetName"));
const auto *target_source = P::as_string(P::find_member(*fields, "targetSource"));
const auto target_line = P::as_uint32(P::find_member(*fields, "targetLine"));
const auto target_column = P::as_uint32(P::find_member(*fields, "targetByteColumn"));
require(source && line && column && kind && name && target_source && target_line &&
target_column);
origins.push_back(SpliceOrigin{.source = *source,
.line = *line,
.byteColumn = *column,
.targetKind = *kind,
.targetName = *name,
.targetSource = *target_source,
.targetLine = *target_line,
.targetByteColumn = *target_column});
}
const auto snapshot = snapshot_from_splices(origins);
const auto *entity = snapshot.find("gcc:declaration:Ready@input.cpp:1:20");
require(entity != nullptr);
expect(entity->name == "Ready");
expect(!entity->synthesized);
require(entity->provenance.origins.size() == 2);
expect(entity->provenance.origins[0].kind == OriginKind::splice);
require(entity->provenance.origins[0].range.has_value());
expect(entity->provenance.origins[0].range->line == 4);
expect(entity->provenance.origins[0].range->character == 31);
expect(entity->provenance.origins[1].kind == OriginKind::reflected_declaration);
require(entity->provenance.origins[1].range.has_value());
expect(entity->provenance.origins[1].range->line == 1);
expect(entity->provenance.origins[1].range->character == 20);
expect(entity->provenance.navigable());
expect(!entity->provenance.editable());
expect(!entity->provenance.gap);
const auto expansion = snapshot.expand(entity->id);
require(expansion.has_value());
const auto view = render(*entity, *expansion);
require(!view.lines.empty());
expect(!view.compilable());
}
VOID_TEST(Reflection, splice_without_a_target_location_stays_an_explicit_gap) {
const std::vector<SpliceOrigin> origins{
SpliceOrigin{.source = "/p/a.cpp", .line = 2, .byteColumn = 5, .targetKind = "value"}};
const auto snapshot = snapshot_from_splices(origins);
require(snapshot.size() == 1);
const auto *entity = snapshot.find("gcc:value:<anonymous>@/p/a.cpp:2:5");
require(entity != nullptr);
expect(entity->synthesized);
require(entity->provenance.origins.size() == 1);
expect(entity->provenance.gap);
expect(entity->provenance.navigable() && !entity->provenance.editable());
}
VOID_TEST(Reflection, repeated_splice_targets_keep_one_stable_identity) {
const std::vector<SpliceOrigin> origins{SpliceOrigin{.source = "/p/a.cpp",
.line = 2,
.byteColumn = 5,
.targetKind = "declaration",
.targetName = "f",
.targetSource = "/p/a.cpp",
.targetLine = 1,
.targetByteColumn = 4},
SpliceOrigin{.source = "/p/a.cpp",
.line = 9,
.byteColumn = 7,
.targetKind = "declaration",
.targetName = "f",
.targetSource = "/p/a.cpp",
.targetLine = 1,
.targetByteColumn = 4}};
const auto snapshot = snapshot_from_splices(origins);
expect(snapshot.size() == 1);
const auto *entity = snapshot.find("gcc:declaration:f@/p/a.cpp:1:4");
require(entity != nullptr);
require(entity->provenance.origins.size() == 2);
expect(entity->provenance.origins[0].range->line == 2);
}
VOID_TEST(Reflection, hover_and_locations_expose_source_links) {
Snapshot snapshot;
Entity node = entity("node", {member("value", integral(41))});
node.provenance =
chain({Origin{.kind = OriginKind::splice, .range = range("/p/a.cpp", 4)},
Origin{.kind = OriginKind::reflected_declaration, .range = range("/p/a.cpp", 1)}});
require(snapshot.add(std::move(node)));
const auto *stored = snapshot.find("node");
require(stored != nullptr);
const auto expansion = snapshot.expand("node");
require(expansion.has_value());
const auto hovered = hover(*stored, *expansion);
expect(hovered.markup.find("entity : int") != std::string::npos);
expect(hovered.markup.find("value : int = 41") != std::string::npos);
expect(hovered.markup.find("/p/a.cpp:4:0") != std::string::npos);
expect(hovered.markup.find("/p/a.cpp:1:0") != std::string::npos);
require(hovered.range.has_value());
expect(hovered.range->line == 4);
expect(locations(stored->provenance).size() == 2);
const auto bounded = locations(stored->provenance, 1);
require(bounded.size() == 1);
expect(bounded.front().line == 4);
}