George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 1 | //== RetainCountDiagnostics.h - Checks for leaks and other issues -*- C++ -*--// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines diagnostics for RetainCountChecker, which implements |
| 10 | // a reference count checker for Core Foundation and Cocoa on (Mac OS X). |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H |
| 15 | #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H |
| 16 | |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h" |
| 19 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
George Karpenkov | efef49c | 2018-08-21 03:09:02 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/RetainSummaryManager.h" |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 21 | |
| 22 | namespace clang { |
| 23 | namespace ento { |
| 24 | namespace retaincountchecker { |
| 25 | |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame] | 26 | class RefCountBug : public BugType { |
Vlad Tsyrklevich | d5dd6a5 | 2019-01-18 08:43:22 +0000 | [diff] [blame] | 27 | public: |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 28 | enum RefCountBugType { |
| 29 | UseAfterRelease, |
| 30 | ReleaseNotOwned, |
| 31 | DeallocNotOwned, |
| 32 | FreeNotOwned, |
| 33 | OverAutorelease, |
| 34 | ReturnNotOwnedForOwned, |
| 35 | LeakWithinFunction, |
| 36 | LeakAtReturn, |
| 37 | }; |
| 38 | RefCountBug(const CheckerBase *checker, RefCountBugType BT); |
| 39 | StringRef getDescription() const; |
George Karpenkov | a9e2956 | 2019-01-22 19:51:00 +0000 | [diff] [blame^] | 40 | |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 41 | RefCountBugType getBugType() const { |
| 42 | return BT; |
| 43 | } |
Vlad Tsyrklevich | d5dd6a5 | 2019-01-18 08:43:22 +0000 | [diff] [blame] | 44 | |
George Karpenkov | a9e2956 | 2019-01-22 19:51:00 +0000 | [diff] [blame^] | 45 | const CheckerBase *getChecker() const { |
| 46 | return Checker; |
| 47 | } |
| 48 | |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 49 | private: |
| 50 | RefCountBugType BT; |
George Karpenkov | a9e2956 | 2019-01-22 19:51:00 +0000 | [diff] [blame^] | 51 | const CheckerBase *Checker; |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 52 | static StringRef bugTypeToName(RefCountBugType BT); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame] | 55 | class RefCountReport : public BugReport { |
George Karpenkov | f893ea1 | 2018-11-30 02:17:44 +0000 | [diff] [blame] | 56 | protected: |
| 57 | SymbolRef Sym; |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 58 | bool isLeak = false; |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 59 | |
| 60 | public: |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 61 | RefCountReport(const RefCountBug &D, const LangOptions &LOpts, |
George Karpenkov | 717c4c0 | 2019-01-10 18:15:17 +0000 | [diff] [blame] | 62 | ExplodedNode *n, SymbolRef sym, |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 63 | bool isLeak=false); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 64 | |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 65 | RefCountReport(const RefCountBug &D, const LangOptions &LOpts, |
George Karpenkov | 717c4c0 | 2019-01-10 18:15:17 +0000 | [diff] [blame] | 66 | ExplodedNode *n, SymbolRef sym, |
George Karpenkov | 62db886 | 2018-11-30 02:18:23 +0000 | [diff] [blame] | 67 | StringRef endText); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 68 | |
| 69 | llvm::iterator_range<ranges_iterator> getRanges() override { |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 70 | if (!isLeak) |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 71 | return BugReport::getRanges(); |
| 72 | return llvm::make_range(ranges_iterator(), ranges_iterator()); |
| 73 | } |
| 74 | }; |
| 75 | |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame] | 76 | class RefLeakReport : public RefCountReport { |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 77 | const MemRegion* AllocBinding; |
| 78 | const Stmt *AllocStmt; |
| 79 | |
| 80 | // Finds the function declaration where a leak warning for the parameter |
| 81 | // 'sym' should be raised. |
| 82 | void deriveParamLocation(CheckerContext &Ctx, SymbolRef sym); |
| 83 | // Finds the location where a leak warning for 'sym' should be raised. |
| 84 | void deriveAllocLocation(CheckerContext &Ctx, SymbolRef sym); |
| 85 | // Produces description of a leak warning which is printed on the console. |
George Karpenkov | 936a9c9 | 2018-12-07 20:21:37 +0000 | [diff] [blame] | 86 | void createDescription(CheckerContext &Ctx); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 87 | |
| 88 | public: |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 89 | RefLeakReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n, |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame] | 90 | SymbolRef sym, CheckerContext &Ctx); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 91 | |
| 92 | PathDiagnosticLocation getLocation(const SourceManager &SM) const override { |
| 93 | assert(Location.isValid()); |
| 94 | return Location; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | } // end namespace retaincountchecker |
| 99 | } // end namespace ento |
| 100 | } // end namespace clang |
| 101 | |
| 102 | #endif |