[cfe-commits] r143747 - in /cfe/trunk: lib/ARCMigrate/TransGCCalls.cpp lib/ARCMigrate/TransformActions.cpp lib/ARCMigrate/Transforms.cpp lib/ARCMigrate/Transforms.h test/ARCMT/GC-check.m

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Nov 4 16:43:03 PDT 2011


Author: akirtzidis
Date: Fri Nov  4 18:43:03 2011
New Revision: 143747

URL: http://llvm.org/viewvc/llvm-project?rev=143747&view=rev
Log:
[arcmt] In GC, error out when there is a call that returns a pointer to
GC managed non-objc object memory.

Modified:
    cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp
    cfe/trunk/lib/ARCMigrate/TransformActions.cpp
    cfe/trunk/lib/ARCMigrate/Transforms.cpp
    cfe/trunk/lib/ARCMigrate/Transforms.h
    cfe/trunk/test/ARCMT/GC-check.m

Modified: cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp?rev=143747&r1=143746&r2=143747&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransGCCalls.cpp Fri Nov  4 18:43:03 2011
@@ -35,6 +35,13 @@
   bool VisitCallExpr(CallExpr *E) {
     TransformActions &TA = MigrateCtx.getPass().TA;
 
+    if (MigrateCtx.isGCOwnedNonObjC(E->getType())) {
+      TA.reportError("call returns pointer to GC managed memory; "
+                     "it will become unmanaged in ARC",
+                     E->getLocStart(), E->getSourceRange());
+      return true;
+    }
+
     Expr *CEE = E->getCallee()->IgnoreParenImpCasts();
     if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) {
       if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DRE->getDecl())) {

Modified: cfe/trunk/lib/ARCMigrate/TransformActions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransformActions.cpp?rev=143747&r1=143746&r2=143747&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransformActions.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransformActions.cpp Fri Nov  4 18:43:03 2011
@@ -122,6 +122,8 @@
                        ASTContext &ctx, Preprocessor &PP)
     : CapturedDiags(capturedDiags), Ctx(ctx), PP(PP), IsInTransaction(false) { }
 
+  ASTContext &getASTContext() { return Ctx; }
+
   void startTransaction();
   bool commitTransaction();
   void abortTransaction();
@@ -674,6 +676,12 @@
                                    SourceRange range) {
   assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() &&
          "Errors should be emitted out of a transaction");
+
+  SourceManager &SM = static_cast<TransformActionsImpl*>(Impl)->
+                                             getASTContext().getSourceManager();
+  if (SM.isInSystemHeader(SM.getExpansionLoc(loc)))
+    return;
+
   // FIXME: Use a custom category name to distinguish rewriter errors.
   std::string rewriteErr = "[rewriter] ";
   rewriteErr += error;
@@ -688,6 +696,12 @@
                                   SourceRange range) {
   assert(!static_cast<TransformActionsImpl*>(Impl)->isInTransaction() &&
          "Errors should be emitted out of a transaction");
+
+  SourceManager &SM = static_cast<TransformActionsImpl*>(Impl)->
+                                             getASTContext().getSourceManager();
+  if (SM.isInSystemHeader(SM.getExpansionLoc(loc)))
+    return;
+
   // FIXME: Use a custom category name to distinguish rewriter errors.
   std::string rewriteNote = "[rewriter] ";
   rewriteNote += note;

Modified: cfe/trunk/lib/ARCMigrate/Transforms.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.cpp?rev=143747&r1=143746&r2=143747&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.cpp Fri Nov  4 18:43:03 2011
@@ -324,6 +324,26 @@
     delete *I;
 }
 
+bool MigrationContext::isGCOwnedNonObjC(QualType T) {
+  while (!T.isNull()) {
+    if (const AttributedType *AttrT = T->getAs<AttributedType>()) {
+      if (AttrT->getAttrKind() == AttributedType::attr_objc_ownership)
+        return !AttrT->getModifiedType()->isObjCRetainableType();
+    }
+
+    if (T->isArrayType())
+      T = Pass.Ctx.getBaseElementType(T);
+    else if (const PointerType *PT = T->getAs<PointerType>())
+      T = PT->getPointeeType();
+    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
+      T = RT->getPointeeType();
+    else
+      break;
+  }
+
+  return false;
+}
+
 void MigrationContext::traverse(TranslationUnitDecl *TU) {
   ASTTransform(*this).TraverseDecl(TU);
 }

Modified: cfe/trunk/lib/ARCMigrate/Transforms.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.h?rev=143747&r1=143746&r2=143747&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.h (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.h Fri Nov  4 18:43:03 2011
@@ -82,6 +82,8 @@
     Traversers.push_back(traverser);
   }
 
+  bool isGCOwnedNonObjC(QualType T);
+
   void traverse(TranslationUnitDecl *TU);
 };
 

Modified: cfe/trunk/test/ARCMT/GC-check.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/GC-check.m?rev=143747&r1=143746&r2=143747&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/GC-check.m (original)
+++ cfe/trunk/test/ARCMT/GC-check.m Fri Nov  4 18:43:03 2011
@@ -2,10 +2,13 @@
 // RUN: %clang_cc1 -arcmt-check -verify -triple x86_64-apple-darwin10 -fobjc-gc-only -x objective-c++ %s
 
 #define CF_AUTOMATED_REFCOUNT_UNAVAILABLE __attribute__((unavailable("not available in automatic reference counting mode")))
+typedef unsigned NSUInteger;
 typedef const void * CFTypeRef;
 CFTypeRef CFMakeCollectable(CFTypeRef cf) CF_AUTOMATED_REFCOUNT_UNAVAILABLE; // expected-note {{unavailable}}
+void *__strong NSAllocateCollectable(NSUInteger size, NSUInteger options);
 
 void test1(CFTypeRef *cft) {
   CFTypeRef c = CFMakeCollectable(cft); // expected-error {{CFMakeCollectable will leak the object that it receives in ARC}} \
                 // expected-error {{unavailable}}
+  NSAllocateCollectable(100, 0); // expected-error {{call returns pointer to GC managed memory; it will become unmanaged in ARC}}
 }





More information about the cfe-commits mailing list