#define NULL (void *)0
#define nil (id)0
#define CALLED_ONCE __attribute__((called_once))
#define NORETURN __attribute__((noreturn))
#define LIKELY(X) __builtin_expect(!!(X), 1)
#define UNLIKELY(X) __builtin_expect(!!(X), 0)
#define LIKELY_WITH_PROBA(X, P) __builtin_expect_with_probability(!!(X), 1, P)
#define UNLIKELY_WITH_PROBA(X, P) __builtin_expect_with_probability(!!(X), 0, P)
#define UNPRED(X) __builtin_unpredictable((long)(X))
@protocol NSObject
@end
@interface NSObject <NSObject>
- (instancetype)init;
- (id)copy;
- (id)class;
- autorelease;
@end
typedef unsigned int NSUInteger;
typedef struct {
} NSFastEnumerationState;
@interface NSArray <__covariant NSFastEnumeration>
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
@end
@interface NSMutableArray<ObjectType> : NSArray <ObjectType>
- addObject:anObject;
@end
@class NSString, Protocol;
extern void NSLog(NSString *format, ...);
typedef int group_t;
typedef struct dispatch_queue_s *dispatch_queue_t;
typedef void (^dispatch_block_t)(void);
extern dispatch_queue_t queue;
void dispatch_group_async(dispatch_queue_t queue,
group_t group,
dispatch_block_t block);
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
void escape(void (^callback)(void));
void escape_void(void *);
void indirect_call(void (^callback)(void) CALLED_ONCE);
void indirect_conv(void (^completionHandler)(void));
void filler(void);
void exit(int) NORETURN;
void double_call_one_block(void (^callback)(void) CALLED_ONCE) {
callback(); callback(); }
void double_call_one_block_parens(void (^callback)(void) CALLED_ONCE) {
(callback)(); (callback)(); }
void double_call_one_block_ptr(void (*callback)(void) CALLED_ONCE) {
callback(); callback(); }
void double_call_one_block_ptr_deref(void (*callback)(void) CALLED_ONCE) {
(*callback)(); (*callback)(); }
void multiple_call_one_block(void (^callback)(void) CALLED_ONCE) {
callback(); callback(); callback(); callback(); callback(); }
void double_call_branching_1(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
callback(); } else {
cond += 42;
}
callback(); }
void double_call_branching_2(int cond, void (^callback)(void) CALLED_ONCE) {
callback();
if (cond) {
callback(); } else {
cond += 42;
}
}
void double_call_branching_3(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
callback();
} else {
callback();
}
}
void double_call_branching_4(int cond1, int cond2, void (^callback)(void) CALLED_ONCE) {
if (cond1) {
cond2 = !cond2;
} else {
callback();
}
if (cond2) {
callback(); }
}
void double_call_loop(int counter, void (^callback)(void) CALLED_ONCE) {
while (counter > 0) {
counter--;
callback(); }
}
void never_called_trivial(void (^callback)(void) CALLED_ONCE) {
}
int never_called_branching(int x, void (^callback)(void) CALLED_ONCE) {
x -= 42;
if (x == 10) {
return 0;
}
return x + 15;
}
void escaped_one_block_1(void (^callback)(void) CALLED_ONCE) {
escape(callback); }
void escaped_one_block_2(void (^callback)(void) CALLED_ONCE) {
escape(callback); callback();
}
void escaped_one_path_1(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
escape(callback); } else {
callback();
}
}
void escaped_one_path_2(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
escape(callback); }
callback();
}
void escaped_one_path_3(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
escape(callback);
}
}
void escape_in_between_1(void (^callback)(void) CALLED_ONCE) {
callback(); escape(callback);
callback(); }
void escape_in_between_2(int cond, void (^callback)(void) CALLED_ONCE) {
callback(); if (cond) {
escape(callback);
}
callback(); }
void escape_in_between_3(int cond, void (^callback)(void) CALLED_ONCE) {
callback();
if (cond) {
escape(callback);
} else {
escape_void((__bridge void *)callback);
}
callback(); }
void escaped_as_void_ptr(void (^callback)(void) CALLED_ONCE) {
escape_void((__bridge void *)callback); }
void indirect_call_no_warning_1(void (^callback)(void) CALLED_ONCE) {
indirect_call(callback); }
void indirect_call_no_warning_2(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
indirect_call(callback);
} else {
callback();
}
}
void indirect_call_double_call(void (^callback)(void) CALLED_ONCE) {
indirect_call(callback); callback(); }
void indirect_call_within_direct_call(void (^callback)(void) CALLED_ONCE,
void (^meta)(void (^param)(void) CALLED_ONCE) CALLED_ONCE) {
meta(callback);
callback();
}
void block_call_1(void (^callback)(void) CALLED_ONCE) {
indirect_call( ^{
callback();
});
callback(); }
void block_call_2(void (^callback)(void) CALLED_ONCE) {
escape(^{
callback();
});
callback();
}
void block_call_3(int cond, void (^callback)(void) CALLED_ONCE) {
^{
if (cond) {
callback(); }
callback(); }(); }
void block_call_4(int cond, void (^callback)(void) CALLED_ONCE) {
^{
if (cond) {
escape(callback);
}
}(); }
void block_call_5(void (^outer)(void) CALLED_ONCE) {
^(void (^inner)(void) CALLED_ONCE) {
}(outer);
}
void block_with_called_once(void (^outer)(void) CALLED_ONCE) {
escape_void((__bridge void *)^(void (^inner)(void) CALLED_ONCE) {
inner(); inner(); });
outer(); outer(); }
void block_dispatch_call(int cond, void (^callback)(void) CALLED_ONCE) {
dispatch_async(queue, ^{
if (cond) callback();
});
}
void block_escape_call_1(int cond, void (^callback)(void) CALLED_ONCE) {
escape_void((__bridge void *)^{
if (cond) {
callback();
}
});
}
void block_escape_call_2(int cond, void (^callback)(void) CALLED_ONCE) {
escape_void((__bridge void *)^{
if (cond) {
callback(); }
callback(); });
}
void never_called_one_exit(int cond, void (^callback)(void) CALLED_ONCE) {
if (!cond) return;
callback();
}
void never_called_if_then_1(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) { } else {
callback();
}
}
void never_called_if_then_2(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) { (void)cond;
} else {
callback();
}
}
void never_called_if_else_1(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) { callback();
} else {
}
}
void never_called_if_else_2(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) { callback();
}
}
void never_called_two_ifs(int cond1, int cond2, void (^callback)(void) CALLED_ONCE) {
if (cond1) { if (cond2) { return;
}
callback();
}
}
void never_called_ternary_then(int cond, void (^other)(void), void (^callback)(void) CALLED_ONCE) {
return cond ? other()
: callback();
}
void never_called_for_false(int size, void (^callback)(void) CALLED_ONCE) {
for (int i = 0; i < size; ++i) {
callback();
break;
}
}
void never_called_for_true(int size, void (^callback)(void) CALLED_ONCE) {
for (int i = 0; i < size; ++i) {
return;
}
callback();
}
void never_called_while_false(int cond, void (^callback)(void) CALLED_ONCE) {
while (cond) { callback();
break;
}
}
void never_called_while_true(int cond, void (^callback)(void) CALLED_ONCE) {
while (cond) { return;
}
callback();
}
void never_called_switch_case(int cond, void (^callback)(void) CALLED_ONCE) {
switch (cond) {
case 1:
callback();
break;
case 2:
callback();
break;
case 3: break;
default:
callback();
break;
}
}
void never_called_switch_default(int cond, void (^callback)(void) CALLED_ONCE) {
switch (cond) {
case 1:
callback();
break;
case 2:
callback();
break;
default: break;
}
}
void never_called_switch_two_cases(int cond, void (^callback)(void) CALLED_ONCE) {
switch (cond) {
case 1: break;
case 2: break;
default:
callback();
break;
}
}
void never_called_switch_none(int cond, void (^callback)(void) CALLED_ONCE) {
switch (cond) { case 1:
callback();
break;
case 2:
callback();
break;
}
}
enum YesNoOrMaybe {
YES,
NO,
MAYBE
};
void exhaustive_switch(enum YesNoOrMaybe cond, void (^callback)(void) CALLED_ONCE) {
switch (cond) {
case YES:
callback();
break;
case NO:
callback();
break;
case MAYBE:
callback();
break;
}
}
void called_twice_exceptions(void (^callback)(void) CALLED_ONCE) {
@try {
callback();
callback();
}
@finally {
callback();
}
}
void noreturn_1(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
exit(1);
} else {
callback();
}
}
void noreturn_2(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
callback();
exit(1);
} else {
callback();
}
}
void noreturn_3(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
exit(1);
}
callback();
}
void noreturn_4(void (^callback)(void) CALLED_ONCE) {
exit(1);
}
void noreturn_5(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
if (cond == 42) {
callback();
}
exit(1);
}
callback();
}
void never_called_noreturn_1(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
exit(1);
}
}
void double_call_noreturn(int cond, void (^callback)(void) CALLED_ONCE) {
callback();
if (cond) {
if (cond == 42) {
callback(); }
exit(1);
}
}
void call_with_check_1(void (^callback)(void) CALLED_ONCE) {
if (callback)
callback();
}
void call_with_check_2(void (^callback)(void) CALLED_ONCE) {
if (!callback) {
} else {
callback();
}
}
void call_with_check_3(void (^callback)(void) CALLED_ONCE) {
if (callback != NULL)
callback();
}
void call_with_check_4(void (^callback)(void) CALLED_ONCE) {
if (NULL != callback)
callback();
}
void call_with_check_5(void (^callback)(void) CALLED_ONCE) {
if (callback == NULL) {
} else {
callback();
}
}
void call_with_check_6(void (^callback)(void) CALLED_ONCE) {
if (NULL == callback) {
} else {
callback();
}
}
int call_with_check_7(int (^callback)(void) CALLED_ONCE) {
return callback ? callback() : 0;
}
void call_with_builtin_check_1(int (^callback)(void) CALLED_ONCE) {
if (LIKELY(callback))
callback();
}
void call_with_builtin_check_2(int (^callback)(void) CALLED_ONCE) {
if (!UNLIKELY(callback)) {
} else {
callback();
}
}
void call_with_builtin_check_3(int (^callback)(void) CALLED_ONCE) {
if (__builtin_expect((long)callback, 0L)) {
} else {
callback();
}
}
void call_with_builtin_check_4(int (^callback)(void) CALLED_ONCE) {
if (__builtin_expect(0L, (long)callback)) {
} else {
callback();
}
}
void call_with_builtin_check_5(int (^callback)(void) CALLED_ONCE) {
if (LIKELY_WITH_PROBA(callback, 0.9))
callback();
}
void call_with_builtin_check_6(int (^callback)(void) CALLED_ONCE) {
if (!UNLIKELY_WITH_PROBA(callback, 0.9)) {
} else {
callback();
}
}
void call_with_builtin_check_7(int (^callback)(void) CALLED_ONCE) {
if (UNPRED(callback)) {
} else {
callback();
}
}
void call_with_builtin_check_8(int (^callback)(void) CALLED_ONCE) {
if (LIKELY(callback != nil))
callback();
}
void call_with_builtin_check_9(int (^callback)(void) CALLED_ONCE) {
if (!UNLIKELY(callback == NULL))
callback();
}
void unreachable_true_branch(void (^callback)(void) CALLED_ONCE) {
if (0) {
} else {
callback();
}
}
void unreachable_false_branch(void (^callback)(void) CALLED_ONCE) {
if (1) {
callback();
}
}
void never_called_conv_1(void (^completionHandler)(void)) {
}
void never_called_conv_2(void (^completion)(void)) {
}
void never_called_conv_WithCompletion(void (^callback)(void)) {
}
void indirectly_called_conv(void (^completionHandler)(void)) {
indirect_conv(completionHandler);
}
void escape_through_assignment_1(void (^callback)(void) CALLED_ONCE) {
id escapee;
escapee = callback;
escape(escapee);
}
void escape_through_assignment_2(void (^callback)(void) CALLED_ONCE) {
id escapee = callback;
escape(escapee);
}
void escape_through_assignment_3(void (^callback1)(void) CALLED_ONCE,
void (^callback2)(void) CALLED_ONCE) {
id escapee1 = callback1, escapee2 = callback2;
escape(escapee1);
escape(escapee2);
}
void not_called_in_throw_branch_1(id exception, void (^callback)(void) CALLED_ONCE) {
if (exception) {
@throw exception;
}
callback();
}
void not_called_in_throw_branch_2(id exception, void (^callback)(void) CALLED_ONCE) {
if (exception) {
@throw exception;
}
}
void conventional_error_path_1(int error, void (^completionHandler)(void)) {
if (error) {
return;
}
completionHandler();
}
void conventional_error_path_2(int error, void (^callback)(void) CALLED_ONCE) {
if (error) {
return;
}
callback();
}
void suppression_1(void (^callback)(void) CALLED_ONCE) {
(void)callback; }
void suppression_2(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
(void)callback; } else {
callback();
}
}
void suppression_3(int cond, void (^callback)(void) CALLED_ONCE) {
if (cond) {
(void)callback;
}
}
@interface TestBase : NSObject
- (void)escape:(void (^)(void))callback;
- (void)indirect_call:(void (^)(void))CALLED_ONCE callback;
- (void)indirect_call_conv_1:(int)cond
completionHandler:(void (^)(void))completionHandler;
- (void)indirect_call_conv_2:(int)cond
completionHandler:(void (^)(void))handler;
- (void)indirect_call_conv_3WithCompletion:(void (^)(void))handler;
- (void)indirect_call_conv_4:(void (^)(void))handler
__attribute__((swift_async(swift_private, 1)));
- (void)exit:(int)code NORETURN;
- (int)condition;
@end
@interface TestClass : TestBase
@property(strong) NSMutableArray *handlers;
@property(strong) id storedHandler;
@property int wasCanceled;
@property(getter=hasErrors) int error;
@end
@implementation TestClass
- (void)double_indirect_call_1:(void (^)(void))CALLED_ONCE callback {
[self indirect_call:callback]; [self indirect_call:callback]; }
- (void)double_indirect_call_2:(void (^)(void))CALLED_ONCE callback {
[self indirect_call_conv_1:0 completionHandler:callback];
[self indirect_call_conv_1:1 completionHandler:callback];
}
- (void)double_indirect_call_3:(void (^)(void))completionHandler {
[self indirect_call_conv_2:0 completionHandler:completionHandler];
[self indirect_call_conv_2:1 completionHandler:completionHandler];
}
- (void)double_indirect_call_4:(void (^)(void))completion {
[self indirect_call_conv_2:0 completionHandler:completion];
[self indirect_call_conv_2:1 completionHandler:completion];
}
- (void)double_indirect_call_5:(void (^)(void))withCompletionHandler {
[self indirect_call_conv_2:0 completionHandler:withCompletionHandler];
[self indirect_call_conv_2:1 completionHandler:withCompletionHandler];
}
- (void)double_indirect_call_6:(void (^)(void))completionHandler {
[self indirect_call_conv_3WithCompletion: completionHandler];
[self indirect_call_conv_3WithCompletion: completionHandler];
}
- (void)double_indirect_call_7:(void (^)(void))completionHandler {
[self indirect_call_conv_4: completionHandler];
[self indirect_call_conv_4: completionHandler];
}
- (void)never_called_trivial:(void (^)(void))CALLED_ONCE callback {
filler();
}
- (void)noreturn:(int)cond callback:(void (^)(void))CALLED_ONCE callback {
if (cond) {
[self exit:1];
}
callback();
}
- (void)escaped_one_path:(int)cond callback:(void (^)(void))CALLED_ONCE callback {
if (cond) {
[self escape:callback]; } else {
callback();
}
}
- (void)block_call_1:(void (^)(void))CALLED_ONCE callback {
[self indirect_call:(^{ callback();
})];
callback(); }
- (void)block_call_2:(int)cond callback:(void (^)(void))CALLED_ONCE callback {
[self indirect_call:
^{
if (cond) {
[self escape:callback];
}
}];
}
- (void)block_call_3:(int)cond
completionHandler:(void (^)(void))callback {
[self indirect_call:
^{
if (cond) {
[self escape:callback];
}
}];
}
- (void)block_call_4WithCompletion:(void (^)(void))callback {
[self indirect_call:
^{
if ([self condition]) {
[self escape:callback];
}
}];
}
- (void)never_called_conv:(void (^)(void))completionHandler {
filler();
}
- (void)indirectly_called_conv:(void (^)(void))completionHandler {
indirect_conv(completionHandler);
}
- (void)never_called_one_exit_conv:(int)cond completionHandler:(void (^)(void))handler {
if (!cond) return;
handler();
}
- (void)escape_through_assignment:(void (^)(void))completionHandler {
_storedHandler = completionHandler;
}
- (void)escape_through_copy:(void (^)(void))completionHandler {
_storedHandler = [completionHandler copy];
}
- (void)escape_through_copy_and_autorelease:(void (^)(void))completionHandler {
_storedHandler = [[completionHandler copy] autorelease];
}
- (void)complex_escape:(void (^)(void))completionHandler {
if (completionHandler) {
[_handlers addObject:[[completionHandler copy] autorelease]];
}
}
- (void)test_crash:(void (^)(void))completionHandler cond:(int)cond {
if (cond) {
for (id _ in _handlers) {
}
[_handlers addObject:completionHandler];
}
}
- (void)conventional_error_path_1:(void (^)(void))completionHandler {
if (self.wasCanceled)
return;
completionHandler();
}
- (void)conventional_error_path_2:(void (^)(void))completionHandler {
if (self.wasCanceled)
return;
[_handlers addObject:completionHandler];
}
- (void)conventional_error_path_3:(void (^)(void))completionHandler {
if (self.hasErrors)
return;
completionHandler();
}
- (void)conventional_error_path_3:(int)cond completionHandler:(void (^)(void))handler {
if (self.wasCanceled)
return;
if (cond) {
handler();
}
}
#define NSAssert(condition, desc, ...) NSLog(desc, ##__VA_ARGS__);
- (void)empty_base_1:(void (^)(void))completionHandler {
NSAssert(0, @"Subclass must implement");
}
- (void)empty_base_2:(void (^)(void))completionHandler {
}
- (int)empty_base_3:(void (^)(void))completionHandler {
return 1;
}
- (int)empty_base_4:(void (^)(void))completionHandler {
NSAssert(0, @"Subclass must implement");
return 1;
}
- (int)empty_base_5:(void (^)(void))completionHandler {
NSAssert(0, @"%@ doesn't support", [self class]);
return 1;
}
#undef NSAssert
#define NSAssert(condition, desc, ...) \
if (!(condition)) { \
NSLog(desc, ##__VA_ARGS__); \
}
- (int)empty_base_6:(void (^)(void))completionHandler {
NSAssert(0, @"%@ doesn't support", [self class]);
return 1;
}
#undef NSAssert
#define NSAssert(condition, desc, ...) \
do { \
NSLog(desc, ##__VA_ARGS__); \
} while (0)
- (int)empty_base_7:(void (^)(void))completionHandler {
NSAssert(0, @"%@ doesn't support", [self class]);
return 1;
}
- (void)two_conditions_1:(int)first
second:(int)second
completionHandler:(void (^)(void))completionHandler {
if (first && second) {
completionHandler();
}
}
- (void)two_conditions_2:(int)first
second:(int)second
completionHandler:(void (^)(void))completionHandler {
if (first || second) {
return;
}
completionHandler();
}
- (void)testWithCompletionHandler:(void (^)(void))callback {
if ([self condition]) {
callback();
}
}
- (void)testWithCompletion:(void (^)(void))callback {
if ([self condition]) {
callback();
}
}
- (void)test:(int)cond fooWithReplyTo:(void (^)(void))handler {
if (cond) {
handler();
}
}
- (void)test:(int)cond with:(void (^)(void))fooWithCompletionBlock {
if (cond) {
fooWithCompletionBlock();
}
}
- (void)completion_handler_wrong_type:(int (^)(void))completionHandler {
if ([self condition]) {
completionHandler();
}
}
- (void)test_swift_async_none:(int)cond
completionHandler:(void (^)(void))handler __attribute__((swift_async(none))) {
if (cond) {
handler();
}
}
- (void)test_swift_async_param:(int)cond
callback:(void (^)(void))callback
__attribute__((swift_async(swift_private, 2))) {
if (cond) {
callback();
}
}
- (void)test_nil_suggestion:(int)cond1
second:(int)cond2
completion:(void (^)(void))handler {
if (cond1) {
handler();
}
if (cond2) {
handler(); }
}
- (void)test_nil_suppression_1:(int)cond1
second:(int)cond2
completion:(void (^)(void))handler {
if (cond1) {
handler();
handler = nil;
}
if (cond2) {
handler();
}
}
- (void)test_nil_suppression_2:(int)cond1
second:(int)cond2
completion:(void (^)(void))handler {
if (cond1) {
handler();
handler = NULL;
}
if (cond2) {
handler();
}
}
- (void)test_nil_suppression_3:(int)cond1
second:(int)cond2
completion:(void (^)(void))handler {
if (cond1) {
handler();
handler = 0;
}
if (cond2) {
handler();
}
}
- (void)test_escape_before_branch:(int)cond
withCompletion:(void (^)(void))handler {
if (cond) {
filler();
}
void (^copiedHandler)(void) = ^{
handler();
};
if (cond) {
handler();
} else {
copiedHandler();
}
}
- (void)test_escape_after_branch:(int)cond
withCompletion:(void (^)(void))handler {
if (cond) {
handler();
}
escape(handler);
}
typedef void (^DeferredBlock)(void);
static inline void DefferedCallback(DeferredBlock *inBlock) { (*inBlock)(); }
#define _DEFERCONCAT(a, b) a##b
#define _DEFERNAME(a) _DEFERCONCAT(__DeferredVar_, a)
#define DEFER __extension__ __attribute__((cleanup(DefferedCallback), unused)) \
DeferredBlock _DEFERNAME(__COUNTER__) = ^
- (void)test_cleanup_1:(int)cond
withCompletion:(void (^)(void))handler {
int error = 0;
DEFER {
if (error)
handler();
};
if (cond) {
error = 1;
} else {
handler();
}
}
- (void)test_cleanup_2:(int)cond
withCompletion:(void (^)(void))handler {
int error = 0;
DEFER {
if (error)
handler();
};
if (cond) {
error = 1;
} else {
handler(); }
handler(); }
- (void)initWithAdditions:(int)cond
withCompletion:(void (^)(void))handler {
self = [self init];
if (self) {
escape(handler);
}
}
@end