blob: 51bc7e66dce2d9c7c4d653bce19bf1d19cd8b788 [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 {
Jordan Rose682b3162012-07-02 19:28:21 +000058class ObjCSelfInitChecker : public Checker< check::PostObjCMessage,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000059 check::PostStmt<ObjCIvarRefExpr>,
60 check::PreStmt<ReturnStmt>,
Jordan Rose682b3162012-07-02 19:28:21 +000061 check::PreCall,
62 check::PostCall,
Anna Zaks66843482012-05-08 21:19:21 +000063 check::Location,
64 check::Bind > {
Jordan Rose49afeb02014-05-07 03:30:04 +000065 mutable std::unique_ptr<BugType> BT;
Nico Weber7ce830b2014-05-06 17:33:42 +000066
67 void checkForInvalidSelf(const Expr *E, CheckerContext &C,
68 const char *errorStr) const;
69
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000070public:
Jordan Rose49afeb02014-05-07 03:30:04 +000071 ObjCSelfInitChecker() {}
Jordan Rose547060b2012-07-02 19:28:04 +000072 void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const;
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000073 void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const;
74 void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
Anna Zaks3e0f4152011-10-06 00:43:15 +000075 void checkLocation(SVal location, bool isLoad, const Stmt *S,
76 CheckerContext &C) const;
Anna Zaks66843482012-05-08 21:19:21 +000077 void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
Anna Zaks53a0b6c2012-03-05 18:58:25 +000078
Jordan Rose682b3162012-07-02 19:28:21 +000079 void checkPreCall(const CallEvent &CE, CheckerContext &C) const;
80 void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
Anna Zaks53a0b6c2012-03-05 18:58:25 +000081
Jordan Rosebd94e5d2012-09-08 01:47:11 +000082 void printState(raw_ostream &Out, ProgramStateRef State,
Craig Topperfb6b25b2014-03-15 04:29:04 +000083 const char *NL, const char *Sep) const override;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000084};
85} // end anonymous namespace
86
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +000087namespace {
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +000088enum SelfFlagEnum {
89 /// \brief No flag set.
90 SelfFlag_None = 0x0,
91 /// \brief Value came from 'self'.
92 SelfFlag_Self = 0x1,
93 /// \brief Value came from the result of an initializer (e.g. [super init]).
94 SelfFlag_InitRes = 0x2
95};
96}
97
Jordan Roseb9ed61f2012-11-02 01:54:42 +000098REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned)
99REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool)
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000100
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000101/// \brief A call receiving a reference to 'self' invalidates the object that
102/// 'self' contains. This keeps the "self flags" assigned to the 'self'
103/// object before the call so we can assign them to the new object that 'self'
104/// points to after the call.
105REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned)
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000106
Ted Kremenek49b1e382012-01-26 21:29:00 +0000107static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000108 if (SymbolRef sym = val.getAsSymbol())
109 if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
110 return (SelfFlagEnum)*attachedFlags;
111 return SelfFlag_None;
112}
113
114static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
115 return getSelfFlags(val, C.getState());
116}
117
Ted Kremenek49b1e382012-01-26 21:29:00 +0000118static void addSelfFlag(ProgramStateRef state, SVal val,
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000119 SelfFlagEnum flag, CheckerContext &C) {
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000120 // We tag the symbol that the SVal wraps.
Anna Zaks3f129492012-12-13 00:42:19 +0000121 if (SymbolRef sym = val.getAsSymbol()) {
Jordan Rose5481cfe2012-09-08 01:47:28 +0000122 state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
Anna Zaks3f129492012-12-13 00:42:19 +0000123 C.addTransition(state);
124 }
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000125}
126
127static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
128 return getSelfFlags(val, C) & flag;
129}
130
131/// \brief Returns true of the value of the expression is the object that 'self'
132/// points to and is an object that did not come from the result of calling
133/// an initializer.
134static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
Ted Kremenek632e3b72012-01-06 22:09:28 +0000135 SVal exprVal = C.getState()->getSVal(E, C.getLocationContext());
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000136 if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
137 return false; // value did not come from 'self'.
138 if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
139 return false; // 'self' is properly initialized.
140
141 return true;
142}
143
Nico Weber7ce830b2014-05-06 17:33:42 +0000144void ObjCSelfInitChecker::checkForInvalidSelf(const Expr *E, CheckerContext &C,
145 const char *errorStr) const {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000146 if (!E)
147 return;
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000148
149 if (!C.getState()->get<CalledInit>())
150 return;
151
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000152 if (!isInvalidSelf(E, C))
153 return;
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000154
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000155 // Generate an error node.
156 ExplodedNode *N = C.generateSink();
157 if (!N)
158 return;
159
Jordan Rose49afeb02014-05-07 03:30:04 +0000160 if (!BT)
161 BT.reset(new BugType(this, "Missing \"self = [(super or self) init...]\"",
162 categories::CoreFoundationObjectiveC));
163 BugReport *report = new BugReport(*BT, errorStr, N);
Jordan Rosee10d5a72012-11-02 01:53:40 +0000164 C.emitReport(report);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000165}
166
Jordan Rose547060b2012-07-02 19:28:04 +0000167void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000168 CheckerContext &C) const {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000169 // When encountering a message that does initialization (init rule),
170 // tag the return value so that we know later on that if self has this value
171 // then it is properly initialized.
172
173 // FIXME: A callback should disable checkers at the start of functions.
174 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks00790d92012-02-04 02:31:37 +0000175 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000176 return;
177
Jordan Rose547060b2012-07-02 19:28:04 +0000178 if (isInitMessage(Msg)) {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000179 // Tag the return value as the result of an initializer.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000180 ProgramStateRef state = C.getState();
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000181
182 // FIXME this really should be context sensitive, where we record
183 // the current stack frame (for IPA). Also, we need to clean this
184 // value out when we return from this method.
185 state = state->set<CalledInit>(true);
186
Jordan Rose547060b2012-07-02 19:28:04 +0000187 SVal V = state->getSVal(Msg.getOriginExpr(), C.getLocationContext());
Ted Kremenek70aeefa2011-02-12 03:03:54 +0000188 addSelfFlag(state, V, SelfFlag_InitRes, C);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000189 return;
190 }
191
192 // We don't check for an invalid 'self' in an obj-c message expression to cut
193 // down false positives where logging functions get information from self
194 // (like its class) or doing "invalidation" on self when the initialization
195 // fails.
196}
197
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000198void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E,
199 CheckerContext &C) const {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000200 // FIXME: A callback should disable checkers at the start of functions.
201 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks00790d92012-02-04 02:31:37 +0000202 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000203 return;
204
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000205 checkForInvalidSelf(
206 E->getBase(), C,
207 "Instance variable used while 'self' is not set to the result of "
Nico Weber7ce830b2014-05-06 17:33:42 +0000208 "'[(super or self) init...]'");
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000209}
210
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000211void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
212 CheckerContext &C) const {
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000213 // FIXME: A callback should disable checkers at the start of functions.
214 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
Anna Zaks00790d92012-02-04 02:31:37 +0000215 C.getCurrentAnalysisDeclContext()->getDecl())))
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000216 return;
217
218 checkForInvalidSelf(S->getRetValue(), C,
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000219 "Returning 'self' while it is not set to the result of "
Nico Weber7ce830b2014-05-06 17:33:42 +0000220 "'[(super or self) init...]'");
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000221}
222
Jordan Rose682b3162012-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 Kyrtzidisdd03d8d2011-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 Kyrtzidis4b7433f2011-01-11 19:45:25 +0000238
Jordan Rose682b3162012-07-02 19:28:21 +0000239void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000240 CheckerContext &C) const {
Jordan Rose682b3162012-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 Rose29953492012-07-02 19:27:46 +0000245
Ted Kremenek49b1e382012-01-26 21:29:00 +0000246 ProgramStateRef state = C.getState();
Anna Zaks53a0b6c2012-03-05 18:58:25 +0000247 unsigned NumArgs = CE.getNumArgs();
Anna Zaks51244c22012-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 Zaks53a0b6c2012-03-05 18:58:25 +0000253 for (unsigned i = 0; i < NumArgs; ++i) {
254 SVal argV = CE.getArgSVal(i);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000255 if (isSelfVar(argV, C)) {
David Blaikie2fdacbc2013-02-20 05:52:05 +0000256 unsigned selfFlags = getSelfFlags(state->getSVal(argV.castAs<Loc>()), C);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000257 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000258 return;
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000259 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000260 unsigned selfFlags = getSelfFlags(argV, C);
Anna Zaksda4c8d62011-10-26 21:06:34 +0000261 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000262 return;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000263 }
264 }
265}
266
Jordan Rose682b3162012-07-02 19:28:21 +0000267void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000268 CheckerContext &C) const {
Jordan Rose682b3162012-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 Kremenek49b1e382012-01-26 21:29:00 +0000274 ProgramStateRef state = C.getState();
Jordan Rose682b3162012-07-02 19:28:21 +0000275 SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
276 if (!prevFlags)
277 return;
278 state = state->remove<PreCallSelfFlags>();
279
Anna Zaks53a0b6c2012-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 Kyrtzidis4b7433f2011-01-11 19:45:25 +0000283 if (isSelfVar(argV, C)) {
Anna Zaks51244c22012-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)
David Blaikie2fdacbc2013-02-20 05:52:05 +0000287 addSelfFlag(state, state->getSVal(argV.castAs<Loc>()), prevFlags, C);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000288 return;
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000289 } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
Anna Zaks51244c22012-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 Rose829c3832012-11-02 23:49:29 +0000294 addSelfFlag(state, CE.getReturnValue(), prevFlags, C);
Argyrios Kyrtzidisdd03d8d2011-02-05 05:54:53 +0000295 return;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000296 }
297 }
Jordan Rose829c3832012-11-02 23:49:29 +0000298
299 C.addTransition(state);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000300}
301
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000302void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
Anna Zaks3e0f4152011-10-06 00:43:15 +0000303 const Stmt *S,
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000304 CheckerContext &C) const {
Anna Zaks3f129492012-12-13 00:42:19 +0000305 if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
306 C.getCurrentAnalysisDeclContext()->getDecl())))
307 return;
308
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000309 // Tag the result of a load from 'self' so that we can easily know that the
310 // value is the object that 'self' points to.
Ted Kremenek49b1e382012-01-26 21:29:00 +0000311 ProgramStateRef state = C.getState();
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000312 if (isSelfVar(location, C))
David Blaikie2fdacbc2013-02-20 05:52:05 +0000313 addSelfFlag(state, state->getSVal(location.castAs<Loc>()), SelfFlag_Self,
314 C);
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000315}
316
Anna Zaks66843482012-05-08 21:19:21 +0000317
318void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
319 CheckerContext &C) const {
320 // Allow assignment of anything to self. Self is a local variable in the
321 // initializer, so it is legal to assign anything to it, like results of
322 // static functions/method calls. After self is assigned something we cannot
323 // reason about, stop enforcing the rules.
324 // (Only continue checking if the assigned value should be treated as self.)
325 if ((isSelfVar(loc, C)) &&
326 !hasSelfFlag(val, SelfFlag_InitRes, C) &&
327 !hasSelfFlag(val, SelfFlag_Self, C) &&
328 !isSelfVar(val, C)) {
329
330 // Stop tracking the checker-specific state in the state.
331 ProgramStateRef State = C.getState();
332 State = State->remove<CalledInit>();
333 if (SymbolRef sym = loc.getAsSymbol())
334 State = State->remove<SelfFlag>(sym);
335 C.addTransition(State);
336 }
337}
338
Jordan Rosebd94e5d2012-09-08 01:47:11 +0000339void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State,
340 const char *NL, const char *Sep) const {
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000341 SelfFlagTy FlagMap = State->get<SelfFlag>();
Jordan Rosebd94e5d2012-09-08 01:47:11 +0000342 bool DidCallInit = State->get<CalledInit>();
343 SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>();
344
345 if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
346 return;
347
Anton Yartsev6a619222014-02-17 18:25:34 +0000348 Out << Sep << NL << *this << " :" << NL;
Jordan Rosebd94e5d2012-09-08 01:47:11 +0000349
350 if (DidCallInit)
351 Out << " An init method has been called." << NL;
352
353 if (PreCallFlags != SelfFlag_None) {
354 if (PreCallFlags & SelfFlag_Self) {
355 Out << " An argument of the current call came from the 'self' variable."
356 << NL;
357 }
358 if (PreCallFlags & SelfFlag_InitRes) {
359 Out << " An argument of the current call came from an init method."
360 << NL;
361 }
362 }
363
364 Out << NL;
Jordan Roseb9ed61f2012-11-02 01:54:42 +0000365 for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end();
366 I != E; ++I) {
Jordan Rosebd94e5d2012-09-08 01:47:11 +0000367 Out << I->first << " : ";
368
369 if (I->second == SelfFlag_None)
370 Out << "none";
371
372 if (I->second & SelfFlag_Self)
373 Out << "self variable";
374
375 if (I->second & SelfFlag_InitRes) {
376 if (I->second != SelfFlag_InitRes)
377 Out << " | ";
378 Out << "result of init method";
379 }
380
381 Out << NL;
382 }
383}
384
385
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000386// FIXME: A callback should disable checkers at the start of functions.
387static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
388 if (!ND)
389 return false;
390
391 const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
392 if (!MD)
393 return false;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000394 if (!isInitializationMethod(MD))
395 return false;
396
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +0000397 // self = [super init] applies only to NSObject subclasses.
398 // For instance, NSProxy doesn't implement -init.
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000399 ASTContext &Ctx = MD->getASTContext();
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +0000400 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000401 ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass();
Argyrios Kyrtzidis3ae681e2011-01-25 23:54:44 +0000402 for ( ; ID ; ID = ID->getSuperClass()) {
403 IdentifierInfo *II = ID->getIdentifier();
404
405 if (II == NSObjectII)
406 break;
407 }
408 if (!ID)
409 return false;
410
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000411 return true;
412}
413
414/// \brief Returns true if the location is 'self'.
415static bool isSelfVar(SVal location, CheckerContext &C) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000416 AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000417 if (!analCtx->getSelfDecl())
418 return false;
David Blaikie2fdacbc2013-02-20 05:52:05 +0000419 if (!location.getAs<loc::MemRegionVal>())
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000420 return false;
421
David Blaikie2fdacbc2013-02-20 05:52:05 +0000422 loc::MemRegionVal MRV = location.castAs<loc::MemRegionVal>();
Anna Zaks51244c22012-04-16 21:51:09 +0000423 if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts()))
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000424 return (DR->getDecl() == analCtx->getSelfDecl());
425
426 return false;
427}
428
429static bool isInitializationMethod(const ObjCMethodDecl *MD) {
John McCallb4526252011-03-02 01:50:55 +0000430 return MD->getMethodFamily() == OMF_init;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000431}
432
Jordan Rose547060b2012-07-02 19:28:04 +0000433static bool isInitMessage(const ObjCMethodCall &Call) {
434 return Call.getMethodFamily() == OMF_init;
Argyrios Kyrtzidis4b7433f2011-01-11 19:45:25 +0000435}
Argyrios Kyrtzidised35cf22011-02-22 17:30:38 +0000436
437//===----------------------------------------------------------------------===//
438// Registration.
439//===----------------------------------------------------------------------===//
440
441void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
442 mgr.registerChecker<ObjCSelfInitChecker>();
443}