Objective-C doesn't consider the use of incomplete types as method

parameter types to be ill-formed. However, it relies on the
completeness of method parameter types when producing metadata, e.g.,
for a protocol, leading IR generating to crash in such cases.

Since there's no real way to tighten down the semantics of Objective-C
here without breaking existing code, do something safe but lame:
suppress the generation of metadata when this happens.

Fixes <rdar://problem/9123036>.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132171 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Douglas Gregor 2011-05-27 01:19:52 +00:00
Родитель 9910df05e9
Коммит f968d83747
6 изменённых файлов: 81 добавлений и 11 удалений

Просмотреть файл

@ -914,12 +914,18 @@ public:
std::string &S) const;
/// getObjCEncodingForFunctionDecl - Returns the encoded type for this
//function. This is in the same format as Objective-C method encodings.
void getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S);
/// function. This is in the same format as Objective-C method encodings.
///
/// \returns true if an error occurred (e.g., because one of the parameter
/// types is incomplete), false otherwise.
bool getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S);
/// getObjCEncodingForMethodDecl - Return the encoded type for this method
/// declaration.
void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S)
///
/// \returns true if an error occurred (e.g., because one of the parameter
/// types is incomplete), false otherwise.
bool getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S)
const;
/// getObjCEncodingForBlock - Return the encoded type for this block

Просмотреть файл

@ -3815,6 +3815,9 @@ static bool isTypeTypedefedAsBOOL(QualType T) {
/// getObjCEncodingTypeSize returns size of type for objective-c encoding
/// purpose.
CharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
if (!type->isIncompleteArrayType() && type->isIncompleteType())
return CharUnits::Zero();
CharUnits sz = getTypeSizeInChars(type);
// Make all integer and enum types at least as large as an int
@ -3882,7 +3885,7 @@ std::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
return S;
}
void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
bool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
std::string& S) {
// Encode result type.
getObjCEncodingForType(Decl->getResultType(), S);
@ -3892,8 +3895,11 @@ void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
E = Decl->param_end(); PI != E; ++PI) {
QualType PType = (*PI)->getType();
CharUnits sz = getObjCEncodingTypeSize(PType);
if (sz.isZero())
return true;
assert (sz.isPositive() &&
"getObjCEncodingForMethodDecl - Incomplete param type");
"getObjCEncodingForFunctionDecl - Incomplete param type");
ParmOffset += sz;
}
S += charUnitsToString(ParmOffset);
@ -3916,11 +3922,13 @@ void ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
S += charUnitsToString(ParmOffset);
ParmOffset += getObjCEncodingTypeSize(PType);
}
return false;
}
/// getObjCEncodingForMethodDecl - Return the encoded type for this method
/// declaration.
void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
bool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
std::string& S) const {
// FIXME: This is not very efficient.
// Encode type qualifer, 'in', 'inout', etc. for the return type.
@ -3939,6 +3947,9 @@ void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
E = Decl->sel_param_end(); PI != E; ++PI) {
QualType PType = (*PI)->getType();
CharUnits sz = getObjCEncodingTypeSize(PType);
if (sz.isZero())
return true;
assert (sz.isPositive() &&
"getObjCEncodingForMethodDecl - Incomplete param type");
ParmOffset += sz;
@ -3968,6 +3979,8 @@ void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
S += charUnitsToString(ParmOffset);
ParmOffset += getObjCEncodingTypeSize(PType);
}
return false;
}
/// getObjCEncodingForPropertyDecl - Return the encoded type for this

Просмотреть файл

@ -1686,6 +1686,7 @@ void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
if (DefinedProtocols.count(PD->getIdentifier()))
return GetOrEmitProtocol(PD);
return GetOrEmitProtocolRef(PD);
}
@ -1719,6 +1720,9 @@ llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
ObjCMethodDecl *MD = *i;
llvm::Constant *C = GetMethodDescriptionConstant(MD);
if (!C)
return GetOrEmitProtocolRef(PD);
if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
OptInstanceMethods.push_back(C);
} else {
@ -1730,6 +1734,9 @@ llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
ObjCMethodDecl *MD = *i;
llvm::Constant *C = GetMethodDescriptionConstant(MD);
if (!C)
return GetOrEmitProtocolRef(PD);
if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
OptClassMethods.push_back(C);
} else {
@ -1973,6 +1980,9 @@ CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
ObjCTypes.SelectorPtrTy);
Desc[1] = GetMethodVarType(MD);
if (!Desc[1])
return 0;
return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Desc);
}
@ -3935,8 +3945,10 @@ llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
std::string TypeStr;
CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
TypeStr);
if (CGM.getContext().getObjCEncodingForMethodDecl(
const_cast<ObjCMethodDecl*>(D),
TypeStr))
return 0;
llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
@ -5359,6 +5371,9 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
ObjCMethodDecl *MD = *i;
llvm::Constant *C = GetMethodDescriptionConstant(MD);
if (!C)
return GetOrEmitProtocolRef(PD);
if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
OptInstanceMethods.push_back(C);
} else {
@ -5370,6 +5385,9 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
ObjCMethodDecl *MD = *i;
llvm::Constant *C = GetMethodDescriptionConstant(MD);
if (!C)
return GetOrEmitProtocolRef(PD);
if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
OptClassMethods.push_back(C);
} else {
@ -5509,6 +5527,9 @@ CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
ObjCTypes.SelectorPtrTy);
Desc[1] = GetMethodVarType(MD);
if (!Desc[1])
return 0;
// Protocol methods have no implementation. So, this entry is always NULL.
Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);

Просмотреть файл

@ -2867,6 +2867,16 @@ bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
if ((*Param)->isTemplateParameterPack())
break;
// If our template is a template template parameter that hasn't acquired
// its proper context yet (e.g., because we're using the template template
// parameter in the signature of a function template, before we've built
// the function template itself), don't attempt substitution of default
// template arguments at this point: we don't have enough context to
// do it properly.
if (isTemplateTemplateParameter &&
Template->getDeclContext()->isTranslationUnit())
break;
// We have a default template argument that we will use.
TemplateArgumentLoc Arg;

Просмотреть файл

@ -0,0 +1,19 @@
// RUN: %clang_cc1 %s -emit-llvm -o -
// <rdar://problem/9123036> crash due to forward-declared struct in
// protocol method parameter.
@protocol P
- (void) A:(struct z) z;
@end
@interface I < P >
@end
@implementation I
@end
@interface I2
- (void) A:(struct z2) z2;
@end
@implementation I2
@end

Просмотреть файл

@ -379,9 +379,10 @@ CXString clang_getDeclObjCTypeEncoding(CXCursor C) {
ASTContext &Ctx = AU->getASTContext();
std::string encoding;
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))
Ctx.getObjCEncodingForMethodDecl(OMD, encoding);
else if (ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D))
if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
if (Ctx.getObjCEncodingForMethodDecl(OMD, encoding))
return cxstring::createCXString("?");
} else if (ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D))
Ctx.getObjCEncodingForPropertyDecl(OPD, NULL, encoding);
else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Ctx.getObjCEncodingForFunctionDecl(FD, encoding);