CheckObjCDealloc.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //==- CheckObjCDealloc.cpp - Check ObjC -dealloc implementation --*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines a CheckObjCDealloc, a checker that
  11. // analyzes an Objective-C class's implementation to determine if it
  12. // correctly implements -dealloc.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "ClangSACheckers.h"
  16. #include "clang/StaticAnalyzer/Core/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  18. #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
  19. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  20. #include "clang/AST/Attr.h"
  21. #include "clang/AST/DeclObjC.h"
  22. #include "clang/AST/Expr.h"
  23. #include "clang/AST/ExprObjC.h"
  24. #include "clang/Basic/LangOptions.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. using namespace clang;
  27. using namespace ento;
  28. static bool scan_dealloc(Stmt *S, Selector Dealloc) {
  29. if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
  30. if (ME->getSelector() == Dealloc) {
  31. switch (ME->getReceiverKind()) {
  32. case ObjCMessageExpr::Instance: return false;
  33. case ObjCMessageExpr::SuperInstance: return true;
  34. case ObjCMessageExpr::Class: break;
  35. case ObjCMessageExpr::SuperClass: break;
  36. }
  37. }
  38. // Recurse to children.
  39. for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I)
  40. if (*I && scan_dealloc(*I, Dealloc))
  41. return true;
  42. return false;
  43. }
  44. static bool scan_ivar_release(Stmt *S, ObjCIvarDecl *ID,
  45. const ObjCPropertyDecl *PD,
  46. Selector Release,
  47. IdentifierInfo* SelfII,
  48. ASTContext &Ctx) {
  49. // [mMyIvar release]
  50. if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
  51. if (ME->getSelector() == Release)
  52. if (ME->getInstanceReceiver())
  53. if (Expr *Receiver = ME->getInstanceReceiver()->IgnoreParenCasts())
  54. if (ObjCIvarRefExpr *E = dyn_cast<ObjCIvarRefExpr>(Receiver))
  55. if (E->getDecl() == ID)
  56. return true;
  57. // [self setMyIvar:nil];
  58. if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))
  59. if (ME->getInstanceReceiver())
  60. if (Expr *Receiver = ME->getInstanceReceiver()->IgnoreParenCasts())
  61. if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(Receiver))
  62. if (E->getDecl()->getIdentifier() == SelfII)
  63. if (ME->getMethodDecl() == PD->getSetterMethodDecl() &&
  64. ME->getNumArgs() == 1 &&
  65. ME->getArg(0)->isNullPointerConstant(Ctx,
  66. Expr::NPC_ValueDependentIsNull))
  67. return true;
  68. // self.myIvar = nil;
  69. if (BinaryOperator* BO = dyn_cast<BinaryOperator>(S))
  70. if (BO->isAssignmentOp())
  71. if (ObjCPropertyRefExpr *PRE =
  72. dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParenCasts()))
  73. if (PRE->isExplicitProperty() && PRE->getExplicitProperty() == PD)
  74. if (BO->getRHS()->isNullPointerConstant(Ctx,
  75. Expr::NPC_ValueDependentIsNull)) {
  76. // This is only a 'release' if the property kind is not
  77. // 'assign'.
  78. return PD->getSetterKind() != ObjCPropertyDecl::Assign;
  79. }
  80. // Recurse to children.
  81. for (Stmt::child_iterator I = S->child_begin(), E= S->child_end(); I!=E; ++I)
  82. if (*I && scan_ivar_release(*I, ID, PD, Release, SelfII, Ctx))
  83. return true;
  84. return false;
  85. }
  86. static void checkObjCDealloc(const ObjCImplementationDecl *D,
  87. const LangOptions& LOpts, BugReporter& BR) {
  88. assert (LOpts.getGC() != LangOptions::GCOnly);
  89. ASTContext &Ctx = BR.getContext();
  90. const ObjCInterfaceDecl *ID = D->getClassInterface();
  91. // Does the class contain any ivars that are pointers (or id<...>)?
  92. // If not, skip the check entirely.
  93. // NOTE: This is motivated by PR 2517:
  94. // http://llvm.org/bugs/show_bug.cgi?id=2517
  95. bool containsPointerIvar = false;
  96. for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();
  97. I!=E; ++I) {
  98. ObjCIvarDecl *ID = *I;
  99. QualType T = ID->getType();
  100. if (!T->isObjCObjectPointerType() ||
  101. ID->getAttr<IBOutletAttr>() || // Skip IBOutlets.
  102. ID->getAttr<IBOutletCollectionAttr>()) // Skip IBOutletCollections.
  103. continue;
  104. containsPointerIvar = true;
  105. break;
  106. }
  107. if (!containsPointerIvar)
  108. return;
  109. // Determine if the class subclasses NSObject.
  110. IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
  111. IdentifierInfo* SenTestCaseII = &Ctx.Idents.get("SenTestCase");
  112. for ( ; ID ; ID = ID->getSuperClass()) {
  113. IdentifierInfo *II = ID->getIdentifier();
  114. if (II == NSObjectII)
  115. break;
  116. // FIXME: For now, ignore classes that subclass SenTestCase, as these don't
  117. // need to implement -dealloc. They implement tear down in another way,
  118. // which we should try and catch later.
  119. // http://llvm.org/bugs/show_bug.cgi?id=3187
  120. if (II == SenTestCaseII)
  121. return;
  122. }
  123. if (!ID)
  124. return;
  125. // Get the "dealloc" selector.
  126. IdentifierInfo* II = &Ctx.Idents.get("dealloc");
  127. Selector S = Ctx.Selectors.getSelector(0, &II);
  128. ObjCMethodDecl *MD = 0;
  129. // Scan the instance methods for "dealloc".
  130. for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(),
  131. E = D->instmeth_end(); I!=E; ++I) {
  132. if ((*I)->getSelector() == S) {
  133. MD = *I;
  134. break;
  135. }
  136. }
  137. PathDiagnosticLocation DLoc =
  138. PathDiagnosticLocation::createBegin(D, BR.getSourceManager());
  139. if (!MD) { // No dealloc found.
  140. const char* name = LOpts.getGC() == LangOptions::NonGC
  141. ? "missing -dealloc"
  142. : "missing -dealloc (Hybrid MM, non-GC)";
  143. std::string buf;
  144. llvm::raw_string_ostream os(buf);
  145. os << "Objective-C class '" << *D << "' lacks a 'dealloc' instance method";
  146. BR.EmitBasicReport(D, name, categories::CoreFoundationObjectiveC,
  147. os.str(), DLoc);
  148. return;
  149. }
  150. // dealloc found. Scan for missing [super dealloc].
  151. if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) {
  152. const char* name = LOpts.getGC() == LangOptions::NonGC
  153. ? "missing [super dealloc]"
  154. : "missing [super dealloc] (Hybrid MM, non-GC)";
  155. std::string buf;
  156. llvm::raw_string_ostream os(buf);
  157. os << "The 'dealloc' instance method in Objective-C class '" << *D
  158. << "' does not send a 'dealloc' message to its super class"
  159. " (missing [super dealloc])";
  160. BR.EmitBasicReport(MD, name, categories::CoreFoundationObjectiveC,
  161. os.str(), DLoc);
  162. return;
  163. }
  164. // Get the "release" selector.
  165. IdentifierInfo* RII = &Ctx.Idents.get("release");
  166. Selector RS = Ctx.Selectors.getSelector(0, &RII);
  167. // Get the "self" identifier
  168. IdentifierInfo* SelfII = &Ctx.Idents.get("self");
  169. // Scan for missing and extra releases of ivars used by implementations
  170. // of synthesized properties
  171. for (ObjCImplementationDecl::propimpl_iterator I = D->propimpl_begin(),
  172. E = D->propimpl_end(); I!=E; ++I) {
  173. // We can only check the synthesized properties
  174. if (I->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)
  175. continue;
  176. ObjCIvarDecl *ID = I->getPropertyIvarDecl();
  177. if (!ID)
  178. continue;
  179. QualType T = ID->getType();
  180. if (!T->isObjCObjectPointerType()) // Skip non-pointer ivars
  181. continue;
  182. const ObjCPropertyDecl *PD = I->getPropertyDecl();
  183. if (!PD)
  184. continue;
  185. // ivars cannot be set via read-only properties, so we'll skip them
  186. if (PD->isReadOnly())
  187. continue;
  188. // ivar must be released if and only if the kind of setter was not 'assign'
  189. bool requiresRelease = PD->getSetterKind() != ObjCPropertyDecl::Assign;
  190. if (scan_ivar_release(MD->getBody(), ID, PD, RS, SelfII, Ctx)
  191. != requiresRelease) {
  192. const char *name = 0;
  193. std::string buf;
  194. llvm::raw_string_ostream os(buf);
  195. if (requiresRelease) {
  196. name = LOpts.getGC() == LangOptions::NonGC
  197. ? "missing ivar release (leak)"
  198. : "missing ivar release (Hybrid MM, non-GC)";
  199. os << "The '" << *ID
  200. << "' instance variable was retained by a synthesized property but "
  201. "wasn't released in 'dealloc'";
  202. } else {
  203. name = LOpts.getGC() == LangOptions::NonGC
  204. ? "extra ivar release (use-after-release)"
  205. : "extra ivar release (Hybrid MM, non-GC)";
  206. os << "The '" << *ID
  207. << "' instance variable was not retained by a synthesized property "
  208. "but was released in 'dealloc'";
  209. }
  210. PathDiagnosticLocation SDLoc =
  211. PathDiagnosticLocation::createBegin(*I, BR.getSourceManager());
  212. BR.EmitBasicReport(MD, name, categories::CoreFoundationObjectiveC,
  213. os.str(), SDLoc);
  214. }
  215. }
  216. }
  217. //===----------------------------------------------------------------------===//
  218. // ObjCDeallocChecker
  219. //===----------------------------------------------------------------------===//
  220. namespace {
  221. class ObjCDeallocChecker : public Checker<
  222. check::ASTDecl<ObjCImplementationDecl> > {
  223. public:
  224. void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,
  225. BugReporter &BR) const {
  226. if (mgr.getLangOpts().getGC() == LangOptions::GCOnly)
  227. return;
  228. checkObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOpts(), BR);
  229. }
  230. };
  231. }
  232. void ento::registerObjCDeallocChecker(CheckerManager &mgr) {
  233. mgr.registerChecker<ObjCDeallocChecker>();
  234. }