callHierarchy/outgoingCalls groups calls attributed to the prepared function's innermost enclosing definition by callee, with registered core tests for grouping, innermost attribution, unresolved-callee omission and bounds. Real stdio evidence shows the observer reports only the declarator span, so body calls cannot be attributed without guessing; the matrix records that limitation.
127 lines
6.0 KiB
C++
127 lines
6.0 KiB
C++
#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);
|
|
}
|
|
|
|
VOID_TEST(CallHierarchy, outgoing_reports_calls_inside_the_caller) {
|
|
const auto inner = definition("inner", "/p/a.cpp", range(1));
|
|
const std::vector<Definition> known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}}), inner};
|
|
const std::vector<Call> calls{call("inner", inner, "/p/a.cpp", range(6)),
|
|
call("inner", inner, "/p/a.cpp", range(7))};
|
|
const auto result = outgoing(calls, known, known.front());
|
|
require(result.size() == 1);
|
|
expect(result.front().to == inner);
|
|
require(result.front().from_ranges.size() == 2);
|
|
expect(result.front().from_ranges[0] == range(6));
|
|
}
|
|
|
|
VOID_TEST(CallHierarchy, outgoing_attributes_nested_calls_to_the_innermost_definition) {
|
|
const auto inner = definition("inner", "/p/a.cpp", range(1));
|
|
const auto nested = definition("nested", "/p/a.cpp", {{5, 0}, {9, 0}});
|
|
const std::vector<Definition> known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}}), nested,
|
|
inner};
|
|
const std::vector<Call> calls{call("inner", inner, "/p/a.cpp", range(6)),
|
|
call("inner", inner, "/p/a.cpp", range(15))};
|
|
const auto outer_result = outgoing(calls, known, known.front());
|
|
require(outer_result.size() == 1);
|
|
expect(outer_result.front().from_ranges == std::vector<Range>{range(15)});
|
|
const auto nested_result = outgoing(calls, known, nested);
|
|
require(nested_result.size() == 1);
|
|
expect(nested_result.front().from_ranges == std::vector<Range>{range(6)});
|
|
}
|
|
|
|
VOID_TEST(CallHierarchy, outgoing_skips_unresolved_callees_and_bounds_groups) {
|
|
const auto inner = definition("inner", "/p/a.cpp", range(1));
|
|
const std::vector<Definition> known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}})};
|
|
const std::vector<Call> calls{{"missing", std::nullopt, "/p/a.cpp", range(6)},
|
|
call("inner", inner, "/p/a.cpp", range(7))};
|
|
require(outgoing(calls, known, known.front()).size() == 1);
|
|
expect(outgoing(calls, known, known.front(), Limits{.callers = 0}).empty());
|
|
}
|