1// UNSUPPORTED: system-windows
2// RUN: %clang_analyze_cc1 -fobjc-arc -analyzer-checker=core,osx.cocoa.RunLoopAutoreleaseLeak %s -triple x86_64-darwin -verify
3// RUN: %clang_analyze_cc1 -DEXTRA=1 -DAP1=1 -fobjc-arc -analyzer-checker=core,osx.cocoa.RunLoopAutoreleaseLeak %s -triple x86_64-darwin -verify
4// RUN: %clang_analyze_cc1 -DEXTRA=1 -DAP2=1 -fobjc-arc -analyzer-checker=core,osx.cocoa.RunLoopAutoreleaseLeak %s -triple x86_64-darwin -verify
5// RUN: %clang_analyze_cc1 -DEXTRA=1 -DAP3=1 -fobjc-arc -analyzer-checker=core,osx.cocoa.RunLoopAutoreleaseLeak %s -triple x86_64-darwin -verify
6// RUN: %clang_analyze_cc1 -DEXTRA=1 -DAP4=1 -fobjc-arc -analyzer-checker=core,osx.cocoa.RunLoopAutoreleaseLeak %s -triple x86_64-darwin -verify
7
8#include "../Inputs/system-header-simulator-for-objc-dealloc.h"
9
10#ifndef EXTRA
11
12void just_runloop() { // No warning: no statements in between
13  @autoreleasepool {
14    [[NSRunLoop mainRunLoop] run]; // no-warning
15  }
16}
17
18void just_xpcmain() { // No warning: no statements in between
19  @autoreleasepool {
20    xpc_main(); // no-warning
21  }
22}
23
24void runloop_init_before() { // Warning: object created before the loop.
25  @autoreleasepool {
26    NSObject *object = [[NSObject alloc] init]; // expected-warning{{Temporary objects allocated in the autorelease pool followed by the launch of main run loop may never get released; consider moving them to a separate autorelease pool}}
27    (void) object;
28    [[NSRunLoop mainRunLoop] run];
29  }
30}
31
32void runloop_init_before_separate_pool() { // No warning: separate autorelease pool.
33  @autoreleasepool {
34    NSObject *object;
35    @autoreleasepool {
36      object = [[NSObject alloc] init]; // no-warning
37    }
38    (void) object;
39    [[NSRunLoop mainRunLoop] run];
40  }
41}
42
43void xpcmain_init_before() { // Warning: object created before the loop.
44  @autoreleasepool {
45    NSObject *object = [[NSObject alloc] init]; // expected-warning{{Temporary objects allocated in the autorelease pool followed by the launch of xpc_main may never get released; consider moving them to a separate autorelease pool}}
46    (void) object;
47    xpc_main();
48  }
49}
50
51void runloop_init_before_two_objects() { // Warning: object created before the loop.
52  @autoreleasepool {
53    NSObject *object = [[NSObject alloc] init]; // expected-warning{{Temporary objects allocated in the autorelease pool followed by the launch of main run loop may never get released; consider moving them to a separate autorelease pool}}
54    NSObject *object2 = [[NSObject alloc] init]; // no-warning, warning on the first one is enough.
55    (void) object;
56    (void) object2;
57    [[NSRunLoop mainRunLoop] run];
58  }
59}
60
61void runloop_no_autoreleasepool() {
62  NSObject *object = [[NSObject alloc] init]; // no-warning
63  (void)object;
64  [[NSRunLoop mainRunLoop] run];
65}
66
67void runloop_init_after() { // No warning: objects created after the loop
68  @autoreleasepool {
69    [[NSRunLoop mainRunLoop] run];
70    NSObject *object = [[NSObject alloc] init]; // no-warning
71    (void) object;
72  }
73}
74
75void no_crash_on_empty_children() {
76  @autoreleasepool {
77    for (;;) {}
78    NSObject *object = [[NSObject alloc] init]; // expected-warning{{Temporary objects allocated in the autorelease pool followed by the launch of main run loop may never get released; consider moving them to a separate autorelease pool}}
79    [[NSRunLoop mainRunLoop] run];
80    (void) object;
81  }
82}
83
84#endif
85
86#ifdef AP1
87int main() {
88    NSObject *object = [[NSObject alloc] init]; // expected-warning{{Temporary objects allocated in the autorelease pool of last resort followed by the launch of main run loop may never get released; consider moving them to a separate autorelease pool}}
89    (void) object;
90    [[NSRunLoop mainRunLoop] run];
91    return 0;
92}
93#endif
94
95#ifdef AP2
96// expected-no-diagnostics
97int main() {
98  NSObject *object = [[NSObject alloc] init]; // no-warning
99  (void) object;
100  @autoreleasepool {
101    [[NSRunLoop mainRunLoop] run];
102  }
103  return 0;
104}
105#endif
106
107#ifdef AP3
108// expected-no-diagnostics
109int main() {
110    [[NSRunLoop mainRunLoop] run];
111    NSObject *object = [[NSObject alloc] init]; // no-warning
112    (void) object;
113    return 0;
114}
115#endif
116
117#ifdef AP4
118int main() {
119    NSObject *object = [[NSObject alloc] init]; // expected-warning{{Temporary objects allocated in the autorelease pool of last resort followed by the launch of xpc_main may never get released; consider moving them to a separate autorelease pool}}
120    (void) object;
121    xpc_main();
122    return 0;
123}
124#endif
125