180814287SRaphael Isemann //===-- OptionValueUInt64.cpp ---------------------------------------------===//
267cc0636SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
667cc0636SGreg Clayton //
767cc0636SGreg Clayton //===----------------------------------------------------------------------===//
867cc0636SGreg Clayton 
967cc0636SGreg Clayton #include "lldb/Interpreter/OptionValueUInt64.h"
1067cc0636SGreg Clayton 
11bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
1267cc0636SGreg Clayton 
1367cc0636SGreg Clayton using namespace lldb;
1467cc0636SGreg Clayton using namespace lldb_private;
1567cc0636SGreg Clayton 
Create(llvm::StringRef value_str,Status & error)168cef4b0bSZachary Turner lldb::OptionValueSP OptionValueUInt64::Create(llvm::StringRef value_str,
1797206d57SZachary Turner                                               Status &error) {
1867cc0636SGreg Clayton   lldb::OptionValueSP value_sp(new OptionValueUInt64());
198cef4b0bSZachary Turner   error = value_sp->SetValueFromString(value_str);
2067cc0636SGreg Clayton   if (error.Fail())
2167cc0636SGreg Clayton     value_sp.reset();
2267cc0636SGreg Clayton   return value_sp;
2367cc0636SGreg Clayton }
2467cc0636SGreg Clayton 
DumpValue(const ExecutionContext * exe_ctx,Stream & strm,uint32_t dump_mask)25b9c1b51eSKate Stone void OptionValueUInt64::DumpValue(const ExecutionContext *exe_ctx, Stream &strm,
26b9c1b51eSKate Stone                                   uint32_t dump_mask) {
2767cc0636SGreg Clayton   if (dump_mask & eDumpOptionType)
2867cc0636SGreg Clayton     strm.Printf("(%s)", GetTypeAsCString());
29b9c1b51eSKate Stone   if (dump_mask & eDumpOptionValue) {
3067cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
3167cc0636SGreg Clayton       strm.PutCString(" = ");
32d01b2953SDaniel Malea     strm.Printf("%" PRIu64, m_current_value);
3367cc0636SGreg Clayton   }
3467cc0636SGreg Clayton }
3567cc0636SGreg Clayton 
SetValueFromString(llvm::StringRef value_ref,VarSetOperationType op)3697206d57SZachary Turner Status OptionValueUInt64::SetValueFromString(llvm::StringRef value_ref,
37b9c1b51eSKate Stone                                              VarSetOperationType op) {
3897206d57SZachary Turner   Status error;
39b9c1b51eSKate Stone   switch (op) {
4067cc0636SGreg Clayton   case eVarSetOperationClear:
4167cc0636SGreg Clayton     Clear();
42332e8b1cSGreg Clayton     NotifyValueChanged();
4367cc0636SGreg Clayton     break;
4467cc0636SGreg Clayton 
4567cc0636SGreg Clayton   case eVarSetOperationReplace:
46b9c1b51eSKate Stone   case eVarSetOperationAssign: {
47*3a6ba367SMichał Górny     llvm::StringRef value_trimmed = value_ref.trim();
48*3a6ba367SMichał Górny     uint64_t value;
49*3a6ba367SMichał Górny     if (llvm::to_integer(value_trimmed, value)) {
5067cc0636SGreg Clayton       m_value_was_set = true;
5167cc0636SGreg Clayton       m_current_value = value;
52332e8b1cSGreg Clayton       NotifyValueChanged();
53b9c1b51eSKate Stone     } else {
54b9c1b51eSKate Stone       error.SetErrorStringWithFormat("invalid uint64_t string value: '%s'",
55*3a6ba367SMichał Górny                                      value_ref.str().c_str());
5667cc0636SGreg Clayton     }
57b9c1b51eSKate Stone   } break;
5867cc0636SGreg Clayton 
5967cc0636SGreg Clayton   case eVarSetOperationInsertBefore:
6067cc0636SGreg Clayton   case eVarSetOperationInsertAfter:
6167cc0636SGreg Clayton   case eVarSetOperationRemove:
6267cc0636SGreg Clayton   case eVarSetOperationAppend:
6367cc0636SGreg Clayton   case eVarSetOperationInvalid:
64c95f7e2aSPavel Labath     error = OptionValue::SetValueFromString(value_ref, op);
6567cc0636SGreg Clayton     break;
6667cc0636SGreg Clayton   }
6767cc0636SGreg Clayton   return error;
6867cc0636SGreg Clayton }
69