46#include "llvm/Support/raw_ostream.h"
57class ObjCSelfInitChecker :
public Checker< check::PostObjCMessage,
58 check::PostStmt<ObjCIvarRefExpr>,
59 check::PreStmt<ReturnStmt>,
64 const BugType BT{
this,
"Missing \"self = [(super or self) init...]\"",
68 const char *errorStr)
const;
74 void checkLocation(
SVal location,
bool isLoad,
const Stmt *S,
82 const char *NL,
const char *Sep)
const override;
93 SelfFlag_InitRes = 0x2
108 if (
const SelfFlagEnum *attachedFlags = state->get<SelfFlag>(sym))
109 return *attachedFlags;
110 return SelfFlag_None;
121 state = state->set<SelfFlag>(sym,
123 C.addTransition(state);
135 SVal exprVal =
C.getSVal(
E);
145 const char *errorStr)
const {
149 if (!
C.getState()->get<CalledInit>())
160 C.emitReport(std::make_unique<PathSensitiveBugReport>(BT, errorStr, N));
163void ObjCSelfInitChecker::checkPostObjCMessage(
const ObjCMethodCall &Msg,
171 C.getCurrentAnalysisDeclContext()->getDecl())))
181 state = state->set<CalledInit>(
true);
198 C.getCurrentAnalysisDeclContext()->getDecl())))
203 "Instance variable used while 'self' is not set to the result of "
204 "'[(super or self) init...]'");
207void ObjCSelfInitChecker::checkPreStmt(
const ReturnStmt *S,
211 C.getCurrentAnalysisDeclContext()->getDecl())))
214 checkForInvalidSelf(S->getRetValue(),
C,
215 "Returning 'self' while it is not set to the result of "
216 "'[(super or self) init...]'");
235void ObjCSelfInitChecker::checkPreCall(
const CallEvent &CE,
239 C.getCurrentAnalysisDeclContext()->getDecl())))
249 for (
unsigned i = 0; i < NumArgs; ++i) {
252 SelfFlagEnum selfFlags =
254 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
258 C.addTransition(state->set<PreCallSelfFlags>(selfFlags));
264void ObjCSelfInitChecker::checkPostCall(
const CallEvent &CE,
268 C.getCurrentAnalysisDeclContext()->getDecl())))
272 SelfFlagEnum prevFlags = state->get<PreCallSelfFlags>();
275 state = state->remove<PreCallSelfFlags>();
278 for (
unsigned i = 0; i < NumArgs; ++i) {
296 C.addTransition(state);
299void ObjCSelfInitChecker::checkLocation(
SVal location,
bool isLoad,
303 C.getCurrentAnalysisDeclContext()->getDecl())))
315void ObjCSelfInitChecker::checkBind(
SVal loc,
SVal val,
const Stmt *S,
329 State = State->remove<CalledInit>();
331 State = State->remove<SelfFlag>(sym);
332 C.addTransition(State);
336void ObjCSelfInitChecker::printState(raw_ostream &Out,
ProgramStateRef State,
337 const char *NL,
const char *Sep)
const {
338 SelfFlagTy FlagMap = State->get<SelfFlag>();
339 bool DidCallInit = State->get<CalledInit>();
340 SelfFlagEnum PreCallFlags = State->get<PreCallSelfFlags>();
342 if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
345 Out << Sep << NL << *
this <<
" :" << NL;
348 Out <<
" An init method has been called." << NL;
350 if (PreCallFlags != SelfFlag_None) {
351 if (PreCallFlags & SelfFlag_Self) {
352 Out <<
" An argument of the current call came from the 'self' variable."
355 if (PreCallFlags & SelfFlag_InitRes) {
356 Out <<
" An argument of the current call came from an init method."
362 for (
auto [Sym, Flag] : FlagMap) {
365 if (Flag == SelfFlag_None)
368 if (Flag & SelfFlag_Self)
369 Out <<
"self variable";
371 if (Flag & SelfFlag_InitRes) {
372 if (Flag != SelfFlag_InitRes)
374 Out <<
"result of init method";
398 for ( ; ID ; ID = ID->getSuperClass()) {
401 if (II == NSObjectII)
404 return ID !=
nullptr;
412 if (!isa<loc::MemRegionVal>(location))
438bool ento::shouldRegisterObjCSelfInitChecker(
const CheckerManager &mgr) {
static bool isInitMessage(const ObjCMethodCall &Msg)
static bool isSelfVar(SVal location, CheckerContext &C)
Returns true if the location is 'self'.
static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND)
static bool isInitializationMethod(const ObjCMethodDecl *MD)
static bool isInvalidSelf(const Expr *E, CheckerContext &C)
Returns true of the value of the expression is the object that 'self' points to and is an object that...
static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state)
A call receiving a reference to 'self' invalidates the object that 'self' contains.
static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C)
static void addSelfFlag(ProgramStateRef state, SVal val, SelfFlagEnum flag, CheckerContext &C)
#define REGISTER_MAP_WITH_PROGRAMSTATE(Name, Key, Value)
Declares an immutable map of type NameTy, suitable for placement into the ProgramState.
#define REGISTER_TRAIT_WITH_PROGRAMSTATE(Name, Type)
Declares a program state trait for type Type called Name, and introduce a type named NameTy.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
const ImplicitParamDecl * getSelfDecl() const
ASTContext & getASTContext() const LLVM_READONLY
This represents one expression.
One of these records is kept for each identifier that is lexed.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
This represents a decl that may have a name.
Represents an ObjC class declaration.
ObjCInterfaceDecl * getSuperClass() const
ObjCIvarRefExpr - A reference to an ObjC instance variable.
ObjCMethodDecl - Represents an instance or class method declaration.
ObjCMethodFamily getMethodFamily() const
Determines the family of this method.
ObjCInterfaceDecl * getClassInterface()
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Stmt - This represents one statement.
Represents an abstract call to a function or method along a particular path.
virtual SVal getArgSVal(unsigned Index) const
Returns the value of a given argument at the time of the call.
virtual unsigned getNumArgs() const =0
Returns the number of arguments (explicit and implicit).
SVal getReturnValue() const
Returns the return value of the call.
virtual void printState(raw_ostream &Out, ProgramStateRef State, const char *NL, const char *Sep) const
See CheckerManager::runCheckersForPrintState.
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
Represents any expression that calls an Objective-C method.
const ObjCMessageExpr * getOriginExpr() const override
Returns the expression whose value will be the result of this call.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * stripCasts(bool StripBaseCasts=true) const
Get the underlining region and strip casts.
const char *const CoreFoundationObjectiveC
The JSON file list parser is used to communicate input to InstallAPI.