llvm_cov_json/types/
branch.rs

1use serde::{Deserialize, Deserializer, de::Error};
2
3use crate::RegionKind;
4
5/// A single branch and associated metrics. According to the [LLVM documentation](https://llvm.org/docs/CoverageMappingFormat.html#advanced-concepts),
6/// a branch "may comprise larger boolean expressions using boolean logical operators", thus a the term branch refers to a branch found on source code
7/// level, not on binary level.
8/// See [CoverageExporterJson.cpp](https://github.com/llvm/llvm-project/blob/309d55140c46384b6de7a7573206cbeba3f7077f/llvm/tools/llvm-cov/CoverageExporterJson.cpp#L93) for details
9/// regarding the format.
10#[derive(Debug, PartialEq)]
11pub struct Branch {
12    /// Source line this branch condition starts.
13    pub line_start: u64,
14    ///  Column of `line_start` this branch starts.
15    pub column_start: u64,
16    /// Source line where the branch ends.
17    pub line_end: u64,
18    /// Column of `line_end` where the branch ends.
19    pub column_end: u64,
20    /// Number of times the true branch was taken.
21    pub execution_count: u64,
22    /// Number of times the false branch was taken.
23    pub false_execution_count: u64,
24    /// The file id this branch orginatess from. If this is part of an expansion,
25    /// this is, e.g., the header file.
26    pub file_id: u64,
27    /// The file id where this branch has been expanded to.
28    /// I.e., if branches are introduced via a macro, this is the file that
29    /// used the macro.
30    pub expanded_file_id: u64,
31    /// The kind of this region.
32    pub region_kind: RegionKind,
33}
34
35#[derive(Deserialize)]
36struct BranchTuple(u64, u64, u64, u64, u64, u64, u64, u64, u64);
37
38impl<'de> Deserialize<'de> for Branch {
39    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
40    where
41        D: Deserializer<'de> {
42        let tuple = BranchTuple::deserialize(deserializer)?;
43        let region_kind = match RegionKind::try_from(tuple.8) {
44            Ok(kind) => kind,
45            Err(err) => return Err(Error::custom(err)),
46        };
47
48        Ok(Branch {
49            line_start: tuple.0,
50            column_start: tuple.1,
51            line_end: tuple.2,
52            column_end: tuple.3,
53            execution_count: tuple.4,
54            false_execution_count: tuple.5,
55            file_id: tuple.6,
56            expanded_file_id: tuple.7,
57            region_kind,
58        })
59    }
60}