blob: 0f163d1c4f74e8172d6cc9eebe6529b516faf1a6 [file] [log] [blame]
Greg Clayton67cc0632012-08-22 17:17:09 +00001//===-- OptionValueEnumeration.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/OptionValueEnumeration.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Greg Clayton754a9362012-08-23 00:22:02 +000016#include "lldb/Core/StringList.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000017
18using namespace lldb;
19using namespace lldb_private;
20
21OptionValueEnumeration::OptionValueEnumeration (const OptionEnumValueElement *enumerators,
22 enum_type value) :
23 OptionValue(),
24 m_current_value (value),
25 m_default_value (value),
26 m_enumerations ()
27{
28 SetEnumerations(enumerators);
29}
30
31OptionValueEnumeration::~OptionValueEnumeration()
32{
33}
34
35void
36OptionValueEnumeration::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
37{
38 if (dump_mask & eDumpOptionType)
39 strm.Printf ("(%s)", GetTypeAsCString ());
40 if (dump_mask & eDumpOptionValue)
41 {
42 if (dump_mask & eDumpOptionType)
43 strm.PutCString (" = ");
44 const size_t count = m_enumerations.GetSize ();
45 for (size_t i=0; i<count; ++i)
46 {
47 if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value)
48 {
49 strm.PutCString(m_enumerations.GetCStringAtIndex(i));
50 return;
51 }
52 }
Daniel Malead01b2952012-11-29 21:49:15 +000053 strm.Printf("%" PRIu64, (uint64_t)m_current_value);
Greg Clayton67cc0632012-08-22 17:17:09 +000054 }
55}
56
57Error
Pavel Labathc95f7e22015-02-20 11:14:59 +000058OptionValueEnumeration::SetValueFromString (llvm::StringRef value, VarSetOperationType op)
Greg Clayton67cc0632012-08-22 17:17:09 +000059{
60 Error error;
61 switch (op)
62 {
63 case eVarSetOperationClear:
64 Clear ();
Greg Clayton332e8b12015-01-13 21:13:08 +000065 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +000066 break;
67
68 case eVarSetOperationReplace:
69 case eVarSetOperationAssign:
Greg Clayton67cc0632012-08-22 17:17:09 +000070 {
Pavel Labathc95f7e22015-02-20 11:14:59 +000071 ConstString const_enumerator_name(value.trim());
Greg Clayton67cc0632012-08-22 17:17:09 +000072 const EnumerationMapEntry *enumerator_entry = m_enumerations.FindFirstValueForName (const_enumerator_name.GetCString());
73 if (enumerator_entry)
74 {
75 m_current_value = enumerator_entry->value.value;
Greg Clayton332e8b12015-01-13 21:13:08 +000076 NotifyValueChanged();
Greg Clayton67cc0632012-08-22 17:17:09 +000077 }
78 else
79 {
80 StreamString error_strm;
Pavel Labathc95f7e22015-02-20 11:14:59 +000081 error_strm.Printf("invalid enumeration value '%s'", value.str().c_str());
Greg Clayton67cc0632012-08-22 17:17:09 +000082 const size_t count = m_enumerations.GetSize ();
83 if (count)
84 {
85 error_strm.Printf(", valid values are: %s", m_enumerations.GetCStringAtIndex(0));
86 for (size_t i=1; i<count; ++i)
87 {
88 error_strm.Printf (", %s", m_enumerations.GetCStringAtIndex(i));
89 }
90 }
91 error.SetErrorString(error_strm.GetData());
92 }
Pavel Labathc95f7e22015-02-20 11:14:59 +000093 break;
Greg Clayton67cc0632012-08-22 17:17:09 +000094 }
Greg Clayton67cc0632012-08-22 17:17:09 +000095
96 case eVarSetOperationInsertBefore:
97 case eVarSetOperationInsertAfter:
98 case eVarSetOperationRemove:
99 case eVarSetOperationAppend:
100 case eVarSetOperationInvalid:
Pavel Labathc95f7e22015-02-20 11:14:59 +0000101 error = OptionValue::SetValueFromString (value, op);
Greg Clayton67cc0632012-08-22 17:17:09 +0000102 break;
103 }
104 return error;
105}
106
107void
108OptionValueEnumeration::SetEnumerations (const OptionEnumValueElement *enumerators)
109{
110 m_enumerations.Clear();
111 if (enumerators)
112 {
Ed Masted78c9572014-04-20 00:31:37 +0000113 for (size_t i=0; enumerators[i].string_value != nullptr; ++i)
Greg Clayton67cc0632012-08-22 17:17:09 +0000114 {
115 ConstString const_enumerator_name(enumerators[i].string_value);
116 EnumeratorInfo enumerator_info = { enumerators[i].value, enumerators[i].usage };
117 m_enumerations.Append (const_enumerator_name.GetCString(), enumerator_info);
118 }
119 m_enumerations.Sort();
120 }
121}
122
123
124lldb::OptionValueSP
125OptionValueEnumeration::DeepCopy () const
126{
127 return OptionValueSP(new OptionValueEnumeration(*this));
128}
129
Greg Clayton754a9362012-08-23 00:22:02 +0000130size_t
131OptionValueEnumeration::AutoComplete (CommandInterpreter &interpreter,
132 const char *s,
133 int match_start_point,
134 int max_return_elements,
135 bool &word_complete,
136 StringList &matches)
137{
138 word_complete = false;
139 matches.Clear();
140
141 const uint32_t num_enumerators = m_enumerations.GetSize();
142 if (s && s[0])
143 {
144 const size_t s_len = strlen(s);
145 for (size_t i=0; i<num_enumerators; ++i)
146 {
147 const char *name = m_enumerations.GetCStringAtIndex(i);
148 if (::strncmp(s, name, s_len) == 0)
149 matches.AppendString(name);
150 }
151 }
152 else
153 {
154 // only suggest "true" or "false" by default
155 for (size_t i=0; i<num_enumerators; ++i)
156 matches.AppendString(m_enumerations.GetCStringAtIndex(i));
157 }
158 return matches.GetSize();
159}
160
161
162
163