Home | History | Annotate | Line # | Download | only in RetainCountChecker
      1      1.1  joerg //== RetainCountDiagnostics.h - Checks for leaks and other issues -*- C++ -*--//
      2      1.1  joerg //
      3      1.1  joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4      1.1  joerg // See https://llvm.org/LICENSE.txt for license information.
      5      1.1  joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6      1.1  joerg //
      7      1.1  joerg //===----------------------------------------------------------------------===//
      8      1.1  joerg //
      9      1.1  joerg //  This file defines diagnostics for RetainCountChecker, which implements
     10      1.1  joerg //  a reference count checker for Core Foundation and Cocoa on (Mac OS X).
     11      1.1  joerg //
     12      1.1  joerg //===----------------------------------------------------------------------===//
     13      1.1  joerg 
     14      1.1  joerg #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H
     15      1.1  joerg #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H
     16      1.1  joerg 
     17      1.1  joerg #include "clang/Analysis/PathDiagnostic.h"
     18      1.1  joerg #include "clang/Analysis/RetainSummaryManager.h"
     19      1.1  joerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
     20      1.1  joerg #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
     21      1.1  joerg #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
     22      1.1  joerg 
     23      1.1  joerg namespace clang {
     24      1.1  joerg namespace ento {
     25      1.1  joerg namespace retaincountchecker {
     26      1.1  joerg 
     27      1.1  joerg class RefCountBug : public BugType {
     28      1.1  joerg public:
     29  1.1.1.2  joerg   enum RefCountBugKind {
     30      1.1  joerg     UseAfterRelease,
     31      1.1  joerg     ReleaseNotOwned,
     32      1.1  joerg     DeallocNotOwned,
     33      1.1  joerg     FreeNotOwned,
     34      1.1  joerg     OverAutorelease,
     35      1.1  joerg     ReturnNotOwnedForOwned,
     36      1.1  joerg     LeakWithinFunction,
     37      1.1  joerg     LeakAtReturn,
     38      1.1  joerg   };
     39  1.1.1.2  joerg   RefCountBug(CheckerNameRef Checker, RefCountBugKind BT);
     40      1.1  joerg   StringRef getDescription() const;
     41      1.1  joerg 
     42  1.1.1.2  joerg   RefCountBugKind getBugType() const { return BT; }
     43      1.1  joerg 
     44      1.1  joerg private:
     45  1.1.1.2  joerg   RefCountBugKind BT;
     46  1.1.1.2  joerg   static StringRef bugTypeToName(RefCountBugKind BT);
     47      1.1  joerg };
     48      1.1  joerg 
     49      1.1  joerg class RefCountReport : public PathSensitiveBugReport {
     50      1.1  joerg protected:
     51      1.1  joerg   SymbolRef Sym;
     52      1.1  joerg   bool isLeak = false;
     53      1.1  joerg 
     54      1.1  joerg public:
     55      1.1  joerg   RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
     56      1.1  joerg               ExplodedNode *n, SymbolRef sym,
     57      1.1  joerg               bool isLeak=false);
     58      1.1  joerg 
     59      1.1  joerg   RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
     60      1.1  joerg               ExplodedNode *n, SymbolRef sym,
     61      1.1  joerg               StringRef endText);
     62      1.1  joerg 
     63      1.1  joerg   ArrayRef<SourceRange> getRanges() const override {
     64      1.1  joerg     if (!isLeak)
     65      1.1  joerg       return PathSensitiveBugReport::getRanges();
     66      1.1  joerg     return {};
     67      1.1  joerg   }
     68      1.1  joerg };
     69      1.1  joerg 
     70      1.1  joerg class RefLeakReport : public RefCountReport {
     71  1.1.1.2  joerg   const MemRegion *AllocFirstBinding = nullptr;
     72  1.1.1.2  joerg   const MemRegion *AllocBindingToReport = nullptr;
     73  1.1.1.2  joerg   const Stmt *AllocStmt = nullptr;
     74      1.1  joerg   PathDiagnosticLocation Location;
     75      1.1  joerg 
     76      1.1  joerg   // Finds the function declaration where a leak warning for the parameter
     77      1.1  joerg   // 'sym' should be raised.
     78  1.1.1.2  joerg   void deriveParamLocation(CheckerContext &Ctx);
     79  1.1.1.2  joerg   // Finds the location where the leaking object is allocated.
     80  1.1.1.2  joerg   void deriveAllocLocation(CheckerContext &Ctx);
     81      1.1  joerg   // Produces description of a leak warning which is printed on the console.
     82      1.1  joerg   void createDescription(CheckerContext &Ctx);
     83  1.1.1.2  joerg   // Finds the binding that we should use in a leak warning.
     84  1.1.1.2  joerg   void findBindingToReport(CheckerContext &Ctx, ExplodedNode *Node);
     85      1.1  joerg 
     86      1.1  joerg public:
     87      1.1  joerg   RefLeakReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n,
     88      1.1  joerg                 SymbolRef sym, CheckerContext &Ctx);
     89      1.1  joerg   PathDiagnosticLocation getLocation() const override {
     90      1.1  joerg     assert(Location.isValid());
     91      1.1  joerg     return Location;
     92      1.1  joerg   }
     93      1.1  joerg 
     94      1.1  joerg   PathDiagnosticLocation getEndOfPath() const {
     95      1.1  joerg     return PathSensitiveBugReport::getLocation();
     96      1.1  joerg   }
     97      1.1  joerg };
     98      1.1  joerg 
     99      1.1  joerg } // end namespace retaincountchecker
    100      1.1  joerg } // end namespace ento
    101      1.1  joerg } // end namespace clang
    102      1.1  joerg 
    103      1.1  joerg #endif
    104