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

Function blankCppAnnotationMacroCalls

src/extraction/languages/c-cpp.ts:1312–1357  ·  view source on GitHub ↗
(source: string, options: { keepCReturnTypes?: boolean } = {})

Source from the content-addressed store, hash-verified

1310 * loses that one annotation, never a whole class.
1311 */
1312export function blankCppAnnotationMacroCalls(source: string, options: { keepCReturnTypes?: boolean } = {}): string {
1313 if (!/^[ \t]*[A-Z][A-Z0-9_]{2,}\s*\(/m.test(source)) return source;
1314 const rawStrings = maskCppRawStrings(source);
1315 source = rawStrings.source;
1316 let chars: string[] | null = null;
1317 const re = /^([ \t]*)([A-Z][A-Z0-9_]{2,})(\s*)\(/gm;
1318 let m: RegExpExecArray | null;
1319 while ((m = re.exec(source)) !== null) {
1320 const macroStart = m.index + (m[1] ?? '').length; // skip leading indent
1321 const open = m.index + m[0].length - 1; // index of the opening '('
1322 let i = open;
1323 let depth = 0;
1324 let end = -1;
1325 for (; i < source.length; i++) {
1326 const c = source[i];
1327 if (c === '"' || c === "'") {
1328 const quote = c;
1329 i++;
1330 while (i < source.length && source[i] !== quote) {
1331 if (source[i] === '\\') i++;
1332 i++;
1333 }
1334 continue;
1335 }
1336 if (c === '(') depth++;
1337 else if (c === ')') {
1338 depth--;
1339 if (depth === 0) { end = i + 1; break; }
1340 }
1341 }
1342 if (end < 0) continue;
1343 let j = end;
1344 while (j < source.length && /\s/.test(source[j] as string)) j++;
1345 const after = source[j];
1346 // Only markup is followed by the declaration it decorates; a statement call
1347 // (`;`), init-list item (`,`/`{`), or expression fragment (operator) is not.
1348 if (!after || !/[A-Za-z_~#]/.test(after)) continue;
1349 if (options.keepCReturnTypes && macroStart === m.index && isCReturnTypeMacro(source, m.index, open, end, j)) continue;
1350 chars ??= source.split('');
1351 for (let k = macroStart; k < end; k++) {
1352 if (chars[k] !== '\n' && chars[k] !== '\r') chars[k] = ' ';
1353 }
1354 re.lastIndex = end;
1355 }
1356 return rawStrings.restore(chars ? chars.join('') : source);
1357}
1358
1359/** A macro argument that reads as a C type: `char *`, `const cJSON *`, `unsigned long`, `git_repository *`. */
1360const C_RETURN_TYPE_ARG_RE = /^\s*[A-Za-z_][\w\s*]*$/;

Callers 6

keepFunction · 0.90
extraction.test.tsFile · 0.90
preParseCppSourceFunction · 0.85
preParseCSourceFunction · 0.85

Calls 5

maskCppRawStringsFunction · 0.85
isCReturnTypeMacroFunction · 0.85
restoreMethod · 0.80
execMethod · 0.65
joinMethod · 0.45

Tested by 1

keepFunction · 0.72