From 0f01debd47b43c287f8f1135cd3f9679a5a3ab2e Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Thu, 20 Dec 2007 22:37:58 +0000 Subject: [PATCH] More objective-c type checking. This time comparing objective-c known objects. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@45269 91177308-0d34-0410-b5e6-96231b3b80d8 --- AST/ASTContext.cpp | 14 +++++++++++++- test/Sema/objc-comptypes-1.m | 6 ++++++ test/Sema/objc-string.m | 7 +++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp index 531b348489..417c56570a 100644 --- a/AST/ASTContext.cpp +++ b/AST/ASTContext.cpp @@ -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(lhs.getTypePtr()); + ObjcInterfaceType *rhsIT = cast(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, diff --git a/test/Sema/objc-comptypes-1.m b/test/Sema/objc-comptypes-1.m index 246dfa1a41..c4b457ee68 100644 --- a/test/Sema/objc-comptypes-1.m +++ b/test/Sema/objc-comptypes-1.m @@ -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' variable should generate a @@ -48,6 +49,7 @@ int main() a warning, unless done from an 'id' or an 'id' (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' 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 *')}} diff --git a/test/Sema/objc-string.m b/test/Sema/objc-string.m index 4fe4394164..c73948292d 100644 --- a/test/Sema/objc-string.m +++ b/test/Sema/objc-string.m @@ -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}} -- 2.50.1