fix(cpp): name reference-returning free functions + resolve qualified calls ns::a::Func(...)#790
Open
songwei163 wants to merge 2 commits into
Open
Conversation
…allee A C++ qualified call's `function` field is a `qualified_identifier`, which fell through to the generic `else` in `extractCall` and stored the full `ns::a::Func` text as the callee name. Function nodes are stored under their SIMPLE name (the C++ extractor records the last `::` segment), so the name-based resolver could never link the `calls` edge and these functions reported "No callers". Add a `qualified_identifier` branch that references the last `::` segment, consistent with how the C++ extractor names nodes and how methods resolve. Adds a regression test under "C++ free-function name extraction".
`const std::string& GetRef(a::Ctx& c)` wraps the function_declarator in a reference_declarator. `extractCppQualifiedMethodName` only resolved a name when the declarator contained a `qualified_identifier`, so a plain-identifier free function fell through to the generic declarator-text fallback, which doesn't unwrap reference_declarator and indexed it as `& GetRef(a::Ctx& c)` — unsearchable, and its callers never resolved. Walk the declarator chain (unwrapping pointer/reference/parenthesized wrappers, never descending into the parameter list) to the NAME node and return plain identifiers directly. operator/destructor/template names are still left to the generic fallback, which handles them. Verified across a 10-pattern battery (qualified/plain params, pointer/reference/trailing return, operator, template, out-of-line member, file-scope free fn).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two related C++ extraction bugs, both rooted in how the function NAME is taken from a
function_definition's declarator. tree-sitter parses the code cleanly — these are extractor/resolver issues.Fix 1 — reference-returning free functions are mis-named (unsearchable)
const std::string& GetRef(a::Ctx& c)wraps thefunction_declaratorin areference_declarator.extractCppQualifiedMethodNameonly resolved a name when the declarator contained aqualified_identifier; a plain-identifier free function fell through to the generic declarator-text fallback, which doesn't unwrapreference_declaratorand indexed it as& GetRef(a::Ctx& c)— unsearchable, and its callers never resolved.Now we walk the declarator chain to the NAME node only (unwrapping pointer/reference/parenthesized wrappers, never descending into the parameter list) and return plain identifiers directly.
operator_name/destructor_name/template_functionare still left to the generic fallback, which names them correctly.10-pattern battery, before → after on current
main:GetThing(a::Ctx&)GetThingGetThingint* GetPtr(...)GetPtrGetPtrauto T(...)->a::RetTrailingTrailingstd::string FreeStrRet(...)FreeStrRetFreeStrRetconst std::string& GetRef(a::Ctx&)& GetRef(a::Ctx& c)❌GetRef✅operator==(const a::A&,...)operator==operator==void Klass::Method(a::Ctx&)MethodMethod(recv=Klass)template<class T> a::R TplFn(a::Ctx&)TplFnTplFnvoid Process(a::Ctx&)ProcessProcess(The "named after the first namespaced parameter type" variant —
GetThing→Ctx— was already fixed onmain; this PR closes the remaining reference-return case.)Fix 2 — qualified call
ns::a::Func(...)produces nocallsedgeA C++ qualified call's
functionfield is aqualified_identifier. InextractCall(src/extraction/tree-sitter.ts) it fell through to the genericelseand stored the fullns::a::Functext as the callee name. Function nodes are stored under their simple name, so the name-based resolver never linked thecallsedge — the callee reported "No callers".Added a
qualified_identifierbranch (right after the existingscoped_identifierbranch) that references the last::segment, consistent with how the C++ extractor names nodes and how methods resolve.Why safe
qualified_identifiercall nodes are C++-specific (Rust usesscoped_identifier, which keeps its existing full-text branch); the name-node walk is in the C++ extractor only.Tests
New cases under
__tests__/extraction.test.ts→ "C++ free-function name extraction":GetRef(not& GetRef(...)) and resolves cross-file callers;mmpayinspolicymgrao::insured::GetInsured("x")resolves to the defining file.npm run buildpasses; the C++ extraction tests pass.