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