diff options
author | Pavel Labath <labath@google.com> | 2018-06-21 15:07:43 +0000 |
---|---|---|
committer | Pavel Labath <labath@google.com> | 2018-06-21 15:07:43 +0000 |
commit | 470b286ee5669019a83b2e2ffae845ee44369fbf (patch) | |
tree | 6bb572eb8d9527ee580308d25363a284a0330efa | |
parent | 084d360f6984df29d0ea2bdf8fea1f1d97de2858 (diff) | |
download | bcm5719-llvm-470b286ee5669019a83b2e2ffae845ee44369fbf.tar.gz bcm5719-llvm-470b286ee5669019a83b2e2ffae845ee44369fbf.zip |
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.
llvm-svn: 335244
-rw-r--r-- | lldb/include/lldb/Utility/UUID.h | 7 | ||||
-rw-r--r-- | lldb/source/API/SBModule.cpp | 2 | ||||
-rw-r--r-- | lldb/source/API/SBModuleSpec.cpp | 4 | ||||
-rw-r--r-- | lldb/source/Interpreter/OptionValueUUID.cpp | 17 | ||||
-rw-r--r-- | lldb/source/Utility/UUID.cpp | 16 | ||||
-rw-r--r-- | lldb/unittests/Utility/CMakeLists.txt | 1 | ||||
-rw-r--r-- | lldb/unittests/Utility/UUIDTest.cpp | 36 |
7 files changed, 57 insertions, 26 deletions
diff --git a/lldb/include/lldb/Utility/UUID.h b/lldb/include/lldb/Utility/UUID.h index 5e64e907978..2c12aca57c0 100644 --- a/lldb/include/lldb/Utility/UUID.h +++ b/lldb/include/lldb/Utility/UUID.h @@ -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; diff --git a/lldb/source/API/SBModule.cpp b/lldb/source/API/SBModule.cpp index 8866cf448a3..ed0c75c5c3e 100644 --- a/lldb/source/API/SBModule.cpp +++ b/lldb/source/API/SBModule.cpp @@ -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) { diff --git a/lldb/source/API/SBModuleSpec.cpp b/lldb/source/API/SBModuleSpec.cpp index 89e8bef91bf..0e6e7c9b0b9 100644 --- a/lldb/source/API/SBModuleSpec.cpp +++ b/lldb/source/API/SBModuleSpec.cpp @@ -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) { diff --git a/lldb/source/Interpreter/OptionValueUUID.cpp b/lldb/source/Interpreter/OptionValueUUID.cpp index bec04cba8c3..07c739db08a 100644 --- a/lldb/source/Interpreter/OptionValueUUID.cpp +++ b/lldb/source/Interpreter/OptionValueUUID.cpp @@ -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()); } } } diff --git a/lldb/source/Utility/UUID.cpp b/lldb/source/Utility/UUID.cpp index 097243219fa..864fb38eba1 100644 --- a/lldb/source/Utility/UUID.cpp +++ b/lldb/source/Utility/UUID.cpp @@ -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, diff --git a/lldb/unittests/Utility/CMakeLists.txt b/lldb/unittests/Utility/CMakeLists.txt index f7731a73d97..8b615678393 100644 --- a/lldb/unittests/Utility/CMakeLists.txt +++ b/lldb/unittests/Utility/CMakeLists.txt @@ -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 index 00000000000..f66efa618e0 --- /dev/null +++ b/lldb/unittests/Utility/UUIDTest.cpp @@ -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); +} |