Skip to content

Commit ce510f3

Browse files
authored
refactor(extractors): deduplicate C-family primitive types into shared constant
Extract `C_PRIMITIVE_TYPES` and `isCPrimitiveType` into `helpers.ts` and import them in both `cpp.ts` and `cuda.ts`, eliminating the byte-for-byte duplicate sets that required manual mirroring on every update. Closes #1507 Closes #1518 Impact: 3 functions changed, 6 affected
1 parent 66fc1b9 commit ce510f3

3 files changed

Lines changed: 59 additions & 77 deletions

File tree

src/extractors/cpp.ts

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import type {
55
TreeSitterNode,
66
TreeSitterTree,
77
} from '../types.js';
8-
import { extractModifierVisibility, findChild, nodeEndLine, setTypeMapEntry } from './helpers.js';
8+
import {
9+
extractModifierVisibility,
10+
findChild,
11+
isCPrimitiveType,
12+
nodeEndLine,
13+
setTypeMapEntry,
14+
} from './helpers.js';
915

1016
/**
1117
* Extract symbols from C++ files.
@@ -218,7 +224,7 @@ function handleCppDeclaration(node: TreeSitterNode, ctx: ExtractorOutput): void
218224
if (!typeNode) return;
219225
const typeName = typeNode.text;
220226
// Skip primitive types — they are never class/struct receivers
221-
if (isPrimitiveCppType(typeName)) return;
227+
if (isCPrimitiveType(typeName)) return;
222228
for (let i = 0; i < node.childCount; i++) {
223229
const child = node.child(i);
224230
if (!child) continue;
@@ -361,43 +367,6 @@ function extractCppClassFields(classNode: TreeSitterNode): SubDeclaration[] {
361367
return fields;
362368
}
363369

364-
/**
365-
* Primitive C/C++ types that are never class/struct receivers. Seeding these
366-
* into typeMap would cause spurious receiver edges (e.g. `int x` → `int`).
367-
*/
368-
const CPP_PRIMITIVE_TYPES = new Set([
369-
'int',
370-
'long',
371-
'short',
372-
'unsigned',
373-
'signed',
374-
'float',
375-
'double',
376-
'char',
377-
'bool',
378-
'void',
379-
'wchar_t',
380-
'auto',
381-
'size_t',
382-
'uint8_t',
383-
'uint16_t',
384-
'uint32_t',
385-
'uint64_t',
386-
'int8_t',
387-
'int16_t',
388-
'int32_t',
389-
'int64_t',
390-
'ptrdiff_t',
391-
'intptr_t',
392-
'uintptr_t',
393-
]);
394-
395-
function isPrimitiveCppType(typeName: string): boolean {
396-
// Strip qualifiers like `const`, `volatile`, `unsigned` etc.
397-
const base = typeName.split(/\s+/).pop() ?? typeName;
398-
return CPP_PRIMITIVE_TYPES.has(base) || CPP_PRIMITIVE_TYPES.has(typeName);
399-
}
400-
401370
function extractCppEnumEntries(enumNode: TreeSitterNode): SubDeclaration[] {
402371
const entries: SubDeclaration[] = [];
403372
const body = findChild(enumNode, 'enumerator_list');

src/extractors/cuda.ts

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import type {
55
TreeSitterNode,
66
TreeSitterTree,
77
} from '../types.js';
8-
import { extractModifierVisibility, findChild, nodeEndLine, setTypeMapEntry } from './helpers.js';
8+
import {
9+
extractModifierVisibility,
10+
findChild,
11+
isCPrimitiveType,
12+
nodeEndLine,
13+
setTypeMapEntry,
14+
} from './helpers.js';
915

1016
/**
1117
* Extract symbols from CUDA files.
@@ -218,7 +224,7 @@ function handleCudaDeclaration(node: TreeSitterNode, ctx: ExtractorOutput): void
218224
if (!typeNode) return;
219225
const typeName = typeNode.text;
220226
// Skip primitive types — they are never class/struct receivers
221-
if (isCudaPrimitiveType(typeName)) return;
227+
if (isCPrimitiveType(typeName)) return;
222228
for (let i = 0; i < node.childCount; i++) {
223229
const child = node.child(i);
224230
if (!child) continue;
@@ -411,42 +417,6 @@ function innerCudaDeclarator(node: TreeSitterNode): TreeSitterNode | null {
411417
return null;
412418
}
413419

414-
/**
415-
* Primitive C/C++/CUDA types that are never class/struct receivers. Seeding
416-
* these into typeMap would produce spurious receiver edges (e.g. `int x` → `int`).
417-
*/
418-
const CUDA_PRIMITIVE_TYPES = new Set([
419-
'int',
420-
'long',
421-
'short',
422-
'unsigned',
423-
'signed',
424-
'float',
425-
'double',
426-
'char',
427-
'bool',
428-
'void',
429-
'wchar_t',
430-
'auto',
431-
'size_t',
432-
'uint8_t',
433-
'uint16_t',
434-
'uint32_t',
435-
'uint64_t',
436-
'int8_t',
437-
'int16_t',
438-
'int32_t',
439-
'int64_t',
440-
'ptrdiff_t',
441-
'intptr_t',
442-
'uintptr_t',
443-
]);
444-
445-
function isCudaPrimitiveType(typeName: string): boolean {
446-
const base = typeName.split(/\s+/).pop() ?? typeName;
447-
return CUDA_PRIMITIVE_TYPES.has(base) || CUDA_PRIMITIVE_TYPES.has(typeName);
448-
}
449-
450420
function extractCudaEnumEntries(enumNode: TreeSitterNode): SubDeclaration[] {
451421
const entries: SubDeclaration[] = [];
452422
const body = findChild(enumNode, 'enumerator_list');

src/extractors/helpers.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,49 @@ export function pushImport(
305305
ctx.imports.push(entry);
306306
}
307307

308+
// ── C-family primitive types ───────────────────────────────────────────────
309+
310+
/**
311+
* Primitive C/C++/CUDA types that are never class/struct receivers. Seeding
312+
* these into typeMap would produce spurious receiver edges (e.g. `int x` → `int`).
313+
* Shared between the C++ and CUDA extractors to prevent divergence.
314+
*/
315+
export const C_PRIMITIVE_TYPES: ReadonlySet<string> = new Set([
316+
'int',
317+
'long',
318+
'short',
319+
'unsigned',
320+
'signed',
321+
'float',
322+
'double',
323+
'char',
324+
'bool',
325+
'void',
326+
'wchar_t',
327+
'auto',
328+
'size_t',
329+
'uint8_t',
330+
'uint16_t',
331+
'uint32_t',
332+
'uint64_t',
333+
'int8_t',
334+
'int16_t',
335+
'int32_t',
336+
'int64_t',
337+
'ptrdiff_t',
338+
'intptr_t',
339+
'uintptr_t',
340+
]);
341+
342+
/**
343+
* Return true when `typeName` is a primitive C/C++/CUDA type.
344+
* Strips leading qualifiers (`const int` → `int`) before checking.
345+
*/
346+
export function isCPrimitiveType(typeName: string): boolean {
347+
const base = typeName.split(/\s+/).pop() ?? typeName;
348+
return C_PRIMITIVE_TYPES.has(base) || C_PRIMITIVE_TYPES.has(typeName);
349+
}
350+
308351
// ── Parameter extraction ───────────────────────────────────────────────────
309352

310353
/**

0 commit comments

Comments
 (0)