1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core -analyzer-store=region -fblocks -analyzer-opt-analyze-nested-blocks -verify -Wno-strict-prototypes %s
2 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -analyzer-checker=core -analyzer-store=region -fblocks -analyzer-opt-analyze-nested-blocks -verify -x objective-c++ %s
4 //===----------------------------------------------------------------------===//
5 // The following code is reduced using delta-debugging from Mac OS X headers:
6 //===----------------------------------------------------------------------===//
8 typedef __builtin_va_list va_list;
9 typedef unsigned int uint32_t;
10 typedef struct dispatch_queue_s *dispatch_queue_t;
11 typedef struct dispatch_queue_attr_s *dispatch_queue_attr_t;
12 typedef void (^dispatch_block_t)(void);
13 void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
14 __attribute__((visibility("default"))) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__nothrow__)) dispatch_queue_t dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
15 typedef long dispatch_once_t;
16 void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
18 dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
21 typedef signed char BOOL;
22 typedef unsigned long NSUInteger;
23 typedef struct _NSZone NSZone;
24 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
26 - (BOOL)isEqual:(id)object;
27 - (oneway void)release;
29 @protocol NSCopying - (id)copyWithZone:(NSZone *)zone; @end
30 @protocol NSMutableCopying - (id)mutableCopyWithZone:(NSZone *)zone; @end
31 @protocol NSCoding - (void)encodeWithCoder:(NSCoder *)aCoder; @end
32 @interface NSObject <NSObject> {}
37 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
38 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
40 - (const char *)UTF8String;
41 - (id)initWithFormat:(NSString *)format arguments:(va_list)argList __attribute__((format(__NSString__, 1, 0)));
43 @class NSString, NSData;
44 typedef struct cssm_sample {} CSSM_SAMPLEGROUP, *CSSM_SAMPLEGROUP_PTR;
45 typedef struct __aslclient *aslclient;
46 typedef struct __aslmsg *aslmsg;
47 aslclient asl_open(const char *ident, const char *facility, uint32_t opts);
48 int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __attribute__((__format__ (__printf__, 4, 5)));
54 //===----------------------------------------------------------------------===//
55 // Begin actual test cases.
56 //===----------------------------------------------------------------------===//
58 // test1 - This test case exposed logic that caused the analyzer to crash because of a memory bug
59 // in BlockDataRegion. It represents real code that contains two block literals. Eventually
60 // via IPA 'logQueue' and 'client' should be updated after the call to 'dispatch_once'.
61 void test1(NSString *format, ...) {
62 static dispatch_queue_t logQueue;
63 static aslclient client;
64 static dispatch_once_t pred;
66 if (__builtin_expect(*(&pred), ~0l) != ~0l)
67 dispatch_once(&pred, ^{
68 logQueue = dispatch_queue_create("com.mycompany.myproduct.asl", 0);
69 client = asl_open(((char*)0), "com.mycompany.myproduct", 0);
74 __builtin_va_start(args, format);
76 NSString *str = [[NSString alloc] initWithFormat:format arguments:args];
77 dispatch_async(logQueue, ^{ asl_log(client, ((aslmsg)0), 4, "%s", [str UTF8String]); });
80 __builtin_va_end(args);
83 // test2 - Test that captured variables that are uninitialized are flagged
88 ^{ y = x + 1; }(); // expected-warning{{Variable 'x' is uninitialized when captured by block}}
94 ^{ y = x + 1; }(); // expected-warning {{left operand of '+' is a garbage value}}
98 typedef void (^myblock)(void);
99 myblock f = ^(void) { f(); }; // expected-warning{{Variable 'f' is uninitialized when captured by block}}
103 void testMessaging(void) {
104 // <rdar://problem/12119814>
105 [[^(void){} copy] release];
109 @interface rdar12415065 : NSObject
112 @implementation rdar12415065
114 // At one point this crashed because we created a path note at a
115 // PreStmtPurgeDeadSymbols point but only knew how to deal with PostStmt
116 // points. <rdar://problem/12687586>
118 extern dispatch_queue_t queue;
123 // This previously was a false positive with 'x' being flagged as being
124 // uninitialized when captured by the exterior block (when it is only
125 // captured by the interior block).
126 dispatch_async(queue, ^{
129 dispatch_async(queue, ^{ (void)x; });
136 void testReturnVariousSignatures(void) {
154 // This test used to cause infinite loop in the region invalidation.
155 void blockCapturesItselfInTheLoop(int x, int m) {
156 void (^assignData)(int) = ^(int x){
170 // Blocks that called the function they were contained in that also have
171 // static locals caused crashes.
172 // rdar://problem/21698099
173 void takeNonnullBlock(void (^)(void)) __attribute__((nonnull));
174 void takeNonnullIntBlock(int (^)(void)) __attribute__((nonnull));
176 void testCallContainingWithSignature1(void)
179 static const char str[] = "Lost connection to sharingd";
180 testCallContainingWithSignature1();
184 void testCallContainingWithSignature2(void)
186 takeNonnullBlock(^void{
187 static const char str[] = "Lost connection to sharingd";
188 testCallContainingWithSignature2();
192 void testCallContainingWithSignature3(void)
194 takeNonnullBlock(^void(void){
195 static const char str[] = "Lost connection to sharingd";
196 testCallContainingWithSignature3();
200 void testCallContainingWithSignature4(void)
202 takeNonnullBlock(^void(void){
203 static const char str[] = "Lost connection to sharingd";
204 testCallContainingWithSignature4();
208 void testCallContainingWithSignature5(void)
210 takeNonnullIntBlock(^{
211 static const char str[] = "Lost connection to sharingd";
212 testCallContainingWithSignature5();
217 __attribute__((objc_root_class))
218 @interface SuperClass
222 @interface SomeClass : SuperClass
225 // Make sure to properly handle super-calls when a block captures
226 // a local variable named 'self'.
227 @implementation SomeClass
229 /*__weak*/ SomeClass *weakSelf = self;
231 SomeClass *self = weakSelf;
234 [super someMethod]; // no-warning
240 // The incorrect block variable initialization below is a hard compile-time
242 #if !defined(__cplusplus)
243 void call_block_with_fewer_arguments(void) {
244 void (^b)() = ^(int a) { };
245 b(); // expected-warning {{Block taking 1 argument is called with fewer (0)}}
249 int getBlockFlags(void) {
251 return ((struct Block_layout *)^{ (void)x; })->flags; // no-warning