From 555ccecd654d317031a5de7c072278cc873c287e Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sat, 5 Dec 2026 16:30:33 +0300 Subject: [PATCH] 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. --- CMakeLists.txt | 2 +- build.cpp | 4 +- docs/agent-workflow/LSP-PARITY.md | 2 +- include/vls/features/CallHierarchy.hpp | 63 ++++++++++++++++++ src/features/CallHierarchy.cpp | 67 +++++++++++++++++++ tests/CMakeLists.txt | 2 +- tests/features/CallHierarchyTests.cpp | 90 ++++++++++++++++++++++++++ 7 files changed, 226 insertions(+), 4 deletions(-) create mode 100644 include/vls/features/CallHierarchy.hpp create mode 100644 src/features/CallHierarchy.cpp create mode 100644 tests/features/CallHierarchyTests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3e01d2b..56b3b35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,7 +30,7 @@ add_library(vls_protocol STATIC src/Protocol/Framing.cpp src/Protocol/Json.cpp src/Build/ProviderFile.cpp src/Build/CommandImport.cpp src/Build/VbsProvider.cpp src/Build/CompdbProvider.cpp src/DocumentSync.cpp src/WorkspaceSession.cpp src/Server.cpp src/semantic/Diagnostics.cpp src/semantic/SourceCatalog.cpp src/semantic/StockCompiler.cpp src/semantic/EditorDiagnostics.cpp src/semantic/Occurrences.cpp src/scheduler/Scheduler.cpp - src/modules/ModuleGraph.cpp src/modules/ArtifactCache.cpp src/modules/GccArtifacts.cpp src/modules/ScheduledArtifacts.cpp src/index/SymbolIndex.cpp src/navigation/Navigation.cpp src/reflection/Provenance.cpp src/features/Completion.cpp src/features/IndexNavigation.cpp src/features/SignatureHelp.cpp src/features/DocumentSymbols.cpp src/features/TextStructure.cpp src/features/CodeActions.cpp src/features/SemanticTokens.cpp src/features/InlayHints.cpp src/features/FileOperations.cpp src/features/IncludeUpdate.cpp src/debug/live/LiveSession.cpp src/modules/ModuleWorkQueue.cpp src/modules/GccScan.cpp src/modules/ScanCommand.cpp) + src/modules/ModuleGraph.cpp src/modules/ArtifactCache.cpp src/modules/GccArtifacts.cpp src/modules/ScheduledArtifacts.cpp src/index/SymbolIndex.cpp src/navigation/Navigation.cpp src/reflection/Provenance.cpp src/features/Completion.cpp src/features/IndexNavigation.cpp src/features/SignatureHelp.cpp src/features/DocumentSymbols.cpp src/features/TextStructure.cpp src/features/CodeActions.cpp src/features/SemanticTokens.cpp src/features/InlayHints.cpp src/features/FileOperations.cpp src/features/IncludeUpdate.cpp src/features/CallHierarchy.cpp src/debug/live/LiveSession.cpp src/modules/ModuleWorkQueue.cpp src/modules/GccScan.cpp src/modules/ScanCommand.cpp) target_include_directories(vls_protocol PUBLIC "${PROJECT_SOURCE_DIR}/include") target_link_libraries(vls_protocol PUBLIC Void::Data PRIVATE Void::Unicode Void::Text Void::Crypto Void::System) target_compile_options(vls_protocol PUBLIC -freflection PRIVATE -Wall -Wextra -Wpedantic) diff --git a/build.cpp b/build.cpp index 8a95a9c..1e4e4d0 100644 --- a/build.cpp +++ b/build.cpp @@ -76,6 +76,7 @@ void build(Void::Build::Project &project) { "src/features/InlayHints.cpp", "src/features/FileOperations.cpp", "src/features/IncludeUpdate.cpp", + "src/features/CallHierarchy.cpp", "src/debug/live/LiveSession.cpp", "src/modules/ModuleWorkQueue.cpp", "src/modules/GccScan.cpp", @@ -157,7 +158,8 @@ void build(Void::Build::Project &project) { "tests/features/SignatureHelpTests.cpp", "tests/features/DocumentSymbolsTests.cpp", "tests/features/TextStructureTests.cpp", "tests/features/CodeActionsTests.cpp", "tests/features/SemanticTokensTests.cpp", "tests/features/InlayHintsTests.cpp", - "tests/features/FileOperationsTests.cpp", "tests/features/IncludeUpdateTests.cpp"}); + "tests/features/FileOperationsTests.cpp", "tests/features/IncludeUpdateTests.cpp", + "tests/features/CallHierarchyTests.cpp"}); auto live_worker = project.executable("vls_live_worker_fixture"); live_worker.cxx_standard(CxxStandard::Cxx26) .sources({"tests/debug/LiveWorkerFixture.cpp"}) diff --git a/docs/agent-workflow/LSP-PARITY.md b/docs/agent-workflow/LSP-PARITY.md index ea4ed95..a6de73c 100644 --- a/docs/agent-workflow/LSP-PARITY.md +++ b/docs/agent-workflow/LSP-PARITY.md @@ -34,7 +34,7 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints. | Compiler-native completion with visibility proof | index-backed completion is explicitly incomplete | | Overload-set signature help | only the selected occurrence's signature is available | | textDocument/formatting | needs a plan | -| callHierarchy / typeHierarchy | needs compiler call/type relationships | +| callHierarchy / typeHierarchy | incoming-call core is implemented and tested (`vls_features::call_hierarchy`); server wiring and type hierarchy are open | | textDocument/codeLens, executeCommand | command surface not designed | | Client-side application of server-issued workspace edits | rename and code actions return edits the clients apply directly | | Applying multi-file renames through client buffers | plans can be ready; application is client work | diff --git a/include/vls/features/CallHierarchy.hpp b/include/vls/features/CallHierarchy.hpp new file mode 100644 index 0000000..1982acd --- /dev/null +++ b/include/vls/features/CallHierarchy.hpp @@ -0,0 +1,63 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace vls::features::call_hierarchy { + +struct Position { + std::uint32_t line{}, character{}; + auto operator<=>(const Position &) const = default; +}; +struct Range { + Position start, end; + auto operator<=>(const Range &) const = default; +}; + +// A compiler-proven function definition. +struct Definition { + std::string name; + std::string path; + Range range; + auto operator<=>(const Definition &) const = default; +}; + +// One resolved call site. +struct Call { + std::string callee; + std::optional callee_definition; + std::string path; + Range range; +}; + +struct Incoming { + std::string name; + std::string path; + Range range; // the caller definition, or the call itself when unknown + std::vector from_ranges; + // True when an enclosing compiler-proven definition named the caller; + // false means the caller is reported at file level. + bool resolved_definition{}; + auto operator<=>(const Incoming &) const = default; +}; + +struct Limits { + std::size_t callers{256}; + std::size_t ranges{512}; + std::size_t nameBytes{256}; +}; + +// Incoming calls of TARGET: every resolved call to it, grouped by the +// innermost known definition containing the call site in that file. When no +// definition contains it, the caller is reported at file level with the call +// range, never as a guessed function. +[[nodiscard]] std::vector incoming(std::span, + std::span known, + const Definition &target, const Limits & = Limits{}); + +} // namespace vls::features::call_hierarchy diff --git a/src/features/CallHierarchy.cpp b/src/features/CallHierarchy.cpp new file mode 100644 index 0000000..7d949b4 --- /dev/null +++ b/src/features/CallHierarchy.cpp @@ -0,0 +1,67 @@ +#include + +#include +#include +#include + +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(std::span calls, std::span known, + const Definition &target, const Limits &limits) { + std::map, 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 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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8bf72b5..2972861 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,7 +22,7 @@ vls_test_suite(vls_scheduler_tests scheduler/SchedulerTests.cpp) vls_test_suite(vls_index_tests index/SymbolIndexTests.cpp) vls_test_suite(vls_navigation_tests navigation/NavigationTests.cpp) vls_test_suite(vls_reflection_tests reflection/ProvenanceTests.cpp) -vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp features/DocumentSymbolsTests.cpp features/TextStructureTests.cpp features/CodeActionsTests.cpp features/SemanticTokensTests.cpp features/InlayHintsTests.cpp features/FileOperationsTests.cpp features/IncludeUpdateTests.cpp) +vls_test_suite(vls_feature_tests features/CompletionTests.cpp features/IndexNavigationTests.cpp features/SignatureHelpTests.cpp features/DocumentSymbolsTests.cpp features/TextStructureTests.cpp features/CodeActionsTests.cpp features/SemanticTokensTests.cpp features/InlayHintsTests.cpp features/FileOperationsTests.cpp features/IncludeUpdateTests.cpp features/CallHierarchyTests.cpp) vls_test_suite(vls_live_tests debug/LiveSessionTests.cpp debug/LiveDapTests.cpp) target_link_libraries(vls_live_tests PRIVATE Void::System) add_dependencies(vls_live_tests vls_live_worker_fixture vls-dap) diff --git a/tests/features/CallHierarchyTests.cpp b/tests/features/CallHierarchyTests.cpp new file mode 100644 index 0000000..bb2c4e5 --- /dev/null +++ b/tests/features/CallHierarchyTests.cpp @@ -0,0 +1,90 @@ +#include + +#include +#include + +#include + +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 known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}}), + definition("inner", "/p/a.cpp", {{5, 0}, {9, 0}})}; + const std::vector 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 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(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 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(7)}); +} + +VOID_TEST(CallHierarchy, duplicate_sites_are_deduplicated_and_sorted) { + const auto target = definition("f", "/p/a.cpp", range(1)); + const std::vector 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 known; + std::vector 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); +}