Skip to content

Commit 52a503d

Browse files
committed
Add -Wobjc-property-assign-on-object-type.
This is a warning about using 'assign' instead of 'unsafe_unretained' in Objective-C property declarations. It's off by default because there isn't consensus in the Objective-C steering group that this is the right thing to do, but we're nonetheless okay with adding it because there's a substantial pool of Objective-C programmers who will appreciate the warning. Patch by Alfred Zien! llvm-svn: 341489
1 parent d2eb03a commit 52a503d

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ def FunctionDefInObjCContainer : DiagGroup<"function-def-in-objc-container">;
380380
def BadFunctionCast : DiagGroup<"bad-function-cast">;
381381
def ObjCPropertyImpl : DiagGroup<"objc-property-implementation">;
382382
def ObjCPropertyNoAttribute : DiagGroup<"objc-property-no-attribute">;
383+
def ObjCPropertyAssignOnObjectType : DiagGroup<"objc-property-assign-on-object-type">;
383384
def ObjCProtocolQualifiers : DiagGroup<"objc-protocol-qualifiers">;
384385
def ObjCMissingSuperCalls : DiagGroup<"objc-missing-super-calls">;
385386
def ObjCDesignatedInit : DiagGroup<"objc-designated-initializers">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,9 @@ def err_objc_property_attr_mutually_exclusive : Error<
10461046
"property attributes '%0' and '%1' are mutually exclusive">;
10471047
def err_objc_property_requires_object : Error<
10481048
"property with '%0' attribute must be of object type">;
1049+
def warn_objc_property_assign_on_object : Warning<
1050+
"'assign' property of object type may become a dangling reference; consider using 'unsafe_unretained'">,
1051+
InGroup<ObjCPropertyAssignOnObjectType>, DefaultIgnore;
10491052
def warn_objc_property_no_assignment_attribute : Warning<
10501053
"no 'assign', 'retain', or 'copy' attribute is specified - "
10511054
"'assign' is assumed">,

clang/lib/Sema/SemaObjCProperty.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,14 @@ void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
25572557
PropertyDecl->setInvalidDecl();
25582558
}
25592559

2560+
// Check for assign on object types.
2561+
if ((Attributes & ObjCDeclSpec::DQ_PR_assign) &&
2562+
!(Attributes & ObjCDeclSpec::DQ_PR_unsafe_unretained) &&
2563+
PropertyTy->isObjCRetainableType() &&
2564+
!PropertyTy->isObjCARCImplicitlyUnretainedType()) {
2565+
Diag(Loc, diag::warn_objc_property_assign_on_object);
2566+
}
2567+
25602568
// Check for more than one of { assign, copy, retain }.
25612569
if (Attributes & ObjCDeclSpec::DQ_PR_assign) {
25622570
if (Attributes & ObjCDeclSpec::DQ_PR_copy) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -Wobjc-property-assign-on-object-type %s
2+
3+
@interface Foo @end
4+
@protocol Prot @end
5+
6+
@interface Bar
7+
@property(assign, readonly) Foo* o1; // expected-warning {{'assign' property of object type may become a dangling reference; consider using 'unsafe_unretained'}}
8+
@property(unsafe_unretained, readonly) Foo* o2;
9+
10+
@property(assign) Class classProperty;
11+
@property(assign) Class<Prot> classWithProtocolProperty;
12+
@property(assign) int s1;
13+
@property(assign) int* s2;
14+
@end
15+
16+
@interface Bar ()
17+
@property(readwrite) Foo* o1;
18+
@property(readwrite) Foo* o2;
19+
@end

clang/test/SemaObjC/property-in-class-extension-1.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fobjc-weak -verify -Weverything %s
2-
// RUN: %clang_cc1 -x objective-c++ -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fobjc-weak -fsyntax-only -verify -Weverything %s
1+
// RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fobjc-weak -verify -Wproperty-attribute-mismatch %s
2+
// RUN: %clang_cc1 -x objective-c++ -triple x86_64-apple-darwin11 -fobjc-runtime-has-weak -fobjc-weak -fsyntax-only -verify -Wproperty-attribute-mismatch %s
33
// rdar://12103400
44

55
@class NSString;

0 commit comments

Comments
 (0)