2012-10-30 09:21:35 +08:00
|
|
|
//==- ObjCMissingSuperCallChecker.cpp - Check missing super-calls in ObjC --==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-10-30 09:21:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a ObjCMissingSuperCallChecker, a checker that
|
|
|
|
// analyzes a UIViewController implementation to determine if it
|
|
|
|
// correctly calls super in the methods where this is mandatory.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2012-10-30 09:21:35 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
2012-10-30 09:21:35 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2012-10-30 09:21:35 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-10-30 09:21:35 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
2012-12-13 11:06:45 +08:00
|
|
|
namespace {
|
|
|
|
struct SelectorDescriptor {
|
|
|
|
const char *SelectorName;
|
|
|
|
unsigned ArgumentCount;
|
|
|
|
};
|
2012-10-30 09:21:35 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FindSuperCallVisitor - Identify specific calls to the superclass.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Recurse if we didn't find the super call yet.
|
2015-09-08 11:50:52 +08:00
|
|
|
return !DoesCallSuper;
|
2012-10-30 09:21:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DoesCallSuper;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Selector Sel;
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-09-08 11:50:52 +08:00
|
|
|
// ObjCSuperCallChecker
|
2012-10-30 09:21:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class ObjCSuperCallChecker : public Checker<
|
|
|
|
check::ASTDecl<ObjCImplementationDecl> > {
|
|
|
|
public:
|
2012-12-13 11:06:45 +08:00
|
|
|
ObjCSuperCallChecker() : IsInitialized(false) {}
|
|
|
|
|
2012-10-30 09:21:35 +08:00
|
|
|
void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager &Mgr,
|
|
|
|
BugReporter &BR) const;
|
2012-12-13 11:06:45 +08:00
|
|
|
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::SmallSet<Selector, 16> > SelectorsForClass;
|
|
|
|
mutable bool IsInitialized;
|
2012-10-30 09:21:35 +08:00
|
|
|
};
|
2012-12-13 11:06:45 +08:00
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2012-12-13 11:06:45 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determine whether the given class has a superclass that we want
|
2012-12-13 11:06:45 +08:00
|
|
|
/// to check. The name of the found superclass is stored in SuperclassName.
|
|
|
|
///
|
2012-12-14 02:26:05 +08:00
|
|
|
/// \param D The declaration to check for superclasses.
|
2012-12-13 11:06:45 +08:00
|
|
|
/// \param[out] SuperclassName On return, the found superclass name.
|
|
|
|
bool ObjCSuperCallChecker::isCheckableClass(const ObjCImplementationDecl *D,
|
|
|
|
StringRef &SuperclassName) const {
|
2015-08-08 09:31:51 +08:00
|
|
|
const ObjCInterfaceDecl *ID = D->getClassInterface()->getSuperClass();
|
2012-12-13 11:06:45 +08:00
|
|
|
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::SmallSet<Selector, 16> &ClassSelectors = SelectorsForClass[ClassName];
|
|
|
|
// Fill the Selectors SmallSet with all selectors we want to check.
|
|
|
|
for (ArrayRef<SelectorDescriptor>::iterator I = Sel.begin(), E = Sel.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
SelectorDescriptor Descriptor = *I;
|
|
|
|
assert(Descriptor.ArgumentCount <= 1); // No multi-argument selectors yet.
|
|
|
|
|
|
|
|
// Get the selector.
|
|
|
|
IdentifierInfo *II = &Ctx.Idents.get(Descriptor.SelectorName);
|
|
|
|
|
|
|
|
Selector Sel = Ctx.Selectors.getSelector(Descriptor.ArgumentCount, &II);
|
|
|
|
ClassSelectors.insert(Sel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCSuperCallChecker::initializeSelectors(ASTContext &Ctx) const {
|
|
|
|
|
|
|
|
{ // Initialize selectors for: UIViewController
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Initialize selectors for: UIResponder
|
|
|
|
const SelectorDescriptor Selectors[] = {
|
|
|
|
{ "resignFirstResponder", 0 }};
|
|
|
|
|
|
|
|
fillSelectors(Ctx, Selectors, "UIResponder");
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Initialize selectors for: NSResponder
|
|
|
|
const SelectorDescriptor Selectors[] = {
|
|
|
|
{ "encodeRestorableStateWithCoder", 1 },
|
|
|
|
{ "restoreStateWithCoder", 1 }};
|
|
|
|
|
|
|
|
fillSelectors(Ctx, Selectors, "NSResponder");
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Initialize selectors for: NSDocument
|
|
|
|
const SelectorDescriptor Selectors[] = {
|
|
|
|
{ "encodeRestorableStateWithCoder", 1 },
|
|
|
|
{ "restoreStateWithCoder", 1 }};
|
|
|
|
|
|
|
|
fillSelectors(Ctx, Selectors, "NSDocument");
|
|
|
|
}
|
|
|
|
|
|
|
|
IsInitialized = true;
|
2012-10-30 09:21:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ObjCSuperCallChecker::checkASTDecl(const ObjCImplementationDecl *D,
|
|
|
|
AnalysisManager &Mgr,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
ASTContext &Ctx = BR.getContext();
|
|
|
|
|
2012-12-13 11:06:45 +08:00
|
|
|
// We need to initialize the selector table once.
|
|
|
|
if (!IsInitialized)
|
|
|
|
initializeSelectors(Ctx);
|
2012-10-30 09:21:35 +08:00
|
|
|
|
2012-12-13 11:06:45 +08:00
|
|
|
// Find out whether this class has a superclass that we are supposed to check.
|
|
|
|
StringRef SuperclassName;
|
|
|
|
if (!isCheckableClass(D, SuperclassName))
|
|
|
|
return;
|
2012-10-30 09:21:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Iterate over all instance methods.
|
2014-03-14 03:50:17 +08:00
|
|
|
for (auto *MD : D->instance_methods()) {
|
|
|
|
Selector S = MD->getSelector();
|
2012-10-30 09:21:35 +08:00
|
|
|
// Find out whether this is a selector that we want to check.
|
2012-12-13 11:06:45 +08:00
|
|
|
if (!SelectorsForClass[SuperclassName].count(S))
|
2012-10-30 09:21:35 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check if the method calls its superclass implementation.
|
|
|
|
if (MD->getBody())
|
|
|
|
{
|
|
|
|
FindSuperCallVisitor Visitor(S);
|
|
|
|
Visitor.TraverseDecl(MD);
|
|
|
|
|
|
|
|
// It doesn't call super, emit a diagnostic.
|
|
|
|
if (!Visitor.DoesCallSuper) {
|
|
|
|
PathDiagnosticLocation DLoc =
|
|
|
|
PathDiagnosticLocation::createEnd(MD->getBody(),
|
|
|
|
BR.getSourceManager(),
|
|
|
|
Mgr.getAnalysisDeclContext(D));
|
|
|
|
|
|
|
|
const char *Name = "Missing call to superclass";
|
2012-12-13 11:06:45 +08:00
|
|
|
SmallString<320> Buf;
|
2012-10-30 09:21:35 +08:00
|
|
|
llvm::raw_svector_ostream os(Buf);
|
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
os << "The '" << S.getAsString()
|
2012-12-13 11:06:45 +08:00
|
|
|
<< "' instance method in " << SuperclassName.str() << " subclass '"
|
|
|
|
<< *D << "' is missing a [super " << S.getAsString() << "] call";
|
2012-10-30 09:21:35 +08:00
|
|
|
|
2014-02-12 05:49:21 +08:00
|
|
|
BR.EmitBasicReport(MD, this, Name, categories::CoreFoundationObjectiveC,
|
2012-10-30 09:21:35 +08:00
|
|
|
os.str(), DLoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Check registration.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void ento::registerObjCSuperCallChecker(CheckerManager &Mgr) {
|
|
|
|
Mgr.registerChecker<ObjCSuperCallChecker>();
|
|
|
|
}
|
|
|
|
|
2019-01-26 22:23:08 +08:00
|
|
|
bool ento::shouldRegisterObjCSuperCallChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-30 09:21:35 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
ToDo list for expanding this check in the future, the list is not exhaustive.
|
|
|
|
There are also cases where calling super is suggested but not "mandatory".
|
|
|
|
In addition to be able to check the classes and methods below, architectural
|
|
|
|
improvements like being able to allow for the super-call to be done in a called
|
|
|
|
method would be good too.
|
|
|
|
|
|
|
|
UIDocument subclasses
|
|
|
|
- finishedHandlingError:recovered: (is multi-arg)
|
|
|
|
- finishedHandlingError:recovered: (is multi-arg)
|
|
|
|
|
|
|
|
UIViewController subclasses
|
|
|
|
- loadView (should *never* call super)
|
|
|
|
- transitionFromViewController:toViewController:
|
|
|
|
duration:options:animations:completion: (is multi-arg)
|
|
|
|
|
|
|
|
UICollectionViewController subclasses
|
|
|
|
- loadView (take care because UIViewController subclasses should NOT call super
|
|
|
|
in loadView, but UICollectionViewController subclasses should)
|
|
|
|
|
|
|
|
NSObject subclasses
|
|
|
|
- doesNotRecognizeSelector (it only has to call super if it doesn't throw)
|
|
|
|
|
|
|
|
UIPopoverBackgroundView subclasses (some of those are class methods)
|
|
|
|
- arrowDirection (should *never* call super)
|
|
|
|
- arrowOffset (should *never* call super)
|
|
|
|
- arrowBase (should *never* call super)
|
|
|
|
- arrowHeight (should *never* call super)
|
|
|
|
- contentViewInsets (should *never* call super)
|
|
|
|
|
|
|
|
UITextSelectionRect subclasses (some of those are properties)
|
|
|
|
- rect (should *never* call super)
|
|
|
|
- range (should *never* call super)
|
|
|
|
- writingDirection (should *never* call super)
|
|
|
|
- isVertical (should *never* call super)
|
|
|
|
- containsStart (should *never* call super)
|
|
|
|
- containsEnd (should *never* call super)
|
|
|
|
*/
|