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

Function expandCppMacro

src/extraction/languages/cpp-macro-methods.ts:369–377  ·  view source on GitHub ↗

A macro's expansion for the arguments `args` (as written), or null when they don't fit its parameters.

(macro: CppMacro, args: string[])

Source from the content-addressed store, hash-verified

367
368/** A macro's expansion for the arguments `args` (as written), or null when they don't fit its parameters. */
369function expandCppMacro(macro: CppMacro, args: string[]): string | null {
370 const values = macro.params.length === 0 && args.length === 1 && args[0]!.trim() === '' ? [] : args.map((a) => a.trim());
371 if (values.length !== macro.params.length) return null;
372 const valueOf = new Map<string, string>(macro.params.map((p, k) => [p, values[k]!]));
373 return macro.body
374 .replace(/[A-Za-z_]\w*/g, (word) => valueOf.get(word) ?? word)
375 // `a ## b`: one token.
376 .replace(/\s*##\s*/g, '');
377}
378
379/**
380 * The function-like `#define`s of a C++ source and its `#undef`s, in order,

Callers 1

cppMacroMethodsFunction · 0.85

Calls 2

mapMethod · 0.80
getMethod · 0.65

Tested by

no test coverage detected