1// RUN: %check_clang_tidy %s bugprone-infinite-loop %t -- -- -fblocks
2// RUN: %check_clang_tidy %s bugprone-infinite-loop %t -- -- -fblocks -fobjc-arc
3
4typedef __typeof(sizeof(int)) NSUInteger;
5
6@interface NSArray
7+(instancetype)alloc;
8-(instancetype)init;
9@property(readonly) NSUInteger count;
10-(void)addObject: (id)anObject;
11@end
12
13@interface I
14-(void) instanceMethod;
15+(void) classMethod;
16+(instancetype)alloc;
17-(instancetype)init;
18@end
19
20void plainCFunction() {
21  int i = 0;
22  int j = 0;
23  while (i < 10) {
24    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
25    j++;
26  }
27}
28
29@implementation I
30- (void)instanceMethod {
31  int i = 0;
32  int j = 0;
33  while (i < 10) {
34    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
35    j++;
36  }
37}
38
39+ (void)classMethod {
40  int i = 0;
41  int j = 0;
42  while (i < 10) {
43    // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: this loop is infinite; none of its condition variables (i) are updated in the loop body [bugprone-infinite-loop]
44    j++;
45  }
46}
47@end
48
49void testArrayCount() {
50  NSArray *arr = [[NSArray alloc] init];
51  NSUInteger max_count = 10;
52  while ([arr count] < max_count) {
53    // No warning. Array count is updated on every iteration.
54    [arr addObject: [[I alloc] init]];
55  }
56}
57
58void testArrayCountWithConstant() {
59  NSArray *arr = [[NSArray alloc] init];
60  while ([arr count] < 10) {
61    // No warning. Array count is updated on every iteration.
62    [arr addObject: [[I alloc] init]];
63  }
64}
65
66void testArrayCountProperty() {
67  NSArray *arr = [[NSArray alloc] init];
68  NSUInteger max_count = 10;
69  while (arr.count < max_count) {
70    // No warning. Array count is updated on every iteration.
71    [arr addObject: [[I alloc] init]];
72  }
73}
74
75void testArrayCountPropertyWithConstant() {
76  NSArray *arr = [[NSArray alloc] init];
77  while (arr.count < 10) {
78    // No warning. Array count is updated on every iteration.
79    [arr addObject: [[I alloc] init]];
80  }
81}
82
83@interface MyArray {
84  @public NSUInteger _count;
85}
86+(instancetype)alloc;
87-(instancetype)init;
88-(void)addObject: (id)anObject;
89
90-(void)populate;
91@end
92
93@implementation MyArray
94-(void)populate {
95  NSUInteger max_count = 10;
96  while (_count < max_count) {
97    // No warning. Array count is updated on every iteration.
98    [self addObject: [[I alloc] init]];
99  }
100}
101
102-(void)populateWithConstant {
103  while (_count < 10) {
104    // No warning. Array count is updated on every iteration.
105    [self addObject: [[I alloc] init]];
106  }
107}
108@end
109
110void testArrayCountIvar() {
111  MyArray *arr = [[MyArray alloc] init];
112  NSUInteger max_count = 10;
113  while (arr->_count < max_count) {
114    // No warning. Array count is updated on every iteration.
115    [arr addObject: [[I alloc] init]];
116  }
117}
118
119void testArrayCountIvarWithConstant() {
120  MyArray *arr = [[MyArray alloc] init];
121  while (arr->_count < 10) {
122    // No warning. Array count is updated on every iteration.
123    [arr addObject: [[I alloc] init]];
124  }
125}
126