A macro's expansion for the arguments `args` (as written), or null when they don't fit its parameters.
(macro: CppMacro, args: string[])
| 367 | |
| 368 | /** A macro's expansion for the arguments `args` (as written), or null when they don't fit its parameters. */ |
| 369 | function 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, |
no test coverage detected