[cfe-commits] r143976 - in /cfe/trunk: lib/ARCMigrate/TransGCAttrs.cpp lib/ARCMigrate/Transforms.cpp lib/ARCMigrate/Transforms.h test/ARCMT/GC.m test/ARCMT/GC.m.result

Argyrios Kyrtzidis akyrtzi at gmail.com
Mon Nov 7 10:40:29 PST 2011


Author: akirtzidis
Date: Mon Nov  7 12:40:29 2011
New Revision: 143976

URL: http://llvm.org/viewvc/llvm-project?rev=143976&view=rev
Log:
[arcmt] In GC, change '__weak' -> '__unsafe_unretained' when applied
to objects of classes that don't support ARC weak

Modified:
    cfe/trunk/lib/ARCMigrate/TransGCAttrs.cpp
    cfe/trunk/lib/ARCMigrate/Transforms.cpp
    cfe/trunk/lib/ARCMigrate/Transforms.h
    cfe/trunk/test/ARCMT/GC.m
    cfe/trunk/test/ARCMT/GC.m.result

Modified: cfe/trunk/lib/ARCMigrate/TransGCAttrs.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/TransGCAttrs.cpp?rev=143976&r1=143975&r2=143976&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/TransGCAttrs.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/TransGCAttrs.cpp Mon Nov  7 12:40:29 2011
@@ -12,6 +12,7 @@
 #include "clang/Lex/Lexer.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Analysis/Support/SaveAndRestore.h"
+#include "clang/Sema/SemaDiagnostic.h"
 
 using namespace clang;
 using namespace arcmt;
@@ -81,6 +82,8 @@
 
     ASTContext &Ctx = MigrateCtx.Pass.Ctx;
     SourceManager &SM = Ctx.getSourceManager();
+    if (Loc.isMacroID())
+      Loc = SM.getImmediateExpansionRange(Loc).first;
     llvm::SmallString<32> Buf;
     bool Invalid = false;
     StringRef Spell = Lexer::getSpelling(
@@ -101,7 +104,7 @@
     MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs.back();
 
     Attr.Kind = Kind;
-    Attr.Loc = SM.getImmediateExpansionRange(Loc).first;
+    Attr.Loc = Loc;
     Attr.ModifiedType = TL.getModifiedLoc().getType();
     Attr.Dcl = D;
     Attr.FullyMigratable = FullyMigratable;
@@ -202,12 +205,35 @@
   }
 }
 
+static void checkWeakGCAttrs(MigrationContext &MigrateCtx) {
+  TransformActions &TA = MigrateCtx.Pass.TA;
+
+  for (unsigned i = 0, e = MigrateCtx.GCAttrs.size(); i != e; ++i) {
+    MigrationContext::GCAttrOccurrence &Attr = MigrateCtx.GCAttrs[i];
+    if (Attr.Kind == MigrationContext::GCAttrOccurrence::Weak &&
+        Attr.FullyMigratable) {
+      if (Attr.ModifiedType.isNull() ||
+          !Attr.ModifiedType->isObjCRetainableType())
+        continue;
+      if (!canApplyWeak(MigrateCtx.Pass.Ctx, Attr.ModifiedType,
+                        /*AllowOnUnknownClass=*/true)) {
+        Transaction Trans(TA);
+        TA.replaceText(Attr.Loc, "__weak", "__unsafe_unretained");
+        TA.clearDiagnostic(diag::err_arc_weak_no_runtime,
+                           diag::err_arc_unsupported_weak_class,
+                           Attr.Loc);
+      }
+    }
+  }
+}
+
 void GCAttrsTraverser::traverseTU(MigrationContext &MigrateCtx) {
   GCAttrsCollector(MigrateCtx).TraverseDecl(
                                   MigrateCtx.Pass.Ctx.getTranslationUnitDecl());
 
   clearRedundantStrongs(MigrateCtx);
   errorForGCAttrsOnNonObjC(MigrateCtx);
+  checkWeakGCAttrs(MigrateCtx);
 }
 
 void MigrationContext::dumpGCAttrs() {

Modified: cfe/trunk/lib/ARCMigrate/Transforms.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.cpp?rev=143976&r1=143975&r2=143976&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.cpp Mon Nov  7 12:40:29 2011
@@ -64,7 +64,8 @@
   return isClassInWeakBlacklist(cls->getSuperClass());
 }
 
-bool trans::canApplyWeak(ASTContext &Ctx, QualType type) {
+bool trans::canApplyWeak(ASTContext &Ctx, QualType type,
+                         bool AllowOnUnknownClass) {
   if (!Ctx.getLangOptions().ObjCRuntimeHasWeak)
     return false;
 
@@ -73,9 +74,9 @@
     T = ptr->getPointeeType();
   if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
     ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
-    if (!Class || Class->getName() == "NSObject")
+    if (!AllowOnUnknownClass && (!Class || Class->getName() == "NSObject"))
       return false; // id/NSObject is not safe for weak.
-    if (Class->isForwardDecl())
+    if (!AllowOnUnknownClass && Class->isForwardDecl())
       return false; // forward classes are not verifiable, therefore not safe.
     if (Class->isArcWeakrefUnavailable())
       return false;

Modified: cfe/trunk/lib/ARCMigrate/Transforms.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/Transforms.h?rev=143976&r1=143975&r2=143976&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/Transforms.h (original)
+++ cfe/trunk/lib/ARCMigrate/Transforms.h Mon Nov  7 12:40:29 2011
@@ -137,7 +137,8 @@
 //===----------------------------------------------------------------------===//
 
 /// \brief Determine whether we can add weak to the given type.
-bool canApplyWeak(ASTContext &Ctx, QualType type);
+bool canApplyWeak(ASTContext &Ctx, QualType type,
+                  bool AllowOnUnknownClass = false);
 
 /// \brief 'Loc' is the end of a statement range. This returns the location
 /// immediately after the semicolon following the statement.

Modified: cfe/trunk/test/ARCMT/GC.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/GC.m?rev=143976&r1=143975&r2=143976&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/GC.m (original)
+++ cfe/trunk/test/ARCMT/GC.m Mon Nov  7 12:40:29 2011
@@ -1,7 +1,7 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-arc -x objective-c %s.result
-// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-gc-only -x objective-c %s > %t
+// RUN: %clang_cc1 -triple x86_64-apple-macosx10.7 -fsyntax-only -fobjc-runtime-has-weak -fobjc-arc -x objective-c %s.result
+// RUN: arcmt-test --args -triple x86_64-apple-macosx10.7 -fsyntax-only -fobjc-gc-only -x objective-c %s > %t
 // RUN: diff %t %s.result
-// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-gc-only -x objective-c++ %s > %t
+// RUN: arcmt-test --args -triple x86_64-apple-macosx10.7 -fsyntax-only -fobjc-gc-only -x objective-c++ %s > %t
 // RUN: diff %t %s.result
 
 #include "Common.h"
@@ -41,3 +41,10 @@
   test1(0);
 }
 @end
+
+__attribute__((objc_arc_weak_reference_unavailable))
+ at interface QQ {
+  __weak id s;
+  __weak QQ *q;
+}
+ at end

Modified: cfe/trunk/test/ARCMT/GC.m.result
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ARCMT/GC.m.result?rev=143976&r1=143975&r2=143976&view=diff
==============================================================================
--- cfe/trunk/test/ARCMT/GC.m.result (original)
+++ cfe/trunk/test/ARCMT/GC.m.result Mon Nov  7 12:40:29 2011
@@ -1,7 +1,7 @@
-// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-arc -x objective-c %s.result
-// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-gc-only -x objective-c %s > %t
+// RUN: %clang_cc1 -triple x86_64-apple-macosx10.7 -fsyntax-only -fobjc-runtime-has-weak -fobjc-arc -x objective-c %s.result
+// RUN: arcmt-test --args -triple x86_64-apple-macosx10.7 -fsyntax-only -fobjc-gc-only -x objective-c %s > %t
 // RUN: diff %t %s.result
-// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fsyntax-only -fobjc-gc-only -x objective-c++ %s > %t
+// RUN: arcmt-test --args -triple x86_64-apple-macosx10.7 -fsyntax-only -fobjc-gc-only -x objective-c++ %s > %t
 // RUN: diff %t %s.result
 
 #include "Common.h"
@@ -36,3 +36,10 @@
   test1(0);
 }
 @end
+
+__attribute__((objc_arc_weak_reference_unavailable))
+ at interface QQ {
+  __weak id s;
+  __unsafe_unretained QQ *q;
+}
+ at end





More information about the cfe-commits mailing list