From: Argyrios Kyrtzidis Date: Thu, 14 Jul 2011 23:32:04 +0000 (+0000) Subject: [arcmt] Don't remove retains/releases on a global variable, flag them with errors... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2c18ca0575b60082f2a9f4563b4071071960d37c;p=clang [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 --- 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(E)) - return DRE->getDecl()->getDeclContext()->isFileContext(); - if (ConditionalOperator *condOp = dyn_cast(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(E)) + return DRE->getDecl()->getDeclContext()->isFileContext(); + if (ConditionalOperator *condOp = dyn_cast(E)) + return isGlobalVar(condOp->getTrueExpr()) && + isGlobalVar(condOp->getFalseExpr()); + + return false; +} + namespace { class ReferenceClear : public RecursiveASTVisitor { 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 class BodyTransform : public RecursiveASTVisitor > { 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'}}