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

Function normalizeCppComInterfaces

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

Recover MSVC's COM alias only at type-definition sites. Match on a lexical * mask so comments, literals and directives cannot supply declaration evidence; * replace just the keyword, preserving source offsets on both parser arms.

(source: string)

Source from the content-addressed store, hash-verified

1730 * mask so comments, literals and directives cannot supply declaration evidence;
1731 * replace just the keyword, preserving source offsets on both parser arms. */
1732function normalizeCppComInterfaces(source: string): string {
1733 if (!source.includes('interface')) return source;
1734 // Raw strings have already been masked by preParseCppSource.
1735 let code = maskCppCommentsAndLiterals(source);
1736 const hasAlias = /^[ \t]*#[ \t]*define[ \t]+interface[ \t]+struct\b/m.test(code);
1737 code = code.replace(/^[ \t]*#(?:\\\r?\n|[^\r\n])*/gm, (m) => m.replace(/[^\r\n]/g, '\0'));
1738 const declaration = /(^|[;{}])\s*\binterface\s+[A-Za-z_]\w*\s*(:[^;{}]+)?\{/gm;
1739 const offsets: number[] = [];
1740 let match: RegExpExecArray | null;
1741 while ((match = declaration.exec(code)) !== null) {
1742 const bodyEnd = code.indexOf('}', declaration.lastIndex);
1743 const hasVirtual = /\bvirtual\b/.test(code.slice(declaration.lastIndex, bodyEnd < 0 ? code.length : bodyEnd));
1744 if (!hasAlias && !match[2] && !hasVirtual) continue;
1745 offsets.push(match.index + match[0].indexOf('interface'));
1746 declaration.lastIndex--; // allow the opening brace to start a nested declaration
1747 }
1748 for (const offset of offsets) {
1749 source = source.slice(0, offset) + 'struct ' + source.slice(offset + 9);
1750 }
1751 return source;
1752}
1753
1754/** C/C++ source pre-processing before tree-sitter: recover macro-annotated class
1755 * definitions, macro-prefixed function definitions, macro-prefixed members, and

Callers 1

preParseCppSourceFunction · 0.85

Calls 3

pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected