]> granicus.if.org Git - clang/commitdiff
[analyzer] Stop tracking the objects with attribute cleanup in the RetainCountChecker.
authorAnna Zaks <ganna@apple.com>
Tue, 17 Sep 2013 00:53:28 +0000 (00:53 +0000)
committerAnna Zaks <ganna@apple.com>
Tue, 17 Sep 2013 00:53:28 +0000 (00:53 +0000)
This suppresses false positive leaks. We stop tracking a value if it is assigned to a variable declared with a cleanup attribute.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190835 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/RetainCountChecker.cpp
test/Analysis/retain-release.m

index 7a97ce52c68731c61f692e97dfa4e6640d509c89..d2a4448a5fd71d90db3a9d77df38c15bac332711 100644 (file)
@@ -3356,6 +3356,16 @@ void RetainCountChecker::checkBind(SVal loc, SVal val, const Stmt *S,
     }
   }
 
+  // If we are storing the value into an auto function scope variable annotated
+  // with (__attribute__((cleanup))), stop tracking the value to avoid leak
+  // false positives.
+  if (const VarRegion *LVR = dyn_cast_or_null<VarRegion>(loc.getAsRegion())) {
+    const VarDecl *VD = LVR->getDecl();
+    if (VD->getAttr<CleanupAttr>()) {
+      escapes = true;
+    }
+  }
+
   // If our store can represent the binding and we aren't storing to something
   // that doesn't have local storage then just return and have the simulation
   // state continue as is.
index bb4a1d169d24107eedc596c4dded1aac5e48cac5..eda0c4ee78ea94dd1ba5ed4bcb9394a82fe913b2 100644 (file)
@@ -2022,6 +2022,20 @@ void rdar13783514(xpc_connection_t connection) {
   xpc_connection_set_finalizer_f(connection, releaseAfterXPC);
 } // no-warning
 
+// Do not report leaks when object is cleaned up with __attribute__((cleanup ..)).
+inline static void cleanupFunction(void *tp) {
+    CFTypeRef x = *(CFTypeRef *)tp;
+    if (x) {
+        CFRelease(x);
+    }
+}
+#define ADDCLEANUP __attribute__((cleanup(cleanupFunction)))
+void foo() {
+  ADDCLEANUP CFStringRef myString;
+  myString = CFStringCreateWithCString(0, "hello world", kCFStringEncodingUTF8);
+  ADDCLEANUP CFStringRef myString2 = 
+    CFStringCreateWithCString(0, "hello world", kCFStringEncodingUTF8);
+}
 
 // CHECK:  <key>diagnostics</key>
 // CHECK-NEXT:  <array>