Scalar: Use llvm integer conversion functions
authorPavel Labath <labath@google.com>
Tue, 19 Jun 2018 17:24:03 +0000 (17:24 +0000)
committerPavel Labath <labath@google.com>
Tue, 19 Jun 2018 17:24:03 +0000 (17:24 +0000)
StringConvert was the only non-Utility dependency of this class. Getting
rid of it means it will be easy to move this class to a lower layer.

While I was in there, I also added a couple of unit tests for the Scalar
string conversion function.

llvm-svn: 335060

lldb/source/Core/Scalar.cpp
lldb/unittests/Core/CMakeLists.txt
lldb/unittests/Core/ScalarTest.cpp

index 56d377b..72108c4 100644 (file)
@@ -9,7 +9,6 @@
 
 #include "lldb/Core/Scalar.h"
 
-#include "lldb/Host/StringConvert.h"
 #include "lldb/Utility/DataExtractor.h"
 #include "lldb/Utility/Endian.h"
 #include "lldb/Utility/Status.h"
@@ -2254,17 +2253,15 @@ Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
     error.SetErrorString("Invalid c-string value string.");
     return error;
   }
-  bool success = false;
   switch (encoding) {
   case eEncodingInvalid:
     error.SetErrorString("Invalid encoding.");
     break;
 
   case eEncodingUint:
-    if (byte_size <= sizeof(unsigned long long)) {
-      uint64_t uval64 =
-          StringConvert::ToUInt64(value_str, UINT64_MAX, 0, &success);
-      if (!success)
+    if (byte_size <= sizeof(uint64_t)) {
+      uint64_t uval64;
+      if (!llvm::to_integer(value_str, uval64))
         error.SetErrorStringWithFormat(
             "'%s' is not a valid unsigned integer string value", value_str);
       else if (!UIntValueIsValidForSize(uval64, byte_size))
@@ -2300,10 +2297,9 @@ Status Scalar::SetValueFromCString(const char *value_str, Encoding encoding,
     break;
 
   case eEncodingSint:
-    if (byte_size <= sizeof(long long)) {
-      uint64_t sval64 =
-          StringConvert::ToSInt64(value_str, INT64_MAX, 0, &success);
-      if (!success)
+    if (byte_size <= sizeof(int64_t)) {
+      int64_t sval64;
+      if (!llvm::to_integer(value_str, sval64))
         error.SetErrorStringWithFormat(
             "'%s' is not a valid signed integer string value", value_str);
       else if (!SIntValueIsValidForSize(sval64, byte_size))
index 41ace5b..34c022c 100644 (file)
@@ -9,6 +9,7 @@ add_lldb_unittest(LLDBCoreTests
   LINK_LIBS
     lldbCore
     lldbHost
+    LLVMTestingSupport
   LINK_COMPONENTS
     Support
   )
index 5fad169..9c241c2 100644 (file)
 #include "lldb/Utility/Endian.h"
 #include "lldb/Utility/Status.h"
 #include "lldb/Utility/StreamString.h"
+#include "llvm/Testing/Support/Error.h"
 
 using namespace lldb_private;
+using namespace llvm;
 
 TEST(ScalarTest, RightShiftOperator) {
   int a = 0x00001000;
@@ -185,3 +187,34 @@ TEST(ScalarTest, Promotion) {
     }
   }
 }
+
+TEST(ScalarTest, SetValueFromCString) {
+  Scalar a;
+
+  EXPECT_THAT_ERROR(
+      a.SetValueFromCString("1234567890123", lldb::eEncodingUint, 8).ToError(),
+      Succeeded());
+  EXPECT_EQ(1234567890123ull, a);
+
+  EXPECT_THAT_ERROR(
+      a.SetValueFromCString("-1234567890123", lldb::eEncodingSint, 8).ToError(),
+      Succeeded());
+  EXPECT_EQ(-1234567890123ll, a);
+
+  EXPECT_THAT_ERROR(
+      a.SetValueFromCString("asdf", lldb::eEncodingSint, 8).ToError(),
+      Failed());
+  EXPECT_THAT_ERROR(
+      a.SetValueFromCString("asdf", lldb::eEncodingUint, 8).ToError(),
+      Failed());
+  EXPECT_THAT_ERROR(
+      a.SetValueFromCString("1234567890123", lldb::eEncodingUint, 4).ToError(),
+      Failed());
+  EXPECT_THAT_ERROR(a.SetValueFromCString("123456789012345678901234567890",
+                                          lldb::eEncodingUint, 8)
+                        .ToError(),
+                    Failed());
+  EXPECT_THAT_ERROR(
+      a.SetValueFromCString("-123", lldb::eEncodingUint, 8).ToError(),
+      Failed());
+}