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

Function cppMacroMethods

src/extraction/languages/cpp-macro-methods.ts:92–149  ·  view source on GitHub ↗

* The method nodes, and their `contains` edges, for every definition the * file's own method-defining macros expand to where the file invokes them at * namespace scope. `nodes` are the file's extracted nodes: the class a * method's receiver names, and the ids already taken.

(
  filePath: string,
  source: string,
  preParse: (source: string) => string,
  nodes: readonly Node[],
)

Source from the content-addressed store, hash-verified

90 * method's receiver names, and the ids already taken.
91 */
92function cppMacroMethods(
93 filePath: string,
94 source: string,
95 preParse: (source: string) => string,
96 nodes: readonly Node[],
97): { nodes: Node[]; edges: Edge[] } {
98 const out = { nodes: [] as Node[], edges: [] as Edge[] };
99 const { macros, undefs } = cppMacroDirectives(source);
100 const defining = macros.filter((m) => cppDefinitionHeads(m.body.replace(/\s*##\s*/g, '')).length > 0);
101 if (defining.length === 0) return out;
102 // Scopes as the parser saw them (after preParse, offsets unchanged), like the walker's.
103 const scopes = scanCppBraceScopes(preParse(source));
104 if (!scopes) return out;
105
106 const names = new Set(defining.map((m) => m.name));
107 const taken = new Set(nodes.map((n) => n.id));
108 const fileId = `file:${filePath}`;
109 for (const call of cppMacroInvocations(source, names)) {
110 if (!scopes.atNamespaceScope(call.at)) continue;
111 const macro = liveMacro(defining, undefs, call.name, call.at);
112 const expansion = macro && expandCppMacro(macro, call.args);
113 if (!expansion) continue;
114 const start = scopes.positionOf(call.at);
115 const end = scopes.positionOf(call.end);
116 const prefix = scopes.namespacesAt(call.at);
117 for (const def of cppDefinitionHeads(expansion)) {
118 const receiverLast = def.receiver.split('::').pop()!;
119 // A constructor's or destructor's.
120 if (def.name === receiverLast || def.name.startsWith('~')) continue;
121 let id = generateNodeId(filePath, 'method', def.name, start.line);
122 if (taken.has(id)) id = `${id}:${start.column}`;
123 for (let k = 1; taken.has(id); k++) id = `${generateNodeId(filePath, 'method', def.name, start.line)}:${start.column}:${k}`;
124 taken.add(id);
125 const returnType = normalizeCppReturnType(cppTypeText(def.returnText));
126 out.nodes.push({
127 id,
128 kind: 'method',
129 name: def.name,
130 qualifiedName: composeQualifiedName(prefix, def.receiver, def.name),
131 filePath,
132 language: 'cpp',
133 startLine: start.line,
134 endLine: end.line,
135 startColumn: start.column,
136 endColumn: end.column,
137 signature: def.head,
138 ...(returnType ? { returnType } : {}),
139 updatedAt: Date.now(),
140 });
141 out.edges.push({ source: fileId, target: id, kind: 'contains' });
142 // As for an out-of-line definition, the class of its receiver's name in the same file holds it.
143 const owner = nodes.find((n) => n.name === def.receiver && n.filePath === filePath &&
144 (n.kind === 'struct' || n.kind === 'union' || n.kind === 'class' || n.kind === 'enum' || n.kind === 'trait'));
145 if (owner) out.edges.push({ source: owner.id, target: id, kind: 'contains' });
146 }
147 }
148 return out;
149}

Callers 1

appendCppMacroMethodsFunction · 0.85

Calls 15

scanCppBraceScopesFunction · 0.90
generateNodeIdFunction · 0.90
normalizeCppReturnTypeFunction · 0.90
cppMacroDirectivesFunction · 0.85
cppDefinitionHeadsFunction · 0.85
cppMacroInvocationsFunction · 0.85
liveMacroFunction · 0.85
expandCppMacroFunction · 0.85
cppTypeTextFunction · 0.85
composeQualifiedNameFunction · 0.85
mapMethod · 0.80
atNamespaceScopeMethod · 0.80

Tested by

no test coverage detected