Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Core/StructuredDataImpl.h
Line
Count
Source (jump to first uncovered line)
1
//===-- StructuredDataImpl.h ------------------------------------*- C++ -*-===//
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
#ifndef LLDB_CORE_STRUCTUREDDATAIMPL_H
10
#define LLDB_CORE_STRUCTUREDDATAIMPL_H
11
12
#include "lldb/Target/StructuredDataPlugin.h"
13
#include "lldb/Utility/Event.h"
14
#include "lldb/Utility/Status.h"
15
#include "lldb/Utility/Stream.h"
16
#include "lldb/Utility/StructuredData.h"
17
#include "lldb/lldb-enumerations.h"
18
#include "lldb/lldb-forward.h"
19
#include "llvm/ADT/StringRef.h"
20
21
#pragma mark--
22
#pragma mark StructuredDataImpl
23
24
namespace lldb_private {
25
26
class StructuredDataImpl {
27
public:
28
2.16k
  StructuredDataImpl() = default;
29
30
1.14k
  StructuredDataImpl(const StructuredDataImpl &rhs) = default;
31
32
  StructuredDataImpl(StructuredData::ObjectSP obj)
33
83
      : m_data_sp(std::move(obj)) {}
34
35
  StructuredDataImpl(const lldb::EventSP &event_sp)
36
      : m_plugin_wp(
37
            EventDataStructuredData::GetPluginFromEvent(event_sp.get())),
38
0
        m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
39
0
  }
40
41
3.37k
  ~StructuredDataImpl() = default;
42
43
1.06k
  StructuredDataImpl &operator=(const StructuredDataImpl &rhs) = default;
44
45
461
  bool IsValid() const { return m_data_sp.get() != nullptr; }
46
47
0
  void Clear() {
48
0
    m_plugin_wp.reset();
49
0
    m_data_sp.reset();
50
0
  }
51
52
24
  Status GetAsJSON(Stream &stream) const {
53
24
    Status error;
54
55
24
    if (!m_data_sp) {
56
3
      error.SetErrorString("No structured data.");
57
3
      return error;
58
3
    }
59
60
21
    llvm::json::OStream s(stream.AsRawOstream());
61
21
    m_data_sp->Serialize(s);
62
21
    return error;
63
24
  }
64
65
0
  Status GetDescription(Stream &stream) const {
66
0
    Status error;
67
0
68
0
    if (!m_data_sp) {
69
0
      error.SetErrorString("Cannot pretty print structured data: "
70
0
                           "no data to print.");
71
0
      return error;
72
0
    }
73
0
74
0
    // Grab the plugin
75
0
    lldb::StructuredDataPluginSP plugin_sp = m_plugin_wp.lock();
76
0
77
0
    // If there's no plugin, call underlying data's dump method:
78
0
    if (!plugin_sp) {
79
0
      if (!m_data_sp) {
80
0
        error.SetErrorString("No data to describe.");
81
0
        return error;
82
0
      }
83
0
      m_data_sp->GetDescription(stream);
84
0
      return error;
85
0
    }
86
0
    // Get the data's description.
87
0
    return plugin_sp->GetDescription(m_data_sp, stream);
88
0
  }
89
90
32
  StructuredData::ObjectSP GetObjectSP() { return m_data_sp; }
91
92
1.07k
  void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
93
94
55
  lldb::StructuredDataType GetType() const {
95
55
    return (m_data_sp ? 
m_data_sp->GetType()52
:
96
55
        
lldb::eStructuredDataTypeInvalid3
);
97
55
  }
98
99
71
  size_t GetSize() const {
100
71
    if (!m_data_sp)
101
0
      return 0;
102
103
71
    if (m_data_sp->GetType() == lldb::eStructuredDataTypeDictionary) {
104
2
      auto dict = m_data_sp->GetAsDictionary();
105
2
      return (dict->GetSize());
106
69
    } else if (m_data_sp->GetType() == lldb::eStructuredDataTypeArray) {
107
69
      auto array = m_data_sp->GetAsArray();
108
69
      return (array->GetSize());
109
69
    } else
110
0
      return 0;
111
71
  }
112
113
721
  StructuredData::ObjectSP GetValueForKey(const char *key) const {
114
721
    if (m_data_sp) {
115
721
      auto dict = m_data_sp->GetAsDictionary();
116
721
      if (dict)
117
721
        return dict->GetValueForKey(llvm::StringRef(key));
118
721
    }
119
0
    return StructuredData::ObjectSP();
120
721
  }
121
122
138
  StructuredData::ObjectSP GetItemAtIndex(size_t idx) const {
123
138
    if (m_data_sp) {
124
138
      auto array = m_data_sp->GetAsArray();
125
138
      if (array)
126
138
        return array->GetItemAtIndex(idx);
127
138
    }
128
0
    return StructuredData::ObjectSP();
129
138
  }
130
131
11
  uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
132
11
    return (m_data_sp ? m_data_sp->GetUnsignedIntegerValue(fail_value)
133
11
                      : 
fail_value0
);
134
11
  }
135
136
1
  int64_t GetIntegerValue(int64_t fail_value = 0) const {
137
1
    return (m_data_sp ? m_data_sp->GetSignedIntegerValue(fail_value)
138
1
                      : 
fail_value0
);
139
1
  }
140
141
1
  double GetFloatValue(double fail_value = 0.0) const {
142
1
    return (m_data_sp ? m_data_sp->GetFloatValue(fail_value) : 
fail_value0
);
143
1
  }
144
145
86
  bool GetBooleanValue(bool fail_value = false) const {
146
86
    return (m_data_sp ? m_data_sp->GetBooleanValue(fail_value) : 
fail_value0
);
147
86
  }
148
149
0
  size_t GetStringValue(char *dst, size_t dst_len) const {
150
0
    if (!m_data_sp)
151
0
      return 0;
152
0
153
0
    llvm::StringRef result = m_data_sp->GetStringValue();
154
0
    if (result.empty())
155
0
      return 0;
156
0
157
0
    if (!dst || !dst_len) {
158
0
      char s[1];
159
0
      return (::snprintf(s, 1, "%s", result.data()));
160
0
    }
161
0
    return (::snprintf(dst, dst_len, "%s", result.data()));
162
0
  }
163
164
1
  void *GetGenericValue() const {
165
1
    if (!m_data_sp)
166
0
      return nullptr;
167
168
1
    StructuredData::Generic *generic_data = m_data_sp->GetAsGeneric();
169
1
    if (!generic_data)
170
0
      return nullptr;
171
172
1
    return generic_data->GetValue();
173
1
  }
174
175
0
  StructuredData::ObjectSP GetObjectSP() const { return m_data_sp; }
176
177
private:
178
  lldb::StructuredDataPluginWP m_plugin_wp;
179
  StructuredData::ObjectSP m_data_sp;
180
};
181
} // namespace lldb_private
182
#endif