Skip to content

Commit 5e9f247

Browse files
authored
[lldb] Make LanguageRuntime::GetTypeBitSize return an optional (NFC) (#96013)
Make LanguageRuntime::GetTypeBitSize return an optional. This should be NFC, though the ObjCLanguageRuntime implementation is (possibly) more defensive against returning 0. I'm not sure if it's possible for both `m_ivar.size` and `m_ivar.offset` to be zero. Previously, we'd return 0 and cache it, only to discard it the next time when finding it in the cache, and recomputing it again. The new code will avoid putting it in the cache in the first place.
1 parent 482c41e commit 5e9f247

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

lldb/include/lldb/Target/LanguageRuntime.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ class LanguageRuntime : public Runtime, public PluginInterface {
170170
return m_process->GetTarget().GetSearchFilterForModule(nullptr);
171171
}
172172

173-
virtual bool GetTypeBitSize(const CompilerType &compiler_type,
174-
uint64_t &size) {
175-
return false;
173+
virtual std::optional<uint64_t>
174+
GetTypeBitSize(const CompilerType &compiler_type) {
175+
return {};
176176
}
177177

178178
virtual void SymbolsDidLoad(const ModuleList &module_list) {}

lldb/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,18 +350,17 @@ ObjCLanguageRuntime::EncodingToTypeSP ObjCLanguageRuntime::GetEncodingToType() {
350350
return nullptr;
351351
}
352352

353-
bool ObjCLanguageRuntime::GetTypeBitSize(const CompilerType &compiler_type,
354-
uint64_t &size) {
353+
std::optional<uint64_t>
354+
ObjCLanguageRuntime::GetTypeBitSize(const CompilerType &compiler_type) {
355355
void *opaque_ptr = compiler_type.GetOpaqueQualType();
356-
size = m_type_size_cache.Lookup(opaque_ptr);
357-
// an ObjC object will at least have an ISA, so 0 is definitely not OK
358-
if (size > 0)
359-
return true;
356+
uint64_t cached_size = m_type_size_cache.Lookup(opaque_ptr);
357+
if (cached_size > 0)
358+
return cached_size;
360359

361360
ClassDescriptorSP class_descriptor_sp =
362361
GetClassDescriptorFromClassName(compiler_type.GetTypeName());
363362
if (!class_descriptor_sp)
364-
return false;
363+
return {};
365364

366365
int32_t max_offset = INT32_MIN;
367366
uint64_t sizeof_max = 0;
@@ -377,11 +376,13 @@ bool ObjCLanguageRuntime::GetTypeBitSize(const CompilerType &compiler_type,
377376
}
378377
}
379378

380-
size = 8 * (max_offset + sizeof_max);
381-
if (found)
379+
uint64_t size = 8 * (max_offset + sizeof_max);
380+
if (found && size > 0) {
382381
m_type_size_cache.Insert(opaque_ptr, size);
382+
return size;
383+
}
383384

384-
return found;
385+
return {};
385386
}
386387

387388
lldb::BreakpointPreconditionSP

lldb/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class ObjCLanguageRuntime : public LanguageRuntime {
107107
int64_t *value_bits = nullptr,
108108
uint64_t *payload = nullptr) = 0;
109109
/// @}
110-
110+
111111
virtual uint64_t GetInstanceSize() = 0;
112112

113113
// use to implement version-specific additional constraints on pointers
@@ -321,8 +321,8 @@ class ObjCLanguageRuntime : public LanguageRuntime {
321321
m_negative_complete_class_cache.clear();
322322
}
323323

324-
bool GetTypeBitSize(const CompilerType &compiler_type,
325-
uint64_t &size) override;
324+
std::optional<uint64_t>
325+
GetTypeBitSize(const CompilerType &compiler_type) override;
326326

327327
/// Check whether the name is "self" or "_cmd" and should show up in
328328
/// "frame variable".

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4744,11 +4744,11 @@ TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type,
47444744
ExecutionContext exe_ctx(exe_scope);
47454745
Process *process = exe_ctx.GetProcessPtr();
47464746
if (process) {
4747-
ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
4748-
if (objc_runtime) {
4749-
uint64_t bit_size = 0;
4750-
if (objc_runtime->GetTypeBitSize(GetType(qual_type), bit_size))
4751-
return bit_size;
4747+
if (ObjCLanguageRuntime *objc_runtime =
4748+
ObjCLanguageRuntime::Get(*process)) {
4749+
if (std::optional<uint64_t> bit_size =
4750+
objc_runtime->GetTypeBitSize(GetType(qual_type)))
4751+
return *bit_size;
47524752
}
47534753
} else {
47544754
static bool g_printed = false;

0 commit comments

Comments
 (0)