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