From: Anna Zaks Date: Tue, 28 Feb 2012 22:39:22 +0000 (+0000) Subject: [analyzer] Leaks should be uniqued by the allocation point in the X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e7e0168f625368032a5d2b4471d3406cd9d9f8ae;p=clang [analyzer] Leaks should be uniqued by the allocation point in the closest function context (RetainCountChecker). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151661 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp b/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp index 2350298b63..6b14013eea 100644 --- a/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp @@ -2114,14 +2114,16 @@ namespace { }; } +// Find the first node in the current function context that referred to the +// tracked symbol and the memory location that value was stored to. Note, the +// value is only reported if the allocation occurred in the same function as +// the leak. static std::pair GetAllocationSite(ProgramStateManager& StateMgr, const ExplodedNode *N, SymbolRef Sym) { - - // Find both first node that referred to the tracked symbol and the - // memory location that value was store to. const ExplodedNode *Last = N; const MemRegion* FirstBinding = 0; + const LocationContext *LeakContext = N->getLocationContext(); while (N) { ProgramStateRef St = N->getState(); @@ -2134,10 +2136,20 @@ GetAllocationSite(ProgramStateManager& StateMgr, const ExplodedNode *N, StateMgr.iterBindings(St, FB); if (FB) FirstBinding = FB.getRegion(); - Last = N; + // Allocation node, is the last node in the current context in which the + // symbol was tracked. + if (N->getLocationContext() == LeakContext) + Last = N; + N = N->pred_empty() ? NULL : *(N->pred_begin()); } + // If allocation happened in a function different from the leak node context, + // do not report the binding. + if (N->getLocationContext() != LeakContext) { + FirstBinding = 0; + } + return std::make_pair(Last, FirstBinding); } diff --git a/test/Analysis/retain-release-inline.m b/test/Analysis/retain-release-inline.m index bfb00776ec..0323374a14 100644 --- a/test/Analysis/retain-release-inline.m +++ b/test/Analysis/retain-release-inline.m @@ -253,6 +253,19 @@ extern CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void); + (id)array; @end +enum { + NSASCIIStringEncoding = 1, + NSNEXTSTEPStringEncoding = 2, + NSJapaneseEUCStringEncoding = 3, + NSUTF8StringEncoding = 4, + NSISOLatin1StringEncoding = 5, + NSSymbolStringEncoding = 6, + NSNonLossyASCIIStringEncoding = 7, +}; +typedef struct __CFString * CFMutableStringRef; +typedef NSUInteger NSStringEncoding; + +extern CFStringRef CFStringCreateWithCStringNoCopy(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding, CFAllocatorRef contentsDeallocator); //===----------------------------------------------------------------------===// // Test cases. @@ -285,13 +298,19 @@ void test_neg() { // Test returning retained and not-retained values. //===----------------------------------------------------------------------===// -id test_return_retained() { - return [[NSString alloc] init]; // expected-warning {{leak}} +// On return (intraprocedural), assume CF objects are leaked. +CFStringRef test_return_ratained_CF(char *bytes) { + CFStringRef str; + return CFStringCreateWithCStringNoCopy(0, bytes, NSNEXTSTEPStringEncoding, 0); // expected-warning {{leak}} +} + +// On return (intraprocedural), assume NSObjects are not leaked. +id test_return_retained_NS() { + return [[NSString alloc] init]; // no-warning } void test_test_return_retained() { - id x = test_return_retained(); + id x = test_return_retained_NS(); // expected-warning {{leak}} [x retain]; [x release]; } -