[cfe-commits] r139818 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/SemaObjC/class-type-conversion.m
Fariborz Jahanian
fjahanian at apple.com
Thu Sep 15 11:30:22 PDT 2011
Author: fjahanian
Date: Thu Sep 15 13:30:22 2011
New Revision: 139818
URL: http://llvm.org/viewvc/llvm-project?rev=139818&view=rev
Log:
Objective-c: Conversion from type Class to any root class type is allowed
in class methods with no warning. //rdar://10109725
Added:
cfe/trunk/test/SemaObjC/class-type-conversion.m
Modified:
cfe/trunk/lib/Sema/SemaExpr.cpp
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=139818&r1=139817&r2=139818&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Sep 15 13:30:22 2011
@@ -5227,6 +5227,35 @@
return ConvTy;
}
+static Sema::AssignConvertType
+checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType,
+ QualType RHSType);
+/// checkClassTypes - Routine checks for conversion of "Class" type.
+// Conversion from type Class to any root class type in a class method
+// is allowed.
+static Sema::AssignConvertType
+checkClassTypes(Sema &S, QualType LHSType) {
+ // Conversion from type Class to any root class type is allowed.
+ DeclContext *DC = S.CurContext;
+ while (isa<BlockDecl>(DC))
+ DC = DC->getParent();
+ ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(DC);
+ if (MD && MD->isClassMethod()) {
+ ObjCInterfaceDecl *Root = 0;
+ if (ObjCInterfaceDecl * IDecl = MD->getClassInterface())
+ do
+ Root = IDecl;
+ while ((IDecl = IDecl->getSuperClass()));
+ if (Root){
+ QualType RHSType =
+ S.Context.getObjCObjectPointerType(
+ S.Context.getObjCInterfaceType(Root));
+ return checkObjCPointerTypesForAssignment(S, LHSType, RHSType);
+ }
+ }
+ return Sema::IncompatiblePointer;
+}
+
/// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types
/// for assignment compatibility.
static Sema::AssignConvertType
@@ -5243,10 +5272,9 @@
return Sema::Compatible;
}
if (RHSType->isObjCBuiltinType()) {
- // Class is not compatible with ObjC object pointers.
if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() &&
!LHSType->isObjCQualifiedClassType())
- return Sema::IncompatiblePointer;
+ return checkClassTypes(S, LHSType);
return Sema::Compatible;
}
QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType();
Added: cfe/trunk/test/SemaObjC/class-type-conversion.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/class-type-conversion.m?rev=139818&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/class-type-conversion.m (added)
+++ cfe/trunk/test/SemaObjC/class-type-conversion.m Thu Sep 15 13:30:22 2011
@@ -0,0 +1,46 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// rdar://10109725
+
+ at interface NSObject {
+ Class isa;
+}
+- (id)addObserver:(NSObject *)observer; // expected-note 2 {{passing argument to parameter 'observer' here}}
+ at end
+
+ at interface MyClass : NSObject {
+}
+ at end
+
+ at implementation NSObject
++ (void)initialize
+{
+ NSObject *obj = 0;
+ [obj addObserver:self];
+ [obj addObserver:(Class)0];
+}
+
+- init
+{
+ NSObject *obj = 0;
+ [obj addObserver:self];
+ return [obj addObserver:(Class)0]; // expected-warning {{incompatible pointer types sending 'Class' to parameter of type 'NSObject *'}}
+}
+- (id)addObserver:(NSObject *)observer { return 0; }
+ at end
+
+ at implementation MyClass
+
++ (void)initialize
+{
+ NSObject *obj = 0;
+ [obj addObserver:self];
+ [obj addObserver:(Class)0];
+}
+
+- init
+{
+ NSObject *obj = 0;
+ [obj addObserver:self];
+ return [obj addObserver:(Class)0]; // expected-warning {{incompatible pointer types sending 'Class' to parameter of type 'NSObject *'}}
+}
+ at end
More information about the cfe-commits
mailing list