teemperor created this revision.
teemperor added a reviewer: JDevlieghere.
Herald added subscribers: lldb-commits, MaskRay, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLDB.
We have a few checks in LLDB where we compare a single ConstString against a
hardcoded list of strings. This patch introduces a
utility function 'oneOf' in ConstString which is makes this check more readable.
For example, before this patch we had to write this:
if (ft == llvm::sys::fs::file_type::directory_file &&
(file_spec.GetFileNameExtension() == g_sdk_suffix ||
file_spec.GetFileNameExtension() == g_kdk_suffix)) {
And after this patch we can now write this:
if (ft == llvm::sys::fs::file_type::directory_file &&
file_spec.GetFileNameExtension().oneOf(g_sdk_suffix, g_kdk_suffix)) {
Repository:
rLLDB LLDB
https://reviews.llvm.org/D61231
Files:
lldb/include/lldb/Utility/ConstString.h
lldb/source/Core/Debugger.cpp
lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp
lldb/source/Plugins/Language/ObjC/CF.cpp
lldb/source/Plugins/Language/ObjC/Cocoa.cpp
lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp
lldb/unittests/Utility/ConstStringTest.cpp
Index: lldb/unittests/Utility/ConstStringTest.cpp
===================================================================
--- lldb/unittests/Utility/ConstStringTest.cpp
+++ lldb/unittests/Utility/ConstStringTest.cpp
@@ -137,3 +137,26 @@
EXPECT_TRUE(null == static_cast<const char *>(nullptr));
EXPECT_TRUE(null != "bar");
}
+
+TEST(ConstStringTest, OneOf) {
+ ConstString foo("foo");
+
+ EXPECT_TRUE(foo.oneOf("foo"));
+ EXPECT_TRUE(foo.oneOf("foo", "bar"));
+ EXPECT_TRUE(foo.oneOf("bar", "foo"));
+ EXPECT_TRUE(foo.oneOf("bar", "bar", "foo"));
+ EXPECT_TRUE(foo.oneOf("bar", "foo", "bar"));
+ EXPECT_TRUE(foo.oneOf("foo", "foo", "foo"));
+
+ EXPECT_FALSE(foo.oneOf(""));
+ EXPECT_FALSE(foo.oneOf("bar"));
+ EXPECT_FALSE(foo.oneOf("bar", ""));
+ EXPECT_FALSE(foo.oneOf("bar", "bar"));
+
+ ConstString empty("");
+ EXPECT_TRUE(empty.oneOf(""));
+ EXPECT_TRUE(empty.oneOf("foo", ""));
+ EXPECT_TRUE(empty.oneOf("foo", "", "foo"));
+ EXPECT_TRUE(empty.oneOf("foo", "", "foo"));
+ EXPECT_TRUE(empty.oneOf("foo", "foo", ""));
+}
Index: lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp
===================================================================
--- lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp
+++ lldb/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp
@@ -429,8 +429,7 @@
PlatformDarwinKernel *thisp = (PlatformDarwinKernel *)baton;
FileSpec file_spec(path);
if (ft == llvm::sys::fs::file_type::directory_file &&
- (file_spec.GetFileNameExtension() == g_sdk_suffix ||
- file_spec.GetFileNameExtension() == g_kdk_suffix)) {
+ file_spec.GetFileNameExtension().oneOf(g_sdk_suffix, g_kdk_suffix)) {
AddRootSubdirsToSearchPaths(thisp, file_spec.GetPath());
}
return FileSystem::eEnumerateDirectoryResultNext;
Index: lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
===================================================================
--- lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
+++ lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
@@ -736,21 +736,18 @@
static ConstString g_sect_name_go_symtab(".gosymtab");
SectionType section_type = eSectionTypeOther;
if (m_sect_headers[idx].flags & llvm::COFF::IMAGE_SCN_CNT_CODE &&
- ((const_sect_name == g_code_sect_name) ||
- (const_sect_name == g_CODE_sect_name))) {
+ const_sect_name.oneOf(g_code_sect_name, g_CODE_sect_name)) {
section_type = eSectionTypeCode;
} else if (m_sect_headers[idx].flags &
llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA &&
- ((const_sect_name == g_data_sect_name) ||
- (const_sect_name == g_DATA_sect_name))) {
+ const_sect_name.oneOf(g_data_sect_name, g_DATA_sect_name)) {
if (m_sect_headers[idx].size == 0 && m_sect_headers[idx].offset == 0)
section_type = eSectionTypeZeroFill;
else
section_type = eSectionTypeData;
} else if (m_sect_headers[idx].flags &
llvm::COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA &&
- ((const_sect_name == g_bss_sect_name) ||
- (const_sect_name == g_BSS_sect_name))) {
+ const_sect_name.oneOf(g_bss_sect_name, g_BSS_sect_name)) {
if (m_sect_headers[idx].size == 0)
section_type = eSectionTypeZeroFill;
else
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===================================================================
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -1996,8 +1996,7 @@
// custom extension and file name makes it highly unlikely that this will
// collide with anything else.
ConstString file_extension = m_file.GetFileNameExtension();
- bool skip_oatdata_oatexec =
- file_extension == ".oat" || file_extension == ".odex";
+ bool skip_oatdata_oatexec = file_extension.oneOf(".oat", ".odex");
ArchSpec arch = GetArchitecture();
ModuleSP module_sp(GetModule());
Index: lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
===================================================================
--- lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
+++ lldb/source/Plugins/Language/ObjC/ObjCLanguage.cpp
@@ -1036,7 +1036,7 @@
prefix.clear();
suffix.clear();
- if (type_hint == g_CFBag || type_hint == g_CFBinaryHeap) {
+ if (type_hint.oneOf(g_CFBag, g_CFBinaryHeap)) {
prefix = "@";
return true;
}
@@ -1070,13 +1070,13 @@
return true;
}
- if (type_hint == g_NSData || type_hint == g_NSArray) {
+ if (type_hint.oneOf(g_NSData, g_NSArray)) {
prefix = "@\"";
suffix = "\"";
return true;
}
- if (type_hint == g_NSString || type_hint == g_NSStringStar) {
+ if (type_hint.oneOf(g_NSString, g_NSStringStar)) {
prefix = "@";
return true;
}
Index: lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
===================================================================
--- lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
+++ lldb/source/Plugins/Language/ObjC/NSDictionary.cpp
@@ -383,15 +383,15 @@
if (class_name.IsEmpty())
return false;
- if (class_name == g_DictionaryI || class_name == g_DictionaryMImmutable) {
+ if (class_name.oneOf(g_DictionaryI, g_DictionaryMImmutable)) {
Status error;
value = process_sp->ReadUnsignedIntegerFromMemory(valobj_addr + ptr_size,
ptr_size, 0, error);
if (error.Fail())
return false;
value &= (is_64bit ? ~0xFC00000000000000UL : ~0xFC000000U);
- } else if (class_name == g_DictionaryM || class_name == g_DictionaryMLegacy ||
- class_name == g_DictionaryCF) {
+ } else if (class_name.oneOf(g_DictionaryM, g_DictionaryMLegacy,
+ g_DictionaryCF)) {
AppleObjCRuntime *apple_runtime =
llvm::dyn_cast_or_null<AppleObjCRuntime>(runtime);
Status error;
@@ -409,8 +409,7 @@
value = 1;
} else if (class_name == g_Dictionary0) {
value = 0;
- }
- else {
+ } else {
auto &map(NSDictionary_Additionals::GetAdditionalSummaries());
for (auto &candidate : map) {
if (candidate.first && candidate.first->Match(class_name))
Index: lldb/source/Plugins/Language/ObjC/Cocoa.cpp
===================================================================
--- lldb/source/Plugins/Language/ObjC/Cocoa.cpp
+++ lldb/source/Plugins/Language/ObjC/Cocoa.cpp
@@ -829,8 +829,7 @@
return false;
uint64_t info_bits = 0, value_bits = 0;
- if ((class_name == g_NSDate) || (class_name == g___NSDate) ||
- (class_name == g___NSTaggedDate)) {
+ if (class_name.oneOf(g_NSDate, g___NSDate, g___NSTaggedDate)) {
if (descriptor->GetTaggedPointerInfo(&info_bits, &value_bits)) {
date_value_bits = ((value_bits << 8) | (info_bits << 4));
memcpy(&date_value, &date_value_bits, sizeof(date_value_bits));
Index: lldb/source/Plugins/Language/ObjC/CF.cpp
===================================================================
--- lldb/source/Plugins/Language/ObjC/CF.cpp
+++ lldb/source/Plugins/Language/ObjC/CF.cpp
@@ -139,8 +139,8 @@
bool is_type_ok = false; // check to see if this is a CFBag we know about
if (descriptor->IsCFType()) {
ConstString type_name(valobj.GetTypeName());
- if (type_name == "__CFMutableBitVector" || type_name == "__CFBitVector" ||
- type_name == "CFMutableBitVectorRef" || type_name == "CFBitVectorRef") {
+ if (type_name.oneOf("__CFMutableBitVector", "__CFBitVector",
+ "CFMutableBitVectorRef", "CFBitVectorRef")) {
if (valobj.IsPointerType())
is_type_ok = true;
}
Index: lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp
===================================================================
--- lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp
+++ lldb/source/Plugins/Language/CPlusPlus/LibStdcppUniquePointer.cpp
@@ -130,11 +130,11 @@
size_t LibStdcppUniquePtrSyntheticFrontEnd::GetIndexOfChildWithName(
ConstString name) {
- if (name == "ptr" || name == "pointer")
+ if (name.oneOf("ptr", "pointer"))
return 0;
- if (name == "del" || name == "deleter")
+ if (name.oneOf("del", "deleter"))
return 1;
- if (name == "obj" || name == "object" || name == "$$dereference$$")
+ if (name.oneOf("obj", "object", "$$dereference$$"))
return 2;
return UINT32_MAX;
}
Index: lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
===================================================================
--- lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
+++ lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp
@@ -167,7 +167,7 @@
lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
ConstString var_name = var_sp->GetName();
- if (!var_name || var_name == "this" || var_name == ".block_descriptor")
+ if (!var_name || var_name.oneOf("this", ".block_descriptor"))
continue;
stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
Index: lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
===================================================================
--- lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
+++ lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
@@ -767,7 +767,7 @@
static const ConstString Class_name("Class");
if (m_ast_context->getLangOpts().ObjC)
- if (name == id_name || name == Class_name)
+ if (name.oneOf(id_name, Class_name))
return true;
StringRef name_string_ref = name.GetStringRef();
Index: lldb/source/Core/Debugger.cpp
===================================================================
--- lldb/source/Core/Debugger.cpp
+++ lldb/source/Core/Debugger.cpp
@@ -633,8 +633,8 @@
FileSpec plugin_file_spec(path);
FileSystem::Instance().Resolve(plugin_file_spec);
- if (plugin_file_spec.GetFileNameExtension() != g_dylibext &&
- plugin_file_spec.GetFileNameExtension() != g_solibext) {
+ if (!plugin_file_spec.GetFileNameExtension().oneOf(g_dylibext,
+ g_solibext)) {
return FileSystem::eEnumerateDirectoryResultNext;
}
Index: lldb/include/lldb/Utility/ConstString.h
===================================================================
--- lldb/include/lldb/Utility/ConstString.h
+++ lldb/include/lldb/Utility/ConstString.h
@@ -211,6 +211,14 @@
bool operator<(ConstString rhs) const;
+ /// Compares this ConstString against the given set of strings. Returns true
+ /// iff at least one of the given strings is equal to this ConstString.
+ template <typename T, typename... Args>
+ bool oneOf(T first, Args... args) const {
+ return oneOf(first) || oneOf(args...);
+ }
+ template <typename T> bool oneOf(T first) const { return *this == first; }
+
/// Get the string value as a C string.
///
/// Get the value of the contained string as a NULL terminated C string
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits