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

Function cppMacroDirectives

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

* The function-like `#define`s of a C++ source and its `#undef`s, in order, * read past comments and literals: a `#define` in a comment or a raw string * defines nothing.

(source: string)

Source from the content-addressed store, hash-verified

382 * defines nothing.
383 */
384function cppMacroDirectives(source: string): { macros: CppMacro[]; undefs: Array<{ name: string; at: number }> } {
385 const macros: CppMacro[] = [];
386 const undefs: Array<{ name: string; at: number }> = [];
387 for (const d of cppCodeScan(source, null).directives) {
388 const text = withoutLineComment(d.text.replace(/\\\r?\n/g, ' ').replace(/\/\*[\s\S]*?\*\//g, ' '));
389 const define = /^#[ \t]*define[ \t]+([A-Za-z_]\w*)\(([^)]*)\)/.exec(text);
390 if (define) {
391 const params = define[2]!.split(',').map((p) => p.trim()).filter((p) => p !== '');
392 if (!params.some((p) => p.endsWith('...'))) macros.push({ name: define[1]!, params, body: text.slice(define[0].length), at: d.at });
393 continue;
394 }
395 const undef = /^#[ \t]*undef[ \t]+([A-Za-z_]\w*)/.exec(text);
396 if (undef) undefs.push({ name: undef[1]!, at: d.at });
397 }
398 return { macros, undefs };
399}
400
401/** The invocations of the macros `names` in code (not in a directive, comment or literal), with their arguments. */
402function cppMacroInvocations(source: string, names: ReadonlySet<string>): Array<{ name: string; at: number; end: number; args: string[] }> {

Callers 1

cppMacroMethodsFunction · 0.85

Calls 5

cppCodeScanFunction · 0.85
withoutLineCommentFunction · 0.85
mapMethod · 0.80
pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected