blob: 8a340792d78ff2a52578b2af3874229837affb26 [file] [log] [blame]
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001//===-- OptionValueBoolean.cpp ----------------------------------*- C++ -*-===//
Greg Clayton67cc0632012-08-22 17:17:09 +00002//
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
Zachary Turnerf343968f2016-08-09 23:06:08 +000016#include "lldb/Host/PosixApi.h"
Pavel Labath47cbf4a2018-04-10 09:03:59 +000017#include "lldb/Interpreter/OptionArgParser.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/Stream.h"
Zachary Turner573ab902017-03-21 18:25:04 +000019#include "lldb/Utility/StringList.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000020#include "llvm/ADT/STLExtras.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000021
22using namespace lldb;
23using namespace lldb_private;
24
Kate Stoneb9c1b512016-09-06 20:57:50 +000025void 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 Clayton67cc0632012-08-22 17:17:09 +000032 if (dump_mask & eDumpOptionType)
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 strm.PutCString(" = ");
34 strm.PutCString(m_current_value ? "true" : "false");
35 }
Greg Clayton67cc0632012-08-22 17:17:09 +000036}
37
Zachary Turner97206d52017-05-12 04:51:55 +000038Status OptionValueBoolean::SetValueFromString(llvm::StringRef value_str,
39 VarSetOperationType op) {
40 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 switch (op) {
42 case eVarSetOperationClear:
43 Clear();
44 NotifyValueChanged();
45 break;
Greg Clayton67cc0632012-08-22 17:17:09 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 case eVarSetOperationReplace:
48 case eVarSetOperationAssign: {
49 bool success = false;
Pavel Labath47cbf4a2018-04-10 09:03:59 +000050 bool value = OptionArgParser::ToBoolean(value_str, false, &success);
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 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 Clayton67cc0632012-08-22 17:17:09 +000061 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 } 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 Clayton67cc0632012-08-22 17:17:09 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075lldb::OptionValueSP OptionValueBoolean::DeepCopy() const {
76 return OptionValueSP(new OptionValueBoolean(*this));
Greg Clayton67cc0632012-08-22 17:17:09 +000077}
78
Raphael Isemanna2e76c02018-07-13 18:28:14 +000079size_t OptionValueBoolean::AutoComplete(CommandInterpreter &interpreter,
80 CompletionRequest &request) {
81 request.SetWordComplete(false);
82 request.GetMatches().Clear();
Zachary Turner4aa87532016-11-17 01:37:42 +000083 static const llvm::StringRef g_autocomplete_entries[] = {
84 "true", "false", "on", "off", "yes", "no", "1", "0"};
Kate Stoneb9c1b512016-09-06 20:57:50 +000085
Zachary Turner4aa87532016-11-17 01:37:42 +000086 auto entries = llvm::makeArrayRef(g_autocomplete_entries);
87
88 // only suggest "true" or "false" by default
Raphael Isemanna2e76c02018-07-13 18:28:14 +000089 if (request.GetCursorArgumentPrefix().empty())
Zachary Turner4aa87532016-11-17 01:37:42 +000090 entries = entries.take_front(2);
91
92 for (auto entry : entries) {
Raphael Isemanna2e76c02018-07-13 18:28:14 +000093 if (entry.startswith_lower(request.GetCursorArgumentPrefix()))
94 request.GetMatches().AppendString(entry);
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 }
Raphael Isemanna2e76c02018-07-13 18:28:14 +000096 return request.GetMatches().GetSize();
Greg Clayton754a9362012-08-23 00:22:02 +000097}