blob: d311cdfa963c5a44a35fe871ff2011afa68b8ff0 [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
George Karpenkov0bb17c42019-01-10 18:16:25 +000027class RefCountBug : public BugType {
Vlad Tsyrklevichd5dd6a52019-01-18 08:43:22 +000028public:
George Karpenkov2c2d0b62019-01-18 19:24:55 +000029 enum RefCountBugType {
30 UseAfterRelease,
31 ReleaseNotOwned,
32 DeallocNotOwned,
33 FreeNotOwned,
34 OverAutorelease,
35 ReturnNotOwnedForOwned,
36 LeakWithinFunction,
37 LeakAtReturn,
38 };
39 RefCountBug(const CheckerBase *checker, RefCountBugType BT);
40 StringRef getDescription() const;
41 RefCountBugType getBugType() const {
42 return BT;
43 }
Vlad Tsyrklevichd5dd6a52019-01-18 08:43:22 +000044
George Karpenkov2c2d0b62019-01-18 19:24:55 +000045private:
46 RefCountBugType BT;
47 static StringRef bugTypeToName(RefCountBugType BT);
George Karpenkov70c2ee32018-08-17 21:41:07 +000048};
49
George Karpenkov0bb17c42019-01-10 18:16:25 +000050class RefCountReport : public BugReport {
George Karpenkovf893ea12018-11-30 02:17:44 +000051protected:
52 SymbolRef Sym;
George Karpenkov2c2d0b62019-01-18 19:24:55 +000053 bool isLeak = false;
George Karpenkov70c2ee32018-08-17 21:41:07 +000054
55public:
George Karpenkov2c2d0b62019-01-18 19:24:55 +000056 RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
George Karpenkov717c4c02019-01-10 18:15:17 +000057 ExplodedNode *n, SymbolRef sym,
George Karpenkov2c2d0b62019-01-18 19:24:55 +000058 bool isLeak=false);
George Karpenkov70c2ee32018-08-17 21:41:07 +000059
George Karpenkov2c2d0b62019-01-18 19:24:55 +000060 RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
George Karpenkov717c4c02019-01-10 18:15:17 +000061 ExplodedNode *n, SymbolRef sym,
George Karpenkov62db8862018-11-30 02:18:23 +000062 StringRef endText);
George Karpenkov70c2ee32018-08-17 21:41:07 +000063
64 llvm::iterator_range<ranges_iterator> getRanges() override {
George Karpenkov2c2d0b62019-01-18 19:24:55 +000065 if (!isLeak)
George Karpenkov70c2ee32018-08-17 21:41:07 +000066 return BugReport::getRanges();
67 return llvm::make_range(ranges_iterator(), ranges_iterator());
68 }
69};
70
George Karpenkov0bb17c42019-01-10 18:16:25 +000071class RefLeakReport : public RefCountReport {
George Karpenkov70c2ee32018-08-17 21:41:07 +000072 const MemRegion* AllocBinding;
73 const Stmt *AllocStmt;
74
75 // Finds the function declaration where a leak warning for the parameter
76 // 'sym' should be raised.
77 void deriveParamLocation(CheckerContext &Ctx, SymbolRef sym);
78 // Finds the location where a leak warning for 'sym' should be raised.
79 void deriveAllocLocation(CheckerContext &Ctx, SymbolRef sym);
80 // Produces description of a leak warning which is printed on the console.
George Karpenkov936a9c92018-12-07 20:21:37 +000081 void createDescription(CheckerContext &Ctx);
George Karpenkov70c2ee32018-08-17 21:41:07 +000082
83public:
George Karpenkov2c2d0b62019-01-18 19:24:55 +000084 RefLeakReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n,
George Karpenkov0bb17c42019-01-10 18:16:25 +000085 SymbolRef sym, CheckerContext &Ctx);
George Karpenkov70c2ee32018-08-17 21:41:07 +000086
87 PathDiagnosticLocation getLocation(const SourceManager &SM) const override {
88 assert(Location.isValid());
89 return Location;
90 }
91};
92
93} // end namespace retaincountchecker
94} // end namespace ento
95} // end namespace clang
96
97#endif