[arcmt] Before applying '__weak' check whether the objc class is annotated with objc_arc_weak_reference_unavailable
or is in a list of classes not supporting 'weak'.
rdar://9489367.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135002 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/ARCMigrate/TransBlockObjCVariable.cpp b/lib/ARCMigrate/TransBlockObjCVariable.cpp
index 97695cb..0e342b7 100644
--- a/lib/ARCMigrate/TransBlockObjCVariable.cpp
+++ b/lib/ARCMigrate/TransBlockObjCVariable.cpp
@@ -98,12 +98,12 @@
BlocksAttr *attr = var->getAttr<BlocksAttr>();
if(!attr)
continue;
- bool hasWeak = Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak;
+ bool useWeak = canApplyWeak(Pass.Ctx, var->getType());
SourceManager &SM = Pass.Ctx.getSourceManager();
Transaction Trans(Pass.TA);
Pass.TA.replaceText(SM.getInstantiationLoc(attr->getLocation()),
"__block",
- hasWeak ? "__weak" : "__unsafe_unretained");
+ useWeak ? "__weak" : "__unsafe_unretained");
}
}
diff --git a/lib/ARCMigrate/TransProperties.cpp b/lib/ARCMigrate/TransProperties.cpp
index 0efc4c0..82ef717 100644
--- a/lib/ARCMigrate/TransProperties.cpp
+++ b/lib/ARCMigrate/TransProperties.cpp
@@ -112,7 +112,7 @@
}
void applyWeak(PropData &prop) {
- assert(Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak);
+ assert(canApplyWeak(Pass.Ctx, prop.IvarD->getType()));
Transaction Trans(Pass.TA);
Pass.TA.insert(prop.IvarD->getLocation(), "__weak ");
@@ -157,7 +157,7 @@
// There is a "error: existing ivar for assign property must be
// __unsafe_unretained"; fix it.
- if (!Pass.Ctx.getLangOptions().ObjCRuntimeHasWeak) {
+ if (!canApplyWeak(Pass.Ctx, ivarD->getType())) {
// We will just add __unsafe_unretained to the ivar.
Transaction Trans(Pass.TA);
Pass.TA.insert(ivarD->getLocation(), "__unsafe_unretained ");
diff --git a/lib/ARCMigrate/Transforms.cpp b/lib/ARCMigrate/Transforms.cpp
index 5468291..c2f85f6 100644
--- a/lib/ARCMigrate/Transforms.cpp
+++ b/lib/ARCMigrate/Transforms.cpp
@@ -29,6 +29,61 @@
// Helpers.
//===----------------------------------------------------------------------===//
+/// \brief True if the class is one that does not support weak.
+static bool isClassInWeakBlacklist(ObjCInterfaceDecl *cls) {
+ if (!cls)
+ return false;
+
+ bool inList = llvm::StringSwitch<bool>(cls->getName())
+ .Case("NSColorSpace", true)
+ .Case("NSFont", true)
+ .Case("NSFontPanel", true)
+ .Case("NSImage", true)
+ .Case("NSLazyBrowserCell", true)
+ .Case("NSWindow", true)
+ .Case("NSWindowController", true)
+ .Case("NSMenuView", true)
+ .Case("NSPersistentUIWindowInfo", true)
+ .Case("NSTableCellView", true)
+ .Case("NSATSTypeSetter", true)
+ .Case("NSATSGlyphStorage", true)
+ .Case("NSLineFragmentRenderingContext", true)
+ .Case("NSAttributeDictionary", true)
+ .Case("NSParagraphStyle", true)
+ .Case("NSTextTab", true)
+ .Case("NSSimpleHorizontalTypesetter", true)
+ .Case("_NSCachedAttributedString", true)
+ .Case("NSStringDrawingTextStorage", true)
+ .Case("NSTextView", true)
+ .Case("NSSubTextStorage", true)
+ .Default(false);
+
+ if (inList)
+ return true;
+
+ return isClassInWeakBlacklist(cls->getSuperClass());
+}
+
+bool trans::canApplyWeak(ASTContext &Ctx, QualType type) {
+ if (!Ctx.getLangOptions().ObjCRuntimeHasWeak)
+ return false;
+
+ QualType T = type;
+ while (const PointerType *ptr = T->getAs<PointerType>())
+ T = ptr->getPointeeType();
+ if (const ObjCObjectPointerType *ObjT = T->getAs<ObjCObjectPointerType>()) {
+ ObjCInterfaceDecl *Class = ObjT->getInterfaceDecl();
+ if (!Class || Class->getName() == "NSObject")
+ return false; // id/NSObject is not safe for weak.
+ if (Class->isArcWeakrefUnavailable())
+ return false;
+ if (isClassInWeakBlacklist(Class))
+ return false;
+ }
+
+ return true;
+}
+
/// \brief 'Loc' is the end of a statement range. This returns the location
/// immediately after the semicolon following the statement.
/// If no semicolon is found or the location is inside a macro, the returned
diff --git a/lib/ARCMigrate/Transforms.h b/lib/ARCMigrate/Transforms.h
index 2106497..d52fba0 100644
--- a/lib/ARCMigrate/Transforms.h
+++ b/lib/ARCMigrate/Transforms.h
@@ -44,6 +44,9 @@
// Helpers.
//===----------------------------------------------------------------------===//
+/// \brief Determine whether we can add weak to the given type.
+bool canApplyWeak(ASTContext &Ctx, QualType type);
+
/// \brief 'Loc' is the end of a statement range. This returns the location
/// immediately after the semicolon following the statement.
/// If no semicolon is found or the location is inside a macro, the returned