Modernize UUID class
authorPavel Labath <labath@google.com>
Thu, 21 Jun 2018 15:07:43 +0000 (15:07 +0000)
committerPavel Labath <labath@google.com>
Thu, 21 Jun 2018 15:07:43 +0000 (15:07 +0000)
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.

llvm-svn: 335244

lldb/include/lldb/Utility/UUID.h
lldb/source/API/SBModule.cpp
lldb/source/API/SBModuleSpec.cpp
lldb/source/Interpreter/OptionValueUUID.cpp
lldb/source/Utility/UUID.cpp
lldb/unittests/Utility/CMakeLists.txt
lldb/unittests/Utility/UUIDTest.cpp [new file with mode: 0644]

index 5e64e90..2c12aca 100644 (file)
@@ -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;
 
index 8866cf4..ed0c75c 100644 (file)
@@ -144,7 +144,7 @@ const uint8_t *SBModule::GetUUIDBytes() const {
   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) {
index 89e8bef..0e6e7c9 100644 (file)
@@ -82,11 +82,11 @@ void SBModuleSpec::SetTriple(const char *triple) {
 }
 
 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) {
index bec04cb..07c739d 100644 (file)
@@ -86,17 +86,12 @@ size_t OptionValueUUID::AutoComplete(CommandInterpreter &interpreter,
         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());
             }
           }
         }
index 0972432..864fb38 100644 (file)
@@ -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_bytes, uint32_t num_uuid_bytes) {
   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 *cstr, uint32_t num_uuid_bytes) {
 
 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_private::UUID &lhs,
 
 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,
index f7731a7..8b61567 100644 (file)
@@ -15,6 +15,7 @@ add_lldb_unittest(UtilityTests
   TimeoutTest.cpp
   TimerTest.cpp
   UriParserTest.cpp
+  UUIDTest.cpp
   VASprintfTest.cpp
 
   LINK_LIBS
diff --git a/lldb/unittests/Utility/UUIDTest.cpp b/lldb/unittests/Utility/UUIDTest.cpp
new file mode 100644 (file)
index 0000000..f66efa6
--- /dev/null
@@ -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);
+}