[cfe-commits] r156871 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaObjCProperty.cpp test/SemaObjC/property-impl-misuse.m test/SemaObjC/property-typecheck-1.m
Fariborz Jahanian
fjahanian at apple.com
Tue May 15 15:37:05 PDT 2012
Author: fjahanian
Date: Tue May 15 17:37:04 2012
New Revision: 156871
URL: http://llvm.org/viewvc/llvm-project?rev=156871&view=rev
Log:
objective-c: perform strict type checking on property
type and its accessor type and issue error if types
are incompatible, instead of crashing in IRgen.
// rdar://1105153
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaObjCProperty.cpp
cfe/trunk/test/SemaObjC/property-impl-misuse.m
cfe/trunk/test/SemaObjC/property-typecheck-1.m
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=156871&r1=156870&r2=156871&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue May 15 17:37:04 2012
@@ -685,6 +685,8 @@
def error_property_ivar_type : Error<
"type of property %0 (%1) does not match type of ivar %2 (%3)">;
+def error_property_accessor_type : Error<
+ "type of property %0 (%1) does not match type of accessor %2 (%3)">;
def error_ivar_in_superclass_use : Error<
"property %0 attempting to use ivar %1 declared in super class %2">;
def error_weak_property : Error<
Modified: cfe/trunk/lib/Sema/SemaObjCProperty.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaObjCProperty.cpp?rev=156871&r1=156870&r2=156871&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaObjCProperty.cpp (original)
+++ cfe/trunk/lib/Sema/SemaObjCProperty.cpp Tue May 15 17:37:04 2012
@@ -1050,21 +1050,42 @@
bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property,
ObjCMethodDecl *GetterMethod,
SourceLocation Loc) {
- if (GetterMethod &&
- !Context.hasSameType(GetterMethod->getResultType().getNonReferenceType(),
- property->getType().getNonReferenceType())) {
- AssignConvertType result = Incompatible;
- if (property->getType()->isObjCObjectPointerType())
- result = CheckAssignmentConstraints(Loc, GetterMethod->getResultType(),
- property->getType());
- if (result != Compatible) {
- Diag(Loc, diag::warn_accessor_property_type_mismatch)
- << property->getDeclName()
- << GetterMethod->getSelector();
- Diag(GetterMethod->getLocation(), diag::note_declared_at);
- return true;
+ if (!GetterMethod)
+ return false;
+ QualType GetterType = GetterMethod->getResultType().getNonReferenceType();
+ QualType PropertyIvarType = property->getType().getNonReferenceType();
+ bool compat = Context.hasSameType(PropertyIvarType, GetterType);
+ if (!compat) {
+ if (isa<ObjCObjectPointerType>(PropertyIvarType) &&
+ isa<ObjCObjectPointerType>(GetterType))
+ compat =
+ Context.canAssignObjCInterfaces(
+ PropertyIvarType->getAs<ObjCObjectPointerType>(),
+ GetterType->getAs<ObjCObjectPointerType>());
+ else if (CheckAssignmentConstraints(Loc, PropertyIvarType, GetterType)
+ != Compatible) {
+ Diag(Loc, diag::error_property_accessor_type)
+ << property->getDeclName() << PropertyIvarType
+ << GetterMethod->getSelector() << GetterType;
+ Diag(GetterMethod->getLocation(), diag::note_declared_at);
+ return true;
+ } else {
+ compat = true;
+ QualType lhsType =Context.getCanonicalType(PropertyIvarType).getUnqualifiedType();
+ QualType rhsType =Context.getCanonicalType(GetterType).getUnqualifiedType();
+ if (lhsType != rhsType && lhsType->isArithmeticType())
+ compat = false;
}
}
+
+ if (!compat) {
+ Diag(Loc, diag::warn_accessor_property_type_mismatch)
+ << property->getDeclName()
+ << GetterMethod->getSelector();
+ Diag(GetterMethod->getLocation(), diag::note_declared_at);
+ return true;
+ }
+
return false;
}
Modified: cfe/trunk/test/SemaObjC/property-impl-misuse.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/property-impl-misuse.m?rev=156871&r1=156870&r2=156871&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/property-impl-misuse.m (original)
+++ cfe/trunk/test/SemaObjC/property-impl-misuse.m Tue May 15 17:37:04 2012
@@ -34,3 +34,16 @@
self.gradientStyle; // expected-error {{property 'gradientStyle' not found on object of type 'Class'}}
}
@end
+
+// rdar://1105153
+ at interface rdar1105153
+ at property int P; // expected-error {{type of property 'P' ('int') does not match type of accessor 'P' ('void')}}
+- (void)P; // expected-note {{declared here}}
+
+ at property int P1; // expected-warning {{type of property 'P1' does not match type of accessor 'P1'}}
+- (double) P1; // expected-note {{declared here}}
+
+ at property int P2; // expected-error {{type of property 'P2' ('int') does not match type of accessor 'P2' ('double *')}}
+- (double*)P2; // expected-note {{declared here}}
+
+ at end
Modified: cfe/trunk/test/SemaObjC/property-typecheck-1.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/property-typecheck-1.m?rev=156871&r1=156870&r2=156871&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/property-typecheck-1.m (original)
+++ cfe/trunk/test/SemaObjC/property-typecheck-1.m Tue May 15 17:37:04 2012
@@ -73,11 +73,11 @@
NSArray* first;
}
- at property (readonly) NSArray* pieces; // expected-warning {{type of property 'pieces' does not match type of accessor 'pieces'}}
- at property (readonly) NSMutableArray* first;
+ at property (readonly) NSArray* pieces;
+ at property (readonly) NSMutableArray* first; // expected-warning {{type of property 'first' does not match type of accessor 'first'}}
-- (NSMutableArray*) pieces; // expected-note {{declared here}} // expected-note {{declared here}}
-- (NSArray*) first;
+- (NSMutableArray*) pieces;
+- (NSArray*) first; // expected-note 2 {{declared here}}
@end
@interface Class2 {
@@ -90,12 +90,12 @@
- (id) lastPiece
{
- return container.pieces; // expected-warning {{type of property 'pieces' does not match type of accessor 'pieces'}}
+ return container.pieces;
}
- (id)firstPeice
{
- return container.first;
+ return container.first; // expected-warning {{type of property 'first' does not match type of accessor 'first'}}
}
@end
More information about the cfe-commits
mailing list