]> granicus.if.org Git - clang/commitdiff
More objective-c type checking. This time comparing objective-c known objects.
authorFariborz Jahanian <fjahanian@apple.com>
Thu, 20 Dec 2007 22:37:58 +0000 (22:37 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Thu, 20 Dec 2007 22:37:58 +0000 (22:37 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45269 91177308-0d34-0410-b5e6-96231b3b80d8

AST/ASTContext.cpp
test/Sema/objc-comptypes-1.m
test/Sema/objc-string.m

index 531b3484894a4c97cded481322586e62cc900071..417c56570ac71290e484e330fec707b10ced630a 100644 (file)
@@ -1235,7 +1235,19 @@ bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
 }
 
 bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
-  return true; // FIXME: IMPLEMENT.
+  if (lhs == rhs)
+    return true;
+  ObjcInterfaceType *lhsIT = cast<ObjcInterfaceType>(lhs.getTypePtr());
+  ObjcInterfaceType *rhsIT = cast<ObjcInterfaceType>(rhs.getTypePtr());
+  ObjcInterfaceDecl *rhsIDecl = rhsIT->getDecl();
+  ObjcInterfaceDecl *lhsIDecl = lhsIT->getDecl();
+  // rhs is derived from lhs it is OK; else it is not OK.
+  while (rhsIDecl != NULL) {
+    if (rhsIDecl == lhsIDecl)
+      return true;
+    rhsIDecl = rhsIDecl->getSuperClass();
+  }
+  return false;
 }
 
 bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs, 
index 246dfa1a415950031de280f9d038ad32f72aed4b..c4b457ee6801a8b585510962e456d3e394addb07 100644 (file)
@@ -33,6 +33,7 @@ int main()
   /* Assigning to a 'MyClass *' variable should always generate a
      warning, unless done from an 'id'.  */
   obj_c = obj;    /* Ok */
+  obj_c = obj_cp; // // expected-warning {{incompatible pointer types assigning 'MyOtherClass *' to 'MyClass *'}}
   obj_c = obj_C;  // expected-warning {{incompatible pointer types assigning 'Class' to 'MyClass *'}}
 
   /* Assigning to an 'id<MyProtocol>' variable should generate a
@@ -48,6 +49,7 @@ int main()
      a warning, unless done from an 'id' or an 'id<MyProtocol>' (since
      MyOtherClass implements MyProtocol).  */
   obj_cp = obj;    /* Ok */
+  obj_cp = obj_c;  // expected-warning {{incompatible pointer types assigning 'MyClass *' to 'MyOtherClass *'}}
   obj_cp = obj_p;  /* Ok */
   obj_cp = obj_C;  // expected-warning {{incompatible pointer types assigning 'Class' to 'MyOtherClass *'}}
 
@@ -64,6 +66,10 @@ int main()
   /* Any comparison between 'MyClass *' and anything which is not an 'id'
      must generate a warning.  */
   if (obj_p == obj_c) foo() ; // expected-error {{invalid operands to binary expression ('id<MyProtocol>' and 'MyClass *')}}
+
+  if (obj_c == obj_cp) foo() ; // expected-warning {{comparison of distinct pointer types ('MyClass *' and 'MyOtherClass *')}} 
+  if (obj_cp == obj_c) foo() ; // expected-warning {{comparison of distinct pointer types ('MyOtherClass *' and 'MyClass *')}}
+
   if (obj_c == obj_C) foo() ;  // expected-warning {{comparison of distinct pointer types ('MyClass *' and 'Class')}}
   if (obj_C == obj_c) foo() ;  // expected-warning {{comparison of distinct pointer types ('Class' and 'MyClass *')}} 
 
index 4fe4394164861c66b7e4a6cea24abfeaa2081eeb..c73948292d81edafa8d477243b0fd6f5a65e17a1 100644 (file)
@@ -1,12 +1,11 @@
 // RUN: clang %s -verify -fsyntax-only
 
-@class NSString;
 @interface NSConstantString;
 @end
 
 
 
-NSString *s = @"123"; // simple
-NSString *t = @"123" @"456"; // concat
-NSString *u = @"123" @ blah; // expected-error: {{unexpected token}}
+NSConstantString *s = @"123"; // simple
+NSConstantString *t = @"123" @"456"; // concat
+NSConstantString *u = @"123" @ blah; // expected-error: {{unexpected token}}