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

Function walkTranslationUnit

src/resolution/cpp-macro-visibility.ts:284–405  ·  view source on GitHub ↗
(
  rootFile: string,
  language: UnresolvedRef['language'],
  context: ResolutionContext,
  cache: Cache,
  // Follow this macro alone, and whether it is defined, not whether it is function-like.
  watched?: string
)

Source from the content-addressed store, hash-verified

282}
283
284function walkTranslationUnit(
285 rootFile: string,
286 language: UnresolvedRef['language'],
287 context: ResolutionContext,
288 cache: Cache,
289 // Follow this macro alone, and whether it is defined, not whether it is function-like.
290 watched?: string
291): Timeline {
292 const timeline = new Map<string, Event[]>();
293 const definitions = new Map<string, { defined: Truth; value: Truth; macro: Truth }>();
294 const scanning = new Set<string>();
295 const macroNames = new Set<string>();
296 const once = new Map<string, Truth>();
297 // Every change a later directive could observe (definitions, `#pragma once`,
298 // the macro-name set) bumps `version`. A visit that left it unchanged, entered
299 // again with the same inherited truth while it is still unchanged, starts from
300 // the same state, so it would take the same branches and change nothing again;
301 // the events it would push repeat each name's current state, which is already
302 // its last event. Include cycles cut by the recursion stack (`cuts`) must still
303 // be cut for the repeat to hold.
304 let version = 0;
305 const cuts: string[] = [];
306 const visits = new Map<string, { inherited: Truth; start: number; end: number; cuts: string[] }>();
307 let budget = WALK_EVENT_BUDGET;
308 let cutoff = Infinity;
309 const condition = (expression: string): Truth => {
310 const text = expression.trim();
311 if (/^(?:0x[\da-f]+|\d+)[uUlL]*$/i.test(text)) return Number(text.replace(/[uUlL]+$/, '')) !== 0;
312 const def = text.match(/^(!)?\s*defined\s*(?:\(\s*(\w+)\s*\)|(\w+))$/);
313 if (def) {
314 const known = definitions.get(def[2] ?? def[3]!)?.defined;
315 return def[1] ? not(known) : known;
316 }
317 return /^\w+$/.test(text) ? definitions.get(text)?.value : undefined;
318 };
319 const scan = (file: string, inherited: Truth, includeLine?: number): void => {
320 if (inherited === false || once.get(file) === true || cutoff !== Infinity) return;
321 if (scanning.has(file)) {
322 cuts.push(file);
323 return;
324 }
325 const seen = visits.get(file);
326 if (
327 seen && seen.inherited === inherited && seen.start === seen.end && seen.end === version &&
328 seen.cuts.every((c) => scanning.has(c))
329 ) {
330 cuts.push(...seen.cuts);
331 return;
332 }
333 const start = version;
334 const firstCut = cuts.length;
335 scanning.add(file);
336 let active: Truth = inherited;
337 const frames: Array<{ parent: Truth; taken: Truth }> = [];
338 for (const ev of summarize(file, context, cache)) {
339 const line = includeLine ?? ev.line;
340 if (cutoff === Infinity && --budget < 0) cutoff = line;
341 if (cutoff !== Infinity) break;

Callers 2

isVisibleCppMacroFunction · 0.85
isCppMacroDefinedAtFunction · 0.85

Calls 1

scanFunction · 0.70

Tested by

no test coverage detected