Improve semantic checking for blocks. Radar 6441502

llvm-svn: 71145
This commit is contained in:
Mike Stump 2009-05-07 03:14:14 +00:00
parent 759de47964
commit 1b821b4fc5
4 changed files with 135 additions and 97 deletions

View File

@ -1039,6 +1039,8 @@ def ext_typecheck_comparison_of_pointer_integer : Warning<
"comparison between pointer and integer (%0 and %1)">; "comparison between pointer and integer (%0 and %1)">;
def ext_typecheck_comparison_of_distinct_pointers : Warning< def ext_typecheck_comparison_of_distinct_pointers : Warning<
"comparison of distinct pointer types (%0 and %1)">; "comparison of distinct pointer types (%0 and %1)">;
def ext_typecheck_cond_incompatible_operands : Warning<
"incompatible operand types (%0 and %1)">;
def err_typecheck_comparison_of_distinct_pointers : Error< def err_typecheck_comparison_of_distinct_pointers : Error<
"comparison of distinct pointer types (%0 and %1)">; "comparison of distinct pointer types (%0 and %1)">;
def err_typecheck_vector_comparison : Error< def err_typecheck_vector_comparison : Error<

View File

@ -2786,99 +2786,113 @@ QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS,
return RHSTy; return RHSTy;
} }
const PointerType *LHSPT = LHSTy->getAsPointerType();
const PointerType *RHSPT = RHSTy->getAsPointerType();
const BlockPointerType *LHSBPT = LHSTy->getAsBlockPointerType();
const BlockPointerType *RHSBPT = RHSTy->getAsBlockPointerType();
// Handle the case where both operands are pointers before we handle null // Handle the case where both operands are pointers before we handle null
// pointer constants in case both operands are null pointer constants. // pointer constants in case both operands are null pointer constants.
if (const PointerType *LHSPT = LHSTy->getAsPointerType()) { // C99 6.5.15p3,6 if ((LHSPT || LHSBPT) && (RHSPT || RHSBPT)) { // C99 6.5.15p3,6
if (const PointerType *RHSPT = RHSTy->getAsPointerType()) { // get the "pointed to" types
// get the "pointed to" types QualType lhptee = LHSPT ? LHSPT->getPointeeType() : LHSBPT->getPointeeType();
QualType lhptee = LHSPT->getPointeeType(); QualType rhptee = RHSPT ? RHSPT->getPointeeType() : RHSBPT->getPointeeType();
QualType rhptee = RHSPT->getPointeeType();
// ignore qualifiers on void (C99 6.5.15p3, clause 6) // ignore qualifiers on void (C99 6.5.15p3, clause 6)
if (lhptee->isVoidType() && if (lhptee->isVoidType()
rhptee->isIncompleteOrObjectType()) { && (RHSBPT || rhptee->isIncompleteOrObjectType())) {
// Figure out necessary qualifiers (C99 6.5.15p6) // Figure out necessary qualifiers (C99 6.5.15p6)
QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
QualType destType = Context.getPointerType(destPointee); QualType destType = Context.getPointerType(destPointee);
ImpCastExprToType(LHS, destType); // add qualifiers if necessary ImpCastExprToType(LHS, destType); // add qualifiers if necessary
ImpCastExprToType(RHS, destType); // promote to void* ImpCastExprToType(RHS, destType); // promote to void*
return destType; return destType;
} }
if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { if (rhptee->isVoidType()
QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); && (LHSBPT || lhptee->isIncompleteOrObjectType())) {
QualType destType = Context.getPointerType(destPointee); QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
ImpCastExprToType(LHS, destType); // add qualifiers if necessary QualType destType = Context.getPointerType(destPointee);
ImpCastExprToType(RHS, destType); // promote to void* ImpCastExprToType(LHS, destType); // add qualifiers if necessary
return destType; ImpCastExprToType(RHS, destType); // promote to void*
} return destType;
}
if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { bool sameKind = (LHSPT && RHSPT) || (LHSBPT && RHSBPT);
// Two identical pointer types are always compatible. if (sameKind
return LHSTy; && Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) {
} // Two identical pointer types are always compatible.
return LHSTy;
}
QualType compositeType = LHSTy; QualType compositeType = LHSTy;
// If either type is an Objective-C object type then check // If either type is an Objective-C object type then check
// compatibility according to Objective-C. // compatibility according to Objective-C.
if (Context.isObjCObjectPointerType(LHSTy) || if (Context.isObjCObjectPointerType(LHSTy) ||
Context.isObjCObjectPointerType(RHSTy)) { Context.isObjCObjectPointerType(RHSTy)) {
// If both operands are interfaces and either operand can be // If both operands are interfaces and either operand can be
// assigned to the other, use that type as the composite // assigned to the other, use that type as the composite
// type. This allows // type. This allows
// xxx ? (A*) a : (B*) b // xxx ? (A*) a : (B*) b
// where B is a subclass of A. // where B is a subclass of A.
// //
// Additionally, as for assignment, if either type is 'id' // Additionally, as for assignment, if either type is 'id'
// allow silent coercion. Finally, if the types are // allow silent coercion. Finally, if the types are
// incompatible then make sure to use 'id' as the composite // incompatible then make sure to use 'id' as the composite
// type so the result is acceptable for sending messages to. // type so the result is acceptable for sending messages to.
// FIXME: Consider unifying with 'areComparableObjCPointerTypes'. // FIXME: Consider unifying with 'areComparableObjCPointerTypes'.
// It could return the composite type. // It could return the composite type.
const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType(); const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType(); const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
if (LHSIface && RHSIface && if (LHSIface && RHSIface &&
Context.canAssignObjCInterfaces(LHSIface, RHSIface)) { Context.canAssignObjCInterfaces(LHSIface, RHSIface)) {
compositeType = LHSTy; compositeType = LHSTy;
} else if (LHSIface && RHSIface && } else if (LHSIface && RHSIface &&
Context.canAssignObjCInterfaces(RHSIface, LHSIface)) { Context.canAssignObjCInterfaces(RHSIface, LHSIface)) {
compositeType = RHSTy; compositeType = RHSTy;
} else if (Context.isObjCIdStructType(lhptee) || } else if (Context.isObjCIdStructType(lhptee) ||
Context.isObjCIdStructType(rhptee)) { Context.isObjCIdStructType(rhptee)) {
compositeType = Context.getObjCIdType(); compositeType = Context.getObjCIdType();
} else { } else if (LHSBPT || RHSBPT) {
Diag(QuestionLoc, diag::ext_typecheck_comparison_of_distinct_pointers) if (!sameKind
<< LHSTy << RHSTy || !Context.typesAreBlockCompatible(lhptee.getUnqualifiedType(),
<< LHS->getSourceRange() << RHS->getSourceRange(); rhptee.getUnqualifiedType()))
QualType incompatTy = Context.getObjCIdType(); Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands)
ImpCastExprToType(LHS, incompatTy); << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange();
ImpCastExprToType(RHS, incompatTy); return QualType();
return incompatTy; } else {
} Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands)
} else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), << LHSTy << RHSTy
rhptee.getUnqualifiedType())) { << LHS->getSourceRange() << RHS->getSourceRange();
Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) QualType incompatTy = Context.getObjCIdType();
<< LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange();
// In this situation, we assume void* type. No especially good
// reason, but this is what gcc does, and we do have to pick
// to get a consistent AST.
QualType incompatTy = Context.getPointerType(Context.VoidTy);
ImpCastExprToType(LHS, incompatTy); ImpCastExprToType(LHS, incompatTy);
ImpCastExprToType(RHS, incompatTy); ImpCastExprToType(RHS, incompatTy);
return incompatTy; return incompatTy;
} }
// The pointer types are compatible. } else if (!sameKind
// C99 6.5.15p6: If both operands are pointers to compatible types *or* to || !Context.typesAreCompatible(lhptee.getUnqualifiedType(),
// differently qualified versions of compatible types, the result type is rhptee.getUnqualifiedType())) {
// a pointer to an appropriately qualified version of the *composite* Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers)
// type. << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange();
// FIXME: Need to calculate the composite type. // In this situation, we assume void* type. No especially good
// FIXME: Need to add qualifiers // reason, but this is what gcc does, and we do have to pick
ImpCastExprToType(LHS, compositeType); // to get a consistent AST.
ImpCastExprToType(RHS, compositeType); QualType incompatTy = Context.getPointerType(Context.VoidTy);
return compositeType; ImpCastExprToType(LHS, incompatTy);
ImpCastExprToType(RHS, incompatTy);
return incompatTy;
} }
// The pointer types are compatible.
// C99 6.5.15p6: If both operands are pointers to compatible types *or* to
// differently qualified versions of compatible types, the result type is
// a pointer to an appropriately qualified version of the *composite*
// type.
// FIXME: Need to calculate the composite type.
// FIXME: Need to add qualifiers
ImpCastExprToType(LHS, compositeType);
ImpCastExprToType(RHS, compositeType);
return compositeType;
} }
// GCC compatibility: soften pointer/integer mismatch. // GCC compatibility: soften pointer/integer mismatch.
@ -2895,11 +2909,6 @@ QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS,
return LHSTy; return LHSTy;
} }
// Selection between block pointer types is ok as long as they are the same.
if (LHSTy->isBlockPointerType() && RHSTy->isBlockPointerType() &&
Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy))
return LHSTy;
// Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type // Need to handle "id<xx>" explicitly. Unlike "id", whose canonical type
// evaluates to "struct objc_object *" (and is handled above when comparing // evaluates to "struct objc_object *" (and is handled above when comparing
// id with statically typed objects). // id with statically typed objects).
@ -3671,7 +3680,7 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc,
QualType lType = lex->getType(); QualType lType = lex->getType();
QualType rType = rex->getType(); QualType rType = rex->getType();
if (!lType->isFloatingType()) { if (!lType->isFloatingType() && !(lType->isBlockPointerType() && isRelational)) {
// For non-floating point types, check for self-comparisons of the form // For non-floating point types, check for self-comparisons of the form
// x == x, x != x, x < x, etc. These always evaluate to a constant, and // x == x, x != x, x < x, etc. These always evaluate to a constant, and
// often indicate logic errors in the program. // often indicate logic errors in the program.
@ -3795,7 +3804,7 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc,
return ResultTy; return ResultTy;
} }
// Handle block pointer types. // Handle block pointer types.
if (lType->isBlockPointerType() && rType->isBlockPointerType()) { if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) {
QualType lpointee = lType->getAsBlockPointerType()->getPointeeType(); QualType lpointee = lType->getAsBlockPointerType()->getPointeeType();
QualType rpointee = rType->getAsBlockPointerType()->getPointeeType(); QualType rpointee = rType->getAsBlockPointerType()->getPointeeType();
@ -3808,11 +3817,16 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc,
return ResultTy; return ResultTy;
} }
// Allow block pointers to be compared with null pointer constants. // Allow block pointers to be compared with null pointer constants.
if ((lType->isBlockPointerType() && rType->isPointerType()) || if (!isRelational
(lType->isPointerType() && rType->isBlockPointerType())) { && ((lType->isBlockPointerType() && rType->isPointerType())
|| (lType->isPointerType() && rType->isBlockPointerType()))) {
if (!LHSIsNull && !RHSIsNull) { if (!LHSIsNull && !RHSIsNull) {
Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) if (!((rType->isPointerType() && rType->getAsPointerType()
<< lType << rType << lex->getSourceRange() << rex->getSourceRange(); ->getPointeeType()->isVoidType())
|| (lType->isPointerType() && lType->getAsPointerType()
->getPointeeType()->isVoidType())))
Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks)
<< lType << rType << lex->getSourceRange() << rex->getSourceRange();
} }
ImpCastExprToType(rex, lType); // promote the pointer to pointer ImpCastExprToType(rex, lType); // promote the pointer to pointer
return ResultTy; return ResultTy;
@ -3867,14 +3881,20 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc,
} }
// Handle block pointers. // Handle block pointers.
if (lType->isBlockPointerType() && rType->isIntegerType()) { if (lType->isBlockPointerType() && rType->isIntegerType()) {
if (!RHSIsNull) if (isRelational)
Diag(Loc, diag::err_typecheck_invalid_operands)
<< lType << rType << lex->getSourceRange() << rex->getSourceRange();
else if (!RHSIsNull)
Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer)
<< lType << rType << lex->getSourceRange() << rex->getSourceRange(); << lType << rType << lex->getSourceRange() << rex->getSourceRange();
ImpCastExprToType(rex, lType); // promote the integer to pointer ImpCastExprToType(rex, lType); // promote the integer to pointer
return ResultTy; return ResultTy;
} }
if (lType->isIntegerType() && rType->isBlockPointerType()) { if (lType->isIntegerType() && rType->isBlockPointerType()) {
if (!LHSIsNull) if (isRelational)
Diag(Loc, diag::err_typecheck_invalid_operands)
<< lType << rType << lex->getSourceRange() << rex->getSourceRange();
else if (!LHSIsNull)
Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer)
<< lType << rType << lex->getSourceRange() << rex->getSourceRange(); << lType << rType << lex->getSourceRange() << rex->getSourceRange();
ImpCastExprToType(lex, rType); // promote the integer to pointer ImpCastExprToType(lex, rType); // promote the integer to pointer

View File

@ -156,3 +156,19 @@ void test16(__block int i) { // expected-error {{__block attribute not allowed,
__block int a[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}} __block int a[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
__block int (*ap)[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}} __block int (*ap)[size]; // expected-error {{__block attribute not allowed on declaration with a variably modified type}}
} }
void test17() {
void (^bp)(int);
void (*rp)(int);
void (^bp1)();
void *vp = bp;
f(1 ? bp : vp);
f(1 ? vp : bp);
f(1 ? bp : bp1); // expected-error {{incompatible operand types ('void (^)(int)' and 'void (^)()')}}
(void)(bp > rp); // expected-error {{invalid operands}}
(void)(bp > 0); // expected-error {{invalid operands}}
(void)(bp > bp); // expected-error {{invalid operands}}
(void)(bp > vp); // expected-error {{invalid operands}}
f(1 ? bp : rp); // expected-error {{incompatible operand types ('void (^)(int)' and 'void (*)(int)')}}
}

View File

@ -8,7 +8,7 @@
void f0(int cond, A *a, B *b) { void f0(int cond, A *a, B *b) {
// Ensure that we can still send a message to result of incompatible // Ensure that we can still send a message to result of incompatible
// conditional expression. // conditional expression.
[ (cond ? a : b) test ]; // expected-warning{{comparison of distinct pointer types ('A *' and 'B *')}} expected-warning {{method '-test' not found}} [ (cond ? a : b) test ]; // expected-warning{{incompatible operand types ('A *' and 'B *')}} expected-warning {{method '-test' not found}}
} }
@interface NSKey @end @interface NSKey @end
@ -22,8 +22,8 @@ void foo (int i, NSKey *NSKeyValueCoding_NullValue, UpdatesList *nukedUpdatesLis
NSKey *key; NSKey *key;
KeySub *keysub; KeySub *keysub;
obj = i ? NSKeyValueCoding_NullValue : nukedUpdatesList; // expected-warning{{comparison of distinct pointer types ('NSKey *' and 'UpdatesList *')}} obj = i ? NSKeyValueCoding_NullValue : nukedUpdatesList; // expected-warning{{incompatible operand types ('NSKey *' and 'UpdatesList *')}}
key = i ? NSKeyValueCoding_NullValue : nukedUpdatesList; // expected-warning{{comparison of distinct pointer types ('NSKey *' and 'UpdatesList *')}} key = i ? NSKeyValueCoding_NullValue : nukedUpdatesList; // expected-warning{{incompatible operand types ('NSKey *' and 'UpdatesList *')}}
key = i ? NSKeyValueCoding_NullValue : keysub; key = i ? NSKeyValueCoding_NullValue : keysub;
keysub = i ? NSKeyValueCoding_NullValue : keysub; // expected-warning{{incompatible pointer types assigning 'NSKey *', expected 'KeySub *'}} keysub = i ? NSKeyValueCoding_NullValue : keysub; // expected-warning{{incompatible pointer types assigning 'NSKey *', expected 'KeySub *'}}
} }