Home | History | Annotate | Download | only in Analysis
      1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount -analyzer-store=region -Wno-objc-root-class -verify %s
      2 // expected-no-diagnostics
      3 
      4 
      5 //===----------------------------------------------------------------------===//
      6 // The following code is reduced using delta-debugging from
      7 // Foundation.h (Mac OS X).
      8 //
      9 // It includes the basic definitions for the test cases below.
     10 // Not directly including Foundation.h directly makes this test case 
     11 // both svelte and portable to non-Mac platforms.
     12 //===----------------------------------------------------------------------===//
     13 
     14 typedef const void * CFTypeRef;
     15 typedef const struct __CFString * CFStringRef;
     16 typedef const struct __CFAllocator * CFAllocatorRef;
     17 extern const CFAllocatorRef kCFAllocatorDefault;
     18 extern CFTypeRef CFRetain(CFTypeRef cf);
     19 void CFRelease(CFTypeRef cf);
     20 typedef const struct __CFDictionary * CFDictionaryRef;
     21 const void *CFDictionaryGetValue(CFDictionaryRef theDict, const void *key);
     22 extern CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...);
     23 typedef signed char BOOL;
     24 typedef int NSInteger;
     25 typedef unsigned int NSUInteger;
     26 typedef struct objc_selector *SEL;
     27 @class NSString, Protocol;
     28 extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
     29 typedef NSInteger NSComparisonResult;
     30 typedef struct _NSZone NSZone;
     31 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
     32 @protocol NSObject
     33 - (BOOL)isEqual:(id)object;
     34 - (oneway void)release;
     35 - (Class)class;
     36 - (id)retain;
     37 @end
     38 @protocol NSCopying
     39 - (id)copyWithZone:(NSZone *)zone;
     40 @end
     41 @protocol NSMutableCopying
     42 - (id)mutableCopyWithZone:(NSZone *)zone;
     43 @end
     44 @protocol NSCoding
     45 - (void)encodeWithCoder:(NSCoder *)aCoder;
     46 @end
     47 @interface NSObject <NSObject> {}
     48 - (id)init;
     49 + (id)alloc;
     50 + (Class)class;
     51 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
     52 @end
     53 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
     54 typedef struct {} NSFastEnumerationState;
     55 @protocol NSFastEnumeration
     56 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
     57 @end
     58 @class NSString;
     59 typedef struct _NSRange {} NSRange;
     60 @interface NSArray : NSObject <NSCopying, NSMutableCopying, NSCoding, NSFastEnumeration>
     61 - (NSUInteger)count;
     62 @end
     63 @interface NSMutableArray : NSArray
     64 - (void)addObject:(id)anObject;
     65 - (id)initWithCapacity:(NSUInteger)numItems;
     66 @end
     67 typedef unsigned short unichar;
     68 @class NSData, NSArray, NSDictionary, NSCharacterSet, NSData, NSURL, NSError, NSLocale;
     69 typedef NSUInteger NSStringCompareOptions;
     70 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>    - (NSUInteger)length;
     71 - (NSComparisonResult)compare:(NSString *)string;
     72 - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
     73 - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
     74 - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale;
     75 - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
     76 - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
     77 @end
     78 @interface NSSimpleCString : NSString {} @end
     79 @interface NSConstantString : NSSimpleCString @end
     80 extern void *_NSConstantStringClassReference;
     81 
     82 //===----------------------------------------------------------------------===//
     83 // Test cases.
     84 //===----------------------------------------------------------------------===//
     85 
     86 //  <rdar://problem/6062730>
     87 // The analyzer doesn't perform any inter-procedural analysis, so delegates
     88 // involving [NSObject performSelector...] tend to lead to false positives.
     89 // For now the analyzer just stops tracking the reference count of the
     90 // receiver until we have better support for delegates.
     91 
     92 @interface test_6062730 : NSObject
     93 + (void)postNotification:(NSString *)str;
     94 - (void)foo;
     95 - (void)bar;
     96 @end
     97 
     98 @implementation test_6062730
     99 - (void) foo {
    100   NSString *str = [[NSString alloc] init]; // no-warning
    101   [test_6062730 performSelectorOnMainThread:@selector(postNotification:) withObject:str waitUntilDone:1];
    102 }
    103 
    104 - (void) bar {
    105   NSString *str = [[NSString alloc] init]; // no-warning
    106   [[self class] performSelectorOnMainThread:@selector(postNotification:) withObject:str waitUntilDone:1];
    107 }
    108 
    109 + (void) postNotification:(NSString *)str {
    110   [str release]; // no-warning
    111 }
    112 @end
    113 
    114 
    115 @interface ObjectThatRequiresDelegate : NSObject
    116 - (id)initWithDelegate:(id)delegate;
    117 - (id)initWithNumber:(int)num delegate:(id)delegate;
    118 @end
    119 
    120 
    121 @interface DelegateRequirerTest
    122 @end
    123 @implementation DelegateRequirerTest
    124 
    125 - (void)test {
    126   (void)[[ObjectThatRequiresDelegate alloc] initWithDelegate:self];
    127   (void)[[ObjectThatRequiresDelegate alloc] initWithNumber:0 delegate:self];
    128   // no leak warnings -- these objects could be released in callback methods
    129 }
    130 
    131 @end
    132