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:
1 parent
3344a9f170
commit
b1c8a4a625
6 files changed
+240
-4
No files matched your search
@@ -68,6 +68,22 @@ struct body_entry {
|
||||
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 {
|
||||
unsigned uid{};
|
||||
span where{};
|
||||
@@ -530,6 +546,18 @@ bool call_token(tree call, tree decl, span &out, const char **path_out) {
|
||||
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) {
|
||||
for (unsigned i = 0; i != body_count; ++i) {
|
||||
if (bodies[i].uid == uid) {
|
||||
@@ -574,6 +602,75 @@ void record_body(tree decl) {
|
||||
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) {
|
||||
if (at_capacity) {
|
||||
return;
|
||||
@@ -626,6 +723,13 @@ void record_use(tree call, tree decl) {
|
||||
IDENTIFIER_LENGTH(DECL_NAME(decl)));
|
||||
std::strcpy(entry.type, type_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);
|
||||
if (signature && std::strlen(signature) <= max_signature && utf8_valid(signature)) {
|
||||
std::strcpy(entry.signature, signature);
|
||||
@@ -781,6 +885,32 @@ void finish(void *, void *) {
|
||||
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);
|
||||
if (std::fflush(out) != 0 || std::ferror(out) || ::fsync(fd) != 0) {
|
||||
std::fclose(out);
|
||||
|
||||
Reference in new issue
Block a user