Report class and base facts from the stock observer

For each call whose result is a class type, the observer records the
class name span and its direct bases (bounded, deduplicated by uid).
The corpus verifier validates the new types array, and the occurrence
decoder binds each reference to its captured snapshot through the
negotiated encoding, rejecting uncaptured or malformed entries. A
registered decoder case covers the new field. LSP type-hierarchy
handlers remain open.
This commit is contained in:
faumaray committed 2026-12-05 22:56:00 +03:00
1 parent 3344a9f170
commit b1c8a4a625
6 files changed
+240 -4

No files matched your search

+15
View File
@@ -79,6 +79,21 @@ the upgraded snapshot (keeping `snapshot.prev.json`) and rewrites a legacy
about 25-30s in a Debug build (it passed VBS at ~27s); the full CMake run then about 25-30s in a Debug build (it passed VBS at ~27s); the full CMake run then
passed 18/18. passed 18/18.
## December 5 observer-reported type hierarchy facts
The stock observer now records, for each call whose result is a class type, the
class name span and its direct base classes (`types` in the report, bounded to
256 types and 16 bases, deduplicated by type uid). The corpus verifier validates
the shape, and the occurrence decoder binds each reference to its captured
snapshot through the negotiated encoding, rejecting uncaptured or malformed
entries (registered `vls_diagnostic_tests` case
`type_entry_without_bases_decodes`). A real fixture reported
`Derived` at 2:8 with base `Base` at 1:8.
The LSP handlers (`prepareTypeHierarchy`, `supertypes`, `subtypes`) are not
wired yet; they need the occurrence-to-type link and retained per-entry types.
All 18 native suites and all 18 CMake tests passed.
## December 5 formatting through clang-format ## December 5 formatting through clang-format
Per the user's decision, `textDocument/formatting` runs clang-format as a Per the user's decision, `textDocument/formatting` runs clang-format as a
@@ -68,6 +68,22 @@ struct body_entry {
unsigned end_line{}, end_byte{}; unsigned end_line{}, end_byte{};
}; };
constexpr unsigned max_types = 256;
constexpr unsigned max_bases = 16;
struct type_ref {
unsigned uid{};
span where{};
char name[max_name + 1]{};
char path[max_path + 1]{};
};
struct type_entry {
type_ref self;
unsigned base_count{};
type_ref bases[max_bases];
};
struct use_entry { struct use_entry {
unsigned uid{}; unsigned uid{};
span where{}; span where{};
@@ -530,6 +546,18 @@ bool call_token(tree call, tree decl, span &out, const char **path_out) {
return true; return true;
} }
type_entry types[max_types];
unsigned type_count;
type_entry *find_type(unsigned uid) {
for (unsigned i = 0; i != type_count; ++i) {
if (types[i].self.uid == uid) {
return &types[i];
}
}
return nullptr;
}
body_entry *find_body(unsigned uid) { body_entry *find_body(unsigned uid) {
for (unsigned i = 0; i != body_count; ++i) { for (unsigned i = 0; i != body_count; ++i) {
if (bodies[i].uid == uid) { if (bodies[i].uid == uid) {
@@ -574,6 +602,75 @@ void record_body(tree decl) {
entry.definition = true; entry.definition = true;
} }
void fill_type_ref(type_ref &ref, tree type, tree name_decl) {
tree name = DECL_NAME(name_decl);
span where;
const char *path = nullptr;
if (!token_span(DECL_SOURCE_LOCATION(name_decl), IDENTIFIER_LENGTH(name),
IDENTIFIER_POINTER(name), where, &path)) {
return;
}
ref.uid = TYPE_UID(type);
ref.where = where;
std::strcpy(ref.name, IDENTIFIER_POINTER(name));
std::strcpy(ref.path, path);
}
// Records the class of a resolved method and its direct bases. The class name
// span is the declaration's name token, so a hierarchy item always points at
// real source.
void record_type(tree type) {
while (type && TREE_CODE(type) == REFERENCE_TYPE) {
type = TREE_TYPE(type);
}
if (at_capacity || !type || TREE_CODE(type) != RECORD_TYPE || !TYPE_NAME(type) ||
TREE_CODE(TYPE_NAME(type)) != TYPE_DECL || !DECL_NAME(TYPE_NAME(type))) {
return;
}
type_entry *entry = find_type(TYPE_UID(type));
if (!entry) {
if (type_count == max_types) {
at_capacity = true;
return;
}
entry = &types[type_count];
entry->self = {};
entry->base_count = 0;
fill_type_ref(entry->self, type, TYPE_NAME(type));
if (!entry->self.uid) {
return;
}
++type_count;
}
tree binfo = TYPE_BINFO(type);
vec<tree, va_gc> *bases = binfo ? BINFO_BASE_BINFOS(binfo) : nullptr;
if (!bases) {
return;
}
for (unsigned i = 0; i < bases->length() && entry->base_count < max_bases; ++i) {
tree base = BINFO_TYPE((*bases)[i]);
if (!base || !TYPE_NAME(base) || TREE_CODE(TYPE_NAME(base)) != TYPE_DECL ||
!DECL_NAME(TYPE_NAME(base))) {
continue;
}
type_ref candidate{};
fill_type_ref(candidate, base, TYPE_NAME(base));
if (!candidate.uid) {
continue;
}
bool duplicate = false;
for (unsigned j = 0; j != entry->base_count; ++j) {
if (entry->bases[j].uid == candidate.uid) {
duplicate = true;
break;
}
}
if (!duplicate) {
entry->bases[entry->base_count++] = candidate;
}
}
}
void record_use(tree call, tree decl) { void record_use(tree call, tree decl) {
if (at_capacity) { if (at_capacity) {
return; return;
@@ -626,6 +723,13 @@ void record_use(tree call, tree decl) {
IDENTIFIER_LENGTH(DECL_NAME(decl))); IDENTIFIER_LENGTH(DECL_NAME(decl)));
std::strcpy(entry.type, type_text); std::strcpy(entry.type, type_text);
std::strcpy(entry.canonical, canonical_text); std::strcpy(entry.canonical, canonical_text);
tree result_type = call ? TREE_TYPE(call) : NULL_TREE;
if (result_type && TREE_CODE(result_type) == REFERENCE_TYPE) {
result_type = TREE_TYPE(result_type);
}
if (result_type && TYPE_P(result_type)) {
record_type(result_type);
}
const char *signature = decl_as_string(decl, TFF_SCOPE | TFF_RETURN_TYPE); const char *signature = decl_as_string(decl, TFF_SCOPE | TFF_RETURN_TYPE);
if (signature && std::strlen(signature) <= max_signature && utf8_valid(signature)) { if (signature && std::strlen(signature) <= max_signature && utf8_valid(signature)) {
std::strcpy(entry.signature, signature); std::strcpy(entry.signature, signature);
@@ -781,6 +885,32 @@ void finish(void *, void *) {
std::fputc('}', out); std::fputc('}', out);
} }
} }
std::fputs("],\"types\":[", out);
const auto write_ref = [out](const type_ref &ref) {
std::fputs("{\"uid\":", out);
std::fprintf(out, "%u,\"name\":", ref.uid);
json_string(out, ref.name);
std::fputs(",\"path\":", out);
json_string(out, ref.path);
std::fprintf(out, ",\"line\":%u,\"startByte\":%u,\"endByte\":%u}", ref.where.line,
ref.where.start, ref.where.end);
};
for (unsigned i = 0; i != type_count; ++i) {
if (i) {
std::fputc(',', out);
}
const type_entry &entry = types[i];
std::fputs("{\"self\":", out);
write_ref(entry.self);
std::fputs(",\"bases\":[", out);
for (unsigned j = 0; j != entry.base_count; ++j) {
if (j) {
std::fputc(',', out);
}
write_ref(entry.bases[j]);
}
std::fputs("]}", out);
}
std::fputs("]}\n", out); std::fputs("]}\n", out);
if (std::fflush(out) != 0 || std::ferror(out) || ::fsync(fd) != 0) { if (std::fflush(out) != 0 || std::ferror(out) || ::fsync(fd) != 0) {
std::fclose(out); std::fclose(out);
+10
View File
@@ -26,8 +26,18 @@ struct Occurrence {
[[nodiscard]] bool has_definition() const noexcept { return definition_file != nullptr; } [[nodiscard]] bool has_definition() const noexcept { return definition_file != nullptr; }
}; };
struct TypeRef {
std::string name, path;
Documents::Range range;
};
// A class observed as a call's result type and its direct bases.
struct ReflectedType {
TypeRef self;
std::vector<TypeRef> bases;
};
struct OccurrenceReport { struct OccurrenceReport {
std::vector<Occurrence> occurrences; std::vector<Occurrence> occurrences;
std::vector<ReflectedType> types;
bool complete{}; bool complete{};
bool partial{}; bool partial{};
std::string reason; std::string reason;
+46 -3
View File
@@ -189,7 +189,7 @@ decode_occurrences(std::string_view report, std::string_view nonce,
if (!parsed) if (!parsed)
return std::unexpected{OccurrenceError::invalid_report}; return std::unexpected{OccurrenceError::invalid_report};
const auto *root = P::as_object(&*parsed); const auto *root = P::as_object(&*parsed);
if (!root || root->size() != 6) if (!root)
return std::unexpected{OccurrenceError::invalid_report}; return std::unexpected{OccurrenceError::invalid_report};
const auto schema = integer(P::find_member(*root, "schema")); const auto schema = integer(P::find_member(*root, "schema"));
const auto *nonce_text = P::as_string(P::find_member(*root, "nonce")); const auto *nonce_text = P::as_string(P::find_member(*root, "nonce"));
@@ -197,14 +197,57 @@ decode_occurrences(std::string_view report, std::string_view nonce,
const auto partial = P::as_bool(P::find_member(*root, "partial")); const auto partial = P::as_bool(P::find_member(*root, "partial"));
const auto *reason = P::as_string(P::find_member(*root, "reason")); const auto *reason = P::as_string(P::find_member(*root, "reason"));
const auto *items = P::as_array(P::find_member(*root, "occurrences")); const auto *items = P::as_array(P::find_member(*root, "occurrences"));
const auto *types = P::as_array(P::find_member(*root, "types"));
if (!schema || *schema != 1 || !nonce_text || !complete || !partial || !reason || if (!schema || *schema != 1 || !nonce_text || !complete || !partial || !reason ||
!plain(*reason, max_reason) || !items || items->size() > max_occurrences) !plain(*reason, max_reason) || !items || items->size() > max_occurrences ||
(root->size() != 6 && root->size() != 7) || (root->size() == 7 && !types) ||
(types && types->size() > max_occurrences))
return std::unexpected{OccurrenceError::invalid_report}; return std::unexpected{OccurrenceError::invalid_report};
if (*nonce_text != nonce) if (*nonce_text != nonce)
return std::unexpected{OccurrenceError::wrong_nonce}; return std::unexpected{OccurrenceError::wrong_nonce};
OccurrenceReport result{{}, *complete, *partial, *reason}; OccurrenceReport result{{}, {}, *complete, *partial, *reason};
if (!*complete) if (!*complete)
return result; return result;
if (types)
for (const auto &item : *types) {
const auto *fields = P::as_object(&item);
const auto *self = fields ? P::find_member(*fields, "self") : nullptr;
const auto *bases = fields ? P::as_array(P::find_member(*fields, "bases")) : nullptr;
if (!fields || fields->size() != 2 || !bases || bases->size() > 16)
return std::unexpected{OccurrenceError::invalid_report};
const auto read = [&](const Json *value) -> std::optional<TypeRef> {
const auto *fields = value ? P::as_object(value) : nullptr;
if (!fields || fields->size() != 6 || !integer(P::find_member(*fields, "uid")) ||
!P::as_string(P::find_member(*fields, "name")))
return std::nullopt;
Json::Object trimmed;
for (const auto *key : {"path", "line", "startByte", "endByte"}) {
const auto *field = P::find_member(*fields, key);
if (!field)
return std::nullopt;
trimmed.emplace(key, *field);
}
const Json holder{std::move(trimmed)};
const auto raw = span(&holder);
if (!raw || !captured(capture, raw->path))
return std::nullopt;
auto bound = bind_span(*raw, {}, capture);
if (!bound)
return std::nullopt;
return TypeRef{std::string{raw->path}, raw->path, bound->second};
};
auto parsed_self = read(self);
if (!parsed_self)
return std::unexpected{OccurrenceError::invalid_report};
ReflectedType type{std::move(*parsed_self), {}};
for (const auto &base : *bases) {
auto parsed_base = read(&base);
if (!parsed_base)
return std::unexpected{OccurrenceError::invalid_report};
type.bases.push_back(std::move(*parsed_base));
}
result.types.push_back(std::move(type));
}
std::set<std::tuple<const void *, std::uint32_t, std::uint32_t>> uses; std::set<std::tuple<const void *, std::uint32_t, std::uint32_t>> uses;
for (const auto &item : *items) { for (const auto &item : *items) {
const auto *fields = P::as_object(&item); const auto *fields = P::as_object(&item);
+16 -1
View File
@@ -101,10 +101,25 @@ def analyze(work, nonce, admitted, complete=True, partial=None):
data = report_path.read_bytes() data = report_path.read_bytes()
check(0 < len(data) <= MAX_REPORT, "report size out of bounds") check(0 < len(data) <= MAX_REPORT, "report size out of bounds")
report = strict_json(data) report = strict_json(data)
check(set(report) == {"schema", "nonce", "complete", "partial", "reason", "occurrences"}, check(set(report) == {"schema", "nonce", "complete", "partial", "reason", "occurrences",
"types"},
f"unexpected report keys {sorted(report)}") f"unexpected report keys {sorted(report)}")
check(type(report["schema"]) is int and report["schema"] == 1, "wrong schema") check(type(report["schema"]) is int and report["schema"] == 1, "wrong schema")
check(report["nonce"] == nonce and HEX.fullmatch(report["nonce"]) is not None, "wrong nonce") check(report["nonce"] == nonce and HEX.fullmatch(report["nonce"]) is not None, "wrong nonce")
check(isinstance(report["types"], list) and len(report["types"]) <= 256,
"types is not a bounded list")
for entry in report["types"]:
check(isinstance(entry, dict) and set(entry) == {"self", "bases"},
f"invalid type entry {entry!r}")
for reference in [entry["self"], *entry["bases"]]:
check(isinstance(reference, dict)
and set(reference) == {"uid", "name", "path", "line", "startByte", "endByte"}
and type(reference["uid"]) is int and reference["uid"] > 0,
f"invalid type reference {reference!r}")
read_span({key: reference[key]
for key in ("path", "line", "startByte", "endByte")},
reference["name"], "type")
check(len(entry["bases"]) <= 16, "too many bases")
check(type(report["complete"]) is bool and type(report["partial"]) is bool, check(type(report["complete"]) is bool and type(report["partial"]) is bool,
"non-boolean status") "non-boolean status")
check(isinstance(report["reason"], str), "reason is not a string") check(isinstance(report["reason"], str), "reason is not a string")
+23
View File
@@ -207,3 +207,26 @@ VOID_TEST(Occurrences, binds_a_multiline_definition_extent) {
expect( expect(
!decode(report(occurrence("f", "int", span_json(path, call), bad)), entries).has_value()); !decode(report(occurrence("f", "int", span_json(path, call), bad)), entries).has_value());
} }
namespace {
std::string ref_only(const Span &value, std::uint32_t uid, std::string_view name) {
const auto span = span_json(path, value);
return "{\"uid\":" + std::to_string(uid) + ",\"name\":\"" + std::string{name} + "\"," +
span.substr(1);
}
std::string typed_report(std::string_view types) {
return R"({"schema":1,"nonce":")" + std::string{nonce} +
R"(","complete":true,"partial":false,"reason":"","occurrences":[],"types":)" +
std::string{types} + "}";
}
} // namespace
VOID_TEST(Occurrences, type_entry_without_bases_decodes) {
auto entries = capture(std::string{source});
const auto declaration = span(source, "f", 0);
const auto types = R"([{"self":)" + ref_only(declaration, 10, "f") + R"(,"bases":[]}])";
auto decoded = decode(typed_report(types), entries);
require(decoded.has_value());
require(decoded->types.size() == 1);
expect(decoded->types[0].bases.empty());
}