| 1 | //===-- OptionValueUInt64.cpp ---------------------------------------------===// |
| 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/OptionValueUInt64.h" |
| 10 | |
| 11 | #include "lldb/Utility/Stream.h" |
| 12 | |
| 13 | using namespace lldb; |
| 14 | using namespace lldb_private; |
| 15 | |
| 16 | lldb::OptionValueSP OptionValueUInt64::Create(llvm::StringRef value_str, |
| 17 | Status &error) { |
| 18 | lldb::OptionValueSP value_sp(new OptionValueUInt64()); |
| 19 | error = value_sp->SetValueFromString(value: value_str); |
| 20 | if (error.Fail()) |
| 21 | value_sp.reset(); |
| 22 | return value_sp; |
| 23 | } |
| 24 | |
| 25 | void OptionValueUInt64::DumpValue(const ExecutionContext *exe_ctx, Stream &strm, |
| 26 | uint32_t dump_mask) { |
| 27 | if (dump_mask & eDumpOptionType) |
| 28 | strm.Printf(format: "(%s)" , GetTypeAsCString()); |
| 29 | if (dump_mask & eDumpOptionValue) { |
| 30 | if (dump_mask & eDumpOptionType) |
| 31 | strm.PutCString(cstr: " = " ); |
| 32 | strm.Printf(format: "%" PRIu64, m_current_value); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | Status OptionValueUInt64::SetValueFromString(llvm::StringRef value_ref, |
| 37 | VarSetOperationType op) { |
| 38 | Status error; |
| 39 | switch (op) { |
| 40 | case eVarSetOperationClear: |
| 41 | Clear(); |
| 42 | NotifyValueChanged(); |
| 43 | break; |
| 44 | |
| 45 | case eVarSetOperationReplace: |
| 46 | case eVarSetOperationAssign: { |
| 47 | llvm::StringRef value_trimmed = value_ref.trim(); |
| 48 | uint64_t value; |
| 49 | if (llvm::to_integer(S: value_trimmed, Num&: value)) { |
| 50 | if (value >= m_min_value && value <= m_max_value) { |
| 51 | m_value_was_set = true; |
| 52 | m_current_value = value; |
| 53 | NotifyValueChanged(); |
| 54 | } else { |
| 55 | error = Status::FromErrorStringWithFormat( |
| 56 | format: "%" PRIu64 " is out of range, valid values must be between %" PRIu64 |
| 57 | " and %" PRIu64 "." , |
| 58 | value, m_min_value, m_max_value); |
| 59 | } |
| 60 | } else { |
| 61 | error = Status::FromErrorStringWithFormat( |
| 62 | format: "invalid uint64_t string value: '%s'" , value_ref.str().c_str()); |
| 63 | } |
| 64 | } break; |
| 65 | |
| 66 | case eVarSetOperationInsertBefore: |
| 67 | case eVarSetOperationInsertAfter: |
| 68 | case eVarSetOperationRemove: |
| 69 | case eVarSetOperationAppend: |
| 70 | case eVarSetOperationInvalid: |
| 71 | error = OptionValue::SetValueFromString(value: value_ref, op); |
| 72 | break; |
| 73 | } |
| 74 | return error; |
| 75 | } |
| 76 | |