From db07b3f7cdcb505329c1280d7cf70791739a7cad Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Tue, 27 Oct 2009 23:02:38 +0000 Subject: [PATCH] Type of a conditional expression with two distinct objective-c class pointer is the most derived common class of the two. This is . git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85337 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ASTContext.h | 4 +++- lib/AST/ASTContext.cpp | 22 ++++++++++++++++++++++ lib/Sema/SemaExpr.cpp | 5 ++++- test/SemaObjC/conditional-expr-6.m | 26 ++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 test/SemaObjC/conditional-expr-6.m diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 3acddab4b2..c4017d3e37 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -1019,7 +1019,9 @@ public: bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, const ObjCInterfaceType *RHS); bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); - + QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, + const ObjCObjectPointerType *RHSOPT); + // Functions for calculating composite types QualType mergeTypes(QualType, QualType); QualType mergeFunctionTypes(QualType, QualType); diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 6c63c524b4..eb6d39225b 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -3846,6 +3846,28 @@ bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, return false; } +/// areCommonBaseCompatible - Returns common base class of the two classes if +/// one found. Note that this is O'2 algorithm. But it will be called as the +/// last type comparison in a ?-exp of ObjC pointer types before a +/// warning is issued. So, its invokation is extremely rare. +QualType ASTContext::areCommonBaseCompatible( + const ObjCObjectPointerType *LHSOPT, + const ObjCObjectPointerType *RHSOPT) { + const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType(); + const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType(); + if (!LHS || !RHS) + return QualType(); + + while (const ObjCInterfaceDecl *LHSIDecl = LHS->getDecl()->getSuperClass()) { + QualType LHSTy = getObjCInterfaceType(LHSIDecl); + LHS = LHSTy->getAs(); + if (canAssignObjCInterfaces(LHS, RHS)) + return getObjCObjectPointerType(LHSTy); + } + + return QualType(); +} + bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS, const ObjCInterfaceType *RHS) { // Verify that the base decls are compatible: the RHS must be a subclass of diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index f4e58f07f8..12f1f513d4 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -3525,7 +3525,10 @@ QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, compositeType = Context.getObjCIdType(); } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { compositeType = Context.getObjCIdType(); - } else { + } else if (!(compositeType = + Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) + ; + else { Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); diff --git a/test/SemaObjC/conditional-expr-6.m b/test/SemaObjC/conditional-expr-6.m new file mode 100644 index 0000000000..88e943f3c6 --- /dev/null +++ b/test/SemaObjC/conditional-expr-6.m @@ -0,0 +1,26 @@ +// RUN: clang-cc -fsyntax-only -verify %s + +@interface NSObject @end + +@interface NSInterm : NSObject +@end + +@interface NSArray : NSInterm +@end + +@interface NSSet : NSObject +@end + + +NSObject* test (int argc) { + NSArray *array = ((void*)0); + NSSet *set = ((void*)0); + return (argc) ? set : array ; +} + + +NSObject* test1 (int argc) { + NSArray *array = ((void*)0); + NSSet *set = ((void*)0); + return (argc) ? array : set; +} -- 2.40.0