From: Zachary Turner Date: Thu, 17 Nov 2016 23:32:26 +0000 (+0000) Subject: Revert "Change RegisterValue getters / setters to use StringRef." X-Git-Tag: llvmorg-4.0.0-rc1~4249 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8df8046bd9ef12e908adf7c2522142b62232e4b5;p=platform%2Fupstream%2Fllvm.git Revert "Change RegisterValue getters / setters to use StringRef." This reverts commit r287279, which breaks some register tests on Linux. llvm-svn: 287281 --- diff --git a/lldb/include/lldb/Core/RegisterValue.h b/lldb/include/lldb/Core/RegisterValue.h index 78dfbea..fc9033d 100644 --- a/lldb/include/lldb/Core/RegisterValue.h +++ b/lldb/include/lldb/Core/RegisterValue.h @@ -233,10 +233,8 @@ public: bool SignExtend(uint32_t sign_bitpos); - Error SetValueFromString(const RegisterInfo *reg_info, - llvm::StringRef value_str); - Error SetValueFromString(const RegisterInfo *reg_info, - const char *value_str) = delete; + Error SetValueFromCString(const RegisterInfo *reg_info, + const char *value_str); Error SetValueFromData(const RegisterInfo *reg_info, DataExtractor &data, lldb::offset_t offset, bool partial_data_ok); diff --git a/lldb/source/Commands/CommandObjectRegister.cpp b/lldb/source/Commands/CommandObjectRegister.cpp index 8d7afbd..e5e8089 100644 --- a/lldb/source/Commands/CommandObjectRegister.cpp +++ b/lldb/source/Commands/CommandObjectRegister.cpp @@ -356,7 +356,7 @@ protected: result.SetStatus(eReturnStatusFailed); } else { const char *reg_name = command.GetArgumentAtIndex(0); - llvm::StringRef value_str = command.GetArgumentAtIndex(1); + const char *value_str = command.GetArgumentAtIndex(1); // in most LLDB commands we accept $rbx as the name for register RBX - and // here we would @@ -373,7 +373,7 @@ protected: if (reg_info) { RegisterValue reg_value; - Error error(reg_value.SetValueFromString(reg_info, value_str)); + Error error(reg_value.SetValueFromCString(reg_info, value_str)); if (error.Success()) { if (reg_ctx->WriteRegister(reg_info, reg_value)) { // Toss all frames and anything else in the thread @@ -386,11 +386,11 @@ protected: if (error.AsCString()) { result.AppendErrorWithFormat( "Failed to write register '%s' with value '%s': %s\n", reg_name, - value_str.str().c_str(), error.AsCString()); + value_str, error.AsCString()); } else { result.AppendErrorWithFormat( "Failed to write register '%s' with value '%s'", reg_name, - value_str.str().c_str()); + value_str); } result.SetStatus(eReturnStatusFailed); } else { diff --git a/lldb/source/Core/RegisterValue.cpp b/lldb/source/Core/RegisterValue.cpp index f1cfa4b..62d8305 100644 --- a/lldb/source/Core/RegisterValue.cpp +++ b/lldb/source/Core/RegisterValue.cpp @@ -347,35 +347,51 @@ Error RegisterValue::SetValueFromData(const RegisterInfo *reg_info, return error; } -// Helper function for RegisterValue::SetValueFromString() +static inline void StripSpaces(llvm::StringRef &Str) { + while (!Str.empty() && isspace(Str[0])) + Str = Str.substr(1); + while (!Str.empty() && isspace(Str.back())) + Str = Str.substr(0, Str.size() - 1); +} + +static inline void LStrip(llvm::StringRef &Str, char c) { + if (!Str.empty() && Str.front() == c) + Str = Str.substr(1); +} + +static inline void RStrip(llvm::StringRef &Str, char c) { + if (!Str.empty() && Str.back() == c) + Str = Str.substr(0, Str.size() - 1); +} + +// Helper function for RegisterValue::SetValueFromCString() static bool ParseVectorEncoding(const RegisterInfo *reg_info, - llvm::StringRef vector_str, + const char *vector_str, const uint32_t byte_size, RegisterValue *reg_value) { // Example: vector_str = "{0x2c 0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a // 0x2a 0x3e 0x84 0x4f 0x2a 0x3e}". - vector_str = vector_str.trim(); - vector_str.consume_front("{"); - vector_str.consume_back("}"); - vector_str = vector_str.trim(); + llvm::StringRef Str(vector_str); + StripSpaces(Str); + LStrip(Str, '{'); + RStrip(Str, '}'); + StripSpaces(Str); char Sep = ' '; // The first split should give us: // ('0x2c', '0x4b 0x2a 0x3e 0xd0 0x4f 0x2a 0x3e 0xac 0x4a 0x2a 0x3e 0x84 0x4f // 0x2a 0x3e'). - llvm::StringRef car; - llvm::StringRef cdr = vector_str; - std::tie(car, cdr) = vector_str.split(cdr); + std::pair Pair = Str.split(Sep); std::vector bytes; unsigned byte = 0; // Using radix auto-sensing by passing 0 as the radix. // Keep on processing the vector elements as long as the parsing succeeds and // the vector size is < byte_size. - while (!car.getAsInteger(0, byte) && bytes.size() < byte_size) { + while (!Pair.first.getAsInteger(0, byte) && bytes.size() < byte_size) { bytes.push_back(byte); - std::tie(car, cdr) = cdr.split(Sep); + Pair = Pair.second.split(Sep); } // Check for vector of exact byte_size elements. @@ -386,129 +402,112 @@ static bool ParseVectorEncoding(const RegisterInfo *reg_info, return true; } -Error RegisterValue::SetValueFromString(const RegisterInfo *reg_info, - llvm::StringRef value_str) { +Error RegisterValue::SetValueFromCString(const RegisterInfo *reg_info, + const char *value_str) { Error error; if (reg_info == nullptr) { error.SetErrorString("Invalid register info argument."); return error; } - m_type = eTypeInvalid; - if (value_str.empty()) { + if (value_str == nullptr || value_str[0] == '\0') { error.SetErrorString("Invalid c-string value string."); return error; } + bool success = false; const uint32_t byte_size = reg_info->byte_size; - - uint64_t uval64; - int64_t ival64; - float flt_val; - double dbl_val; - long double ldbl_val; + static float flt_val; + static double dbl_val; + static long double ldbl_val; switch (reg_info->encoding) { case eEncodingInvalid: error.SetErrorString("Invalid encoding."); break; case eEncodingUint: - if (byte_size > sizeof(uint64_t)) { - error.SetErrorStringWithFormat( - "unsupported unsigned integer byte size: %u", byte_size); - break; - } - if (value_str.getAsInteger(0, uval64)) { - error.SetErrorStringWithFormat( - "'%s' is not a valid unsigned integer string value", - value_str.str().c_str()); - break; - } - - if (!Args::UInt64ValueIsValidForByteSize(uval64, byte_size)) { - error.SetErrorStringWithFormat( - "value 0x%" PRIx64 - " is too large to fit in a %u byte unsigned integer value", - uval64, byte_size); - break; - } - - if (!SetUInt(uval64, reg_info->byte_size)) { + if (byte_size <= sizeof(uint64_t)) { + uint64_t uval64 = + StringConvert::ToUInt64(value_str, UINT64_MAX, 0, &success); + if (!success) + error.SetErrorStringWithFormat( + "'%s' is not a valid unsigned integer string value", value_str); + else if (!Args::UInt64ValueIsValidForByteSize(uval64, byte_size)) + error.SetErrorStringWithFormat( + "value 0x%" PRIx64 + " is too large to fit in a %u byte unsigned integer value", + uval64, byte_size); + else { + if (!SetUInt(uval64, reg_info->byte_size)) + error.SetErrorStringWithFormat( + "unsupported unsigned integer byte size: %u", byte_size); + } + } else { error.SetErrorStringWithFormat( "unsupported unsigned integer byte size: %u", byte_size); - break; + return error; } - // TODO: Shouldn't we be setting m_type here? break; case eEncodingSint: - if (byte_size > sizeof(long long)) { - error.SetErrorStringWithFormat("unsupported signed integer byte size: %u", - byte_size); - break; - } - - if (value_str.getAsInteger(0, ival64)) { - error.SetErrorStringWithFormat( - "'%s' is not a valid signed integer string value", - value_str.str().c_str()); - break; - } - - if (!Args::SInt64ValueIsValidForByteSize(ival64, byte_size)) { - error.SetErrorStringWithFormat( - "value 0x%" PRIx64 - " is too large to fit in a %u byte signed integer value", - ival64, byte_size); - break; - } - - if (!SetUInt(ival64, reg_info->byte_size)) { + if (byte_size <= sizeof(long long)) { + uint64_t sval64 = + StringConvert::ToSInt64(value_str, INT64_MAX, 0, &success); + if (!success) + error.SetErrorStringWithFormat( + "'%s' is not a valid signed integer string value", value_str); + else if (!Args::SInt64ValueIsValidForByteSize(sval64, byte_size)) + error.SetErrorStringWithFormat( + "value 0x%" PRIx64 + " is too large to fit in a %u byte signed integer value", + sval64, byte_size); + else { + if (!SetUInt(sval64, reg_info->byte_size)) + error.SetErrorStringWithFormat( + "unsupported signed integer byte size: %u", byte_size); + } + } else { error.SetErrorStringWithFormat("unsupported signed integer byte size: %u", byte_size); - break; + return error; } - - // TODO: Shouldn't we be setting m_type here? break; - case eEncodingIEEE754: { - std::string value_string = value_str; + case eEncodingIEEE754: if (byte_size == sizeof(float)) { - if (::sscanf(value_string.c_str(), "%f", &flt_val) != 1) { + if (::sscanf(value_str, "%f", &flt_val) == 1) { + m_scalar = flt_val; + m_type = eTypeFloat; + } else error.SetErrorStringWithFormat("'%s' is not a valid float string value", - value_string.c_str()); - break; - } - m_scalar = flt_val; - m_type = eTypeFloat; + value_str); } else if (byte_size == sizeof(double)) { - if (::sscanf(value_string.c_str(), "%lf", &dbl_val) != 1) { + if (::sscanf(value_str, "%lf", &dbl_val) == 1) { + m_scalar = dbl_val; + m_type = eTypeDouble; + } else error.SetErrorStringWithFormat("'%s' is not a valid float string value", - value_string.c_str()); - break; - } - m_scalar = dbl_val; - m_type = eTypeDouble; + value_str); } else if (byte_size == sizeof(long double)) { - if (::sscanf(value_string.c_str(), "%Lf", &ldbl_val) != 1) { + if (::sscanf(value_str, "%Lf", &ldbl_val) == 1) { + m_scalar = ldbl_val; + m_type = eTypeLongDouble; + } else error.SetErrorStringWithFormat("'%s' is not a valid float string value", - value_string.c_str()); - break; - } - m_scalar = ldbl_val; - m_type = eTypeLongDouble; + value_str); } else { error.SetErrorStringWithFormat("unsupported float byte size: %u", byte_size); return error; } break; - } + case eEncodingVector: if (!ParseVectorEncoding(reg_info, value_str, byte_size, this)) error.SetErrorString("unrecognized vector encoding string value."); break; } + if (error.Fail()) + m_type = eTypeInvalid; return error; } diff --git a/lldb/source/Core/ValueObjectRegister.cpp b/lldb/source/Core/ValueObjectRegister.cpp index 80e00e4..0dc2cc0 100644 --- a/lldb/source/Core/ValueObjectRegister.cpp +++ b/lldb/source/Core/ValueObjectRegister.cpp @@ -311,8 +311,7 @@ bool ValueObjectRegister::UpdateValue() { bool ValueObjectRegister::SetValueFromCString(const char *value_str, Error &error) { // The new value will be in the m_data. Copy that into our register value. - error = - m_reg_value.SetValueFromString(&m_reg_info, llvm::StringRef(value_str)); + error = m_reg_value.SetValueFromCString(&m_reg_info, value_str); if (error.Success()) { if (m_reg_ctx_sp->WriteRegister(&m_reg_info, m_reg_value)) { SetNeedsUpdate(); diff --git a/lldb/source/Core/ValueObjectVariable.cpp b/lldb/source/Core/ValueObjectVariable.cpp index f8eedc3..f0695d8 100644 --- a/lldb/source/Core/ValueObjectVariable.cpp +++ b/lldb/source/Core/ValueObjectVariable.cpp @@ -343,7 +343,7 @@ bool ValueObjectVariable::SetValueFromCString(const char *value_str, error.SetErrorString("unable to retrieve register info"); return false; } - error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str)); + error = reg_value.SetValueFromCString(reg_info, value_str); if (error.Fail()) return false; if (reg_ctx->WriteRegister(reg_info, reg_value)) {