@@ -20,7 +20,7 @@ use std::cell::{Cell, RefCell, Ref};
20
20
use std:: ffi:: CStr ;
21
21
use std:: ffi:: CString ;
22
22
use std:: fs:: File ;
23
- use std:: mem:: { forget, zeroed } ;
23
+ use std:: mem:: { forget, MaybeUninit } ;
24
24
use std:: path:: Path ;
25
25
use std:: ptr;
26
26
use std:: rc:: Rc ;
@@ -433,17 +433,20 @@ impl Module {
433
433
return Err ( LLVMString :: create ( string) ) ;
434
434
}
435
435
436
- let mut execution_engine = unsafe { zeroed ( ) } ;
437
- let mut err_string = unsafe { zeroed ( ) } ;
436
+ let mut execution_engine = MaybeUninit :: uninit ( ) ;
437
+ let mut err_string = MaybeUninit :: uninit ( ) ;
438
438
let code = unsafe {
439
- LLVMCreateExecutionEngineForModule ( & mut execution_engine, self . module . get ( ) , & mut err_string) // Takes ownership of module
439
+ // Takes ownership of module
440
+ LLVMCreateExecutionEngineForModule ( execution_engine. as_mut_ptr ( ) , self . module . get ( ) , err_string. as_mut_ptr ( ) )
440
441
} ;
441
442
442
443
if code == 1 {
444
+ let err_string = unsafe { err_string. assume_init ( ) } ;
443
445
return Err ( LLVMString :: new ( err_string) ) ;
444
446
}
445
447
446
448
let context = self . non_global_context . clone ( ) ;
449
+ let execution_engine = unsafe { execution_engine. assume_init ( ) } ;
447
450
let execution_engine = ExecutionEngine :: new ( Rc :: new ( execution_engine) , context, false ) ;
448
451
449
452
* self . owned_by_ee . borrow_mut ( ) = Some ( execution_engine. clone ( ) ) ;
@@ -481,18 +484,21 @@ impl Module {
481
484
return Err ( LLVMString :: create ( string) ) ;
482
485
}
483
486
484
- let mut execution_engine = unsafe { zeroed ( ) } ;
485
- let mut err_string = unsafe { zeroed ( ) } ;
487
+ let mut execution_engine = MaybeUninit :: uninit ( ) ;
488
+ let mut err_string = MaybeUninit :: uninit ( ) ;
486
489
487
490
let code = unsafe {
488
- LLVMCreateInterpreterForModule ( & mut execution_engine, self . module . get ( ) , & mut err_string) // Takes ownership of module
491
+ // Takes ownership of module
492
+ LLVMCreateInterpreterForModule ( execution_engine. as_mut_ptr ( ) , self . module . get ( ) , err_string. as_mut_ptr ( ) )
489
493
} ;
490
494
491
495
if code == 1 {
496
+ let err_string = unsafe { err_string. assume_init ( ) } ;
492
497
return Err ( LLVMString :: new ( err_string) ) ;
493
498
}
494
499
495
500
let context = self . non_global_context . clone ( ) ;
501
+ let execution_engine = unsafe { execution_engine. assume_init ( ) } ;
496
502
let execution_engine = ExecutionEngine :: new ( Rc :: new ( execution_engine) , context, false ) ;
497
503
498
504
* self . owned_by_ee . borrow_mut ( ) = Some ( execution_engine. clone ( ) ) ;
@@ -531,18 +537,21 @@ impl Module {
531
537
return Err ( LLVMString :: create ( string) ) ;
532
538
}
533
539
534
- let mut execution_engine = unsafe { zeroed ( ) } ;
535
- let mut err_string = unsafe { zeroed ( ) } ;
540
+ let mut execution_engine = MaybeUninit :: uninit ( ) ;
541
+ let mut err_string = MaybeUninit :: uninit ( ) ;
536
542
537
543
let code = unsafe {
538
- LLVMCreateJITCompilerForModule ( & mut execution_engine, self . module . get ( ) , opt_level as u32 , & mut err_string) // Takes ownership of module
544
+ // Takes ownership of module
545
+ LLVMCreateJITCompilerForModule ( execution_engine. as_mut_ptr ( ) , self . module . get ( ) , opt_level as u32 , err_string. as_mut_ptr ( ) )
539
546
} ;
540
547
541
548
if code == 1 {
549
+ let err_string = unsafe { err_string. assume_init ( ) } ;
542
550
return Err ( LLVMString :: new ( err_string) ) ;
543
551
}
544
552
545
553
let context = self . non_global_context . clone ( ) ;
554
+ let execution_engine = unsafe { execution_engine. assume_init ( ) } ;
546
555
let execution_engine = ExecutionEngine :: new ( Rc :: new ( execution_engine) , context, true ) ;
547
556
548
557
* self . owned_by_ee . borrow_mut ( ) = Some ( execution_engine. clone ( ) ) ;
@@ -659,14 +668,15 @@ impl Module {
659
668
/// # Remarks
660
669
/// See also: http://llvm.org/doxygen/Analysis_2Analysis_8cpp_source.html
661
670
pub fn verify ( & self ) -> Result < ( ) , LLVMString > {
662
- let mut err_str = unsafe { zeroed ( ) } ;
671
+ let mut err_str = MaybeUninit :: uninit ( ) ;
663
672
664
673
let action = LLVMVerifierFailureAction :: LLVMReturnStatusAction ;
665
674
666
675
let code = unsafe {
667
- LLVMVerifyModule ( self . module . get ( ) , action, & mut err_str)
676
+ LLVMVerifyModule ( self . module . get ( ) , action, err_str. as_mut_ptr ( ) )
668
677
} ;
669
678
679
+ let err_str = unsafe { err_str. assume_init ( ) } ;
670
680
if code == 1 && !err_str. is_null ( ) {
671
681
return Err ( LLVMString :: new ( err_str) ) ;
672
682
}
@@ -767,12 +777,13 @@ impl Module {
767
777
pub fn print_to_file < P : AsRef < Path > > ( & self , path : P ) -> Result < ( ) , LLVMString > {
768
778
let path_str = path. as_ref ( ) . to_str ( ) . expect ( "Did not find a valid Unicode path string" ) ;
769
779
let path = CString :: new ( path_str) . expect ( "Could not convert path to CString" ) ;
770
- let mut err_string = unsafe { zeroed ( ) } ;
780
+ let mut err_string = MaybeUninit :: uninit ( ) ;
771
781
let return_code = unsafe {
772
- LLVMPrintModuleToFile ( self . module . get ( ) , path. as_ptr ( ) as * const i8 , & mut err_string)
782
+ LLVMPrintModuleToFile ( self . module . get ( ) , path. as_ptr ( ) as * const i8 , err_string. as_mut_ptr ( ) )
773
783
} ;
774
784
775
785
if return_code == 1 {
786
+ let err_string = unsafe { err_string. assume_init ( ) } ;
776
787
return Err ( LLVMString :: new ( err_string) ) ;
777
788
}
778
789
@@ -1063,21 +1074,24 @@ impl Module {
1063
1074
///
1064
1075
/// ```
1065
1076
pub fn parse_bitcode_from_buffer ( buffer : & MemoryBuffer ) -> Result < Self , LLVMString > {
1066
- let mut module = unsafe { zeroed ( ) } ;
1067
- let mut err_string = unsafe { zeroed ( ) } ;
1077
+ let mut module = MaybeUninit :: uninit ( ) ;
1078
+ let mut err_string = MaybeUninit :: uninit ( ) ;
1068
1079
1069
1080
// LLVM has a newer version of this function w/o the error result since 3.8 but this deprecated function
1070
1081
// hasen't yet been removed even in the unreleased LLVM 7. Seems fine to use instead of switching to their
1071
1082
// error diagnostics handler
1072
1083
#[ allow( deprecated) ]
1073
1084
let success = unsafe {
1074
- LLVMParseBitcode ( buffer. memory_buffer , & mut module, & mut err_string)
1085
+ LLVMParseBitcode ( buffer. memory_buffer , module. as_mut_ptr ( ) , err_string. as_mut_ptr ( ) )
1075
1086
} ;
1076
1087
1077
1088
if success != 0 {
1089
+ let err_string = unsafe { err_string. assume_init ( ) } ;
1078
1090
return Err ( LLVMString :: new ( err_string) ) ;
1079
1091
}
1080
1092
1093
+ let module = unsafe { module. assume_init ( ) } ;
1094
+
1081
1095
Ok ( Module :: new ( module, None ) )
1082
1096
}
1083
1097
@@ -1100,21 +1114,24 @@ impl Module {
1100
1114
///
1101
1115
/// ```
1102
1116
pub fn parse_bitcode_from_buffer_in_context ( buffer : & MemoryBuffer , context : & Context ) -> Result < Self , LLVMString > {
1103
- let mut module = unsafe { zeroed ( ) } ;
1104
- let mut err_string = unsafe { zeroed ( ) } ;
1117
+ let mut module = MaybeUninit :: uninit ( ) ;
1118
+ let mut err_string = MaybeUninit :: uninit ( ) ;
1105
1119
1106
1120
// LLVM has a newer version of this function w/o the error result since 3.8 but this deprecated function
1107
1121
// hasen't yet been removed even in the unreleased LLVM 7. Seems fine to use instead of switching to their
1108
1122
// error diagnostics handler
1109
1123
#[ allow( deprecated) ]
1110
1124
let success = unsafe {
1111
- LLVMParseBitcodeInContext ( * context. context , buffer. memory_buffer , & mut module, & mut err_string)
1125
+ LLVMParseBitcodeInContext ( * context. context , buffer. memory_buffer , module. as_mut_ptr ( ) , err_string. as_mut_ptr ( ) )
1112
1126
} ;
1113
1127
1114
1128
if success != 0 {
1129
+ let err_string = unsafe { err_string. assume_init ( ) } ;
1115
1130
return Err ( LLVMString :: new ( err_string) ) ;
1116
1131
}
1117
1132
1133
+ let module = unsafe { module. assume_init ( ) } ;
1134
+
1118
1135
Ok ( Module :: new ( module, Some ( & context) ) )
1119
1136
}
1120
1137
0 commit comments