================ @@ -0,0 +1,131 @@ +//===-- GenericStringView.cpp --------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===---------------------------------------------------------------------===// + +#include "Generic.h" +#include "LibCxx.h" + +#include "lldb/ValueObject/ValueObject.h" +#include "lldb/lldb-forward.h" + +#include "Plugins/Language/CPlusPlus/CxxStringTypes.h" +#include <tuple> + +using namespace lldb; +using namespace lldb_private; +using namespace lldb_private::formatters; + +using StringElementType = StringPrinter::StringElementType; + +enum class StlType { + LibCxx, + LibStdcpp, + MsvcStl, +}; + +std::tuple<StlType, lldb::ValueObjectSP, lldb::ValueObjectSP> +extractStringViewData(ValueObject &valobj) { + auto data_sp = valobj.GetChildMemberWithName("_Mydata"); + if (data_sp) + return std::make_tuple(StlType::MsvcStl, data_sp, + valobj.GetChildMemberWithName("_Mysize")); + data_sp = valobj.GetChildMemberWithName("_M_str"); + if (data_sp) + return std::make_tuple(StlType::LibStdcpp, data_sp, + valobj.GetChildMemberWithName("_M_len")); + return std::make_tuple( + StlType::LibCxx, + GetChildMemberWithName(valobj, + {ConstString("__data_"), ConstString("__data")}), + GetChildMemberWithName(valobj, + {ConstString("__size_"), ConstString("__size")})); +} + +template <StringPrinter::StringElementType element_type> +static bool formatStringViewImpl(lldb::ValueObjectSP data_sp, + lldb::ValueObjectSP size_sp, Stream &stream, + const TypeSummaryOptions &summary_options, + std::string prefix_token) { + if (!data_sp || !size_sp) + return false; + + bool success = false; + uint64_t size = size_sp->GetValueAsUnsigned(0, &success); + if (!success) { + stream << "Summary Unavailable"; + return true; + } + + StreamString scratch_stream; + success = StringBufferSummaryProvider<element_type>( + scratch_stream, summary_options, data_sp, size, prefix_token); + + if (success) + stream << scratch_stream.GetData(); + else + stream << "Summary Unavailable"; + return true; +} + +template <StringElementType element_type> +static constexpr const char *getPrefixToken() { + if constexpr (element_type == StringElementType::ASCII) + return ""; + if constexpr (element_type == StringElementType::UTF8) + return "u8"; + if constexpr (element_type == StringElementType::UTF16) + return "u"; + if constexpr (element_type == StringElementType::UTF32) + return "U"; + llvm_unreachable("invalid element type"); +} + +template <StringPrinter::StringElementType element_type> +bool lldb_private::formatters::GenericStringViewSummaryProvider( + ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { + auto [_, data_sp, size_sp] = extractStringViewData(valobj); + return formatStringViewImpl<element_type>(data_sp, size_sp, stream, options, + getPrefixToken<element_type>()); +} + +template bool lldb_private::formatters::GenericStringViewSummaryProvider< + StringElementType::ASCII>(ValueObject &, Stream &, + const TypeSummaryOptions &); +template bool lldb_private::formatters::GenericStringViewSummaryProvider< + StringElementType::UTF8>(ValueObject &, Stream &, + const TypeSummaryOptions &); +template bool lldb_private::formatters::GenericStringViewSummaryProvider< + StringElementType::UTF16>(ValueObject &, Stream &, + const TypeSummaryOptions &); +template bool lldb_private::formatters::GenericStringViewSummaryProvider< + StringElementType::UTF32>(ValueObject &, Stream &, + const TypeSummaryOptions &); + +bool lldb_private::formatters::GenericWStringViewSummaryProvider( + ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) { + auto [stl, data_sp, size_sp] = extractStringViewData(valobj); + if (stl == StlType::LibStdcpp) + return formatStringViewImpl<StringElementType::UTF32>(data_sp, size_sp, + stream, options, "L"); ---------------- Nerixyz wrote:
For MSVC's STL, we should use utf16, but it shouldn't hurt to get the size there as well. https://github.com/llvm/llvm-project/pull/171854 _______________________________________________ lldb-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
