aboutsummaryrefslogtreecommitdiff
path: root/lib/ARCMigrate
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2011-11-07 18:40:29 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2011-11-07 18:40:29 +0000
commit12192cf50a96cb59a3039af044b7fa97f043101c (patch)
treeddce98ab75856767690d0af5db9fc4e2d24a60f3 /lib/ARCMigrate
parent5cad82236ecc0a2eeed2edd75e119f6069a99f4c (diff)
[arcmt] In GC, change '__weak' -> '__unsafe_unretained' when applied
to objects of classes that don't support ARC weak git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@143976 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ARCMigrate')
-rw-r--r--lib/ARCMigrate/TransGCAttrs.cpp28
-rw-r--r--lib/ARCMigrate/Transforms.cpp7
-rw-r--r--lib/ARCMigrate/Transforms.h3
3 files changed, 33 insertions, 5 deletions
diff --git a/lib/ARCMigrate/TransGCAttrs.cpp b/lib/ARCMigrate/TransGCAttrs.cpp
index 79dc0538b6..94eb8781df 100644
--- a/lib/ARCMigrate/TransGCAttrs.cpp
+++ b/lib/ARCMigrate/TransGCAttrs.cpp
@@ -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 @@ public:
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 @@ public:
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 errorForGCAttrsOnNonObjC(MigrationContext &MigrateCtx) {
}
}
+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() {
diff --git a/lib/ARCMigrate/Transforms.cpp b/lib/ARCMigrate/Transforms.cpp
index c82f075bca..a736c2419e 100644
--- a/lib/ARCMigrate/Transforms.cpp
+++ b/lib/ARCMigrate/Transforms.cpp
@@ -64,7 +64,8 @@ static bool isClassInWeakBlacklist(ObjCInterfaceDecl *cls) {
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 @@ bool trans::canApplyWeak(ASTContext &Ctx, QualType type) {
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;
diff --git a/lib/ARCMigrate/Transforms.h b/lib/ARCMigrate/Transforms.h
index 3f1e737e23..7960a7df2e 100644
--- a/lib/ARCMigrate/Transforms.h
+++ b/lib/ARCMigrate/Transforms.h
@@ -137,7 +137,8 @@ public:
//===----------------------------------------------------------------------===//
/// \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.