diff options
Diffstat (limited to 'lib/StaticAnalyzer/Checkers/CheckObjCDealloc.cpp')
| -rw-r--r-- | lib/StaticAnalyzer/Checkers/CheckObjCDealloc.cpp | 68 | 
1 files changed, 37 insertions, 31 deletions
diff --git a/lib/StaticAnalyzer/Checkers/CheckObjCDealloc.cpp b/lib/StaticAnalyzer/Checkers/CheckObjCDealloc.cpp index 0c693a0bd1b6..c325bb1517f3 100644 --- a/lib/StaticAnalyzer/Checkers/CheckObjCDealloc.cpp +++ b/lib/StaticAnalyzer/Checkers/CheckObjCDealloc.cpp @@ -27,9 +27,9 @@  using namespace clang;  using namespace ento; -static bool scan_dealloc(Stmt* S, Selector Dealloc) { +static bool scan_dealloc(Stmt *S, Selector Dealloc) { -  if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) +  if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))      if (ME->getSelector() == Dealloc) {        switch (ME->getReceiverKind()) {        case ObjCMessageExpr::Instance: return false; @@ -48,26 +48,26 @@ static bool scan_dealloc(Stmt* S, Selector Dealloc) {    return false;  } -static bool scan_ivar_release(Stmt* S, ObjCIvarDecl* ID, -                              const ObjCPropertyDecl* PD, +static bool scan_ivar_release(Stmt *S, ObjCIvarDecl *ID, +                              const ObjCPropertyDecl *PD,                                Selector Release,                                IdentifierInfo* SelfII, -                              ASTContext& Ctx) { +                              ASTContext &Ctx) {    // [mMyIvar release] -  if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) +  if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))      if (ME->getSelector() == Release)        if (ME->getInstanceReceiver()) -        if (Expr* Receiver = ME->getInstanceReceiver()->IgnoreParenCasts()) -          if (ObjCIvarRefExpr* E = dyn_cast<ObjCIvarRefExpr>(Receiver)) +        if (Expr *Receiver = ME->getInstanceReceiver()->IgnoreParenCasts()) +          if (ObjCIvarRefExpr *E = dyn_cast<ObjCIvarRefExpr>(Receiver))              if (E->getDecl() == ID)                return true;    // [self setMyIvar:nil]; -  if (ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(S)) +  if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S))      if (ME->getInstanceReceiver()) -      if (Expr* Receiver = ME->getInstanceReceiver()->IgnoreParenCasts()) -        if (DeclRefExpr* E = dyn_cast<DeclRefExpr>(Receiver)) +      if (Expr *Receiver = ME->getInstanceReceiver()->IgnoreParenCasts()) +        if (DeclRefExpr *E = dyn_cast<DeclRefExpr>(Receiver))            if (E->getDecl()->getIdentifier() == SelfII)              if (ME->getMethodDecl() == PD->getSetterMethodDecl() &&                  ME->getNumArgs() == 1 && @@ -78,7 +78,7 @@ static bool scan_ivar_release(Stmt* S, ObjCIvarDecl* ID,    // self.myIvar = nil;    if (BinaryOperator* BO = dyn_cast<BinaryOperator>(S))      if (BO->isAssignmentOp()) -      if (ObjCPropertyRefExpr* PRE = +      if (ObjCPropertyRefExpr *PRE =             dyn_cast<ObjCPropertyRefExpr>(BO->getLHS()->IgnoreParenCasts()))          if (PRE->isExplicitProperty() && PRE->getExplicitProperty() == PD)              if (BO->getRHS()->isNullPointerConstant(Ctx,  @@ -96,13 +96,13 @@ static bool scan_ivar_release(Stmt* S, ObjCIvarDecl* ID,    return false;  } -static void checkObjCDealloc(const ObjCImplementationDecl* D, +static void checkObjCDealloc(const ObjCImplementationDecl *D,                               const LangOptions& LOpts, BugReporter& BR) { -  assert (LOpts.getGCMode() != LangOptions::GCOnly); +  assert (LOpts.getGC() != LangOptions::GCOnly); -  ASTContext& Ctx = BR.getContext(); -  const ObjCInterfaceDecl* ID = D->getClassInterface(); +  ASTContext &Ctx = BR.getContext(); +  const ObjCInterfaceDecl *ID = D->getClassInterface();    // Does the class contain any ivars that are pointers (or id<...>)?    // If not, skip the check entirely. @@ -114,7 +114,7 @@ static void checkObjCDealloc(const ObjCImplementationDecl* D,    for (ObjCInterfaceDecl::ivar_iterator I=ID->ivar_begin(), E=ID->ivar_end();         I!=E; ++I) { -    ObjCIvarDecl* ID = *I; +    ObjCIvarDecl *ID = *I;      QualType T = ID->getType();      if (!T->isObjCObjectPointerType() || @@ -154,7 +154,7 @@ static void checkObjCDealloc(const ObjCImplementationDecl* D,    // Get the "dealloc" selector.    IdentifierInfo* II = &Ctx.Idents.get("dealloc");    Selector S = Ctx.Selectors.getSelector(0, &II); -  ObjCMethodDecl* MD = 0; +  ObjCMethodDecl *MD = 0;    // Scan the instance methods for "dealloc".    for (ObjCImplementationDecl::instmeth_iterator I = D->instmeth_begin(), @@ -166,9 +166,12 @@ static void checkObjCDealloc(const ObjCImplementationDecl* D,      }    } +  PathDiagnosticLocation DLoc = +    PathDiagnosticLocation::createBegin(D, BR.getSourceManager()); +    if (!MD) { // No dealloc found. -    const char* name = LOpts.getGCMode() == LangOptions::NonGC +    const char* name = LOpts.getGC() == LangOptions::NonGC                         ? "missing -dealloc"                         : "missing -dealloc (Hybrid MM, non-GC)"; @@ -176,14 +179,14 @@ static void checkObjCDealloc(const ObjCImplementationDecl* D,      llvm::raw_string_ostream os(buf);      os << "Objective-C class '" << D << "' lacks a 'dealloc' instance method"; -    BR.EmitBasicReport(name, os.str(), D->getLocStart()); +    BR.EmitBasicReport(name, os.str(), DLoc);      return;    }    // dealloc found.  Scan for missing [super dealloc].    if (MD->getBody() && !scan_dealloc(MD->getBody(), S)) { -    const char* name = LOpts.getGCMode() == LangOptions::NonGC +    const char* name = LOpts.getGC() == LangOptions::NonGC                         ? "missing [super dealloc]"                         : "missing [super dealloc] (Hybrid MM, non-GC)"; @@ -193,7 +196,7 @@ static void checkObjCDealloc(const ObjCImplementationDecl* D,         << "' does not send a 'dealloc' message to its super class"             " (missing [super dealloc])"; -    BR.EmitBasicReport(name, os.str(), D->getLocStart()); +    BR.EmitBasicReport(name, os.str(), DLoc);      return;    } @@ -213,7 +216,7 @@ static void checkObjCDealloc(const ObjCImplementationDecl* D,      if ((*I)->getPropertyImplementation() != ObjCPropertyImplDecl::Synthesize)        continue; -    ObjCIvarDecl* ID = (*I)->getPropertyIvarDecl(); +    ObjCIvarDecl *ID = (*I)->getPropertyIvarDecl();      if (!ID)        continue; @@ -221,13 +224,13 @@ static void checkObjCDealloc(const ObjCImplementationDecl* D,      if (!T->isObjCObjectPointerType()) // Skip non-pointer ivars        continue; -    const ObjCPropertyDecl* PD = (*I)->getPropertyDecl(); +    const ObjCPropertyDecl *PD = (*I)->getPropertyDecl();      if (!PD)        continue;      // ivars cannot be set via read-only properties, so we'll skip them      if (PD->isReadOnly()) -       continue; +      continue;      // ivar must be released if and only if the kind of setter was not 'assign'      bool requiresRelease = PD->getSetterKind() != ObjCPropertyDecl::Assign; @@ -240,24 +243,27 @@ static void checkObjCDealloc(const ObjCImplementationDecl* D,        llvm::raw_string_ostream os(buf);        if (requiresRelease) { -        name = LOpts.getGCMode() == LangOptions::NonGC +        name = LOpts.getGC() == LangOptions::NonGC                 ? "missing ivar release (leak)"                 : "missing ivar release (Hybrid MM, non-GC)"; -        os << "The '" << ID +        os << "The '" << *ID             << "' instance variable was retained by a synthesized property but "                "wasn't released in 'dealloc'";        } else { -        name = LOpts.getGCMode() == LangOptions::NonGC +        name = LOpts.getGC() == LangOptions::NonGC                 ? "extra ivar release (use-after-release)"                 : "extra ivar release (Hybrid MM, non-GC)"; -        os << "The '" << ID +        os << "The '" << *ID             << "' instance variable was not retained by a synthesized property "                "but was released in 'dealloc'";        } -      BR.EmitBasicReport(name, category, os.str(), (*I)->getLocation()); +      PathDiagnosticLocation SDLoc = +        PathDiagnosticLocation::createBegin((*I), BR.getSourceManager()); + +      BR.EmitBasicReport(name, category, os.str(), SDLoc);      }    }  } @@ -272,7 +278,7 @@ class ObjCDeallocChecker : public Checker<  public:    void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager& mgr,                      BugReporter &BR) const { -    if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) +    if (mgr.getLangOptions().getGC() == LangOptions::GCOnly)        return;      checkObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR);    }  | 
