1*67e74705SXin Li //== ObjCSelfInitChecker.cpp - Checker for 'self' initialization -*- C++ -*--=//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This defines ObjCSelfInitChecker, a builtin check that checks for uses of
11*67e74705SXin Li // 'self' before proper initialization.
12*67e74705SXin Li //
13*67e74705SXin Li //===----------------------------------------------------------------------===//
14*67e74705SXin Li
15*67e74705SXin Li // This checks initialization methods to verify that they assign 'self' to the
16*67e74705SXin Li // result of an initialization call (e.g. [super init], or [self initWith..])
17*67e74705SXin Li // before using 'self' or any instance variable.
18*67e74705SXin Li //
19*67e74705SXin Li // To perform the required checking, values are tagged with flags that indicate
20*67e74705SXin Li // 1) if the object is the one pointed to by 'self', and 2) if the object
21*67e74705SXin Li // is the result of an initializer (e.g. [super init]).
22*67e74705SXin Li //
23*67e74705SXin Li // Uses of an object that is true for 1) but not 2) trigger a diagnostic.
24*67e74705SXin Li // The uses that are currently checked are:
25*67e74705SXin Li // - Using instance variables.
26*67e74705SXin Li // - Returning the object.
27*67e74705SXin Li //
28*67e74705SXin Li // Note that we don't check for an invalid 'self' that is the receiver of an
29*67e74705SXin Li // obj-c message expression to cut down false positives where logging functions
30*67e74705SXin Li // get information from self (like its class) or doing "invalidation" on self
31*67e74705SXin Li // when the initialization fails.
32*67e74705SXin Li //
33*67e74705SXin Li // Because the object that 'self' points to gets invalidated when a call
34*67e74705SXin Li // receives a reference to 'self', the checker keeps track and passes the flags
35*67e74705SXin Li // for 1) and 2) to the new object that 'self' points to after the call.
36*67e74705SXin Li //
37*67e74705SXin Li //===----------------------------------------------------------------------===//
38*67e74705SXin Li
39*67e74705SXin Li #include "ClangSACheckers.h"
40*67e74705SXin Li #include "clang/AST/ParentMap.h"
41*67e74705SXin Li #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
42*67e74705SXin Li #include "clang/StaticAnalyzer/Core/Checker.h"
43*67e74705SXin Li #include "clang/StaticAnalyzer/Core/CheckerManager.h"
44*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
45*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
46*67e74705SXin Li #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
47*67e74705SXin Li #include "llvm/Support/raw_ostream.h"
48*67e74705SXin Li
49*67e74705SXin Li using namespace clang;
50*67e74705SXin Li using namespace ento;
51*67e74705SXin Li
52*67e74705SXin Li static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND);
53*67e74705SXin Li static bool isInitializationMethod(const ObjCMethodDecl *MD);
54*67e74705SXin Li static bool isInitMessage(const ObjCMethodCall &Msg);
55*67e74705SXin Li static bool isSelfVar(SVal location, CheckerContext &C);
56*67e74705SXin Li
57*67e74705SXin Li namespace {
58*67e74705SXin Li class ObjCSelfInitChecker : public Checker< check::PostObjCMessage,
59*67e74705SXin Li check::PostStmt<ObjCIvarRefExpr>,
60*67e74705SXin Li check::PreStmt<ReturnStmt>,
61*67e74705SXin Li check::PreCall,
62*67e74705SXin Li check::PostCall,
63*67e74705SXin Li check::Location,
64*67e74705SXin Li check::Bind > {
65*67e74705SXin Li mutable std::unique_ptr<BugType> BT;
66*67e74705SXin Li
67*67e74705SXin Li void checkForInvalidSelf(const Expr *E, CheckerContext &C,
68*67e74705SXin Li const char *errorStr) const;
69*67e74705SXin Li
70*67e74705SXin Li public:
ObjCSelfInitChecker()71*67e74705SXin Li ObjCSelfInitChecker() {}
72*67e74705SXin Li void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const;
73*67e74705SXin Li void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const;
74*67e74705SXin Li void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
75*67e74705SXin Li void checkLocation(SVal location, bool isLoad, const Stmt *S,
76*67e74705SXin Li CheckerContext &C) const;
77*67e74705SXin Li void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
78*67e74705SXin Li
79*67e74705SXin Li void checkPreCall(const CallEvent &CE, CheckerContext &C) const;
80*67e74705SXin Li void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
81*67e74705SXin Li
82*67e74705SXin Li void printState(raw_ostream &Out, ProgramStateRef State,
83*67e74705SXin Li const char *NL, const char *Sep) const override;
84*67e74705SXin Li };
85*67e74705SXin Li } // end anonymous namespace
86*67e74705SXin Li
87*67e74705SXin Li namespace {
88*67e74705SXin Li enum SelfFlagEnum {
89*67e74705SXin Li /// \brief No flag set.
90*67e74705SXin Li SelfFlag_None = 0x0,
91*67e74705SXin Li /// \brief Value came from 'self'.
92*67e74705SXin Li SelfFlag_Self = 0x1,
93*67e74705SXin Li /// \brief Value came from the result of an initializer (e.g. [super init]).
94*67e74705SXin Li SelfFlag_InitRes = 0x2
95*67e74705SXin Li };
96*67e74705SXin Li }
97*67e74705SXin Li
REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag,SymbolRef,unsigned)98*67e74705SXin Li REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned)
99*67e74705SXin Li REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool)
100*67e74705SXin Li
101*67e74705SXin Li /// \brief A call receiving a reference to 'self' invalidates the object that
102*67e74705SXin Li /// 'self' contains. This keeps the "self flags" assigned to the 'self'
103*67e74705SXin Li /// object before the call so we can assign them to the new object that 'self'
104*67e74705SXin Li /// points to after the call.
105*67e74705SXin Li REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned)
106*67e74705SXin Li
107*67e74705SXin Li static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
108*67e74705SXin Li if (SymbolRef sym = val.getAsSymbol())
109*67e74705SXin Li if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
110*67e74705SXin Li return (SelfFlagEnum)*attachedFlags;
111*67e74705SXin Li return SelfFlag_None;
112*67e74705SXin Li }
113*67e74705SXin Li
getSelfFlags(SVal val,CheckerContext & C)114*67e74705SXin Li static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
115*67e74705SXin Li return getSelfFlags(val, C.getState());
116*67e74705SXin Li }
117*67e74705SXin Li
addSelfFlag(ProgramStateRef state,SVal val,SelfFlagEnum flag,CheckerContext & C)118*67e74705SXin Li static void addSelfFlag(ProgramStateRef state, SVal val,
119*67e74705SXin Li SelfFlagEnum flag, CheckerContext &C) {
120*67e74705SXin Li // We tag the symbol that the SVal wraps.
121*67e74705SXin Li if (SymbolRef sym = val.getAsSymbol()) {
122*67e74705SXin Li state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
123*67e74705SXin Li C.addTransition(state);
124*67e74705SXin Li }
125*67e74705SXin Li }
126*67e74705SXin Li
hasSelfFlag(SVal val,SelfFlagEnum flag,CheckerContext & C)127*67e74705SXin Li static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
128*67e74705SXin Li return getSelfFlags(val, C) & flag;
129*67e74705SXin Li }
130*67e74705SXin Li
131*67e74705SXin Li /// \brief Returns true of the value of the expression is the object that 'self'
132*67e74705SXin Li /// points to and is an object that did not come from the result of calling
133*67e74705SXin Li /// an initializer.
isInvalidSelf(const Expr * E,CheckerContext & C)134*67e74705SXin Li static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
135*67e74705SXin Li SVal exprVal = C.getState()->getSVal(E, C.getLocationContext());
136*67e74705SXin Li if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
137*67e74705SXin Li return false; // value did not come from 'self'.
138*67e74705SXin Li if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
139*67e74705SXin Li return false; // 'self' is properly initialized.
140*67e74705SXin Li
141*67e74705SXin Li return true;
142*67e74705SXin Li }
143*67e74705SXin Li
checkForInvalidSelf(const Expr * E,CheckerContext & C,const char * errorStr) const144*67e74705SXin Li void ObjCSelfInitChecker::checkForInvalidSelf(const Expr *E, CheckerContext &C,
145*67e74705SXin Li const char *errorStr) const {
146*67e74705SXin Li if (!E)
147*67e74705SXin Li return;
148*67e74705SXin Li
149*67e74705SXin Li if (!C.getState()->get<CalledInit>())
150*67e74705SXin Li return;
151*67e74705SXin Li
152*67e74705SXin Li if (!isInvalidSelf(E, C))
153*67e74705SXin Li return;
154*67e74705SXin Li
155*67e74705SXin Li // Generate an error node.
156*67e74705SXin Li ExplodedNode *N = C.generateErrorNode();
157*67e74705SXin Li if (!N)
158*67e74705SXin Li return;
159*67e74705SXin Li
160*67e74705SXin Li if (!BT)
161*67e74705SXin Li BT.reset(new BugType(this, "Missing \"self = [(super or self) init...]\"",
162*67e74705SXin Li categories::CoreFoundationObjectiveC));
163*67e74705SXin Li C.emitReport(llvm::make_unique<BugReport>(*BT, errorStr, N));
164*67e74705SXin Li }
165*67e74705SXin Li
checkPostObjCMessage(const ObjCMethodCall & Msg,CheckerContext & C) const166*67e74705SXin Li void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
167*67e74705SXin Li CheckerContext &C) const {
168*67e74705SXin Li // When encountering a message that does initialization (init rule),
169*67e74705SXin Li // tag the return value so that we know later on that if self has this value
170*67e74705SXin Li // then it is properly initialized.
171*67e74705SXin Li
172*67e74705SXin Li // FIXME: A callback should disable checkers at the start of functions.
173*67e74705SXin Li if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
174*67e74705SXin Li C.getCurrentAnalysisDeclContext()->getDecl())))
175*67e74705SXin Li return;
176*67e74705SXin Li
177*67e74705SXin Li if (isInitMessage(Msg)) {
178*67e74705SXin Li // Tag the return value as the result of an initializer.
179*67e74705SXin Li ProgramStateRef state = C.getState();
180*67e74705SXin Li
181*67e74705SXin Li // FIXME this really should be context sensitive, where we record
182*67e74705SXin Li // the current stack frame (for IPA). Also, we need to clean this
183*67e74705SXin Li // value out when we return from this method.
184*67e74705SXin Li state = state->set<CalledInit>(true);
185*67e74705SXin Li
186*67e74705SXin Li SVal V = state->getSVal(Msg.getOriginExpr(), C.getLocationContext());
187*67e74705SXin Li addSelfFlag(state, V, SelfFlag_InitRes, C);
188*67e74705SXin Li return;
189*67e74705SXin Li }
190*67e74705SXin Li
191*67e74705SXin Li // We don't check for an invalid 'self' in an obj-c message expression to cut
192*67e74705SXin Li // down false positives where logging functions get information from self
193*67e74705SXin Li // (like its class) or doing "invalidation" on self when the initialization
194*67e74705SXin Li // fails.
195*67e74705SXin Li }
196*67e74705SXin Li
checkPostStmt(const ObjCIvarRefExpr * E,CheckerContext & C) const197*67e74705SXin Li void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E,
198*67e74705SXin Li CheckerContext &C) const {
199*67e74705SXin Li // FIXME: A callback should disable checkers at the start of functions.
200*67e74705SXin Li if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
201*67e74705SXin Li C.getCurrentAnalysisDeclContext()->getDecl())))
202*67e74705SXin Li return;
203*67e74705SXin Li
204*67e74705SXin Li checkForInvalidSelf(
205*67e74705SXin Li E->getBase(), C,
206*67e74705SXin Li "Instance variable used while 'self' is not set to the result of "
207*67e74705SXin Li "'[(super or self) init...]'");
208*67e74705SXin Li }
209*67e74705SXin Li
checkPreStmt(const ReturnStmt * S,CheckerContext & C) const210*67e74705SXin Li void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
211*67e74705SXin Li CheckerContext &C) const {
212*67e74705SXin Li // FIXME: A callback should disable checkers at the start of functions.
213*67e74705SXin Li if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
214*67e74705SXin Li C.getCurrentAnalysisDeclContext()->getDecl())))
215*67e74705SXin Li return;
216*67e74705SXin Li
217*67e74705SXin Li checkForInvalidSelf(S->getRetValue(), C,
218*67e74705SXin Li "Returning 'self' while it is not set to the result of "
219*67e74705SXin Li "'[(super or self) init...]'");
220*67e74705SXin Li }
221*67e74705SXin Li
222*67e74705SXin Li // When a call receives a reference to 'self', [Pre/Post]Call pass
223*67e74705SXin Li // the SelfFlags from the object 'self' points to before the call to the new
224*67e74705SXin Li // object after the call. This is to avoid invalidation of 'self' by logging
225*67e74705SXin Li // functions.
226*67e74705SXin Li // Another common pattern in classes with multiple initializers is to put the
227*67e74705SXin Li // subclass's common initialization bits into a static function that receives
228*67e74705SXin Li // the value of 'self', e.g:
229*67e74705SXin Li // @code
230*67e74705SXin Li // if (!(self = [super init]))
231*67e74705SXin Li // return nil;
232*67e74705SXin Li // if (!(self = _commonInit(self)))
233*67e74705SXin Li // return nil;
234*67e74705SXin Li // @endcode
235*67e74705SXin Li // Until we can use inter-procedural analysis, in such a call, transfer the
236*67e74705SXin Li // SelfFlags to the result of the call.
237*67e74705SXin Li
checkPreCall(const CallEvent & CE,CheckerContext & C) const238*67e74705SXin Li void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE,
239*67e74705SXin Li CheckerContext &C) const {
240*67e74705SXin Li // FIXME: A callback should disable checkers at the start of functions.
241*67e74705SXin Li if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
242*67e74705SXin Li C.getCurrentAnalysisDeclContext()->getDecl())))
243*67e74705SXin Li return;
244*67e74705SXin Li
245*67e74705SXin Li ProgramStateRef state = C.getState();
246*67e74705SXin Li unsigned NumArgs = CE.getNumArgs();
247*67e74705SXin Li // If we passed 'self' as and argument to the call, record it in the state
248*67e74705SXin Li // to be propagated after the call.
249*67e74705SXin Li // Note, we could have just given up, but try to be more optimistic here and
250*67e74705SXin Li // assume that the functions are going to continue initialization or will not
251*67e74705SXin Li // modify self.
252*67e74705SXin Li for (unsigned i = 0; i < NumArgs; ++i) {
253*67e74705SXin Li SVal argV = CE.getArgSVal(i);
254*67e74705SXin Li if (isSelfVar(argV, C)) {
255*67e74705SXin Li unsigned selfFlags = getSelfFlags(state->getSVal(argV.castAs<Loc>()), C);
256*67e74705SXin Li C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
257*67e74705SXin Li return;
258*67e74705SXin Li } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
259*67e74705SXin Li unsigned selfFlags = getSelfFlags(argV, C);
260*67e74705SXin Li C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
261*67e74705SXin Li return;
262*67e74705SXin Li }
263*67e74705SXin Li }
264*67e74705SXin Li }
265*67e74705SXin Li
checkPostCall(const CallEvent & CE,CheckerContext & C) const266*67e74705SXin Li void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
267*67e74705SXin Li CheckerContext &C) const {
268*67e74705SXin Li // FIXME: A callback should disable checkers at the start of functions.
269*67e74705SXin Li if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
270*67e74705SXin Li C.getCurrentAnalysisDeclContext()->getDecl())))
271*67e74705SXin Li return;
272*67e74705SXin Li
273*67e74705SXin Li ProgramStateRef state = C.getState();
274*67e74705SXin Li SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
275*67e74705SXin Li if (!prevFlags)
276*67e74705SXin Li return;
277*67e74705SXin Li state = state->remove<PreCallSelfFlags>();
278*67e74705SXin Li
279*67e74705SXin Li unsigned NumArgs = CE.getNumArgs();
280*67e74705SXin Li for (unsigned i = 0; i < NumArgs; ++i) {
281*67e74705SXin Li SVal argV = CE.getArgSVal(i);
282*67e74705SXin Li if (isSelfVar(argV, C)) {
283*67e74705SXin Li // If the address of 'self' is being passed to the call, assume that the
284*67e74705SXin Li // 'self' after the call will have the same flags.
285*67e74705SXin Li // EX: log(&self)
286*67e74705SXin Li addSelfFlag(state, state->getSVal(argV.castAs<Loc>()), prevFlags, C);
287*67e74705SXin Li return;
288*67e74705SXin Li } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
289*67e74705SXin Li // If 'self' is passed to the call by value, assume that the function
290*67e74705SXin Li // returns 'self'. So assign the flags, which were set on 'self' to the
291*67e74705SXin Li // return value.
292*67e74705SXin Li // EX: self = performMoreInitialization(self)
293*67e74705SXin Li addSelfFlag(state, CE.getReturnValue(), prevFlags, C);
294*67e74705SXin Li return;
295*67e74705SXin Li }
296*67e74705SXin Li }
297*67e74705SXin Li
298*67e74705SXin Li C.addTransition(state);
299*67e74705SXin Li }
300*67e74705SXin Li
checkLocation(SVal location,bool isLoad,const Stmt * S,CheckerContext & C) const301*67e74705SXin Li void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
302*67e74705SXin Li const Stmt *S,
303*67e74705SXin Li CheckerContext &C) const {
304*67e74705SXin Li if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
305*67e74705SXin Li C.getCurrentAnalysisDeclContext()->getDecl())))
306*67e74705SXin Li return;
307*67e74705SXin Li
308*67e74705SXin Li // Tag the result of a load from 'self' so that we can easily know that the
309*67e74705SXin Li // value is the object that 'self' points to.
310*67e74705SXin Li ProgramStateRef state = C.getState();
311*67e74705SXin Li if (isSelfVar(location, C))
312*67e74705SXin Li addSelfFlag(state, state->getSVal(location.castAs<Loc>()), SelfFlag_Self,
313*67e74705SXin Li C);
314*67e74705SXin Li }
315*67e74705SXin Li
316*67e74705SXin Li
checkBind(SVal loc,SVal val,const Stmt * S,CheckerContext & C) const317*67e74705SXin Li void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
318*67e74705SXin Li CheckerContext &C) const {
319*67e74705SXin Li // Allow assignment of anything to self. Self is a local variable in the
320*67e74705SXin Li // initializer, so it is legal to assign anything to it, like results of
321*67e74705SXin Li // static functions/method calls. After self is assigned something we cannot
322*67e74705SXin Li // reason about, stop enforcing the rules.
323*67e74705SXin Li // (Only continue checking if the assigned value should be treated as self.)
324*67e74705SXin Li if ((isSelfVar(loc, C)) &&
325*67e74705SXin Li !hasSelfFlag(val, SelfFlag_InitRes, C) &&
326*67e74705SXin Li !hasSelfFlag(val, SelfFlag_Self, C) &&
327*67e74705SXin Li !isSelfVar(val, C)) {
328*67e74705SXin Li
329*67e74705SXin Li // Stop tracking the checker-specific state in the state.
330*67e74705SXin Li ProgramStateRef State = C.getState();
331*67e74705SXin Li State = State->remove<CalledInit>();
332*67e74705SXin Li if (SymbolRef sym = loc.getAsSymbol())
333*67e74705SXin Li State = State->remove<SelfFlag>(sym);
334*67e74705SXin Li C.addTransition(State);
335*67e74705SXin Li }
336*67e74705SXin Li }
337*67e74705SXin Li
printState(raw_ostream & Out,ProgramStateRef State,const char * NL,const char * Sep) const338*67e74705SXin Li void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State,
339*67e74705SXin Li const char *NL, const char *Sep) const {
340*67e74705SXin Li SelfFlagTy FlagMap = State->get<SelfFlag>();
341*67e74705SXin Li bool DidCallInit = State->get<CalledInit>();
342*67e74705SXin Li SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>();
343*67e74705SXin Li
344*67e74705SXin Li if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
345*67e74705SXin Li return;
346*67e74705SXin Li
347*67e74705SXin Li Out << Sep << NL << *this << " :" << NL;
348*67e74705SXin Li
349*67e74705SXin Li if (DidCallInit)
350*67e74705SXin Li Out << " An init method has been called." << NL;
351*67e74705SXin Li
352*67e74705SXin Li if (PreCallFlags != SelfFlag_None) {
353*67e74705SXin Li if (PreCallFlags & SelfFlag_Self) {
354*67e74705SXin Li Out << " An argument of the current call came from the 'self' variable."
355*67e74705SXin Li << NL;
356*67e74705SXin Li }
357*67e74705SXin Li if (PreCallFlags & SelfFlag_InitRes) {
358*67e74705SXin Li Out << " An argument of the current call came from an init method."
359*67e74705SXin Li << NL;
360*67e74705SXin Li }
361*67e74705SXin Li }
362*67e74705SXin Li
363*67e74705SXin Li Out << NL;
364*67e74705SXin Li for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end();
365*67e74705SXin Li I != E; ++I) {
366*67e74705SXin Li Out << I->first << " : ";
367*67e74705SXin Li
368*67e74705SXin Li if (I->second == SelfFlag_None)
369*67e74705SXin Li Out << "none";
370*67e74705SXin Li
371*67e74705SXin Li if (I->second & SelfFlag_Self)
372*67e74705SXin Li Out << "self variable";
373*67e74705SXin Li
374*67e74705SXin Li if (I->second & SelfFlag_InitRes) {
375*67e74705SXin Li if (I->second != SelfFlag_InitRes)
376*67e74705SXin Li Out << " | ";
377*67e74705SXin Li Out << "result of init method";
378*67e74705SXin Li }
379*67e74705SXin Li
380*67e74705SXin Li Out << NL;
381*67e74705SXin Li }
382*67e74705SXin Li }
383*67e74705SXin Li
384*67e74705SXin Li
385*67e74705SXin Li // FIXME: A callback should disable checkers at the start of functions.
shouldRunOnFunctionOrMethod(const NamedDecl * ND)386*67e74705SXin Li static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
387*67e74705SXin Li if (!ND)
388*67e74705SXin Li return false;
389*67e74705SXin Li
390*67e74705SXin Li const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
391*67e74705SXin Li if (!MD)
392*67e74705SXin Li return false;
393*67e74705SXin Li if (!isInitializationMethod(MD))
394*67e74705SXin Li return false;
395*67e74705SXin Li
396*67e74705SXin Li // self = [super init] applies only to NSObject subclasses.
397*67e74705SXin Li // For instance, NSProxy doesn't implement -init.
398*67e74705SXin Li ASTContext &Ctx = MD->getASTContext();
399*67e74705SXin Li IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
400*67e74705SXin Li ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass();
401*67e74705SXin Li for ( ; ID ; ID = ID->getSuperClass()) {
402*67e74705SXin Li IdentifierInfo *II = ID->getIdentifier();
403*67e74705SXin Li
404*67e74705SXin Li if (II == NSObjectII)
405*67e74705SXin Li break;
406*67e74705SXin Li }
407*67e74705SXin Li return ID != nullptr;
408*67e74705SXin Li }
409*67e74705SXin Li
410*67e74705SXin Li /// \brief Returns true if the location is 'self'.
isSelfVar(SVal location,CheckerContext & C)411*67e74705SXin Li static bool isSelfVar(SVal location, CheckerContext &C) {
412*67e74705SXin Li AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
413*67e74705SXin Li if (!analCtx->getSelfDecl())
414*67e74705SXin Li return false;
415*67e74705SXin Li if (!location.getAs<loc::MemRegionVal>())
416*67e74705SXin Li return false;
417*67e74705SXin Li
418*67e74705SXin Li loc::MemRegionVal MRV = location.castAs<loc::MemRegionVal>();
419*67e74705SXin Li if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts()))
420*67e74705SXin Li return (DR->getDecl() == analCtx->getSelfDecl());
421*67e74705SXin Li
422*67e74705SXin Li return false;
423*67e74705SXin Li }
424*67e74705SXin Li
isInitializationMethod(const ObjCMethodDecl * MD)425*67e74705SXin Li static bool isInitializationMethod(const ObjCMethodDecl *MD) {
426*67e74705SXin Li return MD->getMethodFamily() == OMF_init;
427*67e74705SXin Li }
428*67e74705SXin Li
isInitMessage(const ObjCMethodCall & Call)429*67e74705SXin Li static bool isInitMessage(const ObjCMethodCall &Call) {
430*67e74705SXin Li return Call.getMethodFamily() == OMF_init;
431*67e74705SXin Li }
432*67e74705SXin Li
433*67e74705SXin Li //===----------------------------------------------------------------------===//
434*67e74705SXin Li // Registration.
435*67e74705SXin Li //===----------------------------------------------------------------------===//
436*67e74705SXin Li
registerObjCSelfInitChecker(CheckerManager & mgr)437*67e74705SXin Li void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
438*67e74705SXin Li mgr.registerChecker<ObjCSelfInitChecker>();
439*67e74705SXin Li }
440