Skip to content

Commit 135aa60

Browse files
committed
[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
1 parent 9b629fc commit 135aa60

File tree

7 files changed

+59
-40
lines changed

7 files changed

+59
-40
lines changed

include/clang/Basic/DiagnosticSemaKinds.td

+2
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,8 @@ def error_category_property : Error<
684684
"class implementation">;
685685
def note_property_declare : Note<
686686
"property declared here">;
687+
def note_property_synthesize : Note<
688+
"property synthesized here">;
687689
def error_synthesize_category_decl : Error<
688690
"@synthesize not allowed in a category's implementation">;
689691
def error_reference_property : Error<

lib/ARCMigrate/TransProperties.cpp

+12-6
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,10 @@ class PropertiesRewriter {
226226

227227
for (PropsTy::iterator I = props.begin(), E = props.end(); I != E; ++I) {
228228
if (I->ImplD)
229-
Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership,
230-
I->ImplD->getLocation());
229+
Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
230+
diag::err_arc_assign_property_ownership,
231+
diag::err_arc_inconsistent_property_ownership,
232+
I->IvarD->getLocation());
231233
}
232234
}
233235

@@ -253,8 +255,10 @@ class PropertiesRewriter {
253255
}
254256
}
255257
if (I->ImplD)
256-
Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership,
257-
I->ImplD->getLocation());
258+
Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
259+
diag::err_arc_assign_property_ownership,
260+
diag::err_arc_inconsistent_property_ownership,
261+
I->IvarD->getLocation());
258262
}
259263
}
260264

@@ -276,8 +280,10 @@ class PropertiesRewriter {
276280
canUseWeak ? "__weak " : "__unsafe_unretained ");
277281
}
278282
if (I->ImplD) {
279-
Pass.TA.clearDiagnostic(diag::err_arc_assign_property_ownership,
280-
I->ImplD->getLocation());
283+
Pass.TA.clearDiagnostic(diag::err_arc_strong_property_ownership,
284+
diag::err_arc_assign_property_ownership,
285+
diag::err_arc_inconsistent_property_ownership,
286+
I->IvarD->getLocation());
281287
Pass.TA.clearDiagnostic(
282288
diag::err_arc_objc_property_default_assign_on_object,
283289
I->ImplD->getLocation());

lib/Sema/SemaObjCProperty.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -577,20 +577,20 @@ static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
577577

578578
switch (propertyLifetime) {
579579
case Qualifiers::OCL_Strong:
580-
S.Diag(propertyImplLoc, diag::err_arc_strong_property_ownership)
580+
S.Diag(ivar->getLocation(), diag::err_arc_strong_property_ownership)
581581
<< property->getDeclName()
582582
<< ivar->getDeclName()
583583
<< ivarLifetime;
584584
break;
585585

586586
case Qualifiers::OCL_Weak:
587-
S.Diag(propertyImplLoc, diag::error_weak_property)
587+
S.Diag(ivar->getLocation(), diag::error_weak_property)
588588
<< property->getDeclName()
589589
<< ivar->getDeclName();
590590
break;
591591

592592
case Qualifiers::OCL_ExplicitNone:
593-
S.Diag(propertyImplLoc, diag::err_arc_assign_property_ownership)
593+
S.Diag(ivar->getLocation(), diag::err_arc_assign_property_ownership)
594594
<< property->getDeclName()
595595
<< ivar->getDeclName()
596596
<< ((property->getPropertyAttributesAsWritten()
@@ -606,6 +606,8 @@ static void checkARCPropertyImpl(Sema &S, SourceLocation propertyImplLoc,
606606
}
607607

608608
S.Diag(property->getLocation(), diag::note_property_declare);
609+
if (propertyImplLoc.isValid())
610+
S.Diag(propertyImplLoc, diag::note_property_synthesize);
609611
}
610612

611613
/// setImpliedPropertyAttributeForReadOnlyProperty -

test/SemaObjC/arc-property-lifetime.m

+26-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fsyntax-only -fobjc-arc -verify -Wno-objc-root-class %s
1+
// 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
22
// rdar://9340606
33

44
@interface Foo {
55
@public
6-
id __unsafe_unretained x;
7-
id __weak y;
6+
id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
7+
id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
88
id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}}
99
}
1010
@property(strong) id x; // expected-note {{property declared here}}
@@ -13,15 +13,15 @@ @interface Foo {
1313
@end
1414

1515
@implementation Foo
16-
@synthesize x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
17-
@synthesize y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
16+
@synthesize x; // expected-note {{property synthesized here}}
17+
@synthesize y; // expected-note {{property synthesized here}}
1818
@synthesize z; // suppressed
1919
@end
2020

2121
@interface Bar {
2222
@public
23-
id __unsafe_unretained x;
24-
id __weak y;
23+
id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
24+
id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
2525
id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}}
2626
}
2727
@property(retain) id x; // expected-note {{property declared here}}
@@ -30,15 +30,15 @@ @interface Bar {
3030
@end
3131

3232
@implementation Bar
33-
@synthesize x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
34-
@synthesize y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
33+
@synthesize x; // expected-note {{property synthesized here}}
34+
@synthesize y; // expected-note {{property synthesized here}}
3535
@synthesize z; // suppressed
3636
@end
3737

3838
@interface Bas {
3939
@public
40-
id __unsafe_unretained x;
41-
id __weak y;
40+
id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
41+
id __weak y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
4242
id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}}
4343
}
4444
@property(copy) id x; // expected-note {{property declared here}}
@@ -47,8 +47,8 @@ @interface Bas {
4747
@end
4848

4949
@implementation Bas
50-
@synthesize x; // expected-error {{existing instance variable 'x' for strong property 'x' may not be __unsafe_unretained}}
51-
@synthesize y; // expected-error {{existing instance variable 'y' for strong property 'y' may not be __weak}}
50+
@synthesize x; // expected-note {{property synthesized here}}
51+
@synthesize y; // expected-note {{property synthesized here}}
5252
@synthesize z; // suppressed
5353
@end
5454

@@ -70,7 +70,7 @@ @interface Bav
7070
// rdar://9341593
7171
@interface Gorf {
7272
id __unsafe_unretained x;
73-
id y;
73+
id y; // expected-error {{existing instance variable 'y' for property 'y' with assign attribute must be __unsafe_unretained}}
7474
}
7575
@property(assign) id __unsafe_unretained x;
7676
@property(assign) id y; // expected-note {{property declared here}}
@@ -79,13 +79,13 @@ @interface Gorf {
7979

8080
@implementation Gorf
8181
@synthesize x;
82-
@synthesize y; // expected-error {{existing instance variable 'y' for property 'y' with assign attribute must be __unsafe_unretained}}
82+
@synthesize y; // expected-note {{property synthesized here}}
8383
@synthesize z;
8484
@end
8585

8686
@interface Gorf2 {
8787
id __unsafe_unretained x;
88-
id y;
88+
id y; // expected-error {{existing instance variable 'y' for property 'y' with unsafe_unretained attribute must be __unsafe_unretained}}
8989
}
9090
@property(unsafe_unretained) id __unsafe_unretained x;
9191
@property(unsafe_unretained) id y; // expected-note {{property declared here}}
@@ -94,7 +94,7 @@ @interface Gorf2 {
9494

9595
@implementation Gorf2
9696
@synthesize x;
97-
@synthesize y; // expected-error {{existing instance variable 'y' for property 'y' with unsafe_unretained attribute must be __unsafe_unretained}}
97+
@synthesize y; // expected-note {{property synthesized here}}
9898
@synthesize z;
9999
@end
100100

@@ -173,3 +173,12 @@ void foo(Baz *f) {
173173
@interface Boom
174174
@property (readonly) const void * innerPointer __attribute__((objc_returns_inner_pointer)); // expected-error {{'objc_returns_inner_pointer' attribute only applies to methods}}
175175
@end
176+
177+
@interface Foo2 {
178+
id _prop; // expected-error {{existing instance variable '_prop' for property 'prop' with assign attribute must be __unsafe_unretained}}
179+
}
180+
@property (nonatomic, assign) id prop; // expected-note {{property declared here}}
181+
@end
182+
183+
@implementation Foo2
184+
@end

test/SemaObjC/arc-property.m

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// rdar://9309489
33

44
@interface MyClass {
5-
id __weak myString;
5+
id __weak myString; // expected-error {{existing instance variable 'myString' for strong property 'myString' may not be __weak}}
66
id StrongIvar;
7-
id __weak myString2;
7+
id __weak myString2; // expected-error {{existing instance variable 'myString2' for strong property 'myString2' may not be __weak}}
88
id __weak myString3;
9-
id StrongIvar5;
9+
id StrongIvar5; // expected-error {{existing instance variable 'StrongIvar5' for __weak property 'myString5' must be __weak}}
1010
}
1111
@property (strong) id myString; // expected-note {{property declared here}}
1212
@property (strong) id myString1;
@@ -18,21 +18,21 @@ @interface MyClass {
1818
@end
1919

2020
@implementation MyClass
21-
@synthesize myString; // expected-error {{existing instance variable 'myString' for strong property 'myString' may not be __weak}}
21+
@synthesize myString; // expected-note {{property synthesized here}}
2222
@synthesize myString1 = StrongIvar; // OK
23-
@synthesize myString2 = myString2; // expected-error {{existing instance variable 'myString2' for strong property 'myString2' may not be __weak}}
23+
@synthesize myString2 = myString2; // expected-note {{property synthesized here}}
2424
//
2525
@synthesize myString3; // OK
2626
@synthesize myString4; // OK
27-
@synthesize myString5 = StrongIvar5; // expected-error {{existing instance variable 'StrongIvar5' for __weak property 'myString5' must be __weak}}
27+
@synthesize myString5 = StrongIvar5; // expected-note {{property synthesized here}}
2828

2929
@end
3030

3131
// rdar://9340692
3232
@interface Foo {
3333
@public
34-
id __unsafe_unretained x; // should be __weak
35-
id __strong y;
34+
id __unsafe_unretained x; // expected-error {{existing instance variable 'x' for __weak property 'x' must be __weak}}
35+
id __strong y; // expected-error {{existing instance variable 'y' for __weak property 'y' must be __weak}}
3636
id __autoreleasing z; // expected-error {{instance variables cannot have __autoreleasing ownership}}
3737
}
3838
@property(weak) id x; // expected-note {{property declared here}}
@@ -41,8 +41,8 @@ @interface Foo {
4141
@end
4242

4343
@implementation Foo
44-
@synthesize x; // expected-error {{existing instance variable 'x' for __weak property 'x' must be __weak}}
45-
@synthesize y; // expected-error {{existing instance variable 'y' for __weak property 'y' must be __weak}}
44+
@synthesize x; // expected-note {{property synthesized here}}
45+
@synthesize y; // expected-note {{property synthesized here}}
4646
@synthesize z; // suppressed
4747
@end
4848

test/SemaObjC/warn-direct-ivar-access.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
__attribute__((objc_root_class)) @interface MyObject {
55
@public
66
id _myMaster;
7-
id _isTickledPink;
7+
id _isTickledPink; // expected-error {{existing instance variable '_isTickledPink' for property 'isTickledPink'}}
88
int _myIntProp;
99
}
1010
@property(retain) id myMaster;
@@ -15,7 +15,7 @@
1515
@implementation MyObject
1616

1717
@synthesize myMaster = _myMaster;
18-
@synthesize isTickledPink = _isTickledPink; // expected-error {{existing instance variable '_isTickledPink' for property 'isTickledPink'}}
18+
@synthesize isTickledPink = _isTickledPink; // expected-note {{property synthesized here}}
1919
@synthesize myIntProp = _myIntProp;
2020

2121
- (void) doSomething {

test/SemaObjC/weak-property.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
@interface WeakPropertyTest {
55
Class isa;
66
__weak id value;
7-
id x;
7+
id x; // expected-error {{existing instance variable 'x' for __weak property 'x' must be __weak}}
88
}
99
@property (weak) id value1;
1010
@property __weak id value;
@@ -19,6 +19,6 @@ @interface WeakPropertyTest {
1919
@end
2020

2121
@implementation WeakPropertyTest
22-
@synthesize x; // expected-error {{existing instance variable 'x' for __weak property 'x' must be __weak}}
22+
@synthesize x; // expected-note {{property synthesized here}}
2323
@dynamic value1, value, value2, v1,v2,v3,v4;
2424
@end

0 commit comments

Comments
 (0)