George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 1 | //== RetainCountDiagnostics.h - Checks for leaks and other issues -*- C++ -*--// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines diagnostics for RetainCountChecker, which implements |
| 11 | // a reference count checker for Core Foundation and Cocoa on (Mac OS X). |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H |
| 16 | #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H |
| 17 | |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 19 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h" |
| 20 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
George Karpenkov | efef49c | 2018-08-21 03:09:02 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/RetainSummaryManager.h" |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 22 | |
| 23 | namespace clang { |
| 24 | namespace ento { |
| 25 | namespace retaincountchecker { |
| 26 | |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame^] | 27 | class RefCountBug : public BugType { |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 28 | protected: |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame^] | 29 | RefCountBug(const CheckerBase *checker, StringRef name) |
| 30 | : BugType(checker, name, categories::MemoryRefCount) {} |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 31 | |
| 32 | public: |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 33 | virtual const char *getDescription() const = 0; |
| 34 | |
| 35 | virtual bool isLeak() const { return false; } |
| 36 | }; |
| 37 | |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame^] | 38 | class RefCountReport : public BugReport { |
George Karpenkov | f893ea1 | 2018-11-30 02:17:44 +0000 | [diff] [blame] | 39 | protected: |
| 40 | SymbolRef Sym; |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 41 | |
| 42 | public: |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame^] | 43 | RefCountReport(RefCountBug &D, const LangOptions &LOpts, |
George Karpenkov | 717c4c0 | 2019-01-10 18:15:17 +0000 | [diff] [blame] | 44 | ExplodedNode *n, SymbolRef sym, |
George Karpenkov | 62db886 | 2018-11-30 02:18:23 +0000 | [diff] [blame] | 45 | bool registerVisitor = true); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 46 | |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame^] | 47 | RefCountReport(RefCountBug &D, const LangOptions &LOpts, |
George Karpenkov | 717c4c0 | 2019-01-10 18:15:17 +0000 | [diff] [blame] | 48 | ExplodedNode *n, SymbolRef sym, |
George Karpenkov | 62db886 | 2018-11-30 02:18:23 +0000 | [diff] [blame] | 49 | StringRef endText); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 50 | |
| 51 | llvm::iterator_range<ranges_iterator> getRanges() override { |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame^] | 52 | const RefCountBug& BugTy = static_cast<RefCountBug&>(getBugType()); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 53 | if (!BugTy.isLeak()) |
| 54 | return BugReport::getRanges(); |
| 55 | return llvm::make_range(ranges_iterator(), ranges_iterator()); |
| 56 | } |
| 57 | }; |
| 58 | |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame^] | 59 | class RefLeakReport : public RefCountReport { |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 60 | const MemRegion* AllocBinding; |
| 61 | const Stmt *AllocStmt; |
| 62 | |
| 63 | // Finds the function declaration where a leak warning for the parameter |
| 64 | // 'sym' should be raised. |
| 65 | void deriveParamLocation(CheckerContext &Ctx, SymbolRef sym); |
| 66 | // Finds the location where a leak warning for 'sym' should be raised. |
| 67 | void deriveAllocLocation(CheckerContext &Ctx, SymbolRef sym); |
| 68 | // Produces description of a leak warning which is printed on the console. |
George Karpenkov | 936a9c9 | 2018-12-07 20:21:37 +0000 | [diff] [blame] | 69 | void createDescription(CheckerContext &Ctx); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 70 | |
| 71 | public: |
George Karpenkov | 0bb17c4 | 2019-01-10 18:16:25 +0000 | [diff] [blame^] | 72 | RefLeakReport(RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n, |
| 73 | SymbolRef sym, CheckerContext &Ctx); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 74 | |
| 75 | PathDiagnosticLocation getLocation(const SourceManager &SM) const override { |
| 76 | assert(Location.isValid()); |
| 77 | return Location; |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | } // end namespace retaincountchecker |
| 82 | } // end namespace ento |
| 83 | } // end namespace clang |
| 84 | |
| 85 | #endif |