Ted Kremenek | b0a2e47 | 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 | 7a68194 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 16 | #include "BasicObjCFoundationChecks.h" |
| 17 | |
Ted Kremenek | b0a2e47 | 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 | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 28 | #include <sstream> |
Ted Kremenek | b0a2e47 | 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 | 583c438 | 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 | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 41 | |
Ted Kremenek | 583c438 | 2008-03-27 21:15:17 +0000 | [diff] [blame] | 42 | RVal GetRVal(ValueState* St, Expr* E) { return VMgr->GetRVal(St, E); } |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 43 | |
Ted Kremenek | 583c438 | 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 | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 639247a | 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 | b0a2e47 | 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 | 583c438 | 2008-03-27 21:15:17 +0000 | [diff] [blame] | 59 | |
| 60 | virtual void ReportResults(Diagnostic& D); |
| 61 | |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | } // end anonymous namespace |
| 65 | |
| 66 | |
Ted Kremenek | 7a68194 | 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 | 639247a | 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 | 7a68194 | 2008-03-27 17:17:22 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | b0a2e47 | 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 | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 98 | ObjCInterfaceType* ReceiverType = GetReceiverType(ME); |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 99 | |
| 100 | if (!ReceiverType) |
Ted Kremenek | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 101 | return NULL; |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | 639247a | 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 | b0a2e47 | 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 | 583c438 | 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 | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 125 | //===----------------------------------------------------------------------===// |
| 126 | // Error reporting. |
| 127 | //===----------------------------------------------------------------------===// |
| 128 | |
| 129 | |
Ted Kremenek | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 130 | void BasicObjCFoundationChecks::Warn(NodeTy* N, Expr* E, const std::string& s) { |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 131 | Errors.push_back(AnnotatedPath<ValueState>()); |
Ted Kremenek | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 132 | Errors.back().push_back(N, s, E); |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Ted Kremenek | 583c438 | 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 | b20dccc | 2008-03-27 21:23:57 +0000 | [diff] [blame] | 151 | D.Report(L, diag, &AN.getString(), 1, &R, 1); |
Ted Kremenek | 583c438 | 2008-03-27 21:15:17 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
Ted Kremenek | 639247a | 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 | b0a2e47 | 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 | b20dccc | 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 | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 208 | |
Ted Kremenek | b20dccc | 2008-03-27 21:23:57 +0000 | [diff] [blame] | 209 | switch (len) { |
| 210 | default: |
| 211 | break; |
| 212 | case 8: |
Ted Kremenek | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 213 | |
| 214 | if (!strcmp(cstr, "compare:")) |
| 215 | return CheckNilArg(N, 0); |
| 216 | |
| 217 | break; |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 219 | case 16: |
| 220 | if (!strcmp(cstr, "compare:options:")) |
| 221 | return CheckNilArg(N, 0); |
Ted Kremenek | b20dccc | 2008-03-27 21:23:57 +0000 | [diff] [blame] | 222 | |
| 223 | break; |
Ted Kremenek | 639247a | 2008-03-27 22:05:32 +0000 | [diff] [blame^] | 224 | |
| 225 | case 22: |
| 226 | if (!strcmp(cstr, "compare:options:range:")) |
| 227 | return CheckNilArg(N, 0); |
| 228 | |
| 229 | break; |
| 230 | |
| 231 | case 23: |
| 232 | |
| 233 | if (!strcmp(cstr, "caseInsensitiveCompare:")) |
| 234 | return CheckNilArg(N, 0); |
| 235 | |
| 236 | break; |
| 237 | |
| 238 | case 29: |
| 239 | if (!strcmp(cstr, "compare:options:range:locale:")) |
| 240 | return CheckNilArg(N, 0); |
| 241 | |
| 242 | break; |
| 243 | |
| 244 | case 37: |
| 245 | if (!strcmp(cstr, "componentsSeparatedByCharactersInSet:")) |
| 246 | return CheckNilArg(N, 0); |
| 247 | |
| 248 | break; |
Ted Kremenek | b0a2e47 | 2008-03-27 07:25:52 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | return false; |
| 252 | } |