From: Argyrios Kyrtzidis Date: Wed, 12 Dec 2012 22:48:25 +0000 (+0000) Subject: [objc] For the ARC error that is emitted when a synthesized property implementation X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=135aa60c613bdcc0e8e237b12bf93cab04284419;p=clang [objc] For the ARC error that is emitted when a synthesized property implementation has inconsistent ownership with the backing ivar, point the error location to the ivar. Pointing to the ivar (instead of the @synthesize) is better since this is where a fix is needed. Also provide the location of @synthesize via a note. This also fixes the problem where an auto-synthesized property would emit an error without any location. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@170039 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 219d89f71b..3e638769eb 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -684,6 +684,8 @@ def error_category_property : Error< "class implementation">; def note_property_declare : Note< "property declared here">; +def note_property_synthesize : Note< + "property synthesized here">; def error_synthesize_category_decl : Error< "@synthesize not allowed in a category's implementation">; def error_reference_property : Error< diff --git a/lib/ARCMigrate/TransProperties.cpp b/lib/ARCMigrate/TransProperties.cpp index bf64831309..7196a54efc 100644 --- a/lib/ARCMigrate/TransProperties.cpp +++ b/lib/ARCMigrate/TransProperties.cpp @@ -226,8 +226,10 @@ private: for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) { if (I->ImplD) - Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, - I->ImplD->getLocation()); + Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, + diag::err_arc_assign_property_ownership, + diag::err_arc_inconsistent_property_ownership, + I->IvarD->getLocation()); } } @@ -253,8 +255,10 @@ private: } } if (I->ImplD) - Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, - I->ImplD->getLocation()); + Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, + diag::err_arc_assign_property_ownership, + diag::err_arc_inconsistent_property_ownership, + I->IvarD->getLocation()); } } @@ -276,8 +280,10 @@ private: canUseWeak ? "__weak " : "__unsafe_unretained "); } if (I->ImplD) { - Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership, - I->ImplD->getLocation()); + Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership, + diag::err_arc_assign_property_ownership, + diag::err_arc_inconsistent_property_ownership, + I->IvarD->getLocation()); Pass.TA.clearDiagnostic( diag::err_arc_objc_property_default_assign_on_object, I->ImplD->getLocation()); diff --git a/lib/Sema/SemaObjCProperty.cpp b/lib/Sema/SemaObjCProperty.cpp index b2f93bac15..83a1263532 100644 --- a/lib/Sema/SemaObjCProperty.cpp +++ b/lib/Sema/SemaObjCProperty.cpp @@ -577,20 +577,20 @@ static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, switch (propertyLifetime) { case Qualifiers::OCL_Strong: - S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership) + S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership) << property->getDeclName() << ivar->getDeclName() << ivarLifetime; break; case Qualifiers::OCL_Weak: - S.Diag(propertyImplLoc, diag::error_weak_property) + S.Diag(ivar->getLocation(), diag::error_weak_property) << property->getDeclName() << ivar->getDeclName(); break; case Qualifiers::OCL_ExplicitNone: - S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership) + S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership) << property->getDeclName() << ivar->getDeclName() << ((property->getPropertyAttributesAsWritten() @@ -606,6 +606,8 @@ static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc, } S.Diag(property->getLocation(), diag::note_property_declare); + if (propertyImplLoc.isValid()) + S.Diag(propertyImplLoc, diag::note_property_synthesize); } /// setImpliedPropertyAttributeForReadOnlyProperty - diff --git a/test/SemaObjC/arc-property-lifetime.m b/test/SemaObjC/arc-property-lifetime.m index 19570815f6..b824b2a456 100644 --- a/test/SemaObjC/arc-property-lifetime.m +++ b/test/SemaObjC/arc-property-lifetime.m @@ -1,10 +1,10 @@ -// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify -Wno-objc-root-class %s +// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-default-synthesize-properties -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify -Wno-objc-root-class %s // rdar://9340606 @interface Foo { @public - id __unsafe_unretained x; - id __weak y; + id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}} + id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}} id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}} } @property(strong) id x; // expected-note {{property declared here}} @@ -13,15 +13,15 @@ @end @implementation Foo -@synthesize x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}} -@synthesize y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}} +@synthesize x; // expected-note {{property synthesized here}} +@synthesize y; // expected-note {{property synthesized here}} @synthesize z; // suppressed @end @interface Bar { @public - id __unsafe_unretained x; - id __weak y; + id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}} + id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}} id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}} } @property(retain) id x; // expected-note {{property declared here}} @@ -30,15 +30,15 @@ @end @implementation Bar -@synthesize x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}} -@synthesize y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}} +@synthesize x; // expected-note {{property synthesized here}} +@synthesize y; // expected-note {{property synthesized here}} @synthesize z; // suppressed @end @interface Bas { @public - id __unsafe_unretained x; - id __weak y; + id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}} + id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}} id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}} } @property(copy) id x; // expected-note {{property declared here}} @@ -47,8 +47,8 @@ @end @implementation Bas -@synthesize x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}} -@synthesize y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}} +@synthesize x; // expected-note {{property synthesized here}} +@synthesize y; // expected-note {{property synthesized here}} @synthesize z; // suppressed @end @@ -70,7 +70,7 @@ // rdar://9341593 @interface Gorf { id __unsafe_unretained x; - id y; + id y; // expected-error {{existing instance variable 'y' for property 'y' with assign attribute must be __unsafe_unretained}} } @property(assign) id __unsafe_unretained x; @property(assign) id y; // expected-note {{property declared here}} @@ -79,13 +79,13 @@ @implementation Gorf @synthesize x; -@synthesize y; // expected-error {{existing instance variable 'y' for property 'y' with assign attribute must be __unsafe_unretained}} +@synthesize y; // expected-note {{property synthesized here}} @synthesize z; @end @interface Gorf2 { id __unsafe_unretained x; - id y; + id y; // expected-error {{existing instance variable 'y' for property 'y' with unsafe_unretained attribute must be __unsafe_unretained}} } @property(unsafe_unretained) id __unsafe_unretained x; @property(unsafe_unretained) id y; // expected-note {{property declared here}} @@ -94,7 +94,7 @@ @implementation Gorf2 @synthesize x; -@synthesize y; // expected-error {{existing instance variable 'y' for property 'y' with unsafe_unretained attribute must be __unsafe_unretained}} +@synthesize y; // expected-note {{property synthesized here}} @synthesize z; @end @@ -173,3 +173,12 @@ void foo(Baz *f) { @interface Boom @property (readonly) const void * innerPointer __attribute__((objc_returns_inner_pointer)); // expected-error {{'objc_returns_inner_pointer' attribute only applies to methods}} @end + +@interface Foo2 { + id _prop; // expected-error {{existing instance variable '_prop' for property 'prop' with assign attribute must be __unsafe_unretained}} +} +@property (nonatomic, assign) id prop; // expected-note {{property declared here}} +@end + +@implementation Foo2 +@end diff --git a/test/SemaObjC/arc-property.m b/test/SemaObjC/arc-property.m index 2925459620..cf823ae2b2 100644 --- a/test/SemaObjC/arc-property.m +++ b/test/SemaObjC/arc-property.m @@ -2,11 +2,11 @@ // rdar://9309489 @interface MyClass { - id __weak myString; + id __weak myString; // expected-error {{existing instance variable 'myString' for strong property 'myString' may not be __weak}} id StrongIvar; - id __weak myString2; + id __weak myString2; // expected-error {{existing instance variable 'myString2' for strong property 'myString2' may not be __weak}} id __weak myString3; - id StrongIvar5; + id StrongIvar5; // expected-error {{existing instance variable 'StrongIvar5' for __weak property 'myString5' must be __weak}} } @property (strong) id myString; // expected-note {{property declared here}} @property (strong) id myString1; @@ -18,21 +18,21 @@ @end @implementation MyClass -@synthesize myString; // expected-error {{existing instance variable 'myString' for strong property 'myString' may not be __weak}} +@synthesize myString; // expected-note {{property synthesized here}} @synthesize myString1 = StrongIvar; // OK -@synthesize myString2 = myString2; // expected-error {{existing instance variable 'myString2' for strong property 'myString2' may not be __weak}} +@synthesize myString2 = myString2; // expected-note {{property synthesized here}} // @synthesize myString3; // OK @synthesize myString4; // OK -@synthesize myString5 = StrongIvar5; // expected-error {{existing instance variable 'StrongIvar5' for __weak property 'myString5' must be __weak}} +@synthesize myString5 = StrongIvar5; // expected-note {{property synthesized here}} @end // rdar://9340692 @interface Foo { @public - id __unsafe_unretained x; // should be __weak - id __strong y; + id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for __weak property 'x' must be __weak}} + id __strong y; // expected-error {{existing instance variable 'y' for __weak property 'y' must be __weak}} id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}} } @property(weak) id x; // expected-note {{property declared here}} @@ -41,8 +41,8 @@ @end @implementation Foo -@synthesize x; // expected-error {{existing instance variable 'x' for __weak property 'x' must be __weak}} -@synthesize y; // expected-error {{existing instance variable 'y' for __weak property 'y' must be __weak}} +@synthesize x; // expected-note {{property synthesized here}} +@synthesize y; // expected-note {{property synthesized here}} @synthesize z; // suppressed @end diff --git a/test/SemaObjC/warn-direct-ivar-access.m b/test/SemaObjC/warn-direct-ivar-access.m index 088fe0fa26..4f242e6b33 100644 --- a/test/SemaObjC/warn-direct-ivar-access.m +++ b/test/SemaObjC/warn-direct-ivar-access.m @@ -4,7 +4,7 @@ __attribute__((objc_root_class)) @interface MyObject { @public id _myMaster; - id _isTickledPink; + id _isTickledPink; // expected-error {{existing instance variable '_isTickledPink' for property 'isTickledPink'}} int _myIntProp; } @property(retain) id myMaster; @@ -15,7 +15,7 @@ __attribute__((objc_root_class)) @interface MyObject { @implementation MyObject @synthesize myMaster = _myMaster; -@synthesize isTickledPink = _isTickledPink; // expected-error {{existing instance variable '_isTickledPink' for property 'isTickledPink'}} +@synthesize isTickledPink = _isTickledPink; // expected-note {{property synthesized here}} @synthesize myIntProp = _myIntProp; - (void) doSomething { diff --git a/test/SemaObjC/weak-property.m b/test/SemaObjC/weak-property.m index 141c35b9ac..d306a924e9 100644 --- a/test/SemaObjC/weak-property.m +++ b/test/SemaObjC/weak-property.m @@ -4,7 +4,7 @@ @interface WeakPropertyTest { Class isa; __weak id value; - id x; + id x; // expected-error {{existing instance variable 'x' for __weak property 'x' must be __weak}} } @property (weak) id value1; @property __weak id value; @@ -19,6 +19,6 @@ @end @implementation WeakPropertyTest -@synthesize x; // expected-error {{existing instance variable 'x' for __weak property 'x' must be __weak}} +@synthesize x; // expected-note {{property synthesized here}} @dynamic value1, value, value2, v1,v2,v3,v4; @end