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