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/Host/PosixApi.h"
17 #include "lldb/Interpreter/OptionArgParser.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/StringList.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 Status OptionValueBoolean::SetValueFromString(llvm::StringRef value_str,
39                                               VarSetOperationType op) {
40   Status 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 = OptionArgParser::ToBoolean(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(CommandInterpreter &interpreter,
80                                         CompletionRequest &request) {
81   request.SetWordComplete(false);
82   static const llvm::StringRef g_autocomplete_entries[] = {
83       "true", "false", "on", "off", "yes", "no", "1", "0"};
84 
85   auto entries = llvm::makeArrayRef(g_autocomplete_entries);
86 
87   // only suggest "true" or "false" by default
88   if (request.GetCursorArgumentPrefix().empty())
89     entries = entries.take_front(2);
90 
91   for (auto entry : entries) {
92     if (entry.startswith_lower(request.GetCursorArgumentPrefix()))
93       request.AddCompletion(entry);
94   }
95   return request.GetNumberOfMatches();
96 }
97