xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/ObjCMissingSuperCallChecker.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
10b57cec5SDimitry Andric //==- ObjCMissingSuperCallChecker.cpp - Check missing super-calls in ObjC --==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file defines a ObjCMissingSuperCallChecker, a checker that
100b57cec5SDimitry Andric //  analyzes a UIViewController implementation to determine if it
110b57cec5SDimitry Andric //  correctly calls super in the methods where this is mandatory.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16*a7dea167SDimitry Andric #include "clang/Analysis/PathDiagnostic.h"
170b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h"
180b57cec5SDimitry Andric #include "clang/AST/Expr.h"
190b57cec5SDimitry Andric #include "clang/AST/ExprObjC.h"
200b57cec5SDimitry Andric #include "clang/AST/RecursiveASTVisitor.h"
210b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
220b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
230b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
240b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
250b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
260b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace clang;
290b57cec5SDimitry Andric using namespace ento;
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric namespace {
320b57cec5SDimitry Andric struct SelectorDescriptor {
330b57cec5SDimitry Andric   const char *SelectorName;
340b57cec5SDimitry Andric   unsigned ArgumentCount;
350b57cec5SDimitry Andric };
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
380b57cec5SDimitry Andric // FindSuperCallVisitor - Identify specific calls to the superclass.
390b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric class FindSuperCallVisitor : public RecursiveASTVisitor<FindSuperCallVisitor> {
420b57cec5SDimitry Andric public:
430b57cec5SDimitry Andric   explicit FindSuperCallVisitor(Selector S) : DoesCallSuper(false), Sel(S) {}
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
460b57cec5SDimitry Andric     if (E->getSelector() == Sel)
470b57cec5SDimitry Andric       if (E->getReceiverKind() == ObjCMessageExpr::SuperInstance)
480b57cec5SDimitry Andric         DoesCallSuper = true;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric     // Recurse if we didn't find the super call yet.
510b57cec5SDimitry Andric     return !DoesCallSuper;
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   bool DoesCallSuper;
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric private:
570b57cec5SDimitry Andric   Selector Sel;
580b57cec5SDimitry Andric };
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
610b57cec5SDimitry Andric // ObjCSuperCallChecker
620b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric class ObjCSuperCallChecker : public Checker<
650b57cec5SDimitry Andric                                       check::ASTDecl<ObjCImplementationDecl> > {
660b57cec5SDimitry Andric public:
670b57cec5SDimitry Andric   ObjCSuperCallChecker() : IsInitialized(false) {}
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   void checkASTDecl(const ObjCImplementationDecl *D, AnalysisManager &Mgr,
700b57cec5SDimitry Andric                     BugReporter &BR) const;
710b57cec5SDimitry Andric private:
720b57cec5SDimitry Andric   bool isCheckableClass(const ObjCImplementationDecl *D,
730b57cec5SDimitry Andric                         StringRef &SuperclassName) const;
740b57cec5SDimitry Andric   void initializeSelectors(ASTContext &Ctx) const;
750b57cec5SDimitry Andric   void fillSelectors(ASTContext &Ctx, ArrayRef<SelectorDescriptor> Sel,
760b57cec5SDimitry Andric                      StringRef ClassName) const;
770b57cec5SDimitry Andric   mutable llvm::StringMap<llvm::SmallSet<Selector, 16> > SelectorsForClass;
780b57cec5SDimitry Andric   mutable bool IsInitialized;
790b57cec5SDimitry Andric };
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric /// Determine whether the given class has a superclass that we want
840b57cec5SDimitry Andric /// to check. The name of the found superclass is stored in SuperclassName.
850b57cec5SDimitry Andric ///
860b57cec5SDimitry Andric /// \param D The declaration to check for superclasses.
870b57cec5SDimitry Andric /// \param[out] SuperclassName On return, the found superclass name.
880b57cec5SDimitry Andric bool ObjCSuperCallChecker::isCheckableClass(const ObjCImplementationDecl *D,
890b57cec5SDimitry Andric                                             StringRef &SuperclassName) const {
900b57cec5SDimitry Andric   const ObjCInterfaceDecl *ID = D->getClassInterface()->getSuperClass();
910b57cec5SDimitry Andric   for ( ; ID ; ID = ID->getSuperClass())
920b57cec5SDimitry Andric   {
930b57cec5SDimitry Andric     SuperclassName = ID->getIdentifier()->getName();
940b57cec5SDimitry Andric     if (SelectorsForClass.count(SuperclassName))
950b57cec5SDimitry Andric       return true;
960b57cec5SDimitry Andric   }
970b57cec5SDimitry Andric   return false;
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric void ObjCSuperCallChecker::fillSelectors(ASTContext &Ctx,
1010b57cec5SDimitry Andric                                          ArrayRef<SelectorDescriptor> Sel,
1020b57cec5SDimitry Andric                                          StringRef ClassName) const {
1030b57cec5SDimitry Andric   llvm::SmallSet<Selector, 16> &ClassSelectors = SelectorsForClass[ClassName];
1040b57cec5SDimitry Andric   // Fill the Selectors SmallSet with all selectors we want to check.
1050b57cec5SDimitry Andric   for (ArrayRef<SelectorDescriptor>::iterator I = Sel.begin(), E = Sel.end();
1060b57cec5SDimitry Andric        I != E; ++I) {
1070b57cec5SDimitry Andric     SelectorDescriptor Descriptor = *I;
1080b57cec5SDimitry Andric     assert(Descriptor.ArgumentCount <= 1); // No multi-argument selectors yet.
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric     // Get the selector.
1110b57cec5SDimitry Andric     IdentifierInfo *II = &Ctx.Idents.get(Descriptor.SelectorName);
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric     Selector Sel = Ctx.Selectors.getSelector(Descriptor.ArgumentCount, &II);
1140b57cec5SDimitry Andric     ClassSelectors.insert(Sel);
1150b57cec5SDimitry Andric   }
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric void ObjCSuperCallChecker::initializeSelectors(ASTContext &Ctx) const {
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   { // Initialize selectors for: UIViewController
1210b57cec5SDimitry Andric     const SelectorDescriptor Selectors[] = {
1220b57cec5SDimitry Andric       { "addChildViewController", 1 },
1230b57cec5SDimitry Andric       { "viewDidAppear", 1 },
1240b57cec5SDimitry Andric       { "viewDidDisappear", 1 },
1250b57cec5SDimitry Andric       { "viewWillAppear", 1 },
1260b57cec5SDimitry Andric       { "viewWillDisappear", 1 },
1270b57cec5SDimitry Andric       { "removeFromParentViewController", 0 },
1280b57cec5SDimitry Andric       { "didReceiveMemoryWarning", 0 },
1290b57cec5SDimitry Andric       { "viewDidUnload", 0 },
1300b57cec5SDimitry Andric       { "viewDidLoad", 0 },
1310b57cec5SDimitry Andric       { "viewWillUnload", 0 },
1320b57cec5SDimitry Andric       { "updateViewConstraints", 0 },
1330b57cec5SDimitry Andric       { "encodeRestorableStateWithCoder", 1 },
1340b57cec5SDimitry Andric       { "restoreStateWithCoder", 1 }};
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric     fillSelectors(Ctx, Selectors, "UIViewController");
1370b57cec5SDimitry Andric   }
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   { // Initialize selectors for: UIResponder
1400b57cec5SDimitry Andric     const SelectorDescriptor Selectors[] = {
1410b57cec5SDimitry Andric       { "resignFirstResponder", 0 }};
1420b57cec5SDimitry Andric 
1430b57cec5SDimitry Andric     fillSelectors(Ctx, Selectors, "UIResponder");
1440b57cec5SDimitry Andric   }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   { // Initialize selectors for: NSResponder
1470b57cec5SDimitry Andric     const SelectorDescriptor Selectors[] = {
1480b57cec5SDimitry Andric       { "encodeRestorableStateWithCoder", 1 },
1490b57cec5SDimitry Andric       { "restoreStateWithCoder", 1 }};
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric     fillSelectors(Ctx, Selectors, "NSResponder");
1520b57cec5SDimitry Andric   }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   { // Initialize selectors for: NSDocument
1550b57cec5SDimitry Andric     const SelectorDescriptor Selectors[] = {
1560b57cec5SDimitry Andric       { "encodeRestorableStateWithCoder", 1 },
1570b57cec5SDimitry Andric       { "restoreStateWithCoder", 1 }};
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric     fillSelectors(Ctx, Selectors, "NSDocument");
1600b57cec5SDimitry Andric   }
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   IsInitialized = true;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric void ObjCSuperCallChecker::checkASTDecl(const ObjCImplementationDecl *D,
1660b57cec5SDimitry Andric                                         AnalysisManager &Mgr,
1670b57cec5SDimitry Andric                                         BugReporter &BR) const {
1680b57cec5SDimitry Andric   ASTContext &Ctx = BR.getContext();
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   // We need to initialize the selector table once.
1710b57cec5SDimitry Andric   if (!IsInitialized)
1720b57cec5SDimitry Andric     initializeSelectors(Ctx);
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric   // Find out whether this class has a superclass that we are supposed to check.
1750b57cec5SDimitry Andric   StringRef SuperclassName;
1760b57cec5SDimitry Andric   if (!isCheckableClass(D, SuperclassName))
1770b57cec5SDimitry Andric     return;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric   // Iterate over all instance methods.
1810b57cec5SDimitry Andric   for (auto *MD : D->instance_methods()) {
1820b57cec5SDimitry Andric     Selector S = MD->getSelector();
1830b57cec5SDimitry Andric     // Find out whether this is a selector that we want to check.
1840b57cec5SDimitry Andric     if (!SelectorsForClass[SuperclassName].count(S))
1850b57cec5SDimitry Andric       continue;
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric     // Check if the method calls its superclass implementation.
1880b57cec5SDimitry Andric     if (MD->getBody())
1890b57cec5SDimitry Andric     {
1900b57cec5SDimitry Andric       FindSuperCallVisitor Visitor(S);
1910b57cec5SDimitry Andric       Visitor.TraverseDecl(MD);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric       // It doesn't call super, emit a diagnostic.
1940b57cec5SDimitry Andric       if (!Visitor.DoesCallSuper) {
1950b57cec5SDimitry Andric         PathDiagnosticLocation DLoc =
1960b57cec5SDimitry Andric           PathDiagnosticLocation::createEnd(MD->getBody(),
1970b57cec5SDimitry Andric                                             BR.getSourceManager(),
1980b57cec5SDimitry Andric                                             Mgr.getAnalysisDeclContext(D));
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric         const char *Name = "Missing call to superclass";
2010b57cec5SDimitry Andric         SmallString<320> Buf;
2020b57cec5SDimitry Andric         llvm::raw_svector_ostream os(Buf);
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric         os << "The '" << S.getAsString()
2050b57cec5SDimitry Andric            << "' instance method in " << SuperclassName.str() << " subclass '"
2060b57cec5SDimitry Andric            << *D << "' is missing a [super " << S.getAsString() << "] call";
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric         BR.EmitBasicReport(MD, this, Name, categories::CoreFoundationObjectiveC,
2090b57cec5SDimitry Andric                            os.str(), DLoc);
2100b57cec5SDimitry Andric       }
2110b57cec5SDimitry Andric     }
2120b57cec5SDimitry Andric   }
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2170b57cec5SDimitry Andric // Check registration.
2180b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric void ento::registerObjCSuperCallChecker(CheckerManager &Mgr) {
2210b57cec5SDimitry Andric   Mgr.registerChecker<ObjCSuperCallChecker>();
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric bool ento::shouldRegisterObjCSuperCallChecker(const LangOptions &LO) {
2250b57cec5SDimitry Andric   return true;
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric /*
2290b57cec5SDimitry Andric  ToDo list for expanding this check in the future, the list is not exhaustive.
2300b57cec5SDimitry Andric  There are also cases where calling super is suggested but not "mandatory".
2310b57cec5SDimitry Andric  In addition to be able to check the classes and methods below, architectural
2320b57cec5SDimitry Andric  improvements like being able to allow for the super-call to be done in a called
2330b57cec5SDimitry Andric  method would be good too.
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric UIDocument subclasses
2360b57cec5SDimitry Andric - finishedHandlingError:recovered: (is multi-arg)
2370b57cec5SDimitry Andric - finishedHandlingError:recovered: (is multi-arg)
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric UIViewController subclasses
2400b57cec5SDimitry Andric - loadView (should *never* call super)
2410b57cec5SDimitry Andric - transitionFromViewController:toViewController:
2420b57cec5SDimitry Andric          duration:options:animations:completion: (is multi-arg)
2430b57cec5SDimitry Andric 
2440b57cec5SDimitry Andric UICollectionViewController subclasses
2450b57cec5SDimitry Andric - loadView (take care because UIViewController subclasses should NOT call super
2460b57cec5SDimitry Andric             in loadView, but UICollectionViewController subclasses should)
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric NSObject subclasses
2490b57cec5SDimitry Andric - doesNotRecognizeSelector (it only has to call super if it doesn't throw)
2500b57cec5SDimitry Andric 
2510b57cec5SDimitry Andric UIPopoverBackgroundView subclasses (some of those are class methods)
2520b57cec5SDimitry Andric - arrowDirection (should *never* call super)
2530b57cec5SDimitry Andric - arrowOffset (should *never* call super)
2540b57cec5SDimitry Andric - arrowBase (should *never* call super)
2550b57cec5SDimitry Andric - arrowHeight (should *never* call super)
2560b57cec5SDimitry Andric - contentViewInsets (should *never* call super)
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric UITextSelectionRect subclasses (some of those are properties)
2590b57cec5SDimitry Andric - rect (should *never* call super)
2600b57cec5SDimitry Andric - range (should *never* call super)
2610b57cec5SDimitry Andric - writingDirection (should *never* call super)
2620b57cec5SDimitry Andric - isVertical (should *never* call super)
2630b57cec5SDimitry Andric - containsStart (should *never* call super)
2640b57cec5SDimitry Andric - containsEnd (should *never* call super)
2650b57cec5SDimitry Andric */
266