[Lldb-commits] [lldb] r335060 - Scalar: Use llvm integer conversion functions

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Tue Jun 19 10:24:03 PDT 2018


Author: labath
Date: Tue Jun 19 10:24:03 2018
New Revision: 335060

URL: http://llvm.org/viewvc/llvm-project?rev=335060&view=rev
Log:
Scalar: Use llvm integer conversion functions

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.

Modified:
    lldb/trunk/source/Core/Scalar.cpp
    lldb/trunk/unittests/Core/CMakeLists.txt
    lldb/trunk/unittests/Core/ScalarTest.cpp

Modified: lldb/trunk/source/Core/Scalar.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Scalar.cpp?rev=335060&r1=335059&r2=335060&view=diff
==============================================================================
--- lldb/trunk/source/Core/Scalar.cpp (original)
+++ lldb/trunk/source/Core/Scalar.cpp Tue Jun 19 10:24:03 2018
@@ -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
     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
     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))

Modified: lldb/trunk/unittests/Core/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/CMakeLists.txt?rev=335060&r1=335059&r2=335060&view=diff
==============================================================================
--- lldb/trunk/unittests/Core/CMakeLists.txt (original)
+++ lldb/trunk/unittests/Core/CMakeLists.txt Tue Jun 19 10:24:03 2018
@@ -9,6 +9,7 @@ add_lldb_unittest(LLDBCoreTests
   LINK_LIBS
     lldbCore
     lldbHost
+    LLVMTestingSupport
   LINK_COMPONENTS
     Support
   )

Modified: lldb/trunk/unittests/Core/ScalarTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Core/ScalarTest.cpp?rev=335060&r1=335059&r2=335060&view=diff
==============================================================================
--- lldb/trunk/unittests/Core/ScalarTest.cpp (original)
+++ lldb/trunk/unittests/Core/ScalarTest.cpp Tue Jun 19 10:24:03 2018
@@ -14,8 +14,10 @@
 #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());
+}




More information about the lldb-commits mailing list