blob: 2bd3e9a0e03ac45d73a328c74e6caf7e7774b9e1 [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 Rose55037cd2012-07-02 19:27:46 +000042#include "clang/StaticAnalyzer/Core/PathSensitive/Calls.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
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000075};
76} // end anonymous namespace
77
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000078namespace {
79
80class InitSelfBug : public BugType {
81 const std::string desc;
82public:
Anna Zaks1efcc422012-02-04 02:31:37 +000083 InitSelfBug() : BugType("Missing \"self = [(super or self) init...]\"",
Ted Kremenek6fd45052012-04-05 20:43:28 +000084 categories::CoreFoundationObjectiveC) {}
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +000085};
86
87} // end anonymous namespace
88
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +000089namespace {
90enum SelfFlagEnum {
91 /// \brief No flag set.
92 SelfFlag_None = 0x0,
93 /// \brief Value came from 'self'.
94 SelfFlag_Self = 0x1,
95 /// \brief Value came from the result of an initializer (e.g. [super init]).
96 SelfFlag_InitRes = 0x2
97};
98}
99
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000100typedef llvm::ImmutableMap<SymbolRef, unsigned> SelfFlag;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000101namespace { struct CalledInit {}; }
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000102namespace { struct PreCallSelfFlags {}; }
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000103
104namespace clang {
105namespace ento {
106 template<>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000107 struct ProgramStateTrait<SelfFlag> : public ProgramStatePartialTrait<SelfFlag> {
Ted Kremenek9c378f72011-08-12 23:37:29 +0000108 static void *GDMIndex() { static int index = 0; return &index; }
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000109 };
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000110 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000111 struct ProgramStateTrait<CalledInit> : public ProgramStatePartialTrait<bool> {
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000112 static void *GDMIndex() { static int index = 0; return &index; }
113 };
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000114
115 /// \brief A call receiving a reference to 'self' invalidates the object that
116 /// 'self' contains. This keeps the "self flags" assigned to the 'self'
117 /// object before the call so we can assign them to the new object that 'self'
118 /// points to after the call.
119 template <>
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000120 struct ProgramStateTrait<PreCallSelfFlags> : public ProgramStatePartialTrait<unsigned> {
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000121 static void *GDMIndex() { static int index = 0; return &index; }
122 };
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000123}
124}
125
Ted Kremenek8bef8232012-01-26 21:29:00 +0000126static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000127 if (SymbolRef sym = val.getAsSymbol())
128 if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
129 return (SelfFlagEnum)*attachedFlags;
130 return SelfFlag_None;
131}
132
133static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
134 return getSelfFlags(val, C.getState());
135}
136
Ted Kremenek8bef8232012-01-26 21:29:00 +0000137static void addSelfFlag(ProgramStateRef state, SVal val,
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000138 SelfFlagEnum flag, CheckerContext &C) {
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000139 // We tag the symbol that the SVal wraps.
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000140 if (SymbolRef sym = val.getAsSymbol())
Anna Zaks0bd6b112011-10-26 21:06:34 +0000141 C.addTransition(state->set<SelfFlag>(sym, getSelfFlags(val, C) | flag));
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000142}
143
144static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
145 return getSelfFlags(val, C) & flag;
146}
147
148/// \brief Returns true of the value of the expression is the object that 'self'
149/// points to and is an object that did not come from the result of calling
150/// an initializer.
151static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
Ted Kremenek5eca4822012-01-06 22:09:28 +0000152 SVal exprVal = C.getState()->getSVal(E, C.getLocationContext());
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000153 if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
154 return false; // value did not come from 'self'.
155 if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
156 return false; // 'self' is properly initialized.
157
158 return true;
159}
160
161static void checkForInvalidSelf(const Expr *E, CheckerContext &C,
162 const char *errorStr) {
163 if (!E)
164 return;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000165
166 if (!C.getState()->get<CalledInit>())
167 return;
168
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000169 if (!isInvalidSelf(E, C))
170 return;
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000171
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000172 // Generate an error node.
173 ExplodedNode *N = C.generateSink();
174 if (!N)
175 return;
176
Anna Zakse172e8b2011-08-17 23:00:25 +0000177 BugReport *report =
178 new BugReport(*new InitSelfBug(), errorStr, N);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000179 C.EmitReport(report);
180}
181
Jordan Rosede507ea2012-07-02 19:28:04 +0000182void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000183 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000184 // When encountering a message that does initialization (init rule),
185 // tag the return value so that we know later on that if self has this value
186 // then it is properly initialized.
187
188 // FIXME: A callback should disable checkers at the start of functions.
189 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks1efcc422012-02-04 02:31:37 +0000190 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000191 return;
192
Jordan Rosede507ea2012-07-02 19:28:04 +0000193 if (isInitMessage(Msg)) {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000194 // Tag the return value as the result of an initializer.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000195 ProgramStateRef state = C.getState();
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000196
197 // FIXME this really should be context sensitive, where we record
198 // the current stack frame (for IPA). Also, we need to clean this
199 // value out when we return from this method.
200 state = state->set<CalledInit>(true);
201
Jordan Rosede507ea2012-07-02 19:28:04 +0000202 SVal V = state->getSVal(Msg.getOriginExpr(), C.getLocationContext());
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000203 addSelfFlag(state, V, SelfFlag_InitRes, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000204 return;
205 }
206
207 // We don't check for an invalid 'self' in an obj-c message expression to cut
208 // down false positives where logging functions get information from self
209 // (like its class) or doing "invalidation" on self when the initialization
210 // fails.
211}
212
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000213void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E,
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(E->getBase(), C,
Argyrios Kyrtzidisbe29d8d2011-02-01 19:32:55 +0000221 "Instance variable used while 'self' 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
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000225void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
226 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000227 // FIXME: A callback should disable checkers at the start of functions.
228 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks1efcc422012-02-04 02:31:37 +0000229 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000230 return;
231
232 checkForInvalidSelf(S->getRetValue(), C,
Argyrios Kyrtzidis63eeade2011-02-01 20:33:05 +0000233 "Returning 'self' while it is not set to the result of "
Argyrios Kyrtzidis4717f162011-01-26 01:26:41 +0000234 "'[(super or self) init...]'");
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000235}
236
Jordan Rosefe6a0112012-07-02 19:28:21 +0000237// When a call receives a reference to 'self', [Pre/Post]Call pass
238// the SelfFlags from the object 'self' points to before the call to the new
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000239// object after the call. This is to avoid invalidation of 'self' by logging
240// functions.
241// Another common pattern in classes with multiple initializers is to put the
242// subclass's common initialization bits into a static function that receives
243// the value of 'self', e.g:
244// @code
245// if (!(self = [super init]))
246// return nil;
247// if (!(self = _commonInit(self)))
248// return nil;
249// @endcode
250// Until we can use inter-procedural analysis, in such a call, transfer the
251// SelfFlags to the result of the call.
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000252
Jordan Rosefe6a0112012-07-02 19:28:21 +0000253void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000254 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +0000255 // FIXME: A callback should disable checkers at the start of functions.
256 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
257 C.getCurrentAnalysisDeclContext()->getDecl())))
258 return;
Jordan Rose55037cd2012-07-02 19:27:46 +0000259
Ted Kremenek8bef8232012-01-26 21:29:00 +0000260 ProgramStateRef state = C.getState();
Anna Zaksf420fe32012-03-05 18:58:25 +0000261 unsigned NumArgs = CE.getNumArgs();
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000262 // If we passed 'self' as and argument to the call, record it in the state
263 // to be propagated after the call.
264 // Note, we could have just given up, but try to be more optimistic here and
265 // assume that the functions are going to continue initialization or will not
266 // modify self.
Anna Zaksf420fe32012-03-05 18:58:25 +0000267 for (unsigned i = 0; i < NumArgs; ++i) {
268 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000269 if (isSelfVar(argV, C)) {
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000270 unsigned selfFlags = getSelfFlags(state->getSVal(cast<Loc>(argV)), C);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000271 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000272 return;
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000273 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000274 unsigned selfFlags = getSelfFlags(argV, C);
Anna Zaks0bd6b112011-10-26 21:06:34 +0000275 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000276 return;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000277 }
278 }
279}
280
Jordan Rosefe6a0112012-07-02 19:28:21 +0000281void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000282 CheckerContext &C) const {
Jordan Rosefe6a0112012-07-02 19:28:21 +0000283 // FIXME: A callback should disable checkers at the start of functions.
284 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
285 C.getCurrentAnalysisDeclContext()->getDecl())))
286 return;
287
Ted Kremenek8bef8232012-01-26 21:29:00 +0000288 ProgramStateRef state = C.getState();
Jordan Rosefe6a0112012-07-02 19:28:21 +0000289 SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
290 if (!prevFlags)
291 return;
292 state = state->remove<PreCallSelfFlags>();
293
Anna Zaksf420fe32012-03-05 18:58:25 +0000294 unsigned NumArgs = CE.getNumArgs();
295 for (unsigned i = 0; i < NumArgs; ++i) {
296 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000297 if (isSelfVar(argV, C)) {
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000298 // If the address of 'self' is being passed to the call, assume that the
299 // 'self' after the call will have the same flags.
300 // EX: log(&self)
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000301 addSelfFlag(state, state->getSVal(cast<Loc>(argV)), prevFlags, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000302 return;
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000303 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000304 // If 'self' is passed to the call by value, assume that the function
305 // returns 'self'. So assign the flags, which were set on 'self' to the
306 // return value.
307 // EX: self = performMoreInitialization(self)
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000308 const Expr *CallExpr = CE.getOriginExpr();
309 if (CallExpr)
310 addSelfFlag(state, state->getSVal(CallExpr, C.getLocationContext()),
311 prevFlags, C);
Argyrios Kyrtzidis0ca10402011-02-05 05:54:53 +0000312 return;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000313 }
314 }
315}
316
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000317void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
Anna Zaks390909c2011-10-06 00:43:15 +0000318 const Stmt *S,
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000319 CheckerContext &C) const {
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000320 // Tag the result of a load from 'self' so that we can easily know that the
321 // value is the object that 'self' points to.
Ted Kremenek8bef8232012-01-26 21:29:00 +0000322 ProgramStateRef state = C.getState();
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000323 if (isSelfVar(location, C))
Ted Kremenekb715a7c2011-02-12 03:03:54 +0000324 addSelfFlag(state, state->getSVal(cast<Loc>(location)), SelfFlag_Self, C);
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000325}
326
Anna Zaks6a2a1862012-05-08 21:19:21 +0000327
328void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
329 CheckerContext &C) const {
330 // Allow assignment of anything to self. Self is a local variable in the
331 // initializer, so it is legal to assign anything to it, like results of
332 // static functions/method calls. After self is assigned something we cannot
333 // reason about, stop enforcing the rules.
334 // (Only continue checking if the assigned value should be treated as self.)
335 if ((isSelfVar(loc, C)) &&
336 !hasSelfFlag(val, SelfFlag_InitRes, C) &&
337 !hasSelfFlag(val, SelfFlag_Self, C) &&
338 !isSelfVar(val, C)) {
339
340 // Stop tracking the checker-specific state in the state.
341 ProgramStateRef State = C.getState();
342 State = State->remove<CalledInit>();
343 if (SymbolRef sym = loc.getAsSymbol())
344 State = State->remove<SelfFlag>(sym);
345 C.addTransition(State);
346 }
347}
348
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000349// FIXME: A callback should disable checkers at the start of functions.
350static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
351 if (!ND)
352 return false;
353
354 const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
355 if (!MD)
356 return false;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000357 if (!isInitializationMethod(MD))
358 return false;
359
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000360 // self = [super init] applies only to NSObject subclasses.
361 // For instance, NSProxy doesn't implement -init.
Ted Kremenek9c378f72011-08-12 23:37:29 +0000362 ASTContext &Ctx = MD->getASTContext();
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000363 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenek9c378f72011-08-12 23:37:29 +0000364 ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass();
Argyrios Kyrtzidiseaf969b2011-01-25 23:54:44 +0000365 for ( ; ID ; ID = ID->getSuperClass()) {
366 IdentifierInfo *II = ID->getIdentifier();
367
368 if (II == NSObjectII)
369 break;
370 }
371 if (!ID)
372 return false;
373
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000374 return true;
375}
376
377/// \brief Returns true if the location is 'self'.
378static bool isSelfVar(SVal location, CheckerContext &C) {
Ted Kremenek1d26f482011-10-24 01:32:45 +0000379 AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000380 if (!analCtx->getSelfDecl())
381 return false;
382 if (!isa<loc::MemRegionVal>(location))
383 return false;
384
385 loc::MemRegionVal MRV = cast<loc::MemRegionVal>(location);
Anna Zaks9a70cdd2012-04-16 21:51:09 +0000386 if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts()))
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000387 return (DR->getDecl() == analCtx->getSelfDecl());
388
389 return false;
390}
391
392static bool isInitializationMethod(const ObjCMethodDecl *MD) {
John McCall85f3d762011-03-02 01:50:55 +0000393 return MD->getMethodFamily() == OMF_init;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000394}
395
Jordan Rosede507ea2012-07-02 19:28:04 +0000396static bool isInitMessage(const ObjCMethodCall &Call) {
397 return Call.getMethodFamily() == OMF_init;
Argyrios Kyrtzidisd7a31ba2011-01-11 19:45:25 +0000398}
Argyrios Kyrtzidis769ce3e2011-02-22 17:30:38 +0000399
400//===----------------------------------------------------------------------===//
401// Registration.
402//===----------------------------------------------------------------------===//
403
404void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
405 mgr.registerChecker<ObjCSelfInitChecker>();
406}