]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Analysis/blocks.m
Vendor import of clang trunk r178860:
[FreeBSD/FreeBSD.git] / test / Analysis / blocks.m
1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -analyze -analyzer-checker=core -analyzer-store=region -fblocks -analyzer-opt-analyze-nested-blocks -verify %s
2
3 //===----------------------------------------------------------------------===//
4 // The following code is reduced using delta-debugging from Mac OS X headers:
5 //===----------------------------------------------------------------------===//
6
7 typedef __builtin_va_list va_list;
8 typedef unsigned int uint32_t;
9 typedef struct dispatch_queue_s *dispatch_queue_t;
10 typedef struct dispatch_queue_attr_s *dispatch_queue_attr_t;
11 typedef void (^dispatch_block_t)(void);
12 void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
13 __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);
14 typedef long dispatch_once_t;
15 void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
16 typedef signed char BOOL;
17 typedef unsigned long NSUInteger;
18 typedef struct _NSZone NSZone;
19 @class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
20 @protocol NSObject
21 - (BOOL)isEqual:(id)object;
22 - (oneway void)release;
23 @end
24 @protocol NSCopying  - (id)copyWithZone:(NSZone *)zone; @end
25 @protocol NSMutableCopying  - (id)mutableCopyWithZone:(NSZone *)zone; @end
26 @protocol NSCoding  - (void)encodeWithCoder:(NSCoder *)aCoder; @end
27 @interface NSObject <NSObject> {}
28 + (id)alloc;
29 - (id)init;
30 - (id)copy;
31 @end
32 extern id NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone);
33 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSCoding>
34 - (NSUInteger)length;
35 - (const char *)UTF8String;
36 - (id)initWithFormat:(NSString *)format arguments:(va_list)argList __attribute__((format(__NSString__, 1, 0)));
37 @end
38 @class NSString, NSData;
39 typedef struct cssm_sample {} CSSM_SAMPLEGROUP, *CSSM_SAMPLEGROUP_PTR;
40 typedef struct __aslclient *aslclient;
41 typedef struct __aslmsg *aslmsg;
42 aslclient asl_open(const char *ident, const char *facility, uint32_t opts);
43 int asl_log(aslclient asl, aslmsg msg, int level, const char *format, ...) __attribute__((__format__ (__printf__, 4, 5)));
44
45 //===----------------------------------------------------------------------===//
46 // Begin actual test cases.
47 //===----------------------------------------------------------------------===//
48
49 // test1 - This test case exposed logic that caused the analyzer to crash because of a memory bug
50 //  in BlockDataRegion.  It represents real code that contains two block literals.  Eventually
51 //  via IPA 'logQueue' and 'client' should be updated after the call to 'dispatch_once'.
52 void test1(NSString *format, ...) {
53   static dispatch_queue_t logQueue;
54   static aslclient client;
55   static dispatch_once_t pred;
56   do {
57     if (__builtin_expect(*(&pred), ~0l) != ~0l)
58       dispatch_once(&pred, ^{
59         logQueue = dispatch_queue_create("com.mycompany.myproduct.asl", ((void*)0));
60         client = asl_open(((void*)0), "com.mycompany.myproduct", 0);
61       });
62   } while (0);
63
64   va_list args;
65   __builtin_va_start(args, format);
66
67   NSString *str = [[NSString alloc] initWithFormat:format arguments:args];
68   dispatch_async(logQueue, ^{ asl_log(client, ((void*)0), 4, "%s", [str UTF8String]); });
69   [str release];
70
71   __builtin_va_end(args);
72 }
73
74 // test2 - Test that captured variables that are uninitialized are flagged
75 // as such.
76 void test2() {
77   static int y = 0;
78   int x;
79   ^{ y = x + 1; }();  // expected-warning{{Variable 'x' is uninitialized when captured by block}}
80 }
81
82 void test2_b() {
83   static int y = 0;
84   __block int x;
85   ^{ y = x + 1; }(); // expected-warning {{left operand of '+' is a garbage value}}
86 }
87
88 void test2_c() {
89   typedef void (^myblock)(void);
90   myblock f = ^() { f(); }; // expected-warning{{Variable 'f' is uninitialized when captured by block}}
91 }
92
93
94 void testMessaging() {
95   // <rdar://problem/12119814>
96   [[^(){} copy] release];
97 }
98
99
100 @interface rdar12415065 : NSObject
101 @end
102
103 @implementation rdar12415065
104 - (void)test {
105   // At one point this crashed because we created a path note at a
106   // PreStmtPurgeDeadSymbols point but only knew how to deal with PostStmt
107   // points. <rdar://problem/12687586>
108
109   extern dispatch_queue_t queue;
110
111   if (!queue)
112     return;
113
114   // This previously was a false positive with 'x' being flagged as being
115   // uninitialized when captured by the exterior block (when it is only
116   // captured by the interior block).
117   dispatch_async(queue, ^{
118     double x = 0.0;
119     if (24.0f < x) {
120       dispatch_async(queue, ^{ (void)x; });
121       [self test];
122     }
123   });
124 }
125 @end