[Lldb-commits] [lldb] r335244 - Modernize UUID class

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Thu Jun 21 08:07:43 PDT 2018


Author: labath
Date: Thu Jun 21 08:07:43 2018
New Revision: 335244

URL: http://llvm.org/viewvc/llvm-project?rev=335244&view=rev
Log:
Modernize UUID class

Instead of a separate GetBytes + GetByteSize methods I introduce a
single GetBytes method returning an ArrayRef.

This is NFC cleanup now, but it should make handling arbitrarily-sized
UUIDs cleaner, should we choose to go that way. I also took the
opportunity to add some unit tests for this class.

Added:
    lldb/trunk/unittests/Utility/UUIDTest.cpp
Modified:
    lldb/trunk/include/lldb/Utility/UUID.h
    lldb/trunk/source/API/SBModule.cpp
    lldb/trunk/source/API/SBModuleSpec.cpp
    lldb/trunk/source/Interpreter/OptionValueUUID.cpp
    lldb/trunk/source/Utility/UUID.cpp
    lldb/trunk/unittests/Utility/CMakeLists.txt

Modified: lldb/trunk/include/lldb/Utility/UUID.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/UUID.h?rev=335244&r1=335243&r2=335244&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/UUID.h (original)
+++ lldb/trunk/include/lldb/Utility/UUID.h Thu Jun 21 08:07:43 2018
@@ -15,6 +15,7 @@
 #include <stddef.h>
 #include <stdint.h>
 #include <string>
+#include "llvm/ADT/ArrayRef.h"
 
 namespace llvm {
   class StringRef;
@@ -44,9 +45,9 @@ public:
 
   void Dump(Stream *s) const;
 
-  const void *GetBytes() const;
-
-  size_t GetByteSize() const;
+  llvm::ArrayRef<uint8_t> GetBytes() const {
+    return {m_uuid, m_num_uuid_bytes};
+  }
 
   bool IsValid() const;
 

Modified: lldb/trunk/source/API/SBModule.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBModule.cpp?rev=335244&r1=335243&r2=335244&view=diff
==============================================================================
--- lldb/trunk/source/API/SBModule.cpp (original)
+++ lldb/trunk/source/API/SBModule.cpp Thu Jun 21 08:07:43 2018
@@ -144,7 +144,7 @@ const uint8_t *SBModule::GetUUIDBytes()
   const uint8_t *uuid_bytes = NULL;
   ModuleSP module_sp(GetSP());
   if (module_sp)
-    uuid_bytes = (const uint8_t *)module_sp->GetUUID().GetBytes();
+    uuid_bytes = module_sp->GetUUID().GetBytes().data();
 
   if (log) {
     if (uuid_bytes) {

Modified: lldb/trunk/source/API/SBModuleSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBModuleSpec.cpp?rev=335244&r1=335243&r2=335244&view=diff
==============================================================================
--- lldb/trunk/source/API/SBModuleSpec.cpp (original)
+++ lldb/trunk/source/API/SBModuleSpec.cpp Thu Jun 21 08:07:43 2018
@@ -82,11 +82,11 @@ void SBModuleSpec::SetTriple(const char
 }
 
 const uint8_t *SBModuleSpec::GetUUIDBytes() {
-  return (const uint8_t *)m_opaque_ap->GetUUID().GetBytes();
+  return m_opaque_ap->GetUUID().GetBytes().data();
 }
 
 size_t SBModuleSpec::GetUUIDLength() {
-  return m_opaque_ap->GetUUID().GetByteSize();
+  return m_opaque_ap->GetUUID().GetBytes().size();
 }
 
 bool SBModuleSpec::SetUUIDBytes(const uint8_t *uuid, size_t uuid_len) {

Modified: lldb/trunk/source/Interpreter/OptionValueUUID.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/OptionValueUUID.cpp?rev=335244&r1=335243&r2=335244&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/OptionValueUUID.cpp (original)
+++ lldb/trunk/source/Interpreter/OptionValueUUID.cpp Thu Jun 21 08:07:43 2018
@@ -86,17 +86,12 @@ size_t OptionValueUUID::AutoComplete(Com
         if (module_sp) {
           const UUID &module_uuid = module_sp->GetUUID();
           if (module_uuid.IsValid()) {
-            bool add_uuid = false;
-            if (num_bytes_decoded == 0)
-              add_uuid = true;
-            else
-              add_uuid = ::memcmp(module_uuid.GetBytes(), uuid_bytes,
-                                  num_bytes_decoded) == 0;
-            if (add_uuid) {
-              std::string uuid_str;
-              uuid_str = module_uuid.GetAsString();
-              if (!uuid_str.empty())
-                matches.AppendString(uuid_str.c_str());
+            llvm::ArrayRef<uint8_t> decoded_bytes(uuid_bytes,
+                                                  num_bytes_decoded);
+            llvm::ArrayRef<uint8_t> module_bytes = module_uuid.GetBytes();
+            if (module_bytes.size() >= num_bytes_decoded &&
+                module_bytes.take_front(num_bytes_decoded) == decoded_bytes) {
+              matches.AppendString(module_uuid.GetAsString());
             }
           }
         }

Modified: lldb/trunk/source/Utility/UUID.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/UUID.cpp?rev=335244&r1=335243&r2=335244&view=diff
==============================================================================
--- lldb/trunk/source/Utility/UUID.cpp (original)
+++ lldb/trunk/source/Utility/UUID.cpp Thu Jun 21 08:07:43 2018
@@ -46,14 +46,12 @@ void UUID::Clear() {
   ::memset(m_uuid, 0, sizeof(m_uuid));
 }
 
-const void *UUID::GetBytes() const { return m_uuid; }
-
 std::string UUID::GetAsString(const char *separator) const {
   std::string result;
   char buf[256];
   if (!separator)
     separator = "-";
-  const uint8_t *u = (const uint8_t *)GetBytes();
+  const uint8_t *u = GetBytes().data();
   if (sizeof(buf) >
       (size_t)snprintf(buf, sizeof(buf), "%2.2X%2.2X%2.2X%2.2X%s%2.2X%2.2X%s%2."
                                          "2X%2.2X%s%2.2X%2.2X%s%2.2X%2.2X%2.2X%"
@@ -101,8 +99,6 @@ bool UUID::SetBytes(const void *uuid_byt
   return false;
 }
 
-size_t UUID::GetByteSize() const { return m_num_uuid_bytes; }
-
 bool UUID::IsValid() const {
   return m_uuid[0] || m_uuid[1] || m_uuid[2] || m_uuid[3] || m_uuid[4] ||
          m_uuid[5] || m_uuid[6] || m_uuid[7] || m_uuid[8] || m_uuid[9] ||
@@ -184,8 +180,7 @@ size_t UUID::SetFromCString(const char *
 
 bool lldb_private::operator==(const lldb_private::UUID &lhs,
                               const lldb_private::UUID &rhs) {
-  return ::memcmp(lhs.GetBytes(), rhs.GetBytes(),
-                  sizeof(lldb_private::UUID::ValueType)) == 0;
+  return lhs.GetBytes() == rhs.GetBytes();
 }
 
 bool lldb_private::operator!=(const lldb_private::UUID &lhs,
@@ -195,8 +190,11 @@ bool lldb_private::operator!=(const lldb
 
 bool lldb_private::operator<(const lldb_private::UUID &lhs,
                              const lldb_private::UUID &rhs) {
-  return ::memcmp(lhs.GetBytes(), rhs.GetBytes(),
-                  sizeof(lldb_private::UUID::ValueType)) < 0;
+  if (lhs.GetBytes().size() != rhs.GetBytes().size())
+    return lhs.GetBytes().size() < rhs.GetBytes().size();
+
+  return std::memcmp(lhs.GetBytes().data(), rhs.GetBytes().data(),
+                     lhs.GetBytes().size());
 }
 
 bool lldb_private::operator<=(const lldb_private::UUID &lhs,

Modified: lldb/trunk/unittests/Utility/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/CMakeLists.txt?rev=335244&r1=335243&r2=335244&view=diff
==============================================================================
--- lldb/trunk/unittests/Utility/CMakeLists.txt (original)
+++ lldb/trunk/unittests/Utility/CMakeLists.txt Thu Jun 21 08:07:43 2018
@@ -15,6 +15,7 @@ add_lldb_unittest(UtilityTests
   TimeoutTest.cpp
   TimerTest.cpp
   UriParserTest.cpp
+  UUIDTest.cpp
   VASprintfTest.cpp
 
   LINK_LIBS

Added: lldb/trunk/unittests/Utility/UUIDTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Utility/UUIDTest.cpp?rev=335244&view=auto
==============================================================================
--- lldb/trunk/unittests/Utility/UUIDTest.cpp (added)
+++ lldb/trunk/unittests/Utility/UUIDTest.cpp Thu Jun 21 08:07:43 2018
@@ -0,0 +1,36 @@
+//===-- UUIDTest.cpp --------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+
+#include "lldb/Utility/UUID.h"
+
+using namespace lldb_private;
+
+TEST(UUIDTest, RelationalOperators) {
+  UUID empty;
+  UUID a16("1234567890123456", 16);
+  UUID b16("1234567890123457", 16);
+  UUID a20("12345678901234567890", 20);
+  UUID b20("12345678900987654321", 20);
+
+  EXPECT_EQ(empty, empty);
+  EXPECT_EQ(a16, a16);
+  EXPECT_EQ(a20, a20);
+
+  EXPECT_NE(a16, b16);
+  EXPECT_NE(a20, b20);
+  EXPECT_NE(a16, a20);
+  EXPECT_NE(empty, a16);
+
+  EXPECT_LT(empty, a16);
+  EXPECT_LT(a16, a20);
+  EXPECT_LT(a16, b16);
+  EXPECT_GT(a20, b20);
+}




More information about the lldb-commits mailing list