//==- ObjCMissingSuperCallChecker.cpp - Check missing super-calls in ObjC --==// // // 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 // //===----------------------------------------------------------------------===// // // 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. // //===----------------------------------------------------------------------===// #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" usingnamespaceclang; usingnamespaceento; namespace { struct SelectorDescriptor { … }; //===----------------------------------------------------------------------===// // FindSuperCallVisitor - Identify specific calls to the superclass. //===----------------------------------------------------------------------===// class FindSuperCallVisitor : public RecursiveASTVisitor<FindSuperCallVisitor> { … }; //===----------------------------------------------------------------------===// // ObjCSuperCallChecker //===----------------------------------------------------------------------===// class ObjCSuperCallChecker : public Checker< check::ASTDecl<ObjCImplementationDecl> > { … }; } /// Determine whether the given class has a superclass that we want /// to check. The name of the found superclass is stored in SuperclassName. /// /// \param D The declaration to check for superclasses. /// \param[out] SuperclassName On return, the found superclass name. bool ObjCSuperCallChecker::isCheckableClass(const ObjCImplementationDecl *D, StringRef &SuperclassName) const { … } void ObjCSuperCallChecker::fillSelectors(ASTContext &Ctx, ArrayRef<SelectorDescriptor> Sel, StringRef ClassName) const { … } void ObjCSuperCallChecker::initializeSelectors(ASTContext &Ctx) const { … } void ObjCSuperCallChecker::checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager &Mgr, BugReporter &BR) const { … } //===----------------------------------------------------------------------===// // Check registration. //===----------------------------------------------------------------------===// void ento::registerObjCSuperCallChecker(CheckerManager &Mgr) { … } bool ento::shouldRegisterObjCSuperCallChecker(const CheckerManager &mgr) { … } /* 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) */