MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / recoverCppMacroDefinedName

Function recoverCppMacroDefinedName

src/extraction/languages/c-cpp.ts:328–352  ·  view source on GitHub ↗

* Recover the real function name from the macro-definition idiom * `MACRO_NAME(real_name, typed args…) { body }` — flash-attention's * `DEFINE_FLASH_FORWARD_KERNEL(flash_fwd_kernel, bool Is_dropout, …) { … }` * being the motivating case: tree-sitter parses the invocation as a * function_definiti

(node: SyntaxNode, source: string)

Source from the content-addressed store, hash-verified

326 * google-benchmark's `BENCHMARK_DEFINE_F(Fix, name)` all bail here).
327 */
328function recoverCppMacroDefinedName(node: SyntaxNode, source: string): string | undefined {
329 if (node.type !== 'function_definition') return undefined;
330 const declarator = getChildByField(node, 'declarator');
331 if (declarator?.type !== 'function_declarator') return undefined;
332 const inner = getChildByField(declarator, 'declarator');
333 if (inner?.type !== 'identifier') return undefined;
334 const macroName = getNodeText(inner, source);
335 if (!/^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)+$/.test(macroName)) return undefined;
336 const params = getChildByField(declarator, 'parameters');
337 if (!params || params.namedChildCount < 2) return undefined;
338 const loneIdentText = (p: SyntaxNode): string | null =>
339 p.type === 'parameter_declaration' &&
340 p.namedChildCount === 1 &&
341 p.namedChild(0)?.type === 'type_identifier'
342 ? getNodeText(p.namedChild(0)!, source)
343 : null;
344 const first = params.namedChild(0);
345 const name = first ? loneIdentText(first) : null;
346 if (!name || !/[a-z]/.test(name)) return undefined;
347 for (let i = 1; i < params.namedChildCount; i++) {
348 const p = params.namedChild(i);
349 if (p && loneIdentText(p) !== null) return undefined;
350 }
351 return name;
352}
353
354const CPP_CLASS_SPECIFIERS: ReadonlySet<string> = new Set(['class_specifier', 'struct_specifier', 'union_specifier']);
355

Callers 2

c-cpp.tsFile · 0.85

Calls 3

getChildByFieldFunction · 0.90
getNodeTextFunction · 0.90
loneIdentTextFunction · 0.85

Tested by

no test coverage detected