[Lldb-commits] [lldb] r287281 - Revert "Change RegisterValue getters / setters to use StringRef."

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 17 15:32:27 PST 2016


Author: zturner
Date: Thu Nov 17 17:32:26 2016
New Revision: 287281

URL: http://llvm.org/viewvc/llvm-project?rev=287281&view=rev
Log:
Revert "Change RegisterValue getters / setters to use StringRef."

This reverts commit r287279, which breaks some register
tests on Linux.

Modified:
    lldb/trunk/include/lldb/Core/RegisterValue.h
    lldb/trunk/source/Commands/CommandObjectRegister.cpp
    lldb/trunk/source/Core/RegisterValue.cpp
    lldb/trunk/source/Core/ValueObjectRegister.cpp
    lldb/trunk/source/Core/ValueObjectVariable.cpp

Modified: lldb/trunk/include/lldb/Core/RegisterValue.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/RegisterValue.h?rev=287281&r1=287280&r2=287281&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/RegisterValue.h (original)
+++ lldb/trunk/include/lldb/Core/RegisterValue.h Thu Nov 17 17:32:26 2016
@@ -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);

Modified: lldb/trunk/source/Commands/CommandObjectRegister.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectRegister.cpp?rev=287281&r1=287280&r2=287281&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectRegister.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectRegister.cpp Thu Nov 17 17:32:26 2016
@@ -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 {

Modified: lldb/trunk/source/Core/RegisterValue.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/RegisterValue.cpp?rev=287281&r1=287280&r2=287281&view=diff
==============================================================================
--- lldb/trunk/source/Core/RegisterValue.cpp (original)
+++ lldb/trunk/source/Core/RegisterValue.cpp Thu Nov 17 17:32:26 2016
@@ -347,35 +347,51 @@ Error RegisterValue::SetValueFromData(co
   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<llvm::StringRef, llvm::StringRef> Pair = Str.split(Sep);
   std::vector<uint8_t> 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 Re
   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;
 }

Modified: lldb/trunk/source/Core/ValueObjectRegister.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ValueObjectRegister.cpp?rev=287281&r1=287280&r2=287281&view=diff
==============================================================================
--- lldb/trunk/source/Core/ValueObjectRegister.cpp (original)
+++ lldb/trunk/source/Core/ValueObjectRegister.cpp Thu Nov 17 17:32:26 2016
@@ -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();

Modified: lldb/trunk/source/Core/ValueObjectVariable.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/ValueObjectVariable.cpp?rev=287281&r1=287280&r2=287281&view=diff
==============================================================================
--- lldb/trunk/source/Core/ValueObjectVariable.cpp (original)
+++ lldb/trunk/source/Core/ValueObjectVariable.cpp Thu Nov 17 17:32:26 2016
@@ -343,7 +343,7 @@ bool ValueObjectVariable::SetValueFromCS
       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)) {




More information about the lldb-commits mailing list