pub const RangeIterator = struct annotations: Iteratorcurr: PartialRangeprev: ?PartialRangeThe previous range is tracked as the code length is sometimes implied by the subsequent range.
pub fn init(annotations: Iterator) RangeIteratorpub fn next(self: *RangeIterator) error323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446 pub const RangeIterator = struct {
annotations: Iterator,
curr: PartialRange,
/// The previous range is tracked as the code length is sometimes implied by the subsequent
/// range.
prev: ?PartialRange,
const PartialRange = struct {
line_offset: i32,
file_id: ?u32,
code_offset: u32,
code_length: ?u32,
/// Resolves a partial range to a range with a definite length, or returns null if this
/// is not possible.
fn resolve(self: PartialRange, next_code_offset: ?u32) ?Range {
return .{
.line_offset = self.line_offset,
.file_id = self.file_id,
.code_offset = self.code_offset,
.code_length = b: {
if (self.code_length) |l| break :b l;
const end = next_code_offset orelse return null;
break :b end - self.code_offset;
},
};
}
};
pub fn init(annotations: Iterator) RangeIterator {
return .{
.annotations = annotations,
.curr = .{
.line_offset = 0,
.file_id = null,
.code_offset = 0,
.code_length = null,
},
.prev = null,
};
}
pub const Range = struct {
line_offset: i32,
file_id: ?u32,
code_offset: u32,
code_length: u32,
pub fn contains(self: Range, offset_in_func: usize) bool {
return self.code_offset <= offset_in_func and
offset_in_func < self.code_offset + self.code_length;
}
};
pub fn next(self: *RangeIterator) error{InvalidDebugInfo}!?Range {
while (try self.annotations.next()) |annotation| {
switch (annotation) {
.change_code_offset => |delta| {
self.curr.code_offset += delta;
},
.change_code_length => |length| {
if (self.prev) |*prev| prev.code_length = prev.code_length orelse length;
self.curr.code_offset += length;
},
// LLVM has code to emit these, but I wasn't able to figure out how trigger it
// so this logic is untested.
.change_file => |file_id| {
self.curr.file_id = file_id;
},
// LLVM never emits this opcode, but it's clear enough how to interpret it so we
// may as well handle it in case they emit it in the future
.change_code_length_and_code_offset => |info| {
self.curr.code_length = info.length;
self.curr.code_offset += info.delta;
},
.change_line_offset => |delta| {
self.curr.line_offset += delta;
},
.change_code_offset_and_line_offset => |info| {
self.curr.code_offset += info.code_delta;
self.curr.line_offset += info.line_delta;
},
// Not emitted by LLVM at the time of writing, and we don't want to add support
// without a test case. Safe to ignore since we don't use this info right now.
.change_line_end_delta,
.change_column_start,
.change_column_end_delta,
.change_column_end,
=> {},
// Not emitted by LLVM at the time of writing. Various sources conflict on how
// these opcodes should be interpreted, so we make no attempt to handle them.
.code_offset,
.change_code_offset_base,
.change_range_kind,
=> {
self.annotations = .empty;
self.prev = null;
return null;
},
}
// If we have a new code offset, return the previous range if it exists, resolving
// its length if necessary.
switch (annotation) {
.change_code_offset,
.change_code_offset_and_line_offset,
.change_code_length_and_code_offset,
=> {},
else => continue,
}
defer self.prev = self.curr;
const prev = self.prev orelse continue;
return prev.resolve(self.curr.code_offset);
}
// If we've processed all the binary operations but still have a previous range leftover
// with a known length, return it.
const prev = self.prev orelse return null;
defer self.prev = null;
return prev.resolve(null);
}
}
A virtual machine that processed binary annotations.