[Lldb-commits] [lldb] [lldb] Add a gtest matcher for lldb_private::Value (PR #167427)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 12 11:25:39 PST 2025


================
@@ -0,0 +1,202 @@
+//===-- ValueMatcher.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 "ValueMatcher.h"
+#include "llvm/Support/Format.h"
+#include "llvm/Support/InterleavedRange.h"
+#include "llvm/Support/raw_os_ostream.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace lldb_private;
+
+static void FormatValueDetails(llvm::raw_ostream &os,
+                               Value::ValueType value_type,
+                               Value::ContextType context_type,
+                               const Scalar &scalar,
+                               llvm::ArrayRef<uint8_t> buffer_data) {
+  os << "Value(";
+  os << "value_type=" << Value::GetValueTypeAsCString(value_type);
+  os << ", context_type=" << Value::GetContextTypeAsCString(context_type);
+
+  if (value_type == Value::ValueType::HostAddress) {
+    auto bytes_to_print = buffer_data.take_front(16);
+    os << ", buffer=[";
+    llvm::interleave(
+        bytes_to_print,
+        [&](uint8_t byte) {
+          os << llvm::format("%02x", static_cast<unsigned>(byte));
+        },
+        [&]() { os << " "; });
+    if (buffer_data.size() > 16)
+      os << " ...";
+    os << "] (" << buffer_data.size() << " bytes)";
+  } else {
+    os << ", value=" << scalar;
+  }
+  os << ")";
+}
+
+void lldb_private::PrintTo(const Value &val, std::ostream *os) {
+  if (!os)
+    return;
+
+  llvm::raw_os_ostream raw_os(*os);
+  FormatValueDetails(raw_os, val.GetValueType(), val.GetContextType(),
+                     val.GetScalar(), val.GetBuffer().GetData());
+}
+
+bool ValueMatcher::MatchAndExplain(
+    const Value &val, testing::MatchResultListener *listener) const {
+  if (listener && listener->stream()) {
+    llvm::raw_os_ostream os(*listener->stream());
+    return MatchAndExplainImpl(val, os);
+  }
+
+  llvm::raw_null_ostream os;
+  return MatchAndExplainImpl(val, os);
+}
+
+bool ValueMatcher::MatchAndExplainImpl(const Value &val,
+                                       llvm::raw_ostream &os) const {
+  if (val.GetValueType() != m_value_type) {
+    os << "value_type mismatch: expected "
+       << Value::GetValueTypeAsCString(m_value_type) << ", got "
+       << Value::GetValueTypeAsCString(val.GetValueType()) << " ";
+    return false;
+  }
+
+  if (val.GetContextType() != m_context_type) {
+    os << "context_type mismatch: expected "
+       << Value::GetContextTypeAsCString(m_context_type) << ", got "
+       << Value::GetContextTypeAsCString(val.GetContextType()) << " ";
+    return false;
+  }
+
+  if (m_value_type == Value::ValueType::HostAddress) {
+    const DataBufferHeap &buffer = val.GetBuffer();
+    const size_t buffer_size = buffer.GetByteSize();
+    if (buffer_size != m_expected_bytes.size()) {
+      os << "buffer size mismatch: expected " << m_expected_bytes.size()
+         << ", got " << buffer_size << " ";
+      return false;
+    }
+
+    const uint8_t *data = buffer.GetBytes();
+    for (size_t i = 0; i < buffer_size; ++i) {
+      if (data[i] != m_expected_bytes[i]) {
+        os << "byte mismatch at index " << i << ": expected "
+           << llvm::format("0x%02x", static_cast<unsigned>(m_expected_bytes[i]))
+           << ", got " << llvm::format("0x%02x", static_cast<unsigned>(data[i]))
+           << " ";
+        return false;
+      }
+    }
+  } else {
+    // For Scalar, FileAddress, and LoadAddress - compare m_value
----------------
JDevlieghere wrote:

```suggestion
    // For Scalar, FileAddress, and LoadAddress compare m_value.
```

https://github.com/llvm/llvm-project/pull/167427


More information about the lldb-commits mailing list