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

Function cppDefinitionHeads

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

The method definitions at the top level of a macro's expansion `text`, in order.

(text: string)

Source from the content-addressed store, hash-verified

303
304/** The method definitions at the top level of a macro's expansion `text`, in order. */
305function cppDefinitionHeads(text: string): CppDefinitionHead[] {
306 const heads: CppDefinitionHead[] = [];
307 let segment = 0; // where the current top-level declaration began
308 for (let i = 0; i < text.length; ) {
309 const c = text[i]!;
310 if (c === '"' || c === "'") {
311 i = quotedEnd(text, i);
312 continue;
313 }
314 if (c === ';' || c === '}') {
315 segment = ++i;
316 continue;
317 }
318 if (c === '{' || c === '(') {
319 // A body or group that is no definition read here (a namespace, a class, a statement).
320 const close = matching(text, i, c, c === '{' ? '}' : ')');
321 if (close < 0) return heads;
322 if (c === '{') segment = close + 1;
323 i = close + 1;
324 continue;
325 }
326 const callable = isWordStart(text.charCodeAt(i)) || (c === ':' && text[i + 1] === ':') ? qualifiedCallableAt(text, i) : null;
327 if (!callable) {
328 const end = wordEnd(text, i);
329 i = end < 0 ? i + 1 : end;
330 continue;
331 }
332 const close = matching(text, callable.open, '(', ')');
333 if (close < 0) return heads;
334 const open = bodyOpenAfter(text, close + 1);
335 if (open < 0) {
336 i = close + 1;
337 continue;
338 }
339 // An explicit specialization's arguments (`Read<int32_t, TYPE_INT32>`) are no part of its name.
340 const last = callable.segments[callable.segments.length - 1]!;
341 const name = last.startsWith('operator') ? last : stripCppTemplateArgs(last);
342 const receiver = stripCppTemplateArgs(callable.segments.slice(0, -1).join('::'));
343 if (receiver) {
344 heads.push({
345 receiver,
346 name,
347 returnText: text.slice(segment, i).trim(),
348 head: text.slice(segment, open).replace(/\s+/g, ' ').trim(),
349 });
350 }
351 const bodyClose = matching(text, open, '{', '}');
352 if (bodyClose < 0) return heads;
353 segment = i = bodyClose + 1;
354 }
355 return heads;
356}
357
358/**
359 * The macro of `name` in effect at `offset`: its last `#define` before it,

Callers 1

cppMacroMethodsFunction · 0.85

Calls 9

stripCppTemplateArgsFunction · 0.90
qualifiedCallableAtFunction · 0.85
bodyOpenAfterFunction · 0.85
pushMethod · 0.80
quotedEndFunction · 0.70
matchingFunction · 0.70
isWordStartFunction · 0.70
wordEndFunction · 0.70
joinMethod · 0.45

Tested by

no test coverage detected