IvarInvalidationChecker.cpp [plain text]
#include "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/StmtVisitor.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallString.h"
using namespace clang;
using namespace ento;
namespace {
class IvarInvalidationChecker :
public Checker<check::ASTDecl<ObjCMethodDecl> > {
typedef llvm::DenseSet<const ObjCMethodDecl*> MethodSet;
typedef llvm::DenseMap<const ObjCMethodDecl*,
const ObjCIvarDecl*> MethToIvarMapTy;
typedef llvm::DenseMap<const ObjCPropertyDecl*,
const ObjCIvarDecl*> PropToIvarMapTy;
typedef llvm::DenseMap<const ObjCIvarDecl*,
const ObjCPropertyDecl*> IvarToPropMapTy;
struct IvarInfo {
bool IsInvalidated;
MethodSet InvalidationMethods;
IvarInfo() : IsInvalidated(false) {}
void addInvalidationMethod(const ObjCMethodDecl *MD) {
InvalidationMethods.insert(MD);
}
bool needsInvalidation() const {
return !InvalidationMethods.empty();
}
void markInvalidated() {
IsInvalidated = true;
}
bool markInvalidated(const ObjCMethodDecl *MD) {
if (IsInvalidated)
return true;
for (MethodSet::iterator I = InvalidationMethods.begin(),
E = InvalidationMethods.end(); I != E; ++I) {
if (*I == MD) {
IsInvalidated = true;
return true;
}
}
return false;
}
bool isInvalidated() const {
return IsInvalidated;
}
};
typedef llvm::DenseMap<const ObjCIvarDecl*, IvarInfo> IvarSet;
class MethodCrawler : public ConstStmtVisitor<MethodCrawler> {
IvarSet &IVars;
bool &CalledAnotherInvalidationMethod;
const MethToIvarMapTy &PropertySetterToIvarMap;
const MethToIvarMapTy &PropertyGetterToIvarMap;
const PropToIvarMapTy &PropertyToIvarMap;
const ObjCMethodDecl *InvalidationMethod;
ASTContext &Ctx;
const Expr *peel(const Expr *E) const;
bool isZero(const Expr *E) const;
void markInvalidated(const ObjCIvarDecl *Iv);
void checkObjCIvarRefExpr(const ObjCIvarRefExpr *IvarRef);
void checkObjCPropertyRefExpr(const ObjCPropertyRefExpr *PA);
void checkObjCMessageExpr(const ObjCMessageExpr *ME);
void check(const Expr *E);
public:
MethodCrawler(IvarSet &InIVars,
bool &InCalledAnotherInvalidationMethod,
const MethToIvarMapTy &InPropertySetterToIvarMap,
const MethToIvarMapTy &InPropertyGetterToIvarMap,
const PropToIvarMapTy &InPropertyToIvarMap,
ASTContext &InCtx)
: IVars(InIVars),
CalledAnotherInvalidationMethod(InCalledAnotherInvalidationMethod),
PropertySetterToIvarMap(InPropertySetterToIvarMap),
PropertyGetterToIvarMap(InPropertyGetterToIvarMap),
PropertyToIvarMap(InPropertyToIvarMap),
InvalidationMethod(0),
Ctx(InCtx) {}
void VisitStmt(const Stmt *S) { VisitChildren(S); }
void VisitBinaryOperator(const BinaryOperator *BO);
void VisitObjCMessageExpr(const ObjCMessageExpr *ME);
void VisitChildren(const Stmt *S) {
for (Stmt::const_child_range I = S->children(); I; ++I) {
if (*I)
this->Visit(*I);
if (CalledAnotherInvalidationMethod)
return;
}
}
};
static void containsInvalidationMethod(const ObjCContainerDecl *D,
IvarInfo &Out);
static bool trackIvar(const ObjCIvarDecl *Iv, IvarSet &TrackedIvars);
static const ObjCIvarDecl *findPropertyBackingIvar(
const ObjCPropertyDecl *Prop,
const ObjCInterfaceDecl *InterfaceD,
IvarSet &TrackedIvars);
public:
void checkASTDecl(const ObjCMethodDecl *D, AnalysisManager& Mgr,
BugReporter &BR) const;
};
static bool isInvalidationMethod(const ObjCMethodDecl *M) {
for (specific_attr_iterator<AnnotateAttr>
AI = M->specific_attr_begin<AnnotateAttr>(),
AE = M->specific_attr_end<AnnotateAttr>(); AI != AE; ++AI) {
const AnnotateAttr *Ann = *AI;
if (Ann->getAnnotation() == "objc_instance_variable_invalidator")
return true;
}
return false;
}
void IvarInvalidationChecker::containsInvalidationMethod(
const ObjCContainerDecl *D, IvarInfo &OutInfo) {
if (!D)
return;
for (ObjCContainerDecl::method_iterator
I = D->meth_begin(),
E = D->meth_end(); I != E; ++I) {
const ObjCMethodDecl *MDI = *I;
if (isInvalidationMethod(MDI))
OutInfo.addInvalidationMethod(
cast<ObjCMethodDecl>(MDI->getCanonicalDecl()));
}
if (const ObjCInterfaceDecl *InterfaceD = dyn_cast<ObjCInterfaceDecl>(D)) {
for (ObjCInterfaceDecl::protocol_iterator
I = InterfaceD->protocol_begin(),
E = InterfaceD->protocol_end(); I != E; ++I) {
containsInvalidationMethod(*I, OutInfo);
}
containsInvalidationMethod(InterfaceD->getSuperClass(), OutInfo);
return;
}
if (const ObjCProtocolDecl *ProtD = dyn_cast<ObjCProtocolDecl>(D)) {
for (ObjCInterfaceDecl::protocol_iterator
I = ProtD->protocol_begin(),
E = ProtD->protocol_end(); I != E; ++I) {
containsInvalidationMethod(*I, OutInfo);
}
return;
}
llvm_unreachable("One of the casts above should have succeeded.");
}
bool IvarInvalidationChecker::trackIvar(const ObjCIvarDecl *Iv,
IvarSet &TrackedIvars) {
QualType IvQTy = Iv->getType();
const ObjCObjectPointerType *IvTy = IvQTy->getAs<ObjCObjectPointerType>();
if (!IvTy)
return false;
const ObjCInterfaceDecl *IvInterf = IvTy->getInterfaceDecl();
IvarInfo Info;
containsInvalidationMethod(IvInterf, Info);
if (Info.needsInvalidation()) {
TrackedIvars[cast<ObjCIvarDecl>(Iv->getCanonicalDecl())] = Info;
return true;
}
return false;
}
const ObjCIvarDecl *IvarInvalidationChecker::findPropertyBackingIvar(
const ObjCPropertyDecl *Prop,
const ObjCInterfaceDecl *InterfaceD,
IvarSet &TrackedIvars) {
const ObjCIvarDecl *IvarD = 0;
IvarD = Prop->getPropertyIvarDecl();
if (IvarD) {
if (TrackedIvars.count(IvarD)) {
return IvarD;
}
if (trackIvar(IvarD, TrackedIvars))
return IvarD;
}
StringRef PropName = Prop->getIdentifier()->getName();
for (IvarSet::const_iterator I = TrackedIvars.begin(),
E = TrackedIvars.end(); I != E; ++I) {
const ObjCIvarDecl *Iv = I->first;
StringRef IvarName = Iv->getName();
if (IvarName == PropName)
return Iv;
SmallString<128> PropNameWithUnderscore;
{
llvm::raw_svector_ostream os(PropNameWithUnderscore);
os << '_' << PropName;
}
if (IvarName == PropNameWithUnderscore.str())
return Iv;
}
return 0;
}
void IvarInvalidationChecker::checkASTDecl(const ObjCMethodDecl *D,
AnalysisManager& Mgr,
BugReporter &BR) const {
if (!D->hasBody() || !isInvalidationMethod(D))
return;
IvarSet Ivars;
const ObjCInterfaceDecl *InterfaceD = D->getClassInterface();
ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(InterfaceD);
for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
Iv= Iv->getNextIvar())
trackIvar(Iv, Ivars);
MethToIvarMapTy PropSetterToIvarMap;
MethToIvarMapTy PropGetterToIvarMap;
PropToIvarMapTy PropertyToIvarMap;
IvarToPropMapTy IvarToPopertyMap;
ObjCInterfaceDecl::PropertyMap PropMap;
InterfaceD->collectPropertiesToImplement(PropMap);
for (ObjCInterfaceDecl::PropertyMap::iterator
I = PropMap.begin(), E = PropMap.end(); I != E; ++I) {
const ObjCPropertyDecl *PD = I->second;
const ObjCIvarDecl *ID = findPropertyBackingIvar(PD, InterfaceD, Ivars);
if (!ID) {
continue;
}
PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
PropertyToIvarMap[PD] = ID;
IvarToPopertyMap[ID] = PD;
const ObjCMethodDecl *SetterD = PD->getSetterMethodDecl();
if (SetterD) {
SetterD = cast<ObjCMethodDecl>(SetterD->getCanonicalDecl());
PropSetterToIvarMap[SetterD] = ID;
}
const ObjCMethodDecl *GetterD = PD->getGetterMethodDecl();
if (GetterD) {
GetterD = cast<ObjCMethodDecl>(GetterD->getCanonicalDecl());
PropGetterToIvarMap[GetterD] = ID;
}
}
bool CalledAnotherInvalidationMethod = false;
MethodCrawler(Ivars,
CalledAnotherInvalidationMethod,
PropSetterToIvarMap,
PropGetterToIvarMap,
PropertyToIvarMap,
BR.getContext()).VisitStmt(D->getBody());
if (CalledAnotherInvalidationMethod)
return;
for (IvarSet::const_iterator I = Ivars.begin(), E = Ivars.end(); I != E; ++I){
if (!I->second.isInvalidated()) {
const ObjCIvarDecl *IvarDecl = I->first;
PathDiagnosticLocation IvarDecLocation =
PathDiagnosticLocation::createEnd(D->getBody(), BR.getSourceManager(),
Mgr.getAnalysisDeclContext(D));
SmallString<128> sbuf;
llvm::raw_svector_ostream os(sbuf);
if (IvarDecl->getSynthesize()) {
const ObjCPropertyDecl *PD = IvarToPopertyMap[IvarDecl];
assert(PD &&
"Do we synthesize ivars for something other than properties?");
os << "Property "<< PD->getName() <<
" needs to be invalidated or set to nil";
} else {
os << "Instance variable "<< IvarDecl->getName()
<< " needs to be invalidated or set to nil";
}
BR.EmitBasicReport(D,
"Incomplete invalidation",
categories::CoreFoundationObjectiveC, os.str(),
IvarDecLocation);
}
}
}
void IvarInvalidationChecker::MethodCrawler::markInvalidated(
const ObjCIvarDecl *Iv) {
IvarSet::iterator I = IVars.find(Iv);
if (I != IVars.end()) {
if (InvalidationMethod)
I->second.markInvalidated(InvalidationMethod);
else
I->second.markInvalidated();
}
}
const Expr *IvarInvalidationChecker::MethodCrawler::peel(const Expr *E) const {
E = E->IgnoreParenCasts();
if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E))
E = POE->getSyntacticForm()->IgnoreParenCasts();
if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(E))
E = OVE->getSourceExpr()->IgnoreParenCasts();
return E;
}
void IvarInvalidationChecker::MethodCrawler::checkObjCIvarRefExpr(
const ObjCIvarRefExpr *IvarRef) {
if (const Decl *D = IvarRef->getDecl())
markInvalidated(cast<ObjCIvarDecl>(D->getCanonicalDecl()));
}
void IvarInvalidationChecker::MethodCrawler::checkObjCMessageExpr(
const ObjCMessageExpr *ME) {
const ObjCMethodDecl *MD = ME->getMethodDecl();
if (MD) {
MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
MethToIvarMapTy::const_iterator IvI = PropertyGetterToIvarMap.find(MD);
if (IvI != PropertyGetterToIvarMap.end())
markInvalidated(IvI->second);
}
}
void IvarInvalidationChecker::MethodCrawler::checkObjCPropertyRefExpr(
const ObjCPropertyRefExpr *PA) {
if (PA->isExplicitProperty()) {
const ObjCPropertyDecl *PD = PA->getExplicitProperty();
if (PD) {
PD = cast<ObjCPropertyDecl>(PD->getCanonicalDecl());
PropToIvarMapTy::const_iterator IvI = PropertyToIvarMap.find(PD);
if (IvI != PropertyToIvarMap.end())
markInvalidated(IvI->second);
return;
}
}
if (PA->isImplicitProperty()) {
const ObjCMethodDecl *MD = PA->getImplicitPropertySetter();
if (MD) {
MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
MethToIvarMapTy::const_iterator IvI =PropertyGetterToIvarMap.find(MD);
if (IvI != PropertyGetterToIvarMap.end())
markInvalidated(IvI->second);
return;
}
}
}
bool IvarInvalidationChecker::MethodCrawler::isZero(const Expr *E) const {
E = peel(E);
return (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)
!= Expr::NPCK_NotNull);
}
void IvarInvalidationChecker::MethodCrawler::check(const Expr *E) {
E = peel(E);
if (const ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) {
checkObjCIvarRefExpr(IvarRef);
return;
}
if (const ObjCPropertyRefExpr *PropRef = dyn_cast<ObjCPropertyRefExpr>(E)) {
checkObjCPropertyRefExpr(PropRef);
return;
}
if (const ObjCMessageExpr *MsgExpr = dyn_cast<ObjCMessageExpr>(E)) {
checkObjCMessageExpr(MsgExpr);
return;
}
}
void IvarInvalidationChecker::MethodCrawler::VisitBinaryOperator(
const BinaryOperator *BO) {
VisitStmt(BO);
if (BO->getOpcode() != BO_Assign)
return;
if (!isZero(BO->getRHS()))
return;
check(BO->getLHS());
}
void IvarInvalidationChecker::MethodCrawler::VisitObjCMessageExpr(
const ObjCMessageExpr *ME) {
const ObjCMethodDecl *MD = ME->getMethodDecl();
const Expr *Receiver = ME->getInstanceReceiver();
if (Receiver && isInvalidationMethod(MD))
if (Receiver->isObjCSelfExpr()) {
CalledAnotherInvalidationMethod = true;
return;
}
if (MD && (ME->getNumArgs() == 1) && isZero(ME->getArg(0))) {
MD = cast<ObjCMethodDecl>(MD->getCanonicalDecl());
MethToIvarMapTy::const_iterator IvI = PropertySetterToIvarMap.find(MD);
if (IvI != PropertySetterToIvarMap.end()) {
markInvalidated(IvI->second);
return;
}
}
if (Receiver) {
InvalidationMethod = MD;
check(Receiver->IgnoreParenCasts());
InvalidationMethod = 0;
}
VisitStmt(ME);
}
}
void ento::registerIvarInvalidationChecker(CheckerManager &mgr) {
mgr.registerChecker<IvarInvalidationChecker>();
}