|
| 1 | +use rustc_middle::mir::coverage::FunctionCoverageInfo; |
| 2 | +use rustc_span::source_map::SourceMap; |
| 3 | +use rustc_span::{BytePos, Pos, SourceFile, Span}; |
| 4 | +use tracing::debug; |
| 5 | + |
| 6 | +use crate::coverageinfo::ffi; |
| 7 | +use crate::coverageinfo::mapgen::LocalFileId; |
| 8 | + |
| 9 | +/// Converts the span into its start line and column, and end line and column. |
| 10 | +/// |
| 11 | +/// Line numbers and column numbers are 1-based. Unlike most column numbers emitted by |
| 12 | +/// the compiler, these column numbers are denoted in **bytes**, because that's what |
| 13 | +/// LLVM's `llvm-cov` tool expects to see in coverage maps. |
| 14 | +/// |
| 15 | +/// Returns `None` if the conversion failed for some reason. This shouldn't happen, |
| 16 | +/// but it's hard to rule out entirely (especially in the presence of complex macros |
| 17 | +/// or other expansions), and if it does happen then skipping a span or function is |
| 18 | +/// better than an ICE or `llvm-cov` failure that the user might have no way to avoid. |
| 19 | +pub(crate) fn make_coverage_span( |
| 20 | + file_id: LocalFileId, |
| 21 | + source_map: &SourceMap, |
| 22 | + fn_cov_info: &FunctionCoverageInfo, |
| 23 | + file: &SourceFile, |
| 24 | + span: Span, |
| 25 | +) -> Option<ffi::CoverageSpan> { |
| 26 | + let span = ensure_non_empty_span(source_map, fn_cov_info, span)?; |
| 27 | + |
| 28 | + let lo = span.lo(); |
| 29 | + let hi = span.hi(); |
| 30 | + |
| 31 | + // Column numbers need to be in bytes, so we can't use the more convenient |
| 32 | + // `SourceMap` methods for looking up file coordinates. |
| 33 | + let line_and_byte_column = |pos: BytePos| -> Option<(usize, usize)> { |
| 34 | + let rpos = file.relative_position(pos); |
| 35 | + let line_index = file.lookup_line(rpos)?; |
| 36 | + let line_start = file.lines()[line_index]; |
| 37 | + // Line numbers and column numbers are 1-based, so add 1 to each. |
| 38 | + Some((line_index + 1, (rpos - line_start).to_usize() + 1)) |
| 39 | + }; |
| 40 | + |
| 41 | + let (mut start_line, start_col) = line_and_byte_column(lo)?; |
| 42 | + let (mut end_line, end_col) = line_and_byte_column(hi)?; |
| 43 | + |
| 44 | + // Apply an offset so that code in doctests has correct line numbers. |
| 45 | + // FIXME(#79417): Currently we have no way to offset doctest _columns_. |
| 46 | + start_line = source_map.doctest_offset_line(&file.name, start_line); |
| 47 | + end_line = source_map.doctest_offset_line(&file.name, end_line); |
| 48 | + |
| 49 | + check_coverage_span(ffi::CoverageSpan { |
| 50 | + file_id: file_id.as_u32(), |
| 51 | + start_line: start_line as u32, |
| 52 | + start_col: start_col as u32, |
| 53 | + end_line: end_line as u32, |
| 54 | + end_col: end_col as u32, |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +fn ensure_non_empty_span( |
| 59 | + source_map: &SourceMap, |
| 60 | + fn_cov_info: &FunctionCoverageInfo, |
| 61 | + span: Span, |
| 62 | +) -> Option<Span> { |
| 63 | + if !span.is_empty() { |
| 64 | + return Some(span); |
| 65 | + } |
| 66 | + |
| 67 | + let lo = span.lo(); |
| 68 | + let hi = span.hi(); |
| 69 | + |
| 70 | + // The span is empty, so try to expand it to cover an adjacent '{' or '}', |
| 71 | + // but only within the bounds of the body span. |
| 72 | + let try_next = hi < fn_cov_info.body_span.hi(); |
| 73 | + let try_prev = fn_cov_info.body_span.lo() < lo; |
| 74 | + if !(try_next || try_prev) { |
| 75 | + return None; |
| 76 | + } |
| 77 | + |
| 78 | + source_map |
| 79 | + .span_to_source(span, |src, start, end| try { |
| 80 | + // We're only checking for specific ASCII characters, so we don't |
| 81 | + // have to worry about multi-byte code points. |
| 82 | + if try_next && src.as_bytes()[end] == b'{' { |
| 83 | + Some(span.with_hi(hi + BytePos(1))) |
| 84 | + } else if try_prev && src.as_bytes()[start - 1] == b'}' { |
| 85 | + Some(span.with_lo(lo - BytePos(1))) |
| 86 | + } else { |
| 87 | + None |
| 88 | + } |
| 89 | + }) |
| 90 | + .ok()? |
| 91 | +} |
| 92 | + |
| 93 | +/// If `llvm-cov` sees a source region that is improperly ordered (end < start), |
| 94 | +/// it will immediately exit with a fatal error. To prevent that from happening, |
| 95 | +/// discard regions that are improperly ordered, or might be interpreted in a |
| 96 | +/// way that makes them improperly ordered. |
| 97 | +fn check_coverage_span(cov_span: ffi::CoverageSpan) -> Option<ffi::CoverageSpan> { |
| 98 | + let ffi::CoverageSpan { file_id: _, start_line, start_col, end_line, end_col } = cov_span; |
| 99 | + |
| 100 | + // Line/column coordinates are supposed to be 1-based. If we ever emit |
| 101 | + // coordinates of 0, `llvm-cov` might misinterpret them. |
| 102 | + let all_nonzero = [start_line, start_col, end_line, end_col].into_iter().all(|x| x != 0); |
| 103 | + // Coverage mappings use the high bit of `end_col` to indicate that a |
| 104 | + // region is actually a "gap" region, so make sure it's unset. |
| 105 | + let end_col_has_high_bit_unset = (end_col & (1 << 31)) == 0; |
| 106 | + // If a region is improperly ordered (end < start), `llvm-cov` will exit |
| 107 | + // with a fatal error, which is inconvenient for users and hard to debug. |
| 108 | + let is_ordered = (start_line, start_col) <= (end_line, end_col); |
| 109 | + |
| 110 | + if all_nonzero && end_col_has_high_bit_unset && is_ordered { |
| 111 | + Some(cov_span) |
| 112 | + } else { |
| 113 | + debug!( |
| 114 | + ?cov_span, |
| 115 | + ?all_nonzero, |
| 116 | + ?end_col_has_high_bit_unset, |
| 117 | + ?is_ordered, |
| 118 | + "Skipping source region that would be misinterpreted or rejected by LLVM" |
| 119 | + ); |
| 120 | + // If this happens in a debug build, ICE to make it easier to notice. |
| 121 | + debug_assert!(false, "Improper source region: {cov_span:?}"); |
| 122 | + None |
| 123 | + } |
| 124 | +} |
0 commit comments