Make CompilerType::getBitSize() / getByteSize() return an optional result. NFC
The code in LLDB assumes that CompilerType and friends use the size 0
as a sentinel value to signal an error. This works for C++, where no
zero-sized type exists, but in many other programming languages
(including I believe C) types of size zero are possible and even
common. This is a particular pain point in swift-lldb, where extra
code exists to double-check that a type is *really* of size zero and
not an error at various locations.
To remedy this situation, this patch starts by converting
CompilerType::getBitSize() and getByteSize() to return an optional
result. To avoid wasting space, I hand-rolled my own optional data
type assuming that no type is larger than what fits into 63
bits. Follow-up patches would make similar changes to the ValueObject
hierarchy.
rdar://problem/47178964
Differential Revision: https://reviews.llvm.org/D56688
llvm-svn: 351214
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
index fde610a..1a4a143 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/IRForTarget.cpp
@@ -310,12 +310,14 @@
lldb::TargetSP target_sp(m_execution_unit.GetTarget());
lldb_private::ExecutionContext exe_ctx(target_sp, true);
- if (m_result_type.GetBitSize(exe_ctx.GetBestExecutionContextScope()) == 0) {
+ auto bit_size =
+ m_result_type.GetBitSize(exe_ctx.GetBestExecutionContextScope());
+ if (!bit_size) {
lldb_private::StreamString type_desc_stream;
m_result_type.DumpTypeDescription(&type_desc_stream);
if (log)
- log->Printf("Result type has size 0");
+ log->Printf("Result type has unknown size");
m_error_stream.Printf("Error [IRForTarget]: Size of result type '%s' "
"couldn't be determined\n",
@@ -334,7 +336,10 @@
if (log)
log->Printf("Creating a new result global: \"%s\" with size 0x%" PRIx64,
- m_result_name.GetCString(), m_result_type.GetByteSize(nullptr));
+ m_result_name.GetCString(),
+ m_result_type.GetByteSize(nullptr)
+ ? *m_result_type.GetByteSize(nullptr)
+ : 0);
// Construct a new result global and set up its metadata
@@ -1367,7 +1372,9 @@
value_type = global_variable->getType();
}
- const uint64_t value_size = compiler_type.GetByteSize(nullptr);
+ auto value_size = compiler_type.GetByteSize(nullptr);
+ if (!value_size)
+ return false;
lldb::offset_t value_alignment =
(compiler_type.GetTypeBitAlign() + 7ull) / 8ull;
@@ -1378,13 +1385,13 @@
lldb_private::ClangUtil::GetQualType(compiler_type)
.getAsString()
.c_str(),
- PrintType(value_type).c_str(), value_size, value_alignment);
+ PrintType(value_type).c_str(), *value_size, value_alignment);
}
if (named_decl &&
!m_decl_map->AddValueToStruct(
named_decl, lldb_private::ConstString(name.c_str()), llvm_value_ptr,
- value_size, value_alignment)) {
+ *value_size, value_alignment)) {
if (!global_variable->hasExternalLinkage())
return true;
else