[llvm] 1066e39 - [demangler] Add StringView conversion operator
Nathan Sidwell via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 28 11:20:14 PDT 2022
Author: Nathan Sidwell
Date: 2022-03-28T11:19:55-07:00
New Revision: 1066e397fa907629f0da370f9721821c838ed30a
URL: https://github.com/llvm/llvm-project/commit/1066e397fa907629f0da370f9721821c838ed30a
DIFF: https://github.com/llvm/llvm-project/commit/1066e397fa907629f0da370f9721821c838ed30a.diff
LOG: [demangler] Add StringView conversion operator
The OutputBuffer class tries to present a NUL-terminated string API to
consumers. But several of them would prefer a StringView. In
particular the Microsoft demangler, juggles between NUL-terminated and
StringView, which is confusing.
This adds a StringView conversion, and adjusts the Demanglers that can
benefit from that.
Reviewed By: dblaikie
Differential Revision: https://reviews.llvm.org/D120990
Added:
Modified:
libcxxabi/src/demangle/Utility.h
llvm/include/llvm/Demangle/Utility.h
llvm/lib/Demangle/MicrosoftDemangle.cpp
llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
llvm/unittests/Demangle/ItaniumDemangleTest.cpp
llvm/unittests/Demangle/OutputBufferTest.cpp
Removed:
################################################################################
diff --git a/libcxxabi/src/demangle/Utility.h b/libcxxabi/src/demangle/Utility.h
index 76330851d21b2..1390f14b10d45 100644
--- a/libcxxabi/src/demangle/Utility.h
+++ b/libcxxabi/src/demangle/Utility.h
@@ -75,6 +75,8 @@ class OutputBuffer {
OutputBuffer(const OutputBuffer &) = delete;
OutputBuffer &operator=(const OutputBuffer &) = delete;
+ operator StringView() const { return StringView(Buffer, CurrentPosition); }
+
void reset(char *Buffer_, size_t BufferCapacity_) {
CurrentPosition = 0;
Buffer = Buffer_;
diff --git a/llvm/include/llvm/Demangle/Utility.h b/llvm/include/llvm/Demangle/Utility.h
index 844b63651d578..3ea92c9059162 100644
--- a/llvm/include/llvm/Demangle/Utility.h
+++ b/llvm/include/llvm/Demangle/Utility.h
@@ -75,6 +75,8 @@ class OutputBuffer {
OutputBuffer(const OutputBuffer &) = delete;
OutputBuffer &operator=(const OutputBuffer &) = delete;
+ operator StringView() const { return StringView(Buffer, CurrentPosition); }
+
void reset(char *Buffer_, size_t BufferCapacity_) {
CurrentPosition = 0;
Buffer = Buffer_;
diff --git a/llvm/lib/Demangle/MicrosoftDemangle.cpp b/llvm/lib/Demangle/MicrosoftDemangle.cpp
index a009befdb3715..aca8cf7a0a58c 100644
--- a/llvm/lib/Demangle/MicrosoftDemangle.cpp
+++ b/llvm/lib/Demangle/MicrosoftDemangle.cpp
@@ -970,12 +970,9 @@ void Demangler::memorizeIdentifier(IdentifierNode *Identifier) {
// FIXME: Propagate out-of-memory as an error?
std::terminate();
Identifier->output(OB, OF_Default);
- OB << '\0';
- char *Name = OB.getBuffer();
-
- StringView Owned = copyString(Name);
+ StringView Owned = copyString(OB);
memorizeString(Owned);
- std::free(Name);
+ std::free(OB.getBuffer());
}
IdentifierNode *
@@ -1279,7 +1276,6 @@ Demangler::demangleStringLiteral(StringView &MangledName) {
bool IsWcharT = false;
bool IsNegative = false;
size_t CrcEndPos = 0;
- char *ResultBuffer = nullptr;
EncodedStringLiteralNode *Result = Arena.alloc<EncodedStringLiteralNode>();
@@ -1375,10 +1371,8 @@ Demangler::demangleStringLiteral(StringView &MangledName) {
}
}
- OB << '\0';
- ResultBuffer = OB.getBuffer();
- Result->DecodedString = copyString(ResultBuffer);
- std::free(ResultBuffer);
+ Result->DecodedString = copyString(OB);
+ std::free(OB.getBuffer());
return Result;
StringLiteralError:
@@ -1455,10 +1449,9 @@ Demangler::demangleLocallyScopedNamePiece(StringView &MangledName) {
Scope->output(OB, OF_Default);
OB << '\'';
OB << "::`" << Number << "'";
- OB << '\0';
- char *Result = OB.getBuffer();
- Identifier->Name = copyString(Result);
- std::free(Result);
+
+ Identifier->Name = copyString(OB);
+ std::free(OB.getBuffer());
return Identifier;
}
@@ -2322,8 +2315,8 @@ void Demangler::dumpBackReferences() {
TypeNode *T = Backrefs.FunctionParams[I];
T->output(OB, OF_Default);
- std::printf(" [%d] - %.*s\n", (int)I, (int)OB.getCurrentPosition(),
- OB.getBuffer());
+ StringView B = OB;
+ std::printf(" [%d] - %.*s\n", (int)I, (int)B.size(), B.begin());
}
std::free(OB.getBuffer());
diff --git a/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp b/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
index d07d05a08c556..494cdabad41f3 100644
--- a/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
+++ b/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp
@@ -121,8 +121,8 @@ std::string Node::toString(OutputFlags Flags) const {
OutputBuffer OB;
initializeOutputBuffer(nullptr, nullptr, OB, 1024);
this->output(OB, Flags);
- OB << '\0';
- std::string Owned(OB.getBuffer());
+ StringView SV = OB;
+ std::string Owned(SV.begin(), SV.end());
std::free(OB.getBuffer());
return Owned;
}
diff --git a/llvm/unittests/Demangle/ItaniumDemangleTest.cpp b/llvm/unittests/Demangle/ItaniumDemangleTest.cpp
index 5a059c0a2f944..9d9f683b36925 100644
--- a/llvm/unittests/Demangle/ItaniumDemangleTest.cpp
+++ b/llvm/unittests/Demangle/ItaniumDemangleTest.cpp
@@ -53,7 +53,8 @@ TEST(ItaniumDemangle, MethodOverride) {
}
static std::string toString(OutputBuffer &OB) {
- return {OB.getBuffer(), OB.getCurrentPosition()};
+ StringView SV = OB;
+ return {SV.begin(), SV.end()};
}
TEST(ItaniumDemangle, HalfType) {
diff --git a/llvm/unittests/Demangle/OutputBufferTest.cpp b/llvm/unittests/Demangle/OutputBufferTest.cpp
index 9fc572b34ba7a..829169ba61754 100644
--- a/llvm/unittests/Demangle/OutputBufferTest.cpp
+++ b/llvm/unittests/Demangle/OutputBufferTest.cpp
@@ -15,7 +15,8 @@ using namespace llvm;
using llvm::itanium_demangle::OutputBuffer;
static std::string toString(OutputBuffer &OB) {
- return {OB.getBuffer(), OB.getCurrentPosition()};
+ StringView SV = OB;
+ return {SV.begin(), SV.end()};
}
template <typename T> static std::string printToString(const T &Value) {
More information about the llvm-commits
mailing list