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