[cfe-commits] r151661 - in /cfe/trunk: lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp test/Analysis/retain-release-inline.m

Anna Zaks ganna at apple.com
Tue Feb 28 14:39:22 PST 2012


Author: zaks
Date: Tue Feb 28 16:39:22 2012
New Revision: 151661

URL: http://llvm.org/viewvc/llvm-project?rev=151661&view=rev
Log:
[analyzer] Leaks should be uniqued by the allocation point in the
closest function context (RetainCountChecker).

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp
    cfe/trunk/test/Analysis/retain-release-inline.m

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp?rev=151661&r1=151660&r2=151661&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp Tue Feb 28 16:39:22 2012
@@ -2114,14 +2114,16 @@
   };
 }
 
+// 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<const ExplodedNode*,const MemRegion*>
 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 @@
     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);
 }
 

Modified: cfe/trunk/test/Analysis/retain-release-inline.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/retain-release-inline.m?rev=151661&r1=151660&r2=151661&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/retain-release-inline.m (original)
+++ cfe/trunk/test/Analysis/retain-release-inline.m Tue Feb 28 16:39:22 2012
@@ -253,6 +253,19 @@
 + (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 @@
 // 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];
 }
-





More information about the cfe-commits mailing list