| 1 |
// RUN: %check_clang_tidy %s darwin-dispatch-once-nonstatic %t |
| 2 |
|
| 3 |
typedef int dispatch_once_t; |
| 4 |
extern void dispatch_once(dispatch_once_t *pred, void(^block)(void)); |
| 5 |
|
| 6 |
|
| 7 |
void bad_dispatch_once(dispatch_once_t once, void(^block)(void)) {} |
| 8 |
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: dispatch_once_t variables must have static or global storage duration; function parameters should be pointer references [darwin-dispatch-once-nonstatic] |
| 9 |
|
| 10 |
// file-scope dispatch_once_ts have static storage duration. |
| 11 |
dispatch_once_t global_once; |
| 12 |
static dispatch_once_t file_static_once; |
| 13 |
namespace { |
| 14 |
dispatch_once_t anonymous_once; |
| 15 |
} // end anonymous namespace |
| 16 |
|
| 17 |
int Correct(void) { |
| 18 |
static int value; |
| 19 |
static dispatch_once_t once; |
| 20 |
dispatch_once(&once, ^{ |
| 21 |
value = 1; |
| 22 |
}); |
| 23 |
return value; |
| 24 |
} |
| 25 |
|
| 26 |
int Incorrect(void) { |
| 27 |
static int value; |
| 28 |
dispatch_once_t once; |
| 29 |
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: dispatch_once_t variables must have static or global storage duration [darwin-dispatch-once-nonstatic] |
| 30 |
// CHECK-FIXES: static dispatch_once_t once; |
| 31 |
dispatch_once(&once, ^{ |
| 32 |
value = 1; |
| 33 |
}); |
| 34 |
return value; |
| 35 |
} |
| 36 |
|
| 37 |
struct OnceStruct { |
| 38 |
static dispatch_once_t staticOnce; // Allowed |
| 39 |
int value; |
| 40 |
dispatch_once_t once; // Allowed (at this time) |
| 41 |
}; |
| 42 |
|
| 43 |
@interface MyObject { |
| 44 |
dispatch_once_t _once; |
| 45 |
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: dispatch_once_t variables must have static or global storage duration and cannot be Objective-C instance variables [darwin-dispatch-once-nonstatic] |
| 46 |
// CHECK-FIXES: dispatch_once_t _once; |
| 47 |
} |
| 48 |
@end |