]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/API/SBEnvironment.cpp
Merge ^/head r363583 through r364040.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / API / SBEnvironment.cpp
1 //===-- SBEnvironment.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 "lldb/API/SBEnvironment.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBStringList.h"
13 #include "lldb/Utility/ConstString.h"
14 #include "lldb/Utility/Environment.h"
15
16 using namespace lldb;
17 using namespace lldb_private;
18
19 SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) {
20   LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBEnvironment);
21 }
22
23 SBEnvironment::SBEnvironment(const SBEnvironment &rhs)
24     : m_opaque_up(clone(rhs.m_opaque_up)) {
25   LLDB_RECORD_CONSTRUCTOR(SBEnvironment, (const lldb::SBEnvironment &), rhs);
26 }
27
28 SBEnvironment::SBEnvironment(Environment rhs)
29     : m_opaque_up(new Environment(std::move(rhs))) {}
30
31 SBEnvironment::~SBEnvironment() = default;
32
33 const SBEnvironment &SBEnvironment::operator=(const SBEnvironment &rhs) {
34   LLDB_RECORD_METHOD(const lldb::SBEnvironment &,
35                      SBEnvironment, operator=,(const lldb::SBEnvironment &),
36                      rhs);
37
38   if (this != &rhs)
39     m_opaque_up = clone(rhs.m_opaque_up);
40   return LLDB_RECORD_RESULT(*this);
41 }
42
43 size_t SBEnvironment::GetNumValues() {
44   LLDB_RECORD_METHOD_NO_ARGS(size_t, SBEnvironment, GetNumValues);
45
46   return m_opaque_up->size();
47 }
48
49 const char *SBEnvironment::Get(const char *name) {
50   LLDB_RECORD_METHOD(const char *, SBEnvironment, Get, (const char *), name);
51
52   auto entry = m_opaque_up->find(name);
53   if (entry == m_opaque_up->end()) {
54     return nullptr;
55   }
56   return ConstString(entry->second).AsCString("");
57 }
58
59 const char *SBEnvironment::GetNameAtIndex(size_t index) {
60   LLDB_RECORD_METHOD(const char *, SBEnvironment, GetNameAtIndex, (size_t),
61                      index);
62
63   if (index >= GetNumValues())
64     return nullptr;
65   return ConstString(std::next(m_opaque_up->begin(), index)->first())
66       .AsCString("");
67 }
68
69 const char *SBEnvironment::GetValueAtIndex(size_t index) {
70   LLDB_RECORD_METHOD(const char *, SBEnvironment, GetValueAtIndex, (size_t),
71                      index);
72
73   if (index >= GetNumValues())
74     return nullptr;
75   return ConstString(std::next(m_opaque_up->begin(), index)->second)
76       .AsCString("");
77 }
78
79 bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
80   LLDB_RECORD_METHOD(bool, SBEnvironment, Set,
81                      (const char *, const char *, bool), name, value,
82                      overwrite);
83
84   if (overwrite) {
85     m_opaque_up->insert_or_assign(name, std::string(value));
86     return true;
87   }
88   return m_opaque_up->try_emplace(name, std::string(value)).second;
89 }
90
91 bool SBEnvironment::Unset(const char *name) {
92   LLDB_RECORD_METHOD(bool, SBEnvironment, Unset, (const char *), name);
93
94   return m_opaque_up->erase(name);
95 }
96
97 SBStringList SBEnvironment::GetEntries() {
98   LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStringList, SBEnvironment, GetEntries);
99
100   SBStringList entries;
101   for (const auto &KV : *m_opaque_up) {
102     entries.AppendString(Environment::compose(KV).c_str());
103   }
104   return LLDB_RECORD_RESULT(entries);
105 }
106
107 void SBEnvironment::PutEntry(const char *name_and_value) {
108   LLDB_RECORD_METHOD(void, SBEnvironment, PutEntry, (const char *),
109                      name_and_value);
110
111   auto split = llvm::StringRef(name_and_value).split('=');
112   m_opaque_up->insert_or_assign(split.first.str(), split.second.str());
113 }
114
115 void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {
116   LLDB_RECORD_METHOD(void, SBEnvironment, SetEntries,
117                      (const lldb::SBStringList &, bool), entries, append);
118
119   if (!append)
120     m_opaque_up->clear();
121   for (size_t i = 0; i < entries.GetSize(); i++) {
122     PutEntry(entries.GetStringAtIndex(i));
123   }
124 }
125
126 void SBEnvironment::Clear() {
127   LLDB_RECORD_METHOD_NO_ARGS(void, SBEnvironment, Clear);
128
129   m_opaque_up->clear();
130 }
131
132 Environment &SBEnvironment::ref() const { return *m_opaque_up; }
133
134 namespace lldb_private {
135 namespace repro {
136 template <> void RegisterMethods<SBEnvironment>(Registry &R) {
137   LLDB_REGISTER_CONSTRUCTOR(SBEnvironment, ());
138   LLDB_REGISTER_CONSTRUCTOR(SBEnvironment, (const lldb::SBEnvironment &));
139   LLDB_REGISTER_METHOD(const lldb::SBEnvironment &,
140                        SBEnvironment, operator=,(const lldb::SBEnvironment &));
141   LLDB_REGISTER_METHOD(size_t, SBEnvironment, GetNumValues, ());
142   LLDB_REGISTER_METHOD(const char *, SBEnvironment, Get, (const char *));
143   LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetNameAtIndex, (size_t));
144   LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetValueAtIndex, (size_t));
145   LLDB_REGISTER_METHOD(bool, SBEnvironment, Set,
146                        (const char *, const char *, bool));
147   LLDB_REGISTER_METHOD(bool, SBEnvironment, Unset, (const char *));
148   LLDB_REGISTER_METHOD(lldb::SBStringList, SBEnvironment, GetEntries, ());
149   LLDB_REGISTER_METHOD(void, SBEnvironment, PutEntry, (const char *));
150   LLDB_REGISTER_METHOD(void, SBEnvironment, SetEntries,
151                        (const lldb::SBStringList &, bool));
152   LLDB_REGISTER_METHOD(void, SBEnvironment, Clear, ());
153 }
154 } // namespace repro
155 } // namespace lldb_private