xref: /src/contrib/llvm-project/lldb/source/Interpreter/OptionValueBoolean.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1cfca06d7SDimitry Andric //===-- OptionValueBoolean.cpp --------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "lldb/Interpreter/OptionValueBoolean.h"
10f034231aSEd Maste 
1114f1b3e8SDimitry Andric #include "lldb/Host/PosixApi.h"
12f73363f1SDimitry Andric #include "lldb/Interpreter/OptionArgParser.h"
1374a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
1474a628f7SDimitry Andric #include "lldb/Utility/StringList.h"
150cac4ca3SEd Maste #include "llvm/ADT/STLExtras.h"
16f034231aSEd Maste 
17f034231aSEd Maste using namespace lldb;
18f034231aSEd Maste using namespace lldb_private;
19f034231aSEd Maste 
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)2014f1b3e8SDimitry Andric void OptionValueBoolean::DumpValue(const ExecutionContext *exe_ctx,
2114f1b3e8SDimitry Andric                                    Stream &strm, uint32_t dump_mask) {
22f034231aSEd Maste   if (dump_mask & eDumpOptionType)
23f034231aSEd Maste     strm.Printf("(%s)", GetTypeAsCString());
24f034231aSEd Maste   //    if (dump_mask & eDumpOptionName)
25f034231aSEd Maste   //        DumpQualifiedName (strm);
2614f1b3e8SDimitry Andric   if (dump_mask & eDumpOptionValue) {
27f034231aSEd Maste     if (dump_mask & eDumpOptionType)
28f034231aSEd Maste       strm.PutCString(" = ");
29f034231aSEd Maste     strm.PutCString(m_current_value ? "true" : "false");
30f034231aSEd Maste   }
31f034231aSEd Maste }
32f034231aSEd Maste 
SetValueFromString(llvm::StringRef value_str,VarSetOperationType op)33b76161e4SDimitry Andric Status OptionValueBoolean::SetValueFromString(llvm::StringRef value_str,
3414f1b3e8SDimitry Andric                                               VarSetOperationType op) {
35b76161e4SDimitry Andric   Status error;
3614f1b3e8SDimitry Andric   switch (op) {
37f034231aSEd Maste   case eVarSetOperationClear:
38f034231aSEd Maste     Clear();
39205afe67SEd Maste     NotifyValueChanged();
40f034231aSEd Maste     break;
41f034231aSEd Maste 
42f034231aSEd Maste   case eVarSetOperationReplace:
4314f1b3e8SDimitry Andric   case eVarSetOperationAssign: {
44f034231aSEd Maste     bool success = false;
45f73363f1SDimitry Andric     bool value = OptionArgParser::ToBoolean(value_str, false, &success);
4614f1b3e8SDimitry Andric     if (success) {
47f034231aSEd Maste       m_value_was_set = true;
48f034231aSEd Maste       m_current_value = value;
49205afe67SEd Maste       NotifyValueChanged();
5014f1b3e8SDimitry Andric     } else {
515e95aa85SEd Maste       if (value_str.size() == 0)
52f034231aSEd Maste         error.SetErrorString("invalid boolean string value <empty>");
53f034231aSEd Maste       else
545e95aa85SEd Maste         error.SetErrorStringWithFormat("invalid boolean string value: '%s'",
555e95aa85SEd Maste                                        value_str.str().c_str());
56f034231aSEd Maste     }
5714f1b3e8SDimitry Andric   } break;
58f034231aSEd Maste 
59f034231aSEd Maste   case eVarSetOperationInsertBefore:
60f034231aSEd Maste   case eVarSetOperationInsertAfter:
61f034231aSEd Maste   case eVarSetOperationRemove:
62f034231aSEd Maste   case eVarSetOperationAppend:
63f034231aSEd Maste   case eVarSetOperationInvalid:
645e95aa85SEd Maste     error = OptionValue::SetValueFromString(value_str, op);
65f034231aSEd Maste     break;
66f034231aSEd Maste   }
67f034231aSEd Maste   return error;
68f034231aSEd Maste }
69f034231aSEd Maste 
AutoComplete(CommandInterpreter & interpreter,CompletionRequest & request)70ead24645SDimitry Andric void OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter,
71f73363f1SDimitry Andric                                       CompletionRequest &request) {
72ead24645SDimitry Andric   llvm::StringRef autocomplete_entries[] = {"true", "false", "on", "off",
73ead24645SDimitry Andric                                             "yes",  "no",    "1",  "0"};
74f034231aSEd Maste 
75e3b55780SDimitry Andric   auto entries = llvm::ArrayRef(autocomplete_entries);
7614f1b3e8SDimitry Andric 
77f034231aSEd Maste   // only suggest "true" or "false" by default
78f73363f1SDimitry Andric   if (request.GetCursorArgumentPrefix().empty())
7914f1b3e8SDimitry Andric     entries = entries.take_front(2);
8014f1b3e8SDimitry Andric 
81ead24645SDimitry Andric   for (auto entry : entries)
82ead24645SDimitry Andric     request.TryCompleteCurrentArg(entry);
83f034231aSEd Maste }
84