Coverage Report

Created: 2021-12-07 08:24

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Language/CPlusPlus/LibCxxOptional.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- LibCxxOptional.cpp ------------------------------------------------===//
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
#include "LibCxx.h"
10
#include "lldb/DataFormatters/FormattersHelpers.h"
11
12
using namespace lldb;
13
using namespace lldb_private;
14
15
namespace {
16
17
class OptionalFrontEnd : public SyntheticChildrenFrontEnd {
18
public:
19
12
  OptionalFrontEnd(ValueObject &valobj) : SyntheticChildrenFrontEnd(valobj) {
20
12
    Update();
21
12
  }
22
23
0
  size_t GetIndexOfChildWithName(ConstString name) override {
24
0
    return formatters::ExtractIndexFromString(name.GetCString());
25
0
  }
26
27
3
  bool MightHaveChildren() override { return true; }
28
  bool Update() override;
29
12
  size_t CalculateNumChildren() override { return m_has_value ? 
1U9
:
0U3
; }
30
  ValueObjectSP GetChildAtIndex(size_t idx) override;
31
32
private:
33
  /// True iff the option contains a value.
34
  bool m_has_value = false;
35
};
36
} // namespace
37
38
24
bool OptionalFrontEnd::Update() {
39
24
  ValueObjectSP engaged_sp(
40
24
      m_backend.GetChildMemberWithName(ConstString("__engaged_"), true));
41
42
24
  if (!engaged_sp)
43
0
    return false;
44
45
  // __engaged_ is a bool flag and is true if the optional contains a value.
46
  // Converting it to unsigned gives us a size of 1 if it contains a value
47
  // and 0 if not.
48
24
  m_has_value = engaged_sp->GetValueAsUnsigned(0) != 0;
49
50
24
  return false;
51
24
}
52
53
9
ValueObjectSP OptionalFrontEnd::GetChildAtIndex(size_t idx) {
54
9
  if (!m_has_value)
55
0
    return ValueObjectSP();
56
57
  // __val_ contains the underlying value of an optional if it has one.
58
  // Currently because it is part of an anonymous union GetChildMemberWithName()
59
  // does not peer through and find it unless we are at the parent itself.
60
  // We can obtain the parent through __engaged_.
61
9
  ValueObjectSP val_sp(
62
9
      m_backend.GetChildMemberWithName(ConstString("__engaged_"), true)
63
9
          ->GetParent()
64
9
          ->GetChildAtIndex(0, true)
65
9
          ->GetChildMemberWithName(ConstString("__val_"), true));
66
67
9
  if (!val_sp)
68
0
    return ValueObjectSP();
69
70
9
  CompilerType holder_type = val_sp->GetCompilerType();
71
72
9
  if (!holder_type)
73
0
    return ValueObjectSP();
74
75
9
  return val_sp->Clone(ConstString("Value"));
76
9
}
77
78
SyntheticChildrenFrontEnd *
79
formatters::LibcxxOptionalFrontEndCreator(CXXSyntheticChildren *,
80
12
                                          lldb::ValueObjectSP valobj_sp) {
81
12
  if (valobj_sp)
82
12
    return new OptionalFrontEnd(*valobj_sp);
83
0
  return nullptr;
84
12
}