Home | History | Annotate | Download | only in Analysis
      1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-store=region -analyzer-constraints=range -verify -fobjc-gc %s  -Wno-implicit-function-declaration
      2 
      3 //===----------------------------------------------------------------------===//
      4 // The following code is reduced using delta-debugging from
      5 // Foundation.h and CoreFoundation.h (Mac OS X).
      6 //
      7 // It includes the basic definitions for the test cases below.
      8 // Not directly including [Core]Foundation.h directly makes this test case 
      9 // both svelte and portable to non-Mac platforms.
     10 //===----------------------------------------------------------------------===//
     11 
     12 typedef const void * CFTypeRef;
     13 void CFRelease(CFTypeRef cf);
     14 CFTypeRef CFRetain(CFTypeRef cf);
     15 CFTypeRef CFMakeCollectable(CFTypeRef cf);
     16 typedef const struct __CFAllocator * CFAllocatorRef;
     17 typedef double CFTimeInterval;
     18 typedef CFTimeInterval CFAbsoluteTime;
     19 typedef const struct __CFDate * CFDateRef;
     20 extern CFDateRef CFDateCreate(CFAllocatorRef allocator, CFAbsoluteTime at);
     21 extern CFAbsoluteTime CFDateGetAbsoluteTime(CFDateRef theDate);
     22 typedef struct objc_object {} *id;
     23 typedef signed char BOOL;
     24 static __inline__ __attribute__((always_inline)) id NSMakeCollectable(CFTypeRef cf) { return 0; }
     25 @protocol NSObject  - (BOOL)isEqual:(id)object;
     26 - (oneway void)release;
     27 - (id)retain;
     28 @end
     29 @class NSArray;
     30 
     31 //===----------------------------------------------------------------------===//
     32 // Test cases.
     33 //===----------------------------------------------------------------------===//
     34 
     35 CFAbsoluteTime CFAbsoluteTimeGetCurrent();
     36 
     37 CFAbsoluteTime f1_use_after_release() {
     38   CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
     39   CFDateRef date = CFDateCreate(0, t);
     40   CFRetain(date);
     41   [NSMakeCollectable(date) release];
     42   CFDateGetAbsoluteTime(date); // no-warning
     43   CFRelease(date);
     44   t = CFDateGetAbsoluteTime(date);   // expected-warning{{Reference-counted object is used after it is released}}
     45   return t;
     46 }
     47 
     48 // The following two test cases verifies that CFMakeCollectable is a no-op
     49 // in non-GC mode and a "release" in GC mode.
     50 CFAbsoluteTime f2_use_after_release() {
     51   CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
     52   CFDateRef date = CFDateCreate(0, t);
     53   CFRetain(date);
     54   [(id) CFMakeCollectable(date) release];
     55   CFDateGetAbsoluteTime(date); // no-warning
     56   CFRelease(date);
     57   t = CFDateGetAbsoluteTime(date);   // expected-warning{{Reference-counted object is used after it is released}}
     58   return t;
     59 }
     60 
     61 CFAbsoluteTime f2_noleak() {
     62   CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
     63   CFDateRef date = CFDateCreate(0, t);
     64   CFRetain(date);
     65   [(id) CFMakeCollectable(date) release];
     66   CFDateGetAbsoluteTime(date); // no-warning
     67   t = CFDateGetAbsoluteTime(date);  // no-warning
     68   CFRelease(date); // no-warning
     69   return t;
     70 }
     71 
     72 void f3_leak_with_gc() {
     73   CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); // expected-warning 2 {{leak}}
     74   [[(id) date retain] release];
     75 }
     76 
     77 // The following test case verifies that we "stop tracking" a retained object
     78 // when it is passed as an argument to an implicitly defined function.
     79 CFAbsoluteTime f4() {
     80   CFAbsoluteTime t = CFAbsoluteTimeGetCurrent();
     81   CFDateRef date = CFDateCreate(0, t);
     82   CFRetain(date);
     83   some_implicitly_defined_function_stop_tracking(date); // no-warning
     84   return t;
     85 }
     86