OptionValueBoolean.cpp revision 360784
1//===-- OptionValueBoolean.cpp ----------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Interpreter/OptionValueBoolean.h"
10
11#include "lldb/Host/PosixApi.h"
12#include "lldb/Interpreter/OptionArgParser.h"
13#include "lldb/Utility/Stream.h"
14#include "lldb/Utility/StringList.h"
15#include "llvm/ADT/STLExtras.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
20void OptionValueBoolean::DumpValue(const ExecutionContext *exe_ctx,
21                                   Stream &strm, uint32_t dump_mask) {
22  if (dump_mask & eDumpOptionType)
23    strm.Printf("(%s)", GetTypeAsCString());
24  //    if (dump_mask & eDumpOptionName)
25  //        DumpQualifiedName (strm);
26  if (dump_mask & eDumpOptionValue) {
27    if (dump_mask & eDumpOptionType)
28      strm.PutCString(" = ");
29    strm.PutCString(m_current_value ? "true" : "false");
30  }
31}
32
33Status OptionValueBoolean::SetValueFromString(llvm::StringRef value_str,
34                                              VarSetOperationType op) {
35  Status error;
36  switch (op) {
37  case eVarSetOperationClear:
38    Clear();
39    NotifyValueChanged();
40    break;
41
42  case eVarSetOperationReplace:
43  case eVarSetOperationAssign: {
44    bool success = false;
45    bool value = OptionArgParser::ToBoolean(value_str, false, &success);
46    if (success) {
47      m_value_was_set = true;
48      m_current_value = value;
49      NotifyValueChanged();
50    } else {
51      if (value_str.size() == 0)
52        error.SetErrorString("invalid boolean string value <empty>");
53      else
54        error.SetErrorStringWithFormat("invalid boolean string value: '%s'",
55                                       value_str.str().c_str());
56    }
57  } break;
58
59  case eVarSetOperationInsertBefore:
60  case eVarSetOperationInsertAfter:
61  case eVarSetOperationRemove:
62  case eVarSetOperationAppend:
63  case eVarSetOperationInvalid:
64    error = OptionValue::SetValueFromString(value_str, op);
65    break;
66  }
67  return error;
68}
69
70lldb::OptionValueSP OptionValueBoolean::DeepCopy() const {
71  return OptionValueSP(new OptionValueBoolean(*this));
72}
73
74void OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter,
75                                      CompletionRequest &request) {
76  llvm::StringRef autocomplete_entries[] = {"true", "false", "on", "off",
77                                            "yes",  "no",    "1",  "0"};
78
79  auto entries = llvm::makeArrayRef(autocomplete_entries);
80
81  // only suggest "true" or "false" by default
82  if (request.GetCursorArgumentPrefix().empty())
83    entries = entries.take_front(2);
84
85  for (auto entry : entries)
86    request.TryCompleteCurrentArg(entry);
87}
88