| 1 | //===-- MsvcStl.cpp -------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "MsvcStl.h" |
| 10 | |
| 11 | #include "lldb/Core/Debugger.h" |
| 12 | #include "lldb/Core/FormatEntity.h" |
| 13 | #include "lldb/DataFormatters/StringPrinter.h" |
| 14 | #include "lldb/DataFormatters/TypeSummary.h" |
| 15 | #include "lldb/Utility/ConstString.h" |
| 16 | #include "lldb/Utility/Status.h" |
| 17 | #include "lldb/Utility/Stream.h" |
| 18 | #include "lldb/ValueObject/ValueObject.h" |
| 19 | |
| 20 | #include "Plugins/Language/CPlusPlus/CxxStringTypes.h" |
| 21 | |
| 22 | #include "lldb/lldb-forward.h" |
| 23 | #include <optional> |
| 24 | #include <tuple> |
| 25 | |
| 26 | using namespace lldb; |
| 27 | using namespace lldb_private; |
| 28 | using namespace lldb_private::formatters; |
| 29 | |
| 30 | using StringElementType = StringPrinter::StringElementType; |
| 31 | |
| 32 | template <StringElementType element_type> |
| 33 | static constexpr uint64_t StringElementByteSize() { |
| 34 | switch (element_type) { |
| 35 | case StringElementType::ASCII: |
| 36 | case StringElementType::UTF8: |
| 37 | return 1; |
| 38 | case StringElementType::UTF16: |
| 39 | return 2; |
| 40 | case StringElementType::UTF32: |
| 41 | return 3; |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static ValueObjectSP (ValueObject &valobj) { |
| 47 | return valobj.GetChildAtNamePath(names: {"_Mypair" , "_Myval2" }); |
| 48 | } |
| 49 | |
| 50 | /// Determine the size in bytes of \p valobj (a MSVC STL std::string object) and |
| 51 | /// extract its data payload. Return the size + payload pair. |
| 52 | static std::optional<std::pair<uint64_t, ValueObjectSP>> |
| 53 | (ValueObject &valobj, uint64_t element_size) { |
| 54 | ValueObjectSP valobj_pair_sp = ExtractMsvcStlStringData(valobj); |
| 55 | if (!valobj_pair_sp || !valobj_pair_sp->GetError().Success()) |
| 56 | return {}; |
| 57 | |
| 58 | ValueObjectSP size_sp = valobj_pair_sp->GetChildMemberWithName(name: "_Mysize" ); |
| 59 | ValueObjectSP capacity_sp = valobj_pair_sp->GetChildMemberWithName(name: "_Myres" ); |
| 60 | ValueObjectSP bx_sp = valobj_pair_sp->GetChildMemberWithName(name: "_Bx" ); |
| 61 | if (!size_sp || !capacity_sp || !bx_sp) |
| 62 | return {}; |
| 63 | |
| 64 | bool success = false; |
| 65 | uint64_t size = size_sp->GetValueAsUnsigned(fail_value: 0, success: &success); |
| 66 | if (!success) |
| 67 | return {}; |
| 68 | uint64_t capacity = capacity_sp->GetValueAsUnsigned(fail_value: 0, success: &success); |
| 69 | if (!success) |
| 70 | return {}; |
| 71 | |
| 72 | size_t bufSize = std::max<size_t>(a: 16 / element_size, b: 1); |
| 73 | bool isShortString = capacity < bufSize; |
| 74 | |
| 75 | if (isShortString) { |
| 76 | ValueObjectSP buf_sp = bx_sp->GetChildMemberWithName(name: "_Buf" ); |
| 77 | if (buf_sp) |
| 78 | return std::make_pair(x&: size, y&: buf_sp); |
| 79 | return {}; |
| 80 | } |
| 81 | ValueObjectSP ptr_sp = bx_sp->GetChildMemberWithName(name: "_Ptr" ); |
| 82 | if (ptr_sp) |
| 83 | return std::make_pair(x&: size, y&: ptr_sp); |
| 84 | return {}; |
| 85 | } |
| 86 | |
| 87 | template <StringPrinter::StringElementType element_type> |
| 88 | static bool |
| 89 | MsvcStlStringSummaryProviderImpl(ValueObject &valobj, Stream &stream, |
| 90 | const TypeSummaryOptions &summary_options, |
| 91 | std::string prefix_token) { |
| 92 | auto string_info = |
| 93 | ExtractMsvcStlStringInfo(valobj, StringElementByteSize<element_type>()); |
| 94 | if (!string_info) |
| 95 | return false; |
| 96 | auto [size, location_sp] = *string_info; |
| 97 | |
| 98 | return StringBufferSummaryProvider<element_type>( |
| 99 | stream, summary_options, location_sp, size, prefix_token); |
| 100 | } |
| 101 | template <StringPrinter::StringElementType element_type> |
| 102 | static bool formatStringImpl(ValueObject &valobj, Stream &stream, |
| 103 | const TypeSummaryOptions &summary_options, |
| 104 | std::string prefix_token) { |
| 105 | StreamString scratch_stream; |
| 106 | const bool success = MsvcStlStringSummaryProviderImpl<element_type>( |
| 107 | valobj, scratch_stream, summary_options, prefix_token); |
| 108 | if (success) |
| 109 | stream << scratch_stream.GetData(); |
| 110 | else |
| 111 | stream << "Summary Unavailable" ; |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | bool lldb_private::formatters::IsMsvcStlStringType(ValueObject &valobj) { |
| 116 | std::vector<uint32_t> indexes; |
| 117 | return valobj.GetCompilerType().GetIndexOfChildMemberWithName(name: "_Mypair" , omit_empty_base_classes: true, |
| 118 | child_indexes&: indexes) > 0; |
| 119 | } |
| 120 | |
| 121 | bool lldb_private::formatters::MsvcStlWStringSummaryProvider( |
| 122 | ValueObject &valobj, Stream &stream, |
| 123 | const TypeSummaryOptions &summary_options) { |
| 124 | return formatStringImpl<StringElementType::UTF16>(valobj, stream, |
| 125 | summary_options, prefix_token: "L" ); |
| 126 | } |
| 127 | |
| 128 | template <> |
| 129 | bool lldb_private::formatters::MsvcStlStringSummaryProvider< |
| 130 | StringElementType::ASCII>(ValueObject &valobj, Stream &stream, |
| 131 | const TypeSummaryOptions &summary_options) { |
| 132 | return MsvcStlStringSummaryProviderImpl<StringElementType::ASCII>( |
| 133 | valobj, stream, summary_options, prefix_token: "" ); |
| 134 | } |
| 135 | template <> |
| 136 | bool lldb_private::formatters::MsvcStlStringSummaryProvider< |
| 137 | StringElementType::UTF8>(ValueObject &valobj, Stream &stream, |
| 138 | const TypeSummaryOptions &summary_options) { |
| 139 | return MsvcStlStringSummaryProviderImpl<StringElementType::UTF8>( |
| 140 | valobj, stream, summary_options, prefix_token: "u8" ); |
| 141 | } |
| 142 | template <> |
| 143 | bool lldb_private::formatters::MsvcStlStringSummaryProvider< |
| 144 | StringElementType::UTF16>(ValueObject &valobj, Stream &stream, |
| 145 | const TypeSummaryOptions &summary_options) { |
| 146 | return MsvcStlStringSummaryProviderImpl<StringElementType::UTF16>( |
| 147 | valobj, stream, summary_options, prefix_token: "u" ); |
| 148 | } |
| 149 | template <> |
| 150 | bool lldb_private::formatters::MsvcStlStringSummaryProvider< |
| 151 | StringElementType::UTF32>(ValueObject &valobj, Stream &stream, |
| 152 | const TypeSummaryOptions &summary_options) { |
| 153 | return MsvcStlStringSummaryProviderImpl<StringElementType::UTF32>( |
| 154 | valobj, stream, summary_options, prefix_token: "U" ); |
| 155 | } |
| 156 | |