Skip to content
Snippets Groups Projects
Commit 244b187d authored by Fariborz Jahanian's avatar Fariborz Jahanian
Browse files

objc-arc: desugar certain type and improve on diagnostic for

ownership qualifier cast which won't work.
// rdar://10244607

llvm-svn: 143258
parent 53cafaba
No related branches found
No related tags found
No related merge requests found
...@@ -3043,8 +3043,7 @@ def err_arc_mismatched_cast : Error< ...@@ -3043,8 +3043,7 @@ def err_arc_mismatched_cast : Error<
"an Objective-C pointer|an indirect pointer to an Objective-C pointer}1" "an Objective-C pointer|an indirect pointer to an Objective-C pointer}1"
" to %3 is disallowed with ARC">; " to %3 is disallowed with ARC">;
def err_arc_nolifetime_behavior : Error< def err_arc_nolifetime_behavior : Error<
"casting expression of type %1 to type %0 with qualified lifetime" "explicit ownership qualifier on cast result would have no effect">;
"will not change object lifetime">;
def err_arc_objc_object_in_struct : Error< def err_arc_objc_object_in_struct : Error<
"ARC forbids Objective-C objects in structs or unions">; "ARC forbids Objective-C objects in structs or unions">;
def err_arc_objc_property_default_assign_on_object : Error< def err_arc_objc_property_default_assign_on_object : Error<
......
...@@ -1937,17 +1937,27 @@ Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType, ...@@ -1937,17 +1937,27 @@ Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType,
if (exprACTC == castACTC) { if (exprACTC == castACTC) {
// check for viablity and report error if casting an rvalue to a // check for viablity and report error if casting an rvalue to a
// life-time qualifier. // life-time qualifier.
if ((castACTC == ACTC_retainable) && if ((castACTC == ACTC_retainable) &&
isa<AttributedType>(castType) &&
(castType.getObjCLifetime() != Qualifiers::OCL_None) &&
(CCK == CCK_CStyleCast || CCK == CCK_OtherCast) && (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) &&
castType != castExprType) { (castType != castExprType)) {
SourceLocation loc = const Type *DT = castType.getTypePtr();
(castRange.isValid() ? castRange.getBegin() QualType QDT = castType;
: castExpr->getExprLoc()); // We desugar some types but not others. We ignore those
Diag(loc, diag::err_arc_nolifetime_behavior) // that cannot happen in a cast; i.e. auto, and those which
<< effCastType << castExprType // should not be de-sugared; i.e typedef.
<< castRange << castExpr->getSourceRange(); if (const ParenType *PT = dyn_cast<ParenType>(DT))
QDT = PT->desugar();
else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT))
QDT = TP->desugar();
else if (const AttributedType *AT = dyn_cast<AttributedType>(DT))
QDT = AT->desugar();
if (QDT != castType &&
QDT.getObjCLifetime() != Qualifiers::OCL_None) {
SourceLocation loc =
(castRange.isValid() ? castRange.getBegin()
: castExpr->getExprLoc());
Diag(loc, diag::err_arc_nolifetime_behavior);
}
} }
return ACR_okay; return ACR_okay;
} }
......
...@@ -17,12 +17,14 @@ typedef __autoreleasing NSString * AUTORELEASEPNSString; ...@@ -17,12 +17,14 @@ typedef __autoreleasing NSString * AUTORELEASEPNSString;
- (CFStringRef)myString - (CFStringRef)myString
{ {
CFStringRef myString = CFStringRef myString =
(__bridge CFStringRef) (__strong NSString *)CFBridgingRelease(); // expected-error {{casting expression of type 'NSString *' to type 'NSString *__strong' with qualified lifetimewill not change object lifetime}} (__bridge CFStringRef) (__strong NSString *)CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result would have no effect}}
myString = myString =
(__bridge CFStringRef) (__autoreleasing PNSString) CFBridgingRelease(); // expected-error {{casting expression of type 'NSString *' to type '__autoreleasing PNSString' (aka 'NSString *__autoreleasing') with qualified lifetimewill not change object}} (__bridge CFStringRef) (__autoreleasing PNSString) CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result would have no effect}}
myString = myString =
(__bridge CFStringRef) (AUTORELEASEPNSString) CFBridgingRelease(); // OK (__bridge CFStringRef) (AUTORELEASEPNSString) CFBridgingRelease(); // OK
myString =
(__bridge CFStringRef) (typeof(__strong NSString *)) CFBridgingRelease(); // expected-error {{explicit ownership qualifier on cast result would have no effect}}
return myString; return myString;
} }
......
...@@ -16,7 +16,7 @@ int main() { ...@@ -16,7 +16,7 @@ int main() {
ns1 = (__weak sub *)obj; // expected-error {{assignment of a weak-unavailable object to a __weak object}} \ ns1 = (__weak sub *)obj; // expected-error {{assignment of a weak-unavailable object to a __weak object}} \
// expected-error {{class is incompatible with __weak references}} \ // expected-error {{class is incompatible with __weak references}} \
// expected-error {{casting expression of type 'id' to type 'sub *__weak' with qualified lifetimewill not change object lifetime}} // expected-error {{explicit ownership qualifier on cast result would have no effect}}
} }
// rdar://9732636 // rdar://9732636
...@@ -32,7 +32,7 @@ NOWEAK * Test1() { ...@@ -32,7 +32,7 @@ NOWEAK * Test1() {
__weak id weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}} __weak id weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
return (__weak id)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK *' to a __weak object of type '__weak id'}} \ return (__weak id)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK *' to a __weak object of type '__weak id'}} \
// expected-error {{casting expression of type 'NOWEAK *' to type '__weak id' with qualified lifetimewill not change object lifetime}} // expected-error {{explicit ownership qualifier on cast result would have no effect}}
} }
@protocol P @end @protocol P @end
...@@ -45,6 +45,6 @@ NOWEAK<P, P1> * Test2() { ...@@ -45,6 +45,6 @@ NOWEAK<P, P1> * Test2() {
__weak id<P> weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}} __weak id<P> weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
return (__weak id<P>)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK<P,P1> *' to a __weak object of type '__weak id<P>'}} \ return (__weak id<P>)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK<P,P1> *' to a __weak object of type '__weak id<P>'}} \
// expected-error {{casting expression of type 'NOWEAK<P,P1> *' to type '__weak id<P>' with qualified lifetimewill not change object lifetime}} // expected-error {{explicit ownership qualifier on cast result would have no effect}}
} }
...@@ -16,7 +16,7 @@ int main() { ...@@ -16,7 +16,7 @@ int main() {
ns1 = (__weak sub *)obj; // expected-error {{assignment of a weak-unavailable object to a __weak object}} \ ns1 = (__weak sub *)obj; // expected-error {{assignment of a weak-unavailable object to a __weak object}} \
// expected-error {{class is incompatible with __weak references}} \ // expected-error {{class is incompatible with __weak references}} \
// expected-error {{casting expression of type 'id' to type 'sub *__weak' with qualified lifetimewill not change object lifetime}} // expected-error {{explicit ownership qualifier on cast result would have no effect}}
} }
// rdar://9732636 // rdar://9732636
...@@ -32,7 +32,7 @@ NOWEAK * Test1() { ...@@ -32,7 +32,7 @@ NOWEAK * Test1() {
__weak id weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}} __weak id weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
return (__weak id)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK *' to a __weak object of type '__weak id'}} \ return (__weak id)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK *' to a __weak object of type '__weak id'}} \
// expected-error {{casting expression of type 'NOWEAK *' to type '__weak id' with qualified lifetimewill not change object lifetime}} // expected-error {{explicit ownership qualifier on cast result would have no effect}}
} }
@protocol P @end @protocol P @end
...@@ -45,6 +45,6 @@ NOWEAK<P, P1> * Test2() { ...@@ -45,6 +45,6 @@ NOWEAK<P, P1> * Test2() {
__weak id<P> weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}} __weak id<P> weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
return (__weak id<P, P1>)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK<P,P1> *' to a __weak object of type '__weak id<P,P1>'}} \ return (__weak id<P, P1>)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK<P,P1> *' to a __weak object of type '__weak id<P,P1>'}} \
// expected-error {{casting expression of type 'NOWEAK<P,P1> *' to type '__weak id<P,P1>' with qualified lifetimewill not change object lifetime}} // expected-error {{explicit ownership qualifier on cast result would have no effect}}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment