Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 1 | //== BasicObjCFoundationChecks.cpp - Simple Apple-Foundation checks -*- 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 BasicObjCFoundationChecks, a class that encapsulates |
| 11 | // a set of simple checks to run on Objective-C code using Apple's Foundation |
| 12 | // classes. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 16 | #include "BasicObjCFoundationChecks.h" |
| 17 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 18 | #include "clang/Checker/PathSensitive/ExplodedGraph.h" |
| 19 | #include "clang/Checker/PathSensitive/GRSimpleAPICheck.h" |
| 20 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
| 21 | #include "clang/Checker/PathSensitive/GRState.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 22 | #include "clang/Checker/BugReporter/BugType.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 23 | #include "clang/Checker/PathSensitive/MemRegion.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 24 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Ted Kremenek | 9705309 | 2010-01-26 22:59:55 +0000 | [diff] [blame] | 25 | #include "clang/Checker/Checkers/LocalCheckers.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 27 | #include "clang/AST/Expr.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 29 | #include "clang/AST/ASTContext.h" |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 31 | using namespace clang; |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 32 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 33 | static const ObjCInterfaceType* GetReceiverType(const ObjCMessageExpr* ME) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 34 | QualType T; |
| 35 | switch (ME->getReceiverKind()) { |
| 36 | case ObjCMessageExpr::Instance: |
| 37 | T = ME->getInstanceReceiver()->getType(); |
| 38 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 40 | case ObjCMessageExpr::SuperInstance: |
| 41 | T = ME->getSuperType(); |
| 42 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 44 | case ObjCMessageExpr::Class: |
| 45 | case ObjCMessageExpr::SuperClass: |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | if (const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 50 | return PT->getInterfaceType(); |
Ted Kremenek | c1ff3cd | 2008-04-30 22:48:21 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | c1ff3cd | 2008-04-30 22:48:21 +0000 | [diff] [blame] | 52 | return NULL; |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 55 | static const char* GetReceiverNameType(const ObjCMessageExpr* ME) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 56 | if (const ObjCInterfaceType *ReceiverType = GetReceiverType(ME)) |
| 57 | return ReceiverType->getDecl()->getIdentifier()->getNameStart(); |
| 58 | return NULL; |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 61 | namespace { |
Ted Kremenek | b344f91 | 2008-09-21 19:01:39 +0000 | [diff] [blame] | 62 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 63 | class APIMisuse : public BugType { |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 64 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 65 | APIMisuse(const char* name) : BugType(name, "API Misuse (Apple)") {} |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 66 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 68 | class BasicObjCFoundationChecks : public GRSimpleAPICheck { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 69 | APIMisuse *BT; |
| 70 | BugReporter& BR; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 71 | ASTContext &Ctx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Daniel Dunbar | d777d58 | 2009-10-17 18:12:21 +0000 | [diff] [blame] | 73 | bool isNSString(const ObjCInterfaceType *T, llvm::StringRef suffix); |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 74 | bool AuditNSString(ExplodedNode* N, const ObjCMessageExpr* ME); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 76 | bool CheckNilArg(ExplodedNode* N, unsigned Arg); |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 77 | |
| 78 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | BasicObjCFoundationChecks(ASTContext& ctx, BugReporter& br) |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 80 | : BT(0), BR(br), Ctx(ctx) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 82 | bool Audit(ExplodedNode* N, GRStateManager&); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | void WarnNilArg(ExplodedNode* N, const ObjCMessageExpr* ME, unsigned Arg) { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 86 | std::string sbuf; |
| 87 | llvm::raw_string_ostream os(sbuf); |
| 88 | os << "Argument to '" << GetReceiverNameType(ME) << "' method '" |
| 89 | << ME->getSelector().getAsString() << "' cannot be nil."; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 91 | // Lazily create the BugType object for NilArg. This will be owned |
| 92 | // by the BugReporter object 'BR' once we call BR.EmitWarning. |
| 93 | if (!BT) BT = new APIMisuse("nil argument"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Benjamin Kramer | 4988a9a | 2009-11-29 18:03:28 +0000 | [diff] [blame] | 95 | RangedBugReport *R = new RangedBugReport(*BT, os.str(), N); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 96 | R->addRange(ME->getArg(Arg)->getSourceRange()); |
| 97 | BR.EmitReport(R); |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 98 | } |
| 99 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 101 | } // end anonymous namespace |
| 102 | |
| 103 | |
| 104 | GRSimpleAPICheck* |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 105 | clang::CreateBasicObjCFoundationChecks(ASTContext& Ctx, BugReporter& BR) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | return new BasicObjCFoundationChecks(Ctx, BR); |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 111 | bool BasicObjCFoundationChecks::Audit(ExplodedNode* N, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 112 | GRStateManager&) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 114 | const ObjCMessageExpr* ME = |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 115 | cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt()); |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 116 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 117 | const ObjCInterfaceType *ReceiverType = GetReceiverType(ME); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 119 | if (!ReceiverType) |
Nuno Lopes | f742794 | 2008-05-20 17:33:56 +0000 | [diff] [blame] | 120 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 121 | |
Daniel Dunbar | d777d58 | 2009-10-17 18:12:21 +0000 | [diff] [blame] | 122 | if (isNSString(ReceiverType, |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 123 | ReceiverType->getDecl()->getIdentifier()->getName())) |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 124 | return AuditNSString(N, ME); |
| 125 | |
Nuno Lopes | f742794 | 2008-05-20 17:33:56 +0000 | [diff] [blame] | 126 | return false; |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 129 | static inline bool isNil(SVal X) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | return isa<loc::ConcreteInt>(X); |
Ted Kremenek | e5d5c20 | 2008-03-27 21:15:17 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 133 | //===----------------------------------------------------------------------===// |
| 134 | // Error reporting. |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | |
Zhongxing Xu | 031ccc0 | 2009-08-06 12:48:26 +0000 | [diff] [blame] | 137 | bool BasicObjCFoundationChecks::CheckNilArg(ExplodedNode* N, unsigned Arg) { |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 138 | const ObjCMessageExpr* ME = |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 139 | cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 141 | const Expr * E = ME->getArg(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 143 | if (isNil(N->getState()->getSVal(E))) { |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 144 | WarnNilArg(N, ME, Arg); |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 145 | return true; |
| 146 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 151 | //===----------------------------------------------------------------------===// |
| 152 | // NSString checking. |
| 153 | //===----------------------------------------------------------------------===// |
| 154 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 155 | bool BasicObjCFoundationChecks::isNSString(const ObjCInterfaceType *T, |
Daniel Dunbar | d777d58 | 2009-10-17 18:12:21 +0000 | [diff] [blame] | 156 | llvm::StringRef ClassName) { |
| 157 | return ClassName == "NSString" || ClassName == "NSMutableString"; |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | bool BasicObjCFoundationChecks::AuditNSString(ExplodedNode* N, |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 161 | const ObjCMessageExpr* ME) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 163 | Selector S = ME->getSelector(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 165 | if (S.isUnarySelector()) |
| 166 | return false; |
| 167 | |
| 168 | // FIXME: This is going to be really slow doing these checks with |
| 169 | // lexical comparisons. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Benjamin Kramer | 064fb20 | 2010-02-08 19:51:59 +0000 | [diff] [blame] | 171 | std::string NameStr = S.getAsString(); |
| 172 | llvm::StringRef Name(NameStr); |
| 173 | assert(!Name.empty()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | |
Benjamin Kramer | 064fb20 | 2010-02-08 19:51:59 +0000 | [diff] [blame] | 175 | // FIXME: Checking for initWithFormat: will not work in most cases |
| 176 | // yet because [NSString alloc] returns id, not NSString*. We will |
| 177 | // need support for tracking expected-type information in the analyzer |
| 178 | // to find these errors. |
| 179 | if (Name == "caseInsensitiveCompare:" || |
| 180 | Name == "compare:" || |
| 181 | Name == "compare:options:" || |
| 182 | Name == "compare:options:range:" || |
| 183 | Name == "compare:options:range:locale:" || |
| 184 | Name == "componentsSeparatedByCharactersInSet:" || |
| 185 | Name == "initWithFormat:") |
| 186 | return CheckNilArg(N, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 188 | return false; |
| 189 | } |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 190 | |
| 191 | //===----------------------------------------------------------------------===// |
| 192 | // Error reporting. |
| 193 | //===----------------------------------------------------------------------===// |
| 194 | |
| 195 | namespace { |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 196 | |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 197 | class AuditCFNumberCreate : public GRSimpleAPICheck { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 198 | APIMisuse* BT; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 200 | // FIXME: Either this should be refactored into GRSimpleAPICheck, or |
| 201 | // it should always be passed with a call to Audit. The latter |
| 202 | // approach makes this class more stateless. |
| 203 | ASTContext& Ctx; |
| 204 | IdentifierInfo* II; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 205 | BugReporter& BR; |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 206 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 207 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | AuditCFNumberCreate(ASTContext& ctx, BugReporter& br) |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 209 | : BT(0), Ctx(ctx), II(&Ctx.Idents.get("CFNumberCreate")), BR(br){} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 211 | ~AuditCFNumberCreate() {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 213 | bool Audit(ExplodedNode* N, GRStateManager&); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 215 | private: |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 216 | void AddError(const TypedRegion* R, const Expr* Ex, ExplodedNode *N, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | uint64_t SourceSize, uint64_t TargetSize, uint64_t NumberKind); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 218 | }; |
| 219 | } // end anonymous namespace |
| 220 | |
| 221 | enum CFNumberType { |
| 222 | kCFNumberSInt8Type = 1, |
| 223 | kCFNumberSInt16Type = 2, |
| 224 | kCFNumberSInt32Type = 3, |
| 225 | kCFNumberSInt64Type = 4, |
| 226 | kCFNumberFloat32Type = 5, |
| 227 | kCFNumberFloat64Type = 6, |
| 228 | kCFNumberCharType = 7, |
| 229 | kCFNumberShortType = 8, |
| 230 | kCFNumberIntType = 9, |
| 231 | kCFNumberLongType = 10, |
| 232 | kCFNumberLongLongType = 11, |
| 233 | kCFNumberFloatType = 12, |
| 234 | kCFNumberDoubleType = 13, |
| 235 | kCFNumberCFIndexType = 14, |
| 236 | kCFNumberNSIntegerType = 15, |
| 237 | kCFNumberCGFloatType = 16 |
| 238 | }; |
| 239 | |
| 240 | namespace { |
| 241 | template<typename T> |
| 242 | class Optional { |
| 243 | bool IsKnown; |
| 244 | T Val; |
| 245 | public: |
| 246 | Optional() : IsKnown(false), Val(0) {} |
| 247 | Optional(const T& val) : IsKnown(true), Val(val) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 249 | bool isKnown() const { return IsKnown; } |
| 250 | |
| 251 | const T& getValue() const { |
| 252 | assert (isKnown()); |
| 253 | return Val; |
| 254 | } |
| 255 | |
| 256 | operator const T&() const { |
| 257 | return getValue(); |
| 258 | } |
| 259 | }; |
| 260 | } |
| 261 | |
| 262 | static Optional<uint64_t> GetCFNumberSize(ASTContext& Ctx, uint64_t i) { |
Nuno Lopes | 2550d70 | 2009-12-23 17:49:57 +0000 | [diff] [blame] | 263 | static const unsigned char FixedSize[] = { 8, 16, 32, 64, 32, 64 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 265 | if (i < kCFNumberCharType) |
| 266 | return FixedSize[i-1]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 268 | QualType T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 270 | switch (i) { |
| 271 | case kCFNumberCharType: T = Ctx.CharTy; break; |
| 272 | case kCFNumberShortType: T = Ctx.ShortTy; break; |
| 273 | case kCFNumberIntType: T = Ctx.IntTy; break; |
| 274 | case kCFNumberLongType: T = Ctx.LongTy; break; |
| 275 | case kCFNumberLongLongType: T = Ctx.LongLongTy; break; |
| 276 | case kCFNumberFloatType: T = Ctx.FloatTy; break; |
| 277 | case kCFNumberDoubleType: T = Ctx.DoubleTy; break; |
| 278 | case kCFNumberCFIndexType: |
| 279 | case kCFNumberNSIntegerType: |
| 280 | case kCFNumberCGFloatType: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | // FIXME: We need a way to map from names to Type*. |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 282 | default: |
| 283 | return Optional<uint64_t>(); |
| 284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 286 | return Ctx.getTypeSize(T); |
| 287 | } |
| 288 | |
| 289 | #if 0 |
| 290 | static const char* GetCFNumberTypeStr(uint64_t i) { |
| 291 | static const char* Names[] = { |
| 292 | "kCFNumberSInt8Type", |
| 293 | "kCFNumberSInt16Type", |
| 294 | "kCFNumberSInt32Type", |
| 295 | "kCFNumberSInt64Type", |
| 296 | "kCFNumberFloat32Type", |
| 297 | "kCFNumberFloat64Type", |
| 298 | "kCFNumberCharType", |
| 299 | "kCFNumberShortType", |
| 300 | "kCFNumberIntType", |
| 301 | "kCFNumberLongType", |
| 302 | "kCFNumberLongLongType", |
| 303 | "kCFNumberFloatType", |
| 304 | "kCFNumberDoubleType", |
| 305 | "kCFNumberCFIndexType", |
| 306 | "kCFNumberNSIntegerType", |
| 307 | "kCFNumberCGFloatType" |
| 308 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 310 | return i <= kCFNumberCGFloatType ? Names[i-1] : "Invalid CFNumberType"; |
| 311 | } |
| 312 | #endif |
| 313 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | bool AuditCFNumberCreate::Audit(ExplodedNode* N,GRStateManager&){ |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 315 | const CallExpr* CE = |
| 316 | cast<CallExpr>(cast<PostStmt>(N->getLocation()).getStmt()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | const Expr* Callee = CE->getCallee(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 318 | SVal CallV = N->getState()->getSVal(Callee); |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 319 | const FunctionDecl* FD = CallV.getAsFunctionDecl(); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 320 | |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 321 | if (!FD || FD->getIdentifier() != II || CE->getNumArgs()!=3) |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 322 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 324 | // Get the value of the "theType" argument. |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 325 | SVal TheTypeVal = N->getState()->getSVal(CE->getArg(1)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 327 | // FIXME: We really should allow ranges of valid theType values, and |
| 328 | // bifurcate the state appropriately. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 329 | nonloc::ConcreteInt* V = dyn_cast<nonloc::ConcreteInt>(&TheTypeVal); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 331 | if (!V) |
| 332 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 334 | uint64_t NumberKind = V->getValue().getLimitedValue(); |
| 335 | Optional<uint64_t> TargetSize = GetCFNumberSize(Ctx, NumberKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 337 | // FIXME: In some cases we can emit an error. |
| 338 | if (!TargetSize.isKnown()) |
| 339 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 341 | // Look at the value of the integer being passed by reference. Essentially |
| 342 | // we want to catch cases where the value passed in is not equal to the |
| 343 | // size of the type being created. |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 344 | SVal TheValueExpr = N->getState()->getSVal(CE->getArg(2)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 346 | // FIXME: Eventually we should handle arbitrary locations. We can do this |
| 347 | // by having an enhanced memory model that does low-level typing. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 348 | loc::MemRegionVal* LV = dyn_cast<loc::MemRegionVal>(&TheValueExpr); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 349 | |
| 350 | if (!LV) |
| 351 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Zhongxing Xu | 479529e | 2009-11-10 02:17:20 +0000 | [diff] [blame] | 353 | const TypedRegion* R = dyn_cast<TypedRegion>(LV->StripCasts()); |
Ted Kremenek | 5e77eba | 2009-07-29 18:17:40 +0000 | [diff] [blame] | 354 | |
| 355 | if (!R) |
| 356 | return false; |
| 357 | |
Zhongxing Xu | 018220c | 2010-08-11 06:10:55 +0000 | [diff] [blame] | 358 | QualType T = Ctx.getCanonicalType(R->getValueType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 360 | // FIXME: If the pointee isn't an integer type, should we flag a warning? |
| 361 | // People can do weird stuff with pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
| 363 | if (!T->isIntegerType()) |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 364 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 366 | uint64_t SourceSize = Ctx.getTypeSize(T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 367 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 368 | // CHECK: is SourceSize == TargetSize |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 370 | if (SourceSize == TargetSize) |
| 371 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 373 | AddError(R, CE->getArg(2), N, SourceSize, TargetSize, NumberKind); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 375 | // FIXME: We can actually create an abstract "CFNumber" object that has |
| 376 | // the bits initialized to the provided values. |
| 377 | return SourceSize < TargetSize; |
| 378 | } |
| 379 | |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 380 | void AuditCFNumberCreate::AddError(const TypedRegion* R, const Expr* Ex, |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 381 | ExplodedNode *N, |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 382 | uint64_t SourceSize, uint64_t TargetSize, |
| 383 | uint64_t NumberKind) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 385 | std::string sbuf; |
| 386 | llvm::raw_string_ostream os(sbuf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 387 | |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 388 | os << (SourceSize == 8 ? "An " : "A ") |
| 389 | << SourceSize << " bit integer is used to initialize a CFNumber " |
| 390 | "object that represents " |
| 391 | << (TargetSize == 8 ? "an " : "a ") |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 392 | << TargetSize << " bit integer. "; |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 393 | |
| 394 | if (SourceSize < TargetSize) |
| 395 | os << (TargetSize - SourceSize) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | << " bits of the CFNumber value will be garbage." ; |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 397 | else |
| 398 | os << (SourceSize - TargetSize) |
| 399 | << " bits of the input integer will be lost."; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 401 | // Lazily create the BugType object. This will be owned |
| 402 | // by the BugReporter object 'BR' once we call BR.EmitWarning. |
| 403 | if (!BT) BT = new APIMisuse("Bad use of CFNumberCreate"); |
Benjamin Kramer | 4988a9a | 2009-11-29 18:03:28 +0000 | [diff] [blame] | 404 | RangedBugReport *report = new RangedBugReport(*BT, os.str(), N); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 405 | report->addRange(Ex->getSourceRange()); |
| 406 | BR.EmitReport(report); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | GRSimpleAPICheck* |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | clang::CreateAuditCFNumberCreate(ASTContext& Ctx, BugReporter& BR) { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 411 | return new AuditCFNumberCreate(Ctx, BR); |
Ted Kremenek | 04bc876 | 2008-06-26 23:59:48 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 414 | //===----------------------------------------------------------------------===// |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 415 | // CFRetain/CFRelease checking for null arguments. |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 416 | //===----------------------------------------------------------------------===// |
| 417 | |
| 418 | namespace { |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 419 | class CFRetainReleaseChecker : public CheckerVisitor<CFRetainReleaseChecker> { |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 420 | APIMisuse *BT; |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 421 | IdentifierInfo *Retain, *Release; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 423 | public: |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 424 | CFRetainReleaseChecker(ASTContext& Ctx): BT(NULL), |
| 425 | Retain(&Ctx.Idents.get("CFRetain")), Release(&Ctx.Idents.get("CFRelease")) |
| 426 | {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 428 | static void *getTag() { static int x = 0; return &x; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 430 | void PreVisitCallExpr(CheckerContext& C, const CallExpr* CE); |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 431 | }; |
| 432 | } // end anonymous namespace |
| 433 | |
| 434 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 435 | void CFRetainReleaseChecker::PreVisitCallExpr(CheckerContext& C, |
| 436 | const CallExpr* CE) { |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 437 | // If the CallExpr doesn't have exactly 1 argument just give up checking. |
| 438 | if (CE->getNumArgs() != 1) |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 439 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 441 | // Get the function declaration of the callee. |
| 442 | const GRState* state = C.getState(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 443 | SVal X = state->getSVal(CE->getCallee()); |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 444 | const FunctionDecl* FD = X.getAsFunctionDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 446 | if (!FD) |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 447 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 448 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 449 | // Check if we called CFRetain/CFRelease. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | const IdentifierInfo *FuncII = FD->getIdentifier(); |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 451 | if (!(FuncII == Retain || FuncII == Release)) |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 452 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 454 | // FIXME: The rest of this just checks that the argument is non-null. |
| 455 | // It should probably be refactored and combined with AttrNonNullChecker. |
| 456 | |
| 457 | // Get the argument's value. |
| 458 | const Expr *Arg = CE->getArg(0); |
| 459 | SVal ArgVal = state->getSVal(Arg); |
| 460 | DefinedSVal *DefArgVal = dyn_cast<DefinedSVal>(&ArgVal); |
| 461 | if (!DefArgVal) |
| 462 | return; |
| 463 | |
| 464 | // Get a NULL value. |
| 465 | ValueManager &ValMgr = C.getValueManager(); |
| 466 | DefinedSVal Zero = cast<DefinedSVal>(ValMgr.makeZeroVal(Arg->getType())); |
| 467 | |
| 468 | // Make an expression asserting that they're equal. |
| 469 | SValuator &SVator = ValMgr.getSValuator(); |
| 470 | DefinedOrUnknownSVal ArgIsNull = SVator.EvalEQ(state, Zero, *DefArgVal); |
| 471 | |
| 472 | // Are they equal? |
| 473 | const GRState *stateTrue, *stateFalse; |
| 474 | llvm::tie(stateTrue, stateFalse) = state->Assume(ArgIsNull); |
| 475 | |
| 476 | if (stateTrue && !stateFalse) { |
| 477 | ExplodedNode *N = C.GenerateSink(stateTrue); |
| 478 | if (!N) |
| 479 | return; |
| 480 | |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 481 | if (!BT) |
| 482 | BT = new APIMisuse("null passed to CFRetain/CFRelease"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 484 | const char *description = (FuncII == Retain) |
| 485 | ? "Null pointer argument in call to CFRetain" |
| 486 | : "Null pointer argument in call to CFRelease"; |
| 487 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 488 | EnhancedBugReport *report = new EnhancedBugReport(*BT, description, N); |
| 489 | report->addRange(Arg->getSourceRange()); |
| 490 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Arg); |
| 491 | |
| 492 | C.EmitReport(report); |
| 493 | return; |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 496 | // From here on, we know the argument is non-null. |
| 497 | C.addTransition(stateFalse); |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 501 | // Check for sending 'retain', 'release', or 'autorelease' directly to a Class. |
| 502 | //===----------------------------------------------------------------------===// |
| 503 | |
| 504 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 505 | class ClassReleaseChecker : |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 506 | public CheckerVisitor<ClassReleaseChecker> { |
| 507 | Selector releaseS; |
| 508 | Selector retainS; |
| 509 | Selector autoreleaseS; |
| 510 | Selector drainS; |
| 511 | BugType *BT; |
| 512 | public: |
| 513 | ClassReleaseChecker(ASTContext &Ctx) |
| 514 | : releaseS(GetNullarySelector("release", Ctx)), |
| 515 | retainS(GetNullarySelector("retain", Ctx)), |
| 516 | autoreleaseS(GetNullarySelector("autorelease", Ctx)), |
| 517 | drainS(GetNullarySelector("drain", Ctx)), |
| 518 | BT(0) {} |
| 519 | |
| 520 | static void *getTag() { static int x = 0; return &x; } |
| 521 | |
| 522 | void PreVisitObjCMessageExpr(CheckerContext &C, const ObjCMessageExpr *ME); |
| 523 | }; |
| 524 | } |
| 525 | |
| 526 | void ClassReleaseChecker::PreVisitObjCMessageExpr(CheckerContext &C, |
| 527 | const ObjCMessageExpr *ME) { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 528 | ObjCInterfaceDecl *Class = 0; |
| 529 | switch (ME->getReceiverKind()) { |
| 530 | case ObjCMessageExpr::Class: |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 531 | Class = ME->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 532 | break; |
| 533 | |
| 534 | case ObjCMessageExpr::SuperClass: |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 535 | Class = ME->getSuperType()->getAs<ObjCObjectType>()->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 536 | break; |
| 537 | |
| 538 | case ObjCMessageExpr::Instance: |
| 539 | case ObjCMessageExpr::SuperInstance: |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 540 | return; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 543 | Selector S = ME->getSelector(); |
Benjamin Kramer | 921ddc4 | 2009-11-20 10:03:00 +0000 | [diff] [blame] | 544 | if (!(S == releaseS || S == retainS || S == autoreleaseS || S == drainS)) |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 545 | return; |
| 546 | |
| 547 | if (!BT) |
| 548 | BT = new APIMisuse("message incorrectly sent to class instead of class " |
| 549 | "instance"); |
| 550 | |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 551 | ExplodedNode *N = C.GenerateNode(); |
| 552 | |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 553 | if (!N) |
| 554 | return; |
| 555 | |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 556 | llvm::SmallString<200> buf; |
| 557 | llvm::raw_svector_ostream os(buf); |
| 558 | |
| 559 | os << "The '" << S.getAsString() << "' message should be sent to instances " |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 560 | "of class '" << Class->getName() |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 561 | << "' and not the class directly"; |
| 562 | |
| 563 | RangedBugReport *report = new RangedBugReport(*BT, os.str(), N); |
| 564 | report->addRange(ME->getSourceRange()); |
| 565 | C.EmitReport(report); |
| 566 | } |
| 567 | |
| 568 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 569 | // Check registration. |
Ted Kremenek | 79b4f7d | 2009-07-14 00:43:42 +0000 | [diff] [blame] | 570 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 571 | |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 572 | void clang::RegisterAppleChecks(GRExprEngine& Eng, const Decl &D) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 573 | ASTContext& Ctx = Eng.getContext(); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 574 | BugReporter &BR = Eng.getBugReporter(); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 575 | |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 576 | Eng.AddCheck(CreateBasicObjCFoundationChecks(Ctx, BR), |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 577 | Stmt::ObjCMessageExprClass); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | Eng.AddCheck(CreateAuditCFNumberCreate(Ctx, BR), Stmt::CallExprClass); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Zhongxing Xu | 5ab128b | 2009-08-21 02:18:44 +0000 | [diff] [blame] | 580 | RegisterNSErrorChecks(BR, Eng, D); |
Ted Kremenek | 54cb7cc | 2009-11-03 08:03:59 +0000 | [diff] [blame] | 581 | RegisterNSAutoreleasePoolChecks(Eng); |
Jordy Rose | 61fb55c | 2010-07-06 02:34:42 +0000 | [diff] [blame] | 582 | |
| 583 | Eng.registerCheck(new CFRetainReleaseChecker(Ctx)); |
Ted Kremenek | 50e837b | 2009-11-20 05:27:05 +0000 | [diff] [blame] | 584 | Eng.registerCheck(new ClassReleaseChecker(Ctx)); |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 585 | } |