-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C API wrapper #14
Merged
Merged
C API wrapper #14
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e8ae32f
std based C API
zokier a2da0df
Build and test C API with Travis
zokier 2495462
Use full lto for release profile
zokier df8cb59
Merge remote-tracking branch 'alex/master' into wut
alexcrichton 256d83b
Add CI for GitHub Actions
alexcrichton 295c176
Move demangle crate to a `crates` directory
alexcrichton ed91169
Run rustfmt
alexcrichton 3deb55d
Use a `match` instead of `unwrap`
alexcrichton 52ff4fa
Run rustfmt
alexcrichton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "rustc-demangle-capi" | ||
version = "0.1.0" | ||
authors = ["Torste Aikio <zokier@gmail.com>"] | ||
|
||
[lib] | ||
name = "rustc_demangle" | ||
crate-type = ["staticlib", "cdylib"] | ||
|
||
[dependencies] | ||
rustc-demangle = { version = "*", path = ".." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef RUST_DEMANGLE_H_ | ||
#define RUST_DEMANGLE_H_ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// Demangles symbol given in `mangled` argument into `out` buffer | ||
// | ||
// Returns 0 if `mangled` is not Rust symbol or if `out` buffer is too small | ||
// Returns 1 otherwise | ||
int rustc_demangle(const char *mangled, char *out, size_t out_size); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // RUSTC_DEMANGLE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
extern crate rustc_demangle; | ||
|
||
use std::os::raw::{c_char,c_int}; | ||
use std::io::Write; | ||
|
||
/// C-style interface for demangling. | ||
/// Demangles symbol given in `mangled` argument into `out` buffer | ||
/// | ||
/// Unsafe as it handles buffers by raw pointers. | ||
/// | ||
/// Returns 0 if `mangled` is not Rust symbol or if `out` buffer is too small | ||
/// Returns 1 otherwise | ||
#[no_mangle] | ||
pub unsafe extern fn rustc_demangle(mangled: *const c_char, out: *mut c_char, out_size: usize) -> c_int { | ||
let mangled_str = std::ffi::CStr::from_ptr(mangled).to_str(); | ||
if mangled_str.is_err() { | ||
return 0; | ||
} | ||
let mangled_str = mangled_str.unwrap(); | ||
match rustc_demangle::try_demangle(mangled_str) { | ||
Ok(demangle) => { | ||
let mut out_slice = std::slice::from_raw_parts_mut(out as *mut u8, out_size); | ||
match write!(out_slice, "{:#}\0", demangle) { | ||
Ok(_) => { return 1 }, | ||
Err(_) => { return 0 } | ||
} | ||
}, | ||
Err(_) => { return 0 } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::os::raw::c_char; | ||
use std; | ||
#[test] | ||
fn demangle_c_str_large() { | ||
let mangled = "_ZN4testE\0"; | ||
let mut out_buf: Vec<u8> = vec![42;8]; | ||
let res = unsafe { super::rustc_demangle(mangled.as_ptr() as *const c_char, out_buf.as_mut_ptr() as *mut c_char, 8) }; | ||
assert_eq!(res, 1); | ||
let out_str = std::str::from_utf8(&out_buf[..5]).unwrap(); | ||
assert_eq!(out_str, "test\0"); | ||
} | ||
|
||
#[test] | ||
fn demangle_c_str_exact() { | ||
let mangled = "_ZN4testE\0"; | ||
let mut out_buf: Vec<u8> = vec![42;8]; | ||
let res = unsafe { super::rustc_demangle(mangled.as_ptr() as *const c_char, out_buf.as_mut_ptr() as *mut c_char, 5) }; | ||
assert_eq!(res, 1); | ||
let out_str = std::str::from_utf8(&out_buf).unwrap(); | ||
assert_eq!(out_str, "test\0***"); | ||
} | ||
|
||
#[test] | ||
fn demangle_c_str_small() { | ||
let mangled = "_ZN4testE\0"; | ||
let mut out_buf: Vec<u8> = vec![42;8]; | ||
let res = unsafe { super::rustc_demangle(mangled.as_ptr() as *const c_char, out_buf.as_mut_ptr() as *mut c_char, 4) }; | ||
assert_eq!(res, 0); | ||
let out_str = std::str::from_utf8(&out_buf[4..]).unwrap(); | ||
assert_eq!(out_str, "****"); | ||
} | ||
|
||
#[test] | ||
fn demangle_c_str_smaller() { | ||
let mangled = "_ZN4testE\0"; | ||
let mut out_buf: Vec<u8> = vec![42;8]; | ||
let res = unsafe { super::rustc_demangle(mangled.as_ptr() as *const c_char, out_buf.as_mut_ptr() as *mut c_char, 3) }; | ||
assert_eq!(res, 0); | ||
let out_str = std::str::from_utf8(&out_buf[3..]).unwrap(); | ||
assert_eq!(out_str, "*****"); | ||
} | ||
|
||
#[test] | ||
fn demangle_c_str_zero() { | ||
let mangled = "_ZN4testE\0"; | ||
let mut out_buf: Vec<u8> = vec![42;8]; | ||
let res = unsafe { super::rustc_demangle(mangled.as_ptr() as *const c_char, out_buf.as_mut_ptr() as *mut c_char, 0) }; | ||
assert_eq!(res, 0); | ||
let out_str = std::str::from_utf8(&out_buf).unwrap(); | ||
assert_eq!(out_str, "********"); | ||
} | ||
|
||
#[test] | ||
fn demangle_c_str_not_rust_symbol() { | ||
let mangled = "la la la\0"; | ||
let mut out_buf: Vec<u8> = vec![42;8]; | ||
let res = unsafe { super::rustc_demangle(mangled.as_ptr() as *const c_char, out_buf.as_mut_ptr() as *mut c_char, 8) }; | ||
assert_eq!(res, 0); | ||
} | ||
|
||
#[test] | ||
fn demangle_c_str_null() { | ||
let mangled = "\0"; | ||
let mut out_buf: Vec<u8> = vec![42;8]; | ||
let res = unsafe { super::rustc_demangle(mangled.as_ptr() as *const c_char, out_buf.as_mut_ptr() as *mut c_char, 8) }; | ||
assert_eq!(res, 0); | ||
} | ||
|
||
#[test] | ||
fn demangle_c_str_invalid_utf8() { | ||
let mangled = [116, 101, 115, 116, 165, 0]; | ||
let mut out_buf: Vec<u8> = vec![42;8]; | ||
let res = unsafe { super::rustc_demangle(mangled.as_ptr() as *const c_char, out_buf.as_mut_ptr() as *mut c_char, 8) }; | ||
assert_eq!(res, 0); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
lto = true
this andincremental = false
shouldn't be necessary to specify as well