aboutsummaryrefslogtreecommitdiff
path: root/lldb/unittests/Expression/ValueMatcher.cpp
blob: ee7ccaebabd64b5f3c320f119d9aa9fe2595006d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//===----------------------------------------------------------------------===//
//
// 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,
                                   std::ostream *stream) const {
  if (stream) {
    llvm::raw_os_ostream os(*stream);
    return MatchAndExplainImpl(val, os);
  }

  llvm::raw_null_ostream os;
  return MatchAndExplainImpl(val, os);
}

// Match the provided value and explain any mismatches using
// the raw_ostream. We use the llvm::raw_ostream here to simplify the formatting
// of Scalar values which already know how to print themselves to that stream.
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.
    const Scalar &actual_scalar = val.GetScalar();
    if (actual_scalar != m_expected_scalar) {
      os << "scalar value mismatch: expected " << m_expected_scalar << ", got "
         << actual_scalar;
      return false;
    }
  }

  return true;
}

void ValueMatcher::DescribeTo(std::ostream *os) const {
  if (!os)
    return;
  llvm::raw_os_ostream raw_os(*os);
  FormatValueDetails(raw_os, m_value_type, m_context_type, m_expected_scalar,
                     m_expected_bytes);
}

void ValueMatcher::DescribeNegationTo(std::ostream *os) const {
  if (!os)
    return;
  *os << "value does not match";
}

testing::Matcher<Value>
lldb_private::MatchScalarValue(Value::ValueType value_type,
                               const Scalar &expected_scalar,
                               Value::ContextType context_type) {
  return ValueMatcher(value_type, expected_scalar, context_type);
}

testing::Matcher<Value>
lldb_private::MatchHostValue(Value::ValueType value_type,
                             const std::vector<uint8_t> &expected_bytes,
                             Value::ContextType context_type) {
  return ValueMatcher(value_type, expected_bytes, context_type);
}

testing::Matcher<Value>
lldb_private::IsScalar(const Scalar &expected_scalar,
                       Value::ContextType context_type) {
  return MatchScalarValue(Value::ValueType::Scalar, expected_scalar,
                          context_type);
}

testing::Matcher<Value>
lldb_private::IsLoadAddress(const Scalar &expected_address,
                            Value::ContextType context_type) {
  return MatchScalarValue(Value::ValueType::LoadAddress, expected_address,
                          context_type);
}

testing::Matcher<Value>
lldb_private::IsFileAddress(const Scalar &expected_address,
                            Value::ContextType context_type) {
  return MatchScalarValue(Value::ValueType::FileAddress, expected_address,
                          context_type);
}

testing::Matcher<Value>
lldb_private::IsHostValue(const std::vector<uint8_t> &expected_bytes,
                          Value::ContextType context_type) {
  return MatchHostValue(Value::ValueType::HostAddress, expected_bytes,
                        context_type);
}

Scalar lldb_private::GetScalar(unsigned bits, uint64_t value, bool sign) {
  Scalar scalar(value);
  scalar.TruncOrExtendTo(bits, sign);
  return scalar;
}

llvm::detail::ValueMatchesPoly<testing::Matcher<Value>>
lldb_private::ExpectScalar(const Scalar &expected_scalar,
                           Value::ContextType context_type) {
  return llvm::HasValue(IsScalar(expected_scalar, context_type));
}

llvm::detail::ValueMatchesPoly<testing::Matcher<Value>>
lldb_private::ExpectScalar(unsigned bits, uint64_t value, bool sign,
                           Value::ContextType context_type) {
  return ExpectScalar(GetScalar(bits, value, sign), context_type);
}

llvm::detail::ValueMatchesPoly<testing::Matcher<Value>>
lldb_private::ExpectLoadAddress(const Scalar &expected_address,
                                Value::ContextType context_type) {
  return llvm::HasValue(IsLoadAddress(expected_address, context_type));
}

llvm::detail::ValueMatchesPoly<testing::Matcher<Value>>
lldb_private::ExpectFileAddress(const Scalar &expected_address,
                                Value::ContextType context_type) {
  return llvm::HasValue(IsFileAddress(expected_address, context_type));
}

llvm::detail::ValueMatchesPoly<testing::Matcher<Value>>
lldb_private::ExpectHostAddress(const std::vector<uint8_t> &expected_bytes,
                                Value::ContextType context_type) {
  return llvm::HasValue(IsHostValue(expected_bytes, context_type));
}