aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>2011-07-14 23:32:04 +0000
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>2011-07-14 23:32:04 +0000
commit2c18ca0575b60082f2a9f4563b4071071960d37c (patch)
treecc646d6f0227a762ba6c84fbfd7060b8068f3780
parent12ce0a085f89f07c76bf034aa6b838ef50542241 (diff)
[arcmt] Don't remove retains/releases on a global variable, flag them with errors. rdar://9402555.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135213 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/ARCMigrate/TransRetainReleaseDealloc.cpp9
-rw-r--r--lib/ARCMigrate/TransUnbridgedCasts.cpp11
-rw-r--r--lib/ARCMigrate/Transforms.cpp11
-rw-r--r--lib/ARCMigrate/Transforms.h2
-rw-r--r--test/ARCMT/checking.m6
5 files changed, 28 insertions, 11 deletions
diff --git a/lib/ARCMigrate/TransRetainReleaseDealloc.cpp b/lib/ARCMigrate/TransRetainReleaseDealloc.cpp
index 4eeb47437e..c72312b260 100644
--- a/lib/ARCMigrate/TransRetainReleaseDealloc.cpp
+++ b/lib/ARCMigrate/TransRetainReleaseDealloc.cpp
@@ -78,6 +78,15 @@ public:
Pass.TA.reportError(err, rec->getLocStart());
return true;
}
+
+ if (isGlobalVar(rec) &&
+ (E->getMethodFamily() != OMF_retain || isRemovable(E))) {
+ std::string err = "it is not safe to remove '";
+ err += E->getSelector().getAsString() + "' message on "
+ "a global variable";
+ Pass.TA.reportError(err, rec->getLocStart());
+ return true;
+ }
}
case OMF_dealloc:
break;
diff --git a/lib/ARCMigrate/TransUnbridgedCasts.cpp b/lib/ARCMigrate/TransUnbridgedCasts.cpp
index 6513d98c92..1cacd6d84e 100644
--- a/lib/ARCMigrate/TransUnbridgedCasts.cpp
+++ b/lib/ARCMigrate/TransUnbridgedCasts.cpp
@@ -193,17 +193,6 @@ private:
return true;
return false;
}
-
- static bool isGlobalVar(Expr *E) {
- E = E->IgnoreParenCasts();
- if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
- return DRE->getDecl()->getDeclContext()->isFileContext();
- if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
- return isGlobalVar(condOp->getTrueExpr()) &&
- isGlobalVar(condOp->getFalseExpr());
-
- return false;
- }
};
} // end anonymous namespace
diff --git a/lib/ARCMigrate/Transforms.cpp b/lib/ARCMigrate/Transforms.cpp
index 7a439d95e3..7bd95e54bc 100644
--- a/lib/ARCMigrate/Transforms.cpp
+++ b/lib/ARCMigrate/Transforms.cpp
@@ -152,6 +152,17 @@ bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {
return true;
}
+bool trans::isGlobalVar(Expr *E) {
+ E = E->IgnoreParenCasts();
+ if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
+ return DRE->getDecl()->getDeclContext()->isFileContext();
+ if (ConditionalOperator *condOp = dyn_cast<ConditionalOperator>(E))
+ return isGlobalVar(condOp->getTrueExpr()) &&
+ isGlobalVar(condOp->getFalseExpr());
+
+ return false;
+}
+
namespace {
class ReferenceClear : public RecursiveASTVisitor<ReferenceClear> {
diff --git a/lib/ARCMigrate/Transforms.h b/lib/ARCMigrate/Transforms.h
index 00ff733f47..b47d6d8e9b 100644
--- a/lib/ARCMigrate/Transforms.h
+++ b/lib/ARCMigrate/Transforms.h
@@ -54,6 +54,8 @@ bool canApplyWeak(ASTContext &Ctx, QualType type);
SourceLocation findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx);
bool hasSideEffects(Expr *E, ASTContext &Ctx);
+bool isGlobalVar(Expr *E);
+
template <typename BODY_TRANS>
class BodyTransform : public RecursiveASTVisitor<BodyTransform<BODY_TRANS> > {
diff --git a/test/ARCMT/checking.m b/test/ARCMT/checking.m
index 7ab54ceb05..2e4ec122d6 100644
--- a/test/ARCMT/checking.m
+++ b/test/ARCMT/checking.m
@@ -36,10 +36,16 @@ struct UnsafeS {
- (oneway void)release { } // expected-error {{ARC forbids implementation}}
@end
+id global_foo;
+
void test1(A *a, BOOL b, struct UnsafeS *unsafeS) {
[unsafeS->unsafeObj retain]; // expected-error {{it is not safe to remove 'retain' message on an __unsafe_unretained type}} \
// expected-error {{ARC forbids explicit message send}}
id foo = [unsafeS->unsafeObj retain]; // no warning.
+ [global_foo retain]; // expected-error {{it is not safe to remove 'retain' message on a global variable}} \
+ // expected-error {{ARC forbids explicit message send}}
+ [global_foo release]; // expected-error {{it is not safe to remove 'release' message on a global variable}} \
+ // expected-error {{ARC forbids explicit message send}}
[a dealloc];
[a retain];
[a retainCount]; // expected-error {{ARC forbids explicit message send of 'retainCount'}}