From 44ae6f190993f486d4822dc3da870f564f12b190 Mon Sep 17 00:00:00 2001 From: Irina Popa Date: Fri, 13 Jul 2018 13:59:41 +0300 Subject: [PATCH] rustc_codegen_llvm: use safe references for Twine, DiagnosticInfo, SMDiagnostic. --- src/librustc_codegen_llvm/back/write.rs | 8 +++---- src/librustc_codegen_llvm/llvm/diagnostic.rs | 18 +++++++------- src/librustc_codegen_llvm/llvm/ffi.rs | 25 +++++++++----------- src/librustc_codegen_llvm/llvm/mod.rs | 2 +- 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index ebeb0d4bcb4..7df050e5e9e 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -25,9 +25,7 @@ use rustc::session::config::{self, OutputFilenames, OutputType, Passes, SomePass use rustc::session::Session; use rustc::util::nodemap::FxHashMap; use time_graph::{self, TimeGraph, Timeline}; -use llvm; -use llvm::{PassManagerRef, DiagnosticInfoRef}; -use llvm::SMDiagnosticRef; +use llvm::{self, DiagnosticInfo, PassManagerRef, SMDiagnostic}; use {CodegenResults, ModuleSource, ModuleCodegen, CompiledModule, ModuleKind}; use CrateInfo; use rustc::hir::def_id::{CrateNum, LOCAL_CRATE}; @@ -431,7 +429,7 @@ unsafe extern "C" fn report_inline_asm<'a, 'b>(cgcx: &'a CodegenContext, cgcx.diag_emitter.inline_asm_error(cookie as u32, msg.to_string()); } -unsafe extern "C" fn inline_asm_handler(diag: SMDiagnosticRef, +unsafe extern "C" fn inline_asm_handler(diag: &SMDiagnostic, user: *const c_void, cookie: c_uint) { if user.is_null() { @@ -445,7 +443,7 @@ unsafe extern "C" fn inline_asm_handler(diag: SMDiagnosticRef, report_inline_asm(cgcx, &msg, cookie); } -unsafe extern "C" fn diagnostic_handler(info: DiagnosticInfoRef, user: *mut c_void) { +unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void) { if user.is_null() { return } diff --git a/src/librustc_codegen_llvm/llvm/diagnostic.rs b/src/librustc_codegen_llvm/llvm/diagnostic.rs index e4c278442d9..2c285132226 100644 --- a/src/librustc_codegen_llvm/llvm/diagnostic.rs +++ b/src/librustc_codegen_llvm/llvm/diagnostic.rs @@ -16,7 +16,7 @@ pub use self::Diagnostic::*; use libc::c_uint; use value::Value; -use super::{DiagnosticInfoRef, TwineRef}; +use super::{DiagnosticInfo, Twine}; #[derive(Copy, Clone)] pub enum OptimizationDiagnosticKind { @@ -55,7 +55,7 @@ pub struct OptimizationDiagnostic<'ll> { impl OptimizationDiagnostic<'ll> { unsafe fn unpack( kind: OptimizationDiagnosticKind, - di: DiagnosticInfoRef, + di: &'ll DiagnosticInfo, ) -> Self { let mut function = None; let mut line = 0; @@ -97,14 +97,14 @@ impl OptimizationDiagnostic<'ll> { #[derive(Copy, Clone)] pub struct InlineAsmDiagnostic<'ll> { pub cookie: c_uint, - pub message: TwineRef, + pub message: &'ll Twine, pub instruction: &'ll Value, } impl InlineAsmDiagnostic<'ll> { - unsafe fn unpack(di: DiagnosticInfoRef) -> Self { + unsafe fn unpack(di: &'ll DiagnosticInfo) -> Self { let mut cookie = 0; - let mut message = 0 as *mut _; + let mut message = None; let mut instruction = None; super::LLVMRustUnpackInlineAsmDiagnostic( @@ -116,7 +116,7 @@ impl InlineAsmDiagnostic<'ll> { InlineAsmDiagnostic { cookie, - message, + message: message.unwrap(), instruction: instruction.unwrap(), } } @@ -125,14 +125,14 @@ impl InlineAsmDiagnostic<'ll> { pub enum Diagnostic<'ll> { Optimization(OptimizationDiagnostic<'ll>), InlineAsm(InlineAsmDiagnostic<'ll>), - PGO(DiagnosticInfoRef), + PGO(&'ll DiagnosticInfo), /// LLVM has other types that we do not wrap here. - UnknownDiagnostic(DiagnosticInfoRef), + UnknownDiagnostic(&'ll DiagnosticInfo), } impl Diagnostic<'ll> { - pub unsafe fn unpack(di: DiagnosticInfoRef) -> Self { + pub unsafe fn unpack(di: &'ll DiagnosticInfo) -> Self { use super::DiagnosticKind as Dk; let kind = super::LLVMRustGetDiagInfoKind(di); diff --git a/src/librustc_codegen_llvm/llvm/ffi.rs b/src/librustc_codegen_llvm/llvm/ffi.rs index f6d206cf5ad..a907e9799cd 100644 --- a/src/librustc_codegen_llvm/llvm/ffi.rs +++ b/src/librustc_codegen_llvm/llvm/ffi.rs @@ -404,11 +404,8 @@ pub type ArchiveIteratorRef = *mut ArchiveIterator; extern { pub type ArchiveChild; } pub type ArchiveChildRef = *mut ArchiveChild; extern { pub type Twine; } -pub type TwineRef = *mut Twine; extern { pub type DiagnosticInfo; } -pub type DiagnosticInfoRef = *mut DiagnosticInfo; extern { pub type SMDiagnostic; } -pub type SMDiagnosticRef = *mut SMDiagnostic; extern { pub type RustArchiveMember; } pub type RustArchiveMemberRef = *mut RustArchiveMember; extern { pub type OperandBundleDef; } @@ -416,8 +413,8 @@ pub type OperandBundleDefRef = *mut OperandBundleDef; extern { pub type Linker; } pub type LinkerRef = *mut Linker; -pub type DiagnosticHandler = unsafe extern "C" fn(DiagnosticInfoRef, *mut c_void); -pub type InlineAsmDiagHandler = unsafe extern "C" fn(SMDiagnosticRef, *const c_void, c_uint); +pub type DiagnosticHandler = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void); +pub type InlineAsmDiagHandler = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint); pub mod debuginfo { @@ -1481,32 +1478,32 @@ extern "C" { pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: *mut *const c_char) -> size_t; - pub fn LLVMRustWriteTwineToString(T: TwineRef, s: RustStringRef); + pub fn LLVMRustWriteTwineToString(T: &Twine, s: RustStringRef); pub fn LLVMContextSetDiagnosticHandler(C: &Context, Handler: DiagnosticHandler, DiagnosticContext: *mut c_void); - pub fn LLVMRustUnpackOptimizationDiagnostic(DI: DiagnosticInfoRef, + pub fn LLVMRustUnpackOptimizationDiagnostic(DI: &'a DiagnosticInfo, pass_name_out: RustStringRef, - function_out: *mut Option<&Value>, + function_out: *mut Option<&'a Value>, loc_line_out: *mut c_uint, loc_column_out: *mut c_uint, loc_filename_out: RustStringRef, message_out: RustStringRef); - pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: DiagnosticInfoRef, + pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: &'a DiagnosticInfo, cookie_out: *mut c_uint, - message_out: *mut TwineRef, - instruction_out: *mut Option<&Value>); + message_out: *mut Option<&'a Twine>, + instruction_out: *mut Option<&'a Value>); - pub fn LLVMRustWriteDiagnosticInfoToString(DI: DiagnosticInfoRef, s: RustStringRef); - pub fn LLVMRustGetDiagInfoKind(DI: DiagnosticInfoRef) -> DiagnosticKind; + pub fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: RustStringRef); + pub fn LLVMRustGetDiagInfoKind(DI: &DiagnosticInfo) -> DiagnosticKind; pub fn LLVMRustSetInlineAsmDiagnosticHandler(C: &Context, H: InlineAsmDiagHandler, CX: *mut c_void); - pub fn LLVMRustWriteSMDiagnosticToString(d: SMDiagnosticRef, s: RustStringRef); + pub fn LLVMRustWriteSMDiagnosticToString(d: &SMDiagnostic, s: RustStringRef); pub fn LLVMRustWriteArchive(Dst: *const c_char, NumMembers: size_t, diff --git a/src/librustc_codegen_llvm/llvm/mod.rs b/src/librustc_codegen_llvm/llvm/mod.rs index 6bca2a16221..20c88d94a41 100644 --- a/src/librustc_codegen_llvm/llvm/mod.rs +++ b/src/librustc_codegen_llvm/llvm/mod.rs @@ -237,7 +237,7 @@ pub fn build_string(f: F) -> Option String::from_utf8(buf.into_inner()).ok() } -pub unsafe fn twine_to_string(tr: TwineRef) -> String { +pub unsafe fn twine_to_string(tr: &Twine) -> String { build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM") }