From c98d95677801fedb3ee012a2a244a0a728504165 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 12 Dec 2007 01:00:23 +0000 Subject: [PATCH] Implemented type checking for pointer of objects of protocol-qualified types. Note that incompatible-protocol-qualified-types.m is currently failing. This is unrelated to this patch and Steve is looking at the general problem of not reporting incompitible pointer types in return stetement.. llvm-svn: 44897 --- clang/AST/ASTContext.cpp | 30 ++++++++++++++ clang/include/clang/AST/ASTContext.h | 1 + .../incompatible-protocol-qualified-types.m | 40 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 clang/test/Sema/incompatible-protocol-qualified-types.m diff --git a/clang/AST/ASTContext.cpp b/clang/AST/ASTContext.cpp index e8ed9a209fba..4a40f839f090 100644 --- a/clang/AST/ASTContext.cpp +++ b/clang/AST/ASTContext.cpp @@ -1168,6 +1168,34 @@ bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) { return true; // FIXME: IMPLEMENT. } +bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs, + QualType rhs) { + ObjcQualifiedInterfaceType *lhsQI = + dyn_cast(lhs.getCanonicalType().getTypePtr()); + assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type"); + ObjcQualifiedInterfaceType *rhsQI = + dyn_cast(rhs.getCanonicalType().getTypePtr()); + assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type"); + if (!interfaceTypesAreCompatible(QualType(lhsQI->getInterfaceType(), 0), + QualType(rhsQI->getInterfaceType(), 0))) + return false; + /* All protocols in lhs must have a presense in rhs. */ + for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) { + bool match = false; + ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i); + for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) { + ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j); + if (lhsProto == rhsProto) { + match = true; + break; + } + } + if (!match) + return false; + } + return true; +} + bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) { const VectorType *lVector = lhs->getAsVectorType(); const VectorType *rVector = rhs->getAsVectorType(); @@ -1331,6 +1359,8 @@ bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) { case Type::Vector: case Type::OCUVector: return vectorTypesAreCompatible(lcanon, rcanon); + case Type::ObjcQualifiedInterface: + return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon); default: assert(0 && "unexpected type"); } diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h index 7170f4572bdd..f77895074bb7 100644 --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -284,6 +284,7 @@ public: /// Objective-C specific type checking. bool interfaceTypesAreCompatible(QualType, QualType); + bool QualifiedInterfaceTypesAreCompatible(QualType, QualType); bool objcTypesAreCompatible(QualType, QualType); bool isObjcIdType(QualType T) const { if (!IdStructType) // ObjC isn't enabled diff --git a/clang/test/Sema/incompatible-protocol-qualified-types.m b/clang/test/Sema/incompatible-protocol-qualified-types.m new file mode 100644 index 000000000000..0594eb728485 --- /dev/null +++ b/clang/test/Sema/incompatible-protocol-qualified-types.m @@ -0,0 +1,40 @@ +// RUN: clang -fsyntax-only -verify %s + +@protocol MyProto1 +@end + +@protocol MyProto2 +@end + +@interface INTF @end + +INTF * Func(INTF *p2) +{ + return p2; +} + + +INTF * Func1(INTF *p2) +{ + return p2; +} + +INTF * Func2(INTF *p2) +{ + Func(p2); // expected-warning {{incompatible pointer types passing}} + return p2; // expected-warning {{incompatible pointer types passing}} +} + + + +INTF * Func3(INTF *p2) +{ + return p2; // expected-warning {{incompatible pointer types passing}} +} + + +INTF * Func4(INTF *p2) +{ + return p2; +} +