10b57cec5SDimitry Andric //== RetainCountDiagnostics.h - Checks for leaks and other issues -*- C++ -*--//
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 diagnostics for RetainCountChecker, which implements
100b57cec5SDimitry Andric //  a reference count checker for Core Foundation and Cocoa on (Mac OS X).
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H
150b57cec5SDimitry Andric #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H
160b57cec5SDimitry Andric 
17a7dea167SDimitry Andric #include "clang/Analysis/PathDiagnostic.h"
180b57cec5SDimitry Andric #include "clang/Analysis/RetainSummaryManager.h"
190b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
200b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
210b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace clang {
240b57cec5SDimitry Andric namespace ento {
250b57cec5SDimitry Andric namespace retaincountchecker {
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric class RefCountBug : public BugType {
280b57cec5SDimitry Andric public:
295ffd83dbSDimitry Andric   enum RefCountBugKind {
300b57cec5SDimitry Andric     UseAfterRelease,
310b57cec5SDimitry Andric     ReleaseNotOwned,
320b57cec5SDimitry Andric     DeallocNotOwned,
330b57cec5SDimitry Andric     FreeNotOwned,
340b57cec5SDimitry Andric     OverAutorelease,
350b57cec5SDimitry Andric     ReturnNotOwnedForOwned,
360b57cec5SDimitry Andric     LeakWithinFunction,
370b57cec5SDimitry Andric     LeakAtReturn,
380b57cec5SDimitry Andric   };
395ffd83dbSDimitry Andric   RefCountBug(CheckerNameRef Checker, RefCountBugKind BT);
400b57cec5SDimitry Andric   StringRef getDescription() const;
410b57cec5SDimitry Andric 
getBugType()425ffd83dbSDimitry Andric   RefCountBugKind getBugType() const { return BT; }
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric private:
455ffd83dbSDimitry Andric   RefCountBugKind BT;
465ffd83dbSDimitry Andric   static StringRef bugTypeToName(RefCountBugKind BT);
470b57cec5SDimitry Andric };
480b57cec5SDimitry Andric 
49a7dea167SDimitry Andric class RefCountReport : public PathSensitiveBugReport {
500b57cec5SDimitry Andric protected:
510b57cec5SDimitry Andric   SymbolRef Sym;
520b57cec5SDimitry Andric   bool isLeak = false;
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric public:
550b57cec5SDimitry Andric   RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
560b57cec5SDimitry Andric               ExplodedNode *n, SymbolRef sym,
570b57cec5SDimitry Andric               bool isLeak=false);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
600b57cec5SDimitry Andric               ExplodedNode *n, SymbolRef sym,
610b57cec5SDimitry Andric               StringRef endText);
620b57cec5SDimitry Andric 
getRanges()63a7dea167SDimitry Andric   ArrayRef<SourceRange> getRanges() const override {
640b57cec5SDimitry Andric     if (!isLeak)
65a7dea167SDimitry Andric       return PathSensitiveBugReport::getRanges();
66a7dea167SDimitry Andric     return {};
670b57cec5SDimitry Andric   }
680b57cec5SDimitry Andric };
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric class RefLeakReport : public RefCountReport {
71*fe6060f1SDimitry Andric   const MemRegion *AllocFirstBinding = nullptr;
72*fe6060f1SDimitry Andric   const MemRegion *AllocBindingToReport = nullptr;
73*fe6060f1SDimitry Andric   const Stmt *AllocStmt = nullptr;
74a7dea167SDimitry Andric   PathDiagnosticLocation Location;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   // Finds the function declaration where a leak warning for the parameter
770b57cec5SDimitry Andric   // 'sym' should be raised.
78*fe6060f1SDimitry Andric   void deriveParamLocation(CheckerContext &Ctx);
79*fe6060f1SDimitry Andric   // Finds the location where the leaking object is allocated.
80*fe6060f1SDimitry Andric   void deriveAllocLocation(CheckerContext &Ctx);
810b57cec5SDimitry Andric   // Produces description of a leak warning which is printed on the console.
820b57cec5SDimitry Andric   void createDescription(CheckerContext &Ctx);
83*fe6060f1SDimitry Andric   // Finds the binding that we should use in a leak warning.
84*fe6060f1SDimitry Andric   void findBindingToReport(CheckerContext &Ctx, ExplodedNode *Node);
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric public:
870b57cec5SDimitry Andric   RefLeakReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n,
880b57cec5SDimitry Andric                 SymbolRef sym, CheckerContext &Ctx);
getLocation()89a7dea167SDimitry Andric   PathDiagnosticLocation getLocation() const override {
900b57cec5SDimitry Andric     assert(Location.isValid());
910b57cec5SDimitry Andric     return Location;
920b57cec5SDimitry Andric   }
93a7dea167SDimitry Andric 
getEndOfPath()94a7dea167SDimitry Andric   PathDiagnosticLocation getEndOfPath() const {
95a7dea167SDimitry Andric     return PathSensitiveBugReport::getLocation();
96a7dea167SDimitry Andric   }
970b57cec5SDimitry Andric };
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric } // end namespace retaincountchecker
1000b57cec5SDimitry Andric } // end namespace ento
1010b57cec5SDimitry Andric } // end namespace clang
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric #endif
104