• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xd009642 / llvm-profparser / #201

20 May 2025 06:24AM UTC coverage: 69.494% (-0.3%) from 69.822%
#201

push

web-flow
Try from a file (#50)

* Try from a file

* Add buffered file IO

* fmt

14 of 14 new or added lines in 1 file covered. (100.0%)

8 existing lines in 1 file now uncovered.

1180 of 1698 relevant lines covered (69.49%)

3.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.04
/src/coverage/coverage_mapping.rs
1
use crate::coverage::reporting::*;
2
use crate::coverage::*;
3
use crate::instrumentation_profile::types::*;
4
use crate::util::*;
5
use anyhow::{bail, Result};
6
use nom::error::Error as NomError;
7
use object::{Endian, Endianness, Object, ObjectSection, ReadCache, ReadRef, Section};
8
use std::convert::TryInto;
9
use std::error::Error;
10
use std::fmt;
11
use std::fs;
12
use std::io::BufReader;
13
use std::path::{Path, PathBuf};
14
use tracing::{debug, error, trace, warn};
15

16
/// Stores the instrumentation profile and information from the coverage mapping sections in the
17
/// object files in order to construct a coverage report. Inspired, from the LLVM implementation
18
/// with some differences/simplifications due to the fact this only hands instrumentation profiles
19
/// for coverage.
20
///
21
/// So what the LLVM one has that this one doesn't yet:
22
///
23
/// 1. DenseMap<size_t, DenseSet<size_t>> RecordProvenance
24
/// 2. std::vector<FunctionRecord> functions (this is probably taken straight from
25
/// InstrumentationProfile
26
/// 3. DenseMap<size_t, SmallVector<unsigned, 0>> FilenameHash2RecordIndices
27
/// 4. Vec<Pair<String, u64>> FuncHashMismatches
28
#[derive(Debug)]
29
pub struct CoverageMapping<'a> {
30
    profile: &'a InstrumentationProfile,
31
    pub mapping_info: Vec<CoverageMappingInfo>,
32
}
33

34
#[derive(Copy, Clone, Debug)]
35
pub enum LlvmSection {
36
    CoverageMap,
37
    ProfileNames,
38
    ProfileCounts,
39
    ProfileData,
40
    CoverageFunctions,
41
}
42

43
#[derive(Copy, Clone, Debug)]
44
pub enum SectionReadError {
45
    EmptySection(LlvmSection),
46
    MissingSection(LlvmSection),
47
    InvalidPathList,
48
}
49

50
impl fmt::Display for SectionReadError {
51
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
×
52
        match self {
×
53
            Self::EmptySection(s) => write!(f, "empty section: {:?}", s),
×
54
            Self::MissingSection(s) => write!(f, "missing section: {:?}", s),
×
55
            Self::InvalidPathList => write!(f, "unable to read path list"),
×
56
        }
57
    }
58
}
59

60
impl Error for SectionReadError {}
61

62
pub fn read_object_file(object: &Path, version: u64) -> Result<CoverageMappingInfo> {
1✔
63
    // I believe vnode sections added by llvm are unnecessary
64

65
    let binary_data = ReadCache::new(BufReader::new(fs::File::open(object)?));
1✔
66
    let object_file = object::File::parse(&binary_data)?;
2✔
67

68
    let prof_counts = object_file
2✔
69
        .section_by_name("__llvm_prf_cnts")
70
        .or(object_file.section_by_name(".lprfc"))
1✔
71
        .and_then(|x| parse_profile_counters(object_file.endianness(), &x).ok());
2✔
72

73
    debug!("Parsed prf_cnts: {:?}", prof_counts);
2✔
74

75
    let prof_data = object_file
2✔
76
        .section_by_name("__llvm_prf_data")
77
        .or(object_file.section_by_name(".lprfd"))
1✔
78
        .and_then(|x| parse_profile_data(object_file.endianness(), &x).ok());
2✔
79

80
    debug!("Parsed prf_data section: {:?}", prof_data);
2✔
81

82
    let cov_fun = object_file
3✔
83
        .section_by_name("__llvm_covfun")
84
        .or(object_file.section_by_name(".lcovfun"))
1✔
85
        .map(|x| parse_coverage_functions(object_file.endianness(), &x))
2✔
86
        .ok_or(SectionReadError::MissingSection(
1✔
87
            LlvmSection::CoverageFunctions,
1✔
88
        ))??;
89

90
    debug!("Parsed covfun section: {:?}", cov_fun);
2✔
91

92
    let cov_map = object_file
3✔
93
        .section_by_name("__llvm_covmap")
94
        .or(object_file.section_by_name(".lcovmap"))
1✔
95
        .map(|x| parse_coverage_mapping(object_file.endianness(), &x, version))
2✔
96
        .ok_or(SectionReadError::MissingSection(LlvmSection::CoverageMap))??;
1✔
97

98
    debug!("Parsed covmap section: {:?}", cov_map);
2✔
99

100
    Ok(CoverageMappingInfo {
1✔
101
        cov_map,
1✔
102
        cov_fun,
1✔
103
        prof_counts,
1✔
104
        prof_data,
1✔
105
    })
106
}
107

108
impl<'a> CoverageMapping<'a> {
109
    pub fn new(
1✔
110
        object_files: &[PathBuf],
111
        profile: &'a InstrumentationProfile,
112
        allow_parsing_failures: bool,
113
    ) -> Result<Self> {
114
        let mut mapping_info = vec![];
1✔
115
        let version = match profile.version() {
2✔
116
            Some(v) => v,
1✔
117
            None => bail!("Invalid profile instrumentation, no version number provided"),
×
118
        };
119
        for file in object_files {
2✔
120
            match read_object_file(file.as_path(), version) {
2✔
121
                Ok(info) => mapping_info.push(info),
1✔
122
                Err(e) => {
×
123
                    error!("{} couldn't be interpretted: {}", file.display(), e);
×
124
                    if !allow_parsing_failures {
×
125
                        return Err(e);
×
126
                    }
127
                }
128
            };
129
        }
130
        Ok(Self {
1✔
131
            profile,
×
132
            mapping_info,
1✔
133
        })
134
    }
135

136
    /// All counters of type `CounterKind::ProfileInstrumentation` can be used in function regions
137
    /// other than their own (particulary for functions which are only called from one location).
138
    /// This gathers them all to use as a base list of counters.
139
    pub(crate) fn get_simple_counters(&self, func: &FunctionRecordV3) -> FxHashMap<Counter, i64> {
1✔
140
        let mut result = FxHashMap::default();
1✔
141
        result.insert(Counter::default(), 0);
2✔
142
        let record = self.profile.find_record_by_hash(func.header.name_hash);
1✔
143
        if let Some(func_record) = record.as_ref() {
1✔
144
            result.reserve(func_record.record.counts.len());
2✔
145
            for (id, count) in func_record.record.counts.iter().enumerate() {
1✔
146
                result.insert(Counter::instrumentation(id as u64), *count as i64);
1✔
147
            }
148
        }
149
        result
1✔
150
    }
151

152
    pub fn generate_subreport<P>(&self, mut predicate: P) -> CoverageReport
1✔
153
    where
154
        P: FnMut(&[PathBuf]) -> bool,
155
    {
156
        let mut report = CoverageReport::default();
1✔
157
        //let base_region_ids = info.get_simple_counters(self.profile);
158
        for info in &self.mapping_info {
2✔
159
            for func in &info.cov_fun {
2✔
160
                let base_region_ids = self.get_simple_counters(func);
1✔
161
                let paths = info.get_files_from_id(func.header.filenames_ref);
1✔
162
                if paths.is_empty() || !predicate(&paths) {
3✔
163
                    continue;
×
164
                }
165

166
                let mut region_ids = base_region_ids.clone();
1✔
167

168
                for region in func.regions.iter().filter(|x| !x.count.is_expression()) {
4✔
169
                    let count = region_ids.get(&region.count).copied().unwrap_or_default();
2✔
170
                    let result = report
1✔
171
                        .files
×
172
                        .entry(paths[region.file_id].clone())
1✔
173
                        .or_default();
174
                    result.insert(region.loc.clone(), count as usize);
1✔
175
                }
176

177
                let mut pending_exprs = vec![];
1✔
178

179
                for (expr_index, expr) in func.expressions.iter().enumerate().rev() {
2✔
180
                    let lhs = region_ids.get(&expr.lhs);
2✔
181
                    let rhs = region_ids.get(&expr.rhs);
1✔
182
                    match (lhs, rhs) {
1✔
183
                        (Some(lhs), Some(rhs)) => {
1✔
184
                            let count: i64 = match expr.kind {
1✔
185
                                ExprKind::Subtract => {
×
186
                                    trace!("Subtracting counts: {} - {}", lhs, rhs);
1✔
187
                                    lhs - rhs
2✔
188
                                }
189
                                ExprKind::Add => {
×
190
                                    trace!("Adding counts: {} + {}", lhs, rhs);
2✔
191
                                    lhs + rhs
2✔
192
                                }
193
                            };
194

195
                            let counter = Counter {
196
                                kind: CounterType::Expression(expr.kind),
1✔
197
                                id: expr_index as _,
1✔
198
                            };
199

200
                            region_ids.insert(counter, count);
2✔
201
                            if let Some(expr_region) = func.regions.iter().find(|x| {
2✔
202
                                x.count.is_expression() && x.count.id == expr_index as u64
1✔
203
                            }) {
204
                                let result = report
1✔
205
                                    .files
×
206
                                    .entry(paths[expr_region.file_id].clone())
1✔
207
                                    .or_default();
208
                                result.insert(expr_region.loc.clone(), count as _);
1✔
209
                            }
210
                        }
211
                        _ => {
×
212
                            let lhs_none = lhs.is_none();
2✔
213
                            let rhs_none = rhs.is_none();
1✔
214
                            // These counters have been optimised out, so just add then in as 0
215
                            if lhs_none && expr.lhs.is_instrumentation() {
2✔
216
                                region_ids.insert(expr.lhs, 0);
×
217
                            }
218
                            if rhs_none && expr.rhs.is_instrumentation() {
2✔
219
                                region_ids.insert(expr.rhs, 0);
×
220
                            }
221
                            pending_exprs.push(Some((expr_index, expr)));
2✔
222
                            continue;
×
223
                        }
224
                    }
225
                }
226
                let mut index = 0;
1✔
227
                let mut cleared_expressions = 0;
1✔
228
                let mut tries_left = pending_exprs.len() + 1;
1✔
229
                while pending_exprs.len() != cleared_expressions {
3✔
230
                    assert!(tries_left > 0);
1✔
231
                    if index >= pending_exprs.len() {
1✔
232
                        index = 0;
×
233
                        tries_left -= 1;
×
234
                    }
235
                    let (expr_index, expr) = match pending_exprs[index].as_ref() {
3✔
236
                        Some((idx, expr)) => (idx, expr),
1✔
237
                        None => {
×
238
                            index += 1;
×
239
                            continue;
×
240
                        }
241
                    };
242
                    let lhs = region_ids.get(&expr.lhs);
2✔
243
                    let rhs = region_ids.get(&expr.rhs);
1✔
244
                    match (lhs, rhs) {
1✔
245
                        (Some(lhs), Some(rhs)) => {
1✔
246
                            let count = match expr.kind {
1✔
247
                                ExprKind::Subtract => lhs - rhs,
×
248
                                ExprKind::Add => lhs + rhs,
2✔
249
                            };
250

251
                            let counter = Counter {
252
                                kind: CounterType::Expression(expr.kind),
1✔
253
                                id: *expr_index as _,
1✔
254
                            };
255

256
                            region_ids.insert(counter, count);
2✔
257
                            if let Some(expr_region) = func.regions.iter().find(|x| {
2✔
258
                                x.count.is_expression() && x.count.id == *expr_index as u64
1✔
259
                            }) {
260
                                let result = report
1✔
261
                                    .files
×
262
                                    .entry(paths[expr_region.file_id].clone())
2✔
263
                                    .or_default();
264
                                result.insert(expr_region.loc.clone(), count as _);
1✔
265
                            }
266
                            pending_exprs[index] = None;
2✔
267
                            cleared_expressions += 1;
1✔
268
                        }
269
                        _ => {
×
270
                            index += 1;
×
271
                            continue;
×
272
                        }
273
                    }
274
                }
275
            }
276
        }
277
        report
1✔
278
    }
279

280
    pub fn generate_report(&self) -> CoverageReport {
1✔
281
        self.generate_subreport(|_| true)
3✔
282
    }
283
}
284

285
fn parse_coverage_mapping<'data, R: ReadRef<'data>>(
1✔
286
    endian: Endianness,
287
    section: &Section<'data, '_, R>,
288
    version: u64,
289
) -> Result<FxHashMap<u64, Vec<PathBuf>>, SectionReadError> {
290
    if let Ok(mut data) = section.data() {
2✔
291
        let mut result = FxHashMap::default();
1✔
292
        while !data.is_empty() {
3✔
293
            let data_len = data.len();
1✔
294
            // Read the number of affixed function records (now just 0 as not in this header)
295
            debug_assert_eq!(endian.read_i32_bytes(data[0..4].try_into().unwrap()), 0);
2✔
296
            let filename_data_len = endian.read_i32_bytes(data[4..8].try_into().unwrap());
1✔
297
            // Read the length of the affixed string that contains encoded coverage mapping data (now 0
298
            // as not in this header)
299
            debug_assert_eq!(endian.read_i32_bytes(data[8..12].try_into().unwrap()), 0);
1✔
300
            let _format_version = endian.read_i32_bytes(data[12..16].try_into().unwrap());
1✔
301

302
            let hash = md5::compute(&data[16..(filename_data_len as usize + 16)]);
1✔
303
            let hash = endian.read_u64_bytes(hash.0[..8].try_into().unwrap());
1✔
304

305
            //let bytes = &data[16..(16 + filename_data_len as usize)];
306
            let bytes = &data[16..];
1✔
307
            let (bytes, file_strings) = parse_path_list(bytes, version)
1✔
308
                .map_err(|_: nom::Err<NomError<_>>| SectionReadError::InvalidPathList)?;
×
309
            result.insert(hash, file_strings);
2✔
310
            let read_len = data_len - bytes.len();
1✔
311
            let padding = if !bytes.is_empty() && (read_len & 0x07) != 0 {
3✔
312
                8 - (read_len & 0x07)
1✔
313
            } else {
314
                0
×
315
            };
316
            if padding > bytes.len() {
1✔
UNCOV
317
                break;
×
318
            }
319
            data = &bytes[padding..];
2✔
320
        }
321
        Ok(result)
1✔
322
    } else {
323
        Err(SectionReadError::EmptySection(LlvmSection::CoverageMap))
×
324
    }
325
}
326

327
fn parse_coverage_functions<'data, R: ReadRef<'data>>(
1✔
328
    endian: Endianness,
329
    section: &Section<'data, '_, R>,
330
) -> Result<Vec<FunctionRecordV3>, SectionReadError> {
331
    debug!("Parsing coverage functions");
1✔
332
    if let Ok(original_data) = section.data() {
3✔
333
        let mut bytes = original_data;
1✔
334
        let mut res = vec![];
1✔
335
        let section_len = bytes.len();
1✔
336
        while !bytes.is_empty() {
2✔
337
            let name_hash = endian.read_u64_bytes(bytes[0..8].try_into().unwrap());
2✔
338
            let data_len = endian.read_u32_bytes(bytes[8..12].try_into().unwrap());
1✔
339
            let fn_hash = endian.read_u64_bytes(bytes[12..20].try_into().unwrap());
1✔
340
            let filenames_ref = endian.read_u64_bytes(bytes[20..28].try_into().unwrap());
1✔
341
            let header = FunctionRecordHeader {
342
                name_hash,
343
                data_len,
344
                fn_hash,
345
                filenames_ref,
346
            };
347
            let _start_len = bytes[28..].len();
1✔
348
            bytes = &bytes[28..];
1✔
349

350
            let (data, id_len) = parse_leb128::<NomError<_>>(bytes).unwrap();
1✔
351
            bytes = data;
1✔
352
            let mut filename_indices = vec![];
1✔
353
            for _ in 0..id_len {
3✔
354
                let (data, id) = parse_leb128::<NomError<_>>(bytes).unwrap(); // Issue
2✔
355
                filename_indices.push(id);
1✔
356
                bytes = data;
1✔
357
            }
358

359
            let (data, expr_len) = parse_leb128::<NomError<_>>(bytes).unwrap();
1✔
360
            let expr_len = expr_len as usize;
1✔
361
            bytes = data;
1✔
362
            let mut exprs = vec![Expression::default(); expr_len];
1✔
363
            for i in 0..expr_len {
3✔
364
                let (data, lhs) = parse_leb128::<NomError<_>>(bytes).unwrap();
2✔
365
                let (data, rhs) = parse_leb128::<NomError<_>>(data).unwrap();
1✔
366
                let lhs = parse_counter(lhs, &mut exprs);
1✔
367
                let rhs = parse_counter(rhs, &mut exprs);
1✔
368
                exprs[i].lhs = lhs;
1✔
369
                exprs[i].rhs = rhs;
1✔
370
                bytes = data;
1✔
371
            }
372

373
            let (data, regions) =
1✔
UNCOV
374
                parse_mapping_regions(bytes, &filename_indices, &mut exprs).unwrap();
×
375

376
            res.push(FunctionRecordV3 {
1✔
UNCOV
377
                header,
×
378
                regions,
1✔
379
                expressions: exprs,
1✔
380
            });
381

382
            // Todo set couners for expansion regions - counter of expansion region is the counter
383
            // of the first region from the expanded file. This requires multiple passes to
384
            // correctly propagate across all nested regions. N.B. I haven't seen any expansion
385
            // regions in use so may not be an issue!
386

387
            bytes = data;
1✔
388
            let function_len = section_len - bytes.len(); // this should match header
1✔
389

390
            let padding = if function_len < section_len && (function_len & 0x07) != 0 {
3✔
391
                8 - (function_len & 0x07)
2✔
392
            } else {
393
                0
1✔
394
            };
395

396
            if padding > bytes.len() {
1✔
UNCOV
397
                break;
×
398
            }
399
            // Now apply padding, and if hash is 0 move on as it's a dummy otherwise add to result
400
            // And decide what end type will be
401
            bytes = &bytes[padding..];
2✔
402
        }
403
        Ok(res)
1✔
404
    } else {
405
        error!("Can't read data for coverage function section");
×
406
        Err(SectionReadError::EmptySection(
×
407
            LlvmSection::CoverageFunctions,
×
408
        ))
409
    }
410
}
411

412
/// This code is ported from `RawCoverageMappingReader::readMappingRegionsSubArray`
413
fn parse_mapping_regions<'a>(
1✔
414
    mut bytes: &'a [u8],
415
    file_indices: &[u64],
416
    expressions: &mut Vec<Expression>,
417
) -> IResult<&'a [u8], Vec<CounterMappingRegion>> {
418
    let mut mapping = vec![];
1✔
419
    for i in file_indices {
2✔
420
        let (data, regions_len) = parse_leb128(bytes)?;
2✔
421
        bytes = data;
1✔
422
        let mut last_line = 0;
1✔
423
        for _ in 0..regions_len {
1✔
424
            let mut false_count = Counter::default();
1✔
425
            let mut kind = RegionKind::Code;
1✔
426
            let (data, raw_header) = parse_leb128(bytes)?;
1✔
427
            bytes = data;
1✔
428
            let mut expanded_file_id = 0;
1✔
429
            let mut counter = parse_counter(raw_header, expressions);
1✔
430
            if counter.is_zero() {
1✔
431
                if raw_header & Counter::ENCODING_EXPANSION_REGION_BIT > 0 {
×
432
                    kind = RegionKind::Expansion;
×
433
                    expanded_file_id = raw_header >> Counter::ENCODING_TAG_AND_EXP_REGION_BITS;
×
434
                    if expanded_file_id >= file_indices.len() as u64 {
×
435
                        todo!()
436
                    }
437
                } else {
438
                    let shifted_counter = raw_header >> Counter::ENCODING_TAG_AND_EXP_REGION_BITS;
×
439
                    match shifted_counter.try_into() {
×
440
                        Ok(RegionKind::Code) | Ok(RegionKind::Skipped) => {}
×
441
                        Ok(RegionKind::Branch) => {
×
442
                            kind = RegionKind::Branch;
×
443
                            let (data, c1) = parse_leb128(bytes)?;
×
444
                            let (data, c2) = parse_leb128(data)?;
×
445

446
                            counter = parse_counter(c1, expressions);
×
447
                            false_count = parse_counter(c2, expressions);
×
448
                            bytes = data;
×
449
                        }
450
                        e => panic!("Malformed: {:?}", e),
×
451
                    }
452
                }
453
            }
454

455
            let (data, delta_line) = parse_leb128(bytes)?;
2✔
456
            let (data, column_start) = parse_leb128(data)?;
1✔
457
            let (data, lines_len) = parse_leb128(data)?;
1✔
458
            let (data, column_end) = parse_leb128(data)?;
1✔
459
            bytes = data;
1✔
460

461
            let (column_start, column_end) = if column_start == 0 && column_end == 0 {
3✔
462
                (1usize, usize::MAX)
×
463
            } else {
464
                (column_start as usize, column_end as usize)
1✔
465
            };
466

467
            let line_start = last_line + delta_line as usize;
1✔
468
            let line_end = line_start + lines_len as usize;
2✔
469
            last_line = line_start;
1✔
470

471
            // Add region working-out-stuff
472
            mapping.push(CounterMappingRegion {
2✔
473
                kind,
1✔
474
                count: counter,
1✔
475
                false_count,
1✔
476
                file_id: *i as usize,
1✔
477
                expanded_file_id: expanded_file_id as _,
1✔
478
                loc: SourceLocation {
1✔
479
                    line_start,
×
480
                    line_end,
×
481
                    column_start,
×
482
                    column_end,
×
483
                },
484
            });
485
        }
486
    }
487
    Ok((bytes, mapping))
1✔
488
}
489

490
fn parse_profile_data<'data, R: ReadRef<'data>>(
1✔
491
    endian: Endianness,
492
    section: &Section<'data, '_, R>,
493
) -> Result<Vec<ProfileData>, SectionReadError> {
494
    if let Ok(data) = section.data() {
2✔
495
        let mut bytes = data;
1✔
496
        let mut res = vec![];
1✔
497
        while !bytes.is_empty() {
2✔
498
            // bytes.len() >= 24 {
499
            let name_md5 = endian.read_u64_bytes(bytes[..8].try_into().unwrap());
2✔
500
            let structural_hash = endian.read_u64_bytes(bytes[8..16].try_into().unwrap());
1✔
501

502
            let _counter_ptr = endian.read_u64_bytes(bytes[16..24].try_into().unwrap());
1✔
503
            let counters_location = 24 + 16;
1✔
504
            if bytes.len() <= counters_location {
2✔
505
                bytes = &bytes[counters_location..];
×
506
                let counters_len = endian.read_u32_bytes(bytes[..4].try_into().unwrap());
×
507
                // TODO Might need to get the counter offset and get the list of counters from this?
508
                // And potentially check against the maximum number of counters just to make sure that
509
                // it's not being exceeded?
510
                //
511
                // Also counters_len >= 1 so this should be checked to make sure it's not malformed
512

513
                bytes = &bytes[8..];
×
514

515
                res.push(ProfileData {
×
UNCOV
516
                    name_md5,
×
UNCOV
517
                    structural_hash,
×
UNCOV
518
                    counters_len,
×
519
                });
520
            } else {
521
                bytes = &[];
1✔
522
            }
523
        }
524
        if !bytes.is_empty() {
2✔
525
            warn!("{} bytes left in profile data", bytes.len());
×
526
        }
527
        Ok(res)
1✔
528
    } else {
529
        Err(SectionReadError::EmptySection(LlvmSection::ProfileData))
×
530
    }
531
}
532

533
fn parse_profile_counters<'data, R: ReadRef<'data>>(
1✔
534
    endian: Endianness,
535
    section: &Section<'data, '_, R>,
536
) -> Result<Vec<u64>, SectionReadError> {
537
    if let Ok(data) = section.data() {
2✔
538
        let mut result = vec![];
1✔
539
        for i in (0..data.len()).step_by(8) {
2✔
540
            if data.len() < (i + 8) {
2✔
UNCOV
541
                break;
×
542
            }
543
            result.push(endian.read_u64_bytes(data[i..(i + 8)].try_into().unwrap()));
1✔
544
        }
545
        Ok(result)
1✔
546
    } else {
547
        Err(SectionReadError::EmptySection(LlvmSection::ProfileCounts))
×
548
    }
549
}
550

551
/// The equivalent llvm function is `RawCoverageMappingReader::decodeCounter`. This makes it
552
/// stateless as I don't want to be maintaining an expression vector and clearing it and
553
/// repopulating for every function record.
554
fn parse_counter(input: u64, exprs: &mut Vec<Expression>) -> Counter {
1✔
555
    let ty = (Counter::ENCODING_TAG_MASK & input) as u8;
2✔
556
    let id = input >> 2; // For zero we don't actually care about this but we'll still do it
2✔
557
    let kind = match ty {
2✔
558
        0 => CounterType::Zero,
1✔
559
        1 => CounterType::ProfileInstrumentation,
1✔
560
        2 | 3 => {
561
            let expr_kind = if ty == 2 {
2✔
562
                ExprKind::Subtract
1✔
563
            } else {
564
                ExprKind::Add
1✔
565
            };
566
            let id = id as usize;
1✔
567
            if exprs.len() <= id {
1✔
568
                debug!(
×
569
                    "Not enough expressions resizing {}->{}",
570
                    exprs.len(),
×
571
                    id + 1
×
572
                );
573
                exprs.resize(id + 1, Expression::default());
×
574
            }
575
            exprs[id].set_kind(expr_kind);
1✔
576
            CounterType::Expression(expr_kind)
1✔
577
        }
578
        _ => unreachable!(),
579
    };
580
    Counter { kind, id }
581
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc