================
@@ -0,0 +1,143 @@
+//===-- MsvcStl.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 "MsvcStl.h"
+
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/FormatEntity.h"
+#include "lldb/DataFormatters/StringPrinter.h"
+#include "lldb/DataFormatters/TypeSummary.h"
+#include "lldb/Utility/ConstString.h"
+#include "lldb/Utility/Status.h"
+#include "lldb/Utility/Stream.h"
+#include "lldb/ValueObject/ValueObject.h"
+
+#include "Plugins/Language/CPlusPlus/CxxStringTypes.h"
+
+#include "lldb/lldb-forward.h"
+#include <optional>
+#include <tuple>
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::formatters;
+
+using StringElementType = StringPrinter::StringElementType;
+
+static ValueObjectSP ExtractMsvcStlStringData(ValueObject &valobj) {
+  return valobj.GetChildAtNamePath({"_Mypair", "_Myval2"});
+}
+
+/// Determine the size in bytes of \p valobj (a MSVC STL std::string object) 
and
+/// extract its data payload. Return the size + payload pair.
+static std::optional<std::pair<uint64_t, ValueObjectSP>>
+ExtractMsvcStlStringInfo(ValueObject &valobj, uint64_t element_size) {
+  ValueObjectSP valobj_pair_sp = ExtractMsvcStlStringData(valobj);
+  if (!valobj_pair_sp || !valobj_pair_sp->GetError().Success())
+    return {};
+
+  ValueObjectSP size_sp = valobj_pair_sp->GetChildMemberWithName("_Mysize");
+  ValueObjectSP capacity_sp = valobj_pair_sp->GetChildMemberWithName("_Myres");
+  ValueObjectSP bx_sp = valobj_pair_sp->GetChildMemberWithName("_Bx");
+  if (!size_sp || !capacity_sp || !bx_sp)
+    return {};
+
+  bool success = false;
+  uint64_t size = size_sp->GetValueAsUnsigned(0, &success);
+  if (!success)
+    return {};
+  uint64_t capacity = capacity_sp->GetValueAsUnsigned(0, &success);
+  if (!success)
+    return {};
+
+  size_t bufSize = std::max<size_t>(16 / element_size, 1);
+  bool isShortString = capacity < bufSize;
+
+  if (isShortString) {
+    ValueObjectSP buf_sp = bx_sp->GetChildMemberWithName("_Buf");
+    if (buf_sp)
+      return std::make_pair(size, buf_sp);
+    return {};
+  }
+  ValueObjectSP ptr_sp = bx_sp->GetChildMemberWithName("_Ptr");
+  if (ptr_sp)
+    return std::make_pair(size, ptr_sp);
+  return {};
+}
+
+template <StringPrinter::StringElementType element_type>
+static bool
+MsvcStlStringSummaryProviderImpl(ValueObject &valobj, Stream &stream,
+                                 const TypeSummaryOptions &summary_options,
+                                 std::string prefix_token) {
+  auto string_info = ExtractMsvcStlStringInfo(
+      valobj, StringPrinter::ElementByteSize<element_type>());
+  if (!string_info)
+    return false;
+  uint64_t size;
+  ValueObjectSP location_sp;
+  std::tie(size, location_sp) = *string_info;
----------------
Michael137 wrote:

```suggestion
  auto [size, location_sp] = *string_info;
```

https://github.com/llvm/llvm-project/pull/143177
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to