Skip to content

Commit ee1e70c

Browse files
committed
Cleaned up some code.
Finally marked build_global_string as unsafe as per rust-lang#32
1 parent 2b328d6 commit ee1e70c

File tree

4 files changed

+10
-14
lines changed

4 files changed

+10
-14
lines changed

src/builder.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use llvm_sys::{LLVMTypeKind, LLVMAtomicOrdering};
66
use {IntPredicate, FloatPredicate};
77
use basic_block::BasicBlock;
88
use values::{AggregateValue, AsValueRef, BasicValue, BasicValueEnum, PhiValue, FunctionValue, IntValue, PointerValue, VectorValue, InstructionValue, GlobalValue, IntMathValue, FloatMathValue, PointerMathValue, InstructionOpcode};
9-
use types::{AsTypeRef, BasicType, PointerType, IntMathType, FloatMathType, PointerMathType};
9+
use types::{AsTypeRef, BasicType, IntMathType, FloatMathType, PointerMathType};
1010

1111
use std::ffi::CString;
1212

@@ -101,9 +101,7 @@ impl Builder {
101101
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter()
102102
.map(|val| val.as_value_ref())
103103
.collect();
104-
let value = unsafe {
105-
LLVMBuildGEP(self.builder, ptr.as_value_ref(), index_values.as_mut_ptr(), index_values.len() as u32, c_string.as_ptr())
106-
};
104+
let value = LLVMBuildGEP(self.builder, ptr.as_value_ref(), index_values.as_mut_ptr(), index_values.len() as u32, c_string.as_ptr());
107105

108106
PointerValue::new(value)
109107
}
@@ -116,9 +114,7 @@ impl Builder {
116114
let mut index_values: Vec<LLVMValueRef> = ordered_indexes.iter()
117115
.map(|val| val.as_value_ref())
118116
.collect();
119-
let value = unsafe {
120-
LLVMBuildInBoundsGEP(self.builder, ptr.as_value_ref(), index_values.as_mut_ptr(), index_values.len() as u32, c_string.as_ptr())
121-
};
117+
let value = LLVMBuildInBoundsGEP(self.builder, ptr.as_value_ref(), index_values.as_mut_ptr(), index_values.len() as u32, c_string.as_ptr());
122118

123119
PointerValue::new(value)
124120
}
@@ -1040,16 +1036,17 @@ impl Builder {
10401036
InstructionValue::new(switch_value)
10411037
}
10421038

1043-
pub fn build_global_string(&self, value: &str, name: &str) -> GlobalValue {
1039+
// The unsafety of this function should be fixable with subtypes. See GH #32
1040+
pub unsafe fn build_global_string(&self, value: &str, name: &str) -> GlobalValue {
10441041
let c_string_value = CString::new(value).expect("Conversion to CString failed unexpectedly");
10451042
let c_string_name = CString::new(name).expect("Conversion to CString failed unexpectedly");
1046-
let value = unsafe {
1047-
LLVMBuildGlobalString(self.builder, c_string_value.as_ptr(), c_string_name.as_ptr())
1048-
};
1043+
let value = LLVMBuildGlobalString(self.builder, c_string_value.as_ptr(), c_string_name.as_ptr());
10491044

10501045
GlobalValue::new(value)
10511046
}
10521047

1048+
// REVIEW: Does this similar fn have the same issue build_global_string does? If so, mark as unsafe
1049+
// and fix with subtypes.
10531050
pub fn build_global_string_ptr(&self, value: &str, name: &str) -> GlobalValue {
10541051
let c_string_value = CString::new(value).expect("Conversion to CString failed unexpectedly");
10551052
let c_string_name = CString::new(name).expect("Conversion to CString failed unexpectedly");

src/data_layout.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::ffi::CStr;
22
use std::fmt;
3-
use std::ops::Deref;
43

54
use support::{LLVMString, LLVMStringOrRaw};
65

src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use llvm_sys::analysis::{LLVMVerifyModule, LLVMVerifierFailureAction};
2-
use llvm_sys::bit_reader::{LLVMParseBitcode, LLVMParseBitcodeInContext, LLVMGetBitcodeModuleInContext, LLVMGetBitcodeModule};
2+
use llvm_sys::bit_reader::{LLVMParseBitcode, LLVMParseBitcodeInContext};
33
use llvm_sys::bit_writer::{LLVMWriteBitcodeToFile, LLVMWriteBitcodeToMemoryBuffer};
44
use llvm_sys::core::{LLVMAddFunction, LLVMAddGlobal, LLVMDumpModule, LLVMGetNamedFunction, LLVMGetTypeByName, LLVMSetDataLayout, LLVMSetTarget, LLVMCloneModule, LLVMDisposeModule, LLVMGetTarget, LLVMModuleCreateWithName, LLVMGetModuleContext, LLVMGetFirstFunction, LLVMGetLastFunction, LLVMSetLinkage, LLVMAddGlobalInAddressSpace, LLVMPrintModuleToString, LLVMGetNamedMetadataNumOperands, LLVMAddNamedMetadataOperand, LLVMGetNamedMetadataOperands, LLVMGetFirstGlobal, LLVMGetLastGlobal, LLVMGetNamedGlobal, LLVMPrintModuleToFile, LLVMSetModuleInlineAsm};
55
use llvm_sys::execution_engine::{LLVMCreateInterpreterForModule, LLVMCreateJITCompilerForModule, LLVMCreateExecutionEngineForModule};

tests/test_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ fn test_no_builder_double_free2() {
442442

443443
#[cfg(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8"))]
444444
assert_eq!(*module.print_to_string(), *CString::new("; ModuleID = \'my_mod\'\n\ndefine void @my_fn() {\nentry:\n unreachable\n}\n").unwrap());
445-
#[cfg(any(feature = "llvm3-9", feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0"))]
445+
#[cfg(not(any(feature = "llvm3-6", feature = "llvm3-7", feature = "llvm3-8")))]
446446
assert_eq!(*module.print_to_string(), *CString::new("; ModuleID = \'my_mod\'\nsource_filename = \"my_mod\"\n\ndefine void @my_fn() {\nentry:\n unreachable\n}\n").unwrap());
447447

448448
// 2nd Context drops fine

0 commit comments

Comments
 (0)