forked from TheDan64/inkwell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_context.rs
85 lines (69 loc) · 2.49 KB
/
test_context.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
extern crate inkwell;
use self::inkwell::AddressSpace;
use self::inkwell::context::Context;
use self::inkwell::types::{IntType, StructType};
#[test]
fn test_no_context_double_free() {
let context = Context::create();
let int = context.i8_type();
{
int.get_context();
}
}
// FIXME: This isn't actually safe and stopped working as
// of a recent rust version (late 2018)
// #[test]
// fn test_no_context_double_free2() {
// let context = Context::create();
// let int = context.i8_type();
// let context2 = int.get_context();
// fn move_(_: Context) {}
// move_(context);
// context2.i8_type().const_int(0, false);
// }
#[test]
fn test_no_context_double_free3() {
Context::get_global();
Context::get_global();
}
#[test]
fn test_get_context_from_contextless_value() {
let int = IntType::i8_type();
let context = Context::create();
let global_context = Context::get_global();
assert_eq!(int.get_context(), global_context);
assert_ne!(*int.get_context(), context);
assert_ne!(*global_context, context);
}
#[test]
fn test_basic_block_context() {
let context = Context::create();
let module = context.create_module("my_mod");
let void_type = context.void_type();
let fn_type = void_type.fn_type(&[], false);
let fn_value = module.add_function("my_fn", fn_type, None);
let basic_block = fn_value.append_basic_block("entry");
assert_eq!(basic_block.get_context(), Context::get_global());
let basic_block2 = context.append_basic_block(&fn_value, "entry2");
assert_eq!(*basic_block2.get_context(), context);
}
#[test]
fn test_values_get_context() {
let context = Context::create();
let void_type = context.void_type();
let i8_type = context.i8_type();
let f32_type = context.f32_type();
let f32_vec_type = f32_type.vec_type(3);
let f32_ptr_type = f32_type.ptr_type(AddressSpace::Generic);
let f32_array_type = f32_type.array_type(2);
let fn_type = f32_type.fn_type(&[], false);
let struct_type = StructType::struct_type(&[i8_type.into(), f32_type.into()], false);
assert_eq!(*f32_type.get_context(), context);
assert_eq!(*void_type.get_context(), context);
assert_eq!(*f32_vec_type.get_context(), context);
assert_eq!(*f32_ptr_type.get_context(), context);
assert_eq!(*f32_array_type.get_context(), context);
assert_eq!(*fn_type.get_context(), context);
assert_eq!(*i8_type.get_context(), context);
assert_eq!(struct_type.get_context(), Context::get_global());
}