Coverage Report

Created: 2022-01-22 13:19

/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
1.65k
  StructuredDataImpl() {}
29
30
938
  StructuredDataImpl(const StructuredDataImpl &rhs) = default;
31
32
  StructuredDataImpl(StructuredData::ObjectSP obj)
33
95
      : 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
1
        m_data_sp(EventDataStructuredData::GetObjectFromEvent(event_sp.get())) {
39
1
  }
40
41
2.67k
  ~StructuredDataImpl() = default;
42
43
809
  StructuredDataImpl &operator=(const StructuredDataImpl &rhs) = default;
44
45
441
  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
11
  Status GetAsJSON(Stream &stream) const {
53
11
    Status error;
54
55
11
    if (!m_data_sp) {
56
4
      error.SetErrorString("No structured data.");
57
4
      return error;
58
4
    }
59
60
7
    llvm::json::OStream s(stream.AsRawOstream());
61
7
    m_data_sp->Serialize(s);
62
7
    return error;
63
11
  }
64
65
1
  Status GetDescription(Stream &stream) const {
66
1
    Status error;
67
68
1
    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
74
    // Grab the plugin
75
1
    lldb::StructuredDataPluginSP plugin_sp = m_plugin_wp.lock();
76
77
    // If there's no plugin, call underlying data's dump method:
78
1
    if (!plugin_sp) {
79
1
      if (!m_data_sp) {
80
0
        error.SetErrorString("No data to describe.");
81
0
        return error;
82
0
      }
83
1
      m_data_sp->Dump(stream, true);
84
1
      return error;
85
1
    }
86
    // Get the data's description.
87
0
    return plugin_sp->GetDescription(m_data_sp, stream);
88
1
  }
89
90
32
  StructuredData::ObjectSP GetObjectSP() { return m_data_sp; }
91
92
829
  void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
93
94
54
  lldb::StructuredDataType GetType() const {
95
54
    return (m_data_sp ? 
m_data_sp->GetType()48
:
96
54
        
lldb::eStructuredDataTypeInvalid6
);
97
54
  }
98
99
55
  size_t GetSize() const {
100
55
    if (!m_data_sp)
101
1
      return 0;
102
103
54
    if (m_data_sp->GetType() == lldb::eStructuredDataTypeDictionary) {
104
1
      auto dict = m_data_sp->GetAsDictionary();
105
1
      return (dict->GetSize());
106
53
    } else if (m_data_sp->GetType() == lldb::eStructuredDataTypeArray) {
107
53
      auto array = m_data_sp->GetAsArray();
108
53
      return (array->GetSize());
109
53
    } else
110
0
      return 0;
111
54
  }
112
113
586
  StructuredData::ObjectSP GetValueForKey(const char *key) const {
114
586
    if (m_data_sp) {
115
585
      auto dict = m_data_sp->GetAsDictionary();
116
585
      if (dict)
117
585
        return dict->GetValueForKey(llvm::StringRef(key));
118
585
    }
119
1
    return StructuredData::ObjectSP();
120
586
  }
121
122
97
  StructuredData::ObjectSP GetItemAtIndex(size_t idx) const {
123
97
    if (m_data_sp) {
124
96
      auto array = m_data_sp->GetAsArray();
125
96
      if (array)
126
96
        return array->GetItemAtIndex(idx);
127
96
    }
128
1
    return StructuredData::ObjectSP();
129
97
  }
130
131
3
  uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
132
3
    return (m_data_sp ? 
m_data_sp->GetIntegerValue(fail_value)2
:
fail_value1
);
133
3
  }
134
135
2
  double GetFloatValue(double fail_value = 0.0) const {
136
2
    return (m_data_sp ? 
m_data_sp->GetFloatValue(fail_value)1
:
fail_value1
);
137
2
  }
138
139
47
  bool GetBooleanValue(bool fail_value = false) const {
140
47
    return (m_data_sp ? 
m_data_sp->GetBooleanValue(fail_value)45
:
fail_value2
);
141
47
  }
142
143
439
  size_t GetStringValue(char *dst, size_t dst_len) const {
144
439
    if (!m_data_sp)
145
1
      return 0;
146
147
438
    llvm::StringRef result = m_data_sp->GetStringValue();
148
438
    if (result.empty())
149
1
      return 0;
150
151
437
    if (!dst || !dst_len) {
152
0
      char s[1];
153
0
      return (::snprintf(s, 1, "%s", result.data()));
154
0
    }
155
437
    return (::snprintf(dst, dst_len, "%s", result.data()));
156
437
  }
157
158
0
  StructuredData::ObjectSP GetObjectSP() const { return m_data_sp; }
159
160
private:
161
  lldb::StructuredDataPluginWP m_plugin_wp;
162
  StructuredData::ObjectSP m_data_sp;
163
};
164
} // namespace lldb_private
165
#endif