From b788d9bd277d8e8c829cd5a53fedb934fdb0ae48 Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Tue, 3 Jun 2008 14:04:54 +0000 Subject: [PATCH] Allow implicit pointer/int conversions on ObjCQualifiedIdTypes in Sema::CheckCompareOperands() and Sema::CheckAssignmentConstraints(). Fixes clang on xcode: error: incompatible type sending 'id', expected 'NSCellType'. llvm-svn: 51902 --- clang/lib/Sema/SemaExpr.cpp | 20 ++++++++++++++------ clang/test/Sema/objc-comptypes-7.m | 8 ++++---- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index c384a9a5a2c9..8b31d8856a78 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -1301,6 +1301,11 @@ Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) { if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false)) return Compatible; + // Relax integer conversions like we do for pointers below. + if (rhsType->isIntegerType()) + return IntToPointer; + if (lhsType->isIntegerType()) + return PointerToInt; return Incompatible; } @@ -1647,12 +1652,14 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc, ImpCastExprToType(rex, lType); // promote the pointer to pointer return Context.IntTy; } - if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType()) - && ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { - ImpCastExprToType(rex, lType); - return Context.IntTy; + if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())) { + if (ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { + ImpCastExprToType(rex, lType); + return Context.IntTy; + } } - if (lType->isPointerType() && rType->isIntegerType()) { + if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) && + rType->isIntegerType()) { if (!RHSIsNull) Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, lType.getAsString(), rType.getAsString(), @@ -1660,7 +1667,8 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc, ImpCastExprToType(rex, lType); // promote the integer to pointer return Context.IntTy; } - if (lType->isIntegerType() && rType->isPointerType()) { + if (lType->isIntegerType() && + (rType->isPointerType() || rType->isObjCQualifiedIdType())) { if (!LHSIsNull) Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, lType.getAsString(), rType.getAsString(), diff --git a/clang/test/Sema/objc-comptypes-7.m b/clang/test/Sema/objc-comptypes-7.m index 6e5ff0282fdd..ef3a7790a3d3 100644 --- a/clang/test/Sema/objc-comptypes-7.m +++ b/clang/test/Sema/objc-comptypes-7.m @@ -27,7 +27,7 @@ int main() obj = i; // expected-warning {{incompatible integer to pointer conversion assigning 'int', expected 'id'}} obj = j; // expected-warning {{incompatible pointer types assigning 'int *', expected 'id'}} - obj_p = i; // expected-error {{incompatible type assigning 'int', expected 'id' }} + obj_p = i; // expected-warning {{incompatible integer to pointer conversion assigning 'int', expected 'id'}} obj_p = j; // expected-error {{incompatible type assigning 'int *', expected 'id'}} obj_c = i; // expected-warning {{incompatible integer to pointer conversion assigning 'int', expected 'MyClass *'}} @@ -37,7 +37,7 @@ int main() obj_C = j; // expected-warning {{incompatible pointer types assigning 'int *', expected 'Class'}} i = obj; // expected-warning {{incompatible pointer to integer conversion assigning 'id', expected 'int'}} - i = obj_p; // expected-error {{incompatible type assigning 'id', expected 'int'}} + i = obj_p; // expected-warning {{incompatible pointer to integer conversion assigning 'id', expected 'int'}} i = obj_c; // expected-warning {{incompatible pointer to integer conversion assigning 'MyClass *', expected 'int'}} i = obj_C; // expected-warning {{incompatible pointer to integer conversion assigning 'Class', expected 'int'}} @@ -56,8 +56,8 @@ int main() if (obj_c == j) foo() ; // expected-warning {{comparison of distinct pointer types ('MyClass *' and 'int *')}} if (j == obj_c) foo() ; // expected-warning {{comparison of distinct pointer types ('int *' and 'MyClass *')}} - if (obj_p == i) foo() ; // expected-error {{invalid operands to binary expression ('id' and 'int')}} - if (i == obj_p) foo() ; // expected-error {{invalid operands to binary expression ('int' and 'id')}} + if (obj_p == i) foo() ; // expected-warning {{comparison between pointer and integer ('id' and 'int')}} + if (i == obj_p) foo() ; // expected-warning {{comparison between pointer and integer ('int' and 'id')}} if (obj_p == j) foo() ; // expected-error {{invalid operands to binary expression ('id' and 'int *')}} if (j == obj_p) foo() ; // expected-error {{invalid operands to binary expression ('int *' and 'id')}}