Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 1 | //===-- OptionValueBoolean.cpp ----------------------------------*- C++ -*-===// |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 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/OptionValueBoolean.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
| 16 | #include "lldb/Core/Stream.h" |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 17 | #include "lldb/Core/StringList.h" |
Zachary Turner | f343968f | 2016-08-09 23:06:08 +0000 | [diff] [blame] | 18 | #include "lldb/Host/PosixApi.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 19 | #include "lldb/Interpreter/Args.h" |
Saleem Abdulrasool | 2860695 | 2014-06-27 05:17:41 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | void OptionValueBoolean::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 & eDumpOptionName) |
| 30 | // DumpQualifiedName (strm); |
| 31 | if (dump_mask & eDumpOptionValue) { |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 32 | if (dump_mask & eDumpOptionType) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | strm.PutCString(" = "); |
| 34 | strm.PutCString(m_current_value ? "true" : "false"); |
| 35 | } |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | Error OptionValueBoolean::SetValueFromString(llvm::StringRef value_str, |
| 39 | VarSetOperationType op) { |
| 40 | Error error; |
| 41 | switch (op) { |
| 42 | case eVarSetOperationClear: |
| 43 | Clear(); |
| 44 | NotifyValueChanged(); |
| 45 | break; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | case eVarSetOperationReplace: |
| 48 | case eVarSetOperationAssign: { |
| 49 | bool success = false; |
Zachary Turner | ecbb0bb | 2016-09-19 17:54:06 +0000 | [diff] [blame] | 50 | bool value = Args::StringToBoolean(value_str, false, &success); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | if (success) { |
| 52 | m_value_was_set = true; |
| 53 | m_current_value = value; |
| 54 | NotifyValueChanged(); |
| 55 | } else { |
| 56 | if (value_str.size() == 0) |
| 57 | error.SetErrorString("invalid boolean string value <empty>"); |
| 58 | else |
| 59 | error.SetErrorStringWithFormat("invalid boolean string value: '%s'", |
| 60 | value_str.str().c_str()); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 61 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | } break; |
| 63 | |
| 64 | case eVarSetOperationInsertBefore: |
| 65 | case eVarSetOperationInsertAfter: |
| 66 | case eVarSetOperationRemove: |
| 67 | case eVarSetOperationAppend: |
| 68 | case eVarSetOperationInvalid: |
| 69 | error = OptionValue::SetValueFromString(value_str, op); |
| 70 | break; |
| 71 | } |
| 72 | return error; |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | lldb::OptionValueSP OptionValueBoolean::DeepCopy() const { |
| 76 | return OptionValueSP(new OptionValueBoolean(*this)); |
Greg Clayton | 67cc063 | 2012-08-22 17:17:09 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Zachary Turner | 4aa8753 | 2016-11-17 01:37:42 +0000 | [diff] [blame] | 79 | size_t OptionValueBoolean::AutoComplete( |
| 80 | CommandInterpreter &interpreter, llvm::StringRef s, int match_start_point, |
| 81 | int max_return_elements, bool &word_complete, StringList &matches) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 82 | word_complete = false; |
| 83 | matches.Clear(); |
Zachary Turner | 4aa8753 | 2016-11-17 01:37:42 +0000 | [diff] [blame] | 84 | static const llvm::StringRef g_autocomplete_entries[] = { |
| 85 | "true", "false", "on", "off", "yes", "no", "1", "0"}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | |
Zachary Turner | 4aa8753 | 2016-11-17 01:37:42 +0000 | [diff] [blame] | 87 | auto entries = llvm::makeArrayRef(g_autocomplete_entries); |
| 88 | |
| 89 | // only suggest "true" or "false" by default |
| 90 | if (s.empty()) |
| 91 | entries = entries.take_front(2); |
| 92 | |
| 93 | for (auto entry : entries) { |
| 94 | if (entry.startswith_lower(s)) |
| 95 | matches.AppendString(entry); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | } |
| 97 | return matches.GetSize(); |
Greg Clayton | 754a936 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 98 | } |