clang  14.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 
22 
23 namespace clang {
24 namespace ento {
25 namespace retaincountchecker {
26 
27 class RefCountBug : public BugType {
28 public:
38  };
40  StringRef getDescription() const;
41 
42  RefCountBugKind getBugType() const { return BT; }
43 
44 private:
45  RefCountBugKind BT;
46  static StringRef bugTypeToName(RefCountBugKind BT);
47 };
48 
50 protected:
52  bool isLeak = false;
53 
54 public:
55  RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
56  ExplodedNode *n, SymbolRef sym,
57  bool isLeak=false);
58 
59  RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
60  ExplodedNode *n, SymbolRef sym,
61  StringRef endText);
62 
63  ArrayRef<SourceRange> getRanges() const override {
64  if (!isLeak)
66  return {};
67  }
68 };
69 
70 class RefLeakReport : public RefCountReport {
71  const MemRegion *AllocFirstBinding = nullptr;
72  const MemRegion *AllocBindingToReport = nullptr;
73  const Stmt *AllocStmt = nullptr;
74  PathDiagnosticLocation Location;
75 
76  // Finds the function declaration where a leak warning for the parameter
77  // 'sym' should be raised.
78  void deriveParamLocation(CheckerContext &Ctx);
79  // Finds the location where the leaking object is allocated.
80  void deriveAllocLocation(CheckerContext &Ctx);
81  // Produces description of a leak warning which is printed on the console.
82  void createDescription(CheckerContext &Ctx);
83  // Finds the binding that we should use in a leak warning.
84  void findBindingToReport(CheckerContext &Ctx, ExplodedNode *Node);
85 
86 public:
87  RefLeakReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n,
88  SymbolRef sym, CheckerContext &Ctx);
90  assert(Location.isValid());
91  return Location;
92  }
93 
96  }
97 };
98 
99 } // end namespace retaincountchecker
100 } // end namespace ento
101 } // end namespace clang
102 
103 #endif
clang::ento::PathDiagnosticLocation
Definition: PathDiagnostic.h:197
clang::ento::retaincountchecker::RefCountReport::isLeak
bool isLeak
Definition: RetainCountDiagnostics.h:52
clang::ento::retaincountchecker::RefCountBug::RefCountBugKind
RefCountBugKind
Definition: RetainCountDiagnostics.h:29
clang::ento::retaincountchecker::RefCountReport::getRanges
ArrayRef< SourceRange > getRanges() const override
Get the SourceRanges associated with the report.
Definition: RetainCountDiagnostics.h:63
clang::ento::ExplodedNode
Definition: ExplodedGraph.h:65
clang::ento::retaincountchecker::RefLeakReport::RefLeakReport
RefLeakReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n, SymbolRef sym, CheckerContext &Ctx)
Definition: RetainCountDiagnostics.cpp:994
clang::ento::retaincountchecker::RefCountReport::Sym
SymbolRef Sym
Definition: RetainCountDiagnostics.h:51
CallEvent.h
clang::ento::MemRegion
MemRegion - The root abstract class for all memory regions.
Definition: MemRegion.h:94
clang::ento::retaincountchecker::RefCountReport::RefCountReport
RefCountReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n, SymbolRef sym, bool isLeak=false)
Node
DynTypedNode Node
Definition: ASTMatchFinder.cpp:67
clang::ento::retaincountchecker::RefCountBug::LeakAtReturn
@ LeakAtReturn
Definition: RetainCountDiagnostics.h:37
clang::ento::retaincountchecker::RefCountBug::DeallocNotOwned
@ DeallocNotOwned
Definition: RetainCountDiagnostics.h:32
clang::ento::retaincountchecker::RefLeakReport
Definition: RetainCountDiagnostics.h:70
clang::ento::retaincountchecker::RefCountBug::FreeNotOwned
@ FreeNotOwned
Definition: RetainCountDiagnostics.h:33
clang::ento::SymExpr
Symbolic value.
Definition: SymExpr.h:29
clang::ento::PathDiagnosticLocation::isValid
bool isValid() const
Definition: PathDiagnostic.h:337
clang::ento::BugType
Definition: BugType.h:29
clang::ento::CheckerNameRef
This wrapper is used to ensure that only StringRefs originating from the CheckerRegistry are used as ...
Definition: CheckerManager.h:107
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:89
PathDiagnostic.h
clang::ento::retaincountchecker::RefCountBug::OverAutorelease
@ OverAutorelease
Definition: RetainCountDiagnostics.h:34
clang::ento::retaincountchecker::RefCountBug::getBugType
RefCountBugKind getBugType() const
Definition: RetainCountDiagnostics.h:42
clang::ento::retaincountchecker::RefCountReport
Definition: RetainCountDiagnostics.h:49
clang::ento::retaincountchecker::RefCountBug::ReturnNotOwnedForOwned
@ ReturnNotOwnedForOwned
Definition: RetainCountDiagnostics.h:35
clang::ento::PathSensitiveBugReport::getRanges
ArrayRef< SourceRange > getRanges() const override
Get the SourceRanges associated with the report.
Definition: BugReporter.cpp:2406
BugType.h
llvm::ArrayRef
Definition: LLVM.h:34
BugReporterVisitors.h
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:58
clang::ento::PathSensitiveBugReport
Definition: BugReporter.h:291
clang::ento::retaincountchecker::RefLeakReport::getEndOfPath
PathDiagnosticLocation getEndOfPath() const
Definition: RetainCountDiagnostics.h:94
clang
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
clang::ento::retaincountchecker::RefCountBug::UseAfterRelease
@ UseAfterRelease
Definition: RetainCountDiagnostics.h:30
clang::ento::retaincountchecker::RefCountBug
Definition: RetainCountDiagnostics.h:27
clang::ento::CheckerContext
Definition: CheckerContext.h:23
clang::ento::retaincountchecker::RefCountBug::RefCountBug
RefCountBug(CheckerNameRef Checker, RefCountBugKind BT)
Definition: RetainCountDiagnostics.cpp:68
clang::ento::Checker
Definition: Checker.h:517
clang::ento::retaincountchecker::RefCountBug::ReleaseNotOwned
@ ReleaseNotOwned
Definition: RetainCountDiagnostics.h:31
clang::ento::retaincountchecker::RefCountBug::LeakWithinFunction
@ LeakWithinFunction
Definition: RetainCountDiagnostics.h:36
RetainSummaryManager.h
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:2416
clang::ento::retaincountchecker::RefCountBug::getDescription
StringRef getDescription() const
Definition: RetainCountDiagnostics.cpp:45