Property declared in continuation class can only be used to
change a readonly property declared in the class (and its inherited protocols)
to writable property. (Fixes radar 7350645).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85836 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/DeclObjC.h b/include/clang/AST/DeclObjC.h
index abe4de4..bcd28ea 100644
--- a/include/clang/AST/DeclObjC.h
+++ b/include/clang/AST/DeclObjC.h
@@ -347,6 +347,8 @@
ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;
+ ObjCPropertyDecl *FindPropertyVisibleInPrimaryClass(
+ IdentifierInfo *PropertyId) const;
// Marks the end of the container.
SourceLocation getAtEndLoc() const { return AtEndLoc; }
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 8c7c0cb..3b39fa9 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -264,7 +264,8 @@
"'copy' attribute must be specified for the block property "
"when -fobjc-gc-only is specified">;
def err_use_continuation_class : Error<
- "attribute of property in continuation class of %0 can only be 'readwrite'">;
+ "property declaration in continuation class of %0 is to change a 'readonly' "
+ "property to 'readwrite'">;
def err_continuation_class : Error<"continuation class has no primary class">;
def err_property_type : Error<"property cannot have array or function type %0">;
def error_missing_property_context : Error<
@@ -274,7 +275,7 @@
def error_category_property : Error<
"property declared in category %0 cannot be implemented in "
"class implementation">;
-def note_category_property : Note<
+def note_property_declare : Note<
"property declared here">;
def error_synthesize_category_decl : Error<
"@synthesize not allowed in a category's implementation">;
diff --git a/lib/AST/DeclObjC.cpp b/lib/AST/DeclObjC.cpp
index 49a5668..7b48b72 100644
--- a/lib/AST/DeclObjC.cpp
+++ b/lib/AST/DeclObjC.cpp
@@ -118,6 +118,27 @@
return 0;
}
+/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
+/// with name 'PropertyId' in the primary class; including those in protocols
+/// (direct or indirect) used by the promary class.
+/// FIXME: Convert to DeclContext lookup...
+///
+ObjCPropertyDecl *
+ObjCContainerDecl::FindPropertyVisibleInPrimaryClass(
+ IdentifierInfo *PropertyId) const {
+ assert(isa<ObjCInterfaceDecl>(this) && "FindPropertyVisibleInPrimaryClass");
+ for (prop_iterator I = prop_begin(), E = prop_end(); I != E; ++I)
+ if ((*I)->getIdentifier() == PropertyId)
+ return *I;
+ const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(this);
+ // Look through protocols.
+ for (ObjCInterfaceDecl::protocol_iterator I = OID->protocol_begin(),
+ E = OID->protocol_end(); I != E; ++I)
+ if (ObjCPropertyDecl *P = (*I)->FindPropertyDeclaration(PropertyId))
+ return P;
+ return 0;
+}
+
void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
ASTContext &C)
diff --git a/lib/Sema/SemaDeclObjC.cpp b/lib/Sema/SemaDeclObjC.cpp
index 881652f..f9f0168 100644
--- a/lib/Sema/SemaDeclObjC.cpp
+++ b/lib/Sema/SemaDeclObjC.cpp
@@ -1893,17 +1893,9 @@
// handling.
if ((CCPrimary = CDecl->getClassInterface())) {
// Find the property in continuation class's primary class only.
- ObjCPropertyDecl *PIDecl = 0;
IdentifierInfo *PropertyId = FD.D.getIdentifier();
- for (ObjCInterfaceDecl::prop_iterator
- I = CCPrimary->prop_begin(), E = CCPrimary->prop_end();
- I != E; ++I)
- if ((*I)->getIdentifier() == PropertyId) {
- PIDecl = *I;
- break;
- }
-
- if (PIDecl) {
+ if (ObjCPropertyDecl *PIDecl =
+ CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) {
// property 'PIDecl's readonly attribute will be over-ridden
// with continuation class's readwrite property attribute!
unsigned PIkind = PIDecl->getPropertyAttributes();
@@ -1917,9 +1909,11 @@
if (Attributes & ObjCDeclSpec::DQ_PR_copy)
PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy);
PIDecl->setSetterName(SetterSel);
- } else
+ } else {
Diag(AtLoc, diag::err_use_continuation_class)
<< CCPrimary->getDeclName();
+ Diag(PIDecl->getLocation(), diag::note_property_declare);
+ }
*isOverridingProperty = true;
// Make sure setter decl is synthesized, and added to primary
// class's list.
@@ -2051,7 +2045,7 @@
dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) {
if (CD->getIdentifier()) {
Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName();
- Diag(property->getLocation(), diag::note_category_property);
+ Diag(property->getLocation(), diag::note_property_declare);
return DeclPtrTy();
}
}
diff --git a/test/SemaObjC/continuation-class-err.m b/test/SemaObjC/continuation-class-err.m
index f516a93..e1ae39b 100644
--- a/test/SemaObjC/continuation-class-err.m
+++ b/test/SemaObjC/continuation-class-err.m
@@ -6,10 +6,35 @@
id _object1;
}
@property(readonly) id object;
-@property(readwrite, assign) id object1;
+@property(readwrite, assign) id object1; // expected-note {{property declared here}}
@end
@interface ReadOnly ()
@property(readwrite, copy) id object;
-@property(readonly) id object1; // expected-error {{attribute of property in continuation class of 'ReadOnly' can only be 'readwrite'}}
+@property(readonly) id object1; // expected-error {{property declaration in continuation class of 'ReadOnly' is to change a 'readonly' property to 'readwrite'}}
@end
+
+@protocol Proto
+ @property (copy) id fee; // expected-note {{property declared here}}
+@end
+
+@protocol Foo<Proto>
+ @property (copy) id foo; // expected-note {{property declared here}}
+@end
+
+@interface Bar <Foo> {
+ id _foo;
+ id _fee;
+}
+@end
+
+@interface Bar ()
+@property (copy) id foo; // expected-error {{property declaration in continuation class of 'Bar' is to change a 'readonly' property to 'readwrite'}}
+@property (copy) id fee; // expected-error {{property declaration in continuation class of 'Bar' is to change a 'readonly' property to 'readwrite'}}
+@end
+
+@implementation Bar
+@synthesize foo = _foo;
+@synthesize fee = _fee;
+@end
+