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