[Lldb-commits] [lldb] r280123 - Convert some StringExtractor functions to accept MutableArrayRefs.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Tue Aug 30 11:12:11 PDT 2016


Author: zturner
Date: Tue Aug 30 13:12:11 2016
New Revision: 280123

URL: http://llvm.org/viewvc/llvm-project?rev=280123&view=rev
Log:
Convert some StringExtractor functions to accept MutableArrayRefs.

MutableArrayRef<T> is essentially a safer version of passing around
(T*, length) pairs and provides some convenient functions for working
with the data without having to manually manipulate indices.

This is a minor NFC.

Modified:
    lldb/trunk/include/lldb/Core/DataBuffer.h
    lldb/trunk/include/lldb/Utility/StringExtractor.h
    lldb/trunk/source/Plugins/Process/Utility/DynamicRegisterInfo.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
    lldb/trunk/source/Utility/StringExtractor.cpp
    lldb/trunk/unittests/Utility/StringExtractorTest.cpp

Modified: lldb/trunk/include/lldb/Core/DataBuffer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Core/DataBuffer.h?rev=280123&r1=280122&r2=280123&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Core/DataBuffer.h (original)
+++ lldb/trunk/include/lldb/Core/DataBuffer.h Tue Aug 30 13:12:11 2016
@@ -16,6 +16,8 @@
 
 #include "lldb/lldb-types.h"
 
+#include "llvm/ADT/ArrayRef.h"
+
 namespace lldb_private {
 
 //----------------------------------------------------------------------
@@ -88,6 +90,16 @@ public:
     //------------------------------------------------------------------
     virtual lldb::offset_t
     GetByteSize() const = 0;
+
+    llvm::ArrayRef<uint8_t> GetData() const
+    {
+        return llvm::ArrayRef<uint8_t>(GetBytes(), GetByteSize());
+    }
+
+    llvm::MutableArrayRef<uint8_t> GetData()
+    {
+        return llvm::MutableArrayRef<uint8_t>(GetBytes(), GetByteSize());
+    }
 };
 
 } // namespace lldb_private

Modified: lldb/trunk/include/lldb/Utility/StringExtractor.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/StringExtractor.h?rev=280123&r1=280122&r2=280123&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/StringExtractor.h (original)
+++ lldb/trunk/include/lldb/Utility/StringExtractor.h Tue Aug 30 13:12:11 2016
@@ -17,6 +17,7 @@
 
 // Other libraries and framework includes
 // Project includes
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 
 class StringExtractor
@@ -141,10 +142,10 @@ public:
     GetHexMaxU64 (bool little_endian, uint64_t fail_value);
 
     size_t
-    GetHexBytes (void *dst, size_t dst_len, uint8_t fail_fill_value);
+    GetHexBytes (llvm::MutableArrayRef<uint8_t> dest, uint8_t fail_fill_value);
 
     size_t
-    GetHexBytesAvail (void *dst, size_t dst_len);
+    GetHexBytesAvail (llvm::MutableArrayRef<uint8_t> dest);
 
     uint64_t
     GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value);

Modified: lldb/trunk/source/Plugins/Process/Utility/DynamicRegisterInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Utility/DynamicRegisterInfo.cpp?rev=280123&r1=280122&r2=280123&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Utility/DynamicRegisterInfo.cpp (original)
+++ lldb/trunk/source/Plugins/Process/Utility/DynamicRegisterInfo.cpp Tue Aug 30 13:12:11 2016
@@ -306,8 +306,7 @@ DynamicRegisterInfo::SetRegisterInfo(con
             StringExtractor opcode_extractor;
             // Swap "dwarf_opcode_string" over into "opcode_extractor"
             opcode_extractor.GetStringRef ().swap (dwarf_opcode_string);
-            uint32_t ret_val = opcode_extractor.GetHexBytesAvail (dwarf_opcode_bytes.data (),
-                                                                  reg_info.dynamic_size_dwarf_len);
+            uint32_t ret_val = opcode_extractor.GetHexBytesAvail (dwarf_opcode_bytes);
             assert (ret_val == reg_info.dynamic_size_dwarf_len);
 
             for (j = 0; j < reg_info.dynamic_size_dwarf_len; ++j)

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp?rev=280123&r1=280122&r2=280123&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp Tue Aug 30 13:12:11 2016
@@ -3480,7 +3480,7 @@ GDBRemoteCommunicationClient::ReadRegist
         return nullptr;
 
     DataBufferSP buffer_sp(new DataBufferHeap(response.GetStringRef().size() / 2, 0));
-    response.GetHexBytes(buffer_sp->GetBytes(), buffer_sp->GetByteSize(), '\xcc');
+    response.GetHexBytes(buffer_sp->GetData(), '\xcc');
     return buffer_sp;
 }
 
@@ -3495,7 +3495,7 @@ GDBRemoteCommunicationClient::ReadAllReg
         return nullptr;
 
     DataBufferSP buffer_sp(new DataBufferHeap(response.GetStringRef().size() / 2, 0));
-    response.GetHexBytes(buffer_sp->GetBytes(), buffer_sp->GetByteSize(), '\xcc');
+    response.GetHexBytes(buffer_sp->GetData(), '\xcc');
     return buffer_sp;
 }
 

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp?rev=280123&r1=280122&r2=280123&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerLLGS.cpp Tue Aug 30 13:12:11 2016
@@ -1794,7 +1794,7 @@ GDBRemoteCommunicationServerLLGS::Handle
 
     // Parse out the value.
     uint8_t reg_bytes[32]; // big enough to support up to 256 bit ymmN register
-    size_t reg_size = packet.GetHexBytesAvail (reg_bytes, sizeof(reg_bytes));
+    size_t reg_size = packet.GetHexBytesAvail (reg_bytes);
 
     // Get the thread to use.
     NativeThreadProtocolSP thread_sp = GetThreadFromSuffix (packet);
@@ -1939,10 +1939,10 @@ GDBRemoteCommunicationServerLLGS::Handle
     }
 
     packet.SetFilePos (::strlen("I"));
-    char tmp[4096];
+    uint8_t tmp[4096];
     for (;;)
     {
-        size_t read = packet.GetHexBytesAvail(tmp, sizeof(tmp));
+        size_t read = packet.GetHexBytesAvail(tmp);
         if (read == 0)
         {
             break;
@@ -2118,7 +2118,7 @@ GDBRemoteCommunicationServerLLGS::Handle
 
     // Convert the hex memory write contents to bytes.
     StreamGDBRemote response;
-    const uint64_t convert_count = packet.GetHexBytes(&buf[0], byte_count, 0);
+    const uint64_t convert_count = packet.GetHexBytes(buf, 0);
     if (convert_count != byte_count)
     {
         if (log)

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp?rev=280123&r1=280122&r2=280123&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp Tue Aug 30 13:12:11 2016
@@ -649,8 +649,7 @@ ProcessGDBRemote::BuildDynamicRegisterIn
                         reg_info.dynamic_size_dwarf_len = dwarf_opcode_len;
 
                         StringExtractor opcode_extractor(value);
-                        uint32_t ret_val =
-                            opcode_extractor.GetHexBytesAvail(dwarf_opcode_bytes.data(), dwarf_opcode_len);
+                        uint32_t ret_val = opcode_extractor.GetHexBytesAvail(dwarf_opcode_bytes);
                         assert(dwarf_opcode_len == ret_val);
 
                         reg_info.dynamic_size_dwarf_expr_bytes = dwarf_opcode_bytes.data();
@@ -1986,9 +1985,8 @@ ProcessGDBRemote::SetThreadStopInfo (lld
                 StringExtractor reg_value_extractor;
                 reg_value_extractor.GetStringRef() = pair.second;
                 DataBufferSP buffer_sp(new DataBufferHeap(reg_value_extractor.GetStringRef().size() / 2, 0));
-                reg_value_extractor.GetHexBytes(buffer_sp->GetBytes(), buffer_sp->GetByteSize(), '\xcc');
-                gdb_thread->PrivateSetRegisterValue(
-                    pair.first, llvm::ArrayRef<uint8_t>(buffer_sp->GetBytes(), buffer_sp->GetByteSize()));
+                reg_value_extractor.GetHexBytes(buffer_sp->GetData(), '\xcc');
+                gdb_thread->PrivateSetRegisterValue(pair.first, buffer_sp->GetData());
             }
 
             thread_sp->SetName (thread_name.empty() ? NULL : thread_name.c_str());
@@ -2366,7 +2364,7 @@ ProcessGDBRemote::SetThreadStopInfo (Str
 
                                     const size_t byte_size = bytes.GetStringRef().size()/2;
                                     DataBufferSP data_buffer_sp(new DataBufferHeap(byte_size, 0));
-                                    const size_t bytes_copied = bytes.GetHexBytes (data_buffer_sp->GetBytes(), byte_size, 0);
+                                    const size_t bytes_copied = bytes.GetHexBytes (data_buffer_sp->GetData(), 0);
                                     if (bytes_copied == byte_size)
                                         m_memory_cache.AddL1CacheData(mem_cache_addr, data_buffer_sp);
                                 }
@@ -2585,7 +2583,7 @@ ProcessGDBRemote::SetThreadStopInfo (Str
                             StringExtractor bytes(bytes_str);
                             const size_t byte_size = bytes.GetBytesLeft() / 2;
                             DataBufferSP data_buffer_sp(new DataBufferHeap(byte_size, 0));
-                            const size_t bytes_copied = bytes.GetHexBytes (data_buffer_sp->GetBytes(), byte_size, 0);
+                            const size_t bytes_copied = bytes.GetHexBytes (data_buffer_sp->GetData(), 0);
                             if (bytes_copied == byte_size)
                                 m_memory_cache.AddL1CacheData(mem_cache_addr, data_buffer_sp);
                         }
@@ -3086,7 +3084,7 @@ ProcessGDBRemote::DoReadMemory (addr_t a
             }
             else
             {
-                return response.GetHexBytes(buf, size, '\xdd');
+                return response.GetHexBytes(llvm::MutableArrayRef<uint8_t>((uint8_t*)buf, size), '\xdd');
             }
         }
         else if (response.IsErrorResponse())
@@ -4587,8 +4585,7 @@ ParseRegisters (XMLNode feature_node, Gd
                 dwarf_opcode_bytes.resize (dwarf_opcode_len);
                 reg_info.dynamic_size_dwarf_len = dwarf_opcode_len;
                 opcode_extractor.GetStringRef ().swap (opcode_string);
-                uint32_t ret_val = opcode_extractor.GetHexBytesAvail (dwarf_opcode_bytes.data (),
-                                                                    dwarf_opcode_len);
+                uint32_t ret_val = opcode_extractor.GetHexBytesAvail (dwarf_opcode_bytes);
                 assert (dwarf_opcode_len == ret_val);
 
                 reg_info.dynamic_size_dwarf_expr_bytes = dwarf_opcode_bytes.data ();

Modified: lldb/trunk/source/Utility/StringExtractor.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/StringExtractor.cpp?rev=280123&r1=280122&r2=280123&view=diff
==============================================================================
--- lldb/trunk/source/Utility/StringExtractor.cpp (original)
+++ lldb/trunk/source/Utility/StringExtractor.cpp Tue Aug 30 13:12:11 2016
@@ -357,21 +357,20 @@ StringExtractor::GetHexMaxU64 (bool litt
 }
 
 size_t
-StringExtractor::GetHexBytes (void *dst_void, size_t dst_len, uint8_t fail_fill_value)
+StringExtractor::GetHexBytes (llvm::MutableArrayRef<uint8_t> dest, uint8_t fail_fill_value)
 {
-    uint8_t *dst = (uint8_t*)dst_void;
     size_t bytes_extracted = 0;
-    while (bytes_extracted < dst_len && GetBytesLeft ())
+    while (!dest.empty() && GetBytesLeft() > 0)
     {
-        dst[bytes_extracted] = GetHexU8 (fail_fill_value);
-        if (IsGood())
-            ++bytes_extracted;
-        else
+        dest[0] = GetHexU8 (fail_fill_value);
+        if (!IsGood())
             break;
+        ++bytes_extracted;
+        dest = dest.drop_front();
     }
 
-    for (size_t i = bytes_extracted; i < dst_len; ++i)
-        dst[i] = fail_fill_value;
+    if (!dest.empty())
+        ::memset(dest.data(), fail_fill_value, dest.size());
 
     return bytes_extracted;
 }
@@ -383,18 +382,17 @@ StringExtractor::GetHexBytes (void *dst_
 // Returns the number of bytes successfully decoded
 //----------------------------------------------------------------------
 size_t
-StringExtractor::GetHexBytesAvail (void *dst_void, size_t dst_len)
+StringExtractor::GetHexBytesAvail (llvm::MutableArrayRef<uint8_t> dest)
 {
-    uint8_t *dst = (uint8_t*)dst_void;
     size_t bytes_extracted = 0;
-    while (bytes_extracted < dst_len)
+    while (!dest.empty())
     {
         int decode = DecodeHexU8();
         if (decode == -1)
-        {
             break;
-        }
-        dst[bytes_extracted++] = (uint8_t)decode;
+        dest[0] = (uint8_t)decode;
+        dest = dest.drop_front();
+        ++bytes_extracted;
     }
     return bytes_extracted;
 }

Modified: lldb/trunk/unittests/Utility/StringExtractorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/StringExtractorTest.cpp?rev=280123&r1=280122&r2=280123&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/StringExtractorTest.cpp (original)
+++ lldb/trunk/unittests/Utility/StringExtractorTest.cpp Tue Aug 30 13:12:11 2016
@@ -243,7 +243,7 @@ TEST_F (StringExtractorTest, GetHexBytes
     StringExtractor ex(kHexEncodedBytes);
 
     uint8_t dst[kValidHexPairs];
-    ASSERT_EQ(kValidHexPairs, ex.GetHexBytes (dst, kValidHexPairs, 0xde));
+    ASSERT_EQ(kValidHexPairs, ex.GetHexBytes (dst, 0xde));
     EXPECT_EQ(0xab,dst[0]);
     EXPECT_EQ(0xcd,dst[1]);
     EXPECT_EQ(0xef,dst[2]);
@@ -267,7 +267,7 @@ TEST_F (StringExtractorTest, GetHexBytes
     StringExtractor ex(kHexEncodedBytes);
 
     uint8_t dst[12];
-    ASSERT_EQ(kValidHexPairs, ex.GetHexBytes (dst, sizeof(dst), 0xde));
+    ASSERT_EQ(kValidHexPairs, ex.GetHexBytes (dst, 0xde));
     EXPECT_EQ(0xab,dst[0]);
     EXPECT_EQ(0xcd,dst[1]);
     EXPECT_EQ(0xef,dst[2]);
@@ -297,7 +297,7 @@ TEST_F (StringExtractorTest, GetHexBytes
 
     uint8_t dst[12];
     memset(dst, 0xab, sizeof(dst));
-    ASSERT_EQ(kReadBytes, ex.GetHexBytes (dst, kReadBytes, 0xde));
+    ASSERT_EQ(kReadBytes, ex.GetHexBytes (llvm::MutableArrayRef<uint8_t>(dst, kReadBytes), 0xde));
     EXPECT_EQ(0xab,dst[0]);
     EXPECT_EQ(0xcd,dst[1]);
     EXPECT_EQ(0xef,dst[2]);
@@ -326,7 +326,7 @@ TEST_F (StringExtractorTest, GetHexBytes
     StringExtractor ex(kHexEncodedBytes);
 
     uint8_t dst[kValidHexPairs];
-    ASSERT_EQ(kValidHexPairs, ex.GetHexBytesAvail (dst, kValidHexPairs));
+    ASSERT_EQ(kValidHexPairs, ex.GetHexBytesAvail (dst));
     EXPECT_EQ(0xab,dst[0]);
     EXPECT_EQ(0xcd,dst[1]);
     EXPECT_EQ(0xef,dst[2]);
@@ -351,7 +351,7 @@ TEST_F (StringExtractorTest, GetHexBytes
 
     uint8_t dst[12];
     memset(dst, 0xef, sizeof(dst));
-    ASSERT_EQ(kValidHexPairs, ex.GetHexBytesAvail (dst, sizeof(dst)));
+    ASSERT_EQ(kValidHexPairs, ex.GetHexBytesAvail (dst));
     EXPECT_EQ(0xab,dst[0]);
     EXPECT_EQ(0xcd,dst[1]);
     EXPECT_EQ(0xef,dst[2]);
@@ -381,7 +381,7 @@ TEST_F (StringExtractorTest, GetHexBytes
 
     uint8_t dst[12];
     memset(dst, 0xab, sizeof(dst));
-    ASSERT_EQ(kReadBytes, ex.GetHexBytesAvail (dst, kReadBytes));
+    ASSERT_EQ(kReadBytes, ex.GetHexBytesAvail (llvm::MutableArrayRef<uint8_t>(dst, kReadBytes)));
     EXPECT_EQ(0xab,dst[0]);
     EXPECT_EQ(0xcd,dst[1]);
     EXPECT_EQ(0xef,dst[2]);




More information about the lldb-commits mailing list