Home | History | Annotate | Download | only in Analysis
      1 // RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core -analyzer-checker=deadcode.DeadStores,osx.cocoa.RetainCount -fblocks -verify -Wno-objc-root-class %s
      2 // expected-no-diagnostics
      3 
      4 typedef signed char BOOL;
      5 typedef unsigned int NSUInteger;
      6 typedef struct _NSZone NSZone;
      7 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
      8 @protocol NSObject  - (BOOL)isEqual:(id)object; @end
      9 @protocol NSCopying  - (id)copyWithZone:(NSZone *)zone; @end
     10 @protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder; @end
     11 @interface NSObject <NSObject> {} @end
     12 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
     13 @interface NSValue : NSObject <NSCopying, NSCoding>  - (void)getValue:(void *)value; @end
     14 typedef float CGFloat;
     15 typedef struct _NSPoint {} NSRange;
     16 @interface NSValue (NSValueRangeExtensions)  + (NSValue *)valueWithRange:(NSRange)range;
     17 - (BOOL)containsObject:(id)anObject;
     18 @end
     19 @class NSURLAuthenticationChallenge;
     20 @interface NSResponder : NSObject <NSCoding> {} @end
     21 @class NSArray, NSDictionary, NSString;
     22 @interface NSObject (NSKeyValueBindingCreation)
     23 + (void)exposeBinding:(NSString *)binding;
     24 - (NSArray *)exposedBindings;
     25 @end
     26 extern NSString *NSAlignmentBinding;
     27 
     28 // This test case was reported as a false positive due to a bug in the
     29 // LiveVariables <-> deadcode.DeadStores interplay.  We should not flag a warning
     30 // here.  The test case was reported in:
     31 //  http://lists.llvm.org/pipermail/cfe-dev/2008-July/002157.html
     32 void DeadStoreTest(NSObject *anObject) {
     33   NSArray *keys;
     34   if ((keys = [anObject exposedBindings]) &&   // no-warning
     35       ([keys containsObject:@"name"] && [keys containsObject:@"icon"])) {}
     36 }
     37 
     38 // This test case was a false positive due to how clang models
     39 // pointer types and ObjC object pointer types differently.  Here
     40 // we don't warn about a dead store because 'nil' is assigned to
     41 // an object pointer for the sake of defensive programming.
     42 void rdar_7631278(NSObject *x) {
     43   x = ((void*)0);
     44 }
     45 
     46 // This test case issuing a bogus warning for the declaration of 'isExec'
     47 // because the compound statement for the @synchronized was being visited
     48 // twice by the LiveVariables analysis.
     49 BOOL baz_rdar8527823();
     50 void foo_rdar8527823();
     51 @interface RDar8527823
     52 - (void) bar_rbar8527823;
     53 @end
     54 @implementation RDar8527823
     55 - (void) bar_rbar8527823
     56 {
     57  @synchronized(self) {
     58    BOOL isExec = baz_rdar8527823(); // no-warning
     59    if (isExec) foo_rdar8527823();
     60  }
     61 }
     62 @end
     63 
     64 // Don't flag dead stores to assignments to self within a nested assignment.
     65 @interface Rdar7947686
     66 - (id) init;
     67 @end
     68 
     69 @interface Rdar7947686_B : Rdar7947686
     70 - (id) init;
     71 @end
     72 
     73 @implementation Rdar7947686_B
     74 - (id) init {
     75   id x = (self = [super init]); // no-warning
     76   return x;
     77 }
     78 @end
     79 
     80 // Don't flag dead stores when a variable is captured in a block used
     81 // by a property access.
     82 @interface RDar10591355
     83 @property (assign) int x;
     84 @end
     85 
     86 RDar10591355 *rdar10591355_aux();
     87 
     88 void rdar10591355() {
     89   RDar10591355 *p = rdar10591355_aux();
     90   ^{ (void) p.x; }();
     91 }
     92 
     93 @interface Radar11059352_1 {
     94 @private
     95     int *_pathString;
     96 }
     97 @property int *pathString;
     98 @end
     99 @interface Radar11059352 {
    100 @private
    101 Radar11059352_1 *_Path;
    102 }
    103 @end
    104 @implementation Radar11059352
    105 
    106 - (int*)usePath {
    107     Radar11059352_1 *xxxxx = _Path; // no warning
    108     int *wp = xxxxx.pathString;
    109     return wp;
    110 }
    111 @end
    112 
    113 id test_objc_precise_lifetime_foo();
    114 void test_objc_precise_lifetime() {
    115   __attribute__((objc_precise_lifetime)) id dead = test_objc_precise_lifetime_foo(); // no-warning
    116   dead = 0;
    117   dead = test_objc_precise_lifetime_foo(); // no-warning
    118   dead = 0;
    119 }
    120