[cfe-commits] r143240 - in /cfe/trunk: lib/Sema/SemaType.cpp test/SemaObjC/arc-type-conversion.m

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Oct 28 15:54:28 PDT 2011


Author: akirtzidis
Date: Fri Oct 28 17:54:28 2011
New Revision: 143240

URL: http://llvm.org/viewvc/llvm-project?rev=143240&view=rev
Log:
[ARC] Do not transfer ARC ownership if the cast is going to result in r-value,
in which case the ownership is redundant. Thanks to John for the suggestion.

Modified:
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/SemaObjC/arc-type-conversion.m

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=143240&r1=143239&r2=143240&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Fri Oct 28 17:54:28 2011
@@ -2665,6 +2665,7 @@
   // TODO: mark whether we did this inference?
 }
 
+/// \brief Used for transfering ownership in casts resulting in l-values.
 static void transferARCOwnership(TypeProcessingState &state,
                                  QualType &declSpecTy,
                                  Qualifiers::ObjCLifetime ownership) {
@@ -2672,6 +2673,7 @@
   Declarator &D = state.getDeclarator();
 
   int inner = -1;
+  bool hasIndirection = false;
   for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
     DeclaratorChunk &chunk = D.getTypeObject(i);
     switch (chunk.Kind) {
@@ -2682,11 +2684,15 @@
     case DeclaratorChunk::Array:
     case DeclaratorChunk::Reference:
     case DeclaratorChunk::Pointer:
+      if (inner != -1)
+        hasIndirection = true;
       inner = i;
       break;
 
     case DeclaratorChunk::BlockPointer:
-      return transferARCOwnershipToDeclaratorChunk(state, ownership, i);
+      if (inner != -1)
+        transferARCOwnershipToDeclaratorChunk(state, ownership, i);
+      return;
 
     case DeclaratorChunk::Function:
     case DeclaratorChunk::MemberPointer:
@@ -2695,13 +2701,13 @@
   }
 
   if (inner == -1)
-    return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
+    return;
 
   DeclaratorChunk &chunk = D.getTypeObject(inner); 
   if (chunk.Kind == DeclaratorChunk::Pointer) {
     if (declSpecTy->isObjCRetainableType())
       return transferARCOwnershipToDeclSpec(S, declSpecTy, ownership);
-    if (declSpecTy->isObjCObjectType())
+    if (declSpecTy->isObjCObjectType() && hasIndirection)
       return transferARCOwnershipToDeclaratorChunk(state, ownership, inner);
   } else {
     assert(chunk.Kind == DeclaratorChunk::Array ||

Modified: cfe/trunk/test/SemaObjC/arc-type-conversion.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc-type-conversion.m?rev=143240&r1=143239&r2=143240&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/arc-type-conversion.m (original)
+++ cfe/trunk/test/SemaObjC/arc-type-conversion.m Fri Oct 28 17:54:28 2011
@@ -77,4 +77,12 @@
   Block_strong blk_strong1;
   Block_strong blk_strong2 = (Block)blk_strong1;
   Block_autoreleasing *blk_auto = (Block*)pblk;
+
+  id lv;
+  (void)(id)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'id'}}
+  (void)(id*)lv; // expected-error {{cast of an Objective-C pointer to '__strong id *'}}
+  (void)(NSString*)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'NSString *'}}
+  (void)(NSString**)lv; // expected-error {{cast of an Objective-C pointer to 'NSString *__strong *'}}
+  (void)(Block)&lv; // expected-error {{cast of an indirect pointer to an Objective-C pointer to 'Block'}}
+  (void)(Block*)lv; // expected-error {{cast of an Objective-C pointer to '__strong Block *'}}
 }





More information about the cfe-commits mailing list