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