blob: 13b28dcf5374eeb175b9fb502a12907b7ea21e0c [file] [log] [blame]
Ted Kremenekcdc3a892012-08-24 20:39:55 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.core -analyzer-checker=deadcode.DeadStores,osx.cocoa.RetainCount -fblocks -verify -Wno-objc-root-class %s
Andy Gibbs8e8fb3b2012-10-19 12:44:48 +00002// expected-no-diagnostics
Ted Kremenek03648652008-07-03 22:25:27 +00003
4typedef signed char BOOL;
5typedef unsigned int NSUInteger;
6typedef 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
12extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
13@interface NSValue : NSObject <NSCopying, NSCoding> - (void)getValue:(void *)value; @end
14typedef float CGFloat;
15typedef 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
26extern NSString *NSAlignmentBinding;
27
28// This test case was reported as a false positive due to a bug in the
Ted Kremenekade31952011-03-12 06:14:28 +000029// LiveVariables <-> deadcode.DeadStores interplay. We should not flag a warning
Ted Kremenek03648652008-07-03 22:25:27 +000030// here. The test case was reported in:
31// http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-July/002157.html
32void DeadStoreTest(NSObject *anObject) {
33 NSArray *keys;
34 if ((keys = [anObject exposedBindings]) && // no-warning
35 ([keys containsObject:@"name"] && [keys containsObject:@"icon"])) {}
36}
37
Ted Kremenek89132202010-02-23 21:19:33 +000038// 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.
42void rdar_7631278(NSObject *x) {
43 x = ((void*)0);
44}
Ted Kremenekf50595d2010-10-22 22:08:32 +000045
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.
49BOOL baz_rdar8527823();
50void 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
Ted Kremenekbf5c3ac2011-02-01 20:45:26 +000063
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
Ted Kremenek280cf142011-12-22 00:46:32 +000080// 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
86RDar10591355 *rdar10591355_aux();
87
88void rdar10591355() {
89 RDar10591355 *p = rdar10591355_aux();
90 ^{ (void) p.x; }();
91}
Anna Zaksf1db0c92012-05-15 23:12:53 +000092
93@interface Radar11059352_1 {
94@private
95 int *_pathString;
96}
97@property int *pathString;
98@end
99@interface Radar11059352 {
100@private
101Radar11059352_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
Stephen Hines651f13c2014-04-23 16:59:28 -0700112
113id test_objc_precise_lifetime_foo();
114void 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}