blob: 485ae77242afadaf42fb2bca20d531b29c6c11b8 [file] [log] [blame]
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +00001//== ObjCSelfInitChecker.cpp - Checker for 'self' initialization -*- 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 defines ObjCSelfInitChecker, a builtin check that checks for uses of
11// 'self' before proper initialization.
12//
13//===----------------------------------------------------------------------===//
14
15// This checks initialization methods to verify that they assign 'self' to the
16// result of an initialization call (e.g. [super init], or [self initWith..])
17// before using 'self' or any instance variable.
18//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000019// To perform the required checking, values are tagged with flags that indicate
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000020// 1) if the object is the one pointed to by 'self', and 2) if the object
21// is the result of an initializer (e.g. [super init]).
22//
23// Uses of an object that is true for 1) but not 2) trigger a diagnostic.
24// The uses that are currently checked are:
25// - Using instance variables.
26// - Returning the object.
27//
28// Note that we don't check for an invalid 'self' that is the receiver of an
29// obj-c message expression to cut down false positives where logging functions
30// get information from self (like its class) or doing "invalidation" on self
31// when the initialization fails.
32//
33// Because the object that 'self' points to gets invalidated when a call
34// receives a reference to 'self', the checker keeps track and passes the flags
35// for 1) and 2) to the new object that 'self' points to after the call.
36//
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000037//===----------------------------------------------------------------------===//
38
Argyrios Kyrtzidis027a6ab2011-02-15 07:42:33 +000039#include "ClangSACheckers.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000040#include "clang/AST/ParentMap.h"
41#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000042#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000043#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rosef540c542012-07-26 21:39:41 +000044#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000045#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000046#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Benjamin Kramer9d5a78d2012-12-01 17:54:07 +000047#include "llvm/Support/raw_ostream.h"
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000048
49using namespace clang;
50using namespace ento;
51
52static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND);
53static bool isInitializationMethod(const ObjCMethodDecl *MD);
Jordan Rosede507ea2012-07-02 19:28:04 +000054static bool isInitMessage(const ObjCMethodCall &Msg);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000055static bool isSelfVar(SVal location, CheckerContext &C);
56
57namespace {
Jordan Rosefe6a0112012-07-02 19:28:21 +000058class ObjCSelfInitChecker : public Checker< check::PostObjCMessage,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000059 check::PostStmt<ObjCIvarRefExpr>,
60 check::PreStmt<ReturnStmt>,
Jordan Rosefe6a0112012-07-02 19:28:21 +000061 check::PreCall,
62 check::PostCall,
Anna Zaks6a2a1862012-05-08 21:19:21 +000063 check::Location,
64 check::Bind > {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000065public:
Jordan Rosede507ea2012-07-02 19:28:04 +000066 void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const;
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000067 void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const;
68 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Anna Zaks390909c2011-10-06 00:43:15 +000069 void checkLocation(SVal location, bool isLoad, const Stmt *S,
70 CheckerContext &C) const;
Anna Zaks6a2a1862012-05-08 21:19:21 +000071 void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
Anna Zaksf420fe32012-03-05 18:58:25 +000072
Jordan Rosefe6a0112012-07-02 19:28:21 +000073 void checkPreCall(const CallEvent &CE, CheckerContext &C) const;
74 void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
Anna Zaksf420fe32012-03-05 18:58:25 +000075
Jordan Rosea435e692012-09-08 01:47:11 +000076 void printState(raw_ostream &Out, ProgramStateRef State,
77 const char *NL, const char *Sep) const;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000078};
79} // end anonymous namespace
80
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000081namespace {
82
83class InitSelfBug : public BugType {
84 const std::string desc;
85public:
Anna Zaks1efcc422012-02-04 02:31:37 +000086 InitSelfBug() : BugType("Missing \"self = [(super or self) init...]\"",
Ted Kremenek6fd45052012-04-05 20:43:28 +000087 categories::CoreFoundationObjectiveC) {}
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000088};
89
90} // end anonymous namespace
91
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000092namespace {
93enum SelfFlagEnum {
94 /// \brief No flag set.
95 SelfFlag_None = 0x0,
96 /// \brief Value came from 'self'.
97 SelfFlag_Self = 0x1,
98 /// \brief Value came from the result of an initializer (e.g. [super init]).
99 SelfFlag_InitRes = 0x2
100};
101}
102
Jordan Rose466224f2012-11-02 01:54:42 +0000103REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned)
104REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool)
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000105
Jordan Rose466224f2012-11-02 01:54:42 +0000106/// \brief A call receiving a reference to 'self' invalidates the object that
107/// 'self' contains. This keeps the "self flags" assigned to the 'self'
108/// object before the call so we can assign them to the new object that 'self'
109/// points to after the call.
110REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned)
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000111
Ted Kremenek8bef8232012-01-26 21:29:00 +0000112static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000113 if (SymbolRef sym = val.getAsSymbol())
114 if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
115 return (SelfFlagEnum)*attachedFlags;
116 return SelfFlag_None;
117}
118
119static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
120 return getSelfFlags(val, C.getState());
121}
122
Ted Kremenek8bef8232012-01-26 21:29:00 +0000123static void addSelfFlag(ProgramStateRef state, SVal val,
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000124 SelfFlagEnum flag, CheckerContext &C) {
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000125 // We tag the symbol that the SVal wraps.
Anna Zaks18126522012-12-13 00:42:19 +0000126 if (SymbolRef sym = val.getAsSymbol()) {
Jordan Rose82f2ad42012-09-08 01:47:28 +0000127 state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
Anna Zaks18126522012-12-13 00:42:19 +0000128 C.addTransition(state);
129 }
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000130}
131
132static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
133 return getSelfFlags(val, C) & flag;
134}
135
136/// \brief Returns true of the value of the expression is the object that 'self'
137/// points to and is an object that did not come from the result of calling
138/// an initializer.
139static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000140 SVal exprVal = C.getState()->getSVal(E, C.getLocationContext());
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000141 if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
142 return false; // value did not come from 'self'.
143 if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
144 return false; // 'self' is properly initialized.
145
146 return true;
147}
148
149static void checkForInvalidSelf(const Expr *E, CheckerContext &C,
150 const char *errorStr) {
151 if (!E)
152 return;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000153
154 if (!C.getState()->get<CalledInit>())
155 return;
156
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000157 if (!isInvalidSelf(E, C))
158 return;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000159
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000160 // Generate an error node.
161 ExplodedNode *N = C.generateSink();
162 if (!N)
163 return;
164
Anna Zakse172e8b2011-08-17 23:00:25 +0000165 BugReport *report =
166 new BugReport(*new InitSelfBug(), errorStr, N);
Jordan Rose785950e2012-11-02 01:53:40 +0000167 C.emitReport(report);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000168}
169
Jordan Rosede507ea2012-07-02 19:28:04 +0000170void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000171 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000172 // When encountering a message that does initialization (init rule),
173 // tag the return value so that we know later on that if self has this value
174 // then it is properly initialized.
175
176 // FIXME: A callback should disable checkers at the start of functions.
177 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks1efcc422012-02-04 02:31:37 +0000178 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000179 return;
180
Jordan Rosede507ea2012-07-02 19:28:04 +0000181 if (isInitMessage(Msg)) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000182 // Tag the return value as the result of an initializer.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000183 ProgramStateRef state = C.getState();
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000184
185 // FIXME this really should be context sensitive, where we record
186 // the current stack frame (for IPA). Also, we need to clean this
187 // value out when we return from this method.
188 state = state->set<CalledInit>(true);
189
Jordan Rosede507ea2012-07-02 19:28:04 +0000190 SVal V = state->getSVal(Msg.getOriginExpr(), C.getLocationContext());
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000191 addSelfFlag(state, V, SelfFlag_InitRes, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000192 return;
193 }
194
195 // We don't check for an invalid 'self' in an obj-c message expression to cut
196 // down false positives where logging functions get information from self
197 // (like its class) or doing "invalidation" on self when the initialization
198 // fails.
199}
200
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000201void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E,
202 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000203 // FIXME: A callback should disable checkers at the start of functions.
204 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks1efcc422012-02-04 02:31:37 +0000205 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000206 return;
207
208 checkForInvalidSelf(E->getBase(), C,
Argyrios Kyrtzidisbe29d8d2011-02-01 19:32:55 +0000209 "Instance variable used while 'self' is not set to the result of "
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000210 "'[(super or self) init...]'");
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000211}
212
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000213void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
214 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000215 // FIXME: A callback should disable checkers at the start of functions.
216 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks1efcc422012-02-04 02:31:37 +0000217 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000218 return;
219
220 checkForInvalidSelf(S->getRetValue(), C,
Argyrios Kyrtzidis63eeade2011-02-01 20:33:05 +0000221 "Returning 'self' while it is not set to the result of "
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000222 "'[(super or self) init...]'");
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000223}
224
Jordan Rosefe6a0112012-07-02 19:28:21 +0000225// When a call receives a reference to 'self', [Pre/Post]Call pass
226// the SelfFlags from the object 'self' points to before the call to the new
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000227// object after the call. This is to avoid invalidation of 'self' by logging
228// functions.
229// Another common pattern in classes with multiple initializers is to put the
230// subclass's common initialization bits into a static function that receives
231// the value of 'self', e.g:
232// @code
233// if (!(self = [super init]))
234// return nil;
235// if (!(self = _commonInit(self)))
236// return nil;
237// @endcode
238// Until we can use inter-procedural analysis, in such a call, transfer the
239// SelfFlags to the result of the call.
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000240
Jordan Rosefe6a0112012-07-02 19:28:21 +0000241void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000242 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +0000243 // FIXME: A callback should disable checkers at the start of functions.
244 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
245 C.getCurrentAnalysisDeclContext()->getDecl())))
246 return;
Jordan Rose55037cd2012-07-02 19:27:46 +0000247
Ted Kremenek8bef8232012-01-26 21:29:00 +0000248 ProgramStateRef state = C.getState();
Anna Zaksf420fe32012-03-05 18:58:25 +0000249 unsigned NumArgs = CE.getNumArgs();
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000250 // If we passed 'self' as and argument to the call, record it in the state
251 // to be propagated after the call.
252 // Note, we could have just given up, but try to be more optimistic here and
253 // assume that the functions are going to continue initialization or will not
254 // modify self.
Anna Zaksf420fe32012-03-05 18:58:25 +0000255 for (unsigned i = 0; i < NumArgs; ++i) {
256 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000257 if (isSelfVar(argV, C)) {
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000258 unsigned selfFlags = getSelfFlags(state->getSVal(cast<Loc>(argV)), C);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000259 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000260 return;
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000261 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000262 unsigned selfFlags = getSelfFlags(argV, C);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000263 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000264 return;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000265 }
266 }
267}
268
Jordan Rosefe6a0112012-07-02 19:28:21 +0000269void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000270 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +0000271 // FIXME: A callback should disable checkers at the start of functions.
272 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
273 C.getCurrentAnalysisDeclContext()->getDecl())))
274 return;
275
Ted Kremenek8bef8232012-01-26 21:29:00 +0000276 ProgramStateRef state = C.getState();
Jordan Rosefe6a0112012-07-02 19:28:21 +0000277 SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
278 if (!prevFlags)
279 return;
280 state = state->remove<PreCallSelfFlags>();
281
Anna Zaksf420fe32012-03-05 18:58:25 +0000282 unsigned NumArgs = CE.getNumArgs();
283 for (unsigned i = 0; i < NumArgs; ++i) {
284 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000285 if (isSelfVar(argV, C)) {
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000286 // If the address of 'self' is being passed to the call, assume that the
287 // 'self' after the call will have the same flags.
288 // EX: log(&self)
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000289 addSelfFlag(state, state->getSVal(cast<Loc>(argV)), prevFlags, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000290 return;
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000291 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000292 // If 'self' is passed to the call by value, assume that the function
293 // returns 'self'. So assign the flags, which were set on 'self' to the
294 // return value.
295 // EX: self = performMoreInitialization(self)
Jordan Rose2f3017f2012-11-02 23:49:29 +0000296 addSelfFlag(state, CE.getReturnValue(), prevFlags, C);
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000297 return;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000298 }
299 }
Jordan Rose2f3017f2012-11-02 23:49:29 +0000300
301 C.addTransition(state);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000302}
303
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000304void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
Anna Zaks390909c2011-10-06 00:43:15 +0000305 const Stmt *S,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000306 CheckerContext &C) const {
Anna Zaks18126522012-12-13 00:42:19 +0000307 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
308 C.getCurrentAnalysisDeclContext()->getDecl())))
309 return;
310
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000311 // Tag the result of a load from 'self' so that we can easily know that the
312 // value is the object that 'self' points to.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000313 ProgramStateRef state = C.getState();
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000314 if (isSelfVar(location, C))
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000315 addSelfFlag(state, state->getSVal(cast<Loc>(location)), SelfFlag_Self, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000316}
317
Anna Zaks6a2a1862012-05-08 21:19:21 +0000318
319void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
320 CheckerContext &C) const {
321 // Allow assignment of anything to self. Self is a local variable in the
322 // initializer, so it is legal to assign anything to it, like results of
323 // static functions/method calls. After self is assigned something we cannot
324 // reason about, stop enforcing the rules.
325 // (Only continue checking if the assigned value should be treated as self.)
326 if ((isSelfVar(loc, C)) &&
327 !hasSelfFlag(val, SelfFlag_InitRes, C) &&
328 !hasSelfFlag(val, SelfFlag_Self, C) &&
329 !isSelfVar(val, C)) {
330
331 // Stop tracking the checker-specific state in the state.
332 ProgramStateRef State = C.getState();
333 State = State->remove<CalledInit>();
334 if (SymbolRef sym = loc.getAsSymbol())
335 State = State->remove<SelfFlag>(sym);
336 C.addTransition(State);
337 }
338}
339
Jordan Rosea435e692012-09-08 01:47:11 +0000340void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State,
341 const char *NL, const char *Sep) const {
Jordan Rose466224f2012-11-02 01:54:42 +0000342 SelfFlagTy FlagMap = State->get<SelfFlag>();
Jordan Rosea435e692012-09-08 01:47:11 +0000343 bool DidCallInit = State->get<CalledInit>();
344 SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>();
345
346 if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
347 return;
348
349 Out << Sep << NL << "ObjCSelfInitChecker:" << NL;
350
351 if (DidCallInit)
352 Out << " An init method has been called." << NL;
353
354 if (PreCallFlags != SelfFlag_None) {
355 if (PreCallFlags & SelfFlag_Self) {
356 Out << " An argument of the current call came from the 'self' variable."
357 << NL;
358 }
359 if (PreCallFlags & SelfFlag_InitRes) {
360 Out << " An argument of the current call came from an init method."
361 << NL;
362 }
363 }
364
365 Out << NL;
Jordan Rose466224f2012-11-02 01:54:42 +0000366 for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end();
367 I != E; ++I) {
Jordan Rosea435e692012-09-08 01:47:11 +0000368 Out << I->first << " : ";
369
370 if (I->second == SelfFlag_None)
371 Out << "none";
372
373 if (I->second & SelfFlag_Self)
374 Out << "self variable";
375
376 if (I->second & SelfFlag_InitRes) {
377 if (I->second != SelfFlag_InitRes)
378 Out << " | ";
379 Out << "result of init method";
380 }
381
382 Out << NL;
383 }
384}
385
386
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000387// FIXME: A callback should disable checkers at the start of functions.
388static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
389 if (!ND)
390 return false;
391
392 const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
393 if (!MD)
394 return false;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000395 if (!isInitializationMethod(MD))
396 return false;
397
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000398 // self = [super init] applies only to NSObject subclasses.
399 // For instance, NSProxy doesn't implement -init.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000400 ASTContext &Ctx = MD->getASTContext();
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000401 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenek9c378f72011-08-12 23:37:29 +0000402 ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass();
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000403 for ( ; ID ; ID = ID->getSuperClass()) {
404 IdentifierInfo *II = ID->getIdentifier();
405
406 if (II == NSObjectII)
407 break;
408 }
409 if (!ID)
410 return false;
411
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000412 return true;
413}
414
415/// \brief Returns true if the location is 'self'.
416static bool isSelfVar(SVal location, CheckerContext &C) {
Ted Kremenek1d26f482011-10-24 01:32:45 +0000417 AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000418 if (!analCtx->getSelfDecl())
419 return false;
420 if (!isa<loc::MemRegionVal>(location))
421 return false;
422
423 loc::MemRegionVal MRV = cast<loc::MemRegionVal>(location);
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000424 if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts()))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000425 return (DR->getDecl() == analCtx->getSelfDecl());
426
427 return false;
428}
429
430static bool isInitializationMethod(const ObjCMethodDecl *MD) {
John McCall85f3d762011-03-02 01:50:55 +0000431 return MD->getMethodFamily() == OMF_init;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000432}
433
Jordan Rosede507ea2012-07-02 19:28:04 +0000434static bool isInitMessage(const ObjCMethodCall &Call) {
435 return Call.getMethodFamily() == OMF_init;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000436}
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000437
438//===----------------------------------------------------------------------===//
439// Registration.
440//===----------------------------------------------------------------------===//
441
442void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
443 mgr.registerChecker<ObjCSelfInitChecker>();
444}