Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 1 | //=- NSAutoreleasePoolChecker.cpp --------------------------------*- 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 NSAutoreleasePoolChecker, a small checker that warns |
| 11 | // about subpar uses of NSAutoreleasePool. Note that while the check itself |
| 12 | // (in it's current form) could be written as a flow-insensitive check, in |
| 13 | // can be potentially enhanced in the future with flow-sensitive information. |
| 14 | // It is also a good example of the CheckerVisitor interface. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 19 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 20 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
| 21 | #include "BasicObjCFoundationChecks.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | #include "clang/AST/DeclObjC.h" |
| 24 | #include "clang/AST/Decl.h" |
| 25 | |
| 26 | using namespace clang; |
| 27 | |
| 28 | namespace { |
| 29 | class VISIBILITY_HIDDEN NSAutoreleasePoolChecker |
| 30 | : public CheckerVisitor<NSAutoreleasePoolChecker> { |
| 31 | |
| 32 | Selector releaseS; |
| 33 | |
| 34 | public: |
| 35 | NSAutoreleasePoolChecker(Selector release_s) : releaseS(release_s) {} |
| 36 | |
| 37 | static void *getTag() { |
| 38 | static int x = 0; |
| 39 | return &x; |
| 40 | } |
| 41 | |
| 42 | void PreVisitObjCMessageExpr(CheckerContext &C, const ObjCMessageExpr *ME); |
| 43 | }; |
| 44 | |
| 45 | } // end anonymous namespace |
| 46 | |
| 47 | |
| 48 | void clang::RegisterNSAutoreleasePoolChecks(GRExprEngine &Eng) { |
| 49 | ASTContext &Ctx = Eng.getContext(); |
| 50 | if (Ctx.getLangOptions().getGCMode() != LangOptions::NonGC) { |
| 51 | Eng.registerCheck(new NSAutoreleasePoolChecker(GetNullarySelector("release", |
| 52 | Ctx))); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | NSAutoreleasePoolChecker::PreVisitObjCMessageExpr(CheckerContext &C, |
| 58 | const ObjCMessageExpr *ME) { |
| 59 | |
| 60 | const Expr *receiver = ME->getReceiver(); |
| 61 | if (!receiver) |
| 62 | return; |
| 63 | |
| 64 | // FIXME: Enhance with value-tracking information instead of consulting |
| 65 | // the type of the expression. |
| 66 | const ObjCObjectPointerType* PT = |
| 67 | receiver->getType()->getAs<ObjCObjectPointerType>(); |
Ted Kremenek | 71a5e28 | 2009-11-20 00:12:36 +0000 | [diff] [blame] | 68 | |
| 69 | if (!PT) |
| 70 | return; |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 71 | const ObjCInterfaceDecl* OD = PT->getInterfaceDecl(); |
| 72 | if (!OD) |
| 73 | return; |
| 74 | if (!OD->getIdentifier()->getName().equals("NSAutoreleasePool")) |
| 75 | return; |
| 76 | |
| 77 | // Sending 'release' message? |
| 78 | if (ME->getSelector() != releaseS) |
| 79 | return; |
| 80 | |
| 81 | SourceRange R = ME->getSourceRange(); |
| 82 | |
| 83 | C.getBugReporter().EmitBasicReport("Use -drain instead of -release", |
| 84 | "API Upgrade (Apple)", |
| 85 | "Use -drain instead of -release when using NSAutoreleasePool " |
| 86 | "and garbage collection", ME->getLocStart(), &R, 1); |
| 87 | } |