(file: string, context: ResolutionContext)
| 124 | |
| 125 | /** The line ranges of a C / C++ file each namespace macro opens, with the namespace path it opens. */ |
| 126 | export function cppMacroNamespaceFrames(file: string, context: ResolutionContext): Array<{ start: number; end: number; path: string[] }> { |
| 127 | let memo = CPP_NS_FRAMES.get(context); |
| 128 | if (!memo) { |
| 129 | memo = new Map(); |
| 130 | CPP_NS_FRAMES.set(context, memo); |
| 131 | } |
| 132 | const hit = memo.get(file); |
| 133 | if (hit) return hit; |
| 134 | const frames: Array<{ start: number; end: number; path: string[] }> = []; |
| 135 | const { openers, openerFns, closers, aliases } = cppNamespaceMacros(context); |
| 136 | if (openers.size > 0 || openerFns.size > 0) { |
| 137 | const lines = context.getFileLines?.(file) ?? context.readFile(file)?.split(/\r?\n/) ?? []; |
| 138 | const open: Array<{ start: number; path: string[] }> = []; |
| 139 | lines.forEach((text, i) => { |
| 140 | const m = /^[ \t]*([A-Z_][A-Z0-9_]*)(?:\(\s*([A-Za-z_]\w*)?\s*\))?[ \t]*;?[ \t]*(?:\/\/.*|\/\*.*\*\/[ \t]*)?\r?$/.exec(text); |
| 141 | const token = m?.[1]; |
| 142 | if (!token) return; |
| 143 | const arg = m[2]; |
| 144 | const path = arg !== undefined && openerFns.has(token) ? [aliases.get(arg) ?? arg] : arg === undefined ? openers.get(token) : undefined; |
| 145 | if (path) open.push({ start: i + 1, path }); |
| 146 | else if (closers.has(token) && open.length > 0) frames.push({ ...open.pop()!, end: i + 1 }); |
| 147 | }); |
| 148 | for (const frame of open) frames.push({ ...frame, end: lines.length }); |
| 149 | } |
| 150 | memo.set(file, frames); |
| 151 | return frames; |
| 152 | } |
| 153 | |
| 154 | /** The files the scans above read: every C and C++ source and header. */ |
| 155 | const CPP_SCANNED_FILE = /\.(?:h|hh|hpp|hxx|h\+\+|inl|ipp|tcc|c|cc|cpp|cxx)$/i; |
no test coverage detected