rustc_codegen_llvm: use safe references for RustString.
This commit is contained in:
parent
44ae6f1909
commit
c1eeb69ce8
3 changed files with 33 additions and 30 deletions
|
@ -73,9 +73,9 @@ impl OptimizationDiagnostic<'ll> {
|
||||||
&mut column,
|
&mut column,
|
||||||
filename,
|
filename,
|
||||||
message)
|
message)
|
||||||
)
|
).ok()
|
||||||
)
|
).ok()
|
||||||
);
|
).ok();
|
||||||
|
|
||||||
let mut filename = filename.unwrap_or(String::new());
|
let mut filename = filename.unwrap_or(String::new());
|
||||||
if filename.is_empty() {
|
if filename.is_empty() {
|
||||||
|
|
|
@ -26,7 +26,7 @@ use libc::{c_ulonglong, c_void};
|
||||||
|
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
|
|
||||||
use super::RustStringRef;
|
use super::RustString;
|
||||||
|
|
||||||
pub type Bool = c_uint;
|
pub type Bool = c_uint;
|
||||||
|
|
||||||
|
@ -1402,8 +1402,8 @@ extern "C" {
|
||||||
pub fn LLVMRustDIBuilderCreateOpDeref() -> i64;
|
pub fn LLVMRustDIBuilderCreateOpDeref() -> i64;
|
||||||
pub fn LLVMRustDIBuilderCreateOpPlusUconst() -> i64;
|
pub fn LLVMRustDIBuilderCreateOpPlusUconst() -> i64;
|
||||||
|
|
||||||
pub fn LLVMRustWriteTypeToString(Type: &Type, s: RustStringRef);
|
pub fn LLVMRustWriteTypeToString(Type: &Type, s: &RustString);
|
||||||
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: RustStringRef);
|
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);
|
||||||
|
|
||||||
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
|
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
|
||||||
pub fn LLVMIsAConstantFP(value_ref: &Value) -> Option<&Value>;
|
pub fn LLVMIsAConstantFP(value_ref: &Value) -> Option<&Value>;
|
||||||
|
@ -1478,32 +1478,32 @@ extern "C" {
|
||||||
|
|
||||||
pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: *mut *const c_char) -> size_t;
|
pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: *mut *const c_char) -> size_t;
|
||||||
|
|
||||||
pub fn LLVMRustWriteTwineToString(T: &Twine, s: RustStringRef);
|
pub fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);
|
||||||
|
|
||||||
pub fn LLVMContextSetDiagnosticHandler(C: &Context,
|
pub fn LLVMContextSetDiagnosticHandler(C: &Context,
|
||||||
Handler: DiagnosticHandler,
|
Handler: DiagnosticHandler,
|
||||||
DiagnosticContext: *mut c_void);
|
DiagnosticContext: *mut c_void);
|
||||||
|
|
||||||
pub fn LLVMRustUnpackOptimizationDiagnostic(DI: &'a DiagnosticInfo,
|
pub fn LLVMRustUnpackOptimizationDiagnostic(DI: &'a DiagnosticInfo,
|
||||||
pass_name_out: RustStringRef,
|
pass_name_out: &RustString,
|
||||||
function_out: *mut Option<&'a Value>,
|
function_out: &mut Option<&'a Value>,
|
||||||
loc_line_out: *mut c_uint,
|
loc_line_out: &mut c_uint,
|
||||||
loc_column_out: *mut c_uint,
|
loc_column_out: &mut c_uint,
|
||||||
loc_filename_out: RustStringRef,
|
loc_filename_out: &RustString,
|
||||||
message_out: RustStringRef);
|
message_out: &RustString);
|
||||||
pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: &'a DiagnosticInfo,
|
pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: &'a DiagnosticInfo,
|
||||||
cookie_out: *mut c_uint,
|
cookie_out: *mut c_uint,
|
||||||
message_out: *mut Option<&'a Twine>,
|
message_out: *mut Option<&'a Twine>,
|
||||||
instruction_out: *mut Option<&'a Value>);
|
instruction_out: *mut Option<&'a Value>);
|
||||||
|
|
||||||
pub fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: RustStringRef);
|
pub fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: &RustString);
|
||||||
pub fn LLVMRustGetDiagInfoKind(DI: &DiagnosticInfo) -> DiagnosticKind;
|
pub fn LLVMRustGetDiagInfoKind(DI: &DiagnosticInfo) -> DiagnosticKind;
|
||||||
|
|
||||||
pub fn LLVMRustSetInlineAsmDiagnosticHandler(C: &Context,
|
pub fn LLVMRustSetInlineAsmDiagnosticHandler(C: &Context,
|
||||||
H: InlineAsmDiagHandler,
|
H: InlineAsmDiagHandler,
|
||||||
CX: *mut c_void);
|
CX: *mut c_void);
|
||||||
|
|
||||||
pub fn LLVMRustWriteSMDiagnosticToString(d: &SMDiagnostic, s: RustStringRef);
|
pub fn LLVMRustWriteSMDiagnosticToString(d: &SMDiagnostic, s: &RustString);
|
||||||
|
|
||||||
pub fn LLVMRustWriteArchive(Dst: *const c_char,
|
pub fn LLVMRustWriteArchive(Dst: *const c_char,
|
||||||
NumMembers: size_t,
|
NumMembers: size_t,
|
||||||
|
|
|
@ -22,6 +22,7 @@ pub use self::CallConv::*;
|
||||||
pub use self::Linkage::*;
|
pub use self::Linkage::*;
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use std::string::FromUtf8Error;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
use std::ffi::{CString, CStr};
|
use std::ffi::{CString, CStr};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
@ -92,20 +93,19 @@ impl FromStr for ArchiveKind {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(missing_copy_implementations)]
|
#[repr(C)]
|
||||||
extern { pub type RustString; }
|
pub struct RustString {
|
||||||
type RustStringRef = *mut RustString;
|
bytes: RefCell<Vec<u8>>,
|
||||||
type RustStringRepr = *mut RefCell<Vec<u8>>;
|
}
|
||||||
|
|
||||||
/// Appending to a Rust string -- used by RawRustStringOstream.
|
/// Appending to a Rust string -- used by RawRustStringOstream.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: RustStringRef,
|
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: &RustString,
|
||||||
ptr: *const c_char,
|
ptr: *const c_char,
|
||||||
size: size_t) {
|
size: size_t) {
|
||||||
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
|
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
|
||||||
|
|
||||||
let sr = sr as RustStringRepr;
|
sr.bytes.borrow_mut().extend_from_slice(slice);
|
||||||
(*sr).borrow_mut().extend_from_slice(slice);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
|
pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
|
||||||
|
@ -229,16 +229,19 @@ pub fn get_param(llfn: &'a Value, index: c_uint) -> &'a Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_string<F>(f: F) -> Option<String>
|
pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
|
||||||
where F: FnOnce(RustStringRef)
|
let sr = RustString {
|
||||||
{
|
bytes: RefCell::new(Vec::new()),
|
||||||
let mut buf = RefCell::new(Vec::new());
|
};
|
||||||
f(&mut buf as RustStringRepr as RustStringRef);
|
f(&sr);
|
||||||
String::from_utf8(buf.into_inner()).ok()
|
String::from_utf8(sr.bytes.into_inner())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn twine_to_string(tr: &Twine) -> String {
|
pub fn twine_to_string(tr: &Twine) -> String {
|
||||||
build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
|
unsafe {
|
||||||
|
build_string(|s| LLVMRustWriteTwineToString(tr, s))
|
||||||
|
.expect("got a non-UTF8 Twine from LLVM")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn last_error() -> Option<String> {
|
pub fn last_error() -> Option<String> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue