blob: f27027ab62e625b4d11900ad3dac1e04b686078e [file] [log] [blame]
George Karpenkov70c2ee32018-08-17 21:41:07 +00001//== 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 Karpenkov70c2ee32018-08-17 21:41:07 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
20#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
George Karpenkovefef49c2018-08-21 03:09:02 +000021#include "clang/StaticAnalyzer/Core/RetainSummaryManager.h"
George Karpenkov70c2ee32018-08-17 21:41:07 +000022
23namespace clang {
24namespace ento {
25namespace retaincountchecker {
26
27class CFRefBug : public BugType {
28protected:
29 CFRefBug(const CheckerBase *checker, StringRef name)
30 : BugType(checker, name, categories::MemoryCoreFoundationObjectiveC) {}
31
32public:
33
34 // FIXME: Eventually remove.
35 virtual const char *getDescription() const = 0;
36
37 virtual bool isLeak() const { return false; }
38};
39
George Karpenkov70c2ee32018-08-17 21:41:07 +000040class CFRefReport : public BugReport {
George Karpenkovf893ea12018-11-30 02:17:44 +000041protected:
42 SymbolRef Sym;
George Karpenkov70c2ee32018-08-17 21:41:07 +000043
44public:
45 CFRefReport(CFRefBug &D, const LangOptions &LOpts,
George Karpenkov717c4c02019-01-10 18:15:17 +000046 ExplodedNode *n, SymbolRef sym,
George Karpenkov62db8862018-11-30 02:18:23 +000047 bool registerVisitor = true);
George Karpenkov70c2ee32018-08-17 21:41:07 +000048
49 CFRefReport(CFRefBug &D, const LangOptions &LOpts,
George Karpenkov717c4c02019-01-10 18:15:17 +000050 ExplodedNode *n, SymbolRef sym,
George Karpenkov62db8862018-11-30 02:18:23 +000051 StringRef endText);
George Karpenkov70c2ee32018-08-17 21:41:07 +000052
53 llvm::iterator_range<ranges_iterator> getRanges() override {
54 const CFRefBug& BugTy = static_cast<CFRefBug&>(getBugType());
55 if (!BugTy.isLeak())
56 return BugReport::getRanges();
57 return llvm::make_range(ranges_iterator(), ranges_iterator());
58 }
59};
60
61class CFRefLeakReport : public CFRefReport {
62 const MemRegion* AllocBinding;
63 const Stmt *AllocStmt;
64
65 // Finds the function declaration where a leak warning for the parameter
66 // 'sym' should be raised.
67 void deriveParamLocation(CheckerContext &Ctx, SymbolRef sym);
68 // Finds the location where a leak warning for 'sym' should be raised.
69 void deriveAllocLocation(CheckerContext &Ctx, SymbolRef sym);
70 // Produces description of a leak warning which is printed on the console.
George Karpenkov936a9c92018-12-07 20:21:37 +000071 void createDescription(CheckerContext &Ctx);
George Karpenkov70c2ee32018-08-17 21:41:07 +000072
73public:
74 CFRefLeakReport(CFRefBug &D, const LangOptions &LOpts,
George Karpenkov717c4c02019-01-10 18:15:17 +000075 ExplodedNode *n, SymbolRef sym,
George Karpenkov936a9c92018-12-07 20:21:37 +000076 CheckerContext &Ctx);
George Karpenkov70c2ee32018-08-17 21:41:07 +000077
78 PathDiagnosticLocation getLocation(const SourceManager &SM) const override {
79 assert(Location.isValid());
80 return Location;
81 }
82};
83
84} // end namespace retaincountchecker
85} // end namespace ento
86} // end namespace clang
87
88#endif