OptionValuePathMappings.cpp revision 344779
1//===-- OptionValuePathMappings.cpp -----------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Interpreter/OptionValuePathMappings.h"
11
12#include "lldb/Host/FileSystem.h"
13#include "lldb/Host/StringConvert.h"
14#include "lldb/Utility/Args.h"
15#include "lldb/Utility/FileSpec.h"
16#include "lldb/Utility/Stream.h"
17
18using namespace lldb;
19using namespace lldb_private;
20namespace {
21static bool VerifyPathExists(const char *path) {
22  if (path && path[0])
23    return FileSystem::Instance().Exists(path);
24  else
25    return false;
26}
27}
28
29void OptionValuePathMappings::DumpValue(const ExecutionContext *exe_ctx,
30                                        Stream &strm, uint32_t dump_mask) {
31  if (dump_mask & eDumpOptionType)
32    strm.Printf("(%s)", GetTypeAsCString());
33  if (dump_mask & eDumpOptionValue) {
34    if (dump_mask & eDumpOptionType)
35      strm.Printf(" =%s", (m_path_mappings.GetSize() > 0) ? "\n" : "");
36    m_path_mappings.Dump(&strm);
37  }
38}
39
40Status OptionValuePathMappings::SetValueFromString(llvm::StringRef value,
41                                                   VarSetOperationType op) {
42  Status error;
43  Args args(value.str());
44  const size_t argc = args.GetArgumentCount();
45
46  switch (op) {
47  case eVarSetOperationClear:
48    Clear();
49    NotifyValueChanged();
50    break;
51
52  case eVarSetOperationReplace:
53    // Must be at least one index + 1 pair of paths, and the pair count must be
54    // even
55    if (argc >= 3 && (((argc - 1) & 1) == 0)) {
56      uint32_t idx =
57          StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
58      const uint32_t count = m_path_mappings.GetSize();
59      if (idx > count) {
60        error.SetErrorStringWithFormat(
61            "invalid file list index %u, index must be 0 through %u", idx,
62            count);
63      } else {
64        bool changed = false;
65        for (size_t i = 1; i < argc; i += 2, ++idx) {
66          const char *orginal_path = args.GetArgumentAtIndex(i);
67          const char *replace_path = args.GetArgumentAtIndex(i + 1);
68          if (VerifyPathExists(replace_path)) {
69            ConstString a(orginal_path);
70            ConstString b(replace_path);
71            if (!m_path_mappings.Replace(a, b, idx, m_notify_changes))
72              m_path_mappings.Append(a, b, m_notify_changes);
73            changed = true;
74          } else {
75            error.SetErrorStringWithFormat(
76                "the replacement path doesn't exist: \"%s\"", replace_path);
77            break;
78          }
79        }
80        if (changed)
81          NotifyValueChanged();
82      }
83    } else {
84      error.SetErrorString("replace operation takes an array index followed by "
85                           "one or more path pairs");
86    }
87    break;
88
89  case eVarSetOperationAssign:
90    if (argc < 2 || (argc & 1)) {
91      error.SetErrorString("assign operation takes one or more path pairs");
92      break;
93    }
94    m_path_mappings.Clear(m_notify_changes);
95    // Fall through to append case
96    LLVM_FALLTHROUGH;
97  case eVarSetOperationAppend:
98    if (argc < 2 || (argc & 1)) {
99      error.SetErrorString("append operation takes one or more path pairs");
100      break;
101    } else {
102      bool changed = false;
103      for (size_t i = 0; i < argc; i += 2) {
104        const char *orginal_path = args.GetArgumentAtIndex(i);
105        const char *replace_path = args.GetArgumentAtIndex(i + 1);
106        if (VerifyPathExists(replace_path)) {
107          ConstString a(orginal_path);
108          ConstString b(replace_path);
109          m_path_mappings.Append(a, b, m_notify_changes);
110          m_value_was_set = true;
111          changed = true;
112        } else {
113          error.SetErrorStringWithFormat(
114              "the replacement path doesn't exist: \"%s\"", replace_path);
115          break;
116        }
117      }
118      if (changed)
119        NotifyValueChanged();
120    }
121    break;
122
123  case eVarSetOperationInsertBefore:
124  case eVarSetOperationInsertAfter:
125    // Must be at least one index + 1 pair of paths, and the pair count must be
126    // even
127    if (argc >= 3 && (((argc - 1) & 1) == 0)) {
128      uint32_t idx =
129          StringConvert::ToUInt32(args.GetArgumentAtIndex(0), UINT32_MAX);
130      const uint32_t count = m_path_mappings.GetSize();
131      if (idx > count) {
132        error.SetErrorStringWithFormat(
133            "invalid file list index %u, index must be 0 through %u", idx,
134            count);
135      } else {
136        bool changed = false;
137        if (op == eVarSetOperationInsertAfter)
138          ++idx;
139        for (size_t i = 1; i < argc; i += 2, ++idx) {
140          const char *orginal_path = args.GetArgumentAtIndex(i);
141          const char *replace_path = args.GetArgumentAtIndex(i + 1);
142          if (VerifyPathExists(replace_path)) {
143            ConstString a(orginal_path);
144            ConstString b(replace_path);
145            m_path_mappings.Insert(a, b, idx, m_notify_changes);
146            changed = true;
147          } else {
148            error.SetErrorStringWithFormat(
149                "the replacement path doesn't exist: \"%s\"", replace_path);
150            break;
151          }
152        }
153        if (changed)
154          NotifyValueChanged();
155      }
156    } else {
157      error.SetErrorString("insert operation takes an array index followed by "
158                           "one or more path pairs");
159    }
160    break;
161
162  case eVarSetOperationRemove:
163    if (argc > 0) {
164      std::vector<int> remove_indexes;
165      bool all_indexes_valid = true;
166      size_t i;
167      for (i = 0; all_indexes_valid && i < argc; ++i) {
168        const int idx =
169            StringConvert::ToSInt32(args.GetArgumentAtIndex(i), INT32_MAX);
170        if (idx == INT32_MAX)
171          all_indexes_valid = false;
172        else
173          remove_indexes.push_back(idx);
174      }
175
176      if (all_indexes_valid) {
177        size_t num_remove_indexes = remove_indexes.size();
178        if (num_remove_indexes) {
179          // Sort and then erase in reverse so indexes are always valid
180          llvm::sort(remove_indexes.begin(), remove_indexes.end());
181          for (size_t j = num_remove_indexes - 1; j < num_remove_indexes; ++j) {
182            m_path_mappings.Remove(j, m_notify_changes);
183          }
184        }
185        NotifyValueChanged();
186      } else {
187        error.SetErrorStringWithFormat(
188            "invalid array index '%s', aborting remove operation",
189            args.GetArgumentAtIndex(i));
190      }
191    } else {
192      error.SetErrorString("remove operation takes one or more array index");
193    }
194    break;
195
196  case eVarSetOperationInvalid:
197    error = OptionValue::SetValueFromString(value, op);
198    break;
199  }
200  return error;
201}
202
203lldb::OptionValueSP OptionValuePathMappings::DeepCopy() const {
204  return OptionValueSP(new OptionValuePathMappings(*this));
205}
206