llvm-project/lldb/unittests/Utility/ConstStringTest.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
4.0 KiB
C++
Raw Normal View History

//===-- ConstStringTest.cpp -------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/ConstString.h"
#include "llvm/Support/FormatVariadic.h"
#include "gtest/gtest.h"
using namespace lldb_private;
TEST(ConstStringTest, format_provider) {
EXPECT_EQ("foo", llvm::formatv("{0}", ConstString("foo")).str());
}
TEST(ConstStringTest, MangledCounterpart) {
ConstString uvw("uvw");
ConstString counterpart;
EXPECT_FALSE(uvw.GetMangledCounterpart(counterpart));
EXPECT_EQ("", counterpart.GetStringRef());
ConstString xyz;
xyz.SetStringWithMangledCounterpart("xyz", uvw);
EXPECT_EQ("xyz", xyz.GetStringRef());
EXPECT_TRUE(xyz.GetMangledCounterpart(counterpart));
EXPECT_EQ("uvw", counterpart.GetStringRef());
EXPECT_TRUE(uvw.GetMangledCounterpart(counterpart));
EXPECT_EQ("xyz", counterpart.GetStringRef());
}
TEST(ConstStringTest, UpdateMangledCounterpart) {
{ // Add counterpart
ConstString some1;
some1.SetStringWithMangledCounterpart("some", ConstString(""));
}
{ // Overwrite empty string
ConstString some2;
some2.SetStringWithMangledCounterpart("some", ConstString("one"));
}
{ // Overwrite with identical value
ConstString some2;
some2.SetStringWithMangledCounterpart("some", ConstString("one"));
}
{ // Check counterpart is set
ConstString counterpart;
EXPECT_TRUE(ConstString("some").GetMangledCounterpart(counterpart));
EXPECT_EQ("one", counterpart.GetStringRef());
}
}
TEST(ConstStringTest, FromMidOfBufferStringRef) {
// StringRef's into bigger buffer: no null termination
const char *buffer = "abcdefghi";
llvm::StringRef foo_ref(buffer, 3);
llvm::StringRef bar_ref(buffer + 3, 3);
ConstString foo(foo_ref);
ConstString bar;
bar.SetStringWithMangledCounterpart(bar_ref, foo);
EXPECT_EQ("def", bar.GetStringRef());
ConstString counterpart;
EXPECT_TRUE(bar.GetMangledCounterpart(counterpart));
EXPECT_EQ("abc", counterpart.GetStringRef());
EXPECT_TRUE(foo.GetMangledCounterpart(counterpart));
EXPECT_EQ("def", counterpart.GetStringRef());
}
TEST(ConstStringTest, NullAndEmptyStates) {
ConstString foo("foo");
EXPECT_FALSE(!foo);
EXPECT_FALSE(foo.IsEmpty());
EXPECT_FALSE(foo.IsNull());
ConstString empty("");
EXPECT_TRUE(!empty);
EXPECT_TRUE(empty.IsEmpty());
EXPECT_FALSE(empty.IsNull());
ConstString null;
EXPECT_TRUE(!null);
EXPECT_TRUE(null.IsEmpty());
EXPECT_TRUE(null.IsNull());
}
Allow direct comparison of ConstString against StringRef Summary: When we want to compare a ConstString against a string literal (or any other non-ConstString), we currently have to explicitly turn the other string into a ConstString. This makes sense as comparing ConstStrings against each other is only a fast pointer comparison. However, currently we (rather incorrectly) use in several places in LLDB temporary ConstStrings when we just want to compare a given ConstString against a hardcoded value, for example like this: ``` if (extension != ConstString(".oat") && extension != ConstString(".odex")) ``` Obviously this kind of defeats the point of ConstStrings. In the comparison above we would construct two temporary ConstStrings every time we hit the given code. Constructing a ConstString is relatively expensive: we need to go to the StringPool, take a read and possibly an exclusive write-lock and then look up our temporary string in the string map of the pool. So we do a lot of heavy work for essentially just comparing a <6 characters in two strings. I initially wanted to just fix these issues by turning the temporary ConstString in static variables/ members, but that made the code much less readable. Instead I propose to add a new overload for the ConstString comparison operator that takes a StringRef. This comparison operator directly compares the ConstString content against the given StringRef without turning the StringRef into a ConstString. This means that the example above can look like this now: ``` if (extension != ".oat" && extension != ".odex") ``` It also no longer has to unlock/lock two locks and call multiple functions in other TUs for constructing the temporary ConstString instances. Instead this should end up just being a direct string comparison of the two given strings on most compilers. This patch also directly updates all uses of temporary and short ConstStrings in LLDB to use this new comparison operator. It also adds a some unit tests for the new and old comparison operator. Reviewers: #lldb, JDevlieghere, espindola, amccarth Reviewed By: JDevlieghere, amccarth Subscribers: amccarth, clayborg, JDevlieghere, emaste, arichardson, MaskRay, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D60667 llvm-svn: 359281
2019-04-26 15:21:36 +08:00
TEST(ConstStringTest, CompareConstString) {
ConstString foo("foo");
ConstString foo2("foo");
ConstString bar("bar");
EXPECT_TRUE(foo == foo2);
EXPECT_TRUE(foo2 == foo);
EXPECT_TRUE(foo == ConstString("foo"));
EXPECT_FALSE(foo == bar);
EXPECT_FALSE(foo2 == bar);
EXPECT_FALSE(foo == ConstString("bar"));
EXPECT_FALSE(foo == ConstString("different"));
EXPECT_FALSE(foo == ConstString(""));
EXPECT_FALSE(foo == ConstString());
ConstString empty("");
EXPECT_FALSE(empty == ConstString("bar"));
EXPECT_FALSE(empty == ConstString());
EXPECT_TRUE(empty == ConstString(""));
ConstString null;
EXPECT_FALSE(null == ConstString("bar"));
EXPECT_TRUE(null == ConstString());
EXPECT_FALSE(null == ConstString(""));
}
TEST(ConstStringTest, CompareStringRef) {
ConstString foo("foo");
EXPECT_TRUE(foo == "foo");
EXPECT_TRUE(foo != "");
EXPECT_FALSE(foo == static_cast<const char *>(nullptr));
EXPECT_TRUE(foo != "bar");
ConstString empty("");
EXPECT_FALSE(empty == "foo");
EXPECT_FALSE(empty != "");
EXPECT_FALSE(empty == static_cast<const char *>(nullptr));
EXPECT_TRUE(empty != "bar");
ConstString null;
EXPECT_FALSE(null == "foo");
EXPECT_TRUE(null != "");
EXPECT_TRUE(null == static_cast<const char *>(nullptr));
EXPECT_TRUE(null != "bar");
}