#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/Analysis/PathDiagnostic.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Expr.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
using namespace clang;
using namespace ento;
namespace {
struct SelectorDescriptor {
const char *SelectorName;
unsigned ArgumentCount;
};
class FindSuperCallVisitor : public RecursiveASTVisitor<FindSuperCallVisitor> {
public:
explicit FindSuperCallVisitor(Selector S) : DoesCallSuper(false), Sel(S) {}
bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
if (E->getSelector() == Sel)
if (E->getReceiverKind() == ObjCMessageExpr::SuperInstance)
DoesCallSuper = true;
return !DoesCallSuper;
}
bool DoesCallSuper;
private:
Selector Sel;
};
class ObjCSuperCallChecker : public Checker<
check::ASTDecl<ObjCImplementationDecl> > {
public:
ObjCSuperCallChecker() : IsInitialized(false) {}
void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager &Mgr,
BugReporter &BR) const;
private:
bool isCheckableClass(const ObjCImplementationDecl *D,
StringRef &SuperclassName) const;
void initializeSelectors(ASTContext &Ctx) const;
void fillSelectors(ASTContext &Ctx, ArrayRef<SelectorDescriptor> Sel,
StringRef ClassName) const;
mutable llvm::StringMap<llvm::SmallPtrSet<Selector, 16>> SelectorsForClass;
mutable bool IsInitialized;
};
}
bool ObjCSuperCallChecker::isCheckableClass(const ObjCImplementationDecl *D,
StringRef &SuperclassName) const {
const ObjCInterfaceDecl *ID = D->getClassInterface()->getSuperClass();
for ( ; ID ; ID = ID->getSuperClass())
{
SuperclassName = ID->getIdentifier()->getName();
if (SelectorsForClass.count(SuperclassName))
return true;
}
return false;
}
void ObjCSuperCallChecker::fillSelectors(ASTContext &Ctx,
ArrayRef<SelectorDescriptor> Sel,
StringRef ClassName) const {
llvm::SmallPtrSet<Selector, 16> &ClassSelectors =
SelectorsForClass[ClassName];
for (ArrayRef<SelectorDescriptor>::iterator I = Sel.begin(), E = Sel.end();
I != E; ++I) {
SelectorDescriptor Descriptor = *I;
assert(Descriptor.ArgumentCount <= 1);
IdentifierInfo *II = &Ctx.Idents.get(Descriptor.SelectorName);
Selector Sel = Ctx.Selectors.getSelector(Descriptor.ArgumentCount, &II);
ClassSelectors.insert(Sel);
}
}
void ObjCSuperCallChecker::initializeSelectors(ASTContext &Ctx) const {
{ const SelectorDescriptor Selectors[] = {
{ "addChildViewController", 1 },
{ "viewDidAppear", 1 },
{ "viewDidDisappear", 1 },
{ "viewWillAppear", 1 },
{ "viewWillDisappear", 1 },
{ "removeFromParentViewController", 0 },
{ "didReceiveMemoryWarning", 0 },
{ "viewDidUnload", 0 },
{ "viewDidLoad", 0 },
{ "viewWillUnload", 0 },
{ "updateViewConstraints", 0 },
{ "encodeRestorableStateWithCoder", 1 },
{ "restoreStateWithCoder", 1 }};
fillSelectors(Ctx, Selectors, "UIViewController");
}
{ const SelectorDescriptor Selectors[] = {
{ "resignFirstResponder", 0 }};
fillSelectors(Ctx, Selectors, "UIResponder");
}
{ const SelectorDescriptor Selectors[] = {
{ "encodeRestorableStateWithCoder", 1 },
{ "restoreStateWithCoder", 1 }};
fillSelectors(Ctx, Selectors, "NSResponder");
}
{ const SelectorDescriptor Selectors[] = {
{ "encodeRestorableStateWithCoder", 1 },
{ "restoreStateWithCoder", 1 }};
fillSelectors(Ctx, Selectors, "NSDocument");
}
IsInitialized = true;
}
void ObjCSuperCallChecker::checkASTDecl(const ObjCImplementationDecl *D,
AnalysisManager &Mgr,
BugReporter &BR) const {
ASTContext &Ctx = BR.getContext();
if (!IsInitialized)
initializeSelectors(Ctx);
StringRef SuperclassName;
if (!isCheckableClass(D, SuperclassName))
return;
for (auto *MD : D->instance_methods()) {
Selector S = MD->getSelector();
if (!SelectorsForClass[SuperclassName].count(S))
continue;
if (MD->getBody())
{
FindSuperCallVisitor Visitor(S);
Visitor.TraverseDecl(MD);
if (!Visitor.DoesCallSuper) {
PathDiagnosticLocation DLoc =
PathDiagnosticLocation::createEnd(MD->getBody(),
BR.getSourceManager(),
Mgr.getAnalysisDeclContext(D));
const char *Name = "Missing call to superclass";
SmallString<320> Buf;
llvm::raw_svector_ostream os(Buf);
os << "The '" << S.getAsString()
<< "' instance method in " << SuperclassName.str() << " subclass '"
<< *D << "' is missing a [super " << S.getAsString() << "] call";
BR.EmitBasicReport(MD, this, Name, categories::CoreFoundationObjectiveC,
os.str(), DLoc);
}
}
}
}
void ento::registerObjCSuperCallChecker(CheckerManager &Mgr) {
Mgr.registerChecker<ObjCSuperCallChecker>();
}
bool ento::shouldRegisterObjCSuperCallChecker(const CheckerManager &mgr) {
return true;
}