Skip to content

Commit 44ae6f1

Browse files
committed
rustc_codegen_llvm: use safe references for Twine, DiagnosticInfo, SMDiagnostic.
1 parent 41d7d8e commit 44ae6f1

File tree

4 files changed

+24
-29
lines changed

4 files changed

+24
-29
lines changed

src/librustc_codegen_llvm/back/write.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ use rustc::session::config::{self, OutputFilenames, OutputType, Passes, SomePass
2525
use rustc::session::Session;
2626
use rustc::util::nodemap::FxHashMap;
2727
use time_graph::{self, TimeGraph, Timeline};
28-
use llvm;
29-
use llvm::{PassManagerRef, DiagnosticInfoRef};
30-
use llvm::SMDiagnosticRef;
28+
use llvm::{self, DiagnosticInfo, PassManagerRef, SMDiagnostic};
3129
use {CodegenResults, ModuleSource, ModuleCodegen, CompiledModule, ModuleKind};
3230
use CrateInfo;
3331
use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
@@ -431,7 +429,7 @@ unsafe extern "C" fn report_inline_asm<'a, 'b>(cgcx: &'a CodegenContext,
431429
cgcx.diag_emitter.inline_asm_error(cookie as u32, msg.to_string());
432430
}
433431

434-
unsafe extern "C" fn inline_asm_handler(diag: SMDiagnosticRef,
432+
unsafe extern "C" fn inline_asm_handler(diag: &SMDiagnostic,
435433
user: *const c_void,
436434
cookie: c_uint) {
437435
if user.is_null() {
@@ -445,7 +443,7 @@ unsafe extern "C" fn inline_asm_handler(diag: SMDiagnosticRef,
445443
report_inline_asm(cgcx, &msg, cookie);
446444
}
447445

448-
unsafe extern "C" fn diagnostic_handler(info: DiagnosticInfoRef, user: *mut c_void) {
446+
unsafe extern "C" fn diagnostic_handler(info: &DiagnosticInfo, user: *mut c_void) {
449447
if user.is_null() {
450448
return
451449
}

src/librustc_codegen_llvm/llvm/diagnostic.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use self::Diagnostic::*;
1616
use libc::c_uint;
1717
use value::Value;
1818

19-
use super::{DiagnosticInfoRef, TwineRef};
19+
use super::{DiagnosticInfo, Twine};
2020

2121
#[derive(Copy, Clone)]
2222
pub enum OptimizationDiagnosticKind {
@@ -55,7 +55,7 @@ pub struct OptimizationDiagnostic<'ll> {
5555
impl OptimizationDiagnostic<'ll> {
5656
unsafe fn unpack(
5757
kind: OptimizationDiagnosticKind,
58-
di: DiagnosticInfoRef,
58+
di: &'ll DiagnosticInfo,
5959
) -> Self {
6060
let mut function = None;
6161
let mut line = 0;
@@ -97,14 +97,14 @@ impl OptimizationDiagnostic<'ll> {
9797
#[derive(Copy, Clone)]
9898
pub struct InlineAsmDiagnostic<'ll> {
9999
pub cookie: c_uint,
100-
pub message: TwineRef,
100+
pub message: &'ll Twine,
101101
pub instruction: &'ll Value,
102102
}
103103

104104
impl InlineAsmDiagnostic<'ll> {
105-
unsafe fn unpack(di: DiagnosticInfoRef) -> Self {
105+
unsafe fn unpack(di: &'ll DiagnosticInfo) -> Self {
106106
let mut cookie = 0;
107-
let mut message = 0 as *mut _;
107+
let mut message = None;
108108
let mut instruction = None;
109109

110110
super::LLVMRustUnpackInlineAsmDiagnostic(
@@ -116,7 +116,7 @@ impl InlineAsmDiagnostic<'ll> {
116116

117117
InlineAsmDiagnostic {
118118
cookie,
119-
message,
119+
message: message.unwrap(),
120120
instruction: instruction.unwrap(),
121121
}
122122
}
@@ -125,14 +125,14 @@ impl InlineAsmDiagnostic<'ll> {
125125
pub enum Diagnostic<'ll> {
126126
Optimization(OptimizationDiagnostic<'ll>),
127127
InlineAsm(InlineAsmDiagnostic<'ll>),
128-
PGO(DiagnosticInfoRef),
128+
PGO(&'ll DiagnosticInfo),
129129

130130
/// LLVM has other types that we do not wrap here.
131-
UnknownDiagnostic(DiagnosticInfoRef),
131+
UnknownDiagnostic(&'ll DiagnosticInfo),
132132
}
133133

134134
impl Diagnostic<'ll> {
135-
pub unsafe fn unpack(di: DiagnosticInfoRef) -> Self {
135+
pub unsafe fn unpack(di: &'ll DiagnosticInfo) -> Self {
136136
use super::DiagnosticKind as Dk;
137137
let kind = super::LLVMRustGetDiagInfoKind(di);
138138

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -404,20 +404,17 @@ pub type ArchiveIteratorRef = *mut ArchiveIterator;
404404
extern { pub type ArchiveChild; }
405405
pub type ArchiveChildRef = *mut ArchiveChild;
406406
extern { pub type Twine; }
407-
pub type TwineRef = *mut Twine;
408407
extern { pub type DiagnosticInfo; }
409-
pub type DiagnosticInfoRef = *mut DiagnosticInfo;
410408
extern { pub type SMDiagnostic; }
411-
pub type SMDiagnosticRef = *mut SMDiagnostic;
412409
extern { pub type RustArchiveMember; }
413410
pub type RustArchiveMemberRef = *mut RustArchiveMember;
414411
extern { pub type OperandBundleDef; }
415412
pub type OperandBundleDefRef = *mut OperandBundleDef;
416413
extern { pub type Linker; }
417414
pub type LinkerRef = *mut Linker;
418415

419-
pub type DiagnosticHandler = unsafe extern "C" fn(DiagnosticInfoRef, *mut c_void);
420-
pub type InlineAsmDiagHandler = unsafe extern "C" fn(SMDiagnosticRef, *const c_void, c_uint);
416+
pub type DiagnosticHandler = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
417+
pub type InlineAsmDiagHandler = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint);
421418

422419

423420
pub mod debuginfo {
@@ -1481,32 +1478,32 @@ extern "C" {
14811478

14821479
pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: *mut *const c_char) -> size_t;
14831480

1484-
pub fn LLVMRustWriteTwineToString(T: TwineRef, s: RustStringRef);
1481+
pub fn LLVMRustWriteTwineToString(T: &Twine, s: RustStringRef);
14851482

14861483
pub fn LLVMContextSetDiagnosticHandler(C: &Context,
14871484
Handler: DiagnosticHandler,
14881485
DiagnosticContext: *mut c_void);
14891486

1490-
pub fn LLVMRustUnpackOptimizationDiagnostic(DI: DiagnosticInfoRef,
1487+
pub fn LLVMRustUnpackOptimizationDiagnostic(DI: &'a DiagnosticInfo,
14911488
pass_name_out: RustStringRef,
1492-
function_out: *mut Option<&Value>,
1489+
function_out: *mut Option<&'a Value>,
14931490
loc_line_out: *mut c_uint,
14941491
loc_column_out: *mut c_uint,
14951492
loc_filename_out: RustStringRef,
14961493
message_out: RustStringRef);
1497-
pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: DiagnosticInfoRef,
1494+
pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: &'a DiagnosticInfo,
14981495
cookie_out: *mut c_uint,
1499-
message_out: *mut TwineRef,
1500-
instruction_out: *mut Option<&Value>);
1496+
message_out: *mut Option<&'a Twine>,
1497+
instruction_out: *mut Option<&'a Value>);
15011498

1502-
pub fn LLVMRustWriteDiagnosticInfoToString(DI: DiagnosticInfoRef, s: RustStringRef);
1503-
pub fn LLVMRustGetDiagInfoKind(DI: DiagnosticInfoRef) -> DiagnosticKind;
1499+
pub fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: RustStringRef);
1500+
pub fn LLVMRustGetDiagInfoKind(DI: &DiagnosticInfo) -> DiagnosticKind;
15041501

15051502
pub fn LLVMRustSetInlineAsmDiagnosticHandler(C: &Context,
15061503
H: InlineAsmDiagHandler,
15071504
CX: *mut c_void);
15081505

1509-
pub fn LLVMRustWriteSMDiagnosticToString(d: SMDiagnosticRef, s: RustStringRef);
1506+
pub fn LLVMRustWriteSMDiagnosticToString(d: &SMDiagnostic, s: RustStringRef);
15101507

15111508
pub fn LLVMRustWriteArchive(Dst: *const c_char,
15121509
NumMembers: size_t,

src/librustc_codegen_llvm/llvm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pub fn build_string<F>(f: F) -> Option<String>
237237
String::from_utf8(buf.into_inner()).ok()
238238
}
239239

240-
pub unsafe fn twine_to_string(tr: TwineRef) -> String {
240+
pub unsafe fn twine_to_string(tr: &Twine) -> String {
241241
build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
242242
}
243243

0 commit comments

Comments
 (0)