Rust coverage before splitting instrument_coverage.rs

This commit is contained in:
Rich Kadel 2020-10-05 16:36:10 -07:00
parent 9d78d1d027
commit c7747cc772
214 changed files with 9845 additions and 1267 deletions

View file

@ -7,6 +7,10 @@ use std::cmp::Ord;
use std::fmt::{self, Debug, Formatter};
rustc_index::newtype_index! {
/// An ExpressionOperandId value is assigned directly from either a
/// CounterValueReference.as_u32() (which ascend from 1) or an ExpressionOperandId.as_u32()
/// (which _*descend*_ from u32::MAX). Id value `0` (zero) represents a virtual counter with a
/// constant value of `0`.
pub struct ExpressionOperandId {
derive [HashStable]
DEBUG_FORMAT = "ExpressionOperandId({})",
@ -42,6 +46,20 @@ impl CounterValueReference {
}
rustc_index::newtype_index! {
/// InjectedExpressionId.as_u32() converts to ExpressionOperandId.as_u32()
///
/// Values descend from u32::MAX.
pub struct InjectedExpressionId {
derive [HashStable]
DEBUG_FORMAT = "InjectedExpressionId({})",
MAX = 0xFFFF_FFFF,
}
}
rustc_index::newtype_index! {
/// InjectedExpressionIndex.as_u32() translates to u32::MAX - ExpressionOperandId.as_u32()
///
/// Values ascend from 0.
pub struct InjectedExpressionIndex {
derive [HashStable]
DEBUG_FORMAT = "InjectedExpressionIndex({})",
@ -50,6 +68,9 @@ rustc_index::newtype_index! {
}
rustc_index::newtype_index! {
/// MappedExpressionIndex values ascend from zero, and are recalculated indexes based on their
/// array position in the LLVM coverage map "Expressions" array, which is assembled during the
/// "mapgen" process. They cannot be computed algorithmically, from the other `newtype_index`s.
pub struct MappedExpressionIndex {
derive [HashStable]
DEBUG_FORMAT = "MappedExpressionIndex({})",
@ -64,21 +85,35 @@ impl From<CounterValueReference> for ExpressionOperandId {
}
}
impl From<InjectedExpressionIndex> for ExpressionOperandId {
impl From<&mut CounterValueReference> for ExpressionOperandId {
#[inline]
fn from(v: InjectedExpressionIndex) -> ExpressionOperandId {
fn from(v: &mut CounterValueReference) -> ExpressionOperandId {
ExpressionOperandId::from(v.as_u32())
}
}
#[derive(Clone, Debug, PartialEq, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
impl From<InjectedExpressionId> for ExpressionOperandId {
#[inline]
fn from(v: InjectedExpressionId) -> ExpressionOperandId {
ExpressionOperandId::from(v.as_u32())
}
}
impl From<&mut InjectedExpressionId> for ExpressionOperandId {
#[inline]
fn from(v: &mut InjectedExpressionId) -> ExpressionOperandId {
ExpressionOperandId::from(v.as_u32())
}
}
#[derive(Clone, PartialEq, TyEncodable, TyDecodable, HashStable, TypeFoldable)]
pub enum CoverageKind {
Counter {
function_source_hash: u64,
id: CounterValueReference,
},
Expression {
id: InjectedExpressionIndex,
id: InjectedExpressionId,
lhs: ExpressionOperandId,
op: Op,
rhs: ExpressionOperandId,
@ -88,12 +123,47 @@ pub enum CoverageKind {
impl CoverageKind {
pub fn as_operand_id(&self) -> ExpressionOperandId {
use CoverageKind::*;
match *self {
CoverageKind::Counter { id, .. } => ExpressionOperandId::from(id),
CoverageKind::Expression { id, .. } => ExpressionOperandId::from(id),
CoverageKind::Unreachable => {
bug!("Unreachable coverage cannot be part of an expression")
}
Counter { id, .. } => ExpressionOperandId::from(id),
Expression { id, .. } => ExpressionOperandId::from(id),
Unreachable => bug!("Unreachable coverage cannot be part of an expression"),
}
}
pub fn is_counter(&self) -> bool {
match self {
Self::Counter { .. } => true,
_ => false,
}
}
pub fn is_expression(&self) -> bool {
match self {
Self::Expression { .. } => true,
_ => false,
}
}
pub fn is_unreachable(&self) -> bool {
*self == Self::Unreachable
}
}
impl Debug for CoverageKind {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
use CoverageKind::*;
match self {
Counter { id, .. } => write!(fmt, "Counter({:?})", id.index()),
Expression { id, lhs, op, rhs } => write!(
fmt,
"Expression({:?}) = {} {} {}",
id.index(),
lhs.index(),
if *op == Op::Add { "+" } else { "-" },
rhs.index(),
),
Unreachable => write!(fmt, "Unreachable"),
}
}
}