1 //===-- OptionValueBoolean.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/OptionValueBoolean.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Core/Stream.h" 17 #include "lldb/Core/StringList.h" 18 #include "lldb/Host/PosixApi.h" 19 #include "lldb/Interpreter/Args.h" 20 #include "llvm/ADT/STLExtras.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 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) { 32 if (dump_mask & eDumpOptionType) 33 strm.PutCString(" = "); 34 strm.PutCString(m_current_value ? "true" : "false"); 35 } 36 } 37 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; 46 47 case eVarSetOperationReplace: 48 case eVarSetOperationAssign: { 49 bool success = false; 50 bool value = Args::StringToBoolean(value_str, false, &success); 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()); 61 } 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; 73 } 74 75 lldb::OptionValueSP OptionValueBoolean::DeepCopy() const { 76 return OptionValueSP(new OptionValueBoolean(*this)); 77 } 78 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) { 82 word_complete = false; 83 matches.Clear(); 84 static const llvm::StringRef g_autocomplete_entries[] = { 85 "true", "false", "on", "off", "yes", "no", "1", "0"}; 86 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); 96 } 97 return matches.GetSize(); 98 } 99