* The project's namespace-opening macros — `#define FMT_BEGIN_NAMESPACE * namespace fmt { inline namespace v12 {`, `#define RAPIDJSON_NAMESPACE_BEGIN * namespace RAPIDJSON_NAMESPACE {` (through `#define RAPIDJSON_NAMESPACE * rapidjson`) — as the namespace path each opens (inline namespaces are *
(context: ResolutionContext)
| 70 | * transparent), and the closing macros as how many scopes each closes. |
| 71 | */ |
| 72 | function cppNamespaceMacros(context: ResolutionContext): CppNamespaceMacros { |
| 73 | const hit = CPP_NS_MACROS.get(context); |
| 74 | if (hit) return hit; |
| 75 | const openers = new Map<string, string[]>(); |
| 76 | // `#define PYBIND11_NAMESPACE_BEGIN(name) namespace name {`, used as `PYBIND11_NAMESPACE_BEGIN(detail)`. |
| 77 | const openerFns = new Set<string>(); |
| 78 | const closers = new Map<string, number>(); |
| 79 | const aliases = new Map<string, string>(); |
| 80 | const bodies: Array<[string, string]> = []; |
| 81 | for (const file of context.getAllFiles()) { |
| 82 | if (!/\.(?:h|hh|hpp|hxx|h\+\+|inl|ipp|tcc)$/i.test(file)) continue; |
| 83 | const raw = context.readFile(file); |
| 84 | if (!raw || !raw.includes('#') || !raw.includes('define')) continue; |
| 85 | const source = stripCommentsForRegex(raw.replace(/\\\r?\n/g, ' '), 'cpp'); |
| 86 | for (const m of source.matchAll(/^[ \t]*#[ \t]*define[ \t]+([A-Za-z_]\w*)(\(\s*([A-Za-z_]\w*)?\s*\))?[ \t]+([^\n]*)$/gm)) { |
| 87 | const body = m[4]!.trim(); |
| 88 | if (m[2] !== undefined) { |
| 89 | // (a trailing pragma macro — `PYBIND11_WARNING_PUSH` — rides along) |
| 90 | if (m[3] && new RegExp(`^namespace\\s+${m[3]}\\s*\\{[\\w\\s]*$`).test(body)) openerFns.add(m[1]!); |
| 91 | else if (CPP_CLOSER_BODY.test(body)) closers.set(m[1]!, (body.match(/\}/g) ?? []).length); |
| 92 | continue; |
| 93 | } |
| 94 | if (/^[A-Za-z_]\w*$/.test(body)) aliases.set(m[1]!, body); |
| 95 | else if (CPP_CLOSER_BODY.test(body)) closers.set(m[1]!, (body.match(/\}/g) ?? []).length); |
| 96 | // An inline namespace (transparent, and often named by a macro call) is skipped. |
| 97 | else if (/^(?:inline\s+namespace\s+[^{}]*\{\s*|namespace\s+[A-Za-z_]\w*\s*\{\s*)+[\w\s]*$/.test(body)) bodies.push([m[1]!, body]); |
| 98 | } |
| 99 | } |
| 100 | for (const [name, body] of bodies) { |
| 101 | if (openers.has(name)) continue; |
| 102 | const path = [...body.replace(/inline\s+namespace\s+[^{}]*\{/g, '').matchAll(/namespace\s+([A-Za-z_]\w*)/g)] |
| 103 | .map((m) => aliases.get(m[1]!) ?? m[1]!); |
| 104 | if (path.length > 0) openers.set(name, path); |
| 105 | } |
| 106 | const macros = { openers, openerFns, closers, aliases }; |
| 107 | CPP_NS_MACROS.set(context, macros); |
| 108 | return macros; |
| 109 | } |
| 110 | |
| 111 | /** The outermost namespaces the project's namespace-opening macros open: `fmt` for `FMT_BEGIN_NAMESPACE`. */ |
| 112 | export function cppMacroNamespaceHeads(context: ResolutionContext): string[] { |
no test coverage detected