Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 16 | #include "lldb/Core/StringList.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace lldb; |
| 19 | using namespace lldb_private; |
| 20 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 21 | OptionValueEnumeration::OptionValueEnumeration( |
| 22 | const OptionEnumValueElement *enumerators, enum_type value) |
| 23 | : OptionValue(), m_current_value(value), m_default_value(value), |
| 24 | m_enumerations() { |
| 25 | SetEnumerations(enumerators); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | OptionValueEnumeration::~OptionValueEnumeration() {} |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 30 | void OptionValueEnumeration::DumpValue(const ExecutionContext *exe_ctx, |
| 31 | Stream &strm, uint32_t dump_mask) { |
| 32 | if (dump_mask & eDumpOptionType) |
| 33 | strm.Printf("(%s)", GetTypeAsCString()); |
| 34 | if (dump_mask & eDumpOptionValue) { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 35 | if (dump_mask & eDumpOptionType) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | strm.PutCString(" = "); |
| 37 | const size_t count = m_enumerations.GetSize(); |
| 38 | for (size_t i = 0; i < count; ++i) { |
| 39 | if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value) { |
| 40 | strm.PutCString(m_enumerations.GetCStringAtIndex(i)); |
| 41 | return; |
| 42 | } |
| 43 | } |
| 44 | strm.Printf("%" PRIu64, (uint64_t)m_current_value); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | Error OptionValueEnumeration::SetValueFromString(llvm::StringRef value, |
| 49 | VarSetOperationType op) { |
| 50 | Error error; |
| 51 | switch (op) { |
| 52 | case eVarSetOperationClear: |
| 53 | Clear(); |
| 54 | NotifyValueChanged(); |
| 55 | break; |
| 56 | |
| 57 | case eVarSetOperationReplace: |
| 58 | case eVarSetOperationAssign: { |
| 59 | ConstString const_enumerator_name(value.trim()); |
| 60 | const EnumerationMapEntry *enumerator_entry = |
| 61 | m_enumerations.FindFirstValueForName( |
| 62 | const_enumerator_name.GetCString()); |
| 63 | if (enumerator_entry) { |
| 64 | m_current_value = enumerator_entry->value.value; |
| 65 | NotifyValueChanged(); |
| 66 | } else { |
| 67 | StreamString error_strm; |
| 68 | error_strm.Printf("invalid enumeration value '%s'", value.str().c_str()); |
| 69 | const size_t count = m_enumerations.GetSize(); |
| 70 | if (count) { |
| 71 | error_strm.Printf(", valid values are: %s", |
| 72 | m_enumerations.GetCStringAtIndex(0)); |
| 73 | for (size_t i = 1; i < count; ++i) { |
| 74 | error_strm.Printf(", %s", m_enumerations.GetCStringAtIndex(i)); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 75 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | } |
| 77 | error.SetErrorString(error_strm.GetData()); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 78 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | |
| 82 | case eVarSetOperationInsertBefore: |
| 83 | case eVarSetOperationInsertAfter: |
| 84 | case eVarSetOperationRemove: |
| 85 | case eVarSetOperationAppend: |
| 86 | case eVarSetOperationInvalid: |
| 87 | error = OptionValue::SetValueFromString(value, op); |
| 88 | break; |
| 89 | } |
| 90 | return error; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | void OptionValueEnumeration::SetEnumerations( |
| 94 | const OptionEnumValueElement *enumerators) { |
| 95 | m_enumerations.Clear(); |
| 96 | if (enumerators) { |
| 97 | for (size_t i = 0; enumerators[i].string_value != nullptr; ++i) { |
| 98 | ConstString const_enumerator_name(enumerators[i].string_value); |
| 99 | EnumeratorInfo enumerator_info = {enumerators[i].value, |
| 100 | enumerators[i].usage}; |
| 101 | m_enumerations.Append(const_enumerator_name.GetCString(), |
| 102 | enumerator_info); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 103 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | m_enumerations.Sort(); |
| 105 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | lldb::OptionValueSP OptionValueEnumeration::DeepCopy() const { |
| 109 | return OptionValueSP(new OptionValueEnumeration(*this)); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | size_t OptionValueEnumeration::AutoComplete( |
| 113 | CommandInterpreter &interpreter, const char *s, int match_start_point, |
| 114 | int max_return_elements, bool &word_complete, StringList &matches) { |
| 115 | word_complete = false; |
| 116 | matches.Clear(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | const uint32_t num_enumerators = m_enumerations.GetSize(); |
| 119 | if (s && s[0]) { |
| 120 | const size_t s_len = strlen(s); |
| 121 | for (size_t i = 0; i < num_enumerators; ++i) { |
| 122 | const char *name = m_enumerations.GetCStringAtIndex(i); |
| 123 | if (::strncmp(s, name, s_len) == 0) |
| 124 | matches.AppendString(name); |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 125 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | } else { |
| 127 | // only suggest "true" or "false" by default |
| 128 | for (size_t i = 0; i < num_enumerators; ++i) |
| 129 | matches.AppendString(m_enumerations.GetCStringAtIndex(i)); |
| 130 | } |
| 131 | return matches.GetSize(); |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 132 | } |