RetainCountDiagnostics.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H
  14. #define LLVM_CLANG_LIB_STATICANALYZER_CHECKERS_RETAINCOUNTCHECKER_DIAGNOSTICS_H
  15. #include "clang/Analysis/PathDiagnostic.h"
  16. #include "clang/Analysis/RetainSummaryManager.h"
  17. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  18. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  20. namespace clang {
  21. namespace ento {
  22. namespace retaincountchecker {
  23. class RefCountBug : public BugType {
  24. public:
  25. enum RefCountBugKind {
  26. UseAfterRelease,
  27. ReleaseNotOwned,
  28. DeallocNotOwned,
  29. FreeNotOwned,
  30. OverAutorelease,
  31. ReturnNotOwnedForOwned,
  32. LeakWithinFunction,
  33. LeakAtReturn,
  34. };
  35. RefCountBug(CheckerNameRef Checker, RefCountBugKind BT);
  36. StringRef getDescription() const;
  37. RefCountBugKind getBugType() const { return BT; }
  38. private:
  39. RefCountBugKind BT;
  40. static StringRef bugTypeToName(RefCountBugKind BT);
  41. };
  42. class RefCountReport : public PathSensitiveBugReport {
  43. protected:
  44. SymbolRef Sym;
  45. bool isLeak = false;
  46. public:
  47. RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
  48. ExplodedNode *n, SymbolRef sym,
  49. bool isLeak=false);
  50. RefCountReport(const RefCountBug &D, const LangOptions &LOpts,
  51. ExplodedNode *n, SymbolRef sym,
  52. StringRef endText);
  53. ArrayRef<SourceRange> getRanges() const override {
  54. if (!isLeak)
  55. return PathSensitiveBugReport::getRanges();
  56. return {};
  57. }
  58. };
  59. class RefLeakReport : public RefCountReport {
  60. const MemRegion *AllocFirstBinding = nullptr;
  61. const MemRegion *AllocBindingToReport = nullptr;
  62. const Stmt *AllocStmt = nullptr;
  63. PathDiagnosticLocation Location;
  64. // Finds the function declaration where a leak warning for the parameter
  65. // 'sym' should be raised.
  66. void deriveParamLocation(CheckerContext &Ctx);
  67. // Finds the location where the leaking object is allocated.
  68. void deriveAllocLocation(CheckerContext &Ctx);
  69. // Produces description of a leak warning which is printed on the console.
  70. void createDescription(CheckerContext &Ctx);
  71. // Finds the binding that we should use in a leak warning.
  72. void findBindingToReport(CheckerContext &Ctx, ExplodedNode *Node);
  73. public:
  74. RefLeakReport(const RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n,
  75. SymbolRef sym, CheckerContext &Ctx);
  76. PathDiagnosticLocation getLocation() const override {
  77. assert(Location.isValid());
  78. return Location;
  79. }
  80. PathDiagnosticLocation getEndOfPath() const {
  81. return PathSensitiveBugReport::getLocation();
  82. }
  83. };
  84. } // end namespace retaincountchecker
  85. } // end namespace ento
  86. } // end namespace clang
  87. #endif