[DAG] Fix typo in VSELECT SimplifyDemandedVectorElts handling. NFC.
[llvm-project.git] / lldb / source / API / SBEnvironment.cpp
blob5fafabe02e014056227076a60d7bc5485234dd7d
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 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBEnvironment.h"
10 #include "Utils.h"
11 #include "lldb/API/SBStringList.h"
12 #include "lldb/Utility/ConstString.h"
13 #include "lldb/Utility/Environment.h"
14 #include "lldb/Utility/Instrumentation.h"
16 using namespace lldb;
17 using namespace lldb_private;
19 SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) {
20 LLDB_INSTRUMENT_VA(this);
23 SBEnvironment::SBEnvironment(const SBEnvironment &rhs)
24 : m_opaque_up(clone(rhs.m_opaque_up)) {
25 LLDB_INSTRUMENT_VA(this, rhs);
28 SBEnvironment::SBEnvironment(Environment rhs)
29 : m_opaque_up(new Environment(std::move(rhs))) {}
31 SBEnvironment::~SBEnvironment() = default;
33 const SBEnvironment &SBEnvironment::operator=(const SBEnvironment &rhs) {
34 LLDB_INSTRUMENT_VA(this, rhs);
36 if (this != &rhs)
37 m_opaque_up = clone(rhs.m_opaque_up);
38 return *this;
41 size_t SBEnvironment::GetNumValues() {
42 LLDB_INSTRUMENT_VA(this);
44 return m_opaque_up->size();
47 const char *SBEnvironment::Get(const char *name) {
48 LLDB_INSTRUMENT_VA(this, name);
50 auto entry = m_opaque_up->find(name);
51 if (entry == m_opaque_up->end()) {
52 return nullptr;
54 return ConstString(entry->second).AsCString("");
57 const char *SBEnvironment::GetNameAtIndex(size_t index) {
58 LLDB_INSTRUMENT_VA(this, index);
60 if (index >= GetNumValues())
61 return nullptr;
62 return ConstString(std::next(m_opaque_up->begin(), index)->first())
63 .AsCString("");
66 const char *SBEnvironment::GetValueAtIndex(size_t index) {
67 LLDB_INSTRUMENT_VA(this, index);
69 if (index >= GetNumValues())
70 return nullptr;
71 return ConstString(std::next(m_opaque_up->begin(), index)->second)
72 .AsCString("");
75 bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
76 LLDB_INSTRUMENT_VA(this, name, value, overwrite);
78 if (overwrite) {
79 m_opaque_up->insert_or_assign(name, std::string(value));
80 return true;
82 return m_opaque_up->try_emplace(name, std::string(value)).second;
85 bool SBEnvironment::Unset(const char *name) {
86 LLDB_INSTRUMENT_VA(this, name);
88 return m_opaque_up->erase(name);
91 SBStringList SBEnvironment::GetEntries() {
92 LLDB_INSTRUMENT_VA(this);
94 SBStringList entries;
95 for (const auto &KV : *m_opaque_up) {
96 entries.AppendString(Environment::compose(KV).c_str());
98 return entries;
101 void SBEnvironment::PutEntry(const char *name_and_value) {
102 LLDB_INSTRUMENT_VA(this, name_and_value);
104 auto split = llvm::StringRef(name_and_value).split('=');
105 m_opaque_up->insert_or_assign(split.first.str(), split.second.str());
108 void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {
109 LLDB_INSTRUMENT_VA(this, entries, append);
111 if (!append)
112 m_opaque_up->clear();
113 for (size_t i = 0; i < entries.GetSize(); i++) {
114 PutEntry(entries.GetStringAtIndex(i));
118 void SBEnvironment::Clear() {
119 LLDB_INSTRUMENT_VA(this);
121 m_opaque_up->clear();
124 Environment &SBEnvironment::ref() const { return *m_opaque_up; }