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

Function extractObjcpp

src/extraction/objcpp-extractor.ts:96–170  ·  view source on GitHub ↗
(filePath: string, source: string, run: Run)

Source from the content-addressed store, hash-verified

94 * or can't be split: the caller reads it as Objective-C.
95 */
96export function extractObjcpp(filePath: string, source: string, run: Run): ExtractionResult | null {
97 const startTime = Date.now();
98 let parts = splitObjcpp(source);
99 if (!parts) return null;
100 const lineStarts = lineStartsOf(source);
101 const offsetOf = (line: number, column: number): number => (lineStarts[line - 1] ?? source.length) + column;
102
103 let cpp = run(parts.cpp, 'cpp');
104 let checked = checkCppPass(cpp, parts, offsetOf);
105 if (checked === null) return null;
106 if (checked.misread.size > 0) {
107 parts = splitObjcpp(source, checked.misread);
108 if (!parts) return null;
109 cpp = run(parts.cpp, 'cpp');
110 checked = checkCppPass(cpp, parts, offsetOf);
111 if (checked === null || checked.misread.size > 0) return null;
112 }
113 // An item the C++ pass found nothing in is one it has no symbol for, as a
114 // file-scope constant: it stays with the Objective-C grammar, which may.
115 const kept = parts.items.filter((item, k) => item.bare || checked!.found[k]! > 0);
116 const keptStarts = kept.map((item) => item.start);
117 const inKept = (offset: number): boolean => {
118 const k = lastStartingBefore(keptStarts, offset);
119 return k >= 0 && offset < kept[k]!.end;
120 };
121
122 const cppNodes: Node[] = [];
123 // The C++ functions and methods by their extent, to find the one a message send is written in.
124 const owners = new NestedIntervals<string>();
125 for (const node of cpp.nodes) {
126 if (node.kind === 'file') continue;
127 cppNodes.push(node);
128 if (node.kind === 'function' || node.kind === 'method') {
129 owners.add(offsetOf(node.startLine, node.startColumn) - 1, offsetOf(node.endLine, node.endColumn), node.id);
130 }
131 }
132 // Nothing C++ to keep (a `using` declaration, a forward declaration): the file is read as before.
133 if (cppNodes.length === 0) return null;
134
135 const objc = run(withoutObjcppItems(source, kept), 'objc');
136 const fileId = `file:${filePath}`;
137
138 const references: UnresolvedReference[] = objc.unresolvedReferences;
139 for (const ref of cpp.unresolvedReferences) {
140 if (!inKept(offsetOf(ref.line, ref.column))) continue;
141 ref.language = 'cpp';
142 ref.filePath = filePath;
143 references.push(ref);
144 }
145 if (parts.sends !== null) {
146 // Only what is written inside a rewritten expression: the C++ pass saw `(0 …)` there.
147 const spanStarts = parts.spans.map((span) => span[0]);
148 for (const ref of run(parts.sends, 'objc').unresolvedReferences) {
149 const offset = offsetOf(ref.line, ref.column);
150 const k = lastStartingBefore(spanStarts, offset);
151 if (k < 0 || offset >= parts.spans[k]![1] || !inKept(offset)) continue;
152 const holders = owners.at(offset);
153 ref.fromNodeId = holders.length > 0 ? holders[holders.length - 1]! : fileId;

Callers 1

extractFromSourceFunction · 0.90

Calls 12

addMethod · 0.95
atMethod · 0.95
splitObjcppFunction · 0.90
withoutObjcppItemsFunction · 0.90
checkCppPassFunction · 0.85
inKeptFunction · 0.85
lastStartingBeforeFunction · 0.85
mapMethod · 0.80
pushMethod · 0.80
lineStartsOfFunction · 0.70
offsetOfFunction · 0.70
runFunction · 0.50

Tested by

no test coverage detected