[cfe-commits] r85337 - in /cfe/trunk: include/clang/AST/ASTContext.h lib/AST/ASTContext.cpp lib/Sema/SemaExpr.cpp test/SemaObjC/conditional-expr-6.m
Fariborz Jahanian
fjahanian at apple.com
Tue Oct 27 16:02:39 PDT 2009
Author: fjahanian
Date: Tue Oct 27 18:02:38 2009
New Revision: 85337
URL: http://llvm.org/viewvc/llvm-project?rev=85337&view=rev
Log:
Type of a conditional expression with two distinct objective-c
class pointer is the most derived common class of the two.
This is <rdar://problem/7334235>.
Added:
cfe/trunk/test/SemaObjC/conditional-expr-6.m
Modified:
cfe/trunk/include/clang/AST/ASTContext.h
cfe/trunk/lib/AST/ASTContext.cpp
cfe/trunk/lib/Sema/SemaExpr.cpp
Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=85337&r1=85336&r2=85337&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Tue Oct 27 18:02:38 2009
@@ -1019,7 +1019,9 @@
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);
Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=85337&r1=85336&r2=85337&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Tue Oct 27 18:02:38 2009
@@ -3846,6 +3846,28 @@
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<ObjCInterfaceType>();
+ 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
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=85337&r1=85336&r2=85337&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Oct 27 18:02:38 2009
@@ -3525,7 +3525,10 @@
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();
Added: cfe/trunk/test/SemaObjC/conditional-expr-6.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/conditional-expr-6.m?rev=85337&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/conditional-expr-6.m (added)
+++ cfe/trunk/test/SemaObjC/conditional-expr-6.m Tue Oct 27 18:02:38 2009
@@ -0,0 +1,26 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+ at interface NSObject @end
+
+ at interface NSInterm : NSObject
+ at end
+
+ at interface NSArray : NSInterm
+ at end
+
+ at interface NSSet : NSObject
+ at 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;
+}
More information about the cfe-commits
mailing list