[cfe-commits] r134624 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td include/clang/Sema/Sema.h lib/Sema/SemaExpr.cpp test/SemaObjC/arc-unavailable-for-weakref.m

Fariborz Jahanian fjahanian at apple.com
Thu Jul 7 11:55:47 PDT 2011


Author: fjahanian
Date: Thu Jul  7 13:55:47 2011
New Revision: 134624

URL: http://llvm.org/viewvc/llvm-project?rev=134624&view=rev
Log:
objc-arc: diagnose assignment/cast of a weak-unavailable
object to a __weak object/type. // rdar://9732636.
This is objc side of things. objc++ side tbd.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=134624&r1=134623&r2=134624&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Jul  7 13:55:47 2011
@@ -2572,6 +2572,11 @@
   "the current deployment target does not support automated __weak references">;
 def err_arc_unsupported_weak_class : Error<
   "class is incompatible with __weak references">;
+def err_arc_weak_unavailable_assign : Error<
+  "assignment of a weak-unavailable object to a __weak object">;
+def err_arc_cast_of_weak_unavailable : Error<
+  "cast of weak-unavailable object of type %0 to"
+  " a __weak object of type %1">;
 def err_arc_illegal_explicit_message : Error<
   "ARC forbids explicit message send of %0">;
 def err_arc_unused_init_message : Error<

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=134624&r1=134623&r2=134624&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Thu Jul  7 13:55:47 2011
@@ -5426,6 +5426,10 @@
     /// "id <XXX>" = "Foo *", where "Foo *" doesn't implement the XXX protocol.
     IncompatibleObjCQualifiedId,
 
+    /// IncompatibleObjCWeakRef - Assigning a weak-unavailable object to an
+    /// object with __weak qualifier.
+    IncompatibleObjCWeakRef,
+
     /// Incompatible - We reject this conversion outright, it is invalid to
     /// represent it in the AST.
     Incompatible

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=134624&r1=134623&r2=134624&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Jul  7 13:55:47 2011
@@ -4065,6 +4065,23 @@
           return ExprError();
         }
       }
+    } 
+    else {
+      QualType canCastType = 
+        Context.getCanonicalType(castType).getUnqualifiedType();
+        if (isa<ObjCObjectPointerType>(canCastType) &&
+            castType.getObjCLifetime() == Qualifiers::OCL_Weak &&
+            castExprType->isObjCObjectPointerType()) {
+          if (const ObjCObjectPointerType *ObjT =
+              castExprType->getAs<ObjCObjectPointerType>())
+            if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) {
+              Diag(castExpr->getLocStart(), 
+                   diag::err_arc_cast_of_weak_unavailable)
+                << castExprType << castType 
+                << castExpr->getSourceRange();
+              return ExprError();
+            }
+        }
     }
   }
   
@@ -5124,6 +5141,7 @@
 Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs,
                                  CastKind &Kind) {
   QualType rhsType = rhs.get()->getType();
+  QualType origLhsType = lhsType;
 
   // Get canonical types.  We're not formatting these types, just comparing
   // them.
@@ -5278,7 +5296,17 @@
     // A* -> B*
     if (rhsType->isObjCObjectPointerType()) {
       Kind = CK_BitCast;
-      return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType);
+      Sema::AssignConvertType result = 
+        checkObjCPointerTypesForAssignment(*this, lhsType, rhsType);
+      if (getLangOptions().ObjCAutoRefCount &&
+          result == Compatible && 
+          origLhsType.getObjCLifetime() == Qualifiers::OCL_Weak) {
+        if (const ObjCObjectPointerType *ObjT = 
+              rhsType->getAs<ObjCObjectPointerType>())
+          if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable())
+            result = IncompatibleObjCWeakRef;
+      }
+      return result;
     }
 
     // int or null -> A*
@@ -8679,6 +8707,9 @@
   case IncompatibleVectors:
     DiagKind = diag::warn_incompatible_vectors;
     break;
+  case IncompatibleObjCWeakRef:
+    DiagKind = diag::err_arc_weak_unavailable_assign;
+    break;
   case Incompatible:
     DiagKind = diag::err_typecheck_convert_incompatible;
     isInvalid = true;

Modified: cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m?rev=134624&r1=134623&r2=134624&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m (original)
+++ cfe/trunk/test/SemaObjC/arc-unavailable-for-weakref.m Thu Jul  7 13:55:47 2011
@@ -14,5 +14,22 @@
 
   id obj;
 
-  ns1 = (__weak sub *)obj; // expected-error {{class is incompatible with __weak references}}
+  ns1 = (__weak sub *)obj; // expected-error {{assignment of a weak-unavailable object to a __weak object}} \
+                           // expected-error {{class is incompatible with __weak references}}
 }
+
+// rdar://9732636
+__attribute__((objc_arc_weak_reference_unavailable))
+ at interface NOWEAK
++ (id) new;
+ at end
+
+NOWEAK * Test9732636() {
+  NOWEAK * strong1 = [NOWEAK new];
+  __weak id weak1;
+  weak1 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
+
+  __weak id weak2 = strong1; // expected-error {{assignment of a weak-unavailable object to a __weak object}}
+  return (__weak id)strong1; // expected-error {{cast of weak-unavailable object of type 'NOWEAK *' to a __weak object of type '__weak id'}}
+}
+





More information about the cfe-commits mailing list