| 27 | } _PyCodeLocationInfoKind; |
| 28 | |
| 29 | static bool |
| 30 | parse_linetable(const uintptr_t addrq, const std::string& linetable, int firstlineno, LocationInfo* info) |
| 31 | { |
| 32 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(linetable.c_str()); |
| 33 | uint64_t addr = 0; |
| 34 | info->lineno = firstlineno; |
| 35 | |
| 36 | auto scan_varint = [&]() { |
| 37 | unsigned int read = *ptr++; |
| 38 | unsigned int val = read & 63; |
| 39 | unsigned int shift = 0; |
| 40 | while (read & 64) { |
| 41 | read = *ptr++; |
| 42 | shift += 6; |
| 43 | val |= (read & 63) << shift; |
| 44 | } |
| 45 | return val; |
| 46 | }; |
| 47 | |
| 48 | auto scan_signed_varint = [&]() { |
| 49 | unsigned int uval = scan_varint(); |
| 50 | int sval = uval >> 1; |
| 51 | int sign = (uval & 1) ? -1 : 1; |
| 52 | return sign * sval; |
| 53 | }; |
| 54 | |
| 55 | while (*ptr != '\0') { |
| 56 | uint8_t first_byte = *(ptr++); |
| 57 | uint8_t code = (first_byte >> 3) & 15; |
| 58 | size_t length = (first_byte & 7) + 1; |
| 59 | uintptr_t end_addr = addr + length; |
| 60 | switch (code) { |
| 61 | case PY_CODE_LOCATION_INFO_NONE: { |
| 62 | break; |
| 63 | } |
| 64 | case PY_CODE_LOCATION_INFO_LONG: { |
| 65 | int line_delta = scan_signed_varint(); |
| 66 | info->lineno += line_delta; |
| 67 | info->end_lineno = info->lineno + scan_varint(); |
| 68 | info->column = scan_varint() - 1; |
| 69 | info->end_column = scan_varint() - 1; |
| 70 | break; |
| 71 | } |
| 72 | case PY_CODE_LOCATION_INFO_NO_COLUMNS: { |
| 73 | int line_delta = scan_signed_varint(); |
| 74 | info->lineno += line_delta; |
| 75 | info->column = info->end_column = -1; |
| 76 | break; |
| 77 | } |
| 78 | case PY_CODE_LOCATION_INFO_ONE_LINE0: |
| 79 | case PY_CODE_LOCATION_INFO_ONE_LINE1: |
| 80 | case PY_CODE_LOCATION_INFO_ONE_LINE2: { |
| 81 | int line_delta = code - 10; |
| 82 | info->lineno += line_delta; |
| 83 | info->end_lineno = info->lineno; |
| 84 | info->column = *(ptr++); |
| 85 | info->end_column = *(ptr++); |
| 86 | break; |