From a098e45dc9b459c01ca2e4c7992d0509990adb14 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sat, 5 Dec 2026 17:03:48 +0300 Subject: [PATCH] Serve outgoing call hierarchy 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. --- docs/agent-workflow/LSP-PARITY.md | 2 +- docs/agent-workflow/STATE.md | 2 +- docs/agent-workflow/VERIFICATION.md | 14 +++++ include/vls/features/CallHierarchy.hpp | 14 +++++ include/vls/semantic/EditorDiagnostics.hpp | 3 + src/Server.cpp | 8 +++ src/features/CallHierarchy.cpp | 36 ++++++++++++ src/semantic/EditorDiagnostics.cpp | 65 ++++++++++++++++++++++ tests/features/CallHierarchyTests.cpp | 36 ++++++++++++ 9 files changed, 178 insertions(+), 2 deletions(-) diff --git a/docs/agent-workflow/LSP-PARITY.md b/docs/agent-workflow/LSP-PARITY.md index 0abdae2..b38db63 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 | callHierarchy prepare + incomingCalls are implemented and verified over stdio; type hierarchy and outgoing calls are open | +| callHierarchy / typeHierarchy | prepare, incomingCalls and outgoingCalls are implemented and served; outgoing results need full function extents from the compiler (the observed span is the declarator only). Type hierarchy is 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/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index ce7d4ce..7df8c0d 100644 --- a/docs/agent-workflow/STATE.md +++ b/docs/agent-workflow/STATE.md @@ -102,7 +102,7 @@ ingests occurrence facts after each analysis and the server answers Hash.hpp definition through it. Real persistence migrations and cross-file rename coverage remain open. -Index-backed completion (roadmap 14 first slice) is implemented: `include/vls/features/Completion.hpp`, `src/features/Completion.cpp` and registered `vls_feature_tests` cover prefix extraction, filtering with replacement ranges, label+kind deduplication, deterministic ranking and bounds. `Server.cpp` advertises `completionProvider` and answers `textDocument/completion` from the symbol index through `EditorDiagnostics::completion`; a real stdio session with the stock observer returned `fnv1a64` and `fnv1a64_update` at a real call site. Every result is marked incomplete because the index does not prove visibility. Signature help is now served from the compiler-provided occurrence signature (parameter labels and ranges, active parameter, innermost call selection) and advertised by the server. Document symbols are served from the index by path and document highlights from same-binding occurrence facts; both are advertised and were exercised over stdio with the stock observer. Selection and folding ranges are served from immutable document snapshots with registered bounded cores. Code actions now offer the compiler's own suggested fixes (quickfix) from retained published diagnostics, with SARIF fix decoding bound to the owner snapshot. Semantic tokens encode the proven call occurrences with a bounded one-type legend. Inlay hints project parameter types from the occurrence signature at each argument start. Workspace file-operation notifications invalidate dependent index contexts and re-queue open documents. A will-rename request returns the include updates for open documents. Call hierarchy serves prepare and incoming calls (grouped by enclosing definition or explicitly by file) from the stock call facts. Compiler-native completion with visibility proof and deferred detail resolution remain open. +Index-backed completion (roadmap 14 first slice) is implemented: `include/vls/features/Completion.hpp`, `src/features/Completion.cpp` and registered `vls_feature_tests` cover prefix extraction, filtering with replacement ranges, label+kind deduplication, deterministic ranking and bounds. `Server.cpp` advertises `completionProvider` and answers `textDocument/completion` from the symbol index through `EditorDiagnostics::completion`; a real stdio session with the stock observer returned `fnv1a64` and `fnv1a64_update` at a real call site. Every result is marked incomplete because the index does not prove visibility. Signature help is now served from the compiler-provided occurrence signature (parameter labels and ranges, active parameter, innermost call selection) and advertised by the server. Document symbols are served from the index by path and document highlights from same-binding occurrence facts; both are advertised and were exercised over stdio with the stock observer. Selection and folding ranges are served from immutable document snapshots with registered bounded cores. Code actions now offer the compiler's own suggested fixes (quickfix) from retained published diagnostics, with SARIF fix decoding bound to the owner snapshot. Semantic tokens encode the proven call occurrences with a bounded one-type legend. Inlay hints project parameter types from the occurrence signature at each argument start. Workspace file-operation notifications invalidate dependent index contexts and re-queue open documents. A will-rename request returns the include updates for open documents. Call hierarchy serves prepare and incoming calls (grouped by enclosing definition or explicitly by file) from the stock call facts. Outgoing calls are served too, but the observer's declarator-only definition span limits attribution to the core tests for now. Compiler-native completion with visibility proof and deferred detail resolution remain open. The reflection provenance representation (roadmap 12 first slice) is implemented as a bounded library with a registered suite: innermost-first diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index 27e87e4..808dc8b 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -79,6 +79,20 @@ the upgraded snapshot (keeping `snapshot.prev.json`) and rewrites a legacy about 25-30s in a Debug build (it passed VBS at ~27s); the full CMake run then passed 18/18. +## December 5 outgoing call hierarchy + +`callHierarchy/outgoingCalls` is wired through +`EditorDiagnostics::call_hierarchy_outgoing` to the core `outgoing`, which +attributes each resolved call to the innermost known definition containing it +and groups the results by callee definition. Registered `vls_feature_tests` +cases cover grouping, innermost attribution, unresolved-callee omission and +bounds. A real stdio session with nested calls +(`int outer() { return inner() + inner(); }`) prepared `outer` and reported its +incoming call, but outgoing was empty: the observer's definition span is the +declarator only (`outer` at 1:4-1:9), so a call in the body cannot be attributed +to it without guessing. Extending the compiler to report full function extents +is the prerequisite for real outgoing results. + ## December 5 call hierarchy over LSP `EditorDiagnostics::call_hierarchy_prepare` turns a covering occurrence with a diff --git a/include/vls/features/CallHierarchy.hpp b/include/vls/features/CallHierarchy.hpp index 1982acd..08e87ac 100644 --- a/include/vls/features/CallHierarchy.hpp +++ b/include/vls/features/CallHierarchy.hpp @@ -60,4 +60,18 @@ struct Limits { std::span known, const Definition &target, const Limits & = Limits{}); +struct Outgoing { + Definition to; + std::vector from_ranges; + auto operator<=>(const Outgoing &) const = default; +}; + +// Calls made inside CALLER, grouped by their resolved callee definition. +// A call is attributed to the innermost known definition containing it in the +// same file, so calls inside nested functions are not reported for CALLER. +// Calls without a resolved callee are omitted: an item must point somewhere. +[[nodiscard]] std::vector outgoing(std::span, + std::span known, + const Definition &caller, const Limits & = Limits{}); + } // namespace vls::features::call_hierarchy diff --git a/include/vls/semantic/EditorDiagnostics.hpp b/include/vls/semantic/EditorDiagnostics.hpp index 80cc062..73869d3 100644 --- a/include/vls/semantic/EditorDiagnostics.hpp +++ b/include/vls/semantic/EditorDiagnostics.hpp @@ -72,6 +72,9 @@ class EditorDiagnostics { // Incoming calls of a prepared item, grouped by enclosing definition. [[nodiscard]] Void::LanguageServer::Protocol::Json call_hierarchy_incoming(const Void::LanguageServer::Protocol::Json &item) const; + // Calls made inside a prepared item's function. + [[nodiscard]] Void::LanguageServer::Protocol::Json + call_hierarchy_outgoing(const Void::LanguageServer::Protocol::Json &item) const; // Parameter-type hints from the compiler signature, within the range. [[nodiscard]] Void::LanguageServer::Protocol::Json inlay_hints(std::string_view uri, Documents::Range range) const; diff --git a/src/Server.cpp b/src/Server.cpp index cde6c06..3e0d243 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -454,6 +454,14 @@ DispatchResult Server::handle(std::string_view body) { *uri, {static_cast(*line_value), static_cast(*character_value)})); } + if (*method == "callHierarchy/outgoingCalls") { + if (!diagnostics_ || !diagnostics_->supports_occurrences()) + return error(id, -32601, "Method not found"); + const auto *item = member(params, "item"); + if (!item) + return error(id, -32602, "Expected a call hierarchy item"); + return reply(id, "result", diagnostics_->call_hierarchy_outgoing(*item)); + } if (*method == "callHierarchy/incomingCalls") { if (!diagnostics_ || !diagnostics_->supports_occurrences()) return error(id, -32601, "Method not found"); diff --git a/src/features/CallHierarchy.cpp b/src/features/CallHierarchy.cpp index 7d949b4..2777932 100644 --- a/src/features/CallHierarchy.cpp +++ b/src/features/CallHierarchy.cpp @@ -64,4 +64,40 @@ std::vector incoming(std::span calls, std::span outgoing(std::span calls, std::span known, + const Definition &caller, const Limits &limits) { + std::map, Outgoing> grouped; + std::size_t total_ranges{}; + for (const auto &call : calls) { + if (!call.callee_definition) + 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; + } + if (!enclosing || *enclosing != caller) + continue; + auto &slot = grouped[{call.callee_definition->name, call.callee_definition->path, + call.callee_definition->range}]; + if (slot.to.name.empty()) + slot.to = *call.callee_definition; + 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/src/semantic/EditorDiagnostics.cpp b/src/semantic/EditorDiagnostics.cpp index bbe5f38..4631aa3 100644 --- a/src/semantic/EditorDiagnostics.cpp +++ b/src/semantic/EditorDiagnostics.cpp @@ -1503,6 +1503,71 @@ Json EditorDiagnostics::call_hierarchy_incoming(const Json &item) const { return Json{std::move(result)}; } +Json EditorDiagnostics::call_hierarchy_outgoing(const Json &item) const { + auto &s = *state_; + const auto *fields = P::as_object(&item); + const auto *data = fields ? P::as_object(P::find_member(*fields, "data")) : nullptr; + const auto *name = data ? P::as_string(P::find_member(*data, "name")) : nullptr; + const auto *path = data ? P::as_string(P::find_member(*data, "path")) : nullptr; + const auto line = data ? P::as_int64(P::find_member(*data, "line")) : std::nullopt; + const auto character = data ? P::as_int64(P::find_member(*data, "character")) : std::nullopt; + const auto end_line = data ? P::as_int64(P::find_member(*data, "endLine")) : std::nullopt; + const auto end_character = + data ? P::as_int64(P::find_member(*data, "endCharacter")) : std::nullopt; + if (!name || !path || !line || !character || !end_line || !end_character) + return Json{Json::Array{}}; + const features::call_hierarchy::Definition caller{ + *name, + *path, + {{static_cast(*line), static_cast(*character)}, + {static_cast(*end_line), static_cast(*end_character)}}}; + std::vector calls; + std::vector known; + { + std::lock_guard lock{s.mutex}; + for (const auto &[key, entry] : s.entries) { + if (!entry.occurrences) + continue; + for (const auto &occurrence : *entry.occurrences) { + features::call_hierarchy::Call call; + call.callee = occurrence.name; + call.path = occurrence.use_file->identity.canonical_path.native(); + call.range = {{occurrence.use.start.line, occurrence.use.start.character}, + {occurrence.use.end.line, occurrence.use.end.character}}; + if (occurrence.has_definition()) { + features::call_hierarchy::Definition definition{ + occurrence.name, + occurrence.definition_file->identity.canonical_path.native(), + {{occurrence.definition.start.line, occurrence.definition.start.character}, + {occurrence.definition.end.line, occurrence.definition.end.character}}}; + call.callee_definition = definition; + known.push_back(std::move(definition)); + } + calls.push_back(std::move(call)); + } + } + } + const auto grouped = features::call_hierarchy::outgoing(calls, known, caller); + Json::Array result; + for (const auto &callee : grouped) { + Json::Array ranges; + for (const auto &range : callee.from_ranges) + ranges.push_back(range_json(Documents::Range{{range.start.line, range.start.character}, + {range.end.line, range.end.character}})); + const auto span = range_json( + Documents::Range{{callee.to.range.start.line, callee.to.range.start.character}, + {callee.to.range.end.line, callee.to.range.end.character}}); + Json::Object to{{"name", Json{callee.to.name}}, + {"kind", Json{std::int64_t{12}}}, + {"uri", Json{Documents::path_to_file_uri(callee.to.path)}}, + {"range", span}, + {"selectionRange", span}}; + result.push_back(Json{ + Json::Object{{"to", Json{std::move(to)}}, {"fromRanges", Json{std::move(ranges)}}}}); + } + return Json{std::move(result)}; +} + Json EditorDiagnostics::hover(std::string_view uri, Documents::Position position) const { auto &s = *state_; auto key = Documents::client_uri_key(uri); diff --git a/tests/features/CallHierarchyTests.cpp b/tests/features/CallHierarchyTests.cpp index bb2c4e5..29d3643 100644 --- a/tests/features/CallHierarchyTests.cpp +++ b/tests/features/CallHierarchyTests.cpp @@ -88,3 +88,39 @@ VOID_TEST(CallHierarchy, callers_and_ranges_are_bounded) { 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 known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}}), inner}; + const std::vector 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 known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}}), nested, + inner}; + const std::vector 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(15)}); + const auto nested_result = outgoing(calls, known, nested); + require(nested_result.size() == 1); + expect(nested_result.front().from_ranges == std::vector{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 known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}})}; + const std::vector 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()); +}