diff --git a/libcxxabi/src/demangle/Utility.h b/libcxxabi/src/demangle/Utility.h --- a/libcxxabi/src/demangle/Utility.h +++ b/libcxxabi/src/demangle/Utility.h @@ -75,6 +75,8 @@ 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 --- a/llvm/include/llvm/Demangle/Utility.h +++ b/llvm/include/llvm/Demangle/Utility.h @@ -75,6 +75,8 @@ 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 --- a/llvm/lib/Demangle/MicrosoftDemangle.cpp +++ b/llvm/lib/Demangle/MicrosoftDemangle.cpp @@ -970,12 +970,9 @@ // 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 @@ bool IsWcharT = false; bool IsNegative = false; size_t CrcEndPos = 0; - char *ResultBuffer = nullptr; EncodedStringLiteralNode *Result = Arena.alloc(); @@ -1375,10 +1371,8 @@ } } - 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 @@ 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 @@ 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 --- a/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp +++ b/llvm/lib/Demangle/MicrosoftDemangleNodes.cpp @@ -121,8 +121,8 @@ 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 --- a/llvm/unittests/Demangle/ItaniumDemangleTest.cpp +++ b/llvm/unittests/Demangle/ItaniumDemangleTest.cpp @@ -53,7 +53,8 @@ } 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 --- a/llvm/unittests/Demangle/OutputBufferTest.cpp +++ b/llvm/unittests/Demangle/OutputBufferTest.cpp @@ -15,7 +15,8 @@ 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 static std::string printToString(const T &Value) {