blob: 98d2a85ace36452f274658d157f5f41ac245ae68 [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"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000040#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000041#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rosef540c542012-07-26 21:39:41 +000042#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000043#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000044#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000045#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000046#include "clang/AST/ParentMap.h"
47
48using namespace clang;
49using namespace ento;
50
51static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND);
52static bool isInitializationMethod(const ObjCMethodDecl *MD);
Jordan Rosede507ea2012-07-02 19:28:04 +000053static bool isInitMessage(const ObjCMethodCall &Msg);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000054static bool isSelfVar(SVal location, CheckerContext &C);
55
56namespace {
Jordan Rosefe6a0112012-07-02 19:28:21 +000057class ObjCSelfInitChecker : public Checker< check::PostObjCMessage,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000058 check::PostStmt<ObjCIvarRefExpr>,
59 check::PreStmt<ReturnStmt>,
Jordan Rosefe6a0112012-07-02 19:28:21 +000060 check::PreCall,
61 check::PostCall,
Anna Zaks6a2a1862012-05-08 21:19:21 +000062 check::Location,
63 check::Bind > {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000064public:
Jordan Rosede507ea2012-07-02 19:28:04 +000065 void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const;
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000066 void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const;
67 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Anna Zaks390909c2011-10-06 00:43:15 +000068 void checkLocation(SVal location, bool isLoad, const Stmt *S,
69 CheckerContext &C) const;
Anna Zaks6a2a1862012-05-08 21:19:21 +000070 void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
Anna Zaksf420fe32012-03-05 18:58:25 +000071
Jordan Rosefe6a0112012-07-02 19:28:21 +000072 void checkPreCall(const CallEvent &CE, CheckerContext &C) const;
73 void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
Anna Zaksf420fe32012-03-05 18:58:25 +000074
Jordan Rosea435e692012-09-08 01:47:11 +000075 void printState(raw_ostream &Out, ProgramStateRef State,
76 const char *NL, const char *Sep) const;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000077};
78} // end anonymous namespace
79
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000080namespace {
81
82class InitSelfBug : public BugType {
83 const std::string desc;
84public:
Anna Zaks1efcc422012-02-04 02:31:37 +000085 InitSelfBug() : BugType("Missing \"self = [(super or self) init...]\"",
Ted Kremenek6fd45052012-04-05 20:43:28 +000086 categories::CoreFoundationObjectiveC) {}
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000087};
88
89} // end anonymous namespace
90
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000091namespace {
92enum SelfFlagEnum {
93 /// \brief No flag set.
94 SelfFlag_None = 0x0,
95 /// \brief Value came from 'self'.
96 SelfFlag_Self = 0x1,
97 /// \brief Value came from the result of an initializer (e.g. [super init]).
98 SelfFlag_InitRes = 0x2
99};
100}
101
Jordan Rose466224f2012-11-02 01:54:42 +0000102REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned)
103REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool)
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000104
Jordan Rose466224f2012-11-02 01:54:42 +0000105/// \brief A call receiving a reference to 'self' invalidates the object that
106/// 'self' contains. This keeps the "self flags" assigned to the 'self'
107/// object before the call so we can assign them to the new object that 'self'
108/// points to after the call.
109REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned)
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000110
Ted Kremenek8bef8232012-01-26 21:29:00 +0000111static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000112 if (SymbolRef sym = val.getAsSymbol())
113 if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
114 return (SelfFlagEnum)*attachedFlags;
115 return SelfFlag_None;
116}
117
118static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
119 return getSelfFlags(val, C.getState());
120}
121
Ted Kremenek8bef8232012-01-26 21:29:00 +0000122static void addSelfFlag(ProgramStateRef state, SVal val,
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000123 SelfFlagEnum flag, CheckerContext &C) {
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000124 // We tag the symbol that the SVal wraps.
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000125 if (SymbolRef sym = val.getAsSymbol())
Jordan Rose82f2ad42012-09-08 01:47:28 +0000126 state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
127 C.addTransition(state);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000128}
129
130static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
131 return getSelfFlags(val, C) & flag;
132}
133
134/// \brief Returns true of the value of the expression is the object that 'self'
135/// points to and is an object that did not come from the result of calling
136/// an initializer.
137static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000138 SVal exprVal = C.getState()->getSVal(E, C.getLocationContext());
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000139 if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
140 return false; // value did not come from 'self'.
141 if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
142 return false; // 'self' is properly initialized.
143
144 return true;
145}
146
147static void checkForInvalidSelf(const Expr *E, CheckerContext &C,
148 const char *errorStr) {
149 if (!E)
150 return;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000151
152 if (!C.getState()->get<CalledInit>())
153 return;
154
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000155 if (!isInvalidSelf(E, C))
156 return;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000157
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000158 // Generate an error node.
159 ExplodedNode *N = C.generateSink();
160 if (!N)
161 return;
162
Anna Zakse172e8b2011-08-17 23:00:25 +0000163 BugReport *report =
164 new BugReport(*new InitSelfBug(), errorStr, N);
Jordan Rose785950e2012-11-02 01:53:40 +0000165 C.emitReport(report);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000166}
167
Jordan Rosede507ea2012-07-02 19:28:04 +0000168void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000169 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000170 // When encountering a message that does initialization (init rule),
171 // tag the return value so that we know later on that if self has this value
172 // then it is properly initialized.
173
174 // FIXME: A callback should disable checkers at the start of functions.
175 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks1efcc422012-02-04 02:31:37 +0000176 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000177 return;
178
Jordan Rosede507ea2012-07-02 19:28:04 +0000179 if (isInitMessage(Msg)) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000180 // Tag the return value as the result of an initializer.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000181 ProgramStateRef state = C.getState();
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000182
183 // FIXME this really should be context sensitive, where we record
184 // the current stack frame (for IPA). Also, we need to clean this
185 // value out when we return from this method.
186 state = state->set<CalledInit>(true);
187
Jordan Rosede507ea2012-07-02 19:28:04 +0000188 SVal V = state->getSVal(Msg.getOriginExpr(), C.getLocationContext());
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000189 addSelfFlag(state, V, SelfFlag_InitRes, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000190 return;
191 }
192
193 // We don't check for an invalid 'self' in an obj-c message expression to cut
194 // down false positives where logging functions get information from self
195 // (like its class) or doing "invalidation" on self when the initialization
196 // fails.
197}
198
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000199void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E,
200 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000201 // FIXME: A callback should disable checkers at the start of functions.
202 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks1efcc422012-02-04 02:31:37 +0000203 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000204 return;
205
206 checkForInvalidSelf(E->getBase(), C,
Argyrios Kyrtzidisbe29d8d2011-02-01 19:32:55 +0000207 "Instance variable used while 'self' is not set to the result of "
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000208 "'[(super or self) init...]'");
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000209}
210
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000211void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
212 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000213 // FIXME: A callback should disable checkers at the start of functions.
214 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks1efcc422012-02-04 02:31:37 +0000215 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000216 return;
217
218 checkForInvalidSelf(S->getRetValue(), C,
Argyrios Kyrtzidis63eeade2011-02-01 20:33:05 +0000219 "Returning 'self' while it is not set to the result of "
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000220 "'[(super or self) init...]'");
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000221}
222
Jordan Rosefe6a0112012-07-02 19:28:21 +0000223// When a call receives a reference to 'self', [Pre/Post]Call pass
224// the SelfFlags from the object 'self' points to before the call to the new
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000225// object after the call. This is to avoid invalidation of 'self' by logging
226// functions.
227// Another common pattern in classes with multiple initializers is to put the
228// subclass's common initialization bits into a static function that receives
229// the value of 'self', e.g:
230// @code
231// if (!(self = [super init]))
232// return nil;
233// if (!(self = _commonInit(self)))
234// return nil;
235// @endcode
236// Until we can use inter-procedural analysis, in such a call, transfer the
237// SelfFlags to the result of the call.
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000238
Jordan Rosefe6a0112012-07-02 19:28:21 +0000239void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000240 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +0000241 // FIXME: A callback should disable checkers at the start of functions.
242 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
243 C.getCurrentAnalysisDeclContext()->getDecl())))
244 return;
Jordan Rose55037cd2012-07-02 19:27:46 +0000245
Ted Kremenek8bef8232012-01-26 21:29:00 +0000246 ProgramStateRef state = C.getState();
Anna Zaksf420fe32012-03-05 18:58:25 +0000247 unsigned NumArgs = CE.getNumArgs();
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000248 // If we passed 'self' as and argument to the call, record it in the state
249 // to be propagated after the call.
250 // Note, we could have just given up, but try to be more optimistic here and
251 // assume that the functions are going to continue initialization or will not
252 // modify self.
Anna Zaksf420fe32012-03-05 18:58:25 +0000253 for (unsigned i = 0; i < NumArgs; ++i) {
254 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000255 if (isSelfVar(argV, C)) {
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000256 unsigned selfFlags = getSelfFlags(state->getSVal(cast<Loc>(argV)), C);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000257 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000258 return;
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000259 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000260 unsigned selfFlags = getSelfFlags(argV, C);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000261 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000262 return;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000263 }
264 }
265}
266
Jordan Rosefe6a0112012-07-02 19:28:21 +0000267void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000268 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +0000269 // FIXME: A callback should disable checkers at the start of functions.
270 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
271 C.getCurrentAnalysisDeclContext()->getDecl())))
272 return;
273
Ted Kremenek8bef8232012-01-26 21:29:00 +0000274 ProgramStateRef state = C.getState();
Jordan Rosefe6a0112012-07-02 19:28:21 +0000275 SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
276 if (!prevFlags)
277 return;
278 state = state->remove<PreCallSelfFlags>();
279
Anna Zaksf420fe32012-03-05 18:58:25 +0000280 unsigned NumArgs = CE.getNumArgs();
281 for (unsigned i = 0; i < NumArgs; ++i) {
282 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000283 if (isSelfVar(argV, C)) {
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000284 // If the address of 'self' is being passed to the call, assume that the
285 // 'self' after the call will have the same flags.
286 // EX: log(&self)
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000287 addSelfFlag(state, state->getSVal(cast<Loc>(argV)), prevFlags, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000288 return;
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000289 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000290 // If 'self' is passed to the call by value, assume that the function
291 // returns 'self'. So assign the flags, which were set on 'self' to the
292 // return value.
293 // EX: self = performMoreInitialization(self)
Jordan Rose2f3017f2012-11-02 23:49:29 +0000294 addSelfFlag(state, CE.getReturnValue(), prevFlags, C);
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000295 return;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000296 }
297 }
Jordan Rose2f3017f2012-11-02 23:49:29 +0000298
299 C.addTransition(state);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000300}
301
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000302void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
Anna Zaks390909c2011-10-06 00:43:15 +0000303 const Stmt *S,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000304 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000305 // Tag the result of a load from 'self' so that we can easily know that the
306 // value is the object that 'self' points to.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000307 ProgramStateRef state = C.getState();
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000308 if (isSelfVar(location, C))
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000309 addSelfFlag(state, state->getSVal(cast<Loc>(location)), SelfFlag_Self, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000310}
311
Anna Zaks6a2a1862012-05-08 21:19:21 +0000312
313void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
314 CheckerContext &C) const {
315 // Allow assignment of anything to self. Self is a local variable in the
316 // initializer, so it is legal to assign anything to it, like results of
317 // static functions/method calls. After self is assigned something we cannot
318 // reason about, stop enforcing the rules.
319 // (Only continue checking if the assigned value should be treated as self.)
320 if ((isSelfVar(loc, C)) &&
321 !hasSelfFlag(val, SelfFlag_InitRes, C) &&
322 !hasSelfFlag(val, SelfFlag_Self, C) &&
323 !isSelfVar(val, C)) {
324
325 // Stop tracking the checker-specific state in the state.
326 ProgramStateRef State = C.getState();
327 State = State->remove<CalledInit>();
328 if (SymbolRef sym = loc.getAsSymbol())
329 State = State->remove<SelfFlag>(sym);
330 C.addTransition(State);
331 }
332}
333
Jordan Rosea435e692012-09-08 01:47:11 +0000334void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State,
335 const char *NL, const char *Sep) const {
Jordan Rose466224f2012-11-02 01:54:42 +0000336 SelfFlagTy FlagMap = State->get<SelfFlag>();
Jordan Rosea435e692012-09-08 01:47:11 +0000337 bool DidCallInit = State->get<CalledInit>();
338 SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>();
339
340 if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
341 return;
342
343 Out << Sep << NL << "ObjCSelfInitChecker:" << NL;
344
345 if (DidCallInit)
346 Out << " An init method has been called." << NL;
347
348 if (PreCallFlags != SelfFlag_None) {
349 if (PreCallFlags & SelfFlag_Self) {
350 Out << " An argument of the current call came from the 'self' variable."
351 << NL;
352 }
353 if (PreCallFlags & SelfFlag_InitRes) {
354 Out << " An argument of the current call came from an init method."
355 << NL;
356 }
357 }
358
359 Out << NL;
Jordan Rose466224f2012-11-02 01:54:42 +0000360 for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end();
361 I != E; ++I) {
Jordan Rosea435e692012-09-08 01:47:11 +0000362 Out << I->first << " : ";
363
364 if (I->second == SelfFlag_None)
365 Out << "none";
366
367 if (I->second & SelfFlag_Self)
368 Out << "self variable";
369
370 if (I->second & SelfFlag_InitRes) {
371 if (I->second != SelfFlag_InitRes)
372 Out << " | ";
373 Out << "result of init method";
374 }
375
376 Out << NL;
377 }
378}
379
380
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000381// FIXME: A callback should disable checkers at the start of functions.
382static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
383 if (!ND)
384 return false;
385
386 const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
387 if (!MD)
388 return false;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000389 if (!isInitializationMethod(MD))
390 return false;
391
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000392 // self = [super init] applies only to NSObject subclasses.
393 // For instance, NSProxy doesn't implement -init.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000394 ASTContext &Ctx = MD->getASTContext();
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000395 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenek9c378f72011-08-12 23:37:29 +0000396 ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass();
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000397 for ( ; ID ; ID = ID->getSuperClass()) {
398 IdentifierInfo *II = ID->getIdentifier();
399
400 if (II == NSObjectII)
401 break;
402 }
403 if (!ID)
404 return false;
405
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000406 return true;
407}
408
409/// \brief Returns true if the location is 'self'.
410static bool isSelfVar(SVal location, CheckerContext &C) {
Ted Kremenek1d26f482011-10-24 01:32:45 +0000411 AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000412 if (!analCtx->getSelfDecl())
413 return false;
414 if (!isa<loc::MemRegionVal>(location))
415 return false;
416
417 loc::MemRegionVal MRV = cast<loc::MemRegionVal>(location);
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000418 if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts()))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000419 return (DR->getDecl() == analCtx->getSelfDecl());
420
421 return false;
422}
423
424static bool isInitializationMethod(const ObjCMethodDecl *MD) {
John McCall85f3d762011-03-02 01:50:55 +0000425 return MD->getMethodFamily() == OMF_init;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000426}
427
Jordan Rosede507ea2012-07-02 19:28:04 +0000428static bool isInitMessage(const ObjCMethodCall &Call) {
429 return Call.getMethodFamily() == OMF_init;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000430}
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000431
432//===----------------------------------------------------------------------===//
433// Registration.
434//===----------------------------------------------------------------------===//
435
436void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
437 mgr.registerChecker<ObjCSelfInitChecker>();
438}