about summary refs log tree commit diff
diff options
context:
space:
mode:
authorIrina Popa <irinagpopa@gmail.com>2018-07-13 13:06:06 +0300
committerIrina Popa <irinagpopa@gmail.com>2018-07-30 20:10:32 +0300
commit41d7d8e35e54576d55dc6ed3649a6a6e7d6b749e (patch)
treedf5d5d7bc16b6c9f7ff0cb0f7456e6e55e784adc
parent0e3a70526952c285a3187b855e2cf3398afe6b38 (diff)
downloadrust-41d7d8e35e54576d55dc6ed3649a6a6e7d6b749e.tar.gz
rust-41d7d8e35e54576d55dc6ed3649a6a6e7d6b749e.zip
rustc_codegen_llvm: use safe references for Archive.
-rw-r--r--src/librustc_codegen_llvm/llvm/archive_ro.rs21
-rw-r--r--src/librustc_codegen_llvm/llvm/ffi.rs7
2 files changed, 11 insertions, 17 deletions
diff --git a/src/librustc_codegen_llvm/llvm/archive_ro.rs b/src/librustc_codegen_llvm/llvm/archive_ro.rs
index e25f6691961..98a08f8501a 100644
--- a/src/librustc_codegen_llvm/llvm/archive_ro.rs
+++ b/src/librustc_codegen_llvm/llvm/archive_ro.rs
@@ -10,8 +10,6 @@
 
 //! A wrapper around LLVM's archive (.a) code
 
-use super::ArchiveRef;
-
 use std::ffi::CString;
 use std::marker;
 use std::path::Path;
@@ -19,7 +17,7 @@ use std::slice;
 use std::str;
 
 pub struct ArchiveRO {
-    ptr: ArchiveRef,
+    raw: &'static mut super::Archive,
 }
 
 unsafe impl Send for ArchiveRO {}
@@ -44,12 +42,9 @@ impl ArchiveRO {
     pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
         return unsafe {
             let s = path2cstr(dst);
-            let ar = super::LLVMRustOpenArchive(s.as_ptr());
-            if ar.is_null() {
-                Err(super::last_error().unwrap_or("failed to open archive".to_string()))
-            } else {
-                Ok(ArchiveRO { ptr: ar })
-            }
+            let ar = super::LLVMRustOpenArchive(s.as_ptr())
+                .ok_or_else(|| super::last_error().unwrap_or("failed to open archive".to_string()))?;
+            Ok(ArchiveRO { raw: ar })
         };
 
         #[cfg(unix)]
@@ -65,14 +60,14 @@ impl ArchiveRO {
         }
     }
 
-    pub fn raw(&self) -> ArchiveRef {
-        self.ptr
+    pub fn raw(&self) -> &super::Archive {
+        self.raw
     }
 
     pub fn iter(&self) -> Iter {
         unsafe {
             Iter {
-                ptr: super::LLVMRustArchiveIteratorNew(self.ptr),
+                ptr: super::LLVMRustArchiveIteratorNew(self.raw),
                 _data: marker::PhantomData,
             }
         }
@@ -82,7 +77,7 @@ impl ArchiveRO {
 impl Drop for ArchiveRO {
     fn drop(&mut self) {
         unsafe {
-            super::LLVMRustDestroyArchive(self.ptr);
+            super::LLVMRustDestroyArchive(&mut *(self.raw as *mut _));
         }
     }
 }
diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs
index 566bd3c2c18..f6d206cf5ad 100644
--- a/src/librustc_codegen_llvm/llvm/ffi.rs
+++ b/src/librustc_codegen_llvm/llvm/ffi.rs
@@ -399,7 +399,6 @@ pub type SectionIteratorRef = *mut SectionIterator;
 extern { pub type Pass; }
 extern { pub type TargetMachine; }
 extern { pub type Archive; }
-pub type ArchiveRef = *mut Archive;
 extern { pub type ArchiveIterator; }
 pub type ArchiveIteratorRef = *mut ArchiveIterator;
 extern { pub type ArchiveChild; }
@@ -1471,14 +1470,14 @@ extern "C" {
     pub fn LLVMRustRunRestrictionPass(M: &Module, syms: *const *const c_char, len: size_t);
     pub fn LLVMRustMarkAllFunctionsNounwind(M: &Module);
 
-    pub fn LLVMRustOpenArchive(path: *const c_char) -> ArchiveRef;
-    pub fn LLVMRustArchiveIteratorNew(AR: ArchiveRef) -> ArchiveIteratorRef;
+    pub fn LLVMRustOpenArchive(path: *const c_char) -> Option<&'static mut Archive>;
+    pub fn LLVMRustArchiveIteratorNew(AR: &Archive) -> ArchiveIteratorRef;
     pub fn LLVMRustArchiveIteratorNext(AIR: ArchiveIteratorRef) -> ArchiveChildRef;
     pub fn LLVMRustArchiveChildName(ACR: ArchiveChildRef, size: *mut size_t) -> *const c_char;
     pub fn LLVMRustArchiveChildData(ACR: ArchiveChildRef, size: *mut size_t) -> *const c_char;
     pub fn LLVMRustArchiveChildFree(ACR: ArchiveChildRef);
     pub fn LLVMRustArchiveIteratorFree(AIR: ArchiveIteratorRef);
-    pub fn LLVMRustDestroyArchive(AR: ArchiveRef);
+    pub fn LLVMRustDestroyArchive(AR: &'static mut Archive);
 
     pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: *mut *const c_char) -> size_t;