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 | |
| 27 | class CFRefBug : public BugType { |
| 28 | protected: |
| 29 | CFRefBug(const CheckerBase *checker, StringRef name) |
| 30 | : BugType(checker, name, categories::MemoryCoreFoundationObjectiveC) {} |
| 31 | |
| 32 | public: |
| 33 | |
| 34 | // FIXME: Eventually remove. |
| 35 | virtual const char *getDescription() const = 0; |
| 36 | |
| 37 | virtual bool isLeak() const { return false; } |
| 38 | }; |
| 39 | |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 40 | typedef ::llvm::DenseMap<const ExplodedNode *, const RetainSummary *> |
| 41 | SummaryLogTy; |
| 42 | |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 43 | class CFRefReport : public BugReport { |
George Karpenkov | f893ea1 | 2018-11-30 02:17:44 +0000 | [diff] [blame] | 44 | protected: |
| 45 | SymbolRef Sym; |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 46 | |
| 47 | public: |
| 48 | CFRefReport(CFRefBug &D, const LangOptions &LOpts, |
| 49 | const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym, |
George Karpenkov | 62db886 | 2018-11-30 02:18:23 +0000 | [diff] [blame] | 50 | bool registerVisitor = true); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 51 | |
| 52 | CFRefReport(CFRefBug &D, const LangOptions &LOpts, |
| 53 | const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym, |
George Karpenkov | 62db886 | 2018-11-30 02:18:23 +0000 | [diff] [blame] | 54 | StringRef endText); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 55 | |
| 56 | llvm::iterator_range<ranges_iterator> getRanges() override { |
| 57 | const CFRefBug& BugTy = static_cast<CFRefBug&>(getBugType()); |
| 58 | if (!BugTy.isLeak()) |
| 59 | return BugReport::getRanges(); |
| 60 | return llvm::make_range(ranges_iterator(), ranges_iterator()); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class CFRefLeakReport : public CFRefReport { |
| 65 | const MemRegion* AllocBinding; |
| 66 | const Stmt *AllocStmt; |
| 67 | |
| 68 | // Finds the function declaration where a leak warning for the parameter |
| 69 | // 'sym' should be raised. |
| 70 | void deriveParamLocation(CheckerContext &Ctx, SymbolRef sym); |
| 71 | // Finds the location where a leak warning for 'sym' should be raised. |
| 72 | void deriveAllocLocation(CheckerContext &Ctx, SymbolRef sym); |
| 73 | // Produces description of a leak warning which is printed on the console. |
George Karpenkov | 936a9c9 | 2018-12-07 20:21:37 +0000 | [diff] [blame^] | 74 | void createDescription(CheckerContext &Ctx); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 75 | |
| 76 | public: |
| 77 | CFRefLeakReport(CFRefBug &D, const LangOptions &LOpts, |
| 78 | const SummaryLogTy &Log, ExplodedNode *n, SymbolRef sym, |
George Karpenkov | 936a9c9 | 2018-12-07 20:21:37 +0000 | [diff] [blame^] | 79 | CheckerContext &Ctx); |
George Karpenkov | 70c2ee3 | 2018-08-17 21:41:07 +0000 | [diff] [blame] | 80 | |
| 81 | PathDiagnosticLocation getLocation(const SourceManager &SM) const override { |
| 82 | assert(Location.isValid()); |
| 83 | return Location; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | } // end namespace retaincountchecker |
| 88 | } // end namespace ento |
| 89 | } // end namespace clang |
| 90 | |
| 91 | #endif |