]> granicus.if.org Git - clang/commitdiff
Teach the static analyzer that NSLog() and friends do not hold on to object reference...
authorTed Kremenek <kremenek@apple.com>
Tue, 8 May 2012 00:12:09 +0000 (00:12 +0000)
committerTed Kremenek <kremenek@apple.com>
Tue, 8 May 2012 00:12:09 +0000 (00:12 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156346 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 325a7657e616f9707a4a5008809a502bb652163b..aa392b08608f406f06f0bd87be452d031d3c5f61 100644 (file)
@@ -648,6 +648,10 @@ public:
     return getPersistentSummary(Summ);
   }
 
+  const RetainSummary *getDoNothingSummary() {
+    return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
+  }
+  
   const RetainSummary *getDefaultSummary() {
     return getPersistentSummary(RetEffect::MakeNoRet(),
                                 DoNothing, MayEscape);
@@ -997,6 +1001,8 @@ RetainSummaryManager::getSummary(const FunctionDecl *FD,
       // libdispatch finalizers.
       ScratchArgs = AF.add(ScratchArgs, 1, StopTracking);
       S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
+    } else if (FName.startswith("NSLog")) {
+      S = getDoNothingSummary();
     } else if (FName.startswith("NS") &&
                 (FName.find("Insert") != StringRef::npos)) {
       // Whitelist NSXXInsertXX, for example NSMapInsertIfAbsent, since they can
index fb89cc8114556f5453f17221437871f628ececaa..5422b8bb0fac58d09f3a5c26c8f63b295e0f5a16 100644 (file)
@@ -1784,3 +1784,17 @@ void test_objc_arrays() {
     }
 }
 
+// Test NSLog doesn't escape tracked objects.
+void rdar11400885(int y)
+{
+  @autoreleasepool {
+    NSString *printString;
+    if(y > 2)
+      printString = [[NSString alloc] init];
+    else
+      printString = [[NSString alloc] init];
+    NSLog(@"Once %@", printString);
+    [printString release];
+    NSLog(@"Again: %@", printString); // expected-warning {{Reference-counted object is used after it is released}}
+  }
+}