blob: ec4f19d2da3ba2a6819d44642d61a188043645e7 [file] [log] [blame]
Stephen Hinesef822542014-07-21 00:47:37 -07001// RUN: %clang_cc1 -analyze -analyzer-checker=core,osx.cocoa.RetainCount -verify -fblocks %s
2
3@class NSString;
4typedef long NSInteger;
5typedef unsigned char BOOL;
6@interface NSObject {}
7+(id)alloc;
8-(id)init;
9-(id)autorelease;
10-(id)copy;
11-(id)retain;
12@end
13@interface NSNumber : NSObject
14+ (NSNumber *)numberWithInteger:(NSInteger)value __attribute__((availability(ios,introduced=2.0)));
15@end
16
17NSInteger *inoutIntegerValueGlobal;
18NSInteger *inoutIntegerValueGlobal2;
19NSString *traitNameGlobal;
20static BOOL cond;
21
22static inline void reallyPerformAction(void (^integerHandler)(NSInteger *inoutIntegerValue, NSString *traitName)) {
23 integerHandler(inoutIntegerValueGlobal, traitNameGlobal);
24 integerHandler(inoutIntegerValueGlobal2,traitNameGlobal);
25}
26
27static inline BOOL performAction(NSNumber *(^action)(NSNumber *traitValue)) {
28 __attribute__((__blocks__(byref))) BOOL didFindTrait = 0;
29 reallyPerformAction(^(NSInteger *inoutIntegerValue,NSString *traitName) {
30
31 if (cond) {
32
33 NSNumber *traitValue = @(*inoutIntegerValue);
34
35 NSNumber *newTraitValue = action(traitValue);
36
37 if (traitValue != newTraitValue) {
38 *inoutIntegerValue = newTraitValue ? *inoutIntegerValue : *inoutIntegerValue;
39 }
40 didFindTrait = 1;
41 }
42
43 });
44 return didFindTrait;
45}
46
47void runTest() {
48 __attribute__((__blocks__(byref))) NSNumber *builtinResult = ((NSNumber *)0);
49 BOOL wasBuiltinTrait = performAction(^(NSNumber *traitValue) {
50 builtinResult = [traitValue retain]; // expected-warning {{Potential leak of an object}}
51
52 return traitValue;
53 });
54 if (wasBuiltinTrait) {
55 [builtinResult autorelease];
56 return;
57 } else {
58 return;
59 }
60}