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

Function expandCppMacro

src/extraction/languages/cpp-macro-methods.ts:370–378  ·  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

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