File indexing completed on 2026-05-10 08:42:57
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLDB_UTILITY_ENVIRONMENT_H
0010 #define LLDB_UTILITY_ENVIRONMENT_H
0011
0012 #include "llvm/ADT/StringMap.h"
0013 #include "llvm/Support/Allocator.h"
0014 #include "llvm/Support/FormatProviders.h"
0015
0016 namespace lldb_private {
0017
0018 class Environment : private llvm::StringMap<std::string> {
0019 using Base = llvm::StringMap<std::string>;
0020
0021 public:
0022 class Envp {
0023 public:
0024 Envp(Envp &&RHS) = default;
0025 Envp &operator=(Envp &&RHS) = default;
0026
0027 char *const *get() const { return Data; }
0028 operator char *const *() const { return get(); }
0029
0030 private:
0031 explicit Envp(const Environment &Env);
0032 char *make_entry(llvm::StringRef Key, llvm::StringRef Value);
0033 Envp(const Envp &) = delete;
0034 Envp &operator=(const Envp &) = delete;
0035 friend class Environment;
0036
0037 llvm::BumpPtrAllocator Allocator;
0038 char **Data;
0039 };
0040
0041 using Base::const_iterator;
0042 using Base::iterator;
0043 using Base::value_type;
0044
0045 using Base::begin;
0046 using Base::clear;
0047 using Base::count;
0048 using Base::empty;
0049 using Base::end;
0050 using Base::erase;
0051 using Base::find;
0052 using Base::insert;
0053 using Base::insert_or_assign;
0054 using Base::lookup;
0055 using Base::size;
0056 using Base::try_emplace;
0057 using Base::operator[];
0058
0059 Environment() {}
0060 Environment(const Environment &RHS) : Base(static_cast<const Base&>(RHS)) {}
0061 Environment(Environment &&RHS) : Base(std::move(RHS)) {}
0062 Environment(char *const *Env)
0063 : Environment(const_cast<const char *const *>(Env)) {}
0064 Environment(const char *const *Env);
0065
0066 Environment &operator=(Environment RHS) {
0067 Base::operator=(std::move(RHS));
0068 return *this;
0069 }
0070
0071 std::pair<iterator, bool> insert(llvm::StringRef KeyEqValue) {
0072 auto Split = KeyEqValue.split('=');
0073 return insert(std::make_pair(Split.first, std::string(Split.second)));
0074 }
0075
0076 void insert(iterator first, iterator last);
0077
0078 Envp getEnvp() const { return Envp(*this); }
0079
0080 static std::string compose(const value_type &KeyValue) {
0081 return (KeyValue.first() + "=" + KeyValue.second).str();
0082 }
0083 };
0084
0085 }
0086
0087 namespace llvm {
0088 template <> struct format_provider<lldb_private::Environment> {
0089 static void format(const lldb_private::Environment &Env, raw_ostream &Stream,
0090 StringRef Style) {
0091 for (const auto &KV : Env)
0092 Stream << "env[" << KV.first() << "] = " << KV.second << "\n";
0093 }
0094 };
0095 }
0096
0097 #endif