Skip to content

Commit c0c3562

Browse files
committed
[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. (cherry picked from commit 11a7ee5)
1 parent 162ee50 commit c0c3562

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
@@ -4915,67 +4915,68 @@ TypeSystemClang::GetFloatTypeSemantics(size_t byte_size) {
49154915
return llvm::APFloatBase::Bogus();
49164916
}
49174917

4918+
std::optional<uint64_t>
4919+
TypeSystemClang::GetObjCBitSize(QualType qual_type,
4920+
ExecutionContextScope *exe_scope) {
4921+
assert(qual_type->isObjCObjectOrInterfaceType());
4922+
ExecutionContext exe_ctx(exe_scope);
4923+
if (Process *process = exe_ctx.GetProcessPtr()) {
4924+
if (ObjCLanguageRuntime *objc_runtime =
4925+
ObjCLanguageRuntime::Get(*process)) {
4926+
if (std::optional<uint64_t> bit_size =
4927+
objc_runtime->GetTypeBitSize(GetType(qual_type)))
4928+
return *bit_size;
4929+
}
4930+
} else {
4931+
static bool g_printed = false;
4932+
if (!g_printed) {
4933+
StreamString s;
4934+
DumpTypeDescription(qual_type.getAsOpaquePtr(), s);
4935+
4936+
llvm::outs() << "warning: trying to determine the size of type ";
4937+
llvm::outs() << s.GetString() << "\n";
4938+
llvm::outs() << "without a valid ExecutionContext. this is not "
4939+
"reliable. please file a bug against LLDB.\n";
4940+
llvm::outs() << "backtrace:\n";
4941+
llvm::sys::PrintStackTrace(llvm::outs());
4942+
llvm::outs() << "\n";
4943+
g_printed = true;
4944+
}
4945+
}
4946+
4947+
return getASTContext().getTypeSize(qual_type) +
4948+
getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy);
4949+
}
4950+
49184951
std::optional<uint64_t>
49194952
TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type,
49204953
ExecutionContextScope *exe_scope) {
4921-
if (GetCompleteType(type)) {
4922-
clang::QualType qual_type(GetCanonicalQualType(type));
4923-
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4924-
switch (type_class) {
4925-
case clang::Type::Record:
4926-
if (GetCompleteType(type))
4927-
return getASTContext().getTypeSize(qual_type);
4928-
else
4929-
return std::nullopt;
4930-
break;
4954+
if (!GetCompleteType(type))
4955+
return std::nullopt;
49314956

4932-
case clang::Type::ObjCInterface:
4933-
case clang::Type::ObjCObject: {
4934-
ExecutionContext exe_ctx(exe_scope);
4935-
Process *process = exe_ctx.GetProcessPtr();
4936-
if (process) {
4937-
if (ObjCLanguageRuntime *objc_runtime =
4938-
ObjCLanguageRuntime::Get(*process)) {
4939-
if (std::optional<uint64_t> bit_size =
4940-
objc_runtime->GetTypeBitSize(GetType(qual_type)))
4941-
return *bit_size;
4942-
}
4943-
} else {
4944-
static bool g_printed = false;
4945-
if (!g_printed) {
4946-
StreamString s;
4947-
DumpTypeDescription(type, s);
4948-
4949-
llvm::outs() << "warning: trying to determine the size of type ";
4950-
llvm::outs() << s.GetString() << "\n";
4951-
llvm::outs() << "without a valid ExecutionContext. this is not "
4952-
"reliable. please file a bug against LLDB.\n";
4953-
llvm::outs() << "backtrace:\n";
4954-
llvm::sys::PrintStackTrace(llvm::outs());
4955-
llvm::outs() << "\n";
4956-
g_printed = true;
4957-
}
4958-
}
4959-
}
4960-
[[fallthrough]];
4961-
default:
4962-
const uint32_t bit_size = getASTContext().getTypeSize(qual_type);
4963-
if (bit_size == 0) {
4964-
if (qual_type->isIncompleteArrayType())
4965-
return getASTContext().getTypeSize(
4966-
qual_type->getArrayElementTypeNoTypeQual()
4967-
->getCanonicalTypeUnqualified());
4968-
}
4969-
if (qual_type->isObjCObjectOrInterfaceType())
4970-
return bit_size +
4971-
getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy);
4972-
// Function types actually have a size of 0, that's not an error.
4973-
if (qual_type->isFunctionProtoType())
4974-
return bit_size;
4975-
if (bit_size)
4976-
return bit_size;
4977-
}
4957+
clang::QualType qual_type(GetCanonicalQualType(type));
4958+
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4959+
switch (type_class) {
4960+
case clang::Type::FunctionProto:
4961+
case clang::Type::Record:
4962+
return getASTContext().getTypeSize(qual_type);
4963+
case clang::Type::ObjCInterface:
4964+
case clang::Type::ObjCObject:
4965+
return GetObjCBitSize(qual_type, exe_scope);
4966+
case clang::Type::IncompleteArray: {
4967+
const uint64_t bit_size = getASTContext().getTypeSize(qual_type);
4968+
if (bit_size == 0)
4969+
return getASTContext().getTypeSize(
4970+
qual_type->getArrayElementTypeNoTypeQual()
4971+
->getCanonicalTypeUnqualified());
4972+
4973+
return bit_size;
4974+
}
4975+
default:
4976+
if (const uint64_t bit_size = getASTContext().getTypeSize(qual_type))
4977+
return bit_size;
49784978
}
4979+
49794980
return std::nullopt;
49804981
}
49814982

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

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

1256+
std::optional<uint64_t> GetObjCBitSize(clang::QualType qual_type,
1257+
ExecutionContextScope *exe_scope);
1258+
12561259
// Classes that inherit from TypeSystemClang can see and modify these
12571260
std::string m_target_triple;
12581261
std::unique_ptr<clang::ASTContext> m_ast_up;

0 commit comments

Comments
 (0)