Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 1 | //===-- OptionValueEnumeration.cpp ------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Interpreter/OptionValueEnumeration.h" |
| 10 | |
Zachary Turner | 573ab90 | 2017-03-21 18:25:04 +0000 | [diff] [blame] | 11 | #include "lldb/Utility/StringList.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace lldb; |
| 14 | using namespace lldb_private; |
| 15 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 16 | OptionValueEnumeration::OptionValueEnumeration( |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 17 | const OptionEnumValues &enumerators, enum_type value) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | : OptionValue(), m_current_value(value), m_default_value(value), |
| 19 | m_enumerations() { |
| 20 | SetEnumerations(enumerators); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 21 | } |
| 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | OptionValueEnumeration::~OptionValueEnumeration() {} |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | void OptionValueEnumeration::DumpValue(const ExecutionContext *exe_ctx, |
| 26 | Stream &strm, uint32_t dump_mask) { |
| 27 | if (dump_mask & eDumpOptionType) |
| 28 | strm.Printf("(%s)", GetTypeAsCString()); |
| 29 | if (dump_mask & eDumpOptionValue) { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 30 | if (dump_mask & eDumpOptionType) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | strm.PutCString(" = "); |
| 32 | const size_t count = m_enumerations.GetSize(); |
| 33 | for (size_t i = 0; i < count; ++i) { |
| 34 | if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value) { |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 35 | strm.PutCString(m_enumerations.GetCStringAtIndex(i).GetStringRef()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | return; |
| 37 | } |
| 38 | } |
| 39 | strm.Printf("%" PRIu64, (uint64_t)m_current_value); |
| 40 | } |
| 41 | } |
| 42 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 43 | Status OptionValueEnumeration::SetValueFromString(llvm::StringRef value, |
| 44 | VarSetOperationType op) { |
| 45 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | switch (op) { |
| 47 | case eVarSetOperationClear: |
| 48 | Clear(); |
| 49 | NotifyValueChanged(); |
| 50 | break; |
| 51 | |
| 52 | case eVarSetOperationReplace: |
| 53 | case eVarSetOperationAssign: { |
| 54 | ConstString const_enumerator_name(value.trim()); |
| 55 | const EnumerationMapEntry *enumerator_entry = |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 56 | m_enumerations.FindFirstValueForName(const_enumerator_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | if (enumerator_entry) { |
| 58 | m_current_value = enumerator_entry->value.value; |
| 59 | NotifyValueChanged(); |
| 60 | } else { |
| 61 | StreamString error_strm; |
| 62 | error_strm.Printf("invalid enumeration value '%s'", value.str().c_str()); |
| 63 | const size_t count = m_enumerations.GetSize(); |
| 64 | if (count) { |
| 65 | error_strm.Printf(", valid values are: %s", |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 66 | m_enumerations.GetCStringAtIndex(0).GetCString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | for (size_t i = 1; i < count; ++i) { |
Zachary Turner | 4fa098a | 2016-10-06 21:22:44 +0000 | [diff] [blame] | 68 | error_strm.Printf(", %s", |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 69 | m_enumerations.GetCStringAtIndex(i).GetCString()); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 70 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | } |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 72 | error.SetErrorString(error_strm.GetString()); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 73 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | break; |
| 75 | } |
| 76 | |
| 77 | case eVarSetOperationInsertBefore: |
| 78 | case eVarSetOperationInsertAfter: |
| 79 | case eVarSetOperationRemove: |
| 80 | case eVarSetOperationAppend: |
| 81 | case eVarSetOperationInvalid: |
| 82 | error = OptionValue::SetValueFromString(value, op); |
| 83 | break; |
| 84 | } |
| 85 | return error; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 88 | void OptionValueEnumeration::SetEnumerations( |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 89 | const OptionEnumValues &enumerators) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 90 | m_enumerations.Clear(); |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 91 | |
| 92 | for (const auto &enumerator : enumerators) { |
| 93 | ConstString const_enumerator_name(enumerator.string_value); |
| 94 | EnumeratorInfo enumerator_info = {enumerator.value, enumerator.usage}; |
| 95 | m_enumerations.Append(const_enumerator_name, enumerator_info); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | } |
Tatyana Krasnukha | 8fe53c49 | 2018-09-26 18:50:19 +0000 | [diff] [blame] | 97 | |
| 98 | m_enumerations.Sort(); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | lldb::OptionValueSP OptionValueEnumeration::DeepCopy() const { |
| 102 | return OptionValueSP(new OptionValueEnumeration(*this)); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame] | 105 | size_t OptionValueEnumeration::AutoComplete(CommandInterpreter &interpreter, |
| 106 | CompletionRequest &request) { |
| 107 | request.SetWordComplete(false); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | const uint32_t num_enumerators = m_enumerations.GetSize(); |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame] | 110 | if (!request.GetCursorArgumentPrefix().empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | for (size_t i = 0; i < num_enumerators; ++i) { |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 112 | llvm::StringRef name = m_enumerations.GetCStringAtIndex(i).GetStringRef(); |
Raphael Isemann | a2e76c0 | 2018-07-13 18:28:14 +0000 | [diff] [blame] | 113 | if (name.startswith(request.GetCursorArgumentPrefix())) |
Raphael Isemann | 1a6d7ab | 2018-07-27 18:42:46 +0000 | [diff] [blame] | 114 | request.AddCompletion(name); |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 115 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | } else { |
| 117 | // only suggest "true" or "false" by default |
| 118 | for (size_t i = 0; i < num_enumerators; ++i) |
Raphael Isemann | 1a6d7ab | 2018-07-27 18:42:46 +0000 | [diff] [blame] | 119 | request.AddCompletion(m_enumerations.GetCStringAtIndex(i).GetStringRef()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | } |
Raphael Isemann | 1a6d7ab | 2018-07-27 18:42:46 +0000 | [diff] [blame] | 121 | return request.GetNumberOfMatches(); |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 122 | } |