Add rust demangling - #213

This commit is contained in:
Matt Godbolt 2016-12-23 10:02:03 -06:00
parent 2fefdc98ee
commit 314c062c3a
12 changed files with 125 additions and 17 deletions

1
.idea/.gitignore vendored
View File

@ -1,2 +1,3 @@
/.name
/workspace.xml
/xtextAutoBuilderState.xml

View File

@ -0,0 +1,21 @@
<component name="libraryTable">
<library name="Cargo &lt;rustfilt&gt;">
<CLASSES>
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/regex-0.1.80" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/regex-syntax-0.3.9" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-demangle-0.1.3" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-build-0.1.1" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/thread-id-2.0.0" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/winapi-0.2.8" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/thread_local-0.2.7" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.18" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/aho-corasick-0.5.3" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-0.2.2" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/kernel32-sys-0.2.2" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/memchr-0.1.11" />
<root url="file://$USER_HOME$/.cargo/registry/src/github.com-1ecc6299db9ec823/utf8-ranges-0.1.3" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Rust &lt;rustfilt&gt;">
<CLASSES>
<root url="file://$USER_HOME$/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd" />
<root url="file://$USER_HOME$/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore" />
<root url="file://$USER_HOME$/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcollections" />
<root url="file://$USER_HOME$/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/liballoc" />
<root url="file://$USER_HOME$/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/librustc_unicode" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_3" assert-keyword="false" jdk-15="false" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_3" />
<component name="RustProjectSettings">
<option name="toolchainHomeDirectory" value="$USER_HOME$/.cargo/bin" />
</component>
</project>

View File

@ -3,6 +3,7 @@
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/gcc-explorer.iml" filepath="$PROJECT_DIR$/.idea/gcc-explorer.iml" />
<module fileurl="file://$PROJECT_DIR$/rust/rustfilt.iml" filepath="$PROJECT_DIR$/rust/rustfilt.iml" />
</modules>
</component>
</project>

View File

@ -16,9 +16,9 @@ default: run
endif
endif
.PHONY: clean run test run-amazon c-preload optional-d-support prereqs node_modules bower_modules
.PHONY: dist lint
prereqs: optional-d-support node_modules c-preload bower_modules
.PHONY: clean run test run-amazon c-preload optional-d-support optional-rust-support
.PHONY: dist lint prereqs node_modules bower_modules
prereqs: optional-d-support optional-rust-support node_modules c-preload bower_modules
ifneq "" "$(shell which gdc)"
optional-d-support:
@ -31,6 +31,15 @@ optional-d-support:
@echo "D language support disabled"
endif
ifneq "" "$(shell which cargo)"
optional-rust-support:
cd rust && cargo build --release
else
optional-rust-support:
@echo "Rust language support disabled"
endif
NODE_MODULES=.npm-updated
$(NODE_MODULES): package.json
$(NPM) install

View File

@ -1,6 +1,8 @@
compilers=/usr/local/bin/rustc
compileFilename=example.rs
supportsBinary=false
compilerType=rust
demangler=rust/target/release/rustfilt
stubRe=\bmain\b
stubText=pub fn main() {/*stub provided by Compiler Explorer*/}
binaryHideFuncRe=^(_.*|call_gmon_start|(de)?register_tm_clones|frame_dummy|.*@plt.*)$

View File

@ -0,0 +1 @@
compilers=/home/mgodbolt/.cargo/bin/rustc

7
rust/Cargo.lock generated
View File

@ -2,6 +2,7 @@
name = "rustfilt"
version = "0.1.0"
dependencies = [
"lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-demangle 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -23,6 +24,11 @@ dependencies = [
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.18"
@ -93,6 +99,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum lazy_static 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6abe0ee2e758cd6bc8a2cd56726359007748fbf4128da998b65d0b70f881e19b"
"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70"
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"

View File

@ -6,3 +6,4 @@ authors = ["Matt Godbolt <matt@godbolt.org>"]
[dependencies]
rustc-demangle = "*"
regex = "*"
lazy_static = "*"

16
rust/rustfilt.iml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="RUST_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Rust &lt;rustfilt&gt;" level="project" />
<orderEntry type="library" name="Cargo &lt;rustfilt&gt;" level="project" />
</component>
</module>

View File

@ -1,25 +1,58 @@
#[macro_use] extern crate lazy_static;
extern crate rustc_demangle;
extern crate regex;
use std::io;
use std::io::prelude::*;
use regex::Regex;
use regex::Captures;
use rustc_demangle::demangle;
extern crate rustc_demangle;
extern crate regex;
fn demangle_line(line: &str) -> String {
lazy_static! {
static ref RE: Regex = Regex::new(r"[_a-zA-Z$][_a-zA-Z$0-9.]*").unwrap();
}
RE.replace_all(line, |caps: &Captures| {
format!("{:#}", demangle(caps.at(0).unwrap()))
})
}
#[cfg(test)]
mod tests {
#[test]
fn passes_text() {
assert_eq!(
::demangle_line("mo fo\tboom hello "),
"mo fo\tboom hello ");
}
#[test]
fn demangles() {
assert_eq!(
::demangle_line("_ZN7example4main17h0db00b8b32acffd5E:"),
"example::main:");
}
#[test]
fn handles_mid_demangling() {
assert_eq!(
::demangle_line(" lea rax, [rip + _ZN55_$LT$$RF$$u27$a$u20$T$u20$as$u20$core..fmt..Display$GT$3fmt17h510ed05e72307174E]"),
" lea rax, [rip + <&\'a T as core::fmt::Display>::fmt]");
}
#[test]
fn handles_call_plt() {
assert_eq!(
::demangle_line(" call _ZN3std2io5stdio6_print17he48522be5b0a80d9E@PLT"),
" call std::io::stdio::_print@PLT");
}
}
fn main() {
let stdin = io::stdin();
let token = Regex::new(r"[_a-zA-Z$][_a-zA-Z$0-9]*").unwrap();
for line in stdin.lock().lines() {
println!("{}",
line.unwrap()
.split_whitespace()
.map(|x| demangle(x).to_string())
.collect::<Vec<String>>()
.join(" "));
// println!("{}", token.replace(line.unwrap(), |caps: &Captures| {
// demangle(caps.at(0).unwrap()).to_string()
// }));
println!("{}", demangle_line(&line.unwrap()));
}
}
}