codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

fix(extraction): index C functions decorated with a leading export macro (#515)

Open maxmilian opened this issue 6 months ago • 1 comments

Summary

Fixes #515. A C function written as EXPORT_MACRO ReturnType Name(...) — a leading export/visibility macro in front of a project-defined (typedef'd) return type — was indexed under the garbage name (params) instead of its real name, so it couldn't be searched or navigated to. This pattern is ubiquitous in vendor SDKs, e.g. the Axera SoC SDK from the report: AX_VIN_GLB_API AX_S32 AX_VIN_Init(AX_VOID).

Root cause

Both the macro and the typedef'd return type are unknown identifiers in type position, so tree-sitter-c peels MACRO RET off as a separate (broken) declaration and parses the real function with its name absorbed as the type field and the parameter list wrapped in a bare parenthesized_declarator instead of a function_declarator:

function_definition
  type: type_identifier "AX_VIN_Init"      <- the real name
  declarator: parenthesized_declarator     <- the params
    identifier "AX_VOID"
  body: compound_statement

extractName then falls through to getNodeText(parenthesized_declarator) → "(AX_VOID)".

Fix

recoverMacroDecoratedFunctionName (wired only into the C extractor's resolveName): when a function_definition's declarator field is directly a parenthesized_declarator, take the name from the type field instead. That shape is one valid C never produces.

Tests

__tests__/extraction.test.ts → new C/C++ Extraction block:

  • recovers AX_VIN_Init from the macro-decorated definition (and is not named (AX_VOID))
  • plain int compute(int a) is unaffected
  • int (foo)(void) (redundant parens — keeps a function_declarator) does not misfire and is not renamed to int

Full suite green locally: npm run build clean, 1097 passed | 2 skipped (+3 new).

Scope

Deliberately narrow — verified against the real grammars with parse dumps before/after:

  • Macro + primitive return (AX_API int F(...)), macro + pointer return (AX_API char* F(...)), and all C++ forms already parse to a function_declarator and extract correctly on main — they don't reach this recovery and aren't touched (no resolveName change for the C++ extractor).
  • Bare prototypes in headers (AX_API AX_S32 AX_VIN_Init(void);) are reparsed by tree-sitter as a top-level call expression. Recovering a symbol from that would risk synthesizing symbols from genuine call sites, so it's left as a follow-up. The definition site (this fix) is where the symbol lives, which makes it searchable.

No node-count change — resolveName only renames; it cannot add or drop nodes.

maxmilian avatar May 31 '26 15:05 maxmilian

@colbymchenry friendly ping — #515 is still open and this one's self-contained: it recovers C functions whose signature is preceded by a leading export macro (e.g. EXPORT void foo(void)), which the extraction pass currently skips. No overlap with the recent extraction merges (#956/#980 are about file/repo discovery, not the C symbol scanner). Happy to rebase if it's drifted — any feedback welcome!

maxmilian avatar Jun 24 '26 22:06 maxmilian