[llvm] 78e9491 - [Demangle][Rust] Print special namespaces
David Blaikie via llvm-commits
llvm-commits at lists.llvm.org
Sun May 9 15:46:17 PDT 2021
Author: Tomasz Miąsko
Date: 2021-05-09T15:45:57-07:00
New Revision: 78e949159d105b7947dbae973080ea343e8f9eda
URL: https://github.com/llvm/llvm-project/commit/78e949159d105b7947dbae973080ea343e8f9eda
DIFF: https://github.com/llvm/llvm-project/commit/78e949159d105b7947dbae973080ea343e8f9eda.diff
LOG: [Demangle][Rust] Print special namespaces
Reviewed By: dblaikie
Differential Revision: https://reviews.llvm.org/D101821
Added:
Modified:
llvm/include/llvm/Demangle/RustDemangle.h
llvm/lib/Demangle/RustDemangle.cpp
llvm/test/Demangle/rust.test
Removed:
################################################################################
diff --git a/llvm/include/llvm/Demangle/RustDemangle.h b/llvm/include/llvm/Demangle/RustDemangle.h
index e2286f7e5b024..9bf938d3674da 100644
--- a/llvm/include/llvm/Demangle/RustDemangle.h
+++ b/llvm/include/llvm/Demangle/RustDemangle.h
@@ -12,12 +12,14 @@
#include "llvm/Demangle/DemangleConfig.h"
#include "llvm/Demangle/StringView.h"
#include "llvm/Demangle/Utility.h"
+#include <cstdint>
namespace llvm {
namespace rust_demangle {
using llvm::itanium_demangle::OutputStream;
using llvm::itanium_demangle::StringView;
+using llvm::itanium_demangle::SwapAndRestore;
struct Identifier {
StringView Name;
@@ -52,10 +54,17 @@ class Demangler {
void demanglePath();
Identifier parseIdentifier();
- void parseOptionalBase62Number(char Tag);
+ uint64_t parseOptionalBase62Number(char Tag);
uint64_t parseBase62Number();
uint64_t parseDecimalNumber();
+ void print(char C) {
+ if (Error)
+ return;
+
+ Output += C;
+ }
+
void print(StringView S) {
if (Error)
return;
@@ -63,6 +72,13 @@ class Demangler {
Output += S;
}
+ void printDecimalNumber(uint64_t N) {
+ if (Error)
+ return;
+
+ Output << N;
+ }
+
char look() const {
if (Error || Position >= Input.size())
return 0;
diff --git a/llvm/lib/Demangle/RustDemangle.cpp b/llvm/lib/Demangle/RustDemangle.cpp
index d196d66bd78ee..213bf28948462 100644
--- a/llvm/lib/Demangle/RustDemangle.cpp
+++ b/llvm/lib/Demangle/RustDemangle.cpp
@@ -132,7 +132,7 @@ void Demangler::demanglePath() {
Error = true;
return;
}
- RecursionLevel += 1;
+ SwapAndRestore<size_t> SaveRecursionLevel(RecursionLevel, RecursionLevel + 1);
switch (consume()) {
case 'C': {
@@ -149,15 +149,31 @@ void Demangler::demanglePath() {
}
demanglePath();
- parseOptionalBase62Number('s');
+ uint64_t Disambiguator = parseOptionalBase62Number('s');
Identifier Ident = parseIdentifier();
- if (!Ident.empty()) {
- // FIXME print special namespaces:
- // * "C" closures
- // * "S" shim
- print("::");
- print(Ident.Name);
+ if (isUpper(NS)) {
+ // Special namespaces
+ print("::{");
+ if (NS == 'C')
+ print("closure");
+ else if (NS == 'S')
+ print("shim");
+ else
+ print(NS);
+ if (!Ident.empty()) {
+ print(":");
+ print(Ident.Name);
+ }
+ print('#');
+ printDecimalNumber(Disambiguator);
+ print('}');
+ } else {
+ // Implementation internal namespaces.
+ if (!Ident.empty()) {
+ print("::");
+ print(Ident.Name);
+ }
}
break;
}
@@ -166,8 +182,6 @@ void Demangler::demanglePath() {
Error = true;
break;
}
-
- RecursionLevel -= 1;
}
// <undisambiguated-identifier> = ["u"] <decimal-number> ["_"] <bytes>
@@ -195,11 +209,16 @@ Identifier Demangler::parseIdentifier() {
}
// Parses optional base 62 number. The presence of a number is determined using
-// Tag.
-void Demangler::parseOptionalBase62Number(char Tag) {
- // Parsing result is currently unused.
- if (consumeIf(Tag))
- parseBase62Number();
+// Tag. Returns 0 when tag is absent and parsed value + 1 otherwise.
+uint64_t Demangler::parseOptionalBase62Number(char Tag) {
+ if (!consumeIf(Tag))
+ return 0;
+
+ uint64_t N = parseBase62Number();
+ if (Error || !addAssign(N, 1))
+ return 0;
+
+ return N;
}
// Parses base 62 number with <0-9a-zA-Z> as digits. Number is terminated by
diff --git a/llvm/test/Demangle/rust.test b/llvm/test/Demangle/rust.test
index 1be0692c63618..7e3e543e8aa95 100644
--- a/llvm/test/Demangle/rust.test
+++ b/llvm/test/Demangle/rust.test
@@ -9,6 +9,30 @@ CHECK: hello::rust
CHECK: a::b::c
_RNvNvC1a1b1c
+; Closure namespace
+
+CHECK: crate::{closure#0}
+ _RNCC5crate0
+
+CHECK: crate::{closure#1}
+ _RNCC5crates_0
+
+CHECK: crate::{closure:foo#0}
+ _RNCC5crate3foo
+
+CHECK: crate::{closure:foo#1}
+ _RNCC5crates_3foo
+
+; Shim namespace
+
+CHECK: crate::{shim:reify#0}
+ _RNSC5crate5reify
+
+; Unrecognized special namespace
+
+CHECK: crate::{Z:ident#10}
+ _RNZC5crates8_5ident
+
; Invalid mangled characters
CHECK: _RNvC2a.1c
@@ -17,6 +41,11 @@ CHECK: _RNvC2a.1c
CHECK: _RNvC2a$1c
_RNvC2a$1c
+; Invalid namespace (not in [a-zA-Z]).
+
+CHECK: _RN_C5crate4main
+ _RN_C5crate4main
+
; Invalid identifier length (UINT64_MAX + 3, which happens to be ok after a wraparound).
CHECK: _RNvC2ab18446744073709551618xy
More information about the llvm-commits
mailing list