Add tests to allocation methods and fix is_null()
This commit is contained in:
parent
9cb6463af7
commit
0a0e7e6c0d
3 changed files with 114 additions and 7 deletions
|
@ -21,7 +21,7 @@ pub struct Body {
|
|||
pub(super) arg_count: usize,
|
||||
|
||||
/// Debug information pertaining to user variables, including captures.
|
||||
pub(super) var_debug_info: Vec<VarDebugInfo>,
|
||||
pub var_debug_info: Vec<VarDebugInfo>,
|
||||
}
|
||||
|
||||
pub type BasicBlockIdx = usize;
|
||||
|
@ -616,6 +616,24 @@ pub struct VarDebugInfo {
|
|||
pub argument_index: Option<u16>,
|
||||
}
|
||||
|
||||
impl VarDebugInfo {
|
||||
/// Return a local variable if this info is related to one.
|
||||
pub fn local(&self) -> Option<Local> {
|
||||
match &self.value {
|
||||
VarDebugInfoContents::Place(place) if place.projection.is_empty() => Some(place.local),
|
||||
VarDebugInfoContents::Place(_) | VarDebugInfoContents::Const(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a constant if this info is related to one.
|
||||
pub fn constant(&self) -> Option<&ConstOperand> {
|
||||
match &self.value {
|
||||
VarDebugInfoContents::Place(_) => None,
|
||||
VarDebugInfoContents::Const(const_op) => Some(const_op),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type SourceScope = u32;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
|
|
|
@ -901,6 +901,7 @@ impl Allocation {
|
|||
read_target_uint(&raw)
|
||||
}
|
||||
|
||||
/// Read this allocation and try to convert it to an unassigned integer.
|
||||
pub fn read_uint(&self) -> Result<u128, Error> {
|
||||
if self.bytes.len() > 16 {
|
||||
return Err(error!("Allocation is bigger than largest integer"));
|
||||
|
@ -909,6 +910,7 @@ impl Allocation {
|
|||
read_target_uint(&raw)
|
||||
}
|
||||
|
||||
/// Read this allocation and try to convert it to a signed integer.
|
||||
pub fn read_int(&self) -> Result<i128, Error> {
|
||||
if self.bytes.len() > 16 {
|
||||
return Err(error!("Allocation is bigger than largest integer"));
|
||||
|
@ -917,6 +919,7 @@ impl Allocation {
|
|||
read_target_int(&raw)
|
||||
}
|
||||
|
||||
/// Read this allocation and try to convert it to a boolean.
|
||||
pub fn read_bool(&self) -> Result<bool, Error> {
|
||||
match self.read_int()? {
|
||||
0 => Ok(false),
|
||||
|
@ -925,13 +928,14 @@ impl Allocation {
|
|||
}
|
||||
}
|
||||
|
||||
/// Read this allocation as a pointer and return whether it represents a `null` pointer.
|
||||
pub fn is_null(&self) -> Result<bool, Error> {
|
||||
let len = self.bytes.len();
|
||||
let ptr_len = MachineInfo::target_pointer_width().bytes();
|
||||
if len != ptr_len {
|
||||
return Err(error!("Expected width of pointer (`{ptr_len}`), but found: `{len}`"));
|
||||
}
|
||||
Ok(self.read_uint()? == 0)
|
||||
Ok(self.read_uint()? == 0 && self.provenance.ptrs.is_empty())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue