]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_codegen_llvm/declare.rs
Rollup merge of #68156 - JohnTitor:fix-path-in-doc, r=Dylan-DPC
[rust.git] / src / librustc_codegen_llvm / declare.rs
index 8b6fedc87db968b35a5ac02ca0b60b91c4bf2687..bb06b52162186d129135c1ab8abc3dcb94897cd2 100644 (file)
 //! * Use define_* family of methods when you might be defining the Value.
 //! * When in doubt, define.
 
-use crate::llvm;
-use crate::llvm::AttributePlace::Function;
 use crate::abi::{FnAbi, FnAbiLlvmExt};
 use crate::attributes;
 use crate::context::CodegenCx;
+use crate::llvm;
+use crate::llvm::AttributePlace::Function;
 use crate::type_::Type;
 use crate::value::Value;
-use rustc::ty::Ty;
+use log::debug;
 use rustc::session::config::Sanitizer;
-use rustc_data_structures::small_c_str::SmallCStr;
+use rustc::ty::Ty;
 use rustc_codegen_ssa::traits::*;
+use rustc_data_structures::small_c_str::SmallCStr;
 
 /// Declare a function.
 ///
@@ -35,17 +36,14 @@ fn declare_raw_fn(
 ) -> &'ll Value {
     debug!("declare_raw_fn(name={:?}, ty={:?})", name, ty);
     let namebuf = SmallCStr::new(name);
-    let llfn = unsafe {
-        llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty)
-    };
+    let llfn = unsafe { llvm::LLVMRustGetOrInsertFunction(cx.llmod, namebuf.as_ptr(), ty) };
 
     llvm::SetFunctionCallConv(llfn, callconv);
     // Function addresses in Rust are never significant, allowing functions to
     // be merged.
     llvm::SetUnnamedAddr(llfn, true);
 
-    if cx.tcx.sess.opts.cg.no_redzone
-        .unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
+    if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.target.options.disable_redzone) {
         llvm::Attribute::NoRedZone.apply_llfn(Function, llfn);
     }
 
@@ -53,13 +51,13 @@ fn declare_raw_fn(
         match *sanitizer {
             Sanitizer::Address => {
                 llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
-            },
+            }
             Sanitizer::Memory => {
                 llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
-            },
+            }
             Sanitizer::Thread => {
                 llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
-            },
+            }
             _ => {}
         }
     }
@@ -70,31 +68,16 @@ fn declare_raw_fn(
 }
 
 impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
-
-    fn declare_global(
-        &self,
-        name: &str, ty: &'ll Type
-    ) -> &'ll Value {
+    fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
         debug!("declare_global(name={:?})", name);
-        let namebuf = SmallCStr::new(name);
-        unsafe {
-            llvm::LLVMRustGetOrInsertGlobal(self.llmod, namebuf.as_ptr(), ty)
-        }
+        unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
     }
 
-    fn declare_cfn(
-        &self,
-        name: &str,
-        fn_type: &'ll Type
-    ) -> &'ll Value {
+    fn declare_cfn(&self, name: &str, fn_type: &'ll Type) -> &'ll Value {
         declare_raw_fn(self, name, llvm::CCallConv, fn_type)
     }
 
-    fn declare_fn(
-        &self,
-        name: &str,
-        fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
-    ) -> &'ll Value {
+    fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
         debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
 
         let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
@@ -102,11 +85,7 @@ fn declare_fn(
         llfn
     }
 
-    fn define_global(
-        &self,
-        name: &str,
-        ty: &'ll Type
-    ) -> Option<&'ll Value> {
+    fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
         if self.get_defined_value(name).is_some() {
             None
         } else {
@@ -115,9 +94,7 @@ fn define_global(
     }
 
     fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
-        unsafe {
-            llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty)
-        }
+        unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
     }
 
     fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
@@ -127,15 +104,9 @@ fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
     }
 
     fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
-        self.get_declared_value(name).and_then(|val|{
-            let declaration = unsafe {
-                llvm::LLVMIsDeclaration(val) != 0
-            };
-            if !declaration {
-                Some(val)
-            } else {
-                None
-            }
+        self.get_declared_value(name).and_then(|val| {
+            let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
+            if !declaration { Some(val) } else { None }
         })
     }
 }