Add ConstString::IsNull() to tell between null vs. empty strings and fix usage in Mangled::GetDemangledName()

Summary: `IsEmpty()` and `operator bool() == false` have equal semantics. Usage in Mangled::GetDemangledName() was incorrect. What it actually wants is a check for null-string. Split this off of D50071 and added a test to clarify usage.

Reviewers: labath, jingham

Subscribers: erik.pilkington, lldb-commits

Differential Revision: https://reviews.llvm.org/D50327

llvm-svn: 339014
This commit is contained in:
Stefan Granitz 2018-08-06 14:15:17 +00:00
parent e023706471
commit 4af5a83a48
3 changed files with 28 additions and 2 deletions

View File

@ -345,6 +345,15 @@ public:
//------------------------------------------------------------------
bool IsEmpty() const { return m_string == nullptr || m_string[0] == '\0'; }
//------------------------------------------------------------------
/// Test for null string.
///
/// @return
/// @li \b true if there is no string associated with this instance.
/// @li \b false if there is a string associated with this instance.
//------------------------------------------------------------------
bool IsNull() const { return m_string == nullptr; }
//------------------------------------------------------------------
/// Set the C string value.
///

View File

@ -242,7 +242,7 @@ const ConstString &
Mangled::GetDemangledName(lldb::LanguageType language) const {
// Check to make sure we have a valid mangled name and that we haven't
// already decoded our mangled name.
if (m_mangled && !m_demangled) {
if (m_mangled && m_demangled.IsNull()) {
// We need to generate and cache the demangled name.
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(func_cat, "Mangled::GetDemangledName (m_mangled = %s)",
@ -312,7 +312,7 @@ Mangled::GetDemangledName(lldb::LanguageType language) const {
free(demangled_name);
}
}
if (!m_demangled) {
if (m_demangled.IsNull()) {
// Set the demangled string to the empty string to indicate we tried to
// parse it once and failed.
m_demangled.SetCString("");

View File

@ -33,3 +33,20 @@ TEST(ConstStringTest, MangledCounterpart) {
EXPECT_TRUE(foo.GetMangledCounterpart(counterpart));
EXPECT_EQ("bar", 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());
}