]> granicus.if.org Git - clang/commitdiff
Fix a gcc compatibility issue which allows more protocol-qualified id on RHS to be
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 4 Jun 2008 19:00:03 +0000 (19:00 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 4 Jun 2008 19:00:03 +0000 (19:00 +0000)
assigned to less protocol qualified object on LHS.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51956 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExprObjC.cpp
test/Sema/objc-comptypes-9.m [new file with mode: 0644]

index c9cfc92f4d6b9b69a547371894d5fcb072011bd7..108ff3c21e074aca58e02a06c9f93286c1168c3e 100644 (file)
@@ -327,13 +327,21 @@ static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
 /// lookupCategory is true). 
 static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
                                     ObjCInterfaceDecl *IDecl, 
-                                    bool lookupCategory) {
+                                    bool lookupCategory,
+                                    bool RHSIsQualifiedID = false) {
   
   // 1st, look up the class.
   ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
   for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
     if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
       return true;
+    // This is dubious and is added to be compatible with gcc.
+    // In gcc, it is also allowed assigning a protocol-qualified 'id'
+    // type to a LHS object when protocol in qualified LHS is in list
+    // of protocols in the rhs 'id' object. This IMO, should be a bug.
+    else if (RHSIsQualifiedID &&
+             ProtocolCompatibleWithProtocol(protoList[i], lProto))
+      return true;
   }
   
   // 2nd, look up the category.
@@ -350,7 +358,8 @@ static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
   // 3rd, look up the super class(s)
   if (IDecl->getSuperClass())
     return 
-      ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
+      ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
+                              RHSIsQualifiedID);
   
   return false;
 }
@@ -481,7 +490,7 @@ bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
     ObjCInterfaceDecl *lhsID = IT->getDecl();
     for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
       ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
-      if (!ClassImplementsProtocol(rhsProto, lhsID, compare))
+      if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
         return false;
     }
     return true;
diff --git a/test/Sema/objc-comptypes-9.m b/test/Sema/objc-comptypes-9.m
new file mode 100644 (file)
index 0000000..0358fc3
--- /dev/null
@@ -0,0 +1,84 @@
+// RUN: clang -fsyntax-only %s
+
+typedef signed char BOOL;
+typedef int NSInteger;
+typedef unsigned int NSUInteger;
+typedef struct _NSZone NSZone;
+@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
+
+@protocol NSObject
+- (BOOL)isEqual:(id)object;
+@end
+
+@protocol NSCopying
+- (id)copyWithZone:(NSZone *)zone;
+@end
+
+@protocol NSMutableCopying
+- (id)mutableCopyWithZone:(NSZone *)zone;
+@end
+
+@protocol NSCoding
+- (void)encodeWithCoder:(NSCoder *)aCoder;
+@end
+
+@interface NSObject <NSObject> {}
+@end
+
+@class NSArray;
+
+typedef struct {} NSFastEnumerationState;
+
+@protocol NSFastEnumeration
+- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
+@end
+
+@class NSString;
+
+@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
+- (NSUInteger)count;
+- (id)objectAtIndex:(NSUInteger)index;
+@end
+
+typedef unsigned short unichar;
+
+@interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
+- (NSUInteger)length;
+@end
+
+@interface NSSimpleCString : NSString
+{}
+
+@end
+
+@interface NSConstantString : NSSimpleCString @end
+
+extern void *_NSConstantStringClassReference;
+
+@interface NSResponder : NSObject <NSCoding> {}
+@end
+
+@class NSDate, NSDictionary, NSError, NSException, NSNotification;
+
+@interface NSWindowController : NSResponder <NSCoding> {}
+@end
+
+@class PBXBuildLog, PBXBuildLogItem, PBXBuildLogContainerItem, XCWorkQueueCommand, XCBuildLogContainerItemMutationState;
+
+@protocol PBXBuildLogContainerItems <NSObject>
+- (PBXBuildLog *)buildLog;
+@end
+
+@interface PBXBuildLogItem : NSObject {}
+- (id <PBXBuildLogContainerItems>)superitem;
+@end
+@interface PBXBuildResultsModule
+@end
+
+@implementation PBXBuildResultsModule
+- (void) revealItems
+{
+        PBXBuildLogItem *objItem;
+        PBXBuildLogItem *superitem = [objItem superitem];
+}
+@end