blob: 3eec6815c9049c582463e655a8ccebba3978b343 [file] [log] [blame]
Argyrios Kyrtzidis4b7433f2011-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 Lattner57540c52011-04-15 05:22:18 +000019// To perform the required checking, values are tagged with flags that indicate
Argyrios Kyrtzidis4b7433f2011-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 Kyrtzidis4b7433f2011-01-11 19:45:25 +000037//===----------------------------------------------------------------------===//
38
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +000039#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000040#include "clang/AST/ParentMap.h"
41#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000042#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000043#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Jordan Rose4f7df9b2012-07-26 21:39:41 +000044#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000045#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000046#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Benjamin Kramercfe5aed2012-12-01 17:54:07 +000047#include "llvm/Support/raw_ostream.h"
Argyrios Kyrtzidis4b7433f2011-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 Rose547060b2012-07-02 19:28:04 +000054static bool isInitMessage(const ObjCMethodCall &Msg);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000055static bool isSelfVar(SVal location, CheckerContext &C);
56
57namespace {
Nico Weber7ce830b2014-05-06 17:33:42 +000058class InitSelfBug : public BugType {
59public:
60 InitSelfBug(const CheckerBase *Checker)
61 : BugType(Checker, "Missing \"self = [(super or self) init...]\"",
62 categories::CoreFoundationObjectiveC) {}
63};
64
Jordan Rose682b3162012-07-02 19:28:21 +000065class ObjCSelfInitChecker : public Checker< check::PostObjCMessage,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000066 check::PostStmt<ObjCIvarRefExpr>,
67 check::PreStmt<ReturnStmt>,
Jordan Rose682b3162012-07-02 19:28:21 +000068 check::PreCall,
69 check::PostCall,
Anna Zaks66843482012-05-08 21:19:21 +000070 check::Location,
71 check::Bind > {
Nico Weber7ce830b2014-05-06 17:33:42 +000072 mutable InitSelfBug InitSelfBugType;
73
74 void checkForInvalidSelf(const Expr *E, CheckerContext &C,
75 const char *errorStr) const;
76
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000077public:
Nico Weber7ce830b2014-05-06 17:33:42 +000078 ObjCSelfInitChecker() : InitSelfBugType(this) {}
Jordan Rose547060b2012-07-02 19:28:04 +000079 void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const;
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000080 void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const;
81 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Anna Zaks3e0f4152011-10-06 00:43:15 +000082 void checkLocation(SVal location, bool isLoad, const Stmt *S,
83 CheckerContext &C) const;
Anna Zaks66843482012-05-08 21:19:21 +000084 void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
Anna Zaks53a0b6c2012-03-05 18:58:25 +000085
Jordan Rose682b3162012-07-02 19:28:21 +000086 void checkPreCall(const CallEvent &CE, CheckerContext &C) const;
87 void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
Anna Zaks53a0b6c2012-03-05 18:58:25 +000088
Jordan Rosebd94e5d2012-09-08 01:47:11 +000089 void printState(raw_ostream &Out, ProgramStateRef State,
Craig Topperfb6b25b2014-03-15 04:29:04 +000090 const char *NL, const char *Sep) const override;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000091};
92} // end anonymous namespace
93
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000094namespace {
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000095enum SelfFlagEnum {
96 /// \brief No flag set.
97 SelfFlag_None = 0x0,
98 /// \brief Value came from 'self'.
99 SelfFlag_Self = 0x1,
100 /// \brief Value came from the result of an initializer (e.g. [super init]).
101 SelfFlag_InitRes = 0x2
102};
103}
104
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000105REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned)
106REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool)
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000107
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000108/// \brief A call receiving a reference to 'self' invalidates the object that
109/// 'self' contains. This keeps the "self flags" assigned to the 'self'
110/// object before the call so we can assign them to the new object that 'self'
111/// points to after the call.
112REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned)
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000113
Ted Kremenek49b1e382012-01-26 21:29:00 +0000114static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000115 if (SymbolRef sym = val.getAsSymbol())
116 if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
117 return (SelfFlagEnum)*attachedFlags;
118 return SelfFlag_None;
119}
120
121static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
122 return getSelfFlags(val, C.getState());
123}
124
Ted Kremenek49b1e382012-01-26 21:29:00 +0000125static void addSelfFlag(ProgramStateRef state, SVal val,
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000126 SelfFlagEnum flag, CheckerContext &C) {
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000127 // We tag the symbol that the SVal wraps.
Anna Zaks3f129492012-12-13 00:42:19 +0000128 if (SymbolRef sym = val.getAsSymbol()) {
Jordan Rose5481cfe2012-09-08 01:47:28 +0000129 state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
Anna Zaks3f129492012-12-13 00:42:19 +0000130 C.addTransition(state);
131 }
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000132}
133
134static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
135 return getSelfFlags(val, C) & flag;
136}
137
138/// \brief Returns true of the value of the expression is the object that 'self'
139/// points to and is an object that did not come from the result of calling
140/// an initializer.
141static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
Ted Kremenek632e3b72012-01-06 22:09:28 +0000142 SVal exprVal = C.getState()->getSVal(E, C.getLocationContext());
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000143 if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
144 return false; // value did not come from 'self'.
145 if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
146 return false; // 'self' is properly initialized.
147
148 return true;
149}
150
Nico Weber7ce830b2014-05-06 17:33:42 +0000151void ObjCSelfInitChecker::checkForInvalidSelf(const Expr *E, CheckerContext &C,
152 const char *errorStr) const {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000153 if (!E)
154 return;
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000155
156 if (!C.getState()->get<CalledInit>())
157 return;
158
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000159 if (!isInvalidSelf(E, C))
160 return;
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000161
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000162 // Generate an error node.
163 ExplodedNode *N = C.generateSink();
164 if (!N)
165 return;
166
Nico Weber7ce830b2014-05-06 17:33:42 +0000167 BugReport *report = new BugReport(InitSelfBugType, errorStr, N);
Jordan Rosee10d5a72012-11-02 01:53:40 +0000168 C.emitReport(report);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000169}
170
Jordan Rose547060b2012-07-02 19:28:04 +0000171void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000172 CheckerContext &C) const {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000173 // When encountering a message that does initialization (init rule),
174 // tag the return value so that we know later on that if self has this value
175 // then it is properly initialized.
176
177 // FIXME: A callback should disable checkers at the start of functions.
178 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks00790d92012-02-04 02:31:37 +0000179 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000180 return;
181
Jordan Rose547060b2012-07-02 19:28:04 +0000182 if (isInitMessage(Msg)) {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000183 // Tag the return value as the result of an initializer.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000184 ProgramStateRef state = C.getState();
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000185
186 // FIXME this really should be context sensitive, where we record
187 // the current stack frame (for IPA). Also, we need to clean this
188 // value out when we return from this method.
189 state = state->set<CalledInit>(true);
190
Jordan Rose547060b2012-07-02 19:28:04 +0000191 SVal V = state->getSVal(Msg.getOriginExpr(), C.getLocationContext());
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000192 addSelfFlag(state, V, SelfFlag_InitRes, C);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000193 return;
194 }
195
196 // We don't check for an invalid 'self' in an obj-c message expression to cut
197 // down false positives where logging functions get information from self
198 // (like its class) or doing "invalidation" on self when the initialization
199 // fails.
200}
201
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000202void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E,
203 CheckerContext &C) const {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000204 // FIXME: A callback should disable checkers at the start of functions.
205 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks00790d92012-02-04 02:31:37 +0000206 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000207 return;
208
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000209 checkForInvalidSelf(
210 E->getBase(), C,
211 "Instance variable used while 'self' is not set to the result of "
Nico Weber7ce830b2014-05-06 17:33:42 +0000212 "'[(super or self) init...]'");
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000213}
214
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000215void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
216 CheckerContext &C) const {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000217 // FIXME: A callback should disable checkers at the start of functions.
218 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks00790d92012-02-04 02:31:37 +0000219 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000220 return;
221
222 checkForInvalidSelf(S->getRetValue(), C,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000223 "Returning 'self' while it is not set to the result of "
Nico Weber7ce830b2014-05-06 17:33:42 +0000224 "'[(super or self) init...]'");
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000225}
226
Jordan Rose682b3162012-07-02 19:28:21 +0000227// When a call receives a reference to 'self', [Pre/Post]Call pass
228// the SelfFlags from the object 'self' points to before the call to the new
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000229// object after the call. This is to avoid invalidation of 'self' by logging
230// functions.
231// Another common pattern in classes with multiple initializers is to put the
232// subclass's common initialization bits into a static function that receives
233// the value of 'self', e.g:
234// @code
235// if (!(self = [super init]))
236// return nil;
237// if (!(self = _commonInit(self)))
238// return nil;
239// @endcode
240// Until we can use inter-procedural analysis, in such a call, transfer the
241// SelfFlags to the result of the call.
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000242
Jordan Rose682b3162012-07-02 19:28:21 +0000243void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000244 CheckerContext &C) const {
Jordan Rose682b3162012-07-02 19:28:21 +0000245 // FIXME: A callback should disable checkers at the start of functions.
246 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
247 C.getCurrentAnalysisDeclContext()->getDecl())))
248 return;
Jordan Rose29953492012-07-02 19:27:46 +0000249
Ted Kremenek49b1e382012-01-26 21:29:00 +0000250 ProgramStateRef state = C.getState();
Anna Zaks53a0b6c2012-03-05 18:58:25 +0000251 unsigned NumArgs = CE.getNumArgs();
Anna Zaks51244c22012-04-16 21:51:09 +0000252 // If we passed 'self' as and argument to the call, record it in the state
253 // to be propagated after the call.
254 // Note, we could have just given up, but try to be more optimistic here and
255 // assume that the functions are going to continue initialization or will not
256 // modify self.
Anna Zaks53a0b6c2012-03-05 18:58:25 +0000257 for (unsigned i = 0; i < NumArgs; ++i) {
258 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000259 if (isSelfVar(argV, C)) {
David Blaikie2fdacbc2013-02-20 05:52:05 +0000260 unsigned selfFlags = getSelfFlags(state->getSVal(argV.castAs<Loc>()), C);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000261 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000262 return;
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000263 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000264 unsigned selfFlags = getSelfFlags(argV, C);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000265 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000266 return;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000267 }
268 }
269}
270
Jordan Rose682b3162012-07-02 19:28:21 +0000271void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000272 CheckerContext &C) const {
Jordan Rose682b3162012-07-02 19:28:21 +0000273 // FIXME: A callback should disable checkers at the start of functions.
274 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
275 C.getCurrentAnalysisDeclContext()->getDecl())))
276 return;
277
Ted Kremenek49b1e382012-01-26 21:29:00 +0000278 ProgramStateRef state = C.getState();
Jordan Rose682b3162012-07-02 19:28:21 +0000279 SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
280 if (!prevFlags)
281 return;
282 state = state->remove<PreCallSelfFlags>();
283
Anna Zaks53a0b6c2012-03-05 18:58:25 +0000284 unsigned NumArgs = CE.getNumArgs();
285 for (unsigned i = 0; i < NumArgs; ++i) {
286 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000287 if (isSelfVar(argV, C)) {
Anna Zaks51244c22012-04-16 21:51:09 +0000288 // If the address of 'self' is being passed to the call, assume that the
289 // 'self' after the call will have the same flags.
290 // EX: log(&self)
David Blaikie2fdacbc2013-02-20 05:52:05 +0000291 addSelfFlag(state, state->getSVal(argV.castAs<Loc>()), prevFlags, C);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000292 return;
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000293 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Anna Zaks51244c22012-04-16 21:51:09 +0000294 // If 'self' is passed to the call by value, assume that the function
295 // returns 'self'. So assign the flags, which were set on 'self' to the
296 // return value.
297 // EX: self = performMoreInitialization(self)
Jordan Rose829c3832012-11-02 23:49:29 +0000298 addSelfFlag(state, CE.getReturnValue(), prevFlags, C);
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000299 return;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000300 }
301 }
Jordan Rose829c3832012-11-02 23:49:29 +0000302
303 C.addTransition(state);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000304}
305
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000306void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
Anna Zaks3e0f4152011-10-06 00:43:15 +0000307 const Stmt *S,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000308 CheckerContext &C) const {
Anna Zaks3f129492012-12-13 00:42:19 +0000309 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
310 C.getCurrentAnalysisDeclContext()->getDecl())))
311 return;
312
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000313 // Tag the result of a load from 'self' so that we can easily know that the
314 // value is the object that 'self' points to.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000315 ProgramStateRef state = C.getState();
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000316 if (isSelfVar(location, C))
David Blaikie2fdacbc2013-02-20 05:52:05 +0000317 addSelfFlag(state, state->getSVal(location.castAs<Loc>()), SelfFlag_Self,
318 C);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000319}
320
Anna Zaks66843482012-05-08 21:19:21 +0000321
322void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
323 CheckerContext &C) const {
324 // Allow assignment of anything to self. Self is a local variable in the
325 // initializer, so it is legal to assign anything to it, like results of
326 // static functions/method calls. After self is assigned something we cannot
327 // reason about, stop enforcing the rules.
328 // (Only continue checking if the assigned value should be treated as self.)
329 if ((isSelfVar(loc, C)) &&
330 !hasSelfFlag(val, SelfFlag_InitRes, C) &&
331 !hasSelfFlag(val, SelfFlag_Self, C) &&
332 !isSelfVar(val, C)) {
333
334 // Stop tracking the checker-specific state in the state.
335 ProgramStateRef State = C.getState();
336 State = State->remove<CalledInit>();
337 if (SymbolRef sym = loc.getAsSymbol())
338 State = State->remove<SelfFlag>(sym);
339 C.addTransition(State);
340 }
341}
342
Jordan Rosebd94e5d2012-09-08 01:47:11 +0000343void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State,
344 const char *NL, const char *Sep) const {
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000345 SelfFlagTy FlagMap = State->get<SelfFlag>();
Jordan Rosebd94e5d2012-09-08 01:47:11 +0000346 bool DidCallInit = State->get<CalledInit>();
347 SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>();
348
349 if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
350 return;
351
Anton Yartsev6a619222014-02-17 18:25:34 +0000352 Out << Sep << NL << *this << " :" << NL;
Jordan Rosebd94e5d2012-09-08 01:47:11 +0000353
354 if (DidCallInit)
355 Out << " An init method has been called." << NL;
356
357 if (PreCallFlags != SelfFlag_None) {
358 if (PreCallFlags & SelfFlag_Self) {
359 Out << " An argument of the current call came from the 'self' variable."
360 << NL;
361 }
362 if (PreCallFlags & SelfFlag_InitRes) {
363 Out << " An argument of the current call came from an init method."
364 << NL;
365 }
366 }
367
368 Out << NL;
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000369 for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end();
370 I != E; ++I) {
Jordan Rosebd94e5d2012-09-08 01:47:11 +0000371 Out << I->first << " : ";
372
373 if (I->second == SelfFlag_None)
374 Out << "none";
375
376 if (I->second & SelfFlag_Self)
377 Out << "self variable";
378
379 if (I->second & SelfFlag_InitRes) {
380 if (I->second != SelfFlag_InitRes)
381 Out << " | ";
382 Out << "result of init method";
383 }
384
385 Out << NL;
386 }
387}
388
389
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000390// FIXME: A callback should disable checkers at the start of functions.
391static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
392 if (!ND)
393 return false;
394
395 const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
396 if (!MD)
397 return false;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000398 if (!isInitializationMethod(MD))
399 return false;
400
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +0000401 // self = [super init] applies only to NSObject subclasses.
402 // For instance, NSProxy doesn't implement -init.
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000403 ASTContext &Ctx = MD->getASTContext();
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +0000404 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000405 ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass();
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +0000406 for ( ; ID ; ID = ID->getSuperClass()) {
407 IdentifierInfo *II = ID->getIdentifier();
408
409 if (II == NSObjectII)
410 break;
411 }
412 if (!ID)
413 return false;
414
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000415 return true;
416}
417
418/// \brief Returns true if the location is 'self'.
419static bool isSelfVar(SVal location, CheckerContext &C) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000420 AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000421 if (!analCtx->getSelfDecl())
422 return false;
David Blaikie2fdacbc2013-02-20 05:52:05 +0000423 if (!location.getAs<loc::MemRegionVal>())
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000424 return false;
425
David Blaikie2fdacbc2013-02-20 05:52:05 +0000426 loc::MemRegionVal MRV = location.castAs<loc::MemRegionVal>();
Anna Zaks51244c22012-04-16 21:51:09 +0000427 if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts()))
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000428 return (DR->getDecl() == analCtx->getSelfDecl());
429
430 return false;
431}
432
433static bool isInitializationMethod(const ObjCMethodDecl *MD) {
John McCallb4526252011-03-02 01:50:55 +0000434 return MD->getMethodFamily() == OMF_init;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000435}
436
Jordan Rose547060b2012-07-02 19:28:04 +0000437static bool isInitMessage(const ObjCMethodCall &Call) {
438 return Call.getMethodFamily() == OMF_init;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000439}
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000440
441//===----------------------------------------------------------------------===//
442// Registration.
443//===----------------------------------------------------------------------===//
444
445void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
446 mgr.registerChecker<ObjCSelfInitChecker>();
447}