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

+90
View File
@@ -0,0 +1,90 @@
#include <vls/features/CallHierarchy.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <vector>
namespace {
using namespace vls::features::call_hierarchy;
using Void::Testing::expect;
using Void::Testing::require;
Range range(std::uint32_t line, std::uint32_t character = 0, std::uint32_t length = 4) {
return {{line, character}, {line, character + length}};
}
Definition definition(std::string name, std::string path, Range span) {
return {std::move(name), std::move(path), span};
}
Call call(std::string callee, Definition target, std::string path, Range span) {
return {std::move(callee), std::move(target), std::move(path), span};
}
} // namespace
VOID_TEST(CallHierarchy, groups_calls_by_the_enclosing_definition) {
const auto target = definition("f", "/p/a.cpp", range(1));
const std::vector<Definition> known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}}),
definition("inner", "/p/a.cpp", {{5, 0}, {9, 0}})};
const std::vector<Call> calls{call("f", target, "/p/a.cpp", range(6)),
call("f", target, "/p/a.cpp", range(7)),
call("f", target, "/p/a.cpp", range(12))};
const auto result = incoming(calls, known, target);
require(result.size() == 2);
expect(result[0].name == "inner" && result[0].resolved_definition);
require(result[0].from_ranges.size() == 2);
expect(result[0].from_ranges[0] == range(6));
expect(result[1].name == "outer" && result[1].from_ranges.size() == 1);
}
VOID_TEST(CallHierarchy, without_an_enclosing_definition_the_caller_is_the_file) {
const auto target = definition("f", "/p/a.cpp", range(1));
const std::vector<Call> calls{call("f", target, "/p/lib/b.cpp", range(3))};
const auto result = incoming(calls, {}, target);
require(result.size() == 1);
expect(result[0].name == "b.cpp" && result[0].path == "/p/lib/b.cpp");
expect(!result[0].resolved_definition);
expect(result[0].range == range(3));
expect(result[0].from_ranges == std::vector<Range>{range(3)});
}
VOID_TEST(CallHierarchy, only_calls_to_the_exact_target_are_reported) {
const auto target = definition("f", "/p/a.cpp", range(1));
const std::vector<Call> calls{
call("f", definition("f", "/p/a.cpp", range(9)), "/p/a.cpp", range(6)),
call("f", target, "/p/a.cpp", range(7)),
{"f", std::nullopt, "/p/a.cpp", range(8)}};
const auto result = incoming(calls, {}, target);
require(result.size() == 1);
expect(result[0].from_ranges == std::vector<Range>{range(7)});
}
VOID_TEST(CallHierarchy, duplicate_sites_are_deduplicated_and_sorted) {
const auto target = definition("f", "/p/a.cpp", range(1));
const std::vector<Call> calls{call("f", target, "/p/a.cpp", range(9)),
call("f", target, "/p/a.cpp", range(4)),
call("f", target, "/p/a.cpp", range(9))};
const auto result = incoming(calls, {}, target);
require(result.size() == 1);
require(result[0].from_ranges.size() == 2);
expect(result[0].from_ranges[0] == range(4));
expect(result[0].from_ranges[1] == range(9));
}
VOID_TEST(CallHierarchy, callers_and_ranges_are_bounded) {
const auto target = definition("f", "/p/a.cpp", range(1));
std::vector<Definition> known;
std::vector<Call> calls;
for (int index = 0; index != 4; ++index) {
known.push_back(definition("g" + std::to_string(index), "/p/a.cpp",
{{index * 10u, 0}, {index * 10u + 9u, 0}}));
calls.push_back(call("f", target, "/p/a.cpp", range(index * 10u + 2)));
}
expect(incoming(calls, known, target, Limits{.callers = 2}).size() == 2);
expect(incoming(calls, known, target, Limits{.ranges = 1}).size() == 4);
std::size_t ranges{};
for (const auto &entry : incoming(calls, known, target, Limits{.ranges = 1}))
ranges += entry.from_ranges.size();
expect(ranges == 1);
}