1 //== 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 18 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 19 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h" 20 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" 21 #include "clang/StaticAnalyzer/Core/RetainSummaryManager.h" 22 23 namespace clang { 24 namespace ento { 25 namespace retaincountchecker { 26 27 class RefCountBug : public BugType { 28 protected: RefCountBug(const CheckerBase * checker,StringRef name)29 RefCountBug(const CheckerBase *checker, StringRef name) 30 : BugType(checker, name, categories::MemoryRefCount) {} 31 32 public: 33 virtual const char *getDescription() const = 0; 34 isLeak()35 virtual bool isLeak() const { return false; } 36 }; 37 38 class RefCountReport : public BugReport { 39 protected: 40 SymbolRef Sym; 41 42 public: 43 RefCountReport(RefCountBug &D, const LangOptions &LOpts, 44 ExplodedNode *n, SymbolRef sym, 45 bool registerVisitor = true); 46 47 RefCountReport(RefCountBug &D, const LangOptions &LOpts, 48 ExplodedNode *n, SymbolRef sym, 49 StringRef endText); 50 getRanges()51 llvm::iterator_range<ranges_iterator> getRanges() override { 52 const RefCountBug& BugTy = static_cast<RefCountBug&>(getBugType()); 53 if (!BugTy.isLeak()) 54 return BugReport::getRanges(); 55 return llvm::make_range(ranges_iterator(), ranges_iterator()); 56 } 57 }; 58 59 class RefLeakReport : public RefCountReport { 60 const MemRegion* AllocBinding; 61 const Stmt *AllocStmt; 62 63 // Finds the function declaration where a leak warning for the parameter 64 // 'sym' should be raised. 65 void deriveParamLocation(CheckerContext &Ctx, SymbolRef sym); 66 // Finds the location where a leak warning for 'sym' should be raised. 67 void deriveAllocLocation(CheckerContext &Ctx, SymbolRef sym); 68 // Produces description of a leak warning which is printed on the console. 69 void createDescription(CheckerContext &Ctx); 70 71 public: 72 RefLeakReport(RefCountBug &D, const LangOptions &LOpts, ExplodedNode *n, 73 SymbolRef sym, CheckerContext &Ctx); 74 getLocation(const SourceManager & SM)75 PathDiagnosticLocation getLocation(const SourceManager &SM) const override { 76 assert(Location.isValid()); 77 return Location; 78 } 79 }; 80 81 } // end namespace retaincountchecker 82 } // end namespace ento 83 } // end namespace clang 84 85 #endif 86