clang 10.0.0git

RetainCountDiagnostics.h
Go to the documentation of this file.
1 //== RetainCountDiagnostics.h - Checks for leaks and other issues -*- C++ -*--//
2 //
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
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
17 #include "clang/Analysis/PathDiagnostic.h"
18 #include "clang/Analysis/RetainSummaryManager.h"
19 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22
23 namespace clang {
24 namespace ento {
25 namespace retaincountchecker {
26
27 class RefCountBug : public BugType {
28 public:
29 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
42 RefCountBugType getBugType() const {
43 return BT;
44 }
45
46 const CheckerBase *getChecker() const {
47 return Checker;
48 }
49
50 private:
51 RefCountBugType BT;
52 const CheckerBase *Checker;
53 static StringRef bugTypeToName(RefCountBugType BT);
54 };
55
56 class RefCountReport : public PathSensitiveBugReport {
57 protected:
58 SymbolRef Sym;
59 bool isLeak = false;
60
61 public:
62 RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
63 ExplodedNode *n, SymbolRef sym,
64 bool isLeak=false);
65
66 RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
67 ExplodedNode *n, SymbolRef sym,
68 StringRef endText);
69
70 ArrayRef<SourceRange> getRanges() const override {
71 if (!isLeak)
72 return PathSensitiveBugReport::getRanges();
73 return {};
74 }
75 };
76
77 class RefLeakReport : public RefCountReport {
78 const MemRegion* AllocBinding;
79 const Stmt *AllocStmt;
80 PathDiagnosticLocation Location;
81
82 // Finds the function declaration where a leak warning for the parameter
83 // 'sym' should be raised.
84 void deriveParamLocation(CheckerContext &Ctx, SymbolRef sym);
85 // Finds the location where a leak warning for 'sym' should be raised.
86 void deriveAllocLocation(CheckerContext &Ctx, SymbolRef sym);
87 // Produces description of a leak warning which is printed on the console.
88 void createDescription(CheckerContext &Ctx);
89
90 public:
91 RefLeakReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n,
92 SymbolRef sym, CheckerContext &Ctx);
93 PathDiagnosticLocation getLocation() const override {
94 assert(Location.isValid());
95 return Location;
96 }
97
98 PathDiagnosticLocation getEndOfPath() const {
99 return PathSensitiveBugReport::getLocation();
100 }
101 };
102
103 } // end namespace retaincountchecker
104 } // end namespace ento
105 } // end namespace clang
106
107 #endif
ento
clang::ento::MemRegion
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
clang::ento::retaincountchecker::RefCountReport::Sym
SymbolRef Sym
Definition: RetainCountDiagnostics.h:58
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::ento::retaincountchecker::RefCountBug
Definition: RetainCountDiagnostics.h:27
clang::ento::retaincountchecker::RefCountBug::LeakWithinFunction
Definition: RetainCountDiagnostics.h:36
retaincountchecker
BugType.h
clang::ento::PathSensitiveBugReport::getRanges
ArrayRef< SourceRange > getRanges() const override
Get the SourceRanges associated with the report.
Definition: BugReporter.cpp:2328
clang::ento::retaincountchecker::RefCountBug::DeallocNotOwned
Definition: RetainCountDiagnostics.h:32
clang::ento::retaincountchecker::RefCountBug::OverAutorelease
Definition: RetainCountDiagnostics.h:34
clang::ento::SymExpr
Symbolic value.
Definition: SymExpr.h:29
clang::ento::PathSensitiveBugReport
Definition: BugReporter.h:289
clang::ento::PathDiagnosticLocation
Definition: PathDiagnostic.h:157
clang::ento::ExplodedNode
Definition: ExplodedGraph.h:65
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
clang::ento::CheckerContext
Definition: CheckerContext.h:23
clang::ento::PathDiagnosticLocation::isValid
bool isValid() const
Definition: PathDiagnostic.h:297
clang::ento::retaincountchecker::RefCountBug::LeakAtReturn
Definition: RetainCountDiagnostics.h:37
llvm::ArrayRef
Definition: LLVM.h:31
clang::ento::Checker
Definition: Checker.h:517
clang::ento::retaincountchecker::RefLeakReport::getLocation
PathDiagnosticLocation getLocation() const override
The primary location of the bug report that points at the undesirable behavior in the code...
Definition: RetainCountDiagnostics.h:93
PathDiagnostic.h
BugReporterVisitors.h
clang::ento::retaincountchecker::RefCountBug::getBugType
RefCountBugType getBugType() const
Definition: RetainCountDiagnostics.h:42
RetainSummaryManager.h
clang::ento::retaincountchecker::RefCountBug::ReturnNotOwnedForOwned
Definition: RetainCountDiagnostics.h:35
clang::ento::PathSensitiveBugReport::getLocation
PathDiagnosticLocation getLocation() const override
The primary location of the bug report that points at the undesirable behavior in the code...
Definition: BugReporter.cpp:2338
clang::ento::retaincountchecker::RefLeakReport::getEndOfPath
PathDiagnosticLocation getEndOfPath() const
Definition: RetainCountDiagnostics.h:98
clang::ento::retaincountchecker::RefCountBug::UseAfterRelease
Definition: RetainCountDiagnostics.h:30
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::ento::BugType
Definition: BugType.h:29
clang::ento::retaincountchecker::RefCountBug::RefCountBugType
RefCountBugType
Definition: RetainCountDiagnostics.h:29
clang::ento::CheckerBase
Definition: Checker.h:492
clang::ento::retaincountchecker::RefCountBug::getChecker
const CheckerBase * getChecker() const
Definition: RetainCountDiagnostics.h:46
clang::ento::retaincountchecker::RefCountBug::getDescription
StringRef getDescription() const
Definition: RetainCountDiagnostics.cpp:43
clang::ento::retaincountchecker::RefLeakReport
Definition: RetainCountDiagnostics.h:77
clang::ento::retaincountchecker::RefCountBug::ReleaseNotOwned
Definition: RetainCountDiagnostics.h:31
clang::ento::retaincountchecker::RefCountBug::RefCountBug
RefCountBug(const CheckerBase *checker, RefCountBugType BT)
Definition: RetainCountDiagnostics.cpp:66
clang::ento::retaincountchecker::RefCountReport
Definition: RetainCountDiagnostics.h:56
clang::ento::retaincountchecker::RefCountReport::getRanges
ArrayRef< SourceRange > getRanges() const override
Get the SourceRanges associated with the report.
Definition: RetainCountDiagnostics.h:70
clang::ento::retaincountchecker::RefCountBug::FreeNotOwned
Definition: RetainCountDiagnostics.h:33
CallEvent.h