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 | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/ExplodedGraph.h" |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathSensitive/GRSimpleAPICheck.h" |
| 21 | #include "clang/Analysis/PathSensitive/ValueState.h" |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/PathDiagnostic.h" |
| 24 | #include "clang/AST/Expr.h" |
| 25 | #include "clang/AST/ASTContext.h" |
| 26 | #include "llvm/Support/Compiler.h" |
| 27 | |
| 28 | #include <vector> |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 29 | #include <sstream> |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace clang; |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 33 | static ObjCInterfaceType* GetReceiverType(ObjCMessageExpr* ME) { |
| 34 | Expr* Receiver = ME->getReceiver(); |
| 35 | |
| 36 | if (!Receiver) |
| 37 | return NULL; |
| 38 | |
Ted Kremenek | 7956f75 | 2008-04-03 21:44:24 +0000 | [diff] [blame] | 39 | // FIXME: Cleanup |
| 40 | QualType X = Receiver->getType(); |
| 41 | Type* TP = X.getTypePtr(); |
| 42 | assert (TP->isPointerType()); |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | 7956f75 | 2008-04-03 21:44:24 +0000 | [diff] [blame] | 44 | const PointerType* T = TP->getAsPointerType(); |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 45 | |
| 46 | return dyn_cast<ObjCInterfaceType>(T->getPointeeType().getTypePtr()); |
| 47 | } |
| 48 | |
| 49 | static const char* GetReceiverNameType(ObjCMessageExpr* ME) { |
| 50 | ObjCInterfaceType* ReceiverType = GetReceiverType(ME); |
| 51 | return ReceiverType ? ReceiverType->getDecl()->getIdentifier()->getName() |
| 52 | : NULL; |
| 53 | } |
Ted Kremenek | 5275561 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 57 | class VISIBILITY_HIDDEN NilArg : public BugType { |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 58 | public: |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 59 | virtual ~NilArg() {} |
| 60 | |
| 61 | virtual const char* getName() const { |
| 62 | return "nil argument"; |
| 63 | } |
| 64 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 65 | class Report : public BugReport { |
| 66 | std::string Msg; |
| 67 | const char* s; |
| 68 | SourceRange R; |
| 69 | public: |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 71 | Report(NilArg& Desc, ExplodedNode<ValueState>* N, |
| 72 | ObjCMessageExpr* ME, unsigned Arg) |
| 73 | : BugReport(Desc, N) { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 74 | |
| 75 | Expr* E = ME->getArg(Arg); |
| 76 | R = E->getSourceRange(); |
| 77 | |
| 78 | std::ostringstream os; |
| 79 | |
| 80 | os << "Argument to '" << GetReceiverNameType(ME) << "' method '" |
| 81 | << ME->getSelector().getName() << "' cannot be nil."; |
| 82 | |
| 83 | Msg = os.str(); |
| 84 | s = Msg.c_str(); |
| 85 | } |
| 86 | |
| 87 | virtual ~Report() {} |
| 88 | |
Ted Kremenek | 4d35dac | 2008-04-10 16:05:13 +0000 | [diff] [blame] | 89 | virtual const char* getDescription() const { return s; } |
| 90 | |
| 91 | virtual void getRanges(const SourceRange*& B, const SourceRange*& E) const { |
| 92 | B = &R; |
| 93 | E = B+1; |
| 94 | } |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 95 | }; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 96 | }; |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 98 | |
| 99 | class VISIBILITY_HIDDEN BasicObjCFoundationChecks : public GRSimpleAPICheck { |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 100 | NilArg Desc; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 101 | ASTContext &Ctx; |
| 102 | ValueStateManager* VMgr; |
| 103 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 104 | typedef std::vector<BugReport*> ErrorsTy; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 105 | ErrorsTy Errors; |
| 106 | |
| 107 | RVal GetRVal(ValueState* St, Expr* E) { return VMgr->GetRVal(St, E); } |
| 108 | |
| 109 | bool isNSString(ObjCInterfaceType* T, const char* suffix); |
| 110 | bool AuditNSString(NodeTy* N, ObjCMessageExpr* ME); |
| 111 | |
| 112 | void Warn(NodeTy* N, Expr* E, const std::string& s); |
| 113 | void WarnNilArg(NodeTy* N, Expr* E); |
| 114 | |
| 115 | bool CheckNilArg(NodeTy* N, unsigned Arg); |
| 116 | |
| 117 | public: |
| 118 | BasicObjCFoundationChecks(ASTContext& ctx, ValueStateManager* vmgr) |
| 119 | : Ctx(ctx), VMgr(vmgr) {} |
| 120 | |
| 121 | virtual ~BasicObjCFoundationChecks() { |
| 122 | for (ErrorsTy::iterator I = Errors.begin(), E = Errors.end(); I!=E; ++I) |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 123 | delete *I; |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | virtual bool Audit(ExplodedNode<ValueState>* N); |
| 127 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 128 | virtual void EmitWarnings(BugReporter& BR); |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 132 | void AddError(BugReport* R) { |
| 133 | Errors.push_back(R); |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void WarnNilArg(NodeTy* N, ObjCMessageExpr* ME, unsigned Arg) { |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 137 | AddError(new NilArg::Report(Desc, N, ME, Arg)); |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 138 | } |
| 139 | }; |
| 140 | |
| 141 | } // end anonymous namespace |
| 142 | |
| 143 | |
| 144 | GRSimpleAPICheck* |
| 145 | clang::CreateBasicObjCFoundationChecks(ASTContext& Ctx, |
| 146 | ValueStateManager* VMgr) { |
| 147 | |
| 148 | return new BasicObjCFoundationChecks(Ctx, VMgr); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 153 | bool BasicObjCFoundationChecks::Audit(ExplodedNode<ValueState>* N) { |
| 154 | |
| 155 | ObjCMessageExpr* ME = |
| 156 | cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt()); |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 158 | ObjCInterfaceType* ReceiverType = GetReceiverType(ME); |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 159 | |
| 160 | if (!ReceiverType) |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 161 | return NULL; |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 163 | const char* name = ReceiverType->getDecl()->getIdentifier()->getName(); |
| 164 | |
| 165 | if (!name) |
| 166 | return false; |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 167 | |
| 168 | if (name[0] != 'N' || name[1] != 'S') |
| 169 | return false; |
| 170 | |
| 171 | name += 2; |
| 172 | |
| 173 | // FIXME: Make all of this faster. |
| 174 | |
| 175 | if (isNSString(ReceiverType, name)) |
| 176 | return AuditNSString(N, ME); |
| 177 | |
| 178 | return false; |
| 179 | } |
| 180 | |
Ted Kremenek | e5d5c20 | 2008-03-27 21:15:17 +0000 | [diff] [blame] | 181 | static inline bool isNil(RVal X) { |
| 182 | return isa<lval::ConcreteInt>(X); |
| 183 | } |
| 184 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 185 | //===----------------------------------------------------------------------===// |
| 186 | // Error reporting. |
| 187 | //===----------------------------------------------------------------------===// |
| 188 | |
| 189 | |
Ted Kremenek | 50a6d0c | 2008-04-09 21:41:14 +0000 | [diff] [blame] | 190 | void BasicObjCFoundationChecks::EmitWarnings(BugReporter& BR) { |
| 191 | |
Ted Kremenek | d2f642b | 2008-04-14 17:39:48 +0000 | [diff] [blame] | 192 | for (ErrorsTy::iterator I=Errors.begin(), E=Errors.end(); I!=E; ++I) |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame^] | 193 | BR.EmitWarning(**I); |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | bool BasicObjCFoundationChecks::CheckNilArg(NodeTy* N, unsigned Arg) { |
| 197 | ObjCMessageExpr* ME = |
| 198 | cast<ObjCMessageExpr>(cast<PostStmt>(N->getLocation()).getStmt()); |
| 199 | |
| 200 | Expr * E = ME->getArg(Arg); |
| 201 | |
| 202 | if (isNil(GetRVal(N->getState(), E))) { |
Ted Kremenek | f1ae705 | 2008-04-03 17:57:38 +0000 | [diff] [blame] | 203 | WarnNilArg(N, ME, Arg); |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | |
| 207 | return false; |
| 208 | } |
| 209 | |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 210 | //===----------------------------------------------------------------------===// |
| 211 | // NSString checking. |
| 212 | //===----------------------------------------------------------------------===// |
| 213 | |
| 214 | bool BasicObjCFoundationChecks::isNSString(ObjCInterfaceType* T, |
| 215 | const char* suffix) { |
| 216 | |
| 217 | return !strcmp("String", suffix) || !strcmp("MutableString", suffix); |
| 218 | } |
| 219 | |
| 220 | bool BasicObjCFoundationChecks::AuditNSString(NodeTy* N, |
| 221 | ObjCMessageExpr* ME) { |
| 222 | |
| 223 | Selector S = ME->getSelector(); |
| 224 | |
| 225 | if (S.isUnarySelector()) |
| 226 | return false; |
| 227 | |
| 228 | // FIXME: This is going to be really slow doing these checks with |
| 229 | // lexical comparisons. |
| 230 | |
| 231 | std::string name = S.getName(); |
Ted Kremenek | 9b3fdea | 2008-03-27 21:23:57 +0000 | [diff] [blame] | 232 | assert (!name.empty()); |
| 233 | const char* cstr = &name[0]; |
| 234 | unsigned len = name.size(); |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 235 | |
Ted Kremenek | 9b3fdea | 2008-03-27 21:23:57 +0000 | [diff] [blame] | 236 | switch (len) { |
| 237 | default: |
| 238 | break; |
Ted Kremenek | 8730e13 | 2008-03-28 16:09:38 +0000 | [diff] [blame] | 239 | case 8: |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 240 | if (!strcmp(cstr, "compare:")) |
| 241 | return CheckNilArg(N, 0); |
| 242 | |
| 243 | break; |
Ted Kremenek | 8730e13 | 2008-03-28 16:09:38 +0000 | [diff] [blame] | 244 | |
| 245 | case 15: |
| 246 | // FIXME: Checking for initWithFormat: will not work in most cases |
| 247 | // yet because [NSString alloc] returns id, not NSString*. We will |
| 248 | // need support for tracking expected-type information in the analyzer |
| 249 | // to find these errors. |
| 250 | if (!strcmp(cstr, "initWithFormat:")) |
| 251 | return CheckNilArg(N, 0); |
| 252 | |
| 253 | break; |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 254 | |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 255 | case 16: |
| 256 | if (!strcmp(cstr, "compare:options:")) |
| 257 | return CheckNilArg(N, 0); |
Ted Kremenek | 9b3fdea | 2008-03-27 21:23:57 +0000 | [diff] [blame] | 258 | |
| 259 | break; |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 260 | |
| 261 | case 22: |
| 262 | if (!strcmp(cstr, "compare:options:range:")) |
| 263 | return CheckNilArg(N, 0); |
| 264 | |
| 265 | break; |
| 266 | |
| 267 | case 23: |
| 268 | |
| 269 | if (!strcmp(cstr, "caseInsensitiveCompare:")) |
| 270 | return CheckNilArg(N, 0); |
| 271 | |
| 272 | break; |
Ted Kremenek | 8730e13 | 2008-03-28 16:09:38 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | 4ba6283 | 2008-03-27 22:05:32 +0000 | [diff] [blame] | 274 | case 29: |
| 275 | if (!strcmp(cstr, "compare:options:range:locale:")) |
| 276 | return CheckNilArg(N, 0); |
| 277 | |
| 278 | break; |
| 279 | |
| 280 | case 37: |
| 281 | if (!strcmp(cstr, "componentsSeparatedByCharactersInSet:")) |
| 282 | return CheckNilArg(N, 0); |
| 283 | |
| 284 | break; |
Ted Kremenek | 99c6ad3 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | return false; |
| 288 | } |