Skip to content

Commit 5ec2038

Browse files
committed
rt/core: Remove last_os_error from rust_builtin and instead use StrError from LLVM.
1 parent 0c2b4ed commit 5ec2038

14 files changed

+43
-76
lines changed

src/libcore/io.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl *libc::FILE: Writer {
607607
*self);
608608
if nout != len as size_t {
609609
error!("error writing buffer");
610-
log(error, os::last_os_error());
610+
log(error, os::last_error());
611611
die!();
612612
}
613613
}
@@ -657,7 +657,7 @@ impl fd_t: Writer {
657657
let nout = libc::write(*self, vb, len as size_t);
658658
if nout < 0 as ssize_t {
659659
error!("error writing buffer");
660-
log(error, os::last_os_error());
660+
log(error, os::last_error());
661661
die!();
662662
}
663663
count += nout as uint;
@@ -731,7 +731,7 @@ pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
731731
};
732732
if fd < (0 as c_int) {
733733
result::Err(fmt!("error opening %s: %s", path.to_str(),
734-
os::last_os_error()))
734+
os::last_error()))
735735
} else {
736736
result::Ok(fd_writer(fd, true))
737737
}

src/libcore/os.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ extern mod rustrt {
6262
unsafe fn rust_path_exists(path: *libc::c_char) -> c_int;
6363
unsafe fn rust_list_files2(&&path: ~str) -> ~[~str];
6464
unsafe fn rust_process_wait(handle: c_int) -> c_int;
65-
unsafe fn last_os_error() -> ~str;
6665
unsafe fn rust_set_exit_status(code: libc::intptr_t);
6766
}
6867

68+
extern mod rustllvm {
69+
unsafe fn LLVMGetLastError() -> *libc::c_char;
70+
}
71+
6972
pub const tmpbuf_sz : uint = 1000u;
7073

7174
pub fn getcwd() -> Path {
@@ -767,9 +770,12 @@ pub fn remove_file(p: &Path) -> bool {
767770
}
768771

769772
/// Get a string representing the platform-dependent last error
770-
pub fn last_os_error() -> ~str {
773+
pub fn last_error() -> ~str {
771774
unsafe {
772-
rustrt::last_os_error()
775+
let e = rustllvm::LLVMGetLastError();
776+
let err_str = str::raw::from_c_str(e);
777+
libc::free(e as *libc::c_void);
778+
err_str
773779
}
774780
}
775781

@@ -1000,8 +1006,8 @@ mod tests {
10001006
use vec;
10011007

10021008
#[test]
1003-
pub fn last_os_error() {
1004-
log(debug, os::last_os_error());
1009+
pub fn last_error() {
1010+
log(debug, os::last_error());
10051011
}
10061012

10071013
#[test]

src/rt/rust_builtin.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,50 +52,6 @@ timegm(struct tm *tm)
5252
}
5353
#endif
5454

55-
56-
extern "C" CDECL rust_str*
57-
last_os_error() {
58-
rust_task *task = rust_get_current_task();
59-
60-
LOG(task, task, "last_os_error()");
61-
62-
#if defined(__WIN32__)
63-
LPTSTR buf;
64-
DWORD err = GetLastError();
65-
DWORD res = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
66-
FORMAT_MESSAGE_FROM_SYSTEM |
67-
FORMAT_MESSAGE_IGNORE_INSERTS,
68-
NULL, err,
69-
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
70-
(LPTSTR) &buf, 0, NULL);
71-
if (!res) {
72-
task->fail();
73-
return NULL;
74-
}
75-
#elif defined(_GNU_SOURCE) && !defined(__ANDROID__)
76-
char cbuf[BUF_BYTES];
77-
char *buf = strerror_r(errno, cbuf, sizeof(cbuf));
78-
if (!buf) {
79-
task->fail();
80-
return NULL;
81-
}
82-
#else
83-
char buf[BUF_BYTES];
84-
int err = strerror_r(errno, buf, sizeof(buf));
85-
if (err) {
86-
task->fail();
87-
return NULL;
88-
}
89-
#endif
90-
91-
rust_str * st = make_str(task->kernel, buf, strlen(buf),
92-
"last_os_error");
93-
#ifdef __WIN32__
94-
LocalFree((HLOCAL)buf);
95-
#endif
96-
return st;
97-
}
98-
9955
extern "C" CDECL rust_str *
10056
rust_getcwd() {
10157
rust_task *task = rust_get_current_task();

src/rt/rustrt.def.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ rust_gmtime
1414
rust_localtime
1515
rust_timegm
1616
rust_mktime
17-
last_os_error
1817
new_task
1918
precise_time_ns
2019
rand_free

src/rustllvm/RustWrapper.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "llvm/Support/Timer.h"
3131
#include "llvm/Support/raw_ostream.h"
3232
#include "llvm/Target/TargetMachine.h"
33+
#include "llvm/Support/Errno.h"
3334
#include "llvm/Support/TargetSelect.h"
3435
#include "llvm/Support/TargetRegistry.h"
3536
#include "llvm/Support/SourceMgr.h"
@@ -72,6 +73,13 @@ extern "C" const char *LLVMRustGetLastError(void) {
7273
return LLVMRustError;
7374
}
7475

76+
extern "C" const char *LLVMGetLastError(void) {
77+
std::string e = StrError();
78+
char *err = (char *) malloc(e.length() + 1);
79+
std::strcpy(err, e.c_str());
80+
return err;
81+
}
82+
7583
extern "C" void LLVMAddBasicAliasAnalysisPass(LLVMPassManagerRef PM);
7684

7785
extern "C" void LLVMRustAddPrintModulePass(LLVMPassManagerRef PMR,

src/rustllvm/rustllvm.def.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,4 @@ LLVMX86MMXTypeInContext
577577
LLVMConstNamedStruct
578578
LLVMStructCreateNamed
579579
LLVMStructSetBody
580+
LLVMGetLastError

src/test/auxiliary/anon-extern-mod-cross-crate-1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
// except according to those terms.
1010

1111
#[abi = "cdecl"];
12-
#[link_name = "rustrt"];
12+
#[link_name = "rustllvm"];
1313
#[link(name = "anonexternmod",
1414
vers = "0.1")];
1515

1616
#[crate_type = "lib"];
1717
extern {
18-
fn last_os_error() -> ~str;
18+
unsafe fn LLVMGetLastError() -> *core::libc::c_char;
1919
}

src/test/auxiliary/foreign_lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
#[link(name="foreign_lib", vers="0.0")];
1212

13-
pub extern mod rustrt {
14-
pub fn last_os_error() -> ~str;
13+
pub extern mod rustllvm {
14+
pub unsafe fn LLVMGetLastError() -> *core::libc::c_char;
1515
}

src/test/run-fail/morestack2.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717

1818
extern mod std;
1919

20-
extern mod rustrt {
21-
pub fn last_os_error() -> ~str;
22-
}
2320

2421
fn getbig_call_c_and_fail(i: int) {
2522
if i != 0 {
2623
getbig_call_c_and_fail(i - 1);
2724
} else {
2825
unsafe {
29-
rustrt::last_os_error();
26+
core::os::last_error();
3027
die!();
3128
}
3229
}

src/test/run-pass/anon-extern-mod-cross-crate-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ extern mod anonexternmod;
1515
use anonexternmod::*;
1616

1717
pub fn main() {
18-
last_os_error();
18+
LLVMGetLastError();
1919
}

src/test/run-pass/anon-extern-mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
// except according to those terms.
1010

1111
#[abi = "cdecl"]
12-
#[link_name = "rustrt"]
12+
#[link_name = "rustllvm"]
1313
extern {
14-
fn last_os_error() -> ~str;
14+
unsafe fn LLVMGetLastError() -> *libc::c_char;
1515
}
1616

1717
pub fn main() {
1818
unsafe {
19-
let _ = last_os_error();
19+
let _ = LLVMGetLastError();
2020
}
2121
}

src/test/run-pass/foreign-dupe.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
// calling pin_task and that's having wierd side-effects.
1313

1414
#[abi = "cdecl"]
15-
#[link_name = "rustrt"]
16-
extern mod rustrt1 {
17-
pub fn last_os_error() -> ~str;
15+
#[link_name = "rustllvm"]
16+
extern mod rustllvm1 {
17+
pub unsafe fn LLVMGetLastError() -> *libc::c_char;
1818
}
1919

2020
#[abi = "cdecl"]
21-
#[link_name = "rustrt"]
22-
extern mod rustrt2 {
23-
pub fn last_os_error() -> ~str;
21+
#[link_name = "rustllvm"]
22+
extern mod rustllvm2 {
23+
pub unsafe fn LLVMGetLastError() -> *libc::c_char;
2424
}
2525

2626
pub fn main() {
2727
unsafe {
28-
rustrt1::last_os_error();
29-
rustrt2::last_os_error();
28+
rustllvm1::LLVMGetLastError();
29+
rustllvm2::LLVMGetLastError();
3030
}
3131
}

src/test/run-pass/invoke-external-foreign.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@
1818
extern mod foreign_lib;
1919

2020
pub fn main() {
21-
let foo = foreign_lib::rustrt::last_os_error();
21+
unsafe {
22+
let _foo = foreign_lib::rustllvm::LLVMGetLastError();
23+
}
2224
}

src/test/run-pass/morestack6.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ extern mod rustrt {
1515
pub fn debug_get_stk_seg() -> *u8;
1616

1717
pub fn rust_get_sched_id() -> libc::intptr_t;
18-
pub fn last_os_error() -> ~str;
1918
pub fn rust_getcwd() -> ~str;
2019
pub fn get_task_id() -> libc::intptr_t;
2120
pub fn rust_sched_threads();
2221
pub fn rust_get_task();
2322
}
2423

2524
fn calllink01() { unsafe { rustrt::rust_get_sched_id(); } }
26-
fn calllink02() { unsafe { rustrt::last_os_error(); } }
2725
fn calllink03() { unsafe { rustrt::rust_getcwd(); } }
2826
fn calllink08() { unsafe { rustrt::get_task_id(); } }
2927
fn calllink09() { unsafe { rustrt::rust_sched_threads(); } }

0 commit comments

Comments
 (0)