2017-02-05 08:44:54 +08:00
|
|
|
//===-- ConstStringTest.cpp -------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-02-05 08:44:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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());
|
|
|
|
}
|
2018-08-06 16:27:59 +08:00
|
|
|
|
|
|
|
TEST(ConstStringTest, MangledCounterpart) {
|
2018-08-14 19:07:18 +08:00
|
|
|
ConstString uvw("uvw");
|
2018-08-06 16:27:59 +08:00
|
|
|
ConstString counterpart;
|
2018-08-14 19:07:18 +08:00
|
|
|
EXPECT_FALSE(uvw.GetMangledCounterpart(counterpart));
|
2018-08-06 16:27:59 +08:00
|
|
|
EXPECT_EQ("", counterpart.GetStringRef());
|
|
|
|
|
2018-08-14 19:07:18 +08:00
|
|
|
ConstString xyz;
|
|
|
|
xyz.SetStringWithMangledCounterpart("xyz", uvw);
|
|
|
|
EXPECT_EQ("xyz", xyz.GetStringRef());
|
2018-08-06 16:27:59 +08:00
|
|
|
|
2018-08-14 19:07:18 +08:00
|
|
|
EXPECT_TRUE(xyz.GetMangledCounterpart(counterpart));
|
|
|
|
EXPECT_EQ("uvw", counterpart.GetStringRef());
|
2018-08-06 16:27:59 +08:00
|
|
|
|
2018-08-14 19:07:18 +08:00
|
|
|
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());
|
|
|
|
}
|
2018-08-06 16:27:59 +08:00
|
|
|
}
|
2018-08-06 22:15:17 +08:00
|
|
|
|
2018-08-09 05:57:42 +08:00
|
|
|
TEST(ConstStringTest, FromMidOfBufferStringRef) {
|
|
|
|
// StringRef's into bigger buffer: no null termination
|
2018-08-14 19:07:18 +08:00
|
|
|
const char *buffer = "abcdefghi";
|
2018-08-09 05:57:42 +08:00
|
|
|
llvm::StringRef foo_ref(buffer, 3);
|
|
|
|
llvm::StringRef bar_ref(buffer + 3, 3);
|
|
|
|
|
|
|
|
ConstString foo(foo_ref);
|
|
|
|
|
|
|
|
ConstString bar;
|
|
|
|
bar.SetStringWithMangledCounterpart(bar_ref, foo);
|
2018-08-14 19:07:18 +08:00
|
|
|
EXPECT_EQ("def", bar.GetStringRef());
|
2018-08-09 05:57:42 +08:00
|
|
|
|
|
|
|
ConstString counterpart;
|
|
|
|
EXPECT_TRUE(bar.GetMangledCounterpart(counterpart));
|
2018-08-14 19:07:18 +08:00
|
|
|
EXPECT_EQ("abc", counterpart.GetStringRef());
|
2018-08-09 05:57:42 +08:00
|
|
|
|
|
|
|
EXPECT_TRUE(foo.GetMangledCounterpart(counterpart));
|
2018-08-14 19:07:18 +08:00
|
|
|
EXPECT_EQ("def", counterpart.GetStringRef());
|
2018-08-09 05:57:42 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 22:15:17 +08:00
|
|
|
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");
|
|
|
|
}
|