Skip to content

Commit

Permalink
rustc_codegen_llvm: use safe references for RustString.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinagpopa committed Jul 30, 2018
1 parent 44ae6f1 commit c1eeb69
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/librustc_codegen_llvm/llvm/diagnostic.rs
Expand Up @@ -73,9 +73,9 @@ impl OptimizationDiagnostic<'ll> {
&mut column,
filename,
message)
)
)
);
).ok()
).ok()
).ok();

let mut filename = filename.unwrap_or(String::new());
if filename.is_empty() {
Expand Down
24 changes: 12 additions & 12 deletions src/librustc_codegen_llvm/llvm/ffi.rs
Expand Up @@ -26,7 +26,7 @@ use libc::{c_ulonglong, c_void};

use std::ptr::NonNull;

use super::RustStringRef;
use super::RustString;

pub type Bool = c_uint;

Expand Down Expand Up @@ -1402,8 +1402,8 @@ extern "C" {
pub fn LLVMRustDIBuilderCreateOpDeref() -> i64;
pub fn LLVMRustDIBuilderCreateOpPlusUconst() -> i64;

pub fn LLVMRustWriteTypeToString(Type: &Type, s: RustStringRef);
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: RustStringRef);
pub fn LLVMRustWriteTypeToString(Type: &Type, s: &RustString);
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);

pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
pub fn LLVMIsAConstantFP(value_ref: &Value) -> Option<&Value>;
Expand Down Expand Up @@ -1478,32 +1478,32 @@ extern "C" {

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,
Handler: DiagnosticHandler,
DiagnosticContext: *mut c_void);

pub fn LLVMRustUnpackOptimizationDiagnostic(DI: &'a DiagnosticInfo,
pass_name_out: RustStringRef,
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);
pass_name_out: &RustString,
function_out: &mut Option<&'a Value>,
loc_line_out: &mut c_uint,
loc_column_out: &mut c_uint,
loc_filename_out: &RustString,
message_out: &RustString);
pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: &'a DiagnosticInfo,
cookie_out: *mut c_uint,
message_out: *mut Option<&'a Twine>,
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 LLVMRustSetInlineAsmDiagnosticHandler(C: &Context,
H: InlineAsmDiagHandler,
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,
NumMembers: size_t,
Expand Down
33 changes: 18 additions & 15 deletions src/librustc_codegen_llvm/llvm/mod.rs
Expand Up @@ -22,6 +22,7 @@ pub use self::CallConv::*;
pub use self::Linkage::*;

use std::str::FromStr;
use std::string::FromUtf8Error;
use std::slice;
use std::ffi::{CString, CStr};
use std::cell::RefCell;
Expand Down Expand Up @@ -92,20 +93,19 @@ impl FromStr for ArchiveKind {
}
}

#[allow(missing_copy_implementations)]
extern { pub type RustString; }
type RustStringRef = *mut RustString;
type RustStringRepr = *mut RefCell<Vec<u8>>;
#[repr(C)]
pub struct RustString {
bytes: RefCell<Vec<u8>>,
}

/// Appending to a Rust string -- used by RawRustStringOstream.
#[no_mangle]
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: RustStringRef,
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: &RustString,
ptr: *const c_char,
size: size_t) {
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);

let sr = sr as RustStringRepr;
(*sr).borrow_mut().extend_from_slice(slice);
sr.bytes.borrow_mut().extend_from_slice(slice);
}

pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
Expand Down Expand Up @@ -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>
where F: FnOnce(RustStringRef)
{
let mut buf = RefCell::new(Vec::new());
f(&mut buf as RustStringRepr as RustStringRef);
String::from_utf8(buf.into_inner()).ok()
pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
let sr = RustString {
bytes: RefCell::new(Vec::new()),
};
f(&sr);
String::from_utf8(sr.bytes.into_inner())
}

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

pub fn last_error() -> Option<String> {
Expand Down

0 comments on commit c1eeb69

Please sign in to comment.