Skip to content

Commit a878ed1

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 21406e9 commit a878ed1

File tree

1 file changed

+23
-58
lines changed

1 file changed

+23
-58
lines changed

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

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4956,66 +4956,31 @@ TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type,
49564956
return llvm::createStringError(
49574957
"could not complete type %s",
49584958
GetTypeName(type, base_name_only).AsCString(""));
4959-
if (GetCompleteType(type)) {
4960-
clang::QualType qual_type(GetCanonicalQualType(type));
4961-
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4962-
switch (type_class) {
4963-
case clang::Type::Record:
4964-
if (GetCompleteType(type))
4965-
return getASTContext().getTypeSize(qual_type);
4966-
else
4967-
return llvm::createStringError(
4968-
"could not complete type %s",
4969-
GetTypeName(type, base_name_only).AsCString(""));
4970-
break;
49714959

4972-
case clang::Type::ObjCInterface:
4973-
case clang::Type::ObjCObject: {
4974-
ExecutionContext exe_ctx(exe_scope);
4975-
Process *process = exe_ctx.GetProcessPtr();
4976-
if (process) {
4977-
if (ObjCLanguageRuntime *objc_runtime =
4978-
ObjCLanguageRuntime::Get(*process)) {
4979-
if (std::optional<uint64_t> bit_size =
4980-
objc_runtime->GetTypeBitSize(GetType(qual_type)))
4981-
return *bit_size;
4982-
}
4983-
} else {
4984-
static bool g_printed = false;
4985-
if (!g_printed) {
4986-
StreamString s;
4987-
DumpTypeDescription(type, s);
4988-
4989-
llvm::outs() << "warning: trying to determine the size of type ";
4990-
llvm::outs() << s.GetString() << "\n";
4991-
llvm::outs() << "without a valid ExecutionContext. this is not "
4992-
"reliable. please file a bug against LLDB.\n";
4993-
llvm::outs() << "backtrace:\n";
4994-
llvm::sys::PrintStackTrace(llvm::outs());
4995-
llvm::outs() << "\n";
4996-
g_printed = true;
4997-
}
4998-
}
4999-
}
5000-
[[fallthrough]];
5001-
default:
5002-
const uint32_t bit_size = getASTContext().getTypeSize(qual_type);
5003-
if (bit_size == 0) {
5004-
if (qual_type->isIncompleteArrayType())
5005-
return getASTContext().getTypeSize(
5006-
qual_type->getArrayElementTypeNoTypeQual()
5007-
->getCanonicalTypeUnqualified());
5008-
}
5009-
if (qual_type->isObjCObjectOrInterfaceType())
5010-
return bit_size +
5011-
getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy);
5012-
// Function types actually have a size of 0, that's not an error.
5013-
if (qual_type->isFunctionProtoType())
5014-
return bit_size;
5015-
if (bit_size)
5016-
return bit_size;
5017-
}
4960+
clang::QualType qual_type(GetCanonicalQualType(type));
4961+
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4962+
switch (type_class) {
4963+
case clang::Type::ConstantArray:
4964+
case clang::Type::FunctionProto:
4965+
case clang::Type::Record:
4966+
return getASTContext().getTypeSize(qual_type);
4967+
case clang::Type::ObjCInterface:
4968+
case clang::Type::ObjCObject:
4969+
return GetObjCBitSize(qual_type, exe_scope);
4970+
case clang::Type::IncompleteArray: {
4971+
const uint64_t bit_size = getASTContext().getTypeSize(qual_type);
4972+
if (bit_size == 0)
4973+
return getASTContext().getTypeSize(
4974+
qual_type->getArrayElementTypeNoTypeQual()
4975+
->getCanonicalTypeUnqualified());
4976+
4977+
return bit_size;
50184978
}
4979+
default:
4980+
if (const uint64_t bit_size = getASTContext().getTypeSize(qual_type))
4981+
return bit_size;
4982+
}
4983+
50194984
return llvm::createStringError(
50204985
"could not get size of type %s",
50214986
GetTypeName(type, base_name_only).AsCString(""));

0 commit comments

Comments
 (0)