Skip to content

Commit

Permalink
Add test && change let some to if let some
Browse files Browse the repository at this point in the history
  • Loading branch information
Faouzi1406 committed May 5, 2023
1 parent 4d02a89 commit bff277c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl<'ctx> BasicBlock<'ctx> {
unsafe { Some(InstructionValue::new(value)) }
}

/// Obtains a instruction based on the name
/// Performs a linear lookup to obtain a instruction based on the name
///
/// # Example
/// ```rust
Expand Down Expand Up @@ -303,9 +303,9 @@ impl<'ctx> BasicBlock<'ctx> {
/// let some_number = block.get_instruction_with_name("some_number");
///
/// assert!(some_number.is_some());
/// assert_eq!( some_number.unwrap().get_name().unwrap().to_str(), Ok("some_number"))
/// assert_eq!(some_number.unwrap().get_name().unwrap().to_str(), Ok("some_number"))
/// ```
pub fn get_instruction_with_name(self, name: impl AsRef<str>) -> Option<InstructionValue<'ctx>> {
pub fn get_instruction_with_name(self, name: &str) -> Option<InstructionValue<'ctx>> {
let instruction = self.get_first_instruction()?;
instruction.get_instruction_with_name(name)
}
Expand Down
13 changes: 6 additions & 7 deletions src/values/instruction_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,14 @@ impl<'ctx> InstructionValue<'ctx> {
}
}

/// Get a instruction with it's name
///
/// Get a instruction with it's name
/// Compares against all instructions after self, and self.
pub fn get_instruction_with_name(&self, name:impl AsRef<str>) -> Option<InstructionValue<'ctx>> {
let Some(ins_name) = self.get_name() else {
pub fn get_instruction_with_name(&self, name: &str) -> Option<InstructionValue<'ctx>> {
if let Some(ins_name) = self.get_name() {
if ins_name.to_str() == Ok(name) {
return Some(*self);
}
return self.get_next_instruction()?.get_instruction_with_name(name);
};
if ins_name.to_str() == Ok(name.as_ref()) {
return Some(*self);
}
return self.get_next_instruction()?.get_instruction_with_name(name);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/all/test_instruction_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,32 @@ fn test_metadata_kinds() {
md_string.into(),
]);
}

#[test]
fn test_find_instruction_with_name() {
use inkwell::context::Context;
use inkwell::AddressSpace;

let context = Context::create();
let module = context.create_module("ret");
let builder = context.create_builder();

let void_type = context.void_type();
let i32_type = context.i32_type();
let i32_ptr_type = i32_type.ptr_type(AddressSpace::default());

let fn_type = void_type.fn_type(&[i32_ptr_type.into()], false);
let fn_value = module.add_function("ret", fn_type, None);
let entry = context.append_basic_block(fn_value, "entry");
builder.position_at_end(entry);

let var = builder.build_alloca(i32_type, "some_number");
builder.build_store(var, i32_type.const_int(1 as u64, false));
builder.build_return(None);

let block = fn_value.get_first_basic_block().unwrap();
let some_number = block.get_instruction_with_name("some_number");

assert!(some_number.is_some());
assert_eq!(some_number.unwrap().get_name().unwrap().to_str(), Ok("some_number"))
}

0 comments on commit bff277c

Please sign in to comment.