summaryrefslogtreecommitdiffstats
path: root/third_party/rust/rustc-demangle
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/rustc-demangle
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--third_party/rust/rustc-demangle/.cargo-checksum.json1
-rw-r--r--third_party/rust/rustc-demangle/Cargo.toml22
-rw-r--r--third_party/rust/rustc-demangle/LICENSE-APACHE201
-rw-r--r--third_party/rust/rustc-demangle/LICENSE-MIT25
-rw-r--r--third_party/rust/rustc-demangle/README.md24
-rw-r--r--third_party/rust/rustc-demangle/src/lib.rs413
-rw-r--r--third_party/rust/rustc-demangle/src/main.rs4
7 files changed, 690 insertions, 0 deletions
diff --git a/third_party/rust/rustc-demangle/.cargo-checksum.json b/third_party/rust/rustc-demangle/.cargo-checksum.json
new file mode 100644
index 0000000000..cb06033ae5
--- /dev/null
+++ b/third_party/rust/rustc-demangle/.cargo-checksum.json
@@ -0,0 +1 @@
+{"files":{"Cargo.toml":"8ceaf60013499028a9d0ec93e4b0c5cef763c1bc31dfa117e5ab6989b3ebda6a","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"378f5840b258e2779c39418f3f2d7b2ba96f1c7917dd6be0713f88305dbda397","README.md":"7bb7dbc6378a31b5b925a0cff628a69006c77a4cdf3533f7507b7c8fbe3492b0","src/lib.rs":"2a6aaafcc0c6e398321b33187b47b3ee70ad35f44a22e9d4e1e1d24c5c223f3f","src/main.rs":"b84cf381d0ac832be4fb13f6bbec2fd0a0d0c12d1d68aaa84db7a6d76b1466cb"},"package":"76d7ba1feafada44f2d38eed812bd2489a03c0f5abb975799251518b68848649"} \ No newline at end of file
diff --git a/third_party/rust/rustc-demangle/Cargo.toml b/third_party/rust/rustc-demangle/Cargo.toml
new file mode 100644
index 0000000000..6e57b27e5f
--- /dev/null
+++ b/third_party/rust/rustc-demangle/Cargo.toml
@@ -0,0 +1,22 @@
+# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
+#
+# When uploading crates to the registry Cargo will automatically
+# "normalize" Cargo.toml files for maximal compatibility
+# with all versions of Cargo and also rewrite `path` dependencies
+# to registry (e.g. crates.io) dependencies
+#
+# If you believe there's an error in this file please file an
+# issue against the rust-lang/cargo repository. If you're
+# editing this file be aware that the upstream Cargo.toml
+# will likely look very different (and much more reasonable)
+
+[package]
+name = "rustc-demangle"
+version = "0.1.8"
+authors = ["Alex Crichton <alex@alexcrichton.com>"]
+description = "Rust compiler symbol demangling.\n"
+homepage = "https://github.com/alexcrichton/rustc-demangle"
+documentation = "https://docs.rs/rustc-demangle"
+readme = "README.md"
+license = "MIT/Apache-2.0"
+repository = "https://github.com/alexcrichton/rustc-demangle"
diff --git a/third_party/rust/rustc-demangle/LICENSE-APACHE b/third_party/rust/rustc-demangle/LICENSE-APACHE
new file mode 100644
index 0000000000..16fe87b06e
--- /dev/null
+++ b/third_party/rust/rustc-demangle/LICENSE-APACHE
@@ -0,0 +1,201 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+END OF TERMS AND CONDITIONS
+
+APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+Copyright [yyyy] [name of copyright owner]
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
diff --git a/third_party/rust/rustc-demangle/LICENSE-MIT b/third_party/rust/rustc-demangle/LICENSE-MIT
new file mode 100644
index 0000000000..39e0ed6602
--- /dev/null
+++ b/third_party/rust/rustc-demangle/LICENSE-MIT
@@ -0,0 +1,25 @@
+Copyright (c) 2014 Alex Crichton
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
diff --git a/third_party/rust/rustc-demangle/README.md b/third_party/rust/rustc-demangle/README.md
new file mode 100644
index 0000000000..4b480b86c1
--- /dev/null
+++ b/third_party/rust/rustc-demangle/README.md
@@ -0,0 +1,24 @@
+# rustc-demangle
+
+Symbol demangling for Rust
+
+[![Build Status](https://travis-ci.org/alexcrichton/rustc-demangle.svg?branch=master)](https://travis-ci.org/alexcrichton/rustc-demangle)
+
+[Documentation](https://docs.rs/rustc-demangle)
+
+# License
+
+This project is licensed under either of
+
+ * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
+ http://www.apache.org/licenses/LICENSE-2.0)
+ * MIT license ([LICENSE-MIT](LICENSE-MIT) or
+ http://opensource.org/licenses/MIT)
+
+at your option.
+
+### Contribution
+
+Unless you explicitly state otherwise, any contribution intentionally submitted
+for inclusion in rustc-demangle you, as defined in the Apache-2.0 license, shall
+be dual licensed as above, without any additional terms or conditions.
diff --git a/third_party/rust/rustc-demangle/src/lib.rs b/third_party/rust/rustc-demangle/src/lib.rs
new file mode 100644
index 0000000000..d14f4f60ea
--- /dev/null
+++ b/third_party/rust/rustc-demangle/src/lib.rs
@@ -0,0 +1,413 @@
+//! Demangle Rust compiler symbol names.
+//!
+//! This crate provides a `demangle` function which will return a `Demangle`
+//! sentinel value that can be used to learn about the demangled version of a
+//! symbol name. The demangled representation will be the same as the original
+//! if it doesn't look like a mangled symbol name.
+//!
+//! `Demangle` can be formatted with the `Display` trait. The alternate
+//! modifier (`#`) can be used to format the symbol name without the
+//! trailing hash value.
+//!
+//! # Examples
+//!
+//! ```
+//! use rustc_demangle::demangle;
+//!
+//! assert_eq!(demangle("_ZN4testE").to_string(), "test");
+//! assert_eq!(demangle("_ZN3foo3barE").to_string(), "foo::bar");
+//! assert_eq!(demangle("foo").to_string(), "foo");
+//! // With hash
+//! assert_eq!(format!("{}", demangle("_ZN3foo17h05af221e174051e9E")), "foo::h05af221e174051e9");
+//! // Without hash
+//! assert_eq!(format!("{:#}", demangle("_ZN3foo17h05af221e174051e9E")), "foo");
+//! ```
+
+#![no_std]
+#![deny(missing_docs)]
+
+#[cfg(test)]
+#[macro_use]
+extern crate std;
+
+use core::fmt;
+
+/// Representation of a demangled symbol name.
+pub struct Demangle<'a> {
+ original: &'a str,
+ inner: &'a str,
+ valid: bool,
+ /// The number of ::-separated elements in the original name.
+ elements: usize,
+}
+
+/// De-mangles a Rust symbol into a more readable version
+///
+/// All rust symbols by default are mangled as they contain characters that
+/// cannot be represented in all object files. The mangling mechanism is similar
+/// to C++'s, but Rust has a few specifics to handle items like lifetimes in
+/// symbols.
+///
+/// This function will take a **mangled** symbol (typically acquired from a
+/// `Symbol` which is in turn resolved from a `Frame`) and then writes the
+/// de-mangled version into the given `writer`. If the symbol does not look like
+/// a mangled symbol, it is still written to `writer`.
+///
+/// # Examples
+///
+/// ```
+/// use rustc_demangle::demangle;
+///
+/// assert_eq!(demangle("_ZN4testE").to_string(), "test");
+/// assert_eq!(demangle("_ZN3foo3barE").to_string(), "foo::bar");
+/// assert_eq!(demangle("foo").to_string(), "foo");
+/// ```
+
+// All rust symbols are in theory lists of "::"-separated identifiers. Some
+// assemblers, however, can't handle these characters in symbol names. To get
+// around this, we use C++-style mangling. The mangling method is:
+//
+// 1. Prefix the symbol with "_ZN"
+// 2. For each element of the path, emit the length plus the element
+// 3. End the path with "E"
+//
+// For example, "_ZN4testE" => "test" and "_ZN3foo3barE" => "foo::bar".
+//
+// We're the ones printing our backtraces, so we can't rely on anything else to
+// demangle our symbols. It's *much* nicer to look at demangled symbols, so
+// this function is implemented to give us nice pretty output.
+//
+// Note that this demangler isn't quite as fancy as it could be. We have lots
+// of other information in our symbols like hashes, version, type information,
+// etc. Additionally, this doesn't handle glue symbols at all.
+pub fn demangle(mut s: &str) -> Demangle {
+ // During ThinLTO LLVM may import and rename internal symbols, so strip out
+ // those endings first as they're on of the last manglings applied to symbol
+ // names.
+ let llvm = ".llvm.";
+ if let Some(i) = s.find(llvm) {
+ let candidate = &s[i + llvm.len()..];
+ let all_hex = candidate.chars().all(|c| {
+ match c {
+ 'A' ... 'F' | '0' ... '9' | '@' => true,
+ _ => false,
+ }
+ });
+
+ if all_hex {
+ s = &s[..i];
+ }
+ }
+
+ // First validate the symbol. If it doesn't look like anything we're
+ // expecting, we just print it literally. Note that we must handle non-rust
+ // symbols because we could have any function in the backtrace.
+ let mut valid = true;
+ let mut inner = s;
+ if s.len() > 4 && s.starts_with("_ZN") && s.ends_with('E') {
+ inner = &s[3..s.len() - 1];
+ } else if s.len() > 3 && s.starts_with("ZN") && s.ends_with('E') {
+ // On Windows, dbghelp strips leading underscores, so we accept "ZN...E"
+ // form too.
+ inner = &s[2..s.len() - 1];
+ } else if s.len() > 5 && s.starts_with("__ZN") && s.ends_with('E') {
+ // On OSX, symbols are prefixed with an extra _
+ inner = &s[4..s.len() - 1];
+ } else {
+ valid = false;
+ }
+
+ // only work with ascii text
+ if inner.bytes().any(|c| c & 0x80 != 0) {
+ valid = false;
+ }
+
+ let mut elements = 0;
+ if valid {
+ let mut chars = inner.chars().peekable();
+ while valid {
+ let mut i = 0usize;
+ while let Some(&c) = chars.peek() {
+ if !c.is_digit(10) {
+ break
+ }
+ chars.next();
+ let next = i.checked_mul(10)
+ .and_then(|i| i.checked_add(c as usize - '0' as usize));
+ i = match next {
+ Some(i) => i,
+ None => {
+ valid = false;
+ break
+ }
+ };
+ }
+
+ if i == 0 {
+ valid = chars.next().is_none();
+ break;
+ } else if chars.by_ref().take(i).count() != i {
+ valid = false;
+ } else {
+ elements += 1;
+ }
+ }
+ }
+
+ Demangle {
+ inner: inner,
+ valid: valid,
+ elements: elements,
+ original: s,
+ }
+}
+
+/// Error returned from the `try_demangle` function below when demangling fails.
+#[derive(Debug, Clone)]
+pub struct TryDemangleError {
+ _priv: (),
+}
+
+/// The same as `demangle`, except return an `Err` if the string does not appear
+/// to be a Rust symbol, rather than "demangling" the given string as a no-op.
+///
+/// ```
+/// extern crate rustc_demangle;
+///
+/// let not_a_rust_symbol = "la la la";
+///
+/// // The `try_demangle` function will reject strings which are not Rust symbols.
+/// assert!(rustc_demangle::try_demangle(not_a_rust_symbol).is_err());
+///
+/// // While `demangle` will just pass the non-symbol through as a no-op.
+/// assert_eq!(rustc_demangle::demangle(not_a_rust_symbol).as_str(), not_a_rust_symbol);
+/// ```
+pub fn try_demangle(s: &str) -> Result<Demangle, TryDemangleError> {
+ let sym = demangle(s);
+ if sym.valid {
+ Ok(sym)
+ } else {
+ Err(TryDemangleError { _priv: () })
+ }
+}
+
+impl<'a> Demangle<'a> {
+ /// Returns the underlying string that's being demangled.
+ pub fn as_str(&self) -> &'a str {
+ self.original
+ }
+}
+
+// Rust hashes are hex digits with an `h` prepended.
+fn is_rust_hash(s: &str) -> bool {
+ s.starts_with('h') && s[1..].chars().all(|c| c.is_digit(16))
+}
+
+impl<'a> fmt::Display for Demangle<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ // Alright, let's do this.
+ if !self.valid {
+ return f.write_str(self.inner);
+ }
+
+ let mut inner = self.inner;
+ for element in 0..self.elements {
+ let mut rest = inner;
+ while rest.chars().next().unwrap().is_digit(10) {
+ rest = &rest[1..];
+ }
+ let i: usize = inner[..(inner.len() - rest.len())].parse().unwrap();
+ inner = &rest[i..];
+ rest = &rest[..i];
+ // Skip printing the hash if alternate formatting
+ // was requested.
+ if f.alternate() && element+1 == self.elements && is_rust_hash(&rest) {
+ break;
+ }
+ if element != 0 {
+ try!(f.write_str("::"));
+ }
+ if rest.starts_with("_$") {
+ rest = &rest[1..];
+ }
+ while !rest.is_empty() {
+ if rest.starts_with('.') {
+ if let Some('.') = rest[1..].chars().next() {
+ try!(f.write_str("::"));
+ rest = &rest[2..];
+ } else {
+ try!(f.write_str("."));
+ rest = &rest[1..];
+ }
+ } else if rest.starts_with('$') {
+ macro_rules! demangle {
+ ($($pat:expr => $demangled:expr),*) => ({
+ $(if rest.starts_with($pat) {
+ try!(f.write_str($demangled));
+ rest = &rest[$pat.len()..];
+ } else)*
+ {
+ try!(f.write_str(rest));
+ break;
+ }
+
+ })
+ }
+
+ // see src/librustc/back/link.rs for these mappings
+ demangle! {
+ "$SP$" => "@",
+ "$BP$" => "*",
+ "$RF$" => "&",
+ "$LT$" => "<",
+ "$GT$" => ">",
+ "$LP$" => "(",
+ "$RP$" => ")",
+ "$C$" => ",",
+
+ // in theory we can demangle any Unicode code point, but
+ // for simplicity we just catch the common ones.
+ "$u7e$" => "~",
+ "$u20$" => " ",
+ "$u27$" => "'",
+ "$u5b$" => "[",
+ "$u5d$" => "]",
+ "$u7b$" => "{",
+ "$u7d$" => "}",
+ "$u3b$" => ";",
+ "$u2b$" => "+",
+ "$u22$" => "\""
+ }
+ } else {
+ let idx = match rest.char_indices().find(|&(_, c)| c == '$' || c == '.') {
+ None => rest.len(),
+ Some((i, _)) => i,
+ };
+ try!(f.write_str(&rest[..idx]));
+ rest = &rest[idx..];
+ }
+ }
+ }
+
+ Ok(())
+ }
+}
+
+impl<'a> fmt::Debug for Demangle<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(self, f)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use std::prelude::v1::*;
+
+ macro_rules! t {
+ ($a:expr, $b:expr) => ({
+ assert_eq!(super::demangle($a).to_string(), $b);
+ })
+ }
+
+ macro_rules! t_nohash {
+ ($a:expr, $b:expr) => ({
+ assert_eq!(format!("{:#}", super::demangle($a)), $b);
+ })
+ }
+
+ #[test]
+ fn demangle() {
+ t!("test", "test");
+ t!("_ZN4testE", "test");
+ t!("_ZN4test", "_ZN4test");
+ t!("_ZN4test1a2bcE", "test::a::bc");
+ }
+
+ #[test]
+ fn demangle_dollars() {
+ t!("_ZN4$RP$E", ")");
+ t!("_ZN8$RF$testE", "&test");
+ t!("_ZN8$BP$test4foobE", "*test::foob");
+ t!("_ZN9$u20$test4foobE", " test::foob");
+ t!("_ZN35Bar$LT$$u5b$u32$u3b$$u20$4$u5d$$GT$E", "Bar<[u32; 4]>");
+ }
+
+ #[test]
+ fn demangle_many_dollars() {
+ t!("_ZN13test$u20$test4foobE", "test test::foob");
+ t!("_ZN12test$BP$test4foobE", "test*test::foob");
+ }
+
+
+ #[test]
+ fn demangle_osx() {
+ t!("__ZN5alloc9allocator6Layout9for_value17h02a996811f781011E", "alloc::allocator::Layout::for_value::h02a996811f781011");
+ t!("__ZN38_$LT$core..option..Option$LT$T$GT$$GT$6unwrap18_MSG_FILE_LINE_COL17haf7cb8d5824ee659E", "<core::option::Option<T>>::unwrap::_MSG_FILE_LINE_COL::haf7cb8d5824ee659");
+ t!("__ZN4core5slice89_$LT$impl$u20$core..iter..traits..IntoIterator$u20$for$u20$$RF$$u27$a$u20$$u5b$T$u5d$$GT$9into_iter17h450e234d27262170E", "core::slice::<impl core::iter::traits::IntoIterator for &'a [T]>::into_iter::h450e234d27262170");
+ }
+
+ #[test]
+ fn demangle_windows() {
+ t!("ZN4testE", "test");
+ t!("ZN13test$u20$test4foobE", "test test::foob");
+ t!("ZN12test$RF$test4foobE", "test&test::foob");
+ }
+
+ #[test]
+ fn demangle_elements_beginning_with_underscore() {
+ t!("_ZN13_$LT$test$GT$E", "<test>");
+ t!("_ZN28_$u7b$$u7b$closure$u7d$$u7d$E", "{{closure}}");
+ t!("_ZN15__STATIC_FMTSTRE", "__STATIC_FMTSTR");
+ }
+
+ #[test]
+ fn demangle_trait_impls() {
+ t!("_ZN71_$LT$Test$u20$$u2b$$u20$$u27$static$u20$as$u20$foo..Bar$LT$Test$GT$$GT$3barE",
+ "<Test + 'static as foo::Bar<Test>>::bar");
+ }
+
+ #[test]
+ fn demangle_without_hash() {
+ let s = "_ZN3foo17h05af221e174051e9E";
+ t!(s, "foo::h05af221e174051e9");
+ t_nohash!(s, "foo");
+ }
+
+ #[test]
+ fn demangle_without_hash_edgecases() {
+ // One element, no hash.
+ t_nohash!("_ZN3fooE", "foo");
+ // Two elements, no hash.
+ t_nohash!("_ZN3foo3barE", "foo::bar");
+ // Longer-than-normal hash.
+ t_nohash!("_ZN3foo20h05af221e174051e9abcE", "foo");
+ // Shorter-than-normal hash.
+ t_nohash!("_ZN3foo5h05afE", "foo");
+ // Valid hash, but not at the end.
+ t_nohash!("_ZN17h05af221e174051e93fooE", "h05af221e174051e9::foo");
+ // Not a valid hash, missing the 'h'.
+ t_nohash!("_ZN3foo16ffaf221e174051e9E", "foo::ffaf221e174051e9");
+ // Not a valid hash, has a non-hex-digit.
+ t_nohash!("_ZN3foo17hg5af221e174051e9E", "foo::hg5af221e174051e9");
+ }
+
+ #[test]
+ fn demangle_thinlto() {
+ // One element, no hash.
+ t!("_ZN3fooE.llvm.9D1C9369", "foo");
+ t!("_ZN3fooE.llvm.9D1C9369@@16", "foo");
+ t_nohash!("_ZN9backtrace3foo17hbb467fcdaea5d79bE.llvm.A5310EB9", "backtrace::foo");
+ }
+
+ #[test]
+ fn dont_panic() {
+ super::demangle("_ZN2222222222222222222222EE").to_string();
+ super::demangle("_ZN5*70527e27.ll34csaғE").to_string();
+ super::demangle("_ZN5*70527a54.ll34_$b.1E").to_string();
+ super::demangle("\
+ _ZN5~saäb4e\n\
+ 2734cOsbE\n\
+ 5usage20h)3\0\0\0\0\0\0\07e2734cOsbE\
+ ").to_string();
+ }
+}
diff --git a/third_party/rust/rustc-demangle/src/main.rs b/third_party/rust/rustc-demangle/src/main.rs
new file mode 100644
index 0000000000..8ce9c4fad7
--- /dev/null
+++ b/third_party/rust/rustc-demangle/src/main.rs
@@ -0,0 +1,4 @@
+extern crate rustc_demangle;
+
+fn main() {
+}