mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-04-02 05:27:41 -05:00
impr: General code cleanup
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/Demangle/Demangle.h"
|
||||
#include "llvm/Demangle/StringView.h"
|
||||
#include "llvm/Demangle/StringViewExtras.h"
|
||||
#include "llvm/Demangle/Utility.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -20,17 +20,18 @@
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <string_view>
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
using llvm::itanium_demangle::OutputBuffer;
|
||||
using llvm::itanium_demangle::ScopedOverride;
|
||||
using llvm::itanium_demangle::StringView;
|
||||
using llvm::itanium_demangle::starts_with;
|
||||
|
||||
namespace {
|
||||
|
||||
struct Identifier {
|
||||
StringView Name;
|
||||
std::string_view Name;
|
||||
bool Punycode;
|
||||
|
||||
bool empty() const { return Name.empty(); }
|
||||
@@ -77,7 +78,7 @@ class Demangler {
|
||||
size_t RecursionLevel;
|
||||
size_t BoundLifetimes;
|
||||
// Input string that is being demangled with "_R" prefix removed.
|
||||
StringView Input;
|
||||
std::string_view Input;
|
||||
// Position in the input string.
|
||||
size_t Position;
|
||||
// When true, print methods append the output to the stream.
|
||||
@@ -92,7 +93,7 @@ public:
|
||||
|
||||
Demangler(size_t MaxRecursionLevel = 500);
|
||||
|
||||
bool demangle(StringView MangledName);
|
||||
bool demangle(std::string_view MangledName);
|
||||
|
||||
private:
|
||||
bool demanglePath(IsInType Type,
|
||||
@@ -128,10 +129,10 @@ private:
|
||||
uint64_t parseOptionalBase62Number(char Tag);
|
||||
uint64_t parseBase62Number();
|
||||
uint64_t parseDecimalNumber();
|
||||
uint64_t parseHexNumber(StringView &HexDigits);
|
||||
uint64_t parseHexNumber(std::string_view &HexDigits);
|
||||
|
||||
void print(char C);
|
||||
void print(StringView S);
|
||||
void print(std::string_view S);
|
||||
void printDecimalNumber(uint64_t N);
|
||||
void printBasicType(BasicType);
|
||||
void printLifetime(uint64_t Index);
|
||||
@@ -147,20 +148,13 @@ private:
|
||||
|
||||
} // namespace
|
||||
|
||||
char *llvm::rustDemangle(const char *MangledName) {
|
||||
if (MangledName == nullptr)
|
||||
return nullptr;
|
||||
|
||||
char *llvm::rustDemangle(std::string_view MangledName) {
|
||||
// Return early if mangled name doesn't look like a Rust symbol.
|
||||
StringView Mangled(MangledName);
|
||||
if (!Mangled.startsWith("_R"))
|
||||
if (MangledName.empty() || !starts_with(MangledName, "_R"))
|
||||
return nullptr;
|
||||
|
||||
Demangler D;
|
||||
if (!initializeOutputBuffer(nullptr, nullptr, D.Output, 1024))
|
||||
return nullptr;
|
||||
|
||||
if (!D.demangle(Mangled)) {
|
||||
if (!D.demangle(MangledName)) {
|
||||
std::free(D.Output.getBuffer());
|
||||
return nullptr;
|
||||
}
|
||||
@@ -193,20 +187,20 @@ static inline bool isValid(const char C) {
|
||||
// responsibility of the caller to free the memory behind the output stream.
|
||||
//
|
||||
// <symbol-name> = "_R" <path> [<instantiating-crate>]
|
||||
bool Demangler::demangle(StringView Mangled) {
|
||||
bool Demangler::demangle(std::string_view Mangled) {
|
||||
Position = 0;
|
||||
Error = false;
|
||||
Print = true;
|
||||
RecursionLevel = 0;
|
||||
BoundLifetimes = 0;
|
||||
|
||||
if (!Mangled.consumeFront("_R")) {
|
||||
if (!starts_with(Mangled, "_R")) {
|
||||
Error = true;
|
||||
return false;
|
||||
}
|
||||
Mangled.remove_prefix(2);
|
||||
size_t Dot = Mangled.find('.');
|
||||
Input = Mangled.substr(0, Dot);
|
||||
StringView Suffix = Mangled.dropFront(Dot);
|
||||
Input = Dot == std::string_view::npos ? Mangled : Mangled.substr(0, Dot);
|
||||
|
||||
demanglePath(IsInType::No);
|
||||
|
||||
@@ -218,9 +212,9 @@ bool Demangler::demangle(StringView Mangled) {
|
||||
if (Position != Input.size())
|
||||
Error = true;
|
||||
|
||||
if (!Suffix.empty()) {
|
||||
if (Dot != std::string_view::npos) {
|
||||
print(" (");
|
||||
print(Suffix);
|
||||
print(Mangled.substr(Dot));
|
||||
print(")");
|
||||
}
|
||||
|
||||
@@ -778,7 +772,7 @@ void Demangler::demangleConstInt() {
|
||||
if (consumeIf('n'))
|
||||
print('-');
|
||||
|
||||
StringView HexDigits;
|
||||
std::string_view HexDigits;
|
||||
uint64_t Value = parseHexNumber(HexDigits);
|
||||
if (HexDigits.size() <= 16) {
|
||||
printDecimalNumber(Value);
|
||||
@@ -791,7 +785,7 @@ void Demangler::demangleConstInt() {
|
||||
// <const-data> = "0_" // false
|
||||
// | "1_" // true
|
||||
void Demangler::demangleConstBool() {
|
||||
StringView HexDigits;
|
||||
std::string_view HexDigits;
|
||||
parseHexNumber(HexDigits);
|
||||
if (HexDigits == "0")
|
||||
print("false");
|
||||
@@ -808,7 +802,7 @@ static bool isAsciiPrintable(uint64_t CodePoint) {
|
||||
|
||||
// <const-data> = <hex-number>
|
||||
void Demangler::demangleConstChar() {
|
||||
StringView HexDigits;
|
||||
std::string_view HexDigits;
|
||||
uint64_t CodePoint = parseHexNumber(HexDigits);
|
||||
if (Error || HexDigits.size() > 6) {
|
||||
Error = true;
|
||||
@@ -862,7 +856,7 @@ Identifier Demangler::parseIdentifier() {
|
||||
Error = true;
|
||||
return {};
|
||||
}
|
||||
StringView S = Input.substr(Position, Bytes);
|
||||
std::string_view S = Input.substr(Position, Bytes);
|
||||
Position += Bytes;
|
||||
|
||||
if (!std::all_of(S.begin(), S.end(), isValid)) {
|
||||
@@ -970,7 +964,7 @@ uint64_t Demangler::parseDecimalNumber() {
|
||||
//
|
||||
// <hex-number> = "0_"
|
||||
// | <1-9a-f> {<0-9a-f>} "_"
|
||||
uint64_t Demangler::parseHexNumber(StringView &HexDigits) {
|
||||
uint64_t Demangler::parseHexNumber(std::string_view &HexDigits) {
|
||||
size_t Start = Position;
|
||||
uint64_t Value = 0;
|
||||
|
||||
@@ -994,7 +988,7 @@ uint64_t Demangler::parseHexNumber(StringView &HexDigits) {
|
||||
}
|
||||
|
||||
if (Error) {
|
||||
HexDigits = StringView();
|
||||
HexDigits = std::string_view();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1011,7 +1005,7 @@ void Demangler::print(char C) {
|
||||
Output += C;
|
||||
}
|
||||
|
||||
void Demangler::print(StringView S) {
|
||||
void Demangler::print(std::string_view S) {
|
||||
if (Error || !Print)
|
||||
return;
|
||||
|
||||
@@ -1108,17 +1102,17 @@ static inline bool encodeUTF8(size_t CodePoint, char *Output) {
|
||||
|
||||
// Decodes string encoded using punycode and appends results to Output.
|
||||
// Returns true if decoding was successful.
|
||||
static bool decodePunycode(StringView Input, OutputBuffer &Output) {
|
||||
static bool decodePunycode(std::string_view Input, OutputBuffer &Output) {
|
||||
size_t OutputSize = Output.getCurrentPosition();
|
||||
size_t InputIdx = 0;
|
||||
|
||||
// Rust uses an underscore as a delimiter.
|
||||
size_t DelimiterPos = StringView::npos;
|
||||
size_t DelimiterPos = std::string_view::npos;
|
||||
for (size_t I = 0; I != Input.size(); ++I)
|
||||
if (Input[I] == '_')
|
||||
DelimiterPos = I;
|
||||
|
||||
if (DelimiterPos != StringView::npos) {
|
||||
if (DelimiterPos != std::string_view::npos) {
|
||||
// Copy basic code points before the last delimiter to the output.
|
||||
for (; InputIdx != DelimiterPos; ++InputIdx) {
|
||||
char C = Input[InputIdx];
|
||||
@@ -1126,7 +1120,7 @@ static bool decodePunycode(StringView Input, OutputBuffer &Output) {
|
||||
return false;
|
||||
// Code points are padded with zeros while decoding is in progress.
|
||||
char UTF8[4] = {C};
|
||||
Output += StringView(UTF8, UTF8 + 4);
|
||||
Output += std::string_view(UTF8, 4);
|
||||
}
|
||||
// Skip over the delimiter.
|
||||
++InputIdx;
|
||||
|
||||
Reference in New Issue
Block a user