47 #include "llvm/Support/raw_ostream.h"
49 using namespace clang;
58 class ObjCSelfInitChecker :
public Checker< check::PostObjCMessage,
59 check::PostStmt<ObjCIvarRefExpr>,
60 check::PreStmt<ReturnStmt>,
65 mutable std::unique_ptr<BugType> BT;
68 const char *errorStr)
const;
71 ObjCSelfInitChecker() {}
75 void checkLocation(
SVal location,
bool isLoad,
const Stmt *
S,
83 const char *NL,
const char *Sep)
const override;
94 SelfFlag_InitRes = 0x2
109 if (
const unsigned *attachedFlags = state->get<SelfFlag>(sym))
111 return SelfFlag_None;
122 state = state->set<SelfFlag>(sym,
getSelfFlags(val, state) | flag);
145 const char *errorStr)
const {
149 if (!C.
getState()->get<CalledInit>())
161 BT.reset(
new BugType(
this,
"Missing \"self = [(super or self) init...]\"",
163 C.
emitReport(llvm::make_unique<BugReport>(*BT, errorStr, N));
166 void ObjCSelfInitChecker::checkPostObjCMessage(
const ObjCMethodCall &Msg,
184 state = state->set<CalledInit>(
true);
206 "Instance variable used while 'self' is not set to the result of "
207 "'[(super or self) init...]'");
210 void ObjCSelfInitChecker::checkPreStmt(
const ReturnStmt *
S,
218 "Returning 'self' while it is not set to the result of "
219 "'[(super or self) init...]'");
238 void ObjCSelfInitChecker::checkPreCall(
const CallEvent &CE,
252 for (
unsigned i = 0; i < NumArgs; ++i) {
266 void ObjCSelfInitChecker::checkPostCall(
const CallEvent &CE,
277 state = state->remove<PreCallSelfFlags>();
280 for (
unsigned i = 0; i < NumArgs; ++i) {
301 void ObjCSelfInitChecker::checkLocation(
SVal location,
bool isLoad,
317 void ObjCSelfInitChecker::checkBind(
SVal loc,
SVal val,
const Stmt *S,
331 State = State->remove<CalledInit>();
333 State = State->remove<SelfFlag>(sym);
338 void ObjCSelfInitChecker::printState(raw_ostream &Out,
ProgramStateRef State,
339 const char *NL,
const char *Sep)
const {
340 SelfFlagTy FlagMap = State->get<SelfFlag>();
341 bool DidCallInit = State->get<CalledInit>();
344 if (FlagMap.isEmpty() && !DidCallInit && !PreCallFlags)
347 Out << Sep << NL << *
this <<
" :" << NL;
350 Out <<
" An init method has been called." << NL;
352 if (PreCallFlags != SelfFlag_None) {
353 if (PreCallFlags & SelfFlag_Self) {
354 Out <<
" An argument of the current call came from the 'self' variable."
357 if (PreCallFlags & SelfFlag_InitRes) {
358 Out <<
" An argument of the current call came from an init method."
364 for (SelfFlagTy::iterator I = FlagMap.begin(), E = FlagMap.end();
366 Out << I->first <<
" : ";
368 if (I->second == SelfFlag_None)
371 if (I->second & SelfFlag_Self)
372 Out <<
"self variable";
374 if (I->second & SelfFlag_InitRes) {
375 if (I->second != SelfFlag_InitRes)
377 Out <<
"result of init method";
404 if (II == NSObjectII)
virtual SVal getArgSVal(unsigned Index) const
Returns the value of a given argument at the time of the call.
const Expr * getBase() const
const char *const CoreFoundationObjectiveC
ObjCInterfaceDecl * getClassInterface()
const MemRegion * stripCasts(bool StripBaseCasts=true) const
Get the underlining region and strip casts.
IdentifierInfo * getIdentifier() const
ExplodedNode * addTransition(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generates a new transition in the program state graph (ExplodedGraph). Uses the default CheckerContex...
const ImplicitParamDecl * getSelfDecl() const
Symbolic value. These values used to capture symbolic execution of the program.
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...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
static bool isInitMessage(const ObjCMethodCall &Msg)
static bool isInitializationMethod(const ObjCMethodDecl *MD)
Represents any expression that calls an Objective-C method.
ObjCMethodFamily getMethodFamily() const
Determines the family of this method.
static void addSelfFlag(ProgramStateRef state, SVal val, SelfFlagEnum flag, CheckerContext &C)
static SelfFlagEnum getSelfFlags(SVal val, ProgramStateRef state)
A call receiving a reference to 'self' invalidates the object that 'self' contains. This keeps the "self flags" assigned to the 'self' object before the call so we can assign them to the new object that 'self' points to after the call.
const Decl * getDecl() const
Represents an ObjC class declaration.
ExplodedNode * generateSink(ProgramStateRef State=nullptr, ExplodedNode *Pred=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a sink node. Generating a sink stops exploration of the given path.
ID
Defines the set of possible language-specific address spaces.
const ProgramStateRef & getState() const
static bool shouldRunOnFunctionOrMethod(const NamedDecl *ND)
Optional< T > getAs() const
Convert to the specified SVal type, returning None if this SVal is not of the desired type...
ObjCMethodFamily getMethodFamily() const
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
static bool hasSelfFlag(SVal val, SelfFlagEnum flag, CheckerContext &C)
#define REGISTER_TRAIT_WITH_PROGRAMSTATE(Name, Type)
CHECKER * registerChecker()
Used to register checkers.
IdentifierInfo & get(StringRef Name)
Return the identifier token info for the specified named identifier.
ASTContext & getASTContext() const LLVM_READONLY
static bool isSelfVar(SVal location, CheckerContext &C)
Returns true if the location is 'self'.
REGISTER_MAP_WITH_PROGRAMSTATE(AllocatedData, SymbolRef, MacOSKeychainAPIChecker::AllocationState) static bool isEnclosingFunctionParam(const Expr *E)
const Expr * getRetValue() const
Represents an abstract call to a function or method along a particular path.
ObjCIvarRefExpr - A reference to an ObjC instance variable.
AnalysisDeclContext * getCurrentAnalysisDeclContext() const
virtual unsigned getNumArgs() const =0
Returns the number of arguments (explicit and implicit).
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef. Otherwise, return 0.
ObjCInterfaceDecl * getSuperClass() const
virtual const ObjCMessageExpr * getOriginExpr() const
SVal getReturnValue() const
Returns the return value of the call.
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
const LocationContext * getLocationContext() const