Skip to content

Commit b1a7b7f

Browse files
committed
[lldb][NFCI] Refactor TypeSystemClang::GetBasicTypeEnumeration
`TypeSystemClang::GetBasicTypeEnumeration(ConstString)` builds up a giant `UniqueCStringMap<lldb::BasicType>` which is effectively a vector whose elements are (ConstString, lldb::BasicType). Once sorted, we get to have fairly quick lookups (O(log n) on average). This is fine, but we can do better on average with an llvm::StringMap. This also allows us to simplify the initialization code for the lookup, no more `once_flag`! Additionally, I removed unused declarations of related functions from CompilerType and TypeSystemClang Differential Revision: https://reviews.llvm.org/D152315
1 parent d29b567 commit b1a7b7f

File tree

3 files changed

+43
-57
lines changed

3 files changed

+43
-57
lines changed

lldb/include/lldb/Symbol/CompilerType.h

-2
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,6 @@ class CompilerType {
344344

345345
lldb::BasicType GetBasicTypeEnumeration() const;
346346

347-
static lldb::BasicType GetBasicTypeEnumeration(ConstString name);
348-
349347
/// If this type is an enumeration, iterate through all of its enumerators
350348
/// using a callback. If the callback returns true, keep iterating, else abort
351349
/// the iteration.

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

+42-50
Original file line numberDiff line numberDiff line change
@@ -864,70 +864,62 @@ TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
864864
return CompilerType();
865865
}
866866

867-
lldb::BasicType
868-
TypeSystemClang::GetBasicTypeEnumeration(ConstString name) {
869-
if (name) {
870-
typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
871-
static TypeNameToBasicTypeMap g_type_map;
872-
static llvm::once_flag g_once_flag;
873-
llvm::call_once(g_once_flag, []() {
867+
lldb::BasicType TypeSystemClang::GetBasicTypeEnumeration(llvm::StringRef name) {
868+
static const llvm::StringMap<lldb::BasicType> g_type_map = {
874869
// "void"
875-
g_type_map.Append(ConstString("void"), eBasicTypeVoid);
870+
{"void", eBasicTypeVoid},
876871

877872
// "char"
878-
g_type_map.Append(ConstString("char"), eBasicTypeChar);
879-
g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
880-
g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
881-
g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
882-
g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
883-
g_type_map.Append(ConstString("unsigned wchar_t"),
884-
eBasicTypeUnsignedWChar);
873+
{"char", eBasicTypeChar},
874+
{"signed char", eBasicTypeSignedChar},
875+
{"unsigned char", eBasicTypeUnsignedChar},
876+
{"wchar_t", eBasicTypeWChar},
877+
{"signed wchar_t", eBasicTypeSignedWChar},
878+
{"unsigned wchar_t", eBasicTypeUnsignedWChar},
879+
885880
// "short"
886-
g_type_map.Append(ConstString("short"), eBasicTypeShort);
887-
g_type_map.Append(ConstString("short int"), eBasicTypeShort);
888-
g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
889-
g_type_map.Append(ConstString("unsigned short int"),
890-
eBasicTypeUnsignedShort);
881+
{"short", eBasicTypeShort},
882+
{"short int", eBasicTypeShort},
883+
{"unsigned short", eBasicTypeUnsignedShort},
884+
{"unsigned short int", eBasicTypeUnsignedShort},
891885

892886
// "int"
893-
g_type_map.Append(ConstString("int"), eBasicTypeInt);
894-
g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
895-
g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
896-
g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
887+
{"int", eBasicTypeInt},
888+
{"signed int", eBasicTypeInt},
889+
{"unsigned int", eBasicTypeUnsignedInt},
890+
{"unsigned", eBasicTypeUnsignedInt},
897891

898892
// "long"
899-
g_type_map.Append(ConstString("long"), eBasicTypeLong);
900-
g_type_map.Append(ConstString("long int"), eBasicTypeLong);
901-
g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
902-
g_type_map.Append(ConstString("unsigned long int"),
903-
eBasicTypeUnsignedLong);
893+
{"long", eBasicTypeLong},
894+
{"long int", eBasicTypeLong},
895+
{"unsigned long", eBasicTypeUnsignedLong},
896+
{"unsigned long int", eBasicTypeUnsignedLong},
904897

905898
// "long long"
906-
g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
907-
g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
908-
g_type_map.Append(ConstString("unsigned long long"),
909-
eBasicTypeUnsignedLongLong);
910-
g_type_map.Append(ConstString("unsigned long long int"),
911-
eBasicTypeUnsignedLongLong);
899+
{"long long", eBasicTypeLongLong},
900+
{"long long int", eBasicTypeLongLong},
901+
{"unsigned long long", eBasicTypeUnsignedLongLong},
902+
{"unsigned long long int", eBasicTypeUnsignedLongLong},
912903

913904
// "int128"
914-
g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
915-
g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
905+
{"__int128_t", eBasicTypeInt128},
906+
{"__uint128_t", eBasicTypeUnsignedInt128},
916907

917908
// Miscellaneous
918-
g_type_map.Append(ConstString("bool"), eBasicTypeBool);
919-
g_type_map.Append(ConstString("float"), eBasicTypeFloat);
920-
g_type_map.Append(ConstString("double"), eBasicTypeDouble);
921-
g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
922-
g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
923-
g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
924-
g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
925-
g_type_map.Sort();
926-
});
927-
928-
return g_type_map.Find(name, eBasicTypeInvalid);
929-
}
930-
return eBasicTypeInvalid;
909+
{"bool", eBasicTypeBool},
910+
{"float", eBasicTypeFloat},
911+
{"double", eBasicTypeDouble},
912+
{"long double", eBasicTypeLongDouble},
913+
{"id", eBasicTypeObjCID},
914+
{"SEL", eBasicTypeObjCSel},
915+
{"nullptr", eBasicTypeNullPtr},
916+
};
917+
918+
auto iter = g_type_map.find(name);
919+
if (iter == g_type_map.end())
920+
return eBasicTypeInvalid;
921+
922+
return iter->second;
931923
}
932924

933925
uint32_t TypeSystemClang::GetPointerByteSize() {

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class TypeSystemClang : public TypeSystem {
207207

208208
CompilerType GetBasicType(lldb::BasicType type);
209209

210-
static lldb::BasicType GetBasicTypeEnumeration(ConstString name);
210+
static lldb::BasicType GetBasicTypeEnumeration(llvm::StringRef name);
211211

212212
CompilerType
213213
GetBuiltinTypeForDWARFEncodingAndBitSize(llvm::StringRef type_name,
@@ -834,10 +834,6 @@ class TypeSystemClang : public TypeSystem {
834834
lldb::BasicType
835835
GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override;
836836

837-
static lldb::BasicType
838-
GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type,
839-
ConstString name);
840-
841837
void ForEachEnumerator(
842838
lldb::opaque_compiler_type_t type,
843839
std::function<bool(const CompilerType &integer_type,

0 commit comments

Comments
 (0)