Skip to content

Commit 11a7ee5

Browse files
authored
[lldb][TypeSystemClang][NFC] Clean up TypeSystemClang::GetBitSize (llvm#100674)
This patch performs following NFC changes to TypeSystemClang::GetBitSize: * Factor out the Objective-C logic into a helper function. * Introduce a new case for `FunctionProto`. I don't see a good reason for special-casing this in the `default` case. * We had a redundant check for `GetCompleteType` in the `RecordType` case. We used to allow zero-size types for records and function prototypes, so we can just fold them into the same case. * Introduce a new case for `IncompleteArray`. The motivation for this is that there are a few issues around VLAs (i.e., `Type::IncompleteArray`) whose fixes will be easier to reason about after this refactor. Generally I don't think treating a type with 0 size as an error is the right thing to do (at least not for array types). But that's not something I wanted to fold into this NFC change.
1 parent 6ac0cf9 commit 11a7ee5

File tree

2 files changed

+60
-56
lines changed

2 files changed

+60
-56
lines changed

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

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4725,67 +4725,68 @@ TypeSystemClang::GetFloatTypeSemantics(size_t byte_size) {
47254725
return llvm::APFloatBase::Bogus();
47264726
}
47274727

4728+
std::optional<uint64_t>
4729+
TypeSystemClang::GetObjCBitSize(QualType qual_type,
4730+
ExecutionContextScope *exe_scope) {
4731+
assert(qual_type->isObjCObjectOrInterfaceType());
4732+
ExecutionContext exe_ctx(exe_scope);
4733+
if (Process *process = exe_ctx.GetProcessPtr()) {
4734+
if (ObjCLanguageRuntime *objc_runtime =
4735+
ObjCLanguageRuntime::Get(*process)) {
4736+
if (std::optional<uint64_t> bit_size =
4737+
objc_runtime->GetTypeBitSize(GetType(qual_type)))
4738+
return *bit_size;
4739+
}
4740+
} else {
4741+
static bool g_printed = false;
4742+
if (!g_printed) {
4743+
StreamString s;
4744+
DumpTypeDescription(qual_type.getAsOpaquePtr(), s);
4745+
4746+
llvm::outs() << "warning: trying to determine the size of type ";
4747+
llvm::outs() << s.GetString() << "\n";
4748+
llvm::outs() << "without a valid ExecutionContext. this is not "
4749+
"reliable. please file a bug against LLDB.\n";
4750+
llvm::outs() << "backtrace:\n";
4751+
llvm::sys::PrintStackTrace(llvm::outs());
4752+
llvm::outs() << "\n";
4753+
g_printed = true;
4754+
}
4755+
}
4756+
4757+
return getASTContext().getTypeSize(qual_type) +
4758+
getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy);
4759+
}
4760+
47284761
std::optional<uint64_t>
47294762
TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type,
47304763
ExecutionContextScope *exe_scope) {
4731-
if (GetCompleteType(type)) {
4732-
clang::QualType qual_type(GetCanonicalQualType(type));
4733-
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4734-
switch (type_class) {
4735-
case clang::Type::Record:
4736-
if (GetCompleteType(type))
4737-
return getASTContext().getTypeSize(qual_type);
4738-
else
4739-
return std::nullopt;
4740-
break;
4764+
if (!GetCompleteType(type))
4765+
return std::nullopt;
47414766

4742-
case clang::Type::ObjCInterface:
4743-
case clang::Type::ObjCObject: {
4744-
ExecutionContext exe_ctx(exe_scope);
4745-
Process *process = exe_ctx.GetProcessPtr();
4746-
if (process) {
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;
4752-
}
4753-
} else {
4754-
static bool g_printed = false;
4755-
if (!g_printed) {
4756-
StreamString s;
4757-
DumpTypeDescription(type, s);
4758-
4759-
llvm::outs() << "warning: trying to determine the size of type ";
4760-
llvm::outs() << s.GetString() << "\n";
4761-
llvm::outs() << "without a valid ExecutionContext. this is not "
4762-
"reliable. please file a bug against LLDB.\n";
4763-
llvm::outs() << "backtrace:\n";
4764-
llvm::sys::PrintStackTrace(llvm::outs());
4765-
llvm::outs() << "\n";
4766-
g_printed = true;
4767-
}
4768-
}
4769-
}
4770-
[[fallthrough]];
4771-
default:
4772-
const uint32_t bit_size = getASTContext().getTypeSize(qual_type);
4773-
if (bit_size == 0) {
4774-
if (qual_type->isIncompleteArrayType())
4775-
return getASTContext().getTypeSize(
4776-
qual_type->getArrayElementTypeNoTypeQual()
4777-
->getCanonicalTypeUnqualified());
4778-
}
4779-
if (qual_type->isObjCObjectOrInterfaceType())
4780-
return bit_size +
4781-
getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy);
4782-
// Function types actually have a size of 0, that's not an error.
4783-
if (qual_type->isFunctionProtoType())
4784-
return bit_size;
4785-
if (bit_size)
4786-
return bit_size;
4787-
}
4767+
clang::QualType qual_type(GetCanonicalQualType(type));
4768+
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4769+
switch (type_class) {
4770+
case clang::Type::FunctionProto:
4771+
case clang::Type::Record:
4772+
return getASTContext().getTypeSize(qual_type);
4773+
case clang::Type::ObjCInterface:
4774+
case clang::Type::ObjCObject:
4775+
return GetObjCBitSize(qual_type, exe_scope);
4776+
case clang::Type::IncompleteArray: {
4777+
const uint64_t bit_size = getASTContext().getTypeSize(qual_type);
4778+
if (bit_size == 0)
4779+
return getASTContext().getTypeSize(
4780+
qual_type->getArrayElementTypeNoTypeQual()
4781+
->getCanonicalTypeUnqualified());
4782+
4783+
return bit_size;
4784+
}
4785+
default:
4786+
if (const uint64_t bit_size = getASTContext().getTypeSize(qual_type))
4787+
return bit_size;
47884788
}
4789+
47894790
return std::nullopt;
47904791
}
47914792

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,9 @@ class TypeSystemClang : public TypeSystem {
11721172
/// on creation of a new instance.
11731173
void LogCreation() const;
11741174

1175+
std::optional<uint64_t> GetObjCBitSize(clang::QualType qual_type,
1176+
ExecutionContextScope *exe_scope);
1177+
11751178
// Classes that inherit from TypeSystemClang can see and modify these
11761179
std::string m_target_triple;
11771180
std::unique_ptr<clang::ASTContext> m_ast_up;

0 commit comments

Comments
 (0)