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

Function cppMacroMethods

src/extraction/languages/cpp-macro-methods.ts:93–150  ·  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

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

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