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

Function cppNamespaceMacros

src/resolution/cpp-namespaces.ts:72–109  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

70 * transparent), and the closing macros as how many scopes each closes.
71 */
72function 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`. */
112export function cppMacroNamespaceHeads(context: ResolutionContext): string[] {

Callers 4

cppMacroNamespaceHeadsFunction · 0.85
cppNameMacrosFunction · 0.85
cppMacroNamespaceFramesFunction · 0.85
cppNamespaceScansFunction · 0.85

Calls 9

stripCommentsForRegexFunction · 0.90
readFileMethod · 0.80
pushMethod · 0.80
hasMethod · 0.80
mapMethod · 0.80
getMethod · 0.65
getAllFilesMethod · 0.65
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected