summary refs log tree commit diff
path: root/src/librustc_codegen_ssa/coverageinfo/map.rs
blob: 3bd262cf2b21374915a0b2ff9c9bc82cf2cd82cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use rustc_data_structures::fx::FxHashMap;
use std::collections::hash_map;
use std::slice;

#[derive(Copy, Clone, Debug)]
pub enum CounterOp {
    Add,
    Subtract,
}

pub enum CoverageKind {
    Counter,
    CounterExpression(u32, CounterOp, u32),
}

pub struct CoverageSpan {
    pub start_byte_pos: u32,
    pub end_byte_pos: u32,
}

pub struct CoverageRegion {
    pub kind: CoverageKind,
    pub coverage_span: CoverageSpan,
}

/// Collects all of the coverage regions associated with (a) injected counters, (b) counter
/// expressions (additions or subtraction), and (c) unreachable regions (always counted as zero),
/// for a given Function. Counters and counter expressions are indexed because they can be operands
/// in an expression.
///
/// Note, it's important to distinguish the `unreachable` region type from what LLVM's refers to as
/// a "gap region" (or "gap area"). A gap region is a code region within a counted region (either
/// counter or expression), but the line or lines in the gap region are not executable (such as
/// lines with only whitespace or comments). According to LLVM Code Coverage Mapping documentation,
/// "A count for a gap area is only used as the line execution count if there are no other regions
/// on a line."
#[derive(Default)]
pub struct FunctionCoverageRegions {
    indexed: FxHashMap<u32, CoverageRegion>,
    unreachable: Vec<CoverageSpan>,
}

impl FunctionCoverageRegions {
    pub fn add_counter(&mut self, index: u32, start_byte_pos: u32, end_byte_pos: u32) {
        self.indexed.insert(
            index,
            CoverageRegion {
                kind: CoverageKind::Counter,
                coverage_span: CoverageSpan { start_byte_pos, end_byte_pos },
            },
        );
    }

    pub fn add_counter_expression(
        &mut self,
        index: u32,
        lhs: u32,
        op: CounterOp,
        rhs: u32,
        start_byte_pos: u32,
        end_byte_pos: u32,
    ) {
        self.indexed.insert(
            index,
            CoverageRegion {
                kind: CoverageKind::CounterExpression(lhs, op, rhs),
                coverage_span: CoverageSpan { start_byte_pos, end_byte_pos },
            },
        );
    }

    pub fn add_unreachable(&mut self, start_byte_pos: u32, end_byte_pos: u32) {
        self.unreachable.push(CoverageSpan { start_byte_pos, end_byte_pos });
    }

    pub fn indexed_regions(&self) -> hash_map::Iter<'_, u32, CoverageRegion> {
        self.indexed.iter()
    }

    pub fn unreachable_regions(&self) -> slice::Iter<'_, CoverageSpan> {
        self.unreachable.iter()
    }
}