17330f729Sjoerg //== ObjCSelfInitChecker.cpp - Checker for 'self' initialization -*- C++ -*--=//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This defines ObjCSelfInitChecker, a builtin check that checks for uses of
107330f729Sjoerg // 'self' before proper initialization.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerg // This checks initialization methods to verify that they assign 'self' to the
157330f729Sjoerg // result of an initialization call (e.g. [super init], or [self initWith..])
167330f729Sjoerg // before using 'self' or any instance variable.
177330f729Sjoerg //
187330f729Sjoerg // To perform the required checking, values are tagged with flags that indicate
197330f729Sjoerg // 1) if the object is the one pointed to by 'self', and 2) if the object
207330f729Sjoerg // is the result of an initializer (e.g. [super init]).
217330f729Sjoerg //
227330f729Sjoerg // Uses of an object that is true for 1) but not 2) trigger a diagnostic.
237330f729Sjoerg // The uses that are currently checked are:
247330f729Sjoerg // - Using instance variables.
257330f729Sjoerg // - Returning the object.
267330f729Sjoerg //
277330f729Sjoerg // Note that we don't check for an invalid 'self' that is the receiver of an
287330f729Sjoerg // obj-c message expression to cut down false positives where logging functions
297330f729Sjoerg // get information from self (like its class) or doing "invalidation" on self
307330f729Sjoerg // when the initialization fails.
317330f729Sjoerg //
327330f729Sjoerg // Because the object that 'self' points to gets invalidated when a call
337330f729Sjoerg // receives a reference to 'self', the checker keeps track and passes the flags
347330f729Sjoerg // for 1) and 2) to the new object that 'self' points to after the call.
357330f729Sjoerg //
367330f729Sjoerg //===----------------------------------------------------------------------===//
377330f729Sjoerg
387330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
397330f729Sjoerg #include "clang/AST/ParentMap.h"
407330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
417330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
427330f729Sjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
437330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
447330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
457330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
467330f729Sjoerg #include "llvm/Support/raw_ostream.h"
477330f729Sjoerg
487330f729Sjoerg using namespace clang;
497330f729Sjoerg using namespace ento;
507330f729Sjoerg
517330f729Sjoerg static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND);
527330f729Sjoerg static bool isInitializationMethod(const ObjCMethodDecl *MD);
537330f729Sjoerg static bool isInitMessage(const ObjCMethodCall &Msg);
547330f729Sjoerg static bool isSelfVar(SVal location, CheckerContext &C);
557330f729Sjoerg
567330f729Sjoerg namespace {
577330f729Sjoerg class ObjCSelfInitChecker : public Checker< check::PostObjCMessage,
587330f729Sjoerg check::PostStmt<ObjCIvarRefExpr>,
597330f729Sjoerg check::PreStmt<ReturnStmt>,
607330f729Sjoerg check::PreCall,
617330f729Sjoerg check::PostCall,
627330f729Sjoerg check::Location,
637330f729Sjoerg check::Bind > {
647330f729Sjoerg mutable std::unique_ptr<BugType> BT;
657330f729Sjoerg
667330f729Sjoerg void checkForInvalidSelf(const Expr *E, CheckerContext &C,
677330f729Sjoerg const char *errorStr) const;
687330f729Sjoerg
697330f729Sjoerg public:
ObjCSelfInitChecker()707330f729Sjoerg ObjCSelfInitChecker() {}
717330f729Sjoerg void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const;
727330f729Sjoerg void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const;
737330f729Sjoerg void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
747330f729Sjoerg void checkLocation(SVal location, bool isLoad, const Stmt *S,
757330f729Sjoerg CheckerContext &C) const;
767330f729Sjoerg void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
777330f729Sjoerg
787330f729Sjoerg void checkPreCall(const CallEvent &CE, CheckerContext &C) const;
797330f729Sjoerg void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
807330f729Sjoerg
817330f729Sjoerg void printState(raw_ostream &Out, ProgramStateRef State,
827330f729Sjoerg const char *NL, const char *Sep) const override;
837330f729Sjoerg };
847330f729Sjoerg } // end anonymous namespace
857330f729Sjoerg
867330f729Sjoerg namespace {
877330f729Sjoerg enum SelfFlagEnum {
887330f729Sjoerg /// No flag set.
897330f729Sjoerg SelfFlag_None = 0x0,
907330f729Sjoerg /// Value came from 'self'.
917330f729Sjoerg SelfFlag_Self = 0x1,
927330f729Sjoerg /// Value came from the result of an initializer (e.g. [super init]).
937330f729Sjoerg SelfFlag_InitRes = 0x2
947330f729Sjoerg };
957330f729Sjoerg }
967330f729Sjoerg
REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag,SymbolRef,unsigned)977330f729Sjoerg REGISTER_MAP_WITH_PROGRAMSTATE(SelfFlag, SymbolRef, unsigned)
987330f729Sjoerg REGISTER_TRAIT_WITH_PROGRAMSTATE(CalledInit, bool)
997330f729Sjoerg
1007330f729Sjoerg /// A call receiving a reference to 'self' invalidates the object that
1017330f729Sjoerg /// 'self' contains. This keeps the "self flags" assigned to the 'self'
1027330f729Sjoerg /// object before the call so we can assign them to the new object that 'self'
1037330f729Sjoerg /// points to after the call.
1047330f729Sjoerg REGISTER_TRAIT_WITH_PROGRAMSTATE(PreCallSelfFlags, unsigned)
1057330f729Sjoerg
1067330f729Sjoerg static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state) {
1077330f729Sjoerg if (SymbolRef sym = val.getAsSymbol())
1087330f729Sjoerg if (const unsigned *attachedFlags = state->get<SelfFlag>(sym))
1097330f729Sjoerg return (SelfFlagEnum)*attachedFlags;
1107330f729Sjoerg return SelfFlag_None;
1117330f729Sjoerg }
1127330f729Sjoerg
getSelfFlags(SVal val,CheckerContext & C)1137330f729Sjoerg static SelfFlagEnum getSelfFlags(SVal val, CheckerContext &C) {
1147330f729Sjoerg return getSelfFlags(val, C.getState());
1157330f729Sjoerg }
1167330f729Sjoerg
addSelfFlag(ProgramStateRef state,SVal val,SelfFlagEnum flag,CheckerContext & C)1177330f729Sjoerg static void addSelfFlag(ProgramStateRef state, SVal val,
1187330f729Sjoerg SelfFlagEnum flag, CheckerContext &C) {
1197330f729Sjoerg // We tag the symbol that the SVal wraps.
1207330f729Sjoerg if (SymbolRef sym = val.getAsSymbol()) {
1217330f729Sjoerg state = state->set<SelfFlag>(sym, getSelfFlags(val, state) | flag);
1227330f729Sjoerg C.addTransition(state);
1237330f729Sjoerg }
1247330f729Sjoerg }
1257330f729Sjoerg
hasSelfFlag(SVal val,SelfFlagEnum flag,CheckerContext & C)1267330f729Sjoerg static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C) {
1277330f729Sjoerg return getSelfFlags(val, C) & flag;
1287330f729Sjoerg }
1297330f729Sjoerg
1307330f729Sjoerg /// Returns true of the value of the expression is the object that 'self'
1317330f729Sjoerg /// points to and is an object that did not come from the result of calling
1327330f729Sjoerg /// an initializer.
isInvalidSelf(const Expr * E,CheckerContext & C)1337330f729Sjoerg static bool isInvalidSelf(const Expr *E, CheckerContext &C) {
1347330f729Sjoerg SVal exprVal = C.getSVal(E);
1357330f729Sjoerg if (!hasSelfFlag(exprVal, SelfFlag_Self, C))
1367330f729Sjoerg return false; // value did not come from 'self'.
1377330f729Sjoerg if (hasSelfFlag(exprVal, SelfFlag_InitRes, C))
1387330f729Sjoerg return false; // 'self' is properly initialized.
1397330f729Sjoerg
1407330f729Sjoerg return true;
1417330f729Sjoerg }
1427330f729Sjoerg
checkForInvalidSelf(const Expr * E,CheckerContext & C,const char * errorStr) const1437330f729Sjoerg void ObjCSelfInitChecker::checkForInvalidSelf(const Expr *E, CheckerContext &C,
1447330f729Sjoerg const char *errorStr) const {
1457330f729Sjoerg if (!E)
1467330f729Sjoerg return;
1477330f729Sjoerg
1487330f729Sjoerg if (!C.getState()->get<CalledInit>())
1497330f729Sjoerg return;
1507330f729Sjoerg
1517330f729Sjoerg if (!isInvalidSelf(E, C))
1527330f729Sjoerg return;
1537330f729Sjoerg
1547330f729Sjoerg // Generate an error node.
1557330f729Sjoerg ExplodedNode *N = C.generateErrorNode();
1567330f729Sjoerg if (!N)
1577330f729Sjoerg return;
1587330f729Sjoerg
1597330f729Sjoerg if (!BT)
1607330f729Sjoerg BT.reset(new BugType(this, "Missing \"self = [(super or self) init...]\"",
1617330f729Sjoerg categories::CoreFoundationObjectiveC));
1627330f729Sjoerg C.emitReport(std::make_unique<PathSensitiveBugReport>(*BT, errorStr, N));
1637330f729Sjoerg }
1647330f729Sjoerg
checkPostObjCMessage(const ObjCMethodCall & Msg,CheckerContext & C) const1657330f729Sjoerg void ObjCSelfInitChecker::checkPostObjCMessage(const ObjCMethodCall &Msg,
1667330f729Sjoerg CheckerContext &C) const {
1677330f729Sjoerg // When encountering a message that does initialization (init rule),
1687330f729Sjoerg // tag the return value so that we know later on that if self has this value
1697330f729Sjoerg // then it is properly initialized.
1707330f729Sjoerg
1717330f729Sjoerg // FIXME: A callback should disable checkers at the start of functions.
1727330f729Sjoerg if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
1737330f729Sjoerg C.getCurrentAnalysisDeclContext()->getDecl())))
1747330f729Sjoerg return;
1757330f729Sjoerg
1767330f729Sjoerg if (isInitMessage(Msg)) {
1777330f729Sjoerg // Tag the return value as the result of an initializer.
1787330f729Sjoerg ProgramStateRef state = C.getState();
1797330f729Sjoerg
1807330f729Sjoerg // FIXME this really should be context sensitive, where we record
1817330f729Sjoerg // the current stack frame (for IPA). Also, we need to clean this
1827330f729Sjoerg // value out when we return from this method.
1837330f729Sjoerg state = state->set<CalledInit>(true);
1847330f729Sjoerg
1857330f729Sjoerg SVal V = C.getSVal(Msg.getOriginExpr());
1867330f729Sjoerg addSelfFlag(state, V, SelfFlag_InitRes, C);
1877330f729Sjoerg return;
1887330f729Sjoerg }
1897330f729Sjoerg
1907330f729Sjoerg // We don't check for an invalid 'self' in an obj-c message expression to cut
1917330f729Sjoerg // down false positives where logging functions get information from self
1927330f729Sjoerg // (like its class) or doing "invalidation" on self when the initialization
1937330f729Sjoerg // fails.
1947330f729Sjoerg }
1957330f729Sjoerg
checkPostStmt(const ObjCIvarRefExpr * E,CheckerContext & C) const1967330f729Sjoerg void ObjCSelfInitChecker::checkPostStmt(const ObjCIvarRefExpr *E,
1977330f729Sjoerg CheckerContext &C) const {
1987330f729Sjoerg // FIXME: A callback should disable checkers at the start of functions.
1997330f729Sjoerg if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
2007330f729Sjoerg C.getCurrentAnalysisDeclContext()->getDecl())))
2017330f729Sjoerg return;
2027330f729Sjoerg
2037330f729Sjoerg checkForInvalidSelf(
2047330f729Sjoerg E->getBase(), C,
2057330f729Sjoerg "Instance variable used while 'self' is not set to the result of "
2067330f729Sjoerg "'[(super or self) init...]'");
2077330f729Sjoerg }
2087330f729Sjoerg
checkPreStmt(const ReturnStmt * S,CheckerContext & C) const2097330f729Sjoerg void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
2107330f729Sjoerg CheckerContext &C) const {
2117330f729Sjoerg // FIXME: A callback should disable checkers at the start of functions.
2127330f729Sjoerg if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
2137330f729Sjoerg C.getCurrentAnalysisDeclContext()->getDecl())))
2147330f729Sjoerg return;
2157330f729Sjoerg
2167330f729Sjoerg checkForInvalidSelf(S->getRetValue(), C,
2177330f729Sjoerg "Returning 'self' while it is not set to the result of "
2187330f729Sjoerg "'[(super or self) init...]'");
2197330f729Sjoerg }
2207330f729Sjoerg
2217330f729Sjoerg // When a call receives a reference to 'self', [Pre/Post]Call pass
2227330f729Sjoerg // the SelfFlags from the object 'self' points to before the call to the new
2237330f729Sjoerg // object after the call. This is to avoid invalidation of 'self' by logging
2247330f729Sjoerg // functions.
2257330f729Sjoerg // Another common pattern in classes with multiple initializers is to put the
2267330f729Sjoerg // subclass's common initialization bits into a static function that receives
2277330f729Sjoerg // the value of 'self', e.g:
2287330f729Sjoerg // @code
2297330f729Sjoerg // if (!(self = [super init]))
2307330f729Sjoerg // return nil;
2317330f729Sjoerg // if (!(self = _commonInit(self)))
2327330f729Sjoerg // return nil;
2337330f729Sjoerg // @endcode
2347330f729Sjoerg // Until we can use inter-procedural analysis, in such a call, transfer the
2357330f729Sjoerg // SelfFlags to the result of the call.
2367330f729Sjoerg
checkPreCall(const CallEvent & CE,CheckerContext & C) const2377330f729Sjoerg void ObjCSelfInitChecker::checkPreCall(const CallEvent &CE,
2387330f729Sjoerg CheckerContext &C) const {
2397330f729Sjoerg // FIXME: A callback should disable checkers at the start of functions.
2407330f729Sjoerg if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
2417330f729Sjoerg C.getCurrentAnalysisDeclContext()->getDecl())))
2427330f729Sjoerg return;
2437330f729Sjoerg
2447330f729Sjoerg ProgramStateRef state = C.getState();
2457330f729Sjoerg unsigned NumArgs = CE.getNumArgs();
2467330f729Sjoerg // If we passed 'self' as and argument to the call, record it in the state
2477330f729Sjoerg // to be propagated after the call.
2487330f729Sjoerg // Note, we could have just given up, but try to be more optimistic here and
2497330f729Sjoerg // assume that the functions are going to continue initialization or will not
2507330f729Sjoerg // modify self.
2517330f729Sjoerg for (unsigned i = 0; i < NumArgs; ++i) {
2527330f729Sjoerg SVal argV = CE.getArgSVal(i);
2537330f729Sjoerg if (isSelfVar(argV, C)) {
2547330f729Sjoerg unsigned selfFlags = getSelfFlags(state->getSVal(argV.castAs<Loc>()), C);
2557330f729Sjoerg C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
2567330f729Sjoerg return;
2577330f729Sjoerg } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
2587330f729Sjoerg unsigned selfFlags = getSelfFlags(argV, C);
2597330f729Sjoerg C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
2607330f729Sjoerg return;
2617330f729Sjoerg }
2627330f729Sjoerg }
2637330f729Sjoerg }
2647330f729Sjoerg
checkPostCall(const CallEvent & CE,CheckerContext & C) const2657330f729Sjoerg void ObjCSelfInitChecker::checkPostCall(const CallEvent &CE,
2667330f729Sjoerg CheckerContext &C) const {
2677330f729Sjoerg // FIXME: A callback should disable checkers at the start of functions.
2687330f729Sjoerg if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
2697330f729Sjoerg C.getCurrentAnalysisDeclContext()->getDecl())))
2707330f729Sjoerg return;
2717330f729Sjoerg
2727330f729Sjoerg ProgramStateRef state = C.getState();
2737330f729Sjoerg SelfFlagEnum prevFlags = (SelfFlagEnum)state->get<PreCallSelfFlags>();
2747330f729Sjoerg if (!prevFlags)
2757330f729Sjoerg return;
2767330f729Sjoerg state = state->remove<PreCallSelfFlags>();
2777330f729Sjoerg
2787330f729Sjoerg unsigned NumArgs = CE.getNumArgs();
2797330f729Sjoerg for (unsigned i = 0; i < NumArgs; ++i) {
2807330f729Sjoerg SVal argV = CE.getArgSVal(i);
2817330f729Sjoerg if (isSelfVar(argV, C)) {
2827330f729Sjoerg // If the address of 'self' is being passed to the call, assume that the
2837330f729Sjoerg // 'self' after the call will have the same flags.
2847330f729Sjoerg // EX: log(&self)
2857330f729Sjoerg addSelfFlag(state, state->getSVal(argV.castAs<Loc>()), prevFlags, C);
2867330f729Sjoerg return;
2877330f729Sjoerg } else if (hasSelfFlag(argV, SelfFlag_Self, C)) {
2887330f729Sjoerg // If 'self' is passed to the call by value, assume that the function
2897330f729Sjoerg // returns 'self'. So assign the flags, which were set on 'self' to the
2907330f729Sjoerg // return value.
2917330f729Sjoerg // EX: self = performMoreInitialization(self)
2927330f729Sjoerg addSelfFlag(state, CE.getReturnValue(), prevFlags, C);
2937330f729Sjoerg return;
2947330f729Sjoerg }
2957330f729Sjoerg }
2967330f729Sjoerg
2977330f729Sjoerg C.addTransition(state);
2987330f729Sjoerg }
2997330f729Sjoerg
checkLocation(SVal location,bool isLoad,const Stmt * S,CheckerContext & C) const3007330f729Sjoerg void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
3017330f729Sjoerg const Stmt *S,
3027330f729Sjoerg CheckerContext &C) const {
3037330f729Sjoerg if (!shouldRunOnFunctionOrMethod(dyn_cast<NamedDecl>(
3047330f729Sjoerg C.getCurrentAnalysisDeclContext()->getDecl())))
3057330f729Sjoerg return;
3067330f729Sjoerg
3077330f729Sjoerg // Tag the result of a load from 'self' so that we can easily know that the
3087330f729Sjoerg // value is the object that 'self' points to.
3097330f729Sjoerg ProgramStateRef state = C.getState();
3107330f729Sjoerg if (isSelfVar(location, C))
3117330f729Sjoerg addSelfFlag(state, state->getSVal(location.castAs<Loc>()), SelfFlag_Self,
3127330f729Sjoerg C);
3137330f729Sjoerg }
3147330f729Sjoerg
3157330f729Sjoerg
checkBind(SVal loc,SVal val,const Stmt * S,CheckerContext & C) const3167330f729Sjoerg void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
3177330f729Sjoerg CheckerContext &C) const {
3187330f729Sjoerg // Allow assignment of anything to self. Self is a local variable in the
3197330f729Sjoerg // initializer, so it is legal to assign anything to it, like results of
3207330f729Sjoerg // static functions/method calls. After self is assigned something we cannot
3217330f729Sjoerg // reason about, stop enforcing the rules.
3227330f729Sjoerg // (Only continue checking if the assigned value should be treated as self.)
3237330f729Sjoerg if ((isSelfVar(loc, C)) &&
3247330f729Sjoerg !hasSelfFlag(val, SelfFlag_InitRes, C) &&
3257330f729Sjoerg !hasSelfFlag(val, SelfFlag_Self, C) &&
3267330f729Sjoerg !isSelfVar(val, C)) {
3277330f729Sjoerg
3287330f729Sjoerg // Stop tracking the checker-specific state in the state.
3297330f729Sjoerg ProgramStateRef State = C.getState();
3307330f729Sjoerg State = State->remove<CalledInit>();
3317330f729Sjoerg if (SymbolRef sym = loc.getAsSymbol())
3327330f729Sjoerg State = State->remove<SelfFlag>(sym);
3337330f729Sjoerg C.addTransition(State);
3347330f729Sjoerg }
3357330f729Sjoerg }
3367330f729Sjoerg
printState(raw_ostream & Out,ProgramStateRef State,const char * NL,const char * Sep) const3377330f729Sjoerg void ObjCSelfInitChecker::printState(raw_ostream &Out, ProgramStateRef State,
3387330f729Sjoerg const char *NL, const char *Sep) const {
3397330f729Sjoerg SelfFlagTy FlagMap = State->get<SelfFlag>();
3407330f729Sjoerg bool DidCallInit = State->get<CalledInit>();
3417330f729Sjoerg SelfFlagEnum PreCallFlags = (SelfFlagEnum)State->get<PreCallSelfFlags>();
3427330f729Sjoerg
3437330f729Sjoerg if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
3447330f729Sjoerg return;
3457330f729Sjoerg
3467330f729Sjoerg Out << Sep << NL << *this << " :" << NL;
3477330f729Sjoerg
3487330f729Sjoerg if (DidCallInit)
3497330f729Sjoerg Out << " An init method has been called." << NL;
3507330f729Sjoerg
3517330f729Sjoerg if (PreCallFlags != SelfFlag_None) {
3527330f729Sjoerg if (PreCallFlags & SelfFlag_Self) {
3537330f729Sjoerg Out << " An argument of the current call came from the 'self' variable."
3547330f729Sjoerg << NL;
3557330f729Sjoerg }
3567330f729Sjoerg if (PreCallFlags & SelfFlag_InitRes) {
3577330f729Sjoerg Out << " An argument of the current call came from an init method."
3587330f729Sjoerg << NL;
3597330f729Sjoerg }
3607330f729Sjoerg }
3617330f729Sjoerg
3627330f729Sjoerg Out << NL;
3637330f729Sjoerg for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end();
3647330f729Sjoerg I != E; ++I) {
3657330f729Sjoerg Out << I->first << " : ";
3667330f729Sjoerg
3677330f729Sjoerg if (I->second == SelfFlag_None)
3687330f729Sjoerg Out << "none";
3697330f729Sjoerg
3707330f729Sjoerg if (I->second & SelfFlag_Self)
3717330f729Sjoerg Out << "self variable";
3727330f729Sjoerg
3737330f729Sjoerg if (I->second & SelfFlag_InitRes) {
3747330f729Sjoerg if (I->second != SelfFlag_InitRes)
3757330f729Sjoerg Out << " | ";
3767330f729Sjoerg Out << "result of init method";
3777330f729Sjoerg }
3787330f729Sjoerg
3797330f729Sjoerg Out << NL;
3807330f729Sjoerg }
3817330f729Sjoerg }
3827330f729Sjoerg
3837330f729Sjoerg
3847330f729Sjoerg // FIXME: A callback should disable checkers at the start of functions.
shouldRunOnFunctionOrMethod(const NamedDecl * ND)3857330f729Sjoerg static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND) {
3867330f729Sjoerg if (!ND)
3877330f729Sjoerg return false;
3887330f729Sjoerg
3897330f729Sjoerg const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND);
3907330f729Sjoerg if (!MD)
3917330f729Sjoerg return false;
3927330f729Sjoerg if (!isInitializationMethod(MD))
3937330f729Sjoerg return false;
3947330f729Sjoerg
3957330f729Sjoerg // self = [super init] applies only to NSObject subclasses.
3967330f729Sjoerg // For instance, NSProxy doesn't implement -init.
3977330f729Sjoerg ASTContext &Ctx = MD->getASTContext();
3987330f729Sjoerg IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
3997330f729Sjoerg ObjCInterfaceDecl *ID = MD->getClassInterface()->getSuperClass();
4007330f729Sjoerg for ( ; ID ; ID = ID->getSuperClass()) {
4017330f729Sjoerg IdentifierInfo *II = ID->getIdentifier();
4027330f729Sjoerg
4037330f729Sjoerg if (II == NSObjectII)
4047330f729Sjoerg break;
4057330f729Sjoerg }
4067330f729Sjoerg return ID != nullptr;
4077330f729Sjoerg }
4087330f729Sjoerg
4097330f729Sjoerg /// Returns true if the location is 'self'.
isSelfVar(SVal location,CheckerContext & C)4107330f729Sjoerg static bool isSelfVar(SVal location, CheckerContext &C) {
4117330f729Sjoerg AnalysisDeclContext *analCtx = C.getCurrentAnalysisDeclContext();
4127330f729Sjoerg if (!analCtx->getSelfDecl())
4137330f729Sjoerg return false;
4147330f729Sjoerg if (!location.getAs<loc::MemRegionVal>())
4157330f729Sjoerg return false;
4167330f729Sjoerg
4177330f729Sjoerg loc::MemRegionVal MRV = location.castAs<loc::MemRegionVal>();
4187330f729Sjoerg if (const DeclRegion *DR = dyn_cast<DeclRegion>(MRV.stripCasts()))
4197330f729Sjoerg return (DR->getDecl() == analCtx->getSelfDecl());
4207330f729Sjoerg
4217330f729Sjoerg return false;
4227330f729Sjoerg }
4237330f729Sjoerg
isInitializationMethod(const ObjCMethodDecl * MD)4247330f729Sjoerg static bool isInitializationMethod(const ObjCMethodDecl *MD) {
4257330f729Sjoerg return MD->getMethodFamily() == OMF_init;
4267330f729Sjoerg }
4277330f729Sjoerg
isInitMessage(const ObjCMethodCall & Call)4287330f729Sjoerg static bool isInitMessage(const ObjCMethodCall &Call) {
4297330f729Sjoerg return Call.getMethodFamily() == OMF_init;
4307330f729Sjoerg }
4317330f729Sjoerg
4327330f729Sjoerg //===----------------------------------------------------------------------===//
4337330f729Sjoerg // Registration.
4347330f729Sjoerg //===----------------------------------------------------------------------===//
4357330f729Sjoerg
registerObjCSelfInitChecker(CheckerManager & mgr)4367330f729Sjoerg void ento::registerObjCSelfInitChecker(CheckerManager &mgr) {
4377330f729Sjoerg mgr.registerChecker<ObjCSelfInitChecker>();
4387330f729Sjoerg }
4397330f729Sjoerg
shouldRegisterObjCSelfInitChecker(const CheckerManager & mgr)440*e038c9c4Sjoerg bool ento::shouldRegisterObjCSelfInitChecker(const CheckerManager &mgr) {
4417330f729Sjoerg return true;
4427330f729Sjoerg }
443