Group incoming calls by the enclosing definition

A bounded call-hierarchy core maps resolved call sites to the
innermost compiler-proven definition containing them in the same file,
falling back to an explicitly file-level caller instead of a guessed
function. Registered vls_feature_tests cases cover grouping,
deduplication, exact-target matching and bounds. Server wiring remains.
This commit is contained in:
faumaray committed 2026-12-05 16:30:33 +03:00
1 parent b0b48d961c
commit 555ccecd65
7 files changed
+226 -4

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
#include <vls/features/CallHierarchy.hpp>
#include <algorithm>
#include <map>
#include <tuple>
namespace vls::features::call_hierarchy {
namespace {
bool contains(const Range &outer, const Range &inner) {
return outer.start <= inner.start && inner.end <= outer.end;
}
} // namespace
std::vector<Incoming> incoming(std::span<const Call> calls, std::span<const Definition> known,
const Definition &target, const Limits &limits) {
std::map<std::tuple<std::string, std::string, Range>, Incoming> grouped;
std::size_t total_ranges{};
for (const auto &call : calls) {
if (!call.callee_definition || *call.callee_definition != target)
continue;
const Definition *enclosing{};
for (const auto &definition : known) {
if (definition.path != call.path || !contains(definition.range, call.range))
continue;
if (!enclosing || definition.range.start > enclosing->range.start)
enclosing = &definition;
}
Incoming entry;
if (enclosing) {
entry.name = enclosing->name;
entry.path = enclosing->path;
entry.range = enclosing->range;
entry.resolved_definition = true;
} else {
entry.path = call.path;
entry.range = call.range;
const auto slash = call.path.find_last_of('/');
entry.name = slash == std::string::npos ? call.path : call.path.substr(slash + 1);
}
if (entry.name.size() > limits.nameBytes)
entry.name.resize(limits.nameBytes);
// Unresolved callers group by file, not by each call range.
const Range key_range = enclosing ? enclosing->range : Range{};
auto &slot = grouped[{entry.name, entry.path, key_range}];
if (slot.name.empty()) {
const auto call_range = call.range;
slot = std::move(entry);
if (!enclosing)
slot.range = call_range;
}
if (slot.from_ranges.size() < limits.ranges && total_ranges < limits.ranges &&
std::ranges::find(slot.from_ranges, call.range) == slot.from_ranges.end()) {
slot.from_ranges.push_back(call.range);
++total_ranges;
}
}
std::vector<Incoming> result;
for (auto &[key, entry] : grouped) {
if (result.size() == limits.callers)
break;
std::ranges::sort(entry.from_ranges);
result.push_back(std::move(entry));
}
return result;
}
} // namespace vls::features::call_hierarchy